/* Reset, type, the reading column, KaTeX sizing, focus.
   Lesson and question text belong in .read > .prose. */

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

[hidden] { display: none !important; }

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  font-size: var(--step-0);
  line-height: 1.5;
  font-feature-settings: "kern", "liga", "calt";
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3, p { text-wrap: pretty; }
h1, h2 { text-wrap: balance; overflow-wrap: break-word; }

/* The reset sits at zero specificity so the flow rules below (also zero) can
   layer spacing over it by source order, while any component rule still wins. */
:where(h1, h2, h3, p, ul, ol, dl, figure) { margin: 0; }

/* THE TYPE SCALE, AND WHY THE HEADINGS SIT SO CLOSE TO THEIR TEXT.
 *
 * An editorial page sets a heading and its first paragraph tight — the two are one
 * thought — and puts its clear space ABOVE the heading, where it separates that
 * thought from the last one. The previous scale did the opposite in one place: an
 * h2 carried a full `--flow-heading` above it and a heading's own `line-height`
 * left it visually detached from the paragraph below. The values below are the
 * classic ratios (1.15 for a display size, 1.25 for a section head, 1.4 for a
 * sub-head that sits in running text), and the spacing that goes with them is in
 * the flow rules further down. */
h1 {
  font-family: var(--font-text);
  font-weight: 500;
  font-size: var(--step-3);
  line-height: 1.08;
  letter-spacing: -0.022em;
}

h2 {
  font-family: var(--font-text);
  font-weight: 500;
  font-size: var(--step-2);
  line-height: 1.2;
  letter-spacing: -0.016em;
}

/* An h3 is a SANS sub-head: it labels a part of a page (a settings group, a card's
   section) rather than opening one, so it is small, tight and slightly tracked —
   which is what distinguishes it from the serif heads above it without a rule. */
h3 {
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: var(--step-0);
  line-height: 1.35;
  letter-spacing: -0.005em;
}

a { color: inherit; }

img, svg { max-width: 100%; height: auto; }

button, input, textarea, select { font: inherit; color: inherit; }

::selection {
  background: var(--fg);
  color: var(--bg);
}

:focus {
  outline: none;
  scroll-margin-top: calc(var(--tap) + var(--space-5) + env(safe-area-inset-top));
  scroll-margin-bottom: calc(var(--tap) + var(--space-4));
}

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

[tabindex="-1"]:focus { outline: none; }

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

