/* Hold the Span UI
   Sections: 1 tokens, 2 base, 3 screens & panels, 4 buttons, 5 menus (title, levels, upgrades),
   6 results (stats, stars), 7 HUD, 8 world labels, 9 legal footer, 10 responsive, 11 reduced motion.
   Restyle by editing the tokens in section 1 first; components only reference tokens. */

/* 1. Tokens ------------------------------------------------------------------ */
:root {
  --bg: #0b1220;
  --panel: rgba(13, 20, 36, 0.8);
  --surface: rgba(255, 255, 255, 0.05);
  --surface-hover: rgba(255, 255, 255, 0.1);
  --surface-dim: rgba(255, 255, 255, 0.025);
  --line: rgba(255, 255, 255, 0.14);
  --line-strong: rgba(255, 255, 255, 0.26);
  --line-dim: rgba(255, 255, 255, 0.08);
  --text: #eef3fb;
  --muted: #b3bfd3;          /* >= 4.5:1 on --panel */
  --hud-label: #d5deec;      /* >= 4.5:1 on --hud-chip */
  --hud-chip: rgba(6, 10, 20, 0.66);
  --accent: #ffb43a;
  --accent-ink: #1b1204;
  --blue: #4c96ff;
  --red: #ff5a64;
  --danger-fill: linear-gradient(90deg, #8f1626, #c2263a);
  --radius: 16px;
  --radius-sm: 12px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35), 0 1px 0 rgba(255, 255, 255, 0.05) inset;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --legal-h: 0px;            /* set from #legal's height by main.js (syncLegalHeight) */
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  font-family: var(--font);
  font-size: 16px;
  color: var(--text);
}

