:root {
  --bg: #14243a;
  --panel: rgba(12, 22, 36, 0.92);
  --line: rgba(255, 255, 255, 0.18);
  --fg: #eaf1f8;
  --dim: #9fb3c8;
  --accent: #ffe27a;
  --error: #ff8f8f;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--fg);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  /* È un gioco: niente scroll, zoom a pizzico, selezione o menu di pressione lunga */
  overscroll-behavior: none; touch-action: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
}
canvas#view { position: fixed; inset: 0; width: 100%; height: 100%; display: block; image-rendering: pixelated; }
.hidden { display: none !important; }

/* ---- HUD ---- */
#hud {
  position: fixed; top: calc(var(--safe-t) + 8px); left: calc(var(--safe-l) + 8px);
  display: none; align-items: center; gap: 6px; padding: 4px 4px 4px 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px; font-size: 13px;
}
body.playing #hud { display: flex; }
#hud button {
  all: unset; cursor: pointer; width: 28px; height: 28px; display: grid; place-items: center;
  border-radius: 50%; color: var(--dim); font-size: 16px;
}
#hud button:hover, #hud button:focus-visible { background: rgba(255, 255, 255, 0.12); color: var(--fg); }

#banner {
  position: fixed; top: calc(var(--safe-t) + 52px); left: 50%; transform: translateX(-50%);
  max-width: calc(100vw - 24px); padding: 6px 14px; border-radius: 999px; font-size: 13px; text-align: center;
  background: rgba(150, 60, 40, 0.92); border: 1px solid rgba(255, 255, 255, 0.25);
}
body:not(.playing) #banner { top: auto; bottom: calc(var(--safe-b) + 24px); background: var(--panel); }

/* ---- D-pad ---- */
#pad {
  position: fixed; left: calc(var(--safe-l) + 18px); bottom: calc(var(--safe-b) + 22px);
  width: 152px; height: 152px; border-radius: 50%; display: none; touch-action: none;
  background: rgba(10, 18, 30, 0.42); border: 2px solid rgba(255, 255, 255, 0.22);
}
body.playing.touch #pad { display: block; }
@media (pointer: coarse) { body.playing #pad { display: block; } }
#pad i {
  position: absolute; width: 44px; height: 44px; background: rgba(255, 255, 255, 0.5);
  clip-path: polygon(50% 18%, 88% 78%, 12% 78%); pointer-events: none;
}
#pad .up { left: 54px; top: 8px; }
#pad .down { left: 54px; bottom: 8px; transform: rotate(180deg); }
#pad .left { left: 8px; top: 54px; transform: rotate(-90deg); }
#pad .right { right: 8px; top: 54px; transform: rotate(90deg); }
#pad b { position: absolute; left: 62px; top: 62px; width: 28px; height: 28px; border-radius: 50%; background: rgba(255, 255, 255, 0.14); pointer-events: none; }
#pad[data-active=up] .up, #pad[data-active=down] .down, #pad[data-active=left] .left, #pad[data-active=right] .right { background: var(--accent); }

/* ---- Pannelli ---- */
.panel {
  position: fixed; z-index: 40; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: min(calc(100vw - 32px), 380px); max-height: calc(100% - 32px); overflow: auto;
  padding: 18px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
}
.panel h1 { margin: 0; font-size: 34px; letter-spacing: 2px; color: var(--accent); }
.panel h2 { margin: 0 0 6px; font-size: 18px; }
.tag, .hint { color: var(--dim); font-size: 13px; }
.tag { margin: 2px 0 16px; }
.hint { margin: 14px 0 0; }
label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--dim); }
input {
  width: 100%; padding: 11px 12px; border-radius: 8px; border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.07); color: var(--fg);
  font: inherit; font-size: 16px; /* 16px: evita lo zoom automatico di iOS al focus */
  -webkit-user-select: text; user-select: text;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.error { min-height: 1.4em; margin: 6px 0; color: var(--error); font-size: 13px; }
button.primary {
  width: 100%; padding: 12px; border: 0; border-radius: 8px; cursor: pointer;
  background: var(--accent); color: #2a2208; font: inherit; font-weight: 700;
}
button.primary:disabled { opacity: 0.55; cursor: default; }
.center-msg { text-align: center; }
#account { z-index: 45; } /* sopra l'ingresso (40) e i pannelli a comparsa */
#account label { margin-top: 10px; }