.skip {
  position: absolute;
  left: var(--space-4);
  top: var(--space-3);
  z-index: 50;
  background: var(--fg);
  color: var(--bg);
  padding: var(--space-2) var(--space-4);
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

/* Off-screen until focused. A transform would take it out of the tab order. */
.skip:not(:focus) {
  padding: 0;
  border: 0;
  width: 1px;
  height: 1px;
  min-height: 0;
  overflow: hidden;
  clip: rect(1px, 1px, 1px, 1px);
  white-space: nowrap;
}

.page {
  padding: var(--space-6) max(var(--space-4), env(safe-area-inset-right)) var(--space-8)
    max(var(--space-4), env(safe-area-inset-left));
}

@media (min-width: 720px) {
  .page { padding-top: var(--space-8); }
}

.read {
  width: min(100%, var(--measure));
  margin-inline: auto;
}

.prose {
  font-family: var(--font-text);
  font-size: var(--step-1);
  line-height: 1.7;
}

/* Vertical rhythm.
   Long-form text (notices, family and account pages, cards, state panels, and
   anything marked .prose or .flow) is spaced by its siblings, not by each page.
   Direct children of a flow container get --flow above them; a heading that
   follows content gets clear space above; the block under a heading stays close
   to it. Margins between block siblings collapse, so this never adds to a gap a
   component already sets. Every selector is wrapped in :where(), so the rules
   have zero specificity: a component's own margin always wins, and a flex or
   grid container that spaces its children with `gap` is not a flow container
   unless it opts in with .flow. */
:where(.read, .prose, .card, .state, .block, .flow) > :where(* + *) {
  margin-top: var(--flow);
}

:where(.read, .prose, .card, .state, .block, .flow) > :where(* + h2) {
  margin-top: var(--flow-heading);
}

/* A THIRD-LEVEL HEAD IS CLOSER TO ITS OWN TEXT THAN TO WHAT CAME BEFORE.
   The old gap was 0.75 × the h2 gap, which at 2rem is 24px — barely more than the
   16px a paragraph gets, so a settings group's sub-head read as another line of
   prose. The clear space above a sub-head is now the space above a section head,
   and the space below it is `--flow-under-minor`, which is the tightest in the
   scale: one label and its first line, inseparable. */
:where(.read, .prose, .card, .state, .block, .flow) > :where(* + h3, * + h4) {
  margin-top: var(--flow-subhead);
}

:where(.read, .prose, .card, .state, .block, .flow) > :where(h1 + *) {
  margin-top: var(--flow-under-h1);
}

:where(.read, .prose, .card, .state, .block, .flow) > :where(h2 + *) {
  margin-top: var(--flow-under-heading);
}

:where(.read, .prose, .card, .state, .block, .flow) > :where(h3 + *, h4 + *) {
  margin-top: var(--flow-under-minor);
}

/* A KICKER SITS DIRECTLY ON ITS HEADING. `.kicker` is the small label above an h1
   ("Today", "Account", "Record"); its own margin is the gap under it, so the flow
   rule must not add a second one — the recorded defect where a page opened with a
   label, a gap, a gap, and then the heading.

   THE VALUE IS A TOKEN, and `0` is its own: `tests/check_rhythm.py` requires every
   flow gap to come from a `--flow*` token, which is what keeps long-form spacing in
   one place. `--flow-none` is that token — named rather than written as a bare `0` so
   a container could override it without the rule being re-stated here. */
:where(.read, .prose, .card, .state, .block, .flow) > :where(.kicker + *) {
  margin-top: var(--flow-none);
}

/* Bare lists in a flow container: indented, and items that breathe. A list with
   a class (.plain-list, .rows, .family-list) styles itself. */
:where(.read, .prose, .card, .state, .block, .flow) > :where(ul, ol):where(:not([class])) {
  padding-inline-start: 1.25em;
}

:where(.read, .prose, .card, .state, .block, .flow) > :where(ul, ol):where(:not([class])) > :where(li + li) {
  margin-top: var(--flow-item);
}

.card { --flow-heading: var(--space-5); }

/* A caption, note or link row that follows a card is a new block, not a caption
   to it: .hint's own small margin is for text under a field or a figure. */
.card + .hint { margin-top: var(--flow); }

/* A link inside a sentence stays on the line. a.link is an inline-flex 44px
   target for link rows; in running text that stretches its line box and makes
   one line of a paragraph taller than the rest. */
p:not([class]) a.link,
li:not([class]) a.link {
  display: inline;
  min-height: 0;
}

.prose a {
  text-decoration: underline;
  text-underline-offset: 0.16em;
}

/* THE KICKER IS THE PAGE'S NAME TAG, and it carries a gold rule beside it: the one
   ornament in the type system, and it is the identity's accent rather than a new
   colour. The rule is drawn with a pseudo-element so no markup changed — a page's
   `<p class="kicker">` still reads as one string to every gate that counts them
   (`check_today_clarity.py` counts `.kicker` on Today, and an inline `<span>` would
   be a second thing to keep in step). */
.kicker {
  font-family: var(--font-ui);
  font-size: var(--step--1);
  font-weight: 500;
  line-height: 1.4;
  /* Set as a small label rather than as small prose: the tracking and the caps are
     what make "Today" and "Account" read as the page's name tag instead of as a
     stray line of body text above the heading. Sentence case is kept — the product
     is not a magazine and a shouted label is the ornament §2 forbids — so only the
     letter-spacing, the weight and the gold mark carry it. */
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 var(--space-2);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.kicker::before {
  content: "";
  width: 1.5rem;
  height: 2px;
  flex: none;
  background: var(--gold);
  border-radius: 1px;
}

.muted { color: var(--muted); }
.faint { color: var(--faint); }

.mono, code, kbd {
  font-family: var(--font-mono);
  font-size: 0.92em;
  overflow-wrap: anywhere;
}

.num { font-variant-numeric: tabular-nums; }

/* KaTeX sits in the reading size, and a wide matrix scrolls inside the column. */
.katex { font-size: 1.05em; color: inherit; }

/* KaTeX 0.19 clips MathML with clip-path:inset(50%), and Chrome then drops
   the MathML from the accessibility tree. Keep it off-screen without clip-path
   so a screen reader still gets the MathML. The HTML copy stays aria-hidden. */
.katex .katex-mathml {
  clip: auto;
  clip-path: none;
  height: 1px;
  width: 1px;
  margin: -1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
}

.prose .katex { font-size: 1.02em; }

/* An inline formula holding a matrix is as tall as three or four lines of text.
   Inline, it sits on the paragraph's line-height, so A=[..] B=[..] v=[..] on
   consecutive lines touch. As an inline block with its own vertical margin each
   matrix gets room, and a wide one scrolls rather than overflowing the column. */
.katex:has(.mtable) {
  display: inline-block;
  margin: var(--space-3) 0;
  vertical-align: middle;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
}
/* A displayed equation already has its own margin. */
.katex-display > .katex:has(.mtable) { margin: 0; }

/* Displayed mathematics sits at a fixed indent from the left edge, like the
   prose and headings around it, rather than centred (amsmath's fleqn). The
   eye finds every display at the same place. */
.katex-display { text-align: left; padding-left: var(--space-5); }
.katex-display > .katex { text-align: left; }

/* A worked example as labelled steps (`:::step Label`): the label column names
   the subgoal, the body holds one short sentence and its display. On a phone
   the label sits above its step. */
.wx-s {
  display: grid;
  grid-template-columns: 5.5rem minmax(0, 1fr);
  column-gap: var(--space-4);
  align-items: baseline;
  margin: 0 0 var(--space-4);
}
.wx-s:last-child { margin-bottom: 0; }
.wx-l {
  font-family: var(--font-ui);
  font-size: 0.78em;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.wx-b { min-width: 0; }
.wx-b > :first-child { margin-top: 0; }
.wx-b > :last-child { margin-bottom: 0; }
.wx-b p { margin: 0 0 var(--space-2); }
.wx-b .katex-display { margin: var(--space-2) 0; padding-bottom: 0; }
.wx-b .katex-display { padding-left: 0; }
@media (max-width: 520px) {
  .wx-s { grid-template-columns: minmax(0, 1fr); row-gap: var(--space-1); }
}

.katex-display {
  margin: var(--space-5) 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: var(--space-2);
  max-width: 100%;
}

/* Wide mathematics scrolls inside its own box. Padding sits in the faded
   strip, so at either end the formula itself is solid and the empty pad is
   what fades. In the middle, both edges of the visible formula fade out.
   The mask is greyscale: it only changes how much ink shows. */
.katex.math-fit,
.katex-display.math-fit {
  box-sizing: border-box;
  display: inline-block;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  vertical-align: middle;
}

/* KaTeX lays a matrix out a couple of pixels wider than it inks, so these boxes
   report an overflow on formulas that plainly fit and draw a scrollbar under
   every matrix. Only .math-scroll (genuinely wider than the column) shows one. */
.katex:has(.mtable):not(.math-scroll),
.katex-display:not(.math-scroll),
.katex.math-fit,
.katex-display.math-fit {
  scrollbar-width: none;
}
.katex:has(.mtable):not(.math-scroll)::-webkit-scrollbar,
.katex-display:not(.math-scroll)::-webkit-scrollbar,
.katex.math-fit::-webkit-scrollbar,
.katex-display.math-fit::-webkit-scrollbar {
  display: none;
}

.katex-error,
.math-err {
  display: inline-block;
  max-width: 100%;
  overflow-wrap: anywhere;
  white-space: normal;
  vertical-align: middle;
}

.katex.math-scroll,
.katex-display.math-scroll {
  box-sizing: border-box;
  display: block;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  padding-inline: 1.15rem;
  scroll-padding-inline: 1.15rem;
  mask-image: linear-gradient(to right, transparent, #000 1.15rem, #000 calc(100% - 1.15rem), transparent);
  -webkit-mask-image: linear-gradient(to right, transparent, #000 1.15rem, #000 calc(100% - 1.15rem), transparent);
}

.katex-display.math-scroll { text-align: start; }

.table-scroll {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.math-err {
  font-family: var(--font-mono);
  font-size: var(--step--1);
  color: var(--bad);
}
