/* Kids Love Math — ink on paper, one problem at a time. */

/* Andika (SIL) is drawn for beginning readers: plain zero, schoolbook digits. */
@font-face {
  font-family: 'Andika';
  src: url('../fonts/andika-400.woff2') format('woff2');
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: 'Andika';
  src: url('../fonts/andika-700.woff2') format('woff2');
  font-weight: 700;
  font-display: swap;
}

:root {
  --paper: #ffffff;
  --ink: #000000;
  --muted: #555555;
  --grid: #d6dadf;
  --right: #00703c;
  --wrong: #b3001b;
  --gold: #f4b400;

  --rule: 2px;
  --radius: 10px;
  --side-w: 256px;
  --pad-w: clamp(220px, 25vw, 340px);

  --font-ui: 'Andika', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-num: 'Andika', system-ui, -apple-system, 'Segoe UI', sans-serif;

  /* The visible screen height. app.js replaces this with window.innerHeight, which
     excludes the browser's toolbars even where 100dvh isn't supported. */
  --app-h: 100vh;

  color-scheme: light;
}

@supports (height: 100dvh) {
  :root { --app-h: 100dvh; }
}

/* Dark colours follow the device unless the light/dark switch has picked one. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #000000;
    --ink: #ffffff;
    --muted: #b8b8b8;
    --grid: #32363c;
    --right: #4fd38c;
    --wrong: #ff6b7d;
    --gold: #f6c33c;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --paper: #000000;
  --ink: #ffffff;
  --muted: #b8b8b8;
  --grid: #32363c;
  --right: #4fd38c;
  --wrong: #ff6b7d;
  --gold: #f6c33c;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

html { touch-action: manipulation; -webkit-text-size-adjust: 100%; }

body {
  background: var(--paper);
  color: var(--ink);
  font: 400 18px/1.3 var(--font-ui);
  overflow: hidden;
  overscroll-behavior: none;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

svg.glyph {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ── Layout ─────────────────────────────────────────────────────────────── */

.app {
  display: grid;
  height: var(--app-h);
  grid-template-columns: auto minmax(0, 1fr) var(--pad-w);
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "side main pad";
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  user-select: none;
  -webkit-user-select: none;
}

/* ── Buttons ────────────────────────────────────────────────────────────── */

.btn {
  appearance: none;
  margin: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 52px;
  padding: 0 16px;
  background: var(--paper);
  color: var(--ink);
  border: var(--rule) solid var(--ink);
  border-radius: var(--radius);
  font: 700 19px/1 var(--font-ui);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

.btn[aria-pressed="true"],
.btn[aria-current="true"],
.btn[aria-current="page"],
.btn:active,
.btn.is-pressed {
  background: var(--ink);
  color: var(--paper);
}

.btn:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 3px;
  position: relative;
  z-index: 1;
}

@media (hover: hover) {
  .btn:not(:disabled):hover { box-shadow: inset 0 0 0 2px var(--ink); }
}

.btn-primary {
  min-height: 60px;
  border-width: 3px;
  font-size: 20px;
  font-weight: 700;
}

.btn svg.glyph { width: 28px; height: 28px; }

/* ── Settings sidebar ───────────────────────────────────────────────────── */

/* Control heights and spacing scale with the screen height, so the whole sidebar
   fits without scrolling down to about 580px; at 560px and below it switches to the
   compact layout further down. Scrolling stays available as a last resort. */