/* 2. Base --------------------------------------------------------------------- */
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); overflow: hidden; overscroll-behavior: none; touch-action: manipulation; }
button, a, .legal, .btn, .icon-btn { touch-action: manipulation; }
body { -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
#app { position: fixed; inset: 0; overflow: hidden; }
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2, h3 { margin: 0; letter-spacing: 0.01em; }
h2 { font-size: 24px; font-weight: 700; }

/* 3. Screens & panels ----------------------------------------------------------
   A screen ends where the legal footer begins, so menu content never sits under it. */
.screen {
  position: absolute; top: 0; left: 0; right: 0; bottom: var(--legal-h);
  display: flex; align-items: center; justify-content: center;
  padding: calc(16px + var(--sat)) calc(16px + var(--sar)) 16px calc(16px + var(--sal));
  overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; touch-action: manipulation;
  background: linear-gradient(180deg, rgba(6, 10, 20, 0.35), rgba(6, 10, 20, 0.6));
  animation: fadeIn 0.22s ease-out;
}
.screen-dim { background: rgba(6, 10, 20, 0.6); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
.panel {
  width: min(460px, 100%); margin: auto; padding: 22px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  animation: rise 0.28s cubic-bezier(.2, .8, .2, 1);
}
.panel-narrow { width: min(380px, 100%); text-align: center; }
.panel-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px; margin-bottom: 14px; }
.panel-head h2 { flex: 1; min-width: 0; }
.eyebrow { margin: 0 0 4px; color: var(--accent); font-size: 16px; font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase; }
.muted { color: var(--muted); margin: 0 0 14px; font-size: 16px; line-height: 1.4; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* 4. Buttons ------------------------------------------------------------------ */
.btn-stack { display: grid; gap: 10px; margin-top: 8px; }
.btn {
  min-height: 52px; padding: 12px 18px; border-radius: var(--radius-sm); font-size: 17px; font-weight: 650;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.11), rgba(255, 255, 255, 0.04)); border: 1px solid var(--line-strong); color: var(--text);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), 0 2px 8px rgba(0, 0, 0, 0.18);
  transition: transform 0.08s, background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--surface-hover); }
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: linear-gradient(180deg, #ffd27a 0%, #ffb53a 48%, #ff9a12 52%, #ffab2e 100%); color: var(--accent-ink); border-color: rgba(255, 214, 140, 0.95);
  box-shadow: 0 3px 0 #b8650a, 0 8px 22px rgba(255, 150, 20, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.65); font-weight: 800; letter-spacing: 0.02em;
}
.btn-primary:hover { background: linear-gradient(180deg, #ffdb92 0%, #ffbe4f 48%, #ffa52a 52%, #ffb440 100%); }
.btn-primary:active { transform: translateY(2px); box-shadow: 0 1px 0 #b8650a, 0 4px 12px rgba(255, 150, 20, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.6); }
.btn-ghost { background: transparent; border-color: var(--line); }
/* Disabled: keep the label readable, dim only the surface. */
.btn[disabled] { cursor: not-allowed; color: var(--muted); background: var(--surface-dim); border-color: var(--line-dim); box-shadow: none; }
.btn-block { width: 100%; }
.icon-btn {
  width: 48px; height: 48px; min-width: 48px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm); background: linear-gradient(180deg, rgba(30, 42, 66, 0.82), rgba(8, 13, 26, 0.82)); border: 1px solid var(--line-strong); fill: currentColor;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14), 0 4px 12px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}

/* 5. Menus -------------------------------------------------------------------- */
.back-link {
  display: inline-flex; align-items: center; gap: 6px; min-height: 48px; padding: 0 12px 0 6px; margin: -10px 0 2px -8px;
  border-radius: var(--radius-sm); color: var(--muted); font-size: 16px; font-weight: 600; text-decoration: none;
}
.back-link:hover { color: var(--text); background: var(--surface); }
.back-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.brand { text-align: center; margin-bottom: 20px; }
.emblem { width: 56px; height: 56px; color: var(--accent); margin-bottom: 6px; }
.brand h1 { font-size: clamp(34px, 9vw, 46px); font-weight: 900; text-transform: uppercase; letter-spacing: 0.04em; line-height: 1;
  background: linear-gradient(180deg, #ffffff 0%, #ffffff 45%, #ffd88a 55%, #ffb43a 100%); -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 3px 0 rgba(90, 40, 0, 0.55)) drop-shadow(0 8px 18px rgba(255, 160, 40, 0.25)); }
.tagline { margin: 10px 0 0; color: var(--muted); font-size: 16px; line-height: 1.4; }
.title-meta { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.points-chip { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); font-size: 16px; white-space: nowrap; }
.points-chip strong { color: var(--accent); font-variant-numeric: tabular-nums; }

.level-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.level-card {
  display: grid; grid-template-columns: 48px minmax(0, 1fr) auto; align-items: center; gap: 12px; text-align: left; min-width: 0;
  min-height: 72px; padding: 12px 14px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line);
}
.level-card > span:nth-child(2) { min-width: 0; overflow-wrap: break-word; }
.level-card:hover:not([disabled]) { border-color: var(--line-strong); background: var(--surface-hover); }
.level-num { width: 48px; height: 48px; border-radius: var(--radius-sm); display: grid; place-items: center; font-weight: 800; font-size: 20px; background: rgba(76, 150, 255, 0.16); border: 1px solid rgba(76, 150, 255, 0.45); color: #cfe2ff; }
.level-card.endless .level-num { background: rgba(255, 180, 58, 0.14); border-color: rgba(255, 180, 58, 0.5); color: var(--accent); }
.level-name { display: block; font-weight: 700; font-size: 17px; }
.level-blurb { display: block; color: var(--muted); font-size: 16px; margin-top: 2px; }
/* Locked: text stays opaque; the surface and badge dim, and the lock icon signals state. */
.level-card[disabled] { cursor: not-allowed; background: var(--surface-dim); border-color: var(--line-dim); }
.level-card[disabled] .level-name { color: var(--muted); }
.level-card[disabled] .level-num { background: transparent; border-color: var(--line); color: var(--muted); }
.mini-stars { display: inline-flex; gap: 2px; }
.mini-stars svg { width: 18px; height: 18px; }
.lock { width: 20px; height: 20px; fill: var(--muted); }

.upgrade-list { display: grid; gap: 10px; margin-bottom: 16px; }
.upgrade { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px 12px; align-items: center; padding: 14px; border-radius: 14px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.04); }
.upgrade h3 { font-size: 17px; }
.upgrade p { margin: 2px 0 0; color: var(--muted); font-size: 16px; }
.upgrade .btn { min-width: 112px; min-height: 48px; padding: 8px 14px; font-size: 16px; }
.pips { display: flex; gap: 5px; grid-column: 1 / -1; }
.pip { flex: 1; height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.12); }
.pip.on { background: linear-gradient(90deg, var(--blue), #7fb6ff); }

/* 6. Results ------------------------------------------------------------------ */
.stats { display: grid; gap: 6px; margin: 16px 0; }
.stats div { display: flex; justify-content: space-between; padding: 10px 12px; border-radius: 10px; background: rgba(255, 255, 255, 0.04); border: 1px solid var(--line); }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.stars { display: flex; justify-content: center; gap: 10px; margin-top: 14px; }
.stars svg { width: 52px; height: 52px; }
.stars svg:nth-child(2) { transform: translateY(-8px); width: 60px; height: 60px; }
.stars path { transform-origin: 12px 12px; }
.star-on { fill: var(--accent); stroke: #ffe1a6; }
.star-off { fill: rgba(255, 255, 255, 0.08); stroke: rgba(255, 255, 255, 0.3); }
.stars .star-on { animation: pop 0.4s cubic-bezier(.2, 1.6, .4, 1) both; }
.stars svg:nth-child(2) .star-on { animation-delay: 0.15s; }
.stars svg:nth-child(3) .star-on { animation-delay: 0.3s; }
@keyframes pop { from { transform: scale(0.3); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* 7. HUD ---------------------------------------------------------------------- */
#hud {
  position: absolute; left: 0; right: 0; top: 0; display: flex; align-items: flex-start; gap: 10px;
  padding: calc(10px + var(--sat)) calc(12px + var(--sar)) 12px calc(12px + var(--sal));
  pointer-events: none; background: linear-gradient(180deg, rgba(6, 10, 20, 0.66), rgba(6, 10, 20, 0));
}
#hud .icon-btn { pointer-events: auto; }
.hud-mid { flex: 1; min-width: 0; }
.hud-level { height: 22px; margin: 1px 0 6px; font-weight: 700; font-size: 16px; line-height: 22px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.7); }
.hud-row { display: flex; align-items: center; gap: 8px; }
.hud-squad {
  display: inline-flex; align-items: center; gap: 4px; flex: none; padding: 1px 9px 1px 7px; border-radius: 999px;
  background: linear-gradient(180deg, rgba(40, 92, 190, 0.92), rgba(16, 42, 102, 0.92)); border: 1px solid rgba(150, 196, 255, 0.55);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25), 0 3px 10px rgba(0, 0, 0, 0.3);
  font-weight: 800; font-size: 17px; color: #fff; fill: #bfe0ff; font-variant-numeric: tabular-nums;
}
.progress { position: relative; flex: 1; min-width: 0; height: 10px; border-radius: 6px; background: rgba(4, 8, 18, 0.62); border: 1px solid rgba(255, 255, 255, 0.2); box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5); overflow: hidden; }
#progFill { position: absolute; inset: 0 auto 0 0; width: 0; background: linear-gradient(180deg, #a9d4ff 0%, #4c96ff 55%, #2f74e0 100%); border-radius: 6px; box-shadow: 0 0 8px rgba(90, 160, 255, 0.8); }
.prog-boss { position: absolute; right: 1px; top: 0; width: 8px; height: 8px; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #ffb0b6, #ff3344 60%, #8a0f1c); box-shadow: 0 0 8px #ff4455; }
.bossbar { position: relative; margin-top: 8px; height: 22px; border-radius: 8px; background: rgba(6, 10, 20, 0.78); border: 1px solid rgba(255, 90, 100, 0.55); overflow: hidden; }
#bossFill { position: absolute; inset: 0 auto 0 0; width: 100%; background: var(--danger-fill); transition: width 0.12s linear; }
.bossbar em { position: relative; display: block; font-style: normal; font-size: 16px; font-weight: 700; line-height: 20px; text-align: center; text-shadow: 0 1px 2px #000; }
.hud-score {
  display: flex; flex-direction: column; align-items: flex-end; min-width: 64px; padding: 4px 10px; border-radius: 12px;
  background: linear-gradient(180deg, rgba(34, 44, 66, 0.86), rgba(6, 10, 20, 0.86)); border: 1px solid rgba(255, 200, 110, 0.38);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14), 0 4px 12px rgba(0, 0, 0, 0.3);
}
.hud-score .lbl { font-size: 16px; color: var(--hud-label); line-height: 1.1; }
#hudScore { font-weight: 900; font-size: 20px; line-height: 1.2; font-variant-numeric: tabular-nums; color: #ffd47a; text-shadow: 0 1px 0 rgba(90, 40, 0, 0.8), 0 0 10px rgba(255, 180, 60, 0.35); }
.hint {
  position: absolute; left: 50%; bottom: calc(96px + var(--sab)); transform: translateX(-50%);
  padding: 12px 18px; border-radius: 999px; background: rgba(10, 16, 30, 0.9); border: 1px solid var(--line-strong);
  font-size: 16px; font-weight: 600; pointer-events: none; white-space: nowrap; animation: fadeIn 0.3s;
}

/* 8. World labels ------------------------------------------------------------- */
#labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.tag { position: absolute; left: 0; top: 0; transform: translate(-50%, -100%); will-change: transform; }
.tag-squad span {
  position: relative; display: inline-block; min-width: 44px; padding: 4px 12px; border-radius: 12px; text-align: center;
  background: linear-gradient(180deg, rgba(48, 104, 210, 0.95), rgba(18, 52, 128, 0.95)); border: 1px solid rgba(170, 210, 255, 0.85); color: #fff;
  font-weight: 900; font-size: 20px; font-variant-numeric: tabular-nums; box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 4px 12px rgba(0, 0, 0, 0.35);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
}
/* small gold chevron: the tag sits over the leader */
.tag-squad span::after { content: ''; position: absolute; left: 50%; bottom: -7px; width: 10px; height: 10px; margin-left: -5px; transform: rotate(45deg);
  background: linear-gradient(135deg, #ffe08a, #f2a516); border-right: 1px solid rgba(120, 60, 0, 0.6); border-bottom: 1px solid rgba(120, 60, 0, 0.6); }
.tag-squad.bump span { animation: bump 0.3s ease-out; }
.tag-squad.good span { background: rgba(20, 120, 80, 0.92); border-color: #8ff0c4; }
.tag-squad.bad span { background: rgba(160, 30, 45, 0.92); border-color: #ffb0b6; }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.25); } 100% { transform: scale(1); } }
.tag-boss span {
  display: inline-block; padding: 2px 10px; border-radius: 10px; background: rgba(90, 10, 20, 0.88); border: 1px solid rgba(255, 140, 150, 0.8);
  color: #fff; font-weight: 900; font-size: 24px; font-variant-numeric: tabular-nums; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}
.pop { position: absolute; transform: translate(-50%, -50%); font-weight: 900; font-size: 28px; -webkit-text-stroke: 1px rgba(0, 0, 0, 0.55); text-shadow: 0 2px 0 rgba(0, 0, 0, 0.45), 0 4px 10px rgba(0, 0, 0, 0.5); animation: floatUp 0.9s ease-out forwards; }
.pop.good { color: #9ff5cf; }
.pop.bad { color: #ffb3b9; font-size: 32px; animation: floatHold 1.8s ease-out forwards; }
/* losses hold at full strength before fading so the -N reads (r2 Low) */
@keyframes floatHold { 0%, 50% { opacity: 1; margin-top: 0; } 100% { opacity: 0; margin-top: -60px; } }
@keyframes floatUp { from { opacity: 1; margin-top: 0; } to { opacity: 0; margin-top: -60px; } }
@keyframes popFade { 0%, 55% { opacity: 1; } 100% { opacity: 0; } }
#flash { position: absolute; inset: 0; pointer-events: none; opacity: 0; background: #fff; }

/* 9. Legal footer ------------------------------------------------------------- */
.legal {
  position: absolute; left: 0; right: 0; bottom: 0; margin: 0; text-align: center; font-size: 16px; line-height: 1.35; color: #dce6f5;
  padding: 8px calc(16px + var(--sar)) calc(10px + var(--sab)) calc(16px + var(--sal));
  background: rgba(6, 10, 20, 0.82); border-top: 1px solid var(--line-dim); pointer-events: none;
}
.noscript { position: absolute; inset: 0; display: grid; place-items: center; font-size: 18px; }

/* 10. Responsive --------------------------------------------------------------- */
/* Narrow phones: drop the "Level N" prefix so the level name fits the HUD. */
@media (max-width: 359px) {
  .lvl-prefix { display: none; }
}
/* Short portrait (320x568 class): compact the title so every control is above the fold. */
@media (orientation: portrait) and (max-height: 700px) {
  .emblem, .tagline { display: none; }
  .brand { margin-bottom: 12px; }
  .brand h1 { font-size: 32px; }
  .btn-stack { gap: 8px; }
  .title-meta { margin-top: 10px; flex-wrap: nowrap; }
  .points-chip { min-width: 0; padding: 6px 10px; }
  .title-meta .btn { padding: 10px 12px; }
  .back-link { margin-bottom: -2px; }
  .panel { padding: 18px; }
}
/* Short landscape phones. */
@media (orientation: landscape) and (max-height: 500px) {
  /* compact HUD: a shorter top band leaves the road 2 s ahead visible below it (r2) */
  #hud { padding-top: calc(6px + var(--sat)); padding-bottom: 2px; }
  .hud-level { margin: 0 0 4px; }
  .screen { align-items: flex-start; padding-top: calc(10px + var(--sat)); }
  .panel { padding: 16px; }
  .brand { margin-bottom: 10px; }
  .emblem, .panel-title .tagline { display: none; }
  .brand h1 { font-size: 34px; }
  .panel-title { width: min(620px, 100%); }
  .panel-title { position: relative; }
  /* z-index: the title h1 has a filter (its own stacking layer) and comes later in the DOM */
  .panel-title .back-link { position: absolute; top: 8px; left: 8px; margin: 0; z-index: 1; }
  .panel-title .btn-stack { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .panel-title .btn-primary { grid-column: 1 / -1; }
  .title-meta { margin-top: 10px; }
  .stars svg { width: 40px; height: 40px; }
  .stars svg:nth-child(2) { width: 46px; height: 46px; }
  .stats { margin: 10px 0; }
  .panel-narrow { width: min(560px, 100%); }
  .panel-narrow .btn-stack { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .level-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #scrLevels .panel { width: min(720px, 100%); }
  .level-blurb { display: none; }
  .level-card { min-height: 64px; padding: 8px 12px; }
  .legal { padding-top: 4px; padding-bottom: calc(6px + var(--sab)); }
  .hint { top: calc(76px + var(--sat)); bottom: auto; }
}

/* 11. Reduced motion ----------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
  /* Gate result pops fade in place instead of vanishing. */
  .pop { animation: popFade 0.9s linear forwards !important; }
}
