/* ================================================================
   Spelling Buddy — design tokens
   A page from a school exercise book: cool copybook paper,
   fountain-pen ink, ruled lines, a red margin, pencil yellow.
   ================================================================ */
:root {
  --paper: #f4f8fb;      /* exercise-book white   */
  --ink: #17335b;        /* fountain-pen navy     */
  --rule: #c9ddec;       /* copybook ruling blue  */
  --red: #e4574c;        /* margin-line red       */
  --sun: #ffc531;        /* pencil yellow         */
  --sun-deep: #f0ab00;
  --grass: #2e9e63;      /* marking-tick green    */
  --card: #ffffff;

  --font-display: "Fredoka", "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  --font-body: "Nunito", "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;

  --radius: 18px;
  --shadow: 0 3px 0 rgba(23, 51, 91, 0.18);
}

* { box-sizing: border-box; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
label:has(+ .sr-only:focus-visible) { outline: 3px solid var(--red); outline-offset: 3px; }

/* The `hidden` attribute must always win. `hidden` gets its display:none from
   the UA sheet, so any author `display` on .parent-gate, #btn-finish or
   #btn-continue would silently beat it — and a permanently visible parent gate
   means a permanently visible "Show the answers" button. Enforced here rather
   than remembered at each rule. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 17px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

/* ---------------- screens ---------------- */
.screen {
  display: none;
  max-width: 460px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: 28px 22px calc(28px + env(safe-area-inset-bottom));
  flex-direction: column;
  gap: 18px;
}
.screen.active { display: flex; }

/* copybook ruling on the test screen */
.screen.ruled {
  background:
    repeating-linear-gradient(
      to bottom,
      transparent 0,
      transparent 43px,
      var(--rule) 43px,
      var(--rule) 44px
    );
}

/* ---------------- capture screen ---------------- */
.brand { text-align: center; margin-top: 4vh; }

.brand-badge {
  width: 84px;
  height: 84px;
  margin: 0 auto 14px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--card);
  position: relative;
  display: grid;
  place-items: center;
}
.brand-badge span {
  font-family: var(--font-display);
  font-size: 44px;
  font-weight: 600;
}
/* dashed 田字格 guides */
.brand-badge::before, .brand-badge::after,
.tian::before, .tian::after {
  content: "";
  position: absolute;
  border-color: var(--rule);
  border-style: dashed;
  border-width: 0;
}
.brand-badge::before, .tian::before {
  left: 8%; right: 8%; top: 50%;
  border-top-width: 2px;
}
.brand-badge::after, .tian::after {
  top: 8%; bottom: 8%; left: 50%;
  border-left-width: 2px;
}
.brand-badge span, .tian span { position: relative; z-index: 1; }

h1 {
  font-family: var(--font-display);
  font-size: 30px;
  font-weight: 600;
  margin: 0 0 6px;
}
h1 .zh { display: block; font-size: 20px; font-weight: 500; color: var(--red); }
h2 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 21px;
  margin: 0;
}
.tagline { margin: 0 0 3vh; font-size: 16px; }
.subhead { margin: -8px 0 0; font-size: 15px; }

.capture-alts { display: flex; flex-direction: column; gap: 8px; }

.hint {
  margin-top: auto;
  text-align: center;
  font-size: 13.5px;
  color: #5a7395;
}

/* ---------------- buttons ---------------- */
.btn {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 17px;
  color: var(--ink);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  padding: 13px 20px;
  background: var(--card);
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: center;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
  touch-action: manipulation;
  user-select: none;
}
.btn:active { transform: translateY(3px); box-shadow: 0 0 0 rgba(0,0,0,0); }
.btn:focus-visible { outline: 3px solid var(--red); outline-offset: 3px; }
.btn[disabled] { opacity: 0.4; cursor: default; }

