/**
 * Reset, document defaults and element styling.
 *
 * Everything here reads from the semantic tokens only, so both themes are
 * covered without a single colour literal below this line.
 */

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

html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
    /* Checkboxes, radios and ranges in the site's cream, not the system blue. */
    accent-color: var(--color-accent);
}

@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }
}

body {
    margin: 0;
    background-color: var(--color-canvas);
    color: var(--color-text);
    font-family: var(--font-main);
    font-size: var(--text-m);
    line-height: var(--leading-normal);
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

h1, h2, h3, h4, h5, h6 {
    margin: 0 0 var(--space-xs);
    color: var(--color-text-strong);
    font-family: var(--font-heading);
    font-weight: 700;
    line-height: var(--leading-tight);
}

p, ul, ol, dl, figure, pre, table, blockquote {
    margin: 0 0 var(--space-s);
}

a {
    color: var(--color-link);
    text-decoration: none;
    transition: color var(--duration) var(--ease);
}

a:hover,
a:focus-visible {
    color: var(--color-link-hover);
    text-decoration: underline;
}

/*
 * The ring stands off whatever it marks - a link, a button, a card - so it
 * reads as a ring and not as that thing's own edge. A form field is the
 * exception below.
 */
:focus-visible:not(:where(input, textarea, select)) {
    outline: 2px solid var(--color-highlight);
    outline-offset: 2px;
    border-radius: var(--radius-s);
}

/*
 * A field already says it is focused by brightening its border, so a ring
 * standing off it draws a second edge with a gap between the two - which is
 * what it looked like: a doubled border. Here the ring is laid over the
 * border instead, one line in place of two, and the field is left the corners
 * it has when idle (an outline follows the element's own radius, and the rule
 * above was giving every focused thing the same one).
 */
:where(input, textarea, select):focus-visible {
    /* One line thick, like the border it replaces: at 2px it lay half over
       the border and half inside the field, and a focused field read as
       framed rather than as ready to be typed in. */
    outline: var(--border-thin) solid var(--color-highlight);
    outline-offset: -1px;
}

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

hr {
    border: 0;
    border-top: var(--border-thin) solid var(--color-border);
    margin: var(--space-l) 0;
}

code, kbd, samp, pre {
    font-family: var(--font-mono);
    font-size: 0.9em;
}

pre {
    background: var(--color-surface-sunken);
    border: var(--border-thin) solid var(--color-border);
    border-radius: var(--radius-m);
    padding: var(--space-s);
    overflow-x: auto;
}

blockquote {
    border-left: 3px solid var(--color-border-accent);
    padding-left: var(--space-s);
    color: var(--color-text-muted);
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: var(--space-2xs) var(--space-xs);
    border-bottom: var(--border-thin) solid var(--color-border);
    text-align: left;
}

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

/*
 * Wrapped in :where() on purpose. An element+attribute selector outranks a BEM
 * class, so without it a component could not restyle its own control: the
 * header search field kept this frame inside its panel and showed two borders.
 * :where() drops these defaults to zero specificity, which is what a base layer
 * should be — a starting point any component can override with one class.
 */
:where(
    input[type='text'],
    input[type='search'],
    input[type='email'],
    input[type='password'],
    input[type='date'],
    input[type='number'],
    input[type='tel'],
    input[type='url'],
    textarea,
    select
) {
    width: 100%;
    background: var(--color-surface-sunken);
    border: var(--border-thin) solid var(--color-border);
    border-radius: var(--radius-m);
    padding: var(--space-2xs) var(--space-xs);
    transition: border-color var(--duration) var(--ease);
}

/*
 * The arrow of a drop-down is ours, not the browser's. The native one sits
 * hard against the border and padding does not move it - where it is drawn is
 * the user agent's business - so the control is stripped and the chevron is a
 * background image we place ourselves. Kept in the :where() layer, so a
 * component can still restyle its own select with one class.
 */
:where(select) {
    appearance: none;
    background-image: var(--field-arrow);
    background-repeat: no-repeat;
    background-position: right var(--space-xs) center;
    /* The arrow is 12px wide: room for it, plus its own gap on either side. */
    padding-inline-end: calc(var(--space-xs) * 2 + 12px);
}

/* A right-to-left reading puts the arrow on the other side. */
:where(select):dir(rtl) {
    background-position: left var(--space-xs) center;
}

:where(input, textarea, select):focus {
    border-color: var(--color-border-accent);
}

/*
 * A field the browser filled in from saved logins gets its own light-blue
 * ground, and `background` cannot override it: the UA rule is !important.
 * An inset shadow paints over it from inside the border instead, in the
 * field's own colour, and the text colour is set the only way WebKit obeys.
 * The long transition keeps the UA colour from flashing in on fill.
 */
:where(input, textarea, select):-webkit-autofill,
:where(input, textarea, select):-webkit-autofill:hover,
:where(input, textarea, select):-webkit-autofill:focus {
    -webkit-text-fill-color: var(--color-text-strong);
    caret-color: var(--color-text-strong);
    box-shadow: 0 0 0 1000px var(--color-surface-sunken) inset;
    transition: background-color 600000s 0s, color 600000s 0s;
}

:where(input, textarea, select):autofill {
    box-shadow: 0 0 0 1000px var(--color-surface-sunken) inset;
}

/*
 * The date field draws itself from shadow parts the rules above cannot reach.
 * `color-scheme: dark` in tokens.css already keeps the calendar button light
 * instead of a white square, so what is left is the spacing: WebKit gives the
 * button its own padding, which made the control taller than the fields beside
 * it, and an empty segment of the date renders as a placeholder.
 */
:where(input[type='date']) {
    min-height: 0;
}

:where(input[type='date'])::-webkit-calendar-picker-indicator {
    padding: 0;
    margin-inline-start: var(--space-2xs);
    opacity: 0.55;
    cursor: pointer;
    transition: opacity var(--duration) var(--ease);
}

:where(input[type='date'])::-webkit-calendar-picker-indicator:hover {
    opacity: 1;
}

:where(input[type='date'])::-webkit-datetime-edit {
    padding: 0;
}

:where(input[type='date'])::-webkit-datetime-edit-fields-wrapper {
    padding: 0;
}

::placeholder {
    color: var(--color-text-muted);
    opacity: 1;
}

::selection {
    background: var(--color-accent-soft);
    color: var(--color-text-strong);
}

/* Skip link and screen-reader-only text. */
.screen-reader-text {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

.skip-link:focus-visible {
    position: fixed;
    top: var(--space-2xs);
    left: var(--space-2xs);
    z-index: 999;
    width: auto;
    height: auto;
    clip-path: none;
    margin: 0;
    padding: var(--space-2xs) var(--space-s);
    background: var(--color-accent);
    color: var(--color-on-accent);
    border-radius: var(--radius-m);
}