.side {
  --ctl: clamp(40px, calc(var(--app-h) * 0.07), 58px);
  --gap: clamp(8px, calc(var(--app-h) * 0.024), 22px);
  grid-area: side;
  width: var(--side-w);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  min-height: 0;
  padding: 16px;
  border-right: var(--rule) solid var(--ink);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

/* Who is practising: the student's name, and under it a quiet way to sign out. */
.who {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin-bottom: -4px;
}

.brand {
  margin: 0;
  font: 700 24px/1.1 var(--font-ui);
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}

.sign-out {
  appearance: none;
  min-height: 28px;
  margin: 0;
  padding: 0;
  background: none;
  border: 0;
  color: var(--muted);
  font: 400 15px/1 var(--font-ui);
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.sign-out:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }

.ops { display: flex; flex-direction: column; gap: calc(var(--gap) * 0.4); }

.op {
  justify-content: flex-start;
  gap: 14px;
  min-height: var(--ctl);
  font-size: 20px;
}

.op .sym {
  width: 28px;
  font: 700 34px/1 var(--font-num);
  text-align: center;
}

.field { display: flex; flex-direction: column; gap: 6px; }

.field-label { font-size: 16px; color: var(--muted); }

/* Segmented control: joined buttons, the chosen one filled. */
.seg { display: flex; }

.seg > .btn {
  flex: 1 1 0;
  min-width: 44px;
  min-height: var(--ctl, 52px);
  padding: 0;
  border-radius: 0;
  font: 700 22px/1 var(--font-num);
}

.seg > .btn + .btn { border-left-width: 0; }
.seg > .btn:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.seg > .btn:last-child { border-radius: 0 var(--radius) var(--radius) 0; }

.seg > .btn:disabled {
  color: var(--muted);
  background: repeating-linear-gradient(135deg, transparent 0 7px, var(--grid) 7px 9px);
  cursor: not-allowed;
}

.new { width: 100%; min-height: var(--ctl); }

.dir { margin-top: auto; }

/* ── Settings drawer ────────────────────────────────────────────────────── */

/* In landscape the sidebar is a drawer; app.js opens and closes it. Closed, it slides off
   to the left, and because its grid column is sized by the sidebar, the problem grows
   into the room. */
.side { transition: margin-left 0.3s ease; }

@media (orientation: landscape) {
  .app.is-drawer-closed .side {
    margin-left: calc(-1 * var(--side-w));
    visibility: hidden;
    transition: margin-left 0.3s ease, visibility 0s 0.3s;
  }
}

/* The toggle starts the message line, so it stays in place while the drawer moves. */
.drawer-toggle { flex: none; width: 52px; min-height: 44px; padding: 0; }
.drawer-toggle svg.glyph { width: 26px; height: 26px; transition: transform 0.3s ease; }
.app.is-drawer-closed .drawer-toggle svg.glyph { transform: rotate(180deg); }

/* ── Main: score strip and the problem ──────────────────────────────────── */

.main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

/* Top line of the main area: the tutor's message on the left, the score on the right.
   When the score needs the room, the message wraps onto its own line. */
.topline {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 24px;
  min-height: 62px;
  padding: 14px 20px 0;
}

.marquee {
  flex: 1 1 240px;
  min-width: 0;
  overflow: hidden;
  font: 700 22px/1.3 var(--font-ui);
}

.marquee-text { display: inline-block; white-space: nowrap; }

/* Soft edges while the message moves; none once it rests, so its first letters stay crisp. */
.marquee.is-moving {
  -webkit-mask-image: linear-gradient(to right, transparent, #000 28px, #000 calc(100% - 28px), transparent);
  mask-image: linear-gradient(to right, transparent, #000 28px, #000 calc(100% - 28px), transparent);
}

.marquee.is-moving .marquee-text { will-change: transform; }

.marquee.is-still .marquee-text {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
}

.score {
  flex: 0 1 auto;
  margin-left: auto;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 22px;
}

.tier { display: flex; align-items: center; }

.tier .ico { width: 36px; height: 36px; flex: none; }
.tier .ico + .ico { margin-left: -15px; }

.tier .count { margin-left: 6px; font: 700 20px/1 var(--font-num); }

.score-hint {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 34em;
  color: var(--muted);
  font-size: 16px;
}

.score-hint .ico { width: 30px; height: 30px; flex: none; }

svg.ico {
  fill: var(--gold);
  stroke: var(--ink);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
  overflow: visible;
}

svg.ico .line { fill: none; }
svg.ico .ribbon { fill: var(--paper); }

.ico.is-new { animation: pop 0.45s cubic-bezier(0.3, 1.6, 0.5, 1) both; }

.tier.is-merging .ico {
  transform: translateX(-28px) scale(0.3);
  opacity: 0;
  transition: transform 0.38s ease-in, opacity 0.38s ease-in;
}

.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: clamp(16px, 4vmin, 48px);
  overflow: hidden;
}

/* The problem sits on squared paper: one digit per cell, so place values line up. */
.sheet {
  --cell: 80px;
  --cols: 4;
  --w: 3;
  --btn: 160px;
  --stroke: max(3px, calc(var(--cell) * 0.05));
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell)) calc(var(--cell) * 0.35) var(--btn);
  grid-template-rows: repeat(4, var(--cell));
  font-family: var(--font-num);
  font-size: calc(var(--cell) * 0.86);
  font-weight: 700;
  line-height: 1;
}

