:root {
  --bg: #080B16;
  --panel: #0f1830;
  --panel2: #131f3d;
  --accent: #38bdf8;
  --accent2: #a78bfa;
  --cyan: #22d3ee;
  --gold: #fbbf24;
  --danger: #f87171;
  --text: #e2e8f0;
  --text-dim: #94a3b8;
  --radius: 16px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: 'Outfit', system-ui, -apple-system, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

#app-shell {
  position: fixed; inset: 0; z-index: 10;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
}

/* Below-fold SEO content stays in the DOM for crawlers/GEO but is
   visually behind the fixed full-viewport app shell above it — same
   pattern used across sibling GameCMB games. */
#seo-content {
  position: relative; z-index: 1;
  max-width: 780px; margin: 0 auto; padding: 40px 24px 80px;
  color: var(--text-dim); line-height: 1.7;
}
#seo-content h1 { color: var(--text); font-size: 1.6rem; }
#seo-content h2 { color: var(--text); font-size: 1.25rem; margin-top: 2em; }
#seo-content h3 { color: var(--text); font-size: 1.05rem; margin-top: 1.4em; }
#seo-content a { color: var(--accent); }

.screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  width: 100%; height: 100%;
}
.hidden { display: none !important; }

/* ---------- Landing ---------- */
#landing { align-items: center; justify-content: center; text-align: center; }
.landing-inner { max-width: 420px; width: 100%; padding: 24px; position: relative; }
.logo {
  font-size: 2.6rem; font-weight: 800; margin: 0 0 6px; letter-spacing: 1px;
  text-shadow: 0 0 24px rgba(56,189,248,0.5);
}
.logo span { color: var(--cyan); }
.tagline { color: var(--text-dim); margin: 0 0 20px; font-size: 0.95rem; }
.hero-diagram { width: 180px; margin: 0 auto 20px; filter: drop-shadow(0 0 10px rgba(34,211,238,0.6)); }
.hook-line { color: var(--text-dim); font-style: italic; margin-top: 22px; font-size: 0.85rem; }
.corner-btn { position: absolute; top: 0; right: 0; }

.menu-list { display: flex; flex-direction: column; gap: 10px; }

