:root {
  --bg: #07111e;
  --panel: #0d1b2a;
  --line: #34495c;
  --gold: #f5c45a;
  --muted: #a9b8c7;
  --red: #ed3447;
  --green: #24d36b;
  --blue: #2e91ff;
  --arm: #e52d40;
  --men: #2588ff;
  --head: #19bb59;
  /* Reserved for on-screen controls (0 on desktop) so the stage never sits under them */
  --mc-h: 0px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: #f4f7fb;
  font-family: "IBM Plex Mono", "Courier New", Courier, monospace;
  background-color: #07111e;
  background-image:
    radial-gradient(ellipse at 18% 0%, rgba(36, 90, 72, 0.45) 0%, transparent 42%),
    radial-gradient(ellipse at 85% 8%, rgba(120, 48, 40, 0.28) 0%, transparent 38%),
    linear-gradient(180deg, #143042 0%, #0a1624 55%, #07111e 100%);
}

.wrap {
  max-width: 1180px;
  margin: 0 auto;
  padding: 16px;
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.brand {
  font-weight: 900;
  font-size: clamp(22px, 4vw, 40px);
  letter-spacing: -0.04em;
  text-transform: uppercase;
  text-shadow: 3px 4px #6b2531;
}

.brand span { color: var(--red); font-style: italic; }

.tag {
  font-size: 11px;
  letter-spacing: 0.12em;
  color: var(--gold);
  font-weight: 900;
  text-transform: uppercase;
}

.panel {
  background: linear-gradient(145deg, #142638, #0a1521);
  border: 2px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  box-shadow: 0 10px 28px #0004;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 270px;
  gap: 12px;
  align-items: start;
}

.screen {
  position: relative;
  overflow: hidden;
  border: 3px solid #5a6e52;
  border-radius: 4px;
  background: #13283a;
  box-shadow:
    0 0 0 1px #1a2a18,
    0 12px 40px rgba(0, 0, 0, 0.45),
    inset 0 0 0 1px rgba(245, 196, 90, 0.12);
  display: flex;
  flex-direction: column;
}

.stage {
  position: relative;
  width: 100%;
  min-width: 0;
  flex: 0 1 auto;
}

.fs-exit {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 30;
  display: none;
}

.hud-wrap {
  padding: 6px 8px 0;
}

/* Fullscreen: 16:9 stage; control bar sits BELOW it (never overlays the playfield) */
body.is-fullscreen {
  overflow: hidden;
  background: #000;
}

body.is-fullscreen .wrap {
  max-width: none;
  margin: 0;
  padding: 0;
  min-height: 100vh;
  min-height: 100dvh;
}

body.is-fullscreen .top,
body.is-fullscreen .side,
body.is-fullscreen .footer {
  display: none !important;
}

body.is-fullscreen .game-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
  place-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  background: #000;
}

body.is-fullscreen .screen,
.screen:fullscreen,
.screen:-webkit-full-screen {
  width: 100vw;
  width: 100dvw;
  height: 100vh;
  height: 100dvh;
  max-width: none;
  border: none;
  border-radius: 0;
  box-shadow: none;
  background: #000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
}

body.is-fullscreen .stage,
.screen:fullscreen .stage,
.screen:-webkit-full-screen .stage {
  width: min(100vw, calc((100vh - var(--mc-h)) * 16 / 9));
  width: min(100dvw, calc((100dvh - var(--mc-h)) * 16 / 9));
  height: min(calc(100vh - var(--mc-h)), calc(100vw * 9 / 16));
  height: min(calc(100dvh - var(--mc-h)), calc(100dvw * 9 / 16));
  overflow: hidden;
  background: #13283a;
  flex: 0 0 auto;
}

body.is-fullscreen .hud-wrap,
.screen:fullscreen .hud-wrap,
.screen:-webkit-full-screen .hud-wrap {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 4;
  padding: 8px 10px 0;
  pointer-events: none;
}

body.is-fullscreen .canvas-wrap,
.screen:fullscreen .canvas-wrap,
.screen:-webkit-full-screen .canvas-wrap {
  width: 100%;
  height: 100%;
}

body.is-fullscreen .canvas-wrap canvas,
.screen:fullscreen .canvas-wrap canvas,
.screen:-webkit-full-screen .canvas-wrap canvas {
  width: 100%;
  height: 100%;
}

body.is-fullscreen .mobile-controls,
.screen:fullscreen .mobile-controls,
.screen:-webkit-full-screen .mobile-controls {
  position: relative;
  left: auto;
  right: auto;
  bottom: auto;
  z-index: 4;
  width: min(100vw, calc((100vh - var(--mc-h)) * 16 / 9));
  width: min(100dvw, calc((100dvh - var(--mc-h)) * 16 / 9));
  margin: 0;
  flex: 0 0 auto;
}

body.is-fullscreen .fs-exit,
.screen:fullscreen .fs-exit,
.screen:-webkit-full-screen .fs-exit {
  display: inline-flex;
}

.hud {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 4px;
  margin-bottom: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}

.hud::-webkit-scrollbar { display: none; }

.stat {
  padding: 3px 6px;
  background: #030a12;
  border: 1px solid #34495c;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 800;
  line-height: 1.25;
  white-space: nowrap;
  flex: 0 0 auto;
}

.stat b { color: #70e391; }
.stat.lives b { color: #ff5865; }
.stat.status b { color: var(--gold); }

.canvas-wrap { position: relative; }

.canvas-wrap canvas {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  background: #000;
}

.overlay {
  position: fixed;
  inset: 0;
  background: #06111ded;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top, 0px))
           max(16px, env(safe-area-inset-right, 0px))
           max(16px, env(safe-area-inset-bottom, 0px))
           max(16px, env(safe-area-inset-left, 0px));
  z-index: 200;
  overscroll-behavior: contain;
}

.overlay.hidden { display: none; }

.modal {
  max-width: 680px;
  width: 100%;
  max-height: min(92vh, calc(100dvh - 32px));
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  background: #101f30;
  border: 2px solid #496074;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 20px 60px #0008;
}

.modal h1, .modal h2 { margin: 0 0 8px; font-size: clamp(22px, 4vw, 32px); }
.modal p { color: #c2d0dd; line-height: 1.5; font-size: 13px; }

.modal .btn-row {
  position: sticky;
  bottom: -1px;
  z-index: 1;
  margin-top: 12px;
  margin-bottom: 0;
  padding-top: 10px;
  padding-bottom: 2px;
  background: linear-gradient(180deg, transparent, #101f30 28%);
}

body.modal-open {
  overflow: hidden;
}

.select-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 14px 0;
}

.pick {
  background: #091521;
  color: white;
  border: 2px solid #3b5268;
  border-radius: 10px;
  padding: 8px;
  cursor: pointer;
  text-align: center;
  min-width: 0;
  transition: border-color 0.15s, transform 0.15s;
}

.pick:hover { transform: translateY(-2px); }
.pick img {
  width: 100%;
  height: 120px;
  object-fit: contain;
  image-rendering: pixelated;
}
.pick strong { display: block; font-size: 12px; margin-top: 4px; }
.pick small { display: block; color: #afc0d0; font-size: 10px; margin-top: 3px; }
.pick.selected { border-color: var(--gold); box-shadow: 0 0 0 2px #f5c45a33; }
.pick[data-char="0"].selected { border-color: var(--arm); }
.pick[data-char="1"].selected { border-color: var(--men); }
.pick[data-char="2"].selected { border-color: var(--head); }

.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

.btn {
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--gold);
  color: #15202a;
  font-weight: 900;
  font-family: inherit;
  cursor: pointer;
  font-size: 13px;
}

.btn:hover { filter: brightness(1.08); }
.btn.secondary {
  background: #20354a;
  color: #fff;
  border: 1px solid #40566c;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.side h3 {
  font-size: 12px;
  color: var(--gold);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin: 0 0 8px;
}

.side p, .side li {
  font-size: 11px;
  line-height: 1.55;
  color: #c4d0db;
}

.side ul { padding-left: 16px; margin: 6px 0 12px; }

.controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  margin: 10px 0;
}

.key {
  background: #182c40;
  border: 1px solid #40566c;
  border-radius: 6px;
  text-align: center;
  padding: 6px;
  font-weight: 900;
  font-size: 11px;
}

.levels {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.level {
  font-size: 10px;
  padding: 4px 6px;
  border-radius: 5px;
  background: #15293b;
  border: 1px solid #34495c;
  color: #d3e0eb;
}

.level.current { border-color: var(--gold); color: var(--gold); }
.level.locked { opacity: 0.45; }
.level.done { border-color: var(--green); color: #8dffb0; }

.mobile-controls {
  display: none;
  flex-shrink: 0;
  align-items: stretch;
  justify-content: space-between;
  gap: 12px;
  margin: 0;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  background: #0a1521;
  border-top: 2px solid #34495c;
  box-sizing: border-box;
  /* Exact bar height — fullscreen stage uses the same --mc-h */
  height: var(--mc-h);
}

.mc-cluster {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.mc-move { justify-content: flex-start; }
.mc-actions { justify-content: flex-end; margin-left: auto; }

.mobile-controls button {
  min-width: 56px;
  min-height: 56px;
  padding: 8px 10px;
  border-radius: 12px;
  border: 1px solid #40566c;
  background: #172a3d;
  color: #fff;
  font-weight: 900;
  font-family: inherit;
  font-size: 11px;
  line-height: 1.15;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.mobile-controls .mc-ico {
  font-size: 18px;
  line-height: 1;
}

.mobile-controls .mc-lbl {
  font-size: 9px;
  letter-spacing: 0.04em;
  opacity: 0.85;
}

.mobile-controls button:active {
  background: #243f5a;
  transform: scale(0.96);
}

.footer {
  color: #8195a8;
  font-size: 11px;
  margin: 12px 2px;
}

.toast {
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  background: #06111eee;
  border: 1px solid #6d8499;
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 11px;
  font-weight: 900;
  white-space: nowrap;
  z-index: 3;
  pointer-events: none;
  max-width: 90%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast.hidden { display: none; }

.press-box {
  margin: 10px 0 14px;
  padding: 12px 14px;
  background: #091521;
  border: 1px solid #40566c;
  border-radius: 8px;
  text-align: left;
}

.press-mic {
  font-size: 18px;
  margin-bottom: 4px;
}

.press-quote {
  margin: 0;
  color: #f5c45a;
  font-weight: 800;
  font-style: italic;
  line-height: 1.35;
}

.press-bus {
  margin: 8px 0 0;
  color: #9ad0e8;
  font-size: 12px;
}

.disclaimer-box {
  margin: 12px 0 4px;
  padding: 14px 16px;
  background: #091521;
  border: 1px solid #6d8499;
  border-left: 4px solid #f5c45a;
  border-radius: 8px;
  text-align: left;
  /* No nested scroll — the modal itself is the only scroll container */
  max-height: none;
  overflow: visible;
}

.disclaimer-box p {
  margin: 0 0 10px;
  color: #c2d0dd;
  font-size: 13px;
  line-height: 1.45;
}

.disclaimer-box p:last-child { margin-bottom: 0; }

.disclaimer-note {
  color: #f5c45a !important;
  font-weight: 800;
}

.disclaimer-mini {
  margin: 6px 0 10px;
  padding: 6px 10px;
  font-size: 11px;
  color: #a9b8c7;
  border: 1px dashed #40566c;
  border-radius: 6px;
  background: #091521;
}

.campaign-map-wrap {
  margin: 12px 0;
}

.campaign-map {
  width: 100%;
  height: auto;
  display: block;
  border: 1px solid #40566c;
  border-radius: 10px;
  background: #142638;
}

.campaign-map .map-route {
  stroke: #c4b896;
  stroke-width: 3;
  stroke-dasharray: 6 4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.campaign-map .map-node circle {
  fill: #5a6e7a;
  stroke: #1a2230;
  stroke-width: 2;
}

.campaign-map .map-node text {
  fill: #c2d0dd;
  font-size: 8px;
  font-family: "IBM Plex Mono", monospace;
  font-weight: 700;
}

.campaign-map .map-node.done circle {
  fill: #24d36b;
}

.campaign-map .map-node.from circle {
  fill: #f5c45a;
  stroke: #fff;
}

.campaign-map .map-node.next circle {
  fill: #ed3447;
  stroke: #ffe9a0;
  stroke-width: 2.5;
}

.campaign-map .map-node.locked circle {
  fill: #3a4a58;
  opacity: 0.75;
}

.campaign-map .map-node.locked text {
  opacity: 0.55;
}

.campaign-map .map-pin text {
  fill: #ed3447;
  font-size: 9px;
  font-weight: 900;
  font-family: "IBM Plex Mono", monospace;
}

.map-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 8px;
  font-size: 11px;
  color: #a9b8c7;
}

.map-legend .leg.done { color: #70e391; }
.map-legend .leg.next { color: #ff9aa2; }
.map-legend .leg.locked { color: #8195a8; }

.records {
  margin: 10px 0;
  padding: 8px;
  background: #091521;
  border: 1px solid #34495c;
  border-radius: 8px;
  font-size: 11px;
  color: #c2d0dd;
}

.level-pick {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin: 12px 0;
}

.level-pick button {
  font-family: inherit;
  font-size: 11px;
  font-weight: 800;
  padding: 8px 6px;
  border-radius: 7px;
  border: 1px solid #40566c;
  background: #15293b;
  color: #e8f0f7;
  cursor: pointer;
}

.level-pick button:disabled { opacity: 0.4; cursor: not-allowed; }
.level-pick button.done { border-color: var(--green); }

/* —— Mobile app shell: one viewport, no page scroll, menus on full screen —— */
@media (max-width: 850px) {
  :root {
    --mc-h: calc(76px + env(safe-area-inset-bottom, 0px));
    --top-h: 52px;
  }

  html,
  body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    overscroll-behavior: none;
  }

  body {
    display: flex;
    flex-direction: column;
  }

  .wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    max-width: none;
    margin: 0;
    padding: 6px;
    padding-bottom: 0;
    overflow: hidden;
  }

  .top {
    flex: 0 0 auto;
    align-items: center;
    flex-direction: row;
    gap: 8px;
    margin-bottom: 6px;
    min-height: 0;
  }

  .top .tag { display: none; }

  .brand {
    font-size: 18px;
    text-shadow: 2px 2px #6b2531;
  }

  .top-actions {
    width: auto;
    margin-left: auto;
    gap: 6px;
  }

  .top-actions .btn {
    padding: 8px 10px;
    font-size: 11px;
  }

  .game-layout {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  /* Side panel + footer clutter the phone — menus cover the same info */
  .side,
  .footer {
    display: none !important;
  }

  .screen {
    flex: 1;
    min-height: 0;
    height: auto;
    border-width: 2px;
  }

  .stage {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  .canvas-wrap {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
  }

  .canvas-wrap canvas {
    width: 100%;
    height: 100%;
    max-height: 100%;
    object-fit: contain;
  }

  .mobile-controls { display: flex; }

  /* HUD floats over the canvas so the playfield keeps full 16:9 */
  .hud-wrap {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 4;
    padding: 6px 8px 0;
    pointer-events: none;
    background: linear-gradient(180deg, rgba(7, 17, 30, 0.72), transparent);
  }

  /* Menus cover the whole phone — not a tiny scroll box inside the canvas */
  .overlay {
    position: fixed;
    inset: 0;
    z-index: 200;
    padding: max(12px, env(safe-area-inset-top, 0px))
      max(12px, env(safe-area-inset-right, 0px))
      max(12px, env(safe-area-inset-bottom, 0px))
      max(12px, env(safe-area-inset-left, 0px));
    align-items: stretch;
    justify-content: center;
  }

  .modal {
    width: min(100%, 560px);
    margin: auto 0;
    max-height: calc(100dvh - 24px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    padding: 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
  }

  .modal h1,
  .modal h2 {
    font-size: clamp(20px, 6vw, 28px);
  }

  .disclaimer-box {
    margin: 10px 0 12px;
    padding: 12px;
    max-height: none;
    overflow: visible;
  }

  .disclaimer-box p {
    font-size: 12px;
    line-height: 1.4;
    margin: 0 0 8px;
  }

  .btn-row {
    flex-shrink: 0;
    margin-top: 8px;
    flex-direction: column;
  }

  .btn-row .btn {
    width: 100%;
    justify-content: center;
    text-align: center;
    min-height: 44px;
  }

  .select-grid { gap: 5px; }
  .pick img { height: 90px; }

  body.modal-open .mobile-controls {
    visibility: hidden;
    pointer-events: none;
  }
}

@media (max-width: 520px) {
  :root {
    --mc-h: calc(84px + env(safe-area-inset-bottom, 0px));
  }

  .wrap { padding: 4px; padding-bottom: 0; }
  .stat { font-size: 9px; padding: 2px 5px; }
  .hud { gap: 3px; }
  .level-pick { grid-template-columns: repeat(2, 1fr); }

  .mobile-controls {
    gap: 8px;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom, 0px));
  }

  .mobile-controls button {
    min-width: 52px;
    min-height: 60px;
    padding: 8px 6px;
  }

  .mobile-controls .mc-lbl { font-size: 8px; }

  .top-actions #restartTop { display: none; }
}

/* Landscape phones: shorter bar, more room for the 16:9 stage */
@media (max-width: 950px) and (max-height: 480px) and (orientation: landscape) {
  :root {
    --mc-h: calc(64px + env(safe-area-inset-bottom, 0px));
  }

  html,
  body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
  }

  .wrap {
    padding: 2px;
    padding-bottom: 0;
  }

  .top {
    margin-bottom: 2px;
  }

  .brand { font-size: 14px; }

  .mobile-controls {
    display: flex;
    padding: 4px 10px calc(4px + env(safe-area-inset-bottom, 0px));
  }

  .mobile-controls button {
    min-height: 48px;
    min-width: 48px;
    flex-direction: row;
    gap: 4px;
    padding: 6px 10px;
  }

  .mobile-controls .mc-lbl { font-size: 9px; }

  .hud-wrap {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 4;
    pointer-events: none;
  }

  .side,
  .footer,
  .top .tag {
    display: none !important;
  }

  .overlay {
    position: fixed;
    inset: 0;
    z-index: 200;
    padding: 8px;
  }

  .btn-row {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .btn-row .btn {
    width: auto;
    flex: 1 1 auto;
  }
}
