:root {
  --bg: #14110f; --panel: #241c15; --panel2: #31271d; --edge: #6e5534; --edge2: #4a3a26;
  --gold: #e8c060; --text: #f0e6d2; --muted: #b8a88c; --bad: #ff8a7a; --good: #9be686; --level: #ffd75a;
  --st: env(safe-area-inset-top); --sb: env(safe-area-inset-bottom); --sl: env(safe-area-inset-left); --sr: env(safe-area-inset-right);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; overscroll-behavior: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; font: 15px/1.4 -apple-system, system-ui, sans-serif; }
[hidden] { display: none !important; }
.title { font-family: "Palatino", "Palatino Linotype", "Book Antiqua", Georgia, serif; letter-spacing: .02em; }
.muted { color: var(--muted); }
.bad { color: var(--bad); } .good { color: var(--good); }
button { font: inherit; color: inherit; cursor: pointer; }
input { font-size: 16px; } /* evita lo zoom di iOS */

canvas#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; image-rendering: pixelated; }

#hud { position: fixed; top: calc(var(--st) + 8px); left: calc(var(--sl) + 8px); min-width: 170px; max-width: 58vw;
  background: rgba(20, 15, 11, .72); border: 1px solid var(--edge2); border-radius: 10px; padding: 7px 10px 6px; pointer-events: none; }