/* ---- Azioni, zaino e indicatori ---- */
#hud .chip { padding: 2px 8px; border-radius: 999px; font-size: 12px; color: var(--accent); background: rgba(255, 226, 122, 0.16); }

#bag-btn {
  position: fixed; top: calc(var(--safe-t) + 8px); right: calc(var(--safe-r) + 8px); z-index: 12; display: none;
  height: 36px; padding: 0 16px; border-radius: 999px; cursor: pointer;
  background: var(--panel); border: 1px solid var(--line); color: var(--fg); font: inherit; font-size: 14px; font-weight: 600;
}
body.playing #bag-btn { display: block; }
#help-btn {
  position: fixed; top: calc(var(--safe-t) + 8px); right: calc(var(--safe-r) + 92px); z-index: 12; display: none;
  width: 36px; height: 36px; border-radius: 50%; cursor: pointer; padding: 0;
  background: var(--panel); border: 1px solid var(--line); color: var(--accent); font: inherit; font-size: 17px; font-weight: 800;
}
body.playing #help-btn { display: block; }

#act {
  position: fixed; right: calc(var(--safe-r) + 22px); bottom: calc(var(--safe-b) + 44px); z-index: 12; display: none;
  width: 88px; height: 88px; border-radius: 50%; cursor: pointer; touch-action: none;
  background: rgba(255, 226, 122, 0.92); border: 3px solid rgba(255, 255, 255, 0.6); color: #2a2208;
  font: inherit; font-size: 15px; font-weight: 700; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  line-height: 1.1; padding: 0 6px; /* il nome cambia (Taglia, Scava ferro, Parla…): può andare su due righe */
}
#act:active { transform: scale(0.94); }
body.playing #act { display: block; }
body.sheet-open #act, body.sheet-open #pad { display: none !important; }

#toast, #progress, #placing {
  position: fixed; z-index: 14; background: var(--panel); border: 1px solid var(--line); font-size: 13px;
}
#toast {
  top: calc(var(--safe-t) + 100px); left: 50%; transform: translateX(-50%); max-width: calc(100vw - 24px);
  padding: 7px 14px; border-radius: 999px; text-align: center; border-color: rgba(255, 170, 120, 0.6);
}
#toast.ok { border-color: rgba(140, 220, 120, 0.7); }
#tip {
  position: fixed; z-index: 15; top: calc(var(--safe-t) + 150px); left: 50%; transform: translateX(-50%);
  width: min(92vw, 380px); padding: 10px 34px 10px 14px; border-radius: 14px; font-size: 13px; line-height: 1.4;
  background: rgba(26, 44, 66, 0.97); border: 1px solid var(--accent); box-shadow: 0 6px 18px rgba(0, 0, 0, 0.4);
}
#tip b { display: block; margin-bottom: 2px; font-size: 11px; letter-spacing: 1px; text-transform: uppercase; color: var(--accent); }
#tip i { position: absolute; top: 8px; right: 12px; font-style: normal; color: var(--dim); }
#progress {
  top: calc(var(--safe-t) + 150px); left: 50%; transform: translateX(-50%); width: min(70vw, 260px);
  padding: 7px 12px 10px; border-radius: 12px; text-align: center;
}
#progress i { display: block; height: 8px; margin-top: 6px; border-radius: 4px; overflow: hidden; background: rgba(255, 255, 255, 0.15); }
#progress b { display: block; height: 100%; width: 0; background: var(--accent); }
#placing {
  top: calc(var(--safe-t) + 96px); right: calc(var(--safe-r) + 8px); display: flex; align-items: center; gap: 4px;
  padding: 3px 4px 3px 12px; border-radius: 999px; background: rgba(46, 100, 40, 0.94);
}
#placing.hidden { display: none; }
#placing button { all: unset; cursor: pointer; width: 28px; height: 28px; display: grid; place-items: center; border-radius: 50%; }
#placing button:hover, #placing button:focus-visible { background: rgba(255, 255, 255, 0.18); }