.btn {
  appearance: none; border: 1px solid rgba(148,163,184,0.25);
  background: rgba(19,31,61,0.85);
  color: var(--text); font-family: inherit; font-weight: 600; font-size: 0.95rem;
  padding: 14px 18px; border-radius: 12px; cursor: pointer;
  letter-spacing: 0.4px;
  transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
  min-height: 44px;
  backdrop-filter: blur(6px);
}
.btn:hover { border-color: rgba(56,189,248,0.5); background: rgba(30,45,80,0.9); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary {
  background: linear-gradient(135deg, #22d3ee, #38bdf8);
  color: #051019; border: none; box-shadow: 0 0 22px rgba(56,189,248,0.45);
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-lg { font-size: 1.05rem; padding: 16px 18px; }
.btn-quiet { background: transparent; border-color: rgba(148,163,184,0.18); color: var(--text-dim); }

.icon-btn {
  appearance: none; border: none; background: rgba(19,31,61,0.8);
  color: var(--text); font-size: 1.2rem; width: 44px; height: 44px;
  border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center;
  border: 1px solid rgba(148,163,184,0.2);
}
.icon-btn:hover { border-color: rgba(56,189,248,0.5); }

/* ---------- Level select ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; flex: 0 0 auto;
}
.topbar h2 { margin: 0; font-size: 1.1rem; letter-spacing: 1px; }
/* Flexbox + `flex-wrap`, not CSS Grid: on a 5-column CSS Grid with
   implicit `auto` rows, this browser resolves each row's track height
   from the items' box BEFORE `aspect-ratio` is applied to them, so every
   row ends up far shorter than the (correctly) square-rendered cells —
   the boxes overlap the row below, which is exactly the "numbers not
   lining up" glitch reported on desktop. A wrapping flex container sizes
   each wrapped line from its items' resolved (post aspect-ratio) size,
   so the same five-per-row square layout renders correctly. */
.level-grid {
  flex: 1 1 auto; overflow-y: auto; padding: 8px 18px 28px;
  display: flex; flex-wrap: wrap; gap: 10px;
}
.level-cell {
  flex: 0 0 calc((100% - 40px) / 5); aspect-ratio: 1; border-radius: 12px; border: 1px solid rgba(148,163,184,0.2);
  background: rgba(19,31,61,0.7); color: var(--text); font-weight: 700; font-size: 1rem;
  cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
/* Wraps the lock icon + level number in renderLevelGrid() without adding
   its own box — its children become direct flex items of .level-cell. */
.level-cell-inner { display: contents; }
.level-cell.cleared { border-color: rgba(34,211,238,0.6); box-shadow: inset 0 0 0 1px rgba(34,211,238,0.3); color: var(--cyan); }
/* Locked cells dim the NUMBER, not the whole box — a blanket opacity on
   the element crushed the border/background too, making adjacent locked
   cells blend into what looked like one merged, misaligned block. */
.level-cell.locked {
  background: rgba(10,15,30,0.55);
  border-color: rgba(148,163,184,0.1);
  color: rgba(148,163,184,0.4);
  cursor: not-allowed;
}
.level-cell.locked .lock-icon { opacity: 0.55; font-size: 0.85em; }
.level-cell:not(.locked):hover { border-color: var(--accent); }

/* ---------- Game screen ---------- */
#game-screen { padding: 0; }
.hud {
  flex: 0 0 auto; display: flex; align-items: flex-start; gap: 10px;
  padding: 14px 14px 6px;
}
.hud-center { flex: 1 1 auto; display: flex; flex-direction: column; gap: 6px; align-items: center; }
.hud-row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.hud-chip {
  background: rgba(19,31,61,0.8); border: 1px solid rgba(148,163,184,0.2);
  border-radius: 999px; padding: 5px 12px; font-size: 0.72rem; font-weight: 700;
  letter-spacing: 0.5px; color: var(--text-dim); white-space: nowrap;
}
.hud-time { color: var(--cyan); font-variant-numeric: tabular-nums; }
.hud-chip-gold { color: var(--gold); border-color: rgba(251,191,36,0.4); }

.board-wrap {
  flex: 1 1 auto; display: flex; align-items: center; justify-content: center;
  min-height: 0; padding: 4px 10px;
}
#board {
  border-radius: 20px; background: #0d1220;
  box-shadow: 0 0 0 1px rgba(148,163,184,0.12), 0 20px 60px -20px rgba(0,0,0,0.6), 0 0 60px -10px rgba(56,189,248,0.15);
  max-width: 100%; max-height: 100%; touch-action: none;
}
.bottom-bar { flex: 0 0 auto; display: flex; gap: 10px; justify-content: center; padding: 8px 16px 6px; }
.bottom-bar .btn { flex: 0 1 180px; }

.ad-slot {
  flex: 0 0 auto; display: flex; align-items: center; justify-content: center;
  color: rgba(148,163,184,0.35); font-size: 0.65rem; letter-spacing: 1px;
  min-height: 4px;
}
.ad-slot span { display: none; } /* placeholder only — real ad units render here later */

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(5,8,16,0.72); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal-panel {
  width: 100%; max-width: 380px; max-height: 88vh; overflow-y: auto;
  background: linear-gradient(160deg, rgba(19,31,61,0.97), rgba(11,17,35,0.97));
  border: 1px solid rgba(148,163,184,0.2); border-radius: 20px;
  padding: 26px 22px; box-shadow: 0 30px 80px -20px rgba(0,0,0,0.7);
  text-align: center;
}
.modal-panel h2 { margin: 0 0 14px; font-size: 1.3rem; letter-spacing: 0.5px; }
.modal-note { color: var(--text-dim); font-size: 0.85rem; margin: 0 0 14px; }
.howto-steps { text-align: left; list-style: none; padding: 0; margin: 0 0 16px; display: flex; flex-direction: column; gap: 8px; }
.howto-steps li { background: rgba(255,255,255,0.03); border-radius: 10px; padding: 10px 12px; font-size: 0.88rem; }
.howto-tag { color: var(--cyan); font-weight: 700; margin-bottom: 18px; }

.nick-input {
  width: 100%; padding: 12px 14px; border-radius: 10px; margin-bottom: 8px;
  border: 1px solid rgba(148,163,184,0.3); background: rgba(255,255,255,0.05);
  color: var(--text); font-size: 1rem; font-family: inherit; text-align: center;
}
.nick-error { color: var(--danger); font-size: 0.82rem; margin: 0 0 10px; }

.result-title { color: var(--gold); text-shadow: 0 0 18px rgba(251,191,36,0.5); }
.result-score { font-size: 2.2rem; font-weight: 800; margin-bottom: 4px; }
.result-pb { color: var(--cyan); font-weight: 700; margin-bottom: 12px; font-size: 0.9rem; }
.result-stats {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
  margin: 14px 0 18px; text-align: left;
}
.result-stats div { background: rgba(255,255,255,0.03); border-radius: 10px; padding: 8px 10px; }
.result-stats span { display: block; font-size: 0.65rem; color: var(--text-dim); letter-spacing: 0.5px; }
.result-stats b { font-size: 1rem; }
.result-actions { display: flex; flex-direction: column; gap: 8px; }

/* Nickname editor embedded directly in the result panel — scores submit
   automatically with the player's saved (or default) name so this screen
   is never blocked; this just lets them put their real name on the
   leaderboard entry that already exists, right here, with no separate
   modal to get stuck on. */
.result-nick { margin: 0 0 16px; text-align: left; }
.result-nick-label { display: block; font-size: 0.68rem; color: var(--text-dim); letter-spacing: 0.5px; margin-bottom: 6px; }
.result-nick-row { display: flex; gap: 8px; }
.result-nick-row .nick-input { margin-bottom: 0; flex: 1 1 auto; text-align: left; }
.result-nick-row .btn { flex: 0 0 auto; padding: 0 16px; }

.lb-tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.lb-tab {
  flex: 1; padding: 8px; border-radius: 10px; border: 1px solid rgba(148,163,184,0.2);
  background: transparent; color: var(--text-dim); font-weight: 700; font-size: 0.75rem; cursor: pointer;
}
.lb-tab.active { background: rgba(56,189,248,0.15); color: var(--accent); border-color: rgba(56,189,248,0.4); }
.lb-list { display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow-y: auto; }
.lb-row {
  display: grid; grid-template-columns: 36px 1fr auto; gap: 8px; align-items: center;
  padding: 8px 10px; border-radius: 10px; background: rgba(255,255,255,0.03); text-align: left;
}
.lb-top1 { background: rgba(251,191,36,0.12); }
.lb-top2 { background: rgba(203,213,225,0.1); }
.lb-top3 { background: rgba(217,119,6,0.1); }
.lb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.lb-score { font-weight: 700; color: var(--cyan); }
.lb-divider { height: 1px; background: rgba(148,163,184,0.15); margin: 10px 0; }
.lb-me { border: 1px solid rgba(56,189,248,0.4); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: rgba(19,31,61,0.95); border: 1px solid rgba(148,163,184,0.25);
  color: var(--text); padding: 10px 18px; border-radius: 999px; font-size: 0.85rem;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
  z-index: 60; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: rgba(248,113,113,0.5); color: #fecaca; }
.toast.success { border-color: rgba(34,211,238,0.5); color: #a5f3fc; }

@media (max-width: 480px) {
  .logo { font-size: 2.1rem; }
  .modal-panel { padding: 20px 16px; }
  .result-score { font-size: 1.8rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
