:root {
  color-scheme: light;
  font-family: Inter, ui-rounded, "Arial Rounded MT Bold", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --ink: #20223a;
  --muted: #656b82;
  --purple: #6756e8;
  --purple-dark: #5140d1;
  --purple-soft: #eeebff;
  --green: #168b63;
  --red: #c43f52;
  --card: rgba(255, 255, 255, .92);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 0%, #ffe5f0 0, transparent 28rem),
    radial-gradient(circle at 95% 12%, #dff5ff 0, transparent 30rem),
    linear-gradient(145deg, #f6f4ff, #fffaf2);
}
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

.app-shell { width: min(1180px, calc(100% - 28px)); margin: 0 auto; padding: 20px 0 48px; }
.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 48px; }
.back-link, .done-back { color: var(--purple-dark); font-weight: 850; text-decoration: none; }
.score-pill { display: flex; gap: 6px; align-items: center; padding: 9px 14px; border-radius: 999px; background: white; box-shadow: 0 8px 24px rgba(49, 40, 104, .1); font-size: 17px; }
.screen { animation: arrive .28s ease both; }
.start-screen, .done-screen { width: min(700px, 100%); margin: 30px auto 0; text-align: center; }
.hero-icon { display: grid; place-items: center; width: 82px; height: 82px; margin: 0 auto 15px; border-radius: 28px; background: white; box-shadow: 0 18px 50px rgba(63, 49, 143, .16); font-size: 42px; transform: rotate(-3deg); }
.eyebrow { margin: 0 0 8px; color: var(--purple); text-transform: uppercase; letter-spacing: .13em; font-size: 12px; font-weight: 900; }
h1 { margin: 0; font-size: clamp(38px, 7vw, 64px); line-height: 1; letter-spacing: -.055em; }
.intro { margin: 18px auto 25px; max-width: 610px; color: var(--muted); font-size: 18px; line-height: 1.55; }

.rule-card, .demo-card { border: 1px solid rgba(55, 46, 112, .09); border-radius: 24px; background: var(--card); box-shadow: 0 14px 45px rgba(54, 46, 106, .09); text-align: left; }
.rule-card { padding: 22px 25px; }
.rule-card h2 { margin: 0 0 13px; font-size: 18px; }
.rule-card ol { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 0; padding: 0; list-style: none; }
.rule-card li { display: flex; align-items: center; gap: 9px; min-height: 54px; line-height: 1.3; }
.rule-card b { color: var(--purple); font-size: 18px; }
.rule-number { flex: 0 0 auto; display: grid; place-items: center; width: 30px; height: 30px; border-radius: 10px; background: var(--purple-soft); color: var(--purple-dark); font-weight: 900; }
.demo-card { position: relative; margin-top: 14px; padding: 24px; }
.demo-label { position: absolute; top: 12px; right: 14px; color: #8a849f; font-size: 11px; font-weight: 900; letter-spacing: .1em; text-transform: uppercase; }
.demo-expression { margin: 4px 0 9px; font-size: clamp(24px, 5vw, 34px); font-weight: 900; letter-spacing: .02em; }
.demo-expression mark { padding: 2px 7px; border-radius: 8px; background: #fff0a8; color: inherit; }
.demo-card p { margin: 0; color: var(--muted); line-height: 1.5; }
.primary-button { min-width: 190px; margin-top: 22px; padding: 15px 28px; border: 0; border-radius: 16px; color: white; background: linear-gradient(135deg, #7868f2, #5744d9); box-shadow: 0 12px 28px rgba(87, 68, 217, .28); font-weight: 900; cursor: pointer; }
.primary-button:hover { transform: translateY(-1px); }

.game-screen { margin-top: 12px; }
.progress-row { display: grid; grid-template-columns: auto minmax(120px, 300px); justify-content: space-between; align-items: center; gap: 18px; margin: 10px 4px 18px; color: #555b73; font-size: 13px; font-weight: 850; }
.progress-track { height: 9px; overflow: hidden; border-radius: 99px; background: #dedbec; }
.progress-track span { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #806ef6, #d06fe1); transition: width .35s ease; }
.game-layout { display: grid; grid-template-columns: minmax(0, 1.12fr) minmax(330px, .88fr); gap: 20px; align-items: start; }
.question-card, .scratch-card { border: 1px solid rgba(55, 46, 112, .09); border-radius: 28px; background: var(--card); box-shadow: 0 18px 60px rgba(54, 46, 106, .11); }
.question-card { min-height: 590px; padding: clamp(22px, 4vw, 38px); text-align: center; }
.instruction { margin: 0; color: var(--muted); font-weight: 800; }
/* nowrap is load-bearing: the expression must stay on one line, and fitExpression()
   in game.js scales the type down when a long one would not otherwise fit. */
.expression { display: flex; justify-content: center; align-items: center; flex-wrap: nowrap; min-height: 104px; margin: 10px 0 5px; font-size: clamp(33px, 6vw, 55px); font-weight: 900; line-height: 1.4; letter-spacing: .025em; font-variant-numeric: tabular-nums; }
.expression > span, .expression > button { transition: color .18s ease, background .18s ease, transform .18s ease, box-shadow .18s ease; }
.expression .paren { color: #938cae; padding: 0 .05em; }
.expression .number { padding: .02em .1em; border-radius: 11px; }
.expression .operator-token {
  appearance: none;
  margin: 0;
  padding: .02em .14em;
  border: 0;
  border-radius: 11px;
  color: var(--purple);
  background: transparent;
  font: inherit;
  line-height: inherit;
  cursor: pointer;
}
.expression .operator-token:hover { background: #f0edff; transform: scale(1.06); }
.expression .operator-token:focus-visible { outline: 4px solid rgba(103, 86, 232, .2); outline-offset: 2px; }
.expression .selection-correct { color: #086e4c; background: #bcf1d7; box-shadow: 0 4px 14px rgba(22, 139, 99, .18); transform: scale(1.06); }
.expression .selection-wrong { color: #a5273b; background: #ffd7dd; box-shadow: 0 4px 14px rgba(196, 63, 82, .16); }
.expression .selection-correct.operator-token:hover, .expression .selection-wrong.operator-token:hover { background: inherit; }
.token-pop { animation: tokenPop .4s ease; }
.answer-area { margin: 13px auto 4px; max-width: 390px; padding: 15px; border-radius: 18px; background: #f0edff; }
.answer-area label { display: block; margin-bottom: 10px; color: #4c437b; font-size: 14px; font-weight: 900; }
.answer-display { display: grid; place-items: center; width: 168px; min-height: 58px; margin: 0 auto 12px; padding: 6px 12px; border: 2px solid #c9c0fa; border-radius: 13px; background: white; font-size: 32px; font-weight: 900; font-variant-numeric: tabular-nums; letter-spacing: .04em; }
.answer-display.empty { color: #b3aad4; }

/* Number pad, matching the one in K&K Quest (sites/number-quest style.css
   .quick-numpad). Same grid, tray and key sizing; the action key takes this
   game's purple where K&K uses its mint. */
.quick-numpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; max-width: 320px; margin: 0 auto; padding: 12px; border-radius: 20px; background: #d1d5db; }
.quick-numpad-btn { height: 68px; border: 0; border-radius: 14px; color: #1c1c1e; background: #f2f2f7; font-size: 32px; font-weight: 400; font-family: -apple-system, BlinkMacSystemFont, sans-serif; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; transition: background .1s, transform .1s; }
.quick-numpad-btn:active { background: #c7c7cc; transform: scale(.96); }
.quick-numpad-del { font-size: 22px; }
.quick-numpad-next { color: white; background: var(--purple); font-size: 18px; font-weight: 700; }
.quick-numpad-next:active { background: var(--purple-dark); }
.feedback { min-height: 48px; margin: 16px auto 0; padding: 11px 15px; border-radius: 14px; color: #605977; background: #f6f4fb; line-height: 1.4; font-weight: 750; }
.feedback.good { color: #08724f; background: #e6faef; }
.feedback.bad { color: #a72d40; background: #fff0f2; }
.working-history { margin-top: 21px; padding-top: 17px; border-top: 1px dashed #d9d5e5; text-align: left; }
.working-history h2 { margin: 0 0 8px; color: #777188; text-transform: uppercase; letter-spacing: .08em; font-size: 11px; }
.working-history ol { display: flex; flex-wrap: wrap; gap: 8px; margin: 0; padding: 0; list-style: none; }
.working-history li { padding: 7px 11px; border-radius: 10px; background: #f2f0fa; font-weight: 800; font-variant-numeric: tabular-nums; }

.scratch-card { padding: 17px; }
.scratch-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.scratch-title { margin: 0 0 2px; font-weight: 900; }
.scratch-subtitle { margin: 0; color: var(--muted); font-size: 12px; }
.scratch-actions { display: flex; gap: 6px; }
.scratch-actions button { padding: 7px 10px; border: 1px solid #ddd9e7; border-radius: 9px; color: #5d5871; background: #faf9fc; font-size: 12px; font-weight: 800; cursor: pointer; }
#scratchPad { display: block; width: 100%; height: 510px; border: 1px solid #ddd9e8; border-radius: 16px; background-color: #fffefb; background-image: linear-gradient(#e8e5ef 1px, transparent 1px), linear-gradient(90deg, #e8e5ef 1px, transparent 1px); background-size: 28px 28px; touch-action: none; cursor: crosshair; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
/* The pad is for drawing on, so nothing in that panel should be selectable... */
.scratch-card { -webkit-user-select: none; user-select: none; }
/* ...and while a stroke is in progress, a drag must not select the question
   text either. game.js adds is-drawing for the life of the stroke. */
body.is-drawing, body.is-drawing * { -webkit-user-select: none !important; user-select: none !important; }
.done-screen { margin-top: 70px; }
.done-back { display: block; margin-top: 18px; }

@keyframes arrive { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes tokenPop { 0% { transform: scale(.78); color: var(--purple); } 70% { transform: scale(1.08); } 100% { transform: scale(1); } }

@media (max-width: 800px) {
  .app-shell { width: min(100% - 20px, 700px); padding-top: 8px; }
  .rule-card ol { grid-template-columns: 1fr; gap: 5px; }
  .game-layout { grid-template-columns: 1fr; }
  .question-card { min-height: auto; }
  #scratchPad { height: 340px; }
  .start-screen { margin-top: 16px; }
}

@media (max-width: 480px) {
  .progress-row { grid-template-columns: 1fr; gap: 7px; }
  .progress-track { width: 100%; }
  .question-card { padding: 21px 14px; border-radius: 22px; }
  .expression { font-size: clamp(30px, 10vw, 42px); min-height: 90px; }
  .choice-button { flex: 1 1 120px; min-width: 0; font-size: 17px; }
}

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