/*
 * Base: the HTML elements themselves (replaces Pico). Controls are
 * shrink-wrapped and margin-free by default: layouts and components decide
 * widths and spacing (with gap), so nothing has to undo a default.
 */

/* ---------- Reset ---------- */
@layer reset {
    *, ::before, ::after { box-sizing: border-box; }
    html {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
        -webkit-tap-highlight-color: transparent;
    }
    body, h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd, pre { margin: 0; }
    img, svg, video, canvas { max-width: 100%; }
    img, video { height: auto; }
    button, input, select, textarea { font: inherit; color: inherit; letter-spacing: inherit; }
    fieldset { min-width: 0; margin: 0; padding: 0; border: 0; }
    legend { padding: 0; }
    table { border-collapse: collapse; border-spacing: 0; }
    [hidden] { display: none !important; }
}

@layer base {
    /* ---------- Page ---------- */
    html {
        font-size: var(--root-size);
        font-family: var(--font-body);
        line-height: var(--leading-normal);
        color: var(--text);
        background: var(--bg);
        overflow-wrap: break-word;
        text-underline-offset: 0.15em;
        -webkit-font-smoothing: antialiased;
    }
    ::selection { background: var(--selection); }

    /* ---------- Text ---------- */
    h1, h2, h3, h4, h5, h6 {
        font-weight: var(--weight-semibold);
        line-height: var(--leading-tight);
        letter-spacing: var(--tracking-tight);
        color: var(--text-heading);
        text-wrap: balance;
        margin-bottom: var(--space-3);
    }
    /* Literata marks the page title only; section headings stay in the text face. */
    h1 { font-family: var(--font-head); font-size: var(--text-h1); margin-bottom: var(--space-5); }
    h2 { font-size: var(--text-h2); }
    h3 { font-size: var(--text-lg); }
    h4 { font-size: var(--text-base); }
    h5, h6 { font-size: var(--text-sm); }
    /* A heading after running content gets room above it. */
    :where(p, ul, ol, dl, table, form, pre, blockquote) + :is(h2, h3, h4) { margin-top: var(--space-6); }
    p, ul, ol, dl, pre, blockquote { margin-bottom: var(--space-4); }
    ul, ol { padding-left: var(--space-5); }
    dt { font-weight: var(--weight-semibold); }
    strong, b { font-weight: var(--weight-semibold); }
    small { font-size: var(--text-sm); }
    hr { margin: var(--space-6) 0; border: 0; border-top: var(--border-width) solid var(--border); }
    blockquote { padding-left: var(--space-4); border-left: var(--border-thick) solid var(--border); }
    mark { padding: 0 var(--space-0); border-radius: var(--radius-sm); background: var(--mark-bg); color: var(--mark-text); }
    code, kbd, pre { font-family: var(--font-mono); font-size: var(--text-sm); }
    kbd {
        padding: var(--space-0) var(--space-1);
        border: var(--border-width) solid var(--border-strong);
        border-radius: var(--radius-sm);
        background: var(--surface-sunken);
    }

    a { color: var(--primary); text-decoration-thickness: from-font; }
    a:hover { color: var(--primary-hover); }

    /* ---------- Focus ---------- */
    /* A solid outline is visible on every background, filled pills included. */
    :focus-visible { outline: var(--focus-width) solid var(--primary); outline-offset: var(--focus-width); }

    /* ---------- Tables ---------- */
    table { width: 100%; margin-bottom: var(--space-4); }
    th, td {
        padding: var(--space-2) var(--space-3);
        border-bottom: var(--border-width) solid var(--border);
        text-align: start;
        vertical-align: top;
    }
    th { font-weight: var(--weight-semibold); }

    /* ---------- Form controls ---------- */
    label { display: block; }
    legend { font-weight: var(--weight-medium); margin-bottom: var(--space-2); }

    input:not([type="checkbox"], [type="radio"], [type="range"], [type="file"], [type="color"]),
    select,
    textarea {
        appearance: none;
        display: block;
        width: 100%;
        min-width: 0;
        height: var(--control-h);
        padding: var(--control-pad-y) var(--control-pad-x);
        border: var(--border-width) solid var(--border-strong);
        border-radius: var(--radius-md);
        background-color: var(--surface-sunken);
        color: var(--text);
        font-size: var(--control-text);
        line-height: 1.25;
        transition: border-color var(--duration-fast) ease, box-shadow var(--duration-fast) ease;
    }
    textarea {
        height: auto;
        min-height: calc(var(--control-h) * 1.6);
        line-height: var(--leading-snug);
        resize: vertical;
    }
    select:not([multiple], [size]) {
        padding-right: calc(var(--control-pad-x) + var(--space-5));
        background-image: var(--icon-chevron);
        background-position: right var(--space-3) center;
        background-size: 1rem auto;
        background-repeat: no-repeat;
    }
    input[type="search"] {
        padding-left: calc(var(--control-pad-x) + var(--space-5));
        border-radius: var(--radius-pill);
        background-image: var(--icon-search);
        background-position: left var(--control-pad-x) center;
        background-size: 1rem auto;
        background-repeat: no-repeat;
    }
    input[type="search"]::-webkit-search-decoration { appearance: none; }
    /* A control inside its label sits under the caption text. */
    label > :is(input:not([type="checkbox"], [type="radio"]), select, textarea) { margin-top: var(--space-1); }
    ::placeholder { color: var(--text-muted); opacity: 1; }
    :is(input, select, textarea):focus-visible {
        outline: none;
        border-color: var(--primary);
        box-shadow: 0 0 0 var(--focus-width) var(--focus-ring);
    }
    :is(input, select, textarea)[aria-invalid="true"] { border-color: var(--danger); }
    :is(input, select, textarea, button):disabled { opacity: 0.55; cursor: not-allowed; }
    input[readonly] { background-color: transparent; }
    input[type="date"], input[type="time"] { font-variant-numeric: tabular-nums; }
    input[type="color"] {
        width: 3rem;
        height: var(--control-h);
        padding: var(--space-1);
        border: var(--border-width) solid var(--border-strong);
        border-radius: var(--radius-md);
        background: var(--surface-sunken);
    }
    input[type="file"] { font-size: var(--text-sm); }
    /* The file picker's button looks like the app's quiet buttons. */
    input[type="file"]::file-selector-button {
        margin-right: var(--space-3);
        padding: var(--space-1) var(--space-3);
        border: 0;
        border-radius: var(--radius-md);
        background: var(--secondary-fill);
        color: var(--on-secondary);
        font: inherit;
        cursor: pointer;
    }
    input[type="file"]::file-selector-button:hover { background: var(--secondary-fill-hover); }

    /* Checkboxes and radios: drawn boxes in the primary colour. */
    input:is([type="checkbox"], [type="radio"]) {
        appearance: none;
        flex: none;
        width: 1.25em;
        height: 1.25em;
        margin: 0;
        border: var(--focus-width) solid var(--border-strong);
        border-radius: var(--radius-sm);
        background: var(--surface-sunken) center / 0.8em auto no-repeat;
        vertical-align: -0.25em;
        cursor: pointer;
    }
    input[type="radio"] { border-radius: var(--radius-round); }
    input:is([type="checkbox"], [type="radio"]):checked {
        border-color: var(--primary-fill);
        background-color: var(--primary-fill);
    }
    input[type="checkbox"]:checked { background-image: var(--icon-check); }
    input[type="radio"]:checked { box-shadow: inset 0 0 0 0.2em var(--surface); }
    input:is([type="checkbox"], [type="radio"]):focus-visible { box-shadow: 0 0 0 var(--focus-width) var(--focus-ring); }
    /* "[x] Text" labels: the box and its text on one line. */
    label:has(> input:is([type="checkbox"], [type="radio"]):first-child) {
        display: inline-flex;
        align-items: center;
        gap: var(--space-2);
        cursor: pointer;
    }

    progress {
        appearance: none;
        display: block;
        width: 100%;
        height: var(--space-2);
        overflow: hidden;
        border: 0;
        border-radius: var(--radius-pill);
        background: var(--border);
        color: var(--primary-fill);
    }
    progress::-webkit-progress-bar { background: var(--border); }
    progress::-webkit-progress-value { background: var(--primary-fill); }
    progress::-moz-progress-bar { background: var(--primary-fill); }

    /* ---------- Buttons ----------
     * <button> and button-styled links and summaries (a.button, a.secondary,
 * a.outline, summary.outline) share one box.
     * Primary by default; .secondary is the soft grey one, .outline a
     * bordered one on the card surface, .danger destroys. components.css adds
     * .ghost (text only), .icon (square, for an icon) and .segmented groups. */
    button, a.button, a.secondary, a.outline, summary.outline, [type="submit"], [type="button"], [type="reset"] {
        --btn-bg: var(--primary-fill);
        --btn-bg-hover: var(--primary-fill-hover);
        --btn-border: var(--btn-bg);
        --btn-text: var(--on-primary);
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: var(--space-2);
        height: var(--control-h);
        padding: 0 var(--control-pad-x);
        border: var(--border-width) solid var(--btn-border);
        border-radius: var(--radius-md);
        background: var(--btn-bg);
        color: var(--btn-text);
        font-size: var(--control-text);
        font-weight: var(--weight-medium);
        line-height: 1;
        text-decoration: none;
        white-space: nowrap;
        cursor: pointer;
        transition: background-color var(--duration-fast) ease, border-color var(--duration-fast) ease,
            color var(--duration-fast) ease;
    }
    /* Hover swaps the background property, not --btn-bg, so a component that
     * sets the variables (in a later layer) keeps its hover colour. */
    :is(button, a.button, a.secondary, a.outline, summary.outline, [type="submit"], [type="button"], [type="reset"]):hover {
        background: var(--btn-bg-hover);
        color: var(--btn-text);
        text-decoration: none;
    }
    :is(button, a.button, [type="submit"], [type="button"]).secondary, a.secondary {
        --btn-bg: var(--secondary-fill);
        --btn-bg-hover: var(--secondary-fill-hover);
        --btn-border: transparent;
        --btn-text: var(--on-secondary);
    }
    /* :is(element) lifts the variants above the box rule's a.outline. */
    :is(button, a, summary).outline {
        --btn-bg: var(--surface);
        --btn-bg-hover: var(--surface-sunken);
        --btn-border: var(--border-strong);
        --btn-text: var(--text);
    }
    :is(button, a).danger {
        --btn-bg: var(--danger);
        --btn-bg-hover: var(--danger-fill-hover);
        --btn-text: var(--on-danger);
    }
    :is(button, a).danger:focus-visible { outline-color: var(--danger); }

    /* ---------- Disclosure ---------- */
    summary { cursor: pointer; list-style: none; }
    summary::-webkit-details-marker { display: none; }
    /* A chevron at the end of the summary line, turned down when open. */
    summary::after {
        content: "";
        float: right;
        width: 1rem;
        height: 1lh;
        margin-left: var(--space-2);
        background: var(--icon-chevron) center / 1rem auto no-repeat;
        transform: rotate(-90deg);
        transition: transform var(--duration-normal) ease;
    }
    details[open] > summary::after { transform: rotate(0deg); }
    /* The opened content starts below the summary line. */
    details[open] > summary { margin-bottom: var(--space-3); }

    /* ---------- Dialogs ----------
     * Opened with showModal(): the dialog fills the screen as the dimmed
     * overlay and centres its <article> card (a click on the overlay hits
     * the dialog itself, which journal.js uses to close the lesson popup). */
    dialog {
        position: fixed;
        inset: 0;
        z-index: var(--z-overlay);
        width: 100%;
        max-width: none;
        height: 100%;
        max-height: none;
        margin: 0;
        padding: var(--space-4);
        border: 0;
        background: var(--overlay);
        color: var(--text);
    }
    dialog[open] { display: flex; align-items: center; justify-content: center; }
    dialog::backdrop { background: transparent; }
    dialog > article {
        width: min(var(--dialog-width), 100%);
        max-height: calc(100dvh - var(--space-7));
        overflow: auto;
        padding: var(--space-6);
        border: var(--border-width) solid var(--border);
        border-radius: var(--radius-md);
        background: var(--surface);
        box-shadow: var(--shadow-overlay);
    }
    dialog > article footer {
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: var(--space-2);
        margin-top: var(--space-5);
    }
}