.btn-primary { background: var(--sun); }
.btn-secondary { background: var(--card); }
.btn-outline { background: transparent; box-shadow: none; border-style: dashed; }
.btn-ghost {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  font-weight: 600;
  padding: 8px 12px;
}
.btn-big { font-size: 20px; padding: 18px 22px; display: block; width: 100%; }
.btn-small { font-size: 14px; padding: 6px 12px; }

/* ---------------- status ---------------- */
.status { min-height: 1.4em; text-align: center; font-weight: 700; margin: 0; }
.status.busy::after { content: "…"; }
.status.error { color: var(--red); }

/* ---------------- list cards (exercise-book page) — review + answers ---------------- */
.review-list, .answers-list {
  list-style: none;
  margin: 0;
  padding: 6px 0 6px 18px;
  background: var(--card);
  border: 2px solid var(--rule);
  border-radius: 12px;
  position: relative;
}
/* red margin line, like a real exercise book */
.review-list::before, .answers-list::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0; left: 10px;
  border-left: 2px solid var(--red);
  opacity: 0.65;
}
.review-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px 8px 6px;
  border-bottom: 1px solid var(--rule);
}
.review-item:last-child { border-bottom: none; }
.review-item .num {
  font-family: var(--font-display);
  font-size: 14px;
  min-width: 22px;
  text-align: right;
  color: #5a7395;
}
.review-item input[type="text"] {
  flex: 1;
  min-width: 0;
  font-family: var(--font-body);
  font-size: 17px;
  font-weight: 700;
  color: var(--ink);
  border: none;
  border-bottom: 2px dashed transparent;
  background: transparent;
  padding: 4px 2px;
}
.review-item input[type="text"]:focus {
  outline: none;
  border-bottom-color: var(--sun-deep);
}
.lang-pill {
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 600;
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 3px 10px;
  background: var(--paper);
  cursor: pointer;
}
.lang-pill.zh { background: var(--red); border-color: var(--red); color: #fff; }
.del-item {
  border: none;
  background: none;
  color: var(--red);
  font-size: 20px;
  font-weight: 800;
  cursor: pointer;
  padding: 2px 8px;
}
.review-actions { display: flex; flex-direction: column; gap: 10px; }

/* ---------------- test screen ---------------- */
.screen-head {
  display: flex;
  align-items: center;
  gap: 10px;
}
.screen-head h2 { flex: 1; text-align: center; margin-right: 52px; }
.btn-back { flex: none; }

/* the 田字格 badge — signature element */
.tian-wrap { display: grid; place-items: center; margin-top: 8px; }
.tian {
  width: 128px;
  height: 128px;
  border: 4px solid var(--ink);
  border-radius: 24px;
  background: var(--card);
  position: relative;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow);
}
.tian span {
  font-family: var(--font-display);
  font-size: 58px;
  font-weight: 600;
}
.tian.speaking {
  animation: pulse 0.9s ease-in-out infinite;
  border-color: var(--sun-deep);
}
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