/* Zaino: pannello dal basso sul telefono, colonna a destra sugli schermi larghi */
.sheet {
  position: fixed; z-index: 30; left: 0; right: 0; bottom: var(--kb, 0px); display: flex; flex-direction: column;
  max-height: min(78vh, 560px); padding-bottom: var(--safe-b); touch-action: pan-y;
  background: var(--panel); border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
}
/* niente !important qui: .hidden già vale */
.sheet header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px 4px; }
.sheet h2 { margin: 0; font-size: 18px; }
.sheet-close { all: unset; cursor: pointer; width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; font-size: 18px; }
.sheet-close:hover, .sheet-close:focus-visible { background: rgba(255, 255, 255, 0.12); }
.sheet-body { overflow-y: auto; overscroll-behavior: contain; padding: 0 16px 16px; touch-action: pan-y; -webkit-overflow-scrolling: touch; }
.sheet-body h3 { margin: 14px 0 8px; font-size: 12px; letter-spacing: 1px; text-transform: uppercase; color: var(--dim); }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.row { display: grid; grid-template-columns: 36px 1fr auto; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 10px; background: rgba(255, 255, 255, 0.06); }
.row .title { font-weight: 600; }
.row .qty { color: var(--accent); }
.row .sub { margin-top: 1px; font-size: 12px; color: var(--dim); line-height: 1.35; }
.row .sub.miss, .need.miss { color: var(--error); }
.need.ok { color: var(--fg); }
img.icon { width: 36px; height: 36px; image-rendering: pixelated; }
.group { margin-top: 6px; font-size: 12px; color: var(--accent); font-weight: 600; }
.btn {
  min-width: 64px; min-height: 40px; padding: 0 14px; border: 0; border-radius: 8px; cursor: pointer;
  background: var(--accent); color: #2a2208; font: inherit; font-size: 14px; font-weight: 700;
}
.btn:disabled { opacity: 0.4; cursor: default; }
@media (min-width: 700px) {
  .sheet { left: auto; top: 0; bottom: 0; width: 380px; max-height: none; border-radius: 0; border-top: 0; border-left: 1px solid var(--line); }
}

/* ---- Chat, cassa, scambio ---- */
#chat-btn { /* in alto a destra, sotto «Zaino» */
  position: fixed; top: calc(var(--safe-t) + 52px); right: calc(var(--safe-r) + 8px); z-index: 12; display: none;
  height: 36px; padding: 0 16px; border-radius: 999px; cursor: pointer;
  background: var(--panel); border: 1px solid var(--line); color: var(--fg); font: inherit; font-size: 14px; font-weight: 600;
}
body.playing #chat-btn { display: block; }
.badge {
  position: absolute; top: -6px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  background: #e0524f; color: #fff; font-size: 12px; line-height: 20px; text-align: center;
}
#chatlines {
  position: fixed; z-index: 11; top: calc(var(--safe-t) + 96px); left: calc(var(--safe-l) + 8px); width: min(70vw, 340px);
  display: grid; gap: 4px; pointer-events: none;
}
#chatlines .line, #chat-log .line { font-size: 13px; line-height: 1.35; overflow-wrap: anywhere; }
#chatlines .line { padding: 4px 10px; border-radius: 10px; background: rgba(12, 22, 36, 0.78); }
#chat-log { overflow-y: auto; overscroll-behavior: contain; padding: 4px 16px 8px; height: 34vh; min-height: 120px; touch-action: pan-y; -webkit-user-select: text; user-select: text; }
#chat-log .line { margin: 2px 0; }
.line b { color: var(--accent); }
.line b.me { color: #9be37a; }
.line.g::before { content: "(tutti) "; color: var(--dim); }
#chat-form { display: flex; gap: 8px; padding: 8px 16px 12px; }
#chat-form input { flex: 1; min-width: 0; }
.btn.ghost { background: rgba(255, 255, 255, 0.12); color: var(--fg); }
.btn.wide { width: 100%; margin-top: 12px; }
.btn.on { background: #9be37a; }
.btn-row { display: grid; gap: 8px; }
.btn-row .btn { margin-top: 0; }
.row .actions { display: flex; gap: 6px; }
.row .actions .btn { min-width: 44px; padding: 0 10px; }
#invite {
  position: fixed; z-index: 35; top: calc(var(--safe-t) + 52px); left: 50%; transform: translateX(-50%);
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px; width: max-content; max-width: calc(100vw - 24px);
  padding: 10px 14px; border-radius: 14px; background: var(--panel); border: 1px solid var(--accent); font-size: 14px;
}
#invite.hidden { display: none; }

/* ---- Vita ---- */
#hp {
  position: fixed; z-index: 12; top: calc(var(--safe-t) + 48px); left: calc(var(--safe-l) + 8px); display: none;
  width: min(40vw, 150px); height: 18px; border-radius: 9px; overflow: hidden;
  background: rgba(10, 18, 30, 0.8); border: 1px solid var(--line);
}
body.playing #hp { display: block; }
body.sheet-open #hp { display: none; }
#hp i { position: absolute; inset: 0; }
#hp b { display: block; height: 100%; width: 100%; background: #5cc05a; transition: width 0.25s, background 0.25s; }
#hp span { position: absolute; inset: 0; display: grid; place-items: center; font-size: 11px; font-weight: 700; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8); }
#hurt { position: fixed; inset: 0; z-index: 13; pointer-events: none; opacity: 0; box-shadow: inset 0 0 90px 24px rgba(224, 56, 52, 0.7); }
#hurt.on { animation: hurt 0.5s ease-out; }
@keyframes hurt { from { opacity: 1; } to { opacity: 0; } }
#fade { position: fixed; inset: 0; z-index: 13; pointer-events: none; opacity: 0; background: #05060a; }
#fade.on { animation: fade 0.55s ease-out; }
@keyframes fade { from { opacity: 1; } to { opacity: 0; } }

