:root {
  --notte: #0f3324;
  --foglia: #2f9e4f;
  --arancio: #ff8a1f;
  --arancio-scuro: #c2410c;
  --crema: #fff4dc;
  --inchiostro: #2a1406;
  --sx: env(safe-area-inset-left, 0px);
  --dx: env(safe-area-inset-right, 0px);
  --su: env(safe-area-inset-top, 0px);
  --giu: env(safe-area-inset-bottom, 0px);
  --font: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--notte); color: var(--crema);
  font-family: var(--font);
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  overscroll-behavior: none;
}
canvas#scena { position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh; display: block; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; border: 0; background: none; touch-action: manipulation; cursor: pointer; }

/* ——— HUD ——— */
/* sopra lo strato dei comandi: il tasto pausa deve ricevere il tocco */
#hud {
  position: fixed; inset: 0; z-index: 2; pointer-events: none;
  padding: calc(10px + var(--su)) calc(16px + var(--dx)) 0 calc(16px + var(--sx));
  display: flex; justify-content: space-between; align-items: flex-start;
}
.hud-sx, .hud-dx { display: flex; gap: 14px; align-items: center; }
#hud { padding-left: calc(20px + var(--sx)); }
.hud-sx { flex-direction: column; align-items: flex-start; gap: 4px; }
.contatore {
  display: flex; align-items: center; gap: 8px;
  font-weight: 900; font-size: 30px; letter-spacing: .5px;
  color: #fff; -webkit-text-stroke: 1.5px var(--inchiostro);
  text-shadow: 0 3px 0 var(--inchiostro), 0 4px 10px rgba(0,0,0,.35);
  transition: transform .15s;
}
.contatore.piccolo { font-size: 20px; }
.contatore.pulsa { transform: scale(1.25); }
.ico { display: inline-block; width: 30px; height: 30px; flex: none; }
.piccolo .ico { width: 20px; height: 20px; }
.ico-frutto {
  border-radius: 50% 50% 46% 46%;
  background: radial-gradient(circle at 35% 30%, #ffe08a 0 18%, #ffa21f 45%, #e8590c 100%);
  border: 2px solid var(--inchiostro);
  position: relative;
}
.ico-frutto::after { content: ""; position: absolute; width: 10px; height: 6px; background: #3fbf4a; border: 1.5px solid var(--inchiostro); border-radius: 0 70% 0 70%; top: -6px; left: 50%; }
.ico-cassa { background: #c98a46; border: 3px solid #7a4a1f; border-radius: 3px; background-image: linear-gradient(45deg, transparent 44%, #7a4a1f 44% 56%, transparent 56%), linear-gradient(-45deg, transparent 44%, #7a4a1f 44% 56%, transparent 56%); }
.ico-vita { border-radius: 50%; background: radial-gradient(circle at 50% 70%, #f6e3c4 0 28%, transparent 29%), #d9532b; border: 2px solid var(--inchiostro); position: relative; }
.ico-vita::before, .ico-vita::after { content: ""; position: absolute; top: -5px; width: 10px; height: 12px; background: #d9532b; border: 2px solid var(--inchiostro); border-bottom: 0; border-radius: 50% 50% 0 0; }
.ico-vita::before { left: 0; transform: rotate(-25deg); }
.ico-vita::after { right: 0; transform: rotate(25deg); }
.ico-tempo { border-radius: 50%; border: 3px solid var(--crema); position: relative; }
.ico-tempo::after { content: ""; position: absolute; left: 50%; top: 4px; width: 3px; height: 9px; background: var(--crema); transform-origin: bottom; }

.b-pausa {
  pointer-events: auto; width: 46px; height: 46px; border-radius: 14px;
  background: rgba(15, 51, 36, .55); border: 2px solid rgba(255,255,255,.5);
  display: flex; gap: 6px; align-items: center; justify-content: center;
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
.b-pausa i { display: block; width: 6px; height: 18px; border-radius: 2px; background: #fff; }

/* ——— comandi touch ——— */
#comandi { position: fixed; inset: 0; z-index: 1; touch-action: none; }
#sterzo {
  position: fixed; left: calc(130px + var(--sx)); top: calc(100% - 90px - var(--giu));
  width: 170px; height: 64px; margin: -32px 0 0 -85px; pointer-events: none;
  opacity: .55; transition: opacity .15s;
}
#sterzo.attivo { opacity: 1; transition: none; }
#sterzo .binario { position: absolute; left: 0; right: 0; top: 50%; height: 16px; margin-top: -8px; border-radius: 8px; background: rgba(255,255,255,.18); border: 2px solid rgba(255,255,255,.45); }
#sterzo-pomo {
  position: absolute; left: 50%; top: 50%; width: 60px; height: 60px; margin: -30px 0 0 -30px; border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, #fff, #d9e6e0); box-shadow: 0 4px 10px rgba(0,0,0,.3);
}
.tasto {
  position: fixed; border-radius: 50%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-weight: 900; font-size: 14px; letter-spacing: 1px; color: #fff; -webkit-text-stroke: .5px rgba(0,0,0,.4);
  border: 3px solid rgba(255,255,255,.7);
  box-shadow: 0 6px 0 rgba(0,0,0,.25), inset 0 -6px 0 rgba(0,0,0,.15);
  transition: transform .06s; pointer-events: none;
}
.tasto small { font-size: 9px; letter-spacing: .5px; opacity: .9; margin-top: 2px; }
.tasto.premuto { transform: scale(.9) translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,.25), inset 0 -3px 0 rgba(0,0,0,.15); }
.tasto.derapa { width: 112px; height: 112px; right: calc(24px + var(--dx)); bottom: calc(22px + var(--giu)); background: rgba(255, 138, 31, .82); font-size: 16px; }
.tasto.oggetto { width: 78px; height: 78px; right: calc(150px + var(--dx)); bottom: calc(92px + var(--giu)); background: rgba(58, 123, 255, .8); font-size: 11px; }
.tasto.freno { width: 58px; height: 58px; right: calc(158px + var(--dx)); bottom: calc(16px + var(--giu)); background: rgba(80, 80, 80, .7); font-size: 10px; }

/* ——— posizione, giro, oggetto, derapata ——— */
.posizione { font-weight: 900; color: #fff; -webkit-text-stroke: 2px var(--inchiostro); text-shadow: 0 4px 0 var(--inchiostro); line-height: 1; }
.posizione b { font-size: 54px; }
.posizione sup { font-size: 26px; vertical-align: top; }
.posizione small { font-size: 22px; margin-left: 2px; }
.riga-info { font-weight: 900; font-size: 18px; color: #fff; -webkit-text-stroke: 1px var(--inchiostro); text-shadow: 0 2px 0 var(--inchiostro); }
.riga-info span { font-size: 13px; opacity: .9; }
.riga-info.tempo { font-variant-numeric: tabular-nums; font-size: 16px; }
#minimappa { width: 120px; height: 120px; margin-top: 4px; opacity: .9; }
.slot {
  position: absolute; left: 50%; top: calc(8px + var(--su)); transform: translateX(-50%);
  width: 64px; height: 64px; border-radius: 16px; background: rgba(15,51,36,.55); border: 3px solid rgba(255,255,255,.7);
  display: flex; align-items: center; justify-content: center;
}
.slot svg { width: 46px; height: 46px; }
.slot.rullo { animation: rullo .12s steps(1) infinite; }
@keyframes rullo { 50% { border-color: #ffd43b; } }
.derapata {
  position: absolute; left: 50%; bottom: calc(14px + var(--giu)); transform: translateX(-50%);
  width: 230px; opacity: 0; transition: opacity .15s;
}
.derapata.su { opacity: 1; }
.barra { position: relative; height: 16px; border-radius: 8px; background: rgba(0,0,0,.45); border: 2px solid #fff; overflow: hidden; }
.zona { position: absolute; left: 70%; right: 0; top: 0; bottom: 0; background: rgba(255, 60, 30, .45); }
.riemp { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: #fff; }
.riemp.pronto { background: #ff3b1a; }
.derapata.bruciata .riemp { background: #555; width: 100% !important; }
.tacche { display: flex; gap: 8px; justify-content: center; margin-top: 6px; }
.tacche i { width: 16px; height: 16px; border-radius: 50%; border: 2px solid #fff; background: rgba(0,0,0,.35); }
.tacche i.si { background: #ffd43b; box-shadow: 0 0 10px #ffb02e; }
.conto {
  position: fixed; left: 50%; top: 40%; transform: translate(-50%, -50%); z-index: 2;
  font-weight: 900; font-size: 110px; color: #ffd43b; -webkit-text-stroke: 4px var(--inchiostro); text-shadow: 0 8px 0 var(--inchiostro);
  pointer-events: none; opacity: 0;
}
.conto.su { animation: conto .9s ease-out; }
@keyframes conto { 0% { opacity: 0; transform: translate(-50%, -50%) scale(2); } 15% { opacity: 1; transform: translate(-50%, -50%) scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -50%) scale(.9); } }
.classifica { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; min-width: 330px; }
.classifica li { display: flex; gap: 14px; align-items: center; padding: 6px 14px; border-radius: 12px; background: rgba(255,255,255,.12); font-weight: 800; font-size: 18px; }
.classifica li.tu { background: rgba(255,138,31,.55); }
.classifica li b { width: 34px; font-size: 22px; }
.classifica li span { flex: 1; text-align: left; }
.classifica li em { font-style: normal; font-variant-numeric: tabular-nums; opacity: .9; }
.rosso { color: #ff5a3c; }

/* ——— avvisi in gioco ——— */
.avviso {
  position: fixed; z-index: 2; left: 50%; top: 26%; transform: translate(-50%, -50%) scale(.6);
  font-weight: 900; font-size: 38px; color: #fff; -webkit-text-stroke: 2px var(--inchiostro);
  text-shadow: 0 4px 0 var(--inchiostro); opacity: 0; pointer-events: none; white-space: nowrap;
  transition: opacity .2s, transform .25s cubic-bezier(.3,1.6,.5,1);
}
.avviso.su { opacity: 1; transform: translate(-50%, -50%) scale(1); }

/* ——— schermate ——— */
.schermo {
  position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: calc(16px + var(--su)) calc(24px + var(--dx)) calc(16px + var(--giu)) calc(24px + var(--sx));
  text-align: center; gap: 14px; z-index: 3;
}
.velo { background: rgba(10, 30, 22, .62); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); }
.menu {
  display: grid; grid-template-columns: 1fr auto; grid-template-rows: 1fr auto; align-items: center;
  justify-items: start; text-align: left;
  background: linear-gradient(90deg, rgba(10,30,22,.6), rgba(10,30,22,0) 55%);
}
.menu .marchio { grid-column: 1; grid-row: 1; }
.menu .azioni { grid-column: 1; grid-row: 1; align-self: end; margin-bottom: 4px; display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }
.menu .guida { grid-column: 1 / span 2; grid-row: 2; margin: 0; font-size: 14px; opacity: .9; }
.marchio { align-self: start; margin-top: 6px; }
h1 {
  margin: 0; font-weight: 900; font-size: 64px; line-height: .88; letter-spacing: 1px;
  color: var(--arancio); -webkit-text-stroke: 3px var(--inchiostro);
  text-shadow: 0 6px 0 var(--inchiostro), 0 10px 22px rgba(0,0,0,.4);
  transform: rotate(-3deg); transform-origin: left;
}
h1 span { color: #ffd43b; font-size: 76px; }
h2 { margin: 0; font-weight: 900; font-size: 44px; color: #ffd43b; -webkit-text-stroke: 2px var(--inchiostro); text-shadow: 0 5px 0 var(--inchiostro); }
.sotto { margin: 8px 0 0; font-size: 16px; font-weight: 700; opacity: .95; text-shadow: 0 2px 4px rgba(0,0,0,.5); }
.bottone {
  min-width: 150px; padding: 12px 22px; border-radius: 16px; font-weight: 900; font-size: 18px;
  background: rgba(255,255,255,.14); border: 2px solid rgba(255,255,255,.55);
}
.bottone.grande {
  font-size: 26px; padding: 14px 40px; background: var(--arancio); color: #fff; border: 3px solid var(--inchiostro);
  -webkit-text-stroke: 1px var(--inchiostro);
  box-shadow: 0 6px 0 var(--inchiostro);
}
.bottone:active { transform: translateY(3px); }
.bottone.grande:active { box-shadow: 0 3px 0 var(--inchiostro); }
.colonna { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
.riga { display: flex; gap: 14px; align-items: center; }
.record { margin: 0; font-size: 14px; font-weight: 700; opacity: .9; }
.suggerimento { margin: 0; font-size: 13px; max-width: 360px; background: rgba(0,0,0,.35); padding: 6px 10px; border-radius: 10px; }
.riepilogo { display: flex; gap: 34px; }
.riepilogo div { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.riepilogo b { font-size: 30px; font-weight: 900; }
.riepilogo small { font-size: 13px; opacity: .8; }
.gemma { margin: 0; font-weight: 900; color: #ff9ff0; font-size: 18px; }

.ruota, .caricamento, .errore {
  position: fixed; inset: 0; z-index: 10; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--notte); text-align: center; padding: 24px;
}
.ruota { display: none; font-weight: 800; font-size: 20px; gap: 18px; }
.telefono { width: 46px; height: 82px; border: 4px solid var(--crema); border-radius: 12px; animation: gira 1.8s ease-in-out infinite; }
@keyframes gira { 0%, 25% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }
@media (orientation: portrait) and (max-width: 900px) { .ruota { display: flex; } }
.caricamento p { font-weight: 800; font-size: 18px; opacity: .9; }
.errore { z-index: 11; font-size: 15px; white-space: pre-wrap; }

@media (max-height: 360px) {
  h1 { font-size: 50px; } h1 span { font-size: 60px; }
  .tasto.salto { width: 92px; height: 92px; } .tasto.giro { width: 74px; height: 74px; right: calc(130px + var(--dx)); }
}
