/*
 * The semantic token set of VisualDesign.md section 12, and the only place a theme colour is
 * written down. Components consume `var(--text)`, never `#17201a`; a unit test reads every
 * stylesheet under src/ and fails on a colour literal, so that is a gate rather than a habit.
 *
 * There is no theme selector in this file, which is the point. Each colour is one
 * `light-dark()` pair, and `light-dark()` resolves against the element's used `color-scheme`.
 * static/theme.css already decides `color-scheme` from all three inputs the product has: the
 * system preference, the `data-theme` attribute the boot script writes from the preference
 * cookie, and the per-request `/theme-preference.css` override that serves a visitor with
 * scripting off. So the palette inherits every one of them for free, and a future fourth input
 * needs no edit here. Duplicating a light block and a dark block across those same selectors
 * would have worked too, and would have given the unscripted visitor the system palette under
 * the stored `color-scheme`: right canvas, wrong everything else.
 *
 * The contrast gate lives in tests/tokens/contract.ts. It parses this file, parses the spec's
 * own tables and prose, computes WCAG 2.2 relative luminance for every text and background pair
 * in both themes, and fails on a miss. It also fails on a colour token that no pair covers, so
 * adding one here without saying what it sits on is a red test rather than an untested colour.
 * vite.config.ts runs the same function at build start, so a failing palette cannot be built.
 */