/* controls */
.controls { display: flex; flex-direction: column; gap: 12px; margin-top: 4px; }
.btn-hear { font-size: 22px; padding: 20px; width: 100%; }
.control-row { display: flex; gap: 12px; }
.control-row .btn { flex: 1; }
.btn-play-all-active { background: var(--red); color: #fff; border-color: var(--red); }

/* finishing — the only way to the answer sheet, and it needs a grown-up's code.
   The layout lives on the inner .parent-gate-body so .parent-gate itself never
   needs a `display`, which the `hidden` attribute relies on. The `[hidden]`
   guard near .sr-only is the backstop if that ever slips. */
.parent-gate {
  padding-top: 12px;
  border-top: 2px dashed var(--rule);
}
.parent-gate-body { display: flex; flex-direction: column; gap: 10px; }
.parent-gate-ask {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 16px;
  text-align: center;
  margin: 0;
}
/* No `:empty` collapse on the gate's message: it sits *above* the buttons, so
   collapsing it would jump them ~24px under a finger already on its way to
   "Not yet". .status reserves 1.4em — leave it reserved. */
#pin-status:empty { min-height: 0; }  /* safe: nothing below it */

/* settings drawers — the voice picker and the grown-up's code */
.settings-drawer { margin-top: 4px; }
.settings-drawer summary { list-style: none; cursor: pointer; }
.settings-drawer summary::-webkit-details-marker { display: none; }
.settings-body { display: flex; flex-direction: column; gap: 10px; padding: 10px 2px 2px; }
.settings-field { display: flex; flex-direction: column; gap: 4px; font-family: var(--font-body); font-size: 13px; font-weight: 700; color: var(--ink); }
.settings-control {
  font-family: var(--font-display);
  font-size: 15px; font-weight: 600; color: var(--ink);
  border: 3px solid var(--ink); border-radius: var(--radius);
  padding: 10px 12px; background: var(--card); box-shadow: var(--shadow);
}
.settings-control:focus-visible { outline: 3px solid var(--red); outline-offset: 3px; }

/* The code fields. Must come *after* .settings-control to win the cascade —
   both are single-class selectors, so this is decided by order alone.
   20px, not the drawer's 15px: iOS zooms the whole page when a text input
   under 16px is focused. */
.pin-input { width: 100%; text-align: center; letter-spacing: 0.35em; font-size: 20px; }
/* Masks the code without a password field — see the note in index.html.
   scroll-margin keeps the buttons clear of the keypad, as .answer-row does. */
#pin-entry { -webkit-text-security: disc; scroll-margin-bottom: 140px; }

/* ---------------- answers screen (the answer sheet) ---------------- */
/* Sticky head and footer: with up to 60 rows, "back", "edit" and
   "read it again" must never be something a child scrolls to find. */
#screen-answers .screen-head,
.answers-actions {
  position: sticky;
  z-index: 2;                 /* the list card is position:relative and would paint over these */
  background: var(--paper);
}
#screen-answers .screen-head {
  top: 0;
  padding: 10px 0;
  border-bottom: 2px solid var(--rule);
}
#screen-answers .screen-head h2 { margin-right: 0; }  /* a button each side already centres it */
#screen-test .screen-head h2 { margin-right: 0; }     /* the back button is gone */

.answers-actions {
  bottom: 0;
  margin-top: auto;           /* a one-word list: the action still sits at the foot of the page */
  padding: 10px 0 calc(4px + env(safe-area-inset-bottom));
  border-top: 2px solid var(--rule);
}
#answers-status { margin-bottom: 8px; }
#answers-status:empty { min-height: 0; }  /* collapses, but stays in the a11y tree */

/* Rows are siblings of .review-item, only bigger: this is the moment the
   child studies the spelling, and the whole row is the tap target. */
.answer-item { border-bottom: 1px solid var(--rule); }
.answer-item:last-child { border-bottom: none; }

.answer-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 66px;           /* 1.5 copybook lines, well over the 44px touch floor */
  padding: 10px 12px 10px 6px;
  border: 0;
  border-radius: 10px;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  scroll-margin: 76px 0 104px; /* keyboard focus must not land under a sticky bar */
}
.answer-row:focus-visible { outline: 3px solid var(--red); outline-offset: -3px; }
/* a flat colour, not a pulse — prefers-reduced-motion kills all animation */
.answer-row.speaking { background: var(--sun); }

.answer-row .num {
  flex: none;
  min-width: 30px;            /* room for "60" */
  font-family: var(--font-display);
  font-size: 17px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  opacity: 0.55;
}
.answer-word {
  flex: 1;
  min-width: 0;               /* let a long word wrap instead of stretching the row */
  font-family: var(--font-display);
  font-size: 26px;
  font-weight: 600;
  line-height: 1.25;
  overflow-wrap: anywhere;
}
/* CJK strokes need more room than Latin letters at the same size */
.answer-word:lang(zh) { font-size: 30px; letter-spacing: 0.02em; }
.answer-speaker { flex: none; font-size: 20px; opacity: 0.75; }

/* ---------------- accessibility & motion ---------------- */
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
