:root {
  --navy: #173b70;
  --navy-soft: #2f5a97;
  --gold: #ffd75e;
  --paper: #f6f7fb;
  --card: #ffffff;
  --ink: #1d2433;
  --muted: #6b7488;
  --line: #dfe3ee;
  --good: #1f8a4c;
  --good-soft: #e6f6ec;
  --warn: #b26a00;
  --warn-soft: #fff4e0;
  --bad: #b3261e;
  --bad-soft: #fdecea;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(23, 59, 112, .06), 0 8px 24px rgba(23, 59, 112, .07);
  --cw: 52px;
  --ch: 66px;
  --carry-h: 30px;
  --gutter: 40px;
  --grid-gap: 3px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.5 "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  /* Double-tap zoom is the enemy of a child tapping buttons quickly; pinch
     zoom stays available for anyone who needs it. */
  touch-action: manipulation;
}

/* ------------------------------------------------- 30-day course picker

   Every day is enterable at any time, so these are a chooser rather than a
   ladder: the only state shown is "finished" and "the one you are on". */

/* Which of the 30-day blocks is on screen, and a way to look at another one. */
.cycle-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 0 0 12px;
}
.cycle-line {
  flex: 1 1 220px;
  min-width: 160px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.45;
}
.cycle-nav .ghost:disabled { opacity: .45; cursor: default; }

.day-picker {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(46px, 1fr));
  gap: 8px;
}

.day-chip {
  padding: 9px 0;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--navy);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: transform .12s ease, border-color .12s ease;
}
.day-chip:hover { transform: translateY(-1px); border-color: var(--navy-soft); }
.day-chip:active { transform: translateY(0); }
.day-chip.done {
  background: var(--good-soft);
  border-color: #bfe3cd;
  color: var(--good);
}
.day-chip.current {
  background: var(--navy);
  border-color: var(--navy);
  color: #fff;
  box-shadow: 0 0 0 3px rgba(47, 90, 151, .18);
}
.day-chip.current.done {
  background: var(--good);
  border-color: var(--good);
  color: #fff;
}

/* ------------------------------------------------- per-question progress

   Question n+1 is locked until question n has a verdict, so the strip shows
   three states: finished, current, not open yet. Only finished and current are
   clickable -- the locked ones are disabled, which is what stops a jump ahead. */

.q-progress {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0 0 12px;
  flex-wrap: wrap;
}

.q-strip { display: flex; flex-wrap: wrap; gap: 6px; }

.q-count {
  margin-left: auto;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.q-chip {
  min-width: 32px;
  height: 32px;
  padding: 0 6px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--navy);
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.q-chip:hover { border-color: var(--navy-soft); }
.q-chip.current {
  background: var(--navy);
  border-color: var(--navy);
  color: #fff;
  box-shadow: 0 0 0 3px rgba(47, 90, 151, .18);
}
.q-chip.done {
  background: var(--good-soft);
  border-color: #bfe3cd;
  color: var(--good);
}
.q-chip.current.done {
  background: var(--good);
  border-color: var(--good);
  color: #fff;
}
.q-chip.locked {
  background: #f1f3f8;
  border-color: var(--line);
  color: var(--muted);
  opacity: .6;
  cursor: not-allowed;
}

/* Short viewports keep the strip on one line with the counter. */
@media (max-height: 700px) {
  .q-progress { margin-bottom: 8px; }
  .q-chip { height: 28px; min-width: 28px; }
}

/* ------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px clamp(12px, 3vw, 28px);
  background: var(--navy);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: 0;
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  padding: 4px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: var(--gold);
  color: var(--navy);
  font-size: 20px;
  line-height: 1;
}

.topbar-right { display: flex; align-items: center; gap: 12px; }
.today { font-size: 14px; opacity: .82; }

/* The page's own language switch, in the topbar beside the way back to the station. The same
   shape as the 速算技巧 module's, so the two modules do not look like different sites. */
.lang-switch {
  display: flex;
  gap: 2px;
  padding: 3px;
  border-radius: 11px;
  background: rgba(255, 255, 255, .14);
}
.lang-switch button {
  border: 0;
  border-radius: 9px;
  padding: 5px 10px;
  background: transparent;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  opacity: .78;
}
.lang-switch button:hover { opacity: 1; }
.lang-switch button.active {
  background: #fff;
  color: var(--navy);
  opacity: 1;
}

/* ------------------------------------------------------------ screens */

main { padding: clamp(14px, 3vw, 30px); max-width: 940px; margin: 0 auto; }

.screen { display: none; }
.screen.is-active { display: block; }

.hero { margin-bottom: 22px; }
.eyebrow {
  margin: 0 0 6px;
  font-size: 12px;
  letter-spacing: .12em;
  font-weight: 700;
  color: var(--navy-soft);
}
.hero h1 { margin: 0 0 8px; font-size: clamp(24px, 4.4vw, 34px); line-height: 1.15; }
.lede { margin: 0; color: var(--muted); max-width: 60ch; }

/* -------------------------------------------------------------- cards */

.cards { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }

.card {
  display: flex;
  align-items: center;
  gap: 14px;
  text-align: left;
  padding: 18px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
  font: inherit;
  color: inherit;
  transition: transform .12s ease, border-color .12s ease;
}
.card:hover { transform: translateY(-2px); border-color: var(--navy-soft); }
.card:active { transform: translateY(0); }

.card-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border-radius: 14px;
  background: #eef2fb;
  color: var(--navy);
  font-weight: 700;
  font-size: 17px;
}
.card-body { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.card-body strong { font-size: 17px; }
.card-body small { color: var(--muted); font-size: 13.5px; }
.card-status {
  flex: none;
  font-size: 13px;
  font-weight: 700;
  color: var(--navy);
  background: #eef2fb;
  padding: 5px 11px;
  border-radius: 999px;
}
.card-status.done { color: var(--good); background: var(--good-soft); }

/* ------------------------------------------------- the module's front door

   理解数学 and the practice, side by side and then stacked. Both are the .card above, so they
   cannot drift apart: one class means one size, one radius, one hover and one touch behaviour.

   The grid's own minimum is written as `min(100%, 300px)` rather than a bare 300px, because the
   maths page is also the inside of the station's maths frame: at a 320px window the frame is
   narrower than 300px of content, and a fixed minimum track would push the card out of the
   frame sideways rather than letting it shrink to the width it has. */

.hub-cards { grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); }
.hub-card { min-height: 92px; }
.card-arrow { flex: none; color: var(--navy); font-size: 20px; line-height: 1; }