/* ---- Terreno di proprietà ---- */
#hud .chip.claim-mine { color: #9be37a; background: rgba(155, 227, 122, 0.16); }
#hud .chip.claim-other { color: #ffb070; background: rgba(255, 170, 90, 0.16); }

/* ---- Esperienza e livello ---- */
#xp {
  position: fixed; z-index: 12; top: calc(var(--safe-t) + 70px); left: calc(var(--safe-l) + 8px); display: none;
  width: min(40vw, 150px); height: 6px; border-radius: 3px; background: rgba(10, 18, 30, 0.8); border: 1px solid var(--line);
}
body.playing #xp { display: block; }
body.sheet-open #xp { display: none; }
#xp b { display: block; height: 100%; width: 0; border-radius: 3px; background: #f0c040; transition: width 0.4s; }
#xp-gain {
  position: absolute; left: calc(100% + 8px); top: -5px; font-size: 12px; font-weight: 700; white-space: nowrap; opacity: 0;
  color: #f0c040; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}
#xp-gain.on { animation: xpgain 1.6s ease-out; }
@keyframes xpgain { from { opacity: 1; transform: translateY(0); } to { opacity: 0; transform: translateY(-10px); } }
#levelup { position: fixed; inset: 0; z-index: 13; pointer-events: none; opacity: 0; box-shadow: inset 0 0 90px 24px rgba(240, 192, 64, 0.65); }
#levelup.on { animation: hurt 1s ease-out; }

/* ---- NPC: dialogo, missioni e negozio ---- */
.npc-text { margin: 4px 0 10px; font-style: italic; color: var(--fg); }
.coins { display: flex; align-items: center; gap: 8px; margin: 0 0 4px; font-weight: 700; color: var(--accent); }
.coins img.icon { width: 24px; height: 24px; }
.route-icon { font-size: 26px; text-align: center; }
.row .reward { margin-top: 3px; font-size: 12px; color: var(--accent); }
.row.done { opacity: 0.55; }
.row .price { color: var(--accent); font-weight: 600; }

/* ---- Effetti a tempo (sazio, coraggio, vigore) ---- */
#buffs { position: fixed; z-index: 12; top: calc(var(--safe-t) + 82px); left: calc(var(--safe-l) + 8px); display: none; flex-wrap: wrap; gap: 4px; max-width: 60vw; }
body.playing #buffs { display: flex; }
body.sheet-open #buffs { display: none; }
#buffs .chip { padding: 2px 8px; border-radius: 999px; font-size: 12px; background: rgba(10, 18, 30, 0.8); border: 1px solid var(--line); }
#hud-fed { color: var(--accent); }
#hud-voyage { color: #8fd3ff; }
#buff-coraggio { color: #ff9a96; }
#buff-vigore { color: #9be37a; }

/* ---- Manuale ---- */
.manual-section { margin: 8px 0; border-radius: 10px; background: rgba(255, 255, 255, 0.06); }
.manual-section summary { cursor: pointer; padding: 10px 12px; font-weight: 700; list-style: none; }
.manual-section summary::-webkit-details-marker { display: none; }
.manual-section summary::before { content: "›"; display: inline-block; width: 1.1em; color: var(--accent); transition: transform 0.15s; }
.manual-section[open] summary::before { transform: rotate(90deg); }
.manual-list { margin: 0; padding: 0 12px 12px 30px; font-size: 13.5px; line-height: 1.45; color: var(--fg); }
.manual-list li { margin: 7px 0; }
.manual-list b { color: var(--accent); }
.manual-list i { color: #cfe3f6; }
