:root {
  --bg: #05070c;
  --panel: rgba(9, 14, 22, 0.94);
  --line: #1b2736;
  --fg: #d7e3ef;
  --dim: #5d7085;
  --up: #21d07a;
  --down: #ff4d5e;
  --amber: #ffb020;
  --mono: "SFMono-Regular", "JetBrains Mono", "Menlo", "Consolas", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--mono);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

/* Nothing here is text to be read and copied — it is all controls and readouts.
   Long-pressing a button on mobile otherwise drags out selection handles and,
   on iOS, the copy/define callout. `user-select` alone is not enough: WebKit
   does not reliably inherit it into form controls, so it is set outright. */
*,
*::before,
*::after {
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  user-select: none;
}

/* JS sets exact pixel dimensions from the viewport; these are the fallback so a
   failure to boot still leaves a full-bleed canvas rather than a 300x150 stub */
#game {
  display: block;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  touch-action: none;
}

.hidden { display: none !important; }

/* ── overlays ─────────────────────────────────────────────── */
/* The overlay scrolls, not the panel. Scrolling inside the panel slices its own
   border and bottom padding off mid-row on a phone; scrolling the overlay moves
   the whole card, so it always ends on a clean edge with real margin below it. */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  justify-content: center;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  padding: calc(24px + env(safe-area-inset-top)) calc(24px + env(safe-area-inset-right))
           calc(24px + env(safe-area-inset-bottom)) calc(24px + env(safe-area-inset-left));
  background: radial-gradient(ellipse at 50% 40%, rgba(10, 18, 30, 0.86), rgba(3, 5, 9, 0.97));
  backdrop-filter: blur(3px);
  z-index: 20;
}

.panel {
  width: min(560px, 100%);
  /* centres while it fits and scrolls without clipping the top when it does
     not — which `align-items: center` on the flex parent would do */
  margin: auto;
  flex: 0 0 auto;
  padding: 30px 30px 26px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--panel);
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.7), inset 0 1px 0 rgba(255, 255, 255, 0.04);
}
.panel.wide { width: min(720px, 100%); }

h1 {
  margin: 0;
  font-size: clamp(30px, 6vw, 48px);
  letter-spacing: 0.14em;
  font-weight: 700;
  color: var(--up);
  text-shadow: 0 0 26px rgba(33, 208, 122, 0.35);
}
h2 {
  margin: 0 0 6px;
  font-size: clamp(22px, 4vw, 30px);
  letter-spacing: 0.12em;
}
.cursor { animation: blink 1.1s steps(1) infinite; color: var(--up); }
@keyframes blink { 50% { opacity: 0; } }

.tag { margin: 8px 0 20px; color: var(--dim); font-size: 12.5px; line-height: 1.6; letter-spacing: 0.03em; }
.tag em { color: var(--fg); font-style: normal; }

.fineprint { margin: 18px 0 0; color: #3d4c5e; font-size: 10.5px; line-height: 1.6; }

/* ── level list ───────────────────────────────────────────── */
.levels { display: grid; gap: 6px; }

.lvl {
  display: grid;
  grid-template-columns: 26px 1fr auto;
  gap: 12px;
  align-items: center;
  touch-action: manipulation;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.014);
  color: var(--fg);
  font-family: var(--mono);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s, transform 0.08s;
}
.lvl:hover, .lvl:focus-visible {
  border-color: var(--up);
  background: rgba(33, 208, 122, 0.07);
  outline: none;
  transform: translateX(2px);
}
.lvl .num { color: var(--dim); font-size: 11px; }
.lvl .name { letter-spacing: 0.06em; }
.lvl .desc { display: block; color: var(--dim); font-size: 10.5px; margin-top: 3px; letter-spacing: 0.02em; }
.lvl .meta { text-align: right; font-size: 10.5px; color: var(--dim); white-space: nowrap; }
.lvl .meta b { display: block; color: var(--amber); font-weight: 500; font-size: 12px; }
.lvl .risk { display: inline-block; letter-spacing: 0.16em; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  color: var(--dim);
  font-size: 10.5px;
  letter-spacing: 0.05em;
}
kbd {
  display: inline-block;
  min-width: 18px;
  padding: 1px 5px;
  margin-right: 3px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 3px;
  background: #0d141d;
  color: var(--fg);
  font-family: var(--mono);
  font-size: 10px;
  text-align: center;
}