.paper {
  grid-row: 1 / span 4;
  grid-column: 1 / span var(--cols);
  background-image:
    linear-gradient(to right, var(--grid) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
  box-shadow: inset -1px -1px 0 var(--grid);
}

.row {
  grid-column: 2 / span var(--w);
  display: flex;
  justify-content: flex-end;
  position: relative;
}

.row-top { grid-row: 1; }
.row-bottom { grid-row: 2; }
.answer { grid-row: 3; }
.row-solution { grid-row: 4; color: var(--right); visibility: hidden; }

.d {
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  position: relative;
}

.sign {
  grid-row: 2;
  grid-column: 1;
  display: grid;
  place-items: center;
}

.total {
  grid-row: 2;
  grid-column: 1 / span var(--cols);
  align-self: end;
  height: var(--stroke);
  margin-bottom: calc(var(--stroke) / -2);
  background: var(--ink);
  border-radius: 99px;
  position: relative;
  z-index: 1;
}

/* The answer field: a ruled box under the line, typed into from the keypad. */
.answer { cursor: text; outline: none; }

.answer::before {
  content: '';
  position: absolute;
  inset: calc(var(--cell) * 0.12) calc(var(--cell) * 0.04) calc(var(--cell) * 0.04);
  border: max(2px, calc(var(--cell) * 0.04)) solid var(--ink);
  border-radius: calc(var(--cell) * 0.14);
  pointer-events: none;
}

.answer:focus-visible::before {
  outline: 3px solid var(--ink);
  outline-offset: 4px;
}

.answer .d { padding-top: calc(var(--cell) * 0.08); }

/* Left-to-right: a caret at the right edge. Right-to-left: the next empty cell is underlined. */
.answer .caret {
  position: absolute;
  right: calc(var(--cell) * 0.13);
  top: 28%;
  bottom: 18%;
  width: max(2px, calc(var(--cell) * 0.035));
  background: currentColor;
  animation: blink 1.1s steps(1) infinite;
}

.answer .next::after {
  content: '';
  position: absolute;
  left: 24%;
  right: 24%;
  bottom: 20%;
  height: max(3px, calc(var(--cell) * 0.05));
  background: currentColor;
  border-radius: 99px;
  animation: blink 1.1s steps(1) infinite;
}

.answer.is-nudged { animation: nudge 0.3s ease-in-out; }

.mark {
  grid-row: 3;
  grid-column: 1;
  display: grid;
  place-items: center;
  padding-top: calc(var(--cell) * 0.08);
}

.mark svg.glyph { width: 70%; height: 70%; stroke-width: 3.2; }

/* Lined up with the answer box, but never shorter than a comfortable touch target. */
.action {
  grid-row: 3;
  grid-column: -2 / -1;
  align-self: center;
  height: max(48px, calc(var(--cell) * 0.84));
  min-height: 0;
  margin: calc(var(--cell) * 0.08) 0 0;
  padding: 0 12px;
  font-size: clamp(18px, calc(var(--cell) * 0.26), 40px);
  white-space: nowrap;
}

.sheet.is-right .answer,
.sheet.is-right .mark { color: var(--right); }
.sheet.is-right .answer::before { border-color: var(--right); }
.sheet.is-right .mark svg { animation: pop 0.4s cubic-bezier(0.3, 1.6, 0.5, 1) both; }

.sheet.is-wrong .answer,
.sheet.is-wrong .mark { color: var(--wrong); }
.sheet.is-wrong .answer::before { border-color: var(--wrong); }
.sheet.is-wrong .row-solution { visibility: visible; }

/* ── Keypad ─────────────────────────────────────────────────────────────── */

.pad {
  grid-area: pad;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 16px;
  border-left: var(--rule) solid var(--ink);
}

/* Language and light/dark switches, top right above the keys. */
.prefs { align-self: flex-end; display: flex; gap: 8px; }
/* A fixed width, not a flex basis: Firefox sizes the group from the buttons' min-width
   and would let the buttons spill out of the window. All four fit the narrowest keypad. */
.prefs .btn { flex: none; width: min(56px, calc((var(--pad-w) - 40px) / 4)); min-width: 0; min-height: 44px; }
.prefs svg.glyph { width: 24px; height: 24px; stroke-width: 2.2; }
.lang > .btn { font: 700 17px/1 var(--font-ui); }

.keys {
  margin-block: auto;
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-areas:
    "k7 k8 k9"
    "k4 k5 k6"
    "k1 k2 k3"
    "kc k0 kb";
}

.key {
  aspect-ratio: 1 / 1;
  /* Four rows must fit under the switches: padding 32 + switch row 56 + gaps 30. */
  max-height: calc((var(--app-h) - 118px) / 4);
  min-height: 0;
  padding: 0;
  border-radius: 14px;
  font: 700 clamp(30px, 5vmin, 50px)/1 var(--font-num);
}

.key svg.glyph { width: 42%; height: auto; stroke-width: 2; }

/* ── Portrait: settings on top, keypad at the bottom ────────────────────── */

@media (orientation: portrait) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "side" "main" "pad";
  }

  .side {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
    grid-template-areas:
      "who who who"
      "ops ops new"
      "top bottom dir";
    align-items: end;
    gap: 14px 16px;
    padding: 14px 16px;
    width: auto;
    border-right: 0;
    border-bottom: var(--rule) solid var(--ink);
    overflow: visible;
  }

  .drawer-toggle { display: none; }

  /* The name and Sign out share one line across the top. */
  .who {
    grid-area: who;
    flex-direction: row;
    align-items: baseline;
    justify-content: space-between;
    gap: 16px;
    min-width: 0;
    margin: 0 0 -6px;
  }

  .brand { min-width: 0; font-size: 20px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .prefs .btn { width: 56px; }

  .ops { grid-area: ops; flex-direction: row; }
  .op { flex: 1 1 0; justify-content: center; min-width: 0; padding: 0 8px; }

  .field-top { grid-area: top; }
  .field-bottom { grid-area: bottom; }
  .new { grid-area: new; width: auto; min-height: 58px; white-space: nowrap; }
  .dir { grid-area: dir; margin-top: 0; }
  .dir .field-label { white-space: nowrap; }

  .pad {
    padding: 12px 16px;
    border-left: 0;
    border-top: var(--rule) solid var(--ink);
  }

  .keys {
    grid-template-columns: repeat(6, minmax(0, 1fr));
    grid-template-areas:
      "k1 k2 k3 k4 k5 kb"
      "k6 k7 k8 k9 k0 kc";
  }

  .key { aspect-ratio: auto; height: clamp(60px, calc(var(--app-h) * 0.085), 100px); max-height: none; }
}

