:root {
  --bg: #f7f3ec;
  --surface: #ffffff;
  --ink: #1b1a17;
  --ink-soft: #6b665c;
  --line: #e4ddd0;
  --key: #ece6da;
  --key-press: #ddd5c5;
  --accent: #1f6f50;
  --accent-ink: #ffffff;
  --good: #3a9a5b;
  --miss: #d9a521;
  --fail: #d0503c;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 8px 24px rgba(40, 30, 10, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16140f;
    --surface: #211e18;
    --ink: #f3eee4;
    --ink-soft: #a39d90;
    --line: #34302877;
    --key: #2c2821;
    --key-press: #3a352c;
    --accent: #4fbf8b;
    --accent-ink: #0d1a14;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.app {
  max-width: 440px;
  margin: 0 auto;
  padding: 16px 16px calc(24px + env(safe-area-inset-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.brand {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 4px 2px 16px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 24px;
}
.logo {
  font-family: Fraunces, Georgia, serif;
  font-weight: 800;
  font-size: 28px;
  letter-spacing: -.02em;
}
.tag { color: var(--ink-soft); font-size: 14px; font-variant-numeric: tabular-nums; }

.screen { display: flex; flex-direction: column; flex: 1; }

/* Start */
.headline {
  font-family: Fraunces, Georgia, serif;
  font-weight: 600;
  font-size: clamp(32px, 9vw, 42px);
  line-height: 1.08;
  letter-spacing: -.02em;
  margin: 12px 0 24px;
}
.rules { list-style: none; padding: 0; margin: 0 0 32px; display: grid; gap: 12px; }
.rules li { position: relative; padding-left: 26px; line-height: 1.45; color: var(--ink-soft); }
.rules li strong { color: var(--ink); }
.rules li::before {
  content: ""; position: absolute; left: 4px; top: .55em;
  width: 8px; height: 8px; border-radius: 2px; background: var(--accent);
}
.fine { color: var(--ink-soft); font-size: 13px; text-align: center; margin-top: 12px; }
.fine.error { color: var(--fail); font-weight: 500; }

.btn {
  border: 0; border-radius: 999px; padding: 14px 22px; font-weight: 600;
  transition: transform .08s ease, background .15s ease, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.big { font-size: 18px; padding: 18px 24px; width: 100%; }
.btn.ghost {
  background: transparent; color: var(--ink-soft); font-size: 14px; font-weight: 500;
  align-self: center; margin-top: 14px; text-decoration: underline; text-underline-offset: 3px;
}
.btn:disabled { opacity: .4; cursor: default; }

/* Play */
.play-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.dots { display: flex; gap: 8px; list-style: none; padding: 0; margin: 0; }
.dots li {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--key); border: 1.5px solid var(--line);
}
.dots li.current { border-color: var(--ink); background: transparent; }
.dots li.first { background: var(--good); border-color: var(--good); }
.dots li.miss { background: var(--miss); border-color: var(--miss); }
.dots li.reveal { background: var(--fail); border-color: var(--fail); }
.timer {
  font-variant-numeric: tabular-nums; font-weight: 600; font-size: 20px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 4px 14px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 16px 24px;
  text-align: center;
  margin-bottom: 16px;
}
.qnum { color: var(--ink-soft); font-size: 13px; font-weight: 500; letter-spacing: .06em; text-transform: uppercase; }
.expr {
  font-family: Fraunces, Georgia, serif;
  font-weight: 600;
  font-size: clamp(30px, 10vw, 44px);
  font-variant-numeric: tabular-nums lining-nums;
  margin: 14px 0 18px;
  white-space: nowrap;
}
.expr .op { font-family: Inter, system-ui, sans-serif; font-weight: 500; color: var(--ink-soft); margin: 0 .08em; }
.answer.correct .caret, .answer.revealed .caret { display: none; }
.answer {
  height: 64px;
  display: flex; align-items: center; justify-content: center;
  font-size: 40px; font-weight: 700; font-variant-numeric: tabular-nums;
  border-radius: 12px;
  background: var(--bg);
  border: 2px solid var(--line);
  transition: background .15s, border-color .15s, color .15s;
}
.caret { width: 2px; height: 36px; background: var(--ink); margin-left: 2px; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.answer.correct { border-color: var(--good); color: var(--good); }
.answer.revealed { border-color: var(--fail); color: var(--fail); }
.answer.shake { animation: shake .32s ease; border-color: var(--fail); }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); } 40% { transform: translateX(7px); }
  60% { transform: translateX(-5px); } 80% { transform: translateX(3px); }
}

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: auto; }
.keypad button {
  height: 60px; border: 0; border-radius: 12px;
  background: var(--key); font-size: 24px; font-weight: 600;
  touch-action: manipulation; user-select: none;
}
.keypad button:active { background: var(--key-press); }
.keypad button.muted { color: var(--ink-soft); font-size: 20px; }

/* Result */
.eyebrow { color: var(--ink-soft); font-weight: 500; margin: 8px 0 4px; text-align: center; }
.score {
  font-family: Fraunces, Georgia, serif; font-weight: 800;
  font-size: clamp(44px, 14vw, 60px); letter-spacing: -.02em;
  text-align: center; font-variant-numeric: tabular-nums;
}
.score .sep { color: var(--ink-soft); font-weight: 600; margin: 0 6px; }
.squares { text-align: center; font-size: 26px; letter-spacing: 4px; margin: 6px 0 18px; }
.rank {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px; text-align: center; margin-bottom: 18px;
}
.rank .big { font-size: 22px; font-weight: 700; line-height: 1.3; }
.rank .big em { font-style: normal; color: var(--accent); }
.rank .small { color: var(--ink-soft); font-size: 14px; margin-top: 4px; }
.recap { list-style: none; padding: 0; margin: 22px 0 0; display: grid; gap: 6px; }
.recap li {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 12px; border-radius: 10px; background: var(--surface); border: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
.recap .mk { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.recap .mk.first { background: var(--good); } .recap .mk.miss { background: var(--miss); } .recap .mk.reveal { background: var(--fail); }
.recap .q { flex: 1; }
.recap .a { font-weight: 700; }
.next { text-align: center; color: var(--ink-soft); font-size: 14px; margin-top: 22px; }
.next strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.next .btn { margin-top: 10px; }

.toast {
  position: fixed; left: 50%; bottom: calc(28px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-weight: 600; font-size: 14px;
}

@media (max-height: 640px) {
  .keypad button { height: 50px; }
  .expr { margin: 8px 0 12px; }
  .answer { height: 54px; }
}
@media (prefers-reduced-motion: reduce) {
  .answer.shake { animation: none; }
  .caret { animation: none; }
}