/* ── result panel ─────────────────────────────────────────── */
.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1px;
  margin: 18px 0 22px;
  background: var(--line);
  border: 1px solid var(--line);
}
.stat { padding: 10px 12px; background: #080d14; }
.stat .k { display: block; color: var(--dim); font-size: 9.5px; letter-spacing: 0.16em; }
.stat .v { display: block; margin-top: 4px; font-size: 17px; letter-spacing: 0.04em; }
.stat .v.up { color: var(--up); }
.stat .v.down { color: var(--down); }
.stat .v.gold { color: var(--amber); }

.buttons { display: flex; flex-wrap: wrap; gap: 8px; }
button {
  flex: 1 1 auto;
  padding: 11px 16px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: #0d141d;
  color: var(--fg);
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.12em;
  cursor: pointer;
  /* taps fire immediately instead of waiting out the double-tap-zoom window */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: border-color 0.12s, background 0.12s;
}
button:hover { border-color: var(--up); background: rgba(33, 208, 122, 0.09); }
button.primary { border-color: var(--up); color: var(--up); }
button .hint { margin-left: 6px; color: var(--dim); font-size: 9.5px; }

/* ── hud ──────────────────────────────────────────────────── */
#hud {
  position: fixed;
  top: 0; left: 0; right: 0;
  padding: 10px 12px 0;
  pointer-events: none;
  z-index: 10;
}
.hud-row { display: flex; gap: 1px; background: var(--line); border: 1px solid var(--line); width: fit-content; max-width: 100%; }
.tile { min-width: 74px; padding: 5px 10px 6px; background: rgba(5, 9, 14, 0.85); }
.tile .k { display: block; color: var(--dim); font-size: 8.5px; letter-spacing: 0.18em; }
.tile .v { display: block; margin-top: 1px; font-size: 15px; letter-spacing: 0.03em; font-variant-numeric: tabular-nums; }
.tile .v.dim { color: var(--dim); }

.hud-progress { margin-top: 8px; height: 2px; width: min(340px, 60vw); background: #131c27; }
.hud-progress-fill { height: 100%; width: 0%; background: var(--up); box-shadow: 0 0 8px rgba(33, 208, 122, 0.7); transition: width 0.1s linear; }
.hud-level { margin-top: 7px; color: var(--dim); font-size: 10px; letter-spacing: 0.2em; }

/* ── touch ────────────────────────────────────────────────── */
/* only for touch sessions, and only while actually riding */
#touch { display: none; }
body.touch-mode.playing #touch { display: block; }

/* keyboard hints are meaningless once we know it is a touch session */
body.touch-mode .legend,
body.touch-mode button .hint { display: none; }

#touch {
  position: fixed;
  inset: 0;
  z-index: 15;
  pointer-events: none;
  padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right))
           max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}
.pad { position: absolute; display: flex; gap: 12px; pointer-events: auto; }
.pad.left { left: max(14px, env(safe-area-inset-left)); bottom: max(14px, env(safe-area-inset-bottom)); }
.pad.right { right: max(14px, env(safe-area-inset-right)); bottom: max(14px, env(safe-area-inset-bottom)); }
/* mid-right edge: the top corner belongs to the HUD, the bottom to the pads */
.pad.util {
  right: max(14px, env(safe-area-inset-right));
  top: 50%;
  transform: translateY(-50%);
  flex-direction: column;
  gap: 8px;
}

#touch button {
  width: 72px; height: 72px;
  flex: 0 0 auto;
  border-radius: 50%;
  /* held down for whole runs — no gesture of any kind should start here */
  touch-action: none;
  font-size: 26px;
  line-height: 1;
  padding: 0;
  background: rgba(13, 20, 29, 0.72);
  border-color: #2a4055;
  backdrop-filter: blur(6px);
  transition: none;
}
.pad.util button { width: 42px; height: 42px; font-size: 16px; background: rgba(13, 20, 29, 0.6); }
#touch button.on, #touch button:active {
  background: rgba(33, 208, 122, 0.26);
  border-color: var(--up);
  color: var(--up);
}

/* ── small screens ────────────────────────────────────────── */
@media (max-width: 560px) {
  .overlay {
    padding: calc(16px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right))
             calc(16px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
  }
  .panel { padding: 24px 18px 22px; border-radius: 3px; }
  h1 { font-size: 34px; letter-spacing: 0.1em; }
  .tag { margin: 6px 0 14px; font-size: 11.5px; }
  .lvl { grid-template-columns: 22px 1fr auto; gap: 9px; padding: 9px 10px; font-size: 12px; }
  .lvl .desc { font-size: 9.5px; }
  .fineprint { display: none; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .buttons { flex-direction: column; }
  button { padding: 13px 16px; }
  #hud { padding: 8px 8px 0; }
  /* BEST is shown on the results panel anyway; reclaim the width */
  body.touch-mode .hud-row .tile:nth-child(2) { display: none; }
  .tile { min-width: 58px; padding: 4px 7px 5px; }
  .tile .v { font-size: 12.5px; }
  .tile .k { font-size: 7.5px; letter-spacing: 0.12em; }
  .hud-progress { width: min(220px, 56vw); }
  .hud-level { font-size: 8.5px; letter-spacing: 0.12em; }
}

/* landscape phones: vertical room is the scarce resource */
@media (max-height: 480px) {
  .overlay {
    padding: calc(12px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right))
             calc(12px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
  }
  .panel { padding: 18px 20px 16px; }
  h1 { font-size: 26px; }
  .tag { margin: 4px 0 10px; }
  .lvl { padding: 6px 10px; }
  .lvl .desc { display: none; }
  .stats { margin: 10px 0 12px; }
  .stat { padding: 6px 9px; }
  .stat .v { font-size: 14px; }
  .buttons { flex-direction: row; }
  #touch button { width: 60px; height: 60px; font-size: 22px; }
  .pad.util button { width: 36px; height: 36px; font-size: 14px; }
}
