/* settings.css — the accessibility and display preferences, and the settings page.
 *
 * THREE THINGS THIS FILE IS, AND ONE IT IS NOT
 * --------------------------------------------
 * It is (1) the CSS behind the four attributes `display.js` writes onto `<html>`,
 * (2) the layout for one settings row (a label, a control, a note), and (3) the
 * "danger zone" styling for the two irreversible actions. It is NOT a second
 * design system: every colour is a token, every hairline a `color-mix` of `--fg`,
 * and the only saturated colour on the page is the one the product already
 * reserves for "this cannot be undone".
 *
 * WHY THE ATTRIBUTES AND NOT CLASSES
 * ----------------------------------
 * A preference has to apply to the WHOLE document — the header, the lesson, the
 * question card, a modal — and it has to apply before any view renders. An
 * attribute on `<html>` is the one place a rule can reach everything without
 * every component knowing a setting exists, and it is what `boot-display.js` can
 * set with no help from a module that has not loaded yet.
 *
 * `data-theme` already existed in tokens.css (light / dark / absent = system).
 * The other three are new here and are deliberately written so that their ABSENCE
 * is the default state: a browser that never loaded the script, or a signed-out
 * visitor with no stored copy, gets exactly the page it got before.
 */

/* ------------------------------------------------------------- text size
 *
 * Scaling `--step-0` alone would not be enough: the headings are `--step-3` and
 * the small print `--step--1`, and a learner who asks for larger text means all
 * of it. Two steps are offered, and both are modest on purpose — a `largest` that
 * broke the reading column's measure would cost more comprehension than the size
 * gained. The measure itself (`--measure`) is NOT changed: a wider column at a
 * bigger size would undo the point.
 *
 * `rem`-based, so it composes with the browser's own base size rather than
 * overriding it: a learner at 20px default who also picks "large" gets 22.5px.
 */
:root[data-text="large"] {
  --step--1: 0.9rem;
  --step-0: 1.125rem;
  --step-1: 1.25rem;
  --step-2: 1.75rem;
  --step-3: 2.4rem;
}

:root[data-text="largest"] {
  --step--1: 1rem;
  --step-0: 1.25rem;
  --step-1: 1.4rem;
  --step-2: 1.95rem;
  --step-3: 2.7rem;
}

/* KaTeX sets its own size from the surrounding font, but a displayed equation in
 * a matrix can overflow the column before the text does. Let it shrink rather
 * than push the reading column wider. */
:root[data-text] .katex-display { max-width: 100%; overflow-x: auto; overflow-y: hidden; }

/* --------------------------------------------------------- high contrast
 *
 * "High contrast" here is a REAL change to the tokens, not a border added to
 * everything: every text token goes to the extreme, every hairline becomes the
 * foreground at full strength, and the focus ring grows. That is what the setting
 * is asked for — a learner who needs it needs the text legible, and a thicker
 * border around the same grey text would be decoration.
 *
 * Both themes are handled, because the attribute is applied under whichever theme
 * is active. Light uses pure black on pure white; dark uses pure white on pure
 * black. Neither is a new palette, and each satisfies AA at a much higher ratio
 * than the default ramp.
 */
:root[data-contrast="high"] {
  --muted: var(--fg);
  --faint: var(--fg);
  --line: var(--fg);
  --surface: var(--bg);
  --surface-2: var(--bg);
}

:root[data-contrast="high"][data-theme="light"],
:root[data-contrast="high"]:not([data-theme="dark"]) {
  --bg: #ffffff;
  --fg: #000000;
  --ok: #005c2e;
  --bad: #8a0000;
}

:root[data-contrast="high"][data-theme="dark"] {
  --bg: #000000;
  --fg: #ffffff;
  --surface: #000000;
  --surface-2: #101010;
  --ok: #7dffb8;
  --bad: #ff9d9d;
}

/* A system-dark machine with high contrast and no explicit theme: the light
 * block above would win by source order, so this restores the dark ramp. It is
 * the same selector shape tokens.css uses, deliberately. */
@media (prefers-color-scheme: dark) {
  :root[data-contrast="high"]:not([data-theme="light"]) {
    --bg: #000000;
    --fg: #ffffff;
    --surface: #000000;
    --surface-2: #101010;
    --ok: #7dffb8;
    --bad: #ff9d9d;
  }
}