.hud-name { font-weight: 600; font-size: 14px; }
.hud-small { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { position: relative; height: 13px; background: #3a1414; border-radius: 7px; overflow: hidden; margin: 4px 0 3px; border: 1px solid #000; }
#hp-fill { height: 100%; width: 100%; background: linear-gradient(#e85a4a, #a8281e); transition: width .25s; }
#hp-text { position: absolute; inset: 0; font-size: 10px; line-height: 12px; text-align: center; font-weight: 700; text-shadow: 0 1px 1px #000; }

#zoom { position: fixed; top: calc(var(--st) + 8px); right: calc(var(--sr) + 8px); display: flex; flex-direction: column; gap: 6px; }
#zoom button { width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--edge2); background: rgba(20, 15, 11, .72); font-size: 20px; }

#log { position: fixed; left: calc(var(--sl) + 8px); bottom: calc(var(--sb) + 84px); width: min(440px, calc(100vw - 16px)); pointer-events: none;
  display: flex; flex-direction: column; gap: 3px; font-size: 13px; }
#log div { background: rgba(14, 10, 8, .7); padding: 3px 8px; border-radius: 6px; align-self: flex-start; transition: opacity 1.5s; text-shadow: 0 1px 1px #000; }
#log div.old { opacity: 0; }
#log .good { color: var(--good); } #log .bad { color: var(--bad); } #log .level { color: var(--level); font-weight: 600; } #log .sys { color: #9ac0e8; } #log .chat b { color: var(--gold); }

#buttons { position: fixed; right: calc(var(--sr) + 8px); bottom: calc(var(--sb) + 10px); left: calc(var(--sl) + 8px); display: flex; justify-content: flex-end; gap: 8px; pointer-events: none; }
#buttons button { pointer-events: auto; width: min(64px, 17.5vw); height: 62px; border-radius: 14px; border: 1px solid var(--edge); background: rgba(36, 28, 21, .9);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; box-shadow: 0 2px 6px rgba(0, 0, 0, .5); }
#buttons button span { font-size: 24px; line-height: 1; }
#buttons button small { font-size: 11px; color: var(--muted); }
#buttons button:active { transform: translateY(1px); background: var(--panel2); }

#chatbar { position: fixed; left: 0; right: 0; bottom: 0; display: flex; gap: 8px; padding: 8px calc(var(--sr) + 8px) calc(var(--sb) + 8px) calc(var(--sl) + 8px); background: var(--panel); border-top: 1px solid var(--edge); }
#chatbar input { flex: 1; min-width: 0; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--edge2); background: #120e0b; color: var(--text); }
#chatbar button { padding: 0 16px; border-radius: 10px; border: 1px solid var(--edge); background: var(--panel2); }

#sheet { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); display: flex; align-items: flex-end; justify-content: center; }
.sheet-inner { width: min(600px, 100%); max-height: min(82vh, 760px); display: flex; flex-direction: column; background: var(--panel);
  border: 2px solid var(--edge); border-bottom: 0; border-radius: 16px 16px 0 0; box-shadow: 0 -6px 30px rgba(0, 0, 0, .6); padding-bottom: var(--sb); }
#sheet header { display: flex; align-items: center; padding: 10px 12px 8px 16px; border-bottom: 1px solid var(--edge2); }
#sheet h2 { flex: 1; margin: 0; font-size: 20px; color: var(--gold); font-weight: 600; }
#sheet-close { width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--edge2); background: var(--panel2); font-size: 16px; }
#sheet-body { overflow-y: auto; padding: 12px 14px 16px; -webkit-overflow-scrolling: touch; user-select: none; }
#sheet-body h3 { margin: 14px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
#sheet-body h3:first-child { margin-top: 0; }

.slots { display: grid; grid-template-columns: repeat(auto-fill, minmax(58px, 1fr)); gap: 6px; }
.slot { position: relative; aspect-ratio: 1; border-radius: 9px; border: 1px solid var(--edge2); background: #191410; display: flex; align-items: center; justify-content: center; padding: 0; }
.slot canvas, .ico { image-rendering: pixelated; width: 40px; height: 40px; }
.slot .n { position: absolute; right: 4px; bottom: 2px; font-size: 12px; font-weight: 700; text-shadow: 0 1px 1px #000, 0 0 2px #000; }
.slot .q { position: absolute; left: 4px; top: 3px; width: 7px; height: 7px; border-radius: 50%; }
.slot.sel { border-color: var(--gold); box-shadow: inset 0 0 0 1px var(--gold); }
.slot.empty { opacity: .45; }
.equip { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.equip .slot { aspect-ratio: auto; height: 74px; flex-direction: column; gap: 2px; font-size: 11px; color: var(--muted); }
.statline { display: flex; gap: 14px; flex-wrap: wrap; margin: 8px 0 2px; font-size: 14px; }
.statline b { color: var(--gold); }
.detail { margin-top: 12px; padding: 10px 12px; border-radius: 10px; background: var(--panel2); border: 1px solid var(--edge2); }
.detail .name { font-weight: 600; font-size: 16px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.btn { padding: 9px 14px; border-radius: 10px; border: 1px solid var(--edge); background: #3a2e22; font-size: 14px; }
.btn:disabled { opacity: .4; }
.btn.primary, button.primary { background: linear-gradient(#c8963a, #8a6024); border-color: #e8c060; color: #1a120a; font-weight: 700; }
.btn.danger { border-color: #8a3a2a; color: var(--bad); }

.recipe { display: flex; gap: 10px; align-items: center; padding: 8px; border-radius: 10px; background: #1c1612; border: 1px solid var(--edge2); margin-bottom: 6px; }
.recipe.locked { opacity: .5; }
.recipe .info { flex: 1; min-width: 0; }
.recipe .rname { font-weight: 600; }
.recipe .req { font-size: 12px; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
.chip { font-size: 12px; padding: 1px 6px; border-radius: 6px; background: #2a2119; display: inline-flex; align-items: center; gap: 3px; }
.chip canvas { width: 16px; height: 16px; image-rendering: pixelated; }
.chip.miss { color: var(--bad); }
.recipe .go { display: flex; flex-direction: column; gap: 4px; }
.recipe .go .btn { padding: 6px 10px; font-size: 13px; }

.skill { padding: 8px 0; border-bottom: 1px solid var(--edge2); }
.skill .top { display: flex; justify-content: space-between; }
.skill .lvl { color: var(--gold); font-weight: 700; }
.skill .bar { height: 8px; background: #1a1410; margin-top: 4px; }
.skill .bar div { height: 100%; background: linear-gradient(#e8c060, #a8802a); }
.tabs { display: flex; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }
.tabs button { padding: 7px 12px; border-radius: 9px; border: 1px solid var(--edge2); background: #1c1612; font-size: 13px; }
.tabs button.on { border-color: var(--gold); color: var(--gold); }

.talk { display: flex; gap: 12px; align-items: flex-start; }
.talk canvas { width: 72px; height: 72px; image-rendering: pixelated; background: #191410; border-radius: 10px; border: 1px solid var(--edge2); flex: none; }
.talk p { margin: 0; font-family: "Palatino", Georgia, serif; font-size: 16px; line-height: 1.45; }
.keys { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.keys button { padding: 8px 12px; border-radius: 20px; border: 1px solid var(--edge); background: #3a2e22; font-size: 14px; }

#mapwrap { position: relative; }
#mapcv { width: 100%; height: min(64vh, 620px); border-radius: 10px; border: 1px solid var(--edge2); background: #0e0b09; display: block; touch-action: none; cursor: grab; }
.map-tasti { position: absolute; right: 8px; top: 8px; display: flex; flex-direction: column; gap: 6px; }
.map-tasti button { width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--edge); background: rgba(36, 28, 21, .92); font-size: 19px; }

#intro, #overlay, #entrata { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: radial-gradient(#2a2016, #0c0a08); padding: 20px; }
#overlay { background: rgba(8, 6, 4, .78); }
.intro-box, .overlay-box { width: min(380px, 100%); text-align: center; background: var(--panel); border: 2px solid var(--edge); border-radius: 16px; padding: 22px 20px; }
.intro-box h1 { color: var(--gold); margin: 0 0 6px; font-size: 28px; }
.intro-box label { display: block; text-align: left; font-size: 13px; color: var(--muted); margin-top: 10px; }
.intro-box input { display: block; width: 100%; margin-top: 4px; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--edge2); background: #120e0b; color: var(--text); user-select: text; -webkit-user-select: text; }
#intro-preview { width: 96px; height: 96px; image-rendering: pixelated; margin: 6px auto 0; display: block; }
#intro-colors { display: flex; justify-content: center; gap: 8px; margin: 14px 0 4px; flex-wrap: wrap; }
#intro-colors button { width: 32px; height: 32px; border-radius: 50%; border: 2px solid transparent; }
#intro-colors button.on { border-color: #fff; }
.intro-box .primary, .overlay-box .primary { width: 100%; padding: 13px; border-radius: 12px; margin-top: 8px; font-size: 16px; }

#entrata p { font-size: 15px; }
.nota-iphone { font-size: 13px !important; margin: 14px 0 0; }
.intro-box .bad:empty { display: none; }
.amico { padding: 10px 0; border-bottom: 1px solid var(--edge2); }
.amico .top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.amico .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.amico .online { color: var(--good); font-size: 12px; }
.nuovo-invito { display: flex; gap: 8px; }
.nuovo-invito input { flex: 1; min-width: 0; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--edge2); background: #120e0b; color: var(--text); user-select: text; -webkit-user-select: text; }
.qrbox { text-align: center; margin: 12px 0 4px; padding: 12px; border-radius: 12px; background: var(--panel2); border: 1px solid var(--gold); }
.qrbox img { width: min(240px, 70vw); image-rendering: pixelated; border-radius: 8px; background: #fff; }
.qrbox .link { font-size: 11px; word-break: break-all; color: var(--muted); margin: 8px 0; user-select: text; -webkit-user-select: text; }
#toast { position: fixed; left: 50%; transform: translateX(-50%); top: calc(var(--st) + 10px); background: var(--panel); border: 1px solid var(--gold); border-radius: 12px; padding: 8px 8px 8px 14px; display: flex; gap: 10px; align-items: center; box-shadow: 0 4px 20px #000; }
#toast button { padding: 6px 12px; border-radius: 8px; border: 0; background: var(--gold); color: #1a120a; font-weight: 700; }

@media (min-width: 900px) {
  #buttons { left: auto; }
  #sheet { align-items: center; }
  .sheet-inner { border-radius: 16px; border-bottom: 2px solid var(--edge); }
}
