:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #232733;
  --line: #2c3140;
  --text: #e9ebf1;
  --muted: #8b91a3;
  --accent: #7c9cff;
  --accent-soft: rgba(124, 156, 255, 0.14);
  --on-accent: #0f1115;
  --good: #3ecf8e;
  --good-soft: rgba(62, 207, 142, 0.16);
  --bad: #ff6b6b;
  --bad-soft: rgba(255, 107, 107, 0.16);
  --gold: #f5c04b;
  --radius: 14px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f9;
    --surface: #ffffff;
    --surface-2: #e9ebf2;
    --line: #dde0ea;
    --text: #181b24;
    --muted: #5f6678;
    --accent: #4565f0;
    --accent-soft: rgba(69, 101, 240, 0.1);
    --on-accent: #ffffff;
    --good: #14a36a;
    --good-soft: rgba(20, 163, 106, 0.14);
    --bad: #e03e3e;
    --bad-soft: rgba(224, 62, 62, 0.12);
    --gold: #b98200;
    color-scheme: light;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button {
  font: inherit; color: inherit; background: none; border: 0;
  cursor: pointer; touch-action: manipulation; text-align: inherit;
}
button:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
kbd {
  font: 600 0.7rem/1 ui-monospace, monospace;
  padding: 3px 6px; border-radius: 6px;
  border: 1px solid var(--line); color: var(--muted);
}

#app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 16px;
}

.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.center { text-align: center; }

/* Keyboard hints only on devices with a mouse */
.kbd-only { display: none; }
@media (hover: hover) and (pointer: fine) { .kbd-only { display: inline-block; } p.kbd-only { display: block; } }

/* ---------- Header ---------- */
.top { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.top h1 { font-size: 1.4rem; font-weight: 750; letter-spacing: -0.01em; }
.icon-btn {
  width: 44px; height: 44px; flex: none; border-radius: 12px;
  display: grid; place-items: center;
  font-size: 1.5rem; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-spacer { width: 44px; flex: none; }

/* ---------- Cards & lists ---------- */
.card { background: var(--surface); border: 1px solid transparent; border-radius: var(--radius); }
.list { display: flex; flex-direction: column; gap: 8px; }
.section-title {
  font-size: 0.75rem; font-weight: 650; text-transform: uppercase;
  letter-spacing: 0.08em; color: var(--muted);
}

.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface); border-radius: var(--radius); padding: 14px 6px; text-align: center; }
.stat b { display: block; font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: 0.75rem; color: var(--muted); }

.game-card, .mode-card {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 16px; width: 100%;
  transition: transform 0.08s, border-color 0.15s;
}
.game-card:hover, .mode-card:hover { border-color: var(--accent); }
.game-card:active, .mode-card:active { transform: scale(0.985); }
.game-card b, .mode-card b { display: block; font-size: 1.05rem; }
.game-card small, .mode-card small { display: block; color: var(--muted); font-size: 0.85rem; line-height: 1.3; }
.glyph {
  width: 48px; height: 48px; flex: none; border-radius: 12px;
  display: grid; place-items: center;
  font-size: 1.7rem; background: var(--accent-soft); color: var(--accent);
}
.chev { color: var(--muted); font-size: 1.6rem; }
.soon { padding: 14px; text-align: center; color: var(--muted); background: none; border: 1px dashed var(--line); font-size: 0.9rem; }

