:root {
  --ink: #07050a;
  --panel: #120a10;
  --cream: #f3ead7;
  --muted: #b9ac97;
  --gold: #ffcf4a;
  --gold-deep: #b57a18;
  --wine: #8d2633;
  --wine-deep: #4b0d17;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background:
    radial-gradient(120% 80% at 50% -10%, #2a0e18 0%, #12080e 42%, #07050a 78%),
    #07050a;
  color: var(--cream);
  font: 700 14px/1.3 ui-monospace, Menlo, Consolas, monospace;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    repeating-linear-gradient(0deg, rgba(255,255,255,.018) 0 1px, transparent 1px 3px),
    radial-gradient(100% 60% at 50% 100%, rgba(141,38,51,.16), transparent 70%);
  mix-blend-mode: screen;
  opacity: .5;
}
#app {
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding:
    max(8px, env(safe-area-inset-top))
    max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(8px, env(safe-area-inset-left));
}

/* --------------------------------------------------------------- top bar */

#topbar {
  width: min(96vw, 560px);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 7px 9px;
  background: linear-gradient(180deg, rgba(30,14,20,.9), rgba(12,7,11,.92));
  border: 1px solid rgba(255,207,74,.34);
  border-radius: 8px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.06), 0 6px 18px rgba(0,0,0,.4);
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.brand img { display: block; width: 68px; height: 28px; filter: drop-shadow(0 2px 3px rgba(0,0,0,.5)); }
.brand-copy { display: flex; flex-direction: column; line-height: 1; }
.brand-copy strong {
  font: 900 17px/1 "Arial Black", Impact, sans-serif;
  letter-spacing: .02em;
  color: var(--gold);
  text-shadow: 0 2px 0 #5b111b;
}
.brand-copy span {
  margin-top: 3px;
  font-size: 7.5px;
  letter-spacing: .13em;
  color: var(--muted);
}
.readout {
  display: flex;
  justify-content: center;
  gap: 5px;
  min-width: 0;
}
.chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  padding: 3px 6px;
  background: rgba(0,0,0,.28);
  border: 1px solid rgba(255,207,74,.18);
  border-radius: 5px;
}
.chip span {
  font-size: 6.5px;
  letter-spacing: .13em;
  color: rgba(220,205,180,.62);
  white-space: nowrap;
}
.chip b {
  margin-top: 1px;
  font-size: 12px;
  letter-spacing: .04em;
  color: var(--cream);
  font-variant-numeric: tabular-nums;
}
.chip.lives b { color: #ff6a76; letter-spacing: .02em; }
.actions { display: flex; gap: 5px; }
@media (max-width: 640px) {
  #topbar {
    grid-template-columns: minmax(0, 1fr) auto;
    row-gap: 7px;
  }
  .readout {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-content: stretch;
    gap: 4px;
  }
  .chip {
    flex: 1 1 0;
    min-width: 0;
    padding: 3px 4px;
  }
  .chip b { font-size: 11px; }
  .chip span { font-size: 6px; }
  .actions { justify-self: end; }
  .actions button { min-height: 32px; padding: 4px 7px; }
}
@media (max-width: 390px) {
  .brand-copy span { display: none; }
  .brand img { width: 56px; height: 23px; }
  .actions button { font-size: 9px; }
}
#hud button, .actions button, #dbg, #wheel button, .pad button {
  font: 700 10px/1 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .09em;
  color: var(--cream);
  background: linear-gradient(180deg, rgba(60,22,30,.9), rgba(26,10,15,.95));
  border: 2px solid var(--wine);
  border-radius: 6px;
  min-height: 34px;
  padding: 5px 9px;
  touch-action: manipulation;
  cursor: pointer;
}
.actions button:hover, .actions button:focus-visible { border-color: var(--gold); outline: none; }
.actions button:active, #wheel button:active, .pad button:active { background: var(--wine); color: #fff; }
#act { border-color: var(--gold); color: var(--gold); }
.actions button:disabled { opacity: .4; }

/* ----------------------------------------------------------------- stage */

.stage {
  position: relative;
  border-radius: 7px;
  box-shadow:
    0 0 0 2px #f0d27a,
    0 0 0 7px #5a1414,
    0 0 0 9px #24070d,
    0 18px 42px rgba(0,0,0,.62),
    0 0 60px rgba(141,38,51,.18);
  background: #05060b;
}
.stage::before,
.stage::after {
  content: "";
  position: absolute;
  left: 50%;
  width: 46%;
  height: 3px;
  transform: translateX(-50%);
  background: linear-gradient(90deg, transparent, rgba(255,235,170,.65), transparent);
  pointer-events: none;
}
.stage::before { top: -6px; }
.stage::after { bottom: -6px; opacity: .5; }
.stage-glare {
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: 4px;
  background: linear-gradient(160deg, rgba(255,255,255,.07), transparent 26%, transparent 78%, rgba(0,0,0,.16));
  mix-blend-mode: screen;
}
canvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 4px;
  background: #000;
  touch-action: none;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* -------------------------------------------------------------- controls */