/* The way back up from the practice to the front door. The bar it sits in is the same bar the
   other screens use; the button is given a 44px target of its own, because a control that leaves
   a whole screen is one a child should not have to aim at. */
.back-to-hub { min-height: 44px; display: inline-flex; align-items: center; }

/* Phone: the two cards keep their shape but give their text the room instead of the decoration. */
@media (max-width: 560px) {
  .hub-cards { gap: 12px; }
  .hub-cards .card { padding: 14px; gap: 10px; }
  .hub-cards .card-icon { width: 44px; height: 44px; border-radius: 12px; font-size: 15px; }
  .hub-cards .card-body strong { font-size: 16px; }
  .hub-cards .card-body small { font-size: 13px; }
  .hub-cards .card-arrow { display: none; }
}

/* ------------------------------------------------------------- panels */

.panel {
  margin-top: 18px;
  padding: 18px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.panel h2 { margin: 0 0 12px; font-size: 16px; }
.panel.danger { border-color: #f0cfcc; }
.note { margin: 10px 0 0; font-size: 13px; color: var(--muted); }

.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.field select {
  width: 100%;
  padding: 10px 12px;
  font: inherit;
  color: inherit;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.field.checkbox label {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-weight: 400;
  font-size: 14px;
  color: var(--ink);
}
.field.checkbox input { margin-top: 3px; width: 18px; height: 18px; flex: none; }

/* --------------------------------------------------------------- bars */

.bar { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.track {
  flex: 1;
  height: 8px;
  border-radius: 999px;
  background: #e6e9f2;
  overflow: hidden;
}
.track-fill { display: block; height: 100%; width: 0; background: var(--navy); transition: width .25s ease; }
.counter { font-variant-numeric: tabular-nums; font-size: 14px; color: var(--muted); }

/* ------------------------------------------------------------ buttons */

button { font: inherit; }

.primary, .ghost {
  border-radius: 11px;
  padding: 11px 18px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
}
.primary { background: var(--navy); color: #fff; }
.primary:hover { background: var(--navy-soft); }
.ghost {
  background: #fff;
  color: var(--navy);
  border-color: var(--line);
}
.ghost:hover { border-color: var(--navy-soft); }
.ghost.small { padding: 7px 12px; font-size: 13.5px; }
button:disabled { opacity: .45; cursor: not-allowed; }

.tools { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
.tools.centered { justify-content: center; }

/* ------------------------------------------------------ the grid itself */

.prompt-row { display: flex; align-items: baseline; justify-content: space-between; gap: 14px; }
.prompt { margin: 0 0 12px; font-size: 19px; font-weight: 600; }

.grid-wrap {
  position: relative;
  padding: 16px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;
  overscroll-behavior-x: contain;
}

.grid {
  display: grid;
  grid-template-columns: var(--gutter) repeat(var(--cols, 6), var(--cw));
  grid-auto-rows: min-content;
  gap: var(--grid-gap);
  justify-content: end;
  width: max-content;
  margin: 0 auto;
}

.grid .spacer { grid-column: 1; }

.sign {
  grid-column: 1;
  display: grid;
  place-items: center;
  font-size: 24px;
  font-weight: 700;
  color: var(--navy);
}

.rule {
  grid-column: 1 / -1;
  height: 3px;
  border-radius: 2px;
  background: var(--navy);
  margin: 4px 0;
}

.cell {
  height: var(--ch);
  display: grid;
  place-items: center;
  border-radius: 8px;
  font-size: calc(var(--cw) * .62);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.cell.given {
  background: #eef2fb;
  color: var(--navy);
  border: 1px solid #dbe3f4;
}

.cell.slot {
  background: #fff;
  border: 1.5px dashed #c6cee0;
  color: var(--navy);
  position: relative;
  cursor: pointer;
}
.cell.slot.is-filled { border-style: solid; border-color: #b9c4da; background: #fbfcff; }
.cell.slot.is-selected { border-color: var(--navy); box-shadow: 0 0 0 3px rgba(47, 90, 151, .18); }
.cell.slot.is-ok { border-color: var(--good); background: var(--good-soft); }
.cell.slot.is-bad { border-color: var(--bad); background: var(--bad-soft); }
.cell.slot.is-unsure { border-color: var(--warn); background: var(--warn-soft); }
.cell.slot.is-blank-check { border-color: var(--bad); background: var(--bad-soft); }

.cell.slot .ghost {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  opacity: .38;
  font-size: inherit;
  font-weight: 600;
  pointer-events: none;
}

.cell.slot .mark {
  position: absolute;
  right: 4px;
  top: 3px;
  font-size: 12px;
  line-height: 1;
}

.cell.carry {
  height: var(--carry-h);
  border: 1.5px dashed #d3d9e8;
  border-radius: 7px;
  background: #fcfcfe;
  color: var(--warn);
  font-size: calc(var(--carry-h) * .58);
  font-weight: 700;
  width: calc(var(--cw) * .62);
  justify-self: center;
  align-self: end;
  cursor: pointer;
}
.cell.carry.is-filled { border-style: solid; border-color: #e0c48f; background: #fffaf0; }
.cell.carry.is-selected { border-color: var(--navy); }
.cell.carry.is-bad { border-color: var(--bad); background: var(--bad-soft); }
.cell.carry.is-ok { border-color: var(--good); background: var(--good-soft); }

/* The gutter explains the carry notches once. Putting the word above a notch
   made it collide with the multiplication rule and the notch beside it. */
.row-label {
  grid-column: 1;
  align-self: center;
  justify-self: end;
  padding-right: 6px;
  font-size: 10px;
  letter-spacing: .04em;
  color: #8a7550;
  white-space: nowrap;
}
.cell.carry { position: relative; }

canvas.ink {
  position: absolute;
  left: 0;
  top: 0;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  cursor: crosshair;
}
/* A hidden element has to be hidden explicitly. The author `display` values
   below beat the user-agent `[hidden] { display: none }`, so without this the
   keypad stayed on screen in handwriting mode. */
canvas.ink[hidden],
.keypad[hidden] { display: none; }

.hint { min-height: 20px; margin: 10px 0 0; font-size: 13.5px; color: var(--muted); }

/* ------------------------------------------------------------- keypad */

.keypad {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}
.keypad button {
  padding: 14px 0;
  font-size: 20px;
  font-weight: 600;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--navy);
  cursor: pointer;
}
.keypad button:hover { border-color: var(--navy-soft); background: #f7f9ff; }

/* ---------------------------------------------------------- feedback */

.verdict { margin-top: 16px; }
.verdict:empty { display: none; }

.banner {
  padding: 14px 16px;
  border-radius: 13px;
  border: 1px solid var(--line);
  background: var(--card);
}
.banner h3 { margin: 0 0 6px; font-size: 16px; }
.banner p { margin: 0; font-size: 14.5px; }
.banner ul { margin: 8px 0 0; padding-left: 20px; font-size: 14.5px; }
.banner li { margin-bottom: 4px; }
.banner .zh {
  margin-top: 8px;
  font-size: 13.5px;
  color: var(--muted);
  border-left: 3px solid var(--line);
  padding-left: 10px;
}
.banner.good { background: var(--good-soft); border-color: #bfe3cd; }
.banner.bad { background: var(--bad-soft); border-color: #f0cfcc; }
.banner.warn { background: var(--warn-soft); border-color: #ecd7ac; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.chip {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--navy);
  font-weight: 600;
  cursor: pointer;
  font-size: 14px;
}
.chip:hover { border-color: var(--navy-soft); }
.chip.strong { background: var(--navy); color: #fff; border-color: var(--navy); }

/* ------------------------------------------------------- word problems */

.story {
  padding: 20px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 18px;
  line-height: 1.65;
}
.story b { color: var(--navy); }

.answer-row {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 18px;
}
.answer-row > label { font-weight: 600; }
.answer-input {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #fff;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  padding: 6px 12px;
}
.answer-input:focus-within { border-color: var(--navy); box-shadow: 0 0 0 3px rgba(47, 90, 151, .15); }
.answer-input input {
  border: 0;
  outline: 0;
  font: inherit;
  font-size: 20px;
  width: 8ch;
  color: var(--navy);
  background: transparent;
}
.answer-input .unit { color: var(--muted); font-weight: 600; }

.clock-input { display: flex; align-items: center; gap: 6px; }
.clock-input input {
  width: 3.2ch;
  text-align: center;
  font-size: 20px;
  border: 0;
  outline: 0;
  color: var(--navy);
  background: transparent;
}
.clock-input span { font-weight: 700; color: var(--muted); }

/* -------------------------------------------------------------- scores */

.scoreboard {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.score {
  padding: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-align: center;
}
.score b { display: block; font-size: 30px; color: var(--navy); line-height: 1.2; }
.score small { color: var(--muted); font-size: 13px; }

.breakdown { display: grid; gap: 8px; }
.row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 14.5px;
}
.row .tag {
  flex: none;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  background: #eef2fb;
  color: var(--navy);
}
.row .tag.ok { background: var(--good-soft); color: var(--good); }
.row .tag.bad { background: var(--bad-soft); color: var(--bad); }
.row .tag.warn { background: var(--warn-soft); color: var(--warn); }
.row .grow { flex: 1; }

/* ------------------------------------------------------------- history */

.history { display: grid; gap: 6px; }
.day-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
  padding: 9px 12px;
  border-radius: 10px;
  background: #fbfcff;
  border: 1px solid var(--line);
  font-size: 14px;
}
.day-row .n { font-variant-numeric: tabular-nums; color: var(--muted); }

.tally { display: grid; gap: 8px; }
.tally-row { display: grid; grid-template-columns: 1fr 120px auto; gap: 10px; align-items: center; font-size: 14px; }
.tally-bar { height: 8px; border-radius: 999px; background: #e6e9f2; overflow: hidden; }
.tally-bar span { display: block; height: 100%; background: var(--navy); }
.tally-row .n { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 4ch; text-align: right; }
.tally-empty { color: var(--muted); font-size: 14px; }

/* ------------------------------------------------------------ doing a set

   While a set is open the page is the problem and nothing else. The topbar is
   hidden and the screen is given the whole viewport, so a child never has to
   scroll to reach Check -- on an iPad held in landscape the old layout put the
   buttons below the fold, and scrolling mid-sum is how a digit gets lost. */

body.doing .topbar { display: none; }
/* The viewport height has to be claimed here, on the padded box, with the
   screen taking 100% of what is left. Putting 100dvh on the screen instead
   overflows by exactly the padding -- which is enough to push Check a few
   pixels below the fold and bring back the scrolling this is meant to remove.
   dvh, not vh: iOS Safari's vh includes the collapsing toolbar, so a "full
   height" layout comes out a few dozen pixels too tall. */
body.doing main {
  padding: clamp(8px, 1.6vw, 16px);
  max-width: none;
  height: 100dvh;
  overflow: hidden;
}

body.doing .screen.is-active {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

/* Only the piece that has to grow is allowed to scroll; the buttons stay put.
   The answer box and the day's feedback take what is left over between them, and
   the control deck below them is never pushed off the screen. */
body.doing #screen-practice .verdict {
  flex: 0 1 auto;
  min-height: 0;
  max-height: 30dvh;
  overflow-y: auto;
}
body.doing #screen-practice .practice-deck { flex: none; }
body.doing .bar,
body.doing .prompt-row,
body.doing .tools,
body.doing .answer-row,
body.doing .hint { flex: none; }

/* Short viewports have no room for the hint line's reserved space. */
@media (max-height: 700px) {
  body.doing .hint { min-height: 0; }
}

/* ------------------------------------------------- undo, beside the sum

   Undo used to float halfway down the grid in the old inline panel. It now sits
   in the working overlay's own bar, with Clear, Check my working and Close, so
   every control for the working is in one place instead of round the edge of a
   full-screen panel. */

.grid-wrap { position: relative; }   /* already relative; restated for clarity */

/* ------------------------------------------------------------- the scratchpad

   A word problem is worked out on paper before the answer is typed, so the page
   has to provide the paper. Nothing here is recognised -- it is a sketch pad. It
   now lives in an overlay of its own, reached by a button, rather than being a
   strip that had to be dragged open at the foot of the question. */

.draft-canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 120px;
  border-radius: 10px;
  overflow: hidden;
  background: #fffdf6;
  /* Ruled lines, so sums stay in columns on a blank pad. */
  background-image: repeating-linear-gradient(
    to bottom, transparent, transparent 31px, #e8ebf3 31px, #e8ebf3 32px);
}
canvas.draft-ink {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  cursor: crosshair;
}
.draft-canvas-wrap.erasing canvas.draft-ink { cursor: cell; }

@media (max-width: 560px) {
  .story { font-size: 16.5px; }
  .grid-wrap { padding: 12px 8px; }
  .topbar-right .today { display: none; }
  /* The topbar now carries the language switch and the way back to the station as well, so on a
     phone the brand keeps its mark and drops its words rather than pushing the buttons off the
     edge. Everything stays reachable; nothing moves to another screen. */
  /* The row wraps as well as shrinks.
     Dropping the brand's words above is not enough on its own: the maths page runs in a frame of
     its own inside the station (364px wide on a 390px phone, 294px on a 320px one), and the four
     controls on the right -- the language switch, the settings button, Progress and the way back to
     the station -- need 381px together at this size. A non-wrapping row cannot shrink below that,
     so it overflowed the frame by 31px at 390px and by 101px at 320px, with the way back to the
     station pushed off the right-hand edge. Letting the right-hand group wrap lays the same
     controls out in two lines at their natural size instead: nothing is removed, nothing is made
     smaller than it already was, and the brand keeps its place at the left of the first line.
     Only the group wraps, not the bar: a wrapping bar would push the whole group onto a line of its
     own and make the bar a line taller than it needs to be. */
  .topbar { gap: 8px; padding: 8px 10px; }
  .topbar-right {
    gap: 6px;
    flex-wrap: wrap;
    /* The last line is the way back to the station, and it belongs under the right-hand edge with
       the rest of the row rather than under the middle of it. */
    justify-content: flex-end;
  }
  .brand-text { display: none; }
  .lang-switch button { padding: 5px 8px; font-size: 12.5px; }
}

/* ---------------------------------------------- one correct sum, one point

   The celebration badge for a correct multiplication. It floats over the practice area,
   ignores pointer events so it can never swallow a tap meant for the next sum, and the
   element is hidden again once the animation has played. */

.celebrate {
  position: fixed;
  left: 50%;
  top: 38%;
  /* Above the overlays: a sum laid out in the working and checked correctly is worth
     celebrating, and the badge must not end up behind the panel the child is looking at. */
  z-index: 70;
  display: grid;
  justify-items: center;
  gap: .1rem;
  padding: 16px 26px;
  border: 2px solid var(--gold);
  border-radius: 22px;
  background: linear-gradient(160deg, #fffdf3, #fff3c4);
  box-shadow: 0 14px 40px rgba(23, 59, 112, .22);
  color: var(--navy);
  font-weight: 800;
  text-align: center;
  pointer-events: none;
}

.celebrate[hidden] { display: none; }

.celebrate .celebrate-burst { font-size: 2.4rem; line-height: 1; }

.celebrate strong { font-size: 1.35rem; }

.celebrate .celebrate-points {
  margin-top: 4px;
  padding: 4px 12px;
  border-radius: 999px;
  background: #fff;
  color: var(--good);
  font-size: 1.05rem;
}

.celebrate.is-on { animation: celebrate-pop 1.6s cubic-bezier(.2, 1.3, .4, 1) both; }
.celebrate.is-on .celebrate-burst { animation: celebrate-burst 1.6s ease-out both; }

@keyframes celebrate-pop {
  0%   { opacity: 0; transform: translate(-50%, -28%) scale(.62); }
  16%  { opacity: 1; transform: translate(-50%, -50%) scale(1.09); }
  28%  {            transform: translate(-50%, -50%) scale(1); }
  72%  { opacity: 1; transform: translate(-50%, -58%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -88%) scale(.96); }
}

@keyframes celebrate-burst {
  0%   { transform: rotate(-20deg) scale(.4); }
  30%  { transform: rotate(8deg)   scale(1.3); }
  60%  { transform: rotate(-5deg)  scale(1); }
  100% { transform: rotate(0)      scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .celebrate.is-on { animation: none; opacity: 1; }
  .celebrate.is-on .celebrate-burst { animation: none; }
}

/* ============================================================ the new maths

   Styles for the personalised practice screens: the three-line level strip, today's
   plan, the collapsible vertical working, the teacher panel and the small countdown.
   They reuse the tokens above -- same navy, same radii, same shadows -- so the new
   screens look like the ones they replaced rather than like a second application. */

.icon-button { padding: 6px 10px; font-size: 15px; line-height: 1; }

/* -------------------------------------------------- the three separate facts */

.level-strip {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  margin-bottom: 18px;
}

.level-cell {
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

.level-title {
  font-size: 12px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
}

.level-value { font-size: 17px; color: var(--navy); }
.level-note { font-size: 12.5px; color: var(--muted); }

/* ------------------------------------------------------------------ the plan */

.plan-head { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; }
.plan-progress { display: flex; align-items: center; gap: 12px; flex: 1 1 260px; }
.plan-progress .track { flex: 1; }
.plan-notes { margin-top: 12px; display: grid; gap: 4px; }
.plan-notes .note { margin: 0; }
.toast-line { color: var(--navy); font-weight: 600; }
.chip-label {
  align-self: center;
  font-size: 12px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  margin-right: 4px;
}

.plain-list { margin: 12px 0 0; padding-left: 20px; color: var(--ink); font-size: 15px; }
.plain-list li { margin-bottom: 6px; }

/* ------------------------------------------------- the practice screen chrome */

.mode-line {
  flex: none;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 10px;
}

.mode-line.assessment {
  color: var(--navy);
  font-weight: 600;
  background: #eef3fc;
  border: 1px solid #d6e2f6;
  border-radius: 999px;
  padding: 6px 12px;
  display: inline-block;
}

.countdown {
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  font-weight: 600;
  color: var(--navy);
  background: #eef3fc;
  border: 1px solid #d6e2f6;
  border-radius: 999px;
  padding: 4px 10px;
}

/* A reminder, not an alarm: the countdown never flashes and never turns red. */
.countdown.is-warn { background: var(--warn-soft); border-color: #ecd7ac; color: var(--warn); }
.countdown.is-final { background: var(--navy); border-color: var(--navy); color: #fff; }

.topic-tag {
  flex: none;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  background: #fff;
}

/* ---------------------------------------------------- the practice controls

   One deck, at the foot of the question, in two rows: the answering row (Previous,
   Check Answer, Next) and the tools row (the working, the next step, Check my
   working, the scratchpad, Hint, the teacher). Everything a child needs is in the
   same place on every question and every screen size, and the two rows never move:
   a button that jumps about is a button that has to be hunted for every time.

   The rows are deliberately one shape -- same height, same radius, same gap -- and
   the only filled button in either of them is Check Answer. */

.practice-deck {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.deck-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: stretch;
  justify-content: center;
}

.ctrl {
  flex: 0 1 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 46px;              /* every button in the deck is one height */
  padding: 10px 16px;
  border-radius: 12px;           /* and one radius */
  border: 1px solid transparent;
  background: #fff;
  color: var(--navy);
  font-size: 15px;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  cursor: pointer;
}
.ctrl:hover { border-color: var(--navy-soft); }
.ctrl:disabled { opacity: .45; cursor: not-allowed; }
/* A button that is not offered right now is gone, not moved: nothing in the deck
   shifts sideways when the tools for a question change. */
.ctrl[hidden] { display: none; }

/* First in the hierarchy: the one button that marks the answer. */
.deck-answers .ctrl-primary {
  min-width: 196px;
  background: var(--navy);
  border-color: var(--navy);
  color: #fff;
  font-size: 16px;
  box-shadow: 0 6px 16px rgba(23, 59, 112, .18);
}
.deck-answers .ctrl-primary:hover { background: var(--navy-soft); border-color: var(--navy-soft); }
/* Second: where to go next. Present, obvious, and plainly not the main action. */
.deck-answers .ctrl-nav { min-width: 130px; border-color: var(--line); }
/* Third: the learning tools. Smaller type, no fill, never competing with Check. */
.deck-tools .ctrl { font-size: 14.5px; font-weight: 500; border-color: var(--line); }

/* ------------------------------------------------------------------ the help pair

   Two of the deck's tools are the ones a stuck child reaches for, and the round's report was that
   they looked -- and behaved -- like the same button. So the hierarchy is now visible, and it is
   visible in the two places a child looks:

     * Check Answer is still the ONLY filled navy button in the deck. Nothing here touches it.
     * Hint is a quiet tool, exactly like the four beside it, with one accent edge on its leading
       side so it reads as "a nudge is available". It asks nothing, so it opens nothing.
     * AI Teacher is the only SOFT-FILLED button in the deck: a pale navy tint and a navy border,
       heavier than every quiet tool and still plainly lighter than the filled primary. That is the
       weight a button carries when pressing it starts a conversation rather than printing a line.

   The point is not decoration. A child who can see that the two are different weights will try the
   cheap one first, which is what a hint is for. */
.deck-tools .ctrl-hint {
  border-left: 3px solid var(--navy-soft);
  padding-left: 12px;
}
.deck-tools .ctrl-teacher {
  background: #eef4ff;
  border-color: var(--navy-soft);
  color: var(--navy);
  font-weight: 600;
}
.deck-tools .ctrl-teacher:hover { background: #e3edff; border-color: var(--navy); }
.deck-tools .ctrl:hover { background: #f7f9ff; }
.deck-tools .ctrl.on { background: var(--navy); border-color: var(--navy); color: #fff; }

/* --------------------------------------------------------------- the overlay

   The working and the scratchpad are overlays laid over the question itself. Neither is
   a page, a route or a tab -- the question, its answer, its attempts and its clock are
   the same objects underneath -- so closing one puts the child back exactly where they
   were.

   The two are not the same size, and that is the point.

   The SCRATCHPAD is about four fifths of the screen, centred: it is paper, so a sheet
   with a margin is the right shape for it, and the question showing round the edges is
   no loss.

   The WORKING fills the viewport exactly. A sum set out in columns is five or six rows
   of squares and up to six columns, and on a 320px phone a four-fifths overlay left the
   working about 250px wide -- smaller than the squares need to be written in, with the
   sum scrolled up and down inside it. Full screen is the difference between writing the
   working and peering at it.

   While an overlay is up the page behind cannot scroll and cannot be clicked: the
   backdrop covers it, and the body is pinned. Both panels start hidden and are only
   ever shown by app.js, so `[hidden]` is spelled out here -- an author `display`
   beats the browser's own `[hidden]` rule, and leaving that out is how the old
   working panel ended up on screen whether or not it was open. */

.overlay-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(16, 30, 55, .45);
}
.overlay-backdrop[hidden] { display: none; }

.overlay {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 61;
  display: flex;
  flex-direction: column;
  gap: 0;
  width: 80vw;
  height: 82dvh;
  max-width: 1440px;
  max-height: 1000px;
  padding: clamp(10px, 1.4vw, 18px);
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--paper);
  box-shadow: 0 30px 80px rgba(12, 26, 50, .38);
  overflow: hidden;
}
.overlay[hidden] { display: none; }

/* ------------------------------------------------------------ the working's own space

   THE WORKING IS A WORKSPACE, AND ITS SHAPE NOW FOLLOWS THE SCREEN IT IS ON.

   It used to be full-bleed at every width, which is right for a phone and wrong for everything
   else. Measured at 1100x900 that produced a 1100px-wide slab holding a 502px-wide sum: half the
   screen empty, and -- because the keys sat UNDER the grid rather than beside it -- only 551px of
   height for a grid 813px tall, so the child scrolled a sum that would have fitted if the panel had
   been shaped like the screen it was on.

   So there are two shapes, and one breakpoint between them:

     * a PHONE gets the whole viewport, edge to edge, with the safe-area insets as padding. That is
       what the brief asks for and what the round measured at 320 and 390.
     * an iPad or a desktop gets a large CENTRED workspace instead: 92% of the width and 90% of the
       height, capped at 1180px so a very wide monitor does not stretch it into a banner. It is a
       workspace rather than a card -- it keeps all but a sliver of the screen -- and the sliver is
       what tells a child the page is still there behind it.

   `100dvh`/`dvh` rather than `vh`, because on a phone `vh` counts the browser chrome that is not
   on screen and the panel would be taller than the viewport.

   The extra `.overlay` in the selector is doing real work: the responsive rules further down this
   file set the overlay's width, height, radius and padding for tablets and phones, and they are
   written for `.overlay`, which is one class less specific. Without the second class those rules
   would win on a phone and put the working back inside a floating card -- the bug this round is
   fixing. */
.overlay.working-overlay {
  width: min(1180px, 92vw);
  height: 90dvh;
  max-width: none;
  max-height: none;
  border-radius: 16px;
  padding: clamp(10px, 1.2vw, 18px);
}

/* THE KEYS GO BESIDE THE SUM, NOT UNDER IT.

   This is the change that buys the working its height. A keypad under the grid costs about 120px of
   the one dimension a sum set out in columns actually needs, and at 1100x900 that 120px was the
   difference between a sum that fitted and a sum that scrolled. From 900px up there is room to put
   the keys in a column of their own, so the grid gets the full height of the middle band and the
   keys get a column that costs the grid nothing.

   Below 900px the keys stay underneath, because a phone or a narrow tablet has no width to spare and
   a two-column keypad on a 390px screen would be a column of 40px targets. */
@media (min-width: 900px) {
  .overlay.working-overlay .overlay-body {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    column-gap: 16px;
  }
  /* The step panel, the note and the hint line run the full width; only the grid and the keys are
     side by side. */
  .overlay.working-overlay .step-panel,
  .overlay.working-overlay .working-note,
  .overlay.working-overlay .hint { grid-column: 1 / -1; }
  .overlay.working-overlay .grid-wrap { grid-column: 1; grid-row: 3; }
  .overlay.working-overlay .keypad {
    grid-column: 2;
    grid-row: 3;
    align-self: center;
    grid-template-columns: repeat(3, minmax(56px, 1fr));
    margin-top: 0;
    width: auto;
  }
  .overlay.working-overlay .keypad button { min-height: 56px; min-width: 56px; }
}

/* The working itself gets everything the two bars do not. `min-height: 0` is what lets a
   flex child shrink below its content: without it the wrap holds the 200px floor the
   base rule sets, the panel overflows, and on a short phone the bottom controls end up
   off the screen. */
.overlay.working-overlay .overlay-body { padding-top: 8px; }
.overlay.working-overlay .grid-wrap {
  min-height: 0;
  padding: 8px 4px;
}

/* The bottom bar: Show the step at the leading edge, Check my working where the writing
   finishes. It is a sibling of the working rather than a layer over it, so it can never
   cover a square a child is writing in. */
.overlay-foot {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}
.overlay-foot .ctrl { min-height: 46px; }
.overlay-foot .ctrl-tool { margin-right: auto; }
.overlay-foot .ctrl-primary {
  min-width: 190px;
  font-size: 16px;
  background: var(--navy);
  border-color: var(--navy);
  color: #fff;
}
.overlay-foot .ctrl-primary:hover { background: var(--navy-soft); border-color: var(--navy-soft); }

body.has-overlay { overflow: hidden; }

.overlay-head {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}
.overlay-heading {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}
.overlay-title { font-size: 17px; color: var(--navy); }
.overlay-question { font-size: 15.5px; font-weight: 600; overflow-wrap: anywhere; }
.overlay-count {
  font-size: 11.5px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  background: #fff;
}
/* Close, Undo, Clear and Check my working, together at the top of the overlay. */
.overlay-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.overlay-actions .ctrl-close { border-color: var(--navy); font-weight: 700; }
.overlay-actions .ctrl-primary {
  background: var(--navy);
  border-color: var(--navy);
  color: #fff;
}
.overlay-actions .ctrl-primary:hover { background: var(--navy-soft); border-color: var(--navy-soft); }

.overlay-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 12px;
  overflow: hidden;
}

/* The one step the child is on. It sits at the top of the working, above the grid,
   where the eye lands on opening the overlay. */
.step-panel {
  flex: none;
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 10px 14px;
  border: 1px solid #d6e2f6;
  border-radius: 13px;
  background: #eef3fc;
}
.step-panel[hidden] { display: none; }
.step-tag {
  flex: none;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--navy);
}
.step-text { margin: 0; font-size: 15.5px; line-height: 1.45; }
@media (max-width: 560px) {
  .step-panel { flex-direction: column; gap: 4px; }
}

.working-note, .scratch-note { flex: none; margin: 0; }

/* The working's own area: everything the grid does not need goes back to it. */
.overlay .grid-wrap {
  flex: 1 1 auto;
  min-height: 200px;
  padding: 14px 10px;
  overflow: auto;
  overscroll-behavior: contain;
}
.overlay .grid-wrap[hidden] { display: none; }
.overlay .keypad { flex: none; margin-top: 0; }
.overlay .hint { flex: none; }

/* The scratchpad's own paper: as much of the overlay as is left. */
.scratch-body .draft-canvas-wrap { flex: 1 1 auto; min-height: 0; }

/* --------------------------------------------------------- the teacher panel */

.tutor-panel {
  flex: none;
  margin-top: 14px;
  border: 1px solid #d6e2f6;
  background: #f4f8ff;
  border-radius: var(--radius);
  padding: 12px 14px;
  max-height: 30dvh;
  overflow-y: auto;
}

.tutor-head { display: flex; flex-wrap: wrap; gap: 10px; align-items: baseline; justify-content: space-between; }
.tutor-body p { margin: 8px 0 0; font-size: 15px; line-height: 1.5; }
.tutor-body .note { margin-top: 8px; }

/* ------------------------------------------------ the teacher's side of the conversation

   Only ever shown by the AI Teacher, and that is the point: a hint asks nothing, so Hint never puts
   a box on the screen. The turns alternate, and which side said what is carried by an edge rather
   than by a colour alone -- a child who cannot tell two pale tints apart can still tell which way
   the border is on. */
.tutor-reply { margin-top: 10px; display: block; }
.tutor-reply[hidden] { display: none; }
/* A label for the reply box that a screen reader reads and the eye does not need: the box is the
   only text field in the panel and it is labelled by the line above it. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}
.tutor-asks { margin: 0 0 6px; font-size: 13.5px; color: var(--muted); }
.tutor-reply-row { display: flex; gap: 8px; align-items: stretch; }
.tutor-reply-row input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: var(--navy);
  font: inherit;
  font-size: 15px;
}
.tutor-reply-row input:focus { outline: 2px solid var(--navy-soft); outline-offset: 1px; }
.tutor-reply-row .ctrl { flex: 0 0 auto; }

/* A turn of the conversation. The teacher's own speech is what the panel already drew; a child's
   own words are added with the class below, so the two are told apart without a transcript line. */
.tutor-turn { margin: 8px 0 0; font-size: 15px; line-height: 1.5; }
.tutor-turn.is-teacher { padding-left: 10px; border-left: 3px solid var(--navy-soft); }
.tutor-turn.is-child {
  padding-left: 10px;
  border-left: 3px solid var(--line);
  color: var(--muted);
  font-style: italic;
}
.tutor-turn .tutor-who {
  display: block;
  font-size: 12.5px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  font-style: normal;
}

.announce { font-weight: 600; color: var(--navy); }

.notes-list { margin-top: 12px; display: grid; gap: 6px; }
.notes-list .note { margin: 0; }

/* The result screen's by-skill rows line up like the progress tallies. */
#screen-result .row, #screen-done .row { gap: 10px; }

/* --------------------------------------------------------------- responsive

   Three sizes are checked by hand and by the browser harness: a Mac at desktop
   width, an iPad, and a phone at about 390px. The rule in all three is that the
   buttons stay in their two rows, nothing overflows sideways, and no label is ever
   cut off -- a label that does not fit makes its button wider and pushes the row to
   wrap, rather than being clipped. */

/* iPad: two tight rows of tools rather than one crowded one. */
@media (max-width: 1024px) {
  .deck-row { gap: 8px; }
  .deck-tools .ctrl { flex: 0 1 auto; }
  .overlay .grid-wrap { min-height: 180px; }
}

@media (max-width: 780px) {
  .ctrl { min-height: 44px; padding: 9px 13px; font-size: 14.5px; }
  .deck-answers .ctrl-primary { min-width: 176px; font-size: 15.5px; }
  .deck-answers .ctrl-nav { min-width: 118px; }
  .overlay { width: 88vw; height: 84dvh; border-radius: 18px; }
}

/* Phone: the deck becomes a neat two-column block, Check Answer first across the
   full width, and the scratchpad nearly fills the screen -- still an overlay, though,
   never a page of its own. The working is full screen at every width, so it is not
   touched here beyond its bottom bar. */
@media (max-width: 620px) {
  .level-strip { grid-template-columns: 1fr; }
  .plan-head { flex-direction: column; align-items: stretch; }
  .tools { gap: 8px; }
  .tools .primary, .tools .ghost { flex: 1 1 auto; }
  .deck-answers { flex-wrap: wrap; }
  .deck-answers .ctrl-primary { order: -1; flex: 1 1 100%; min-width: 0; }
  .deck-answers .ctrl-nav { flex: 1 1 40%; min-width: 0; }
  .deck-tools .ctrl { flex: 1 1 46%; }
  .overlay {
    width: calc(100vw - 10px);
    height: calc(100dvh - 10px);
    max-width: none;
    max-height: none;
    padding: 10px;
    border-radius: 14px;
  }
  .overlay-head { padding-bottom: 10px; }
  .overlay-heading { gap: 6px; }
  .overlay-actions { width: 100%; justify-content: space-between; }
  .overlay-actions .ctrl { flex: 1 1 auto; }
  .overlay .grid-wrap { min-height: 160px; padding: 10px 6px; }

  /* THE PHONE GETS THE WHOLE VIEWPORT, and every property the base rules set for a centred panel
     is turned off here rather than merely overridden. That is not belt-and-braces: the base
     `.overlay` centres itself with `left: 50%; top: 50%; transform: translate(-50%, -50%)`, and a
     rule that only reset `inset` would leave the transform in place and slide the working half its
     own size up and off the screen on every phone.

     `inset: 0` with `width: auto` rather than `100vw`, because 100vw counts the scrollbar gutter on
     a desktop browser and the dynamic browser chrome on a phone, and either one puts a few pixels of
     the page back on screen or adds a horizontal scrollbar. A fixed element pinned to all four edges
     is exactly the viewport, whatever the viewport is doing.

     The safe-area insets are padding rather than margin: the working is edge-to-edge, so its content
     must not sit under a notch or a home indicator. `env()` resolves to 0 on a device with no inset,
     which is why the same rule is correct everywhere. */
  .overlay.working-overlay {
    inset: 0;
    left: 0;
    top: 0;
    transform: none;
    width: auto;
    height: auto;
    max-width: none;
    max-height: none;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    padding: calc(6px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right))
             calc(6px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
  }
  .overlay.working-overlay .grid-wrap { min-height: 0; padding: 6px 2px; }
  .overlay.working-overlay .overlay-head { padding-bottom: 8px; }
  .overlay.working-overlay .overlay-actions { width: auto; justify-content: flex-end; }
  .overlay.working-overlay .overlay-actions .ctrl { flex: 0 1 auto; }
  .overlay.working-overlay .overlay-heading { flex: 1 1 auto; }
  .overlay.working-overlay .overlay-count { display: none; }
  .overlay.working-overlay .working-note { display: none; }
  .overlay-foot { padding-top: 8px; gap: 6px; }
  .overlay-foot .ctrl { flex: 0 1 auto; padding: 8px 10px; font-size: 14px; }
  .overlay-foot .ctrl-primary { min-width: 0; }
}

/* A phone with room to spare: the bottom bar stacks, so each of the two things a child does
   after writing is a full-width target under a thumb. This is about height rather than width,
   because it is height the stacked bar costs -- at 390x844 there is plenty, at 320x568 there
   is not, and the working is what the child came for. */
@media (max-width: 620px) and (min-height: 700px) {
  .overlay-foot { flex-direction: column; align-items: stretch; }
  .overlay-foot .ctrl { flex: 1 1 auto; }
  .overlay-foot .ctrl-tool { margin-right: 0; }
  .overlay-foot .ctrl-primary { min-width: 0; width: 100%; }
}

/* A tablet in landscape: wide enough for the keys to sit beside the sum, and it is the height
   that matters here -- a landscape iPad has less of it than a portrait one, so the keys cost more
   there than anywhere else. Same treatment as the desktop, at the same breakpoint. */

/* A narrow phone: the word "Working" in the top bar costs a line that the question then has
   to share, and the question is the thing worth the line. */
@media (max-width: 420px) {
  .overlay.working-overlay .overlay-title { display: none; }
}

@media (max-height: 760px) {
  .tutor-panel { max-height: 24dvh; }
  .overlay .grid-wrap { min-height: 150px; }
  /* A short viewport is exactly when the working needs its room back: the line that says
     what the grid is is read once, and the squares are looked at for minutes. */
  .overlay.working-overlay .grid-wrap { min-height: 0; }
  .overlay.working-overlay .working-note { display: none; }
}

/* A phone held upright but not tall -- a small phone, or a browser with its toolbars out.
   Every pixel the two bars do not need goes to the working, because the sum is the only
   thing on this screen that cannot be made smaller without losing the point of it. */
@media (max-height: 700px) {
  .overlay.working-overlay .overlay-head { padding-bottom: 6px; }
  .overlay.working-overlay .overlay-body { padding-top: 6px; }
  .overlay.working-overlay .overlay-foot { padding-top: 6px; }
  .overlay.working-overlay .working-note { display: none; }
  .overlay.working-overlay .hint:empty { display: none; }
  /* The keypad is the input aid for the tap setting, and two rows of it are 104px. Tightened
     to the 44px floor the rest of this product uses, it is 96px and still a key a thumb hits. */
  .overlay.working-overlay .keypad { gap: 6px; }
  .overlay.working-overlay .keypad button { min-height: 44px; padding: 6px 0; font-size: 17px; }
}

/* A very short viewport -- a phone turned on its side. The question and the deck cannot both fit,
   and cutting the buttons off the bottom of a screen that cannot scroll is the one outcome worth
   going out of the way to avoid: here the question scrolls instead, and the deck stays reachable. */
@media (max-height: 560px) {
  body.doing main { overflow-y: auto; }
  body.doing .screen.is-active { height: auto; min-height: 100%; }
  .ctrl { min-height: 42px; padding: 8px 12px; font-size: 14px; }
  .overlay .grid-wrap { min-height: 130px; }
  /* The two bars, side by side, on a landscape phone: the working keeps the middle. There
     is not enough height for a stacked bottom bar here, and 42px is still a touch target a
     thumb can hit -- the same floor the rest of the deck uses at this height. */
  .overlay.working-overlay .grid-wrap { min-height: 0; }
  .overlay.working-overlay .overlay-foot {
    flex-direction: row;
    justify-content: flex-end;
    padding-top: 6px;
  }
  .overlay-foot .ctrl-tool { margin-right: auto; }
  .overlay-foot .ctrl-primary { width: auto; min-width: 160px; }
}