.mi { width: 36px; flex: none; text-align: center; font-size: 1.4rem; }
.best { flex: none; text-align: right; font-size: 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.best b { display: block; font-size: 1.15rem; color: var(--text); letter-spacing: 0; font-variant-numeric: tabular-nums; }

/* Level selector */
.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 4px; background: var(--surface); border-radius: var(--radius); }
.seg button { padding: 8px 2px; border-radius: 10px; text-align: center; }
.seg button:hover:not(.on) { background: var(--surface-2); }
.seg b { display: block; font-size: 0.9rem; }
.seg small { font-size: 0.72rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.seg .on { background: var(--accent); color: var(--on-accent); }
.seg .on small { color: inherit; opacity: 0.75; }

/* Details panels */
details.card { padding: 14px 16px; }
details summary { cursor: pointer; font-weight: 600; list-style: none; display: flex; justify-content: space-between; }
details summary::-webkit-details-marker { display: none; }
details summary::after { content: '+'; color: var(--muted); font-weight: 400; }
details[open] summary::after { content: '−'; }
details[open] summary { margin-bottom: 12px; }
.tips p { margin-bottom: 10px; font-size: 0.92rem; }
.tips .example { background: var(--surface-2); padding: 10px 12px; border-radius: 10px; }
table.cubes { border-collapse: collapse; width: 100%; margin-bottom: 12px; font-size: 0.82rem; font-variant-numeric: tabular-nums; }
table.cubes th, table.cubes td { padding: 5px 2px; text-align: center; border-bottom: 1px solid var(--line); }
table.cubes th { color: var(--muted); font-weight: 600; text-align: left; }

.settings { display: flex; flex-direction: column; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; font-size: 0.92rem; cursor: pointer; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-height: 52px; padding: 0 20px; border-radius: var(--radius);
  background: var(--surface); font-weight: 650; text-align: center;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.primary kbd { color: inherit; border-color: currentColor; opacity: 0.6; }
.btn.small { min-height: 40px; font-size: 0.85rem; margin-top: 8px; }
.btn.danger { color: var(--bad); }

/* ---------- Play ---------- */
#app.playing { gap: 0; height: 100dvh; min-height: 0; user-select: none; -webkit-user-select: none; }
.hud { display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; }
.hud-main { text-align: center; font-size: 1.5rem; font-weight: 750; font-variant-numeric: tabular-nums; }
.hud-main.low { color: var(--bad); }
.hud-side { text-align: right; color: var(--muted); font-size: 0.85rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
.bar { height: 4px; margin: 10px 0 0; background: var(--surface-2); border-radius: 2px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.12s linear; }

.stage {
  flex: 1; min-height: 180px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  margin: 12px 0; border-radius: 20px;
}
.stage.ok { animation: flash-ok 0.35s ease-out; }
.stage.bad { animation: flash-bad 0.45s ease-out; }
@keyframes flash-ok { from { background: var(--good-soft); } to { background: transparent; } }
@keyframes flash-bad { from { background: var(--bad-soft); } to { background: transparent; } }

.prompt {
  font-size: clamp(2.6rem, 13vw, 4.4rem); font-weight: 750; letter-spacing: -0.02em;
  white-space: nowrap; font-variant-numeric: tabular-nums;
}
.prompt.pop { animation: pop 0.18s ease-out; }
@keyframes pop { from { transform: scale(0.94); opacity: 0.4; } to { transform: none; opacity: 1; } }
.prompt .op { color: var(--accent); font-weight: 400; margin-right: 0.06em; }

.answer {
  min-width: 4ch; padding: 2px 16px 6px;
  border-bottom: 3px solid var(--line);
  font-size: 2.6rem; font-weight: 700; text-align: center; font-variant-numeric: tabular-nums;
}
.answer.empty { color: var(--surface-2); }
.stage.bad .answer { animation: shake 0.3s; }
@keyframes shake { 20%, 60% { transform: translateX(-7px); } 40%, 80% { transform: translateX(7px); } }

.reveal { min-height: 1.4em; color: var(--bad); font-size: 1rem; font-weight: 600; text-align: center; opacity: 0; transition: opacity 0.3s; }
.reveal.show { opacity: 1; }

.hint { text-align: center; font-size: 0.78rem; color: var(--muted); margin-bottom: 10px; }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.keypad button {
  height: clamp(50px, 8.5dvh, 66px); border-radius: 14px;
  background: var(--surface); text-align: center;
  font-size: 1.6rem; font-weight: 600; font-variant-numeric: tabular-nums;
  transition: background 0.06s;
}
.keypad button:hover { background: var(--surface-2); }
.keypad button:active, .keypad button.press { background: var(--line); }
.keypad .fn { color: var(--muted); }
.keypad .go { background: var(--accent); color: var(--on-accent); }
.keypad .go:active, .keypad .go.press { filter: brightness(0.85); background: var(--accent); }

/* ---------- Results ---------- */
.result-hero { text-align: center; padding: 16px 0 4px; }
.r-title { font-size: 1.1rem; font-weight: 650; color: var(--muted); }
.r-score { font-size: 5rem; font-weight: 800; line-height: 1.05; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; }
.badge { display: inline-block; margin-top: 10px; padding: 6px 14px; border-radius: 999px; background: var(--gold); color: #1a1400; font-weight: 750; }
.badge span { font-weight: 500; opacity: 0.75; }
.result-hero .muted:last-child { margin-top: 6px; }

.misses { padding: 14px 16px; }
.misses ul { list-style: none; margin-top: 8px; }
.misses li { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-top: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.misses li:first-child { border-top: 0; }
.misses s { color: var(--bad); margin-right: 6px; }
.misses b { color: var(--good); }

.actions { display: flex; flex-direction: column; gap: 8px; margin-top: auto; }