.controls {
  width: min(96vw, 560px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
}
.hints {
  margin: 0;
  font-size: 8.5px;
  letter-spacing: .08em;
  color: rgba(210,196,172,.55);
  text-align: center;
}
.hints kbd {
  display: inline-block;
  min-width: 15px;
  padding: 1px 3px;
  margin: 0 1px;
  border: 1px solid rgba(255,207,74,.3);
  border-bottom-width: 2px;
  border-radius: 3px;
  color: rgba(243,234,215,.8);
  font: inherit;
}
.hints span { margin: 0 3px; color: rgba(255,207,74,.4); }
.pad {
  display: none;
  grid-template-columns: repeat(3, 56px);
  gap: 6px;
}
.pad button {
  height: 50px;
  font-size: 19px;
  border-radius: 9px;
  backdrop-filter: blur(4px);
}
@media (hover: none), (max-width: 760px), (pointer: coarse) {
  .pad { display: grid; }
  .hints { display: none; }
}
@media (orientation: landscape) and (max-height: 560px) {
  .pad { display: none; }
  .hints { display: block; }
}

/* ------------------------------------------------------------- fullscreen */

body.driving { background: #05060b; }
body.driving #app { display: none; }
#drive {
  position: fixed;
  inset: 0;
  z-index: 4;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 0;
  background: #05060b;
  image-rendering: auto;
}
#drive[hidden] { display: none !important; }

#wheel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 6;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding:
    10px
    max(10px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(10px, env(safe-area-inset-left));
  pointer-events: none;
}
#wheel[hidden] { display: none !important; }
#wheel button {
  pointer-events: auto;
  min-width: 0;
  flex: 1 1 0;
  max-width: 150px;
  min-height: 66px;
  font-size: 14px;
  background: linear-gradient(180deg, rgba(35,43,61,.88), rgba(9,14,24,.92));
  border-color: rgba(255,207,74,.6);
  border-radius: 12px;
  backdrop-filter: blur(5px);
}
#wheel button:active, #wheel button.held { background: #6e4820; color: #fff; border-color: #ffe0a3; box-shadow: inset 0 0 18px rgba(255,207,74,.16); }
#wheel button[data-k="u"] { border-color: #73d4b4; color: #abf1d4; }
#wheel button[data-k="d"] { border-color: #da8590; color: #ffc0c7; }
@media (hover: hover) and (pointer: fine) and (min-width: 800px) {
  #wheel { display: none; }
}
/* Large touch tablets still need controls. Only hide for an actual mouse. */

/* ---------------------------------------------------------------- story card */

#tale {
  position: fixed;
  inset: 0;
  z-index: 65;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 30px;
  padding: max(30px, env(safe-area-inset-top)) 30px max(30px, env(safe-area-inset-bottom));
  background:
    radial-gradient(80% 60% at 50% 40%, rgba(64,18,28,.55), transparent 70%),
    radial-gradient(120% 100% at 50% 100%, rgba(141,38,51,.22), transparent 60%),
    #06050a;
  color: #f7f4ea;
  text-align: center;
}
#tale::before, #tale::after {
  content: "";
  position: absolute;
  left: 8%;
  right: 8%;
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(255,207,74,.55), transparent);
}
#tale::before { top: 8%; }
#tale::after { bottom: 8%; }
#tale[hidden] { display: none !important; }
#tale .tale-kicker {
  font: 700 10px/1 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .24em;
  color: var(--gold);
  opacity: .85;
}
#tale p {
  max-width: 34rem;
  margin: 0;
  padding: clamp(22px, 4vw, 34px);
  font: 700 21px/1.6 ui-monospace, Menlo, Consolas, monospace;
  text-align: left;
  text-shadow: 0 2px 18px rgba(0,0,0,.8);
  background:
    linear-gradient(180deg, rgba(35,12,19,.92), rgba(10,6,10,.96));
  border: 1px solid rgba(255,207,74,.35);
  border-radius: 8px;
  box-shadow:
    inset 0 0 0 1px rgba(255,207,74,.08),
    0 24px 70px rgba(0,0,0,.65);
}
#tale p::first-letter {
  color: var(--gold);
}
#tale span {
  font: 700 11px/1 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .18em;
  color: var(--gold);
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
#tale .tale-sound {
  min-height: 34px;
  padding: 0 14px;
  font: 700 10px/1 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .1em;
  color: var(--gold);
  background: rgba(12,6,10,.6);
  border: 1px solid rgba(255,207,74,.5);
  border-radius: 8px;
  cursor: pointer;
}
#tale .tale-sound[aria-pressed="true"] { background: var(--wine); color: #fff; border-color: var(--gold); }
@media (max-width: 640px) {
  #tale { gap: 22px; padding: max(22px, env(safe-area-inset-top)) 18px max(22px, env(safe-area-inset-bottom)); }
  #tale p { font-size: 15.5px; padding: 22px 18px; }
}