/* The operation symbols say enough on their own when the bar is narrow. */
@media (orientation: portrait) and (max-width: 999px) {
  .op .word { display: none; }
  .op .sym { width: auto; font-size: 38px; }
}

/* Phones: three compact rows of settings so the problem keeps most of the screen. */
@media (orientation: portrait) and (max-width: 599px) {
  .side {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-areas:
      "who who"
      "ops ops"
      "top bottom"
      "new dir";
    gap: 10px 12px;
    padding: 12px;
  }

  .op, .new { min-height: 50px; }
  .seg > .btn { min-width: 0; min-height: 46px; font-size: 20px; }
  .field { gap: 4px; }
  .field-label { font-size: 15px; }

  .topline { gap: 6px 14px; min-height: 44px; padding: 8px 12px 0; }
  .marquee { flex-basis: 160px; font-size: 18px; }
  .score { gap: 6px 14px; }
  .score-hint { font-size: 15px; }
  .tier .ico { width: 28px; height: 28px; }
  .tier .ico + .ico { margin-left: -12px; }

  .pad { gap: 8px; padding: 10px 12px; }
  .keys { gap: 8px; }
}

/* Short landscape screens (phones on their side): the operations become one row of
   symbols, which is what lets the sidebar fit below ~560px. */
@media (orientation: landscape) and (max-height: 560px) {
  .side { padding: 12px; }
  .who { flex-direction: row; align-items: baseline; justify-content: space-between; gap: 12px; }
  .brand { min-width: 0; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .ops { flex-direction: row; gap: 6px; }
  .op { flex: 1 1 0; justify-content: center; min-width: 0; padding: 0; }
  .op .word { display: none; }
  .op .sym { width: auto; }
  .topline { gap: 6px 14px; min-height: 48px; padding-top: 8px; }
  .drawer-toggle { width: 48px; min-height: 40px; }
  .marquee { font-size: 18px; }
  .score { gap: 6px 14px; }
  .tier .ico { width: 28px; height: 28px; }
  .tier .ico + .ico { margin-left: -12px; }
}

/* ── Motion ─────────────────────────────────────────────────────────────── */

@keyframes blink { 50% { opacity: 0; } }

@keyframes pop {
  from { transform: scale(0); }
  to { transform: scale(1); }
}

@keyframes nudge {
  25% { transform: translateX(-7px); }
  75% { transform: translateX(7px); }
}

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