/* Hairlines that are a `color-mix` of `--fg` at 12–16% are invisible at high
 * contrast, and they are what separates a card from the page. Promote them to
 * full strength, and make every focus ring unmistakable. */
:root[data-contrast="high"] * { border-color: var(--fg) !important; }
:root[data-contrast="high"] :focus-visible {
  outline-width: 3px;
  outline-offset: 3px;
}

/* ------------------------------------------------------- reduced motion
 *
 * `prefers-reduced-motion` is honoured already (base.css turns animations off).
 * This attribute is for the learner whose SYSTEM does not say so but who wants it
 * anyway — the setting exists because a browser setting is not something everyone
 * knows how to find. Same rule, on our own attribute.
 */
:root[data-motion="reduced"] { scroll-behavior: auto; }
:root[data-motion="reduced"] *,
:root[data-motion="reduced"] *::before,
:root[data-motion="reduced"] *::after {
  animation: none !important;
  transition: none !important;
}

/* ------------------------------------------------------- keyboard hints
 *
 * The footer of a multiple-choice question names the shortcuts ("1–4 or A–D
 * selects. Enter checks."). A learner who uses a touch screen gets three
 * instructions about keys they do not have, and a learner who simply does not
 * want them has no way to say so — this is that way.
 *
 * `data-keys` is the element's own marker (it is removed when the line becomes a
 * verdict), and `data-keys-hint="off"` on `<html>` is the preference. Both must
 * agree for the line to show, so the verdict is never hidden by a preference.
 */
:root[data-keys-hint="off"] .pq-hint[data-keys] { display: none; }

/* -------------------------------------------------------- collapsible groups
 *
 * The account page is a list of `<details>` groups rather than eight stacked
 * sections, because it is a page a person opens to change ONE thing and closes. A
 * closed group is one line with its heading, so the whole page — the eight things it
 * can do, in the order a person wants them — reads in a single screen, and opening
 * one is a native disclosure: keyboard-operable, announced as expanded/collapsed,
 * and functional before any module loads.
 *
 * The summary carries the `h2`, so the heading structure a screen reader walks is
 * the same as the visual order, and a `<summary>` cannot be an `h2` itself — an
 * `h2` inside it is the arrangement that keeps both true. The marker is drawn
 * rather than left as the platform triangle, because the default one sits
 * differently in every browser and this product has one type ramp.
 *
 * The danger group is styled by `.danger-zone` (below) and keeps that border when
 * closed: a destructive action should be identifiable while it is still collapsed.
 */
.set-group {
  margin-top: var(--space-4);
  border-top: 1px solid var(--hair);
  scroll-margin-top: calc(var(--tap) + var(--space-5) + env(safe-area-inset-top));
}

.set-group:last-of-type { border-bottom: 1px solid var(--hair); }

.set-group-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-height: var(--tap);
  padding: var(--space-3) 0;
  cursor: pointer;
  list-style: none;
}

/* Safari keeps its own marker on a `summary` unless this is reset. */
.set-group-summary::-webkit-details-marker { display: none; }

.set-group-summary::after {
  content: "+";
  font-family: var(--font-ui);
  font-size: var(--step-1);
  line-height: 1;
  color: var(--muted);
  flex: none;
}

.set-group[open] > .set-group-summary::after { content: "–"; }

.set-group-summary > h2 {
  margin: 0;
  font-size: var(--step-1);
  font-weight: 500;
  font-family: var(--font-text);
}

.set-group-summary:hover::after { color: var(--fg); }

.set-group-summary:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
}

/* The body is the group's own flow container, so its rhythm is the page's and a
   group opens into the same spacing every other block has. */
.set-group-body { padding-bottom: var(--space-4); }
.set-group-body > :where(* + *) { margin-top: var(--flow); }
.set-group-body > :where(h2 + *, h3 + *) { margin-top: var(--flow-under-heading); }

/* A nested sub-heading inside a group (the account page's "How you practise" and
   the billing block) keeps the page's small-heading treatment. */
.set-group-body > .h2-small:not(:first-child) { margin-top: var(--space-6); }