/* ------------------------------------------------------------------- debug */

#dbg {
  position: fixed;
  right: 8px;
  bottom: 8px;
  z-index: 80;
  min-width: 40px;
  min-height: 30px;
  padding: 4px 7px;
  font-size: 8px;
  letter-spacing: .1em;
  opacity: .38;
  background: rgba(8,6,10,.8);
  border: 1px solid rgba(255,207,74,.45);
  border-radius: 5px;
}
#dbg:hover, #dbg:focus-visible, #dbg.dbg-on { opacity: 1; }
#dbg.dbg-on { background: var(--wine); color: #fff; border-color: var(--gold); }
body.driving #dbg { display: none; }
#dbg-panel {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding:
    max(64px, calc(env(safe-area-inset-top) + 56px))
    16px
    max(16px, env(safe-area-inset-bottom));
  background: rgba(4,3,7,.82);
  overflow: auto;
}
#dbg-panel[hidden] { display: none !important; }
.dbg-card {
  width: min(560px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  color: #f4f1ea;
  background: linear-gradient(180deg, #1a0d13, #0d080c);
  border: 2px solid var(--gold);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.7);
}
.dbg-card header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  letter-spacing: .16em;
  color: var(--gold);
}
.dbg-card h2 {
  margin: 0 0 8px;
  font-size: 10px;
  letter-spacing: .12em;
  color: var(--gold);
}
.dbg-card button {
  font: 700 11px/1.2 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .05em;
  color: #f4f1ea;
  background: transparent;
  border: 1px solid var(--wine);
  border-radius: 6px;
  min-height: 40px;
  padding: 7px 9px;
  touch-action: manipulation;
}
.dbg-card button:active { background: var(--wine); }
.dbg-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
}
@media (min-width: 640px) {
  .dbg-grid { grid-template-columns: 1fr 1fr; }
}
.dbg-god {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  letter-spacing: .08em;
  font-size: 11px;
}
.dbg-god input { width: 20px; height: 20px; accent-color: var(--wine); }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
noscript p {
  max-width: 36rem;
  margin: 2rem auto;
  padding: 0 1rem;
}

/* ------------------------------------------------------------ game pause */
#pausebtn {
  position: fixed;
  right: max(10px, env(safe-area-inset-right));
  top: calc(160px + env(safe-area-inset-top));
  z-index: 9;
  width: 42px;
  height: 34px;
  padding: 0;
  font: 700 12px/1 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .08em;
  color: var(--gold);
  background: rgba(12,6,10,.58);
  border: 1px solid rgba(255,207,74,.55);
  border-radius: 8px;
  backdrop-filter: blur(4px);
  opacity: .65;
  touch-action: manipulation;
  cursor: pointer;
}
#pausebtn.walk { top: calc(108px + env(safe-area-inset-top)); }
#pausebtn:hover, #pausebtn:focus-visible { opacity: 1; border-color: var(--gold); outline: none; }
#pausebtn[aria-pressed="true"] { background: var(--wine); color: #fff; opacity: 1; }
#pausebtn[hidden] { display: none !important; }

#mapbtn {
  position: fixed;
  right: max(10px, env(safe-area-inset-right));
  top: calc(204px + env(safe-area-inset-top));
  z-index: 9;
  min-width: 44px;
  min-height: 36px;
  padding: 0 9px;
  font: 700 10px/1 ui-monospace, Menlo, Consolas, monospace;
  color: #c4ddf9;
  background: rgba(12,20,34,.88);
  border: 1px solid rgba(143,190,244,.6);
  border-radius: 8px;
  touch-action: manipulation;
  cursor: pointer;
}
#mapbtn[aria-pressed="true"] { top: max(82px, env(safe-area-inset-top)); background: #263d59; }
#mapbtn[hidden] { display: none !important; }
#mapbtn:focus-visible, #wheel button:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }

@media (prefers-reduced-motion: reduce) {
  #tale span { animation: none; }
}

/* Sound stays reachable while a fullscreen scene hides #app. */
#soundbtn {
  position: fixed;
  right: calc(max(10px, env(safe-area-inset-right)) + 52px);
  top: calc(160px + env(safe-area-inset-top));
  z-index: 9;
  min-width: 64px;
  height: 34px;
  padding: 0 8px;
  display: none;
  font: 700 10px/1 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .06em;
  color: var(--gold);
  background: rgba(12,6,10,.58);
  border: 1px solid rgba(255,207,74,.55);
  border-radius: 8px;
  backdrop-filter: blur(4px);
  opacity: .65;
  touch-action: manipulation;
  cursor: pointer;
}
body.driving #soundbtn { display: block; }
#soundbtn:hover, #soundbtn:focus-visible { opacity: 1; border-color: var(--gold); outline: none; }
#soundbtn[aria-pressed="true"] { background: var(--wine); color: #fff; opacity: 1; }
