:root {
  --ink: #17234c;
  --paper: #fffef9;
  --cream: #fff8df;
  --yellow: #ffdb45;
  --orange: #ff8652;
  --pink: #ff6d9f;
  --mint: #55dcc8;
  --green: #1e9a75;
  --red: #d94f67;
  --line: #23315e;
  font-family: "Trebuchet MS", "Avenir Next", system-ui, sans-serif;
  color: var(--ink);
  background: var(--cream);
  font-synthesis: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { min-height: 100vh; margin: 0; background: radial-gradient(circle at 85% 8%, rgba(255,255,255,.55) 0 8rem, transparent 8.1rem), linear-gradient(145deg, #54dbc9 0 23rem, var(--cream) 23rem); }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

.confetti { position: fixed; inset: 0; overflow: hidden; pointer-events: none; opacity: .32; }
.confetti i { position: absolute; width: 1.1rem; height: .35rem; border-radius: 999px; background: var(--pink); transform: rotate(34deg); }
.confetti i:nth-child(1) { left: 6%; top: 16%; }
.confetti i:nth-child(2) { right: 8%; top: 28%; background: var(--yellow); transform: rotate(-25deg); }
.confetti i:nth-child(3) { left: 14%; top: 65%; background: var(--orange); transform: rotate(75deg); }
.confetti i:nth-child(4) { right: 16%; top: 70%; background: var(--mint); }
.confetti i:nth-child(5) { left: 48%; top: 8%; background: var(--yellow); transform: rotate(90deg); }

.topbar, .shell, footer { position: relative; width: min(1120px, calc(100% - 2rem)); margin-inline: auto; }
.topbar { min-height: 6rem; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 1rem; }
.back-link { justify-self: start; padding: .65rem .9rem; border: 2px solid var(--line); border-radius: 999px; background: rgba(255,255,255,.72); color: inherit; font-size: .82rem; font-weight: 900; text-decoration: none; }
.brand { display: flex; align-items: center; gap: .7rem; }
.brand-mark { display: grid; place-items: center; width: 3rem; aspect-ratio: 1; border: 3px solid var(--line); border-radius: 1rem; background: var(--yellow); box-shadow: 4px 4px 0 var(--line); font-size: 1.7rem; font-weight: 900; }
.brand strong, .brand small { display: block; line-height: 1; }
.brand small { margin-top: .3rem; font-size: .68rem; font-weight: 900; letter-spacing: .11em; text-transform: uppercase; }
.offline-pill { justify-self: end; font-size: .73rem; font-weight: 900; }

.shell { min-height: calc(100vh - 9rem); padding: 1rem 0 2rem; }
.view { animation: enter .32s ease both; }
@keyframes enter { from { opacity: 0; transform: translateY(8px); } }

.setup-view { display: grid; grid-template-columns: minmax(0, .86fr) minmax(36rem, 1.14fr); gap: clamp(2rem, 5vw, 5rem); align-items: center; }
.eyebrow { display: inline-flex; padding: .42rem .7rem; border: 2px solid var(--line); border-radius: .55rem; background: var(--yellow); box-shadow: 3px 3px 0 var(--line); font-size: .7rem; font-weight: 900; letter-spacing: .13em; }
h1 { margin: 1.2rem 0 1rem; font-size: clamp(3rem, 6vw, 5.25rem); line-height: .94; letter-spacing: -.065em; }
h1 mark { color: inherit; background: linear-gradient(transparent 58%, var(--yellow) 58%); }
.intro-copy p { max-width: 31rem; font-size: 1.16rem; line-height: 1.55; font-weight: 700; }

.setup-card, .question-card, .result-card { border: 3px solid var(--line); border-radius: 2rem; background: rgba(255,255,255,.96); box-shadow: 10px 10px 0 var(--line); }
.setup-card { padding: clamp(1.35rem, 3vw, 2.2rem); }
.step-heading { display: flex; align-items: center; gap: .75rem; }
.step-heading > span { display: grid; place-items: center; width: 2.2rem; aspect-ratio: 1; border-radius: 50%; background: var(--line); color: white; font-weight: 900; }
.step-heading strong, .step-heading small { display: block; }
.step-heading strong { font-size: 1.13rem; }
.step-heading small { margin-top: .1rem; opacity: .65; font-size: .75rem; font-weight: 800; }
.time-heading { margin-top: 1.5rem; }
.table-picker { display: grid; grid-template-columns: repeat(5, 1fr); gap: .55rem; margin-top: 1rem; }
.table-chip { min-height: 4rem; padding: .4rem; border: 2px solid #cad0df; border-radius: .85rem; background: white; }
.table-chip small, .table-chip strong { display: block; }
.table-chip small { font-size: .52rem; font-weight: 900; opacity: .6; }
.table-chip strong { margin-top: .12rem; font-size: 1.45rem; }
.table-chip.selected { border-color: var(--line); background: var(--yellow); box-shadow: 3px 3px 0 var(--line); transform: translate(-1px,-1px); }
.selection-actions { display: flex; gap: .5rem; margin-top: .75rem; }
.text-button { padding: .25rem; border: 0; background: transparent; font-size: .75rem; font-weight: 900; text-decoration: underline; }
.duration-picker { display: grid; grid-template-columns: repeat(3, 1fr); gap: .65rem; margin-top: 1rem; }
.duration-picker button { padding: .75rem .4rem; border: 2px solid #cad0df; border-radius: .9rem; background: white; }
.duration-picker strong, .duration-picker span { display: block; }
.duration-picker strong { font-size: 1.25rem; }
.duration-picker span { font-size: .66rem; font-weight: 800; }
.duration-picker button.selected { border-color: var(--line); background: #bdf4e8; box-shadow: 3px 3px 0 var(--line); }
.best-line { min-height: 2.2rem; display: flex; align-items: center; gap: .45rem; margin-top: 1rem; padding: .55rem .7rem; border-radius: .65rem; background: #f2f4fa; font-size: .73rem; font-weight: 800; }
.best-line > span:first-child { color: #f3af00; font-size: 1.15rem; }
.primary-button, .secondary-button { width: 100%; min-height: 3.7rem; border: 3px solid var(--line); border-radius: 1rem; font-weight: 900; }
.primary-button { display: flex; align-items: center; justify-content: center; gap: .7rem; margin-top: 1rem; background: var(--orange); box-shadow: 5px 5px 0 var(--line); }
.primary-button:hover, .primary-button:focus-visible { transform: translateY(-2px); box-shadow: 5px 7px 0 var(--line); outline: none; }
.primary-button span { font-size: 1.3rem; }
.warning { margin: .8rem 0 0; color: #a22f46; font-size: .8rem; font-weight: 900; text-align: center; }

.game-view { width: min(760px, 100%); margin-inline: auto; }
.game-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.quit-button { padding: .6rem .85rem; border: 2px solid var(--line); border-radius: 999px; background: white; font-size: .78rem; font-weight: 900; }
.stats { display: flex; gap: .65rem; }
.stats > span { min-width: 5.2rem; padding: .45rem .75rem; border: 2px solid var(--line); border-radius: .8rem; background: white; text-align: center; }
.stats small, .stats strong { display: block; }
.stats small { font-size: .56rem; font-weight: 900; letter-spacing: .08em; }
.stats strong { margin-top: .1rem; font-size: 1.3rem; }
.timer-block { margin: 1.2rem 0; padding: 1rem 1.2rem; border: 3px solid var(--line); border-radius: 1.15rem; background: var(--yellow); }
.timer-row { display: flex; justify-content: space-between; align-items: end; font-weight: 900; }
.timer-row > span { font-size: .84rem; }
.timer-row strong { font-size: 1.7rem; line-height: 1; font-variant-numeric: tabular-nums; }
.timer-row small { font-size: .8rem; }
.timer-track { height: .65rem; margin-top: .7rem; overflow: hidden; border: 2px solid var(--line); border-radius: 999px; background: white; }
.timer-track span { display: block; width: 100%; height: 100%; transform-origin: left; background: var(--green); transition: background .2s; }
.timer-track span.urgent { background: var(--red); }
.question-card { padding: clamp(1.4rem, 5vw, 3rem); text-align: center; }
.question-label { font-size: .72rem; font-weight: 900; letter-spacing: .18em; color: #7f4ad9; }
.equation { display: flex; align-items: center; justify-content: center; gap: clamp(.45rem, 2vw, 1rem); margin: 1.2rem 0 .6rem; font-size: clamp(2.7rem, 10vw, 5.5rem); font-weight: 900; letter-spacing: -.05em; }
.operator { color: #73809e; }
.answer-box input { width: 1.65em; height: 1.25em; padding: 0 .05em; border: 4px solid var(--line); border-radius: .2em; background: #fff8df; font-size: inherit; font-weight: inherit; text-align: center; outline: none; }
.answer-box input:focus { box-shadow: 0 0 0 5px rgba(85,220,200,.45); }
.answer-box input.correct { border-color: var(--green); background: #d9faee; }
.answer-box input.incorrect { border-color: var(--red); background: #fff0f3; }
.feedback { min-height: 1.7rem; font-weight: 900; color: #65708a; }
.feedback.success { color: var(--green); }
.feedback.gentle { color: #a23e54; }
.keypad { width: min(22rem, 100%); display: grid; grid-template-columns: repeat(3, 1fr); gap: .55rem; margin: 1.2rem auto 0; }
.keypad button { min-height: 3.4rem; border: 2px solid var(--line); border-radius: .8rem; background: #f5f6fa; font-size: 1.2rem; font-weight: 900; box-shadow: 2px 2px 0 var(--line); }
.keypad button:active { transform: translate(2px,2px); box-shadow: none; }
.keypad .validate-key { background: var(--mint); }

.result-view { width: min(760px, 100%); margin-inline: auto; }
.result-card { position: relative; padding: clamp(1.6rem, 5vw, 3rem); text-align: center; }
.result-star { position: absolute; right: 1.8rem; top: 1.4rem; color: #efad00; font-size: 2.4rem; transform: rotate(12deg); }
.result-card h2 { max-width: 34rem; margin: 1.2rem auto .5rem; font-size: clamp(2rem, 5vw, 3.2rem); line-height: 1; letter-spacing: -.04em; }
.result-card > p { margin: .8rem auto; font-weight: 700; }
.result-score { width: 12rem; aspect-ratio: 1; display: grid; place-content: center; margin: 1.4rem auto; border: 4px solid var(--line); border-radius: 50%; background: var(--yellow); box-shadow: 7px 7px 0 var(--line); }
.result-score strong, .result-score span { display: block; }
.result-score strong { font-size: 4rem; line-height: .9; }
.result-score span { max-width: 7rem; margin-top: .4rem; font-size: .75rem; font-weight: 900; }
.result-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: .65rem; }
.result-stats > div { padding: .85rem .5rem; border: 2px solid #cbd0de; border-radius: .9rem; }
.result-stats strong, .result-stats span { display: block; }
.result-stats strong { font-size: 1.5rem; }
.result-stats span { margin-top: .2rem; font-size: .67rem; font-weight: 800; }
.record-message { padding: .7rem; border-radius: .7rem; background: #d9faee; color: #127155; }
.mistakes-block { margin-top: 1rem; padding: 1rem; border: 2px dashed #aeb6ca; border-radius: 1rem; text-align: left; }
.mistakes-block h3 { margin: 0 0 .7rem; font-size: .9rem; }
.mistakes-block ul { display: flex; flex-wrap: wrap; gap: .45rem; margin: 0; padding: 0; list-style: none; }
.mistakes-block li { padding: .42rem .65rem; border-radius: .55rem; background: #f2f4fa; font-weight: 900; }
.result-actions { display: grid; grid-template-columns: 1.4fr 1fr; gap: .8rem; margin-top: 1rem; }
.secondary-button { margin-top: 1rem; background: white; }

footer { padding: 1rem 0 2rem; font-size: .72rem; font-weight: 800; text-align: center; opacity: .65; }

@media (max-width: 820px) {
  body { background: linear-gradient(155deg, #54dbc9 0 17rem, var(--cream) 17rem); }
  .topbar { grid-template-columns: auto 1fr; min-height: 5.2rem; }
  .brand { justify-self: end; }
  .offline-pill { display: none; }
  .setup-view { grid-template-columns: 1fr; gap: 1.2rem; }
  .intro-copy { text-align: center; }
  .intro-copy p { margin-inline: auto; }
  .setup-card, .question-card, .result-card { border-radius: 1.4rem; box-shadow: 6px 6px 0 var(--line); }
}

@media (max-width: 520px) {
  .topbar, .shell, footer { width: min(100% - 1rem, 1120px); }
  .back-link span { display: none; }
  .brand-mark { width: 2.5rem; }
  .brand strong { font-size: .9rem; }
  .brand small { font-size: .55rem; }
  .shell { padding-top: .25rem; }
  h1 { font-size: 2.7rem; }
  .intro-copy p { font-size: .95rem; }
  .table-picker { gap: .4rem; }
  .table-chip { min-height: 3.4rem; }
  .duration-picker { gap: .4rem; }
  .game-toolbar { align-items: start; }
  .stats > span { min-width: 4.2rem; padding: .4rem; }
  .question-card { padding-inline: .8rem; }
  .equation { gap: .32rem; font-size: clamp(2.25rem, 13.5vw, 4rem); }
  .answer-box input { border-width: 3px; }
  .result-stats { grid-template-columns: 1fr; }
  .result-actions { grid-template-columns: 1fr; }
  .secondary-button { margin-top: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
}