:root {
	/*
	 * Colour. Light value first, dark second, matching the spec table's column order. Correct
	 * typing deliberately has no token: it settles into --text, and the accent stays with action
	 * and position (section 4).
	 */
	--canvas: light-dark(#f5f7f5, #101512);
	--surface: light-dark(#fbfdfb, #18201b);
	--surface-muted: light-dark(#e9eeea, #222d26);
	--text: light-dark(#17201a, #edf4ef);
	--text-muted: light-dark(#536158, #aab8ae);
	--border: light-dark(#cbd5ce, #3d4b42);
	--accent: light-dark(#0b6b50, #5bd8ab);
	--on-accent: light-dark(#ffffff, #102019);
	--error: light-dark(#a52a36, #ff8f99);
	--warning: light-dark(#875500, #f1bd5b);

	/*
	 * The edge of a control, as distinct from the hairline between two text groups.
	 *
	 * --border is exempt from the contrast gate because WCAG 2.2 puts no ratio on a separator that
	 * carries no information. That exemption was written while no real control existed, and it
	 * stops being true the moment an edge is the thing that says a control is there: 1.4.11 puts
	 * that at 3:1, and --border measures 1.28:1 to 1.47:1 in light and 1.55:1 to 2.01:1 in dark.
	 *
	 * Raising --border is not available. The spec's section 12 table fixes both its values and
	 * tests/tokens/contract.ts fails on a token whose value or existence the table does not match,
	 * so a new colour cannot be invented here either. What is available, and is the honest answer
	 * rather than the one left over, is to stop asking --border to do the job: this alias points a
	 * control's edge at a colour the gate already measures against every background. Nothing new is
	 * declared, --border keeps its stated meaning, and tests/tokens/contract.ts now measures this
	 * alias at the 1.4.11 threshold, so repointing it at the hairline is a red test rather than a
	 * quiet regression.
	 */
	--control-edge: var(--text-muted);

	/* Palette primitives are private to this file. The active names above remain the only names
	 * components may consume. The origin override uses these values for a visitor without script. */
	--high-contrast-light-canvas: #ffffff;
	--high-contrast-light-surface: #ffffff;
	--high-contrast-light-surface-muted: #ffffff;
	--high-contrast-light-text: #000000;
	--high-contrast-light-text-muted: #1a1a1a;
	--high-contrast-light-border: #000000;
	--high-contrast-light-accent: #000000;
	--high-contrast-light-on-accent: #ffffff;
	--high-contrast-light-error: #8b0000;
	--high-contrast-light-warning: #5c4200;
	--high-contrast-dark-canvas: #000000;
	--high-contrast-dark-surface: #000000;
	--high-contrast-dark-surface-muted: #000000;
	--high-contrast-dark-text: #ffffff;
	--high-contrast-dark-text-muted: #e6e6e6;
	--high-contrast-dark-border: #ffffff;
	--high-contrast-dark-accent: #ffffff;
	--high-contrast-dark-on-accent: #000000;
	--high-contrast-dark-error: #ffb3b8;
	--high-contrast-dark-warning: #ffe4a3;

	/* Space: a 4px base on the named steps, so --space-6 is six steps rather than 6px. */
	--space-1: 4px;
	--space-2: 8px;
	--space-3: 12px;
	--space-4: 16px;
	--space-6: 24px;
	--space-8: 32px;
	--space-12: 48px;
	--space-16: 64px;
	--space-24: 96px;

	/* Interactive controls meet the 48px block target floor in VisualDesign.md section 3. */
	--control-min-block: 48px;

	/* The page gutter. Its two steps up are the media queries below. */
	--gutter: 16px;

	/* Measures. The shell is the whole page; the practice column is the reading measure. */
	--shell-max: 80rem;
	--practice-max: 48rem;

	/* Shape. Two radii exist and nothing else may invent a third. Buttons are never pills. */
	--radius-control: 12px;
	--radius-panel: 20px;

	/*
	 * Layers. The whole scale, so a component never writes a bare z-index. The gaps are the
	 * point: there is no room to slip a value between two named layers without adding one here
	 * and arguing for it against the spec.
	 */
	--layer-base: 0;
	--layer-sticky-navigation: 10;
	--layer-popover: 20;
	--layer-dialog-backdrop: 30;
	--layer-dialog: 40;
	--layer-urgent-system-notice: 50;
}

/* Ordinary text-entry and action controls share the reviewed target floor. Native choice inputs
 * keep their compact glyph while their local label owns the full pointer target. The issued mobile
 * keyboard is excluded because its server-signed rectangles are the target geometry. */
:where(
	button:not(.keyboard-key),
	input:not([type='checkbox']):not([type='radio']),
	select,
	textarea
) {
	min-block-size: var(--control-min-block);
}

:where(
	label:has(input[type='checkbox']),
	label:has(input[type='radio'])
) {
	display: inline-flex;
	align-items: center;
	min-block-size: var(--control-min-block);
}

/*
 * The high-contrast variants replace the same semantic token set. Components do not receive a
 * second class or a special colour, so public, private and prerendered surfaces cannot drift.
 * The values are deliberately neutral. Error and warning retain their semantic colour, while
 * their existing text, border, icon and state labels remain the non-colour cue.
 */
:root[data-theme='high-contrast-light'] {
	--canvas: var(--high-contrast-light-canvas);
	--surface: var(--high-contrast-light-surface);
	--surface-muted: var(--high-contrast-light-surface-muted);
	--text: var(--high-contrast-light-text);
	--text-muted: var(--high-contrast-light-text-muted);
	--border: var(--high-contrast-light-border);
	--accent: var(--high-contrast-light-accent);
	--on-accent: var(--high-contrast-light-on-accent);
	--error: var(--high-contrast-light-error);
	--warning: var(--high-contrast-light-warning);
	--control-edge: var(--text-muted);
}

:root[data-theme='high-contrast-dark'] {
	--canvas: var(--high-contrast-dark-canvas);
	--surface: var(--high-contrast-dark-surface);
	--surface-muted: var(--high-contrast-dark-surface-muted);
	--text: var(--high-contrast-dark-text);
	--text-muted: var(--high-contrast-dark-text-muted);
	--border: var(--high-contrast-dark-border);
	--accent: var(--high-contrast-dark-accent);
	--on-accent: var(--high-contrast-dark-on-accent);
	--error: var(--high-contrast-dark-error);
	--warning: var(--high-contrast-dark-warning);
	--control-edge: var(--text-muted);
}

/* The operating system owns forced-colour choices. Rebinding semantic tokens preserves every
 * surface that consumes them while keeping native controls and user-agent focus treatment intact. */
@media (forced-colors: active) {
	:root {
		--canvas: Canvas;
		--surface: Canvas;
		--surface-muted: Canvas;
		--text: CanvasText;
		--text-muted: CanvasText;
		--border: CanvasText;
		--accent: Highlight;
		--on-accent: HighlightText;
		--error: LinkText;
		--warning: CanvasText;
		--control-edge: CanvasText;
	}
}

@media (min-width: 48rem) {
	:root {
		--gutter: 24px;
	}
}

@media (min-width: 64rem) {
	:root {
		--gutter: 40px;
	}
}

/*
 * The two declarations that make the palette visible. Without them the page is painted in the
 * user-agent canvas that `color-scheme` picks, which is close to --canvas but is not it, and
 * every token above would be a value nothing reads.
 */
:root {
	background-color: var(--canvas);
	color: var(--text);
}