/* ------------------------------------------------------------- the page
 *
 * A row is: what it is, the control, and one sentence of why it matters. The
 * groups supply the section rhythm; a row is one fact inside one group.
 */
.set-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  border-bottom: 1px solid color-mix(in srgb, var(--fg) 10%, transparent);
}

.set-row:last-child { border-bottom: 0; }

.set-row-main { flex: 1 1 15rem; min-width: 0; }
.set-row-main > label,
.set-row-main > .set-label {
  display: block;
  font-size: var(--step-0);
  font-weight: 500;
}
.set-row-main > .hint { margin-top: var(--space-1); }
.set-row > .set-control { flex: 0 1 auto; min-width: 9rem; }
.set-row > .set-control select { min-width: 9rem; }

/* A radio group laid out as three toggles, because a native radio pair in a
 * column reads as a form to fill in rather than a choice already made. Same
 * control semantics (real radios), different layout. */
.seg {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.seg > label {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  padding: 0 var(--space-3);
  font-size: var(--step--1);
  cursor: pointer;
  border-right: 1px solid var(--line);
}
.seg > label:last-child { border-right: 0; }
.seg > label > input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  margin: 0;
}
.seg > label:has(input:checked) {
  background: var(--fg);
  color: var(--bg);
  font-weight: 600;
}
.seg > label:has(input:focus-visible) {
  outline: 2px solid var(--focus);
  outline-offset: -3px;
}
/* The dot is not decoration: a checked segment must not rely on colour alone
 * (WCAG 1.4.1), and this product is monochrome anyway. */
.seg > label:has(input:checked)::before {
  content: "✓";
  margin-right: var(--space-1);
}

/* A switch, for the one boolean. A checkbox with a visible track: the native box
 * is 13px on some platforms and the tap target has to be 44. */
.switch {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--tap);
  cursor: pointer;
  font-size: var(--step--1);
}
.switch > input {
  appearance: none;
  width: 2.75rem;
  height: 1.5rem;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-2);
  position: relative;
  cursor: pointer;
  flex: none;
}
.switch > input::after {
  content: "";
  position: absolute;
  top: 0.2rem;
  left: 0.2rem;
  width: 1rem;
  height: 1rem;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 120ms ease-out;
}
.switch > input:checked { background: var(--fg); }
.switch > input:checked::after { background: var(--bg); transform: translateX(1.2rem); }
.switch > input:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* The danger zone. `--bad` is the only colour on a settings page, and it is used
 * for one thing: an action that cannot be undone. */
.danger-zone { border: 1px solid var(--bad); border-radius: var(--radius); padding: var(--space-5); }
.danger-zone > h2 { color: var(--bad); }
.danger-zone .btn.danger {
  border-color: var(--bad);
  color: var(--bad);
  background: transparent;
}
.danger-zone .btn.danger:hover:not(:disabled) { background: var(--bad); color: var(--bg); }

/* A confirmation that has to be typed. The field is deliberately plain — it is
 * a word, not a password — and the button stays disabled until it matches, so the
 * state that cannot be undone is never one click away. */
.confirm-field { max-width: 16rem; }

/* The settings index: four links at the top, each to a section on this page. On a
 * phone the account page is long, and a learner looking for "delete my account"
 * should not have to scroll past four groups to find it. */
.set-nav {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-4);
  margin-top: var(--space-4);
  padding: var(--space-3) 0;
  border-top: 1px solid var(--hair);
  border-bottom: 1px solid var(--hair);
}
.set-nav a { font-size: var(--step--1); }

/* The per-child settings on the Family page: a compact block inside the profile
 * row's detail area, so a parent edits one child without leaving the list. */
.child-settings { display: flex; flex-direction: column; gap: var(--space-3); }
.child-settings .set-row { padding: var(--space-2) 0; }
.child-settings .set-row-main > .set-label { font-size: var(--step--1); }

@media (max-width: 560px) {
  .set-row { align-items: flex-start; }
  .set-row > .set-control,
  .set-row > .set-control select { width: 100%; min-width: 0; }
  .seg { width: 100%; }
  .seg > label { flex: 1 1 0; padding: 0 var(--space-2); }
}
