/* Mostly classless: layout is driven by semantic elements. Classes are only
   for functional bits (the skip link, the draft notice, tool pages, page
   lists, diagrams) and the copy buttons a script adds.
   Sans for prose, mono for paths, dates and code. */

/* Palettes: gruvbox, nord, dracula, solarized. Each sets the same eight
   tokens, selected by <html data-theme="...">. base.html renders `palette`
   from config.toml (unset means gruvbox), and palette.js swaps in the
   reader's stored choice before first paint. Without JavaScript the config
   palette applies. The picker chooses a palette, not a scheme: gruvbox, nord
   and solarized follow prefers-color-scheme, dracula is dark only.
   Code blocks keep the Zola highlighting theme whatever the palette.
   Every text colour must clear 4.5:1 on --bg, --bg-soft and --code-bg: a
   link can wrap inline code. Spec colours that don't are swapped or nudged,
   noted per palette.
   Token blocks stay flat, not nested: the Go tests read them with a regex. */

:root {
    color-scheme: light dark;
    --font-sans: system-ui, sans-serif;
    --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
}

/* Gruvbox. https://github.com/morhetz/gruvbox
   Light --muted is fg3, since fg4 is too dim. Light --accent is faded orange
   nudged a touch darker, so it clears light1, the code background. */
:root,
:root[data-theme="gruvbox"] {
    --bg: #fbf1c7;
    --bg-soft: #f2e5bc;
    --fg: #3c3836;
    --muted: #665c54;
    --link: #076678;
    --accent: #aa3803;
    --border: #d5c4a1;
    --code-bg: #ebdbb2;
}

@media (prefers-color-scheme: dark) {
    /* Don't let the OS preference override another palette */
    :root:not([data-theme]),
    :root[data-theme="gruvbox"] {
        --bg: #282828;
        --bg-soft: #32302f;
        --fg: #ebdbb2;
        --muted: #a89984;
        --link: #83a598;
        --accent: #fe8019;
        --border: #3c3836;
        --code-bg: #1d2021;
    }
}

/* Nord, light and dark. https://www.nordtheme.com
   Light is Snow Storm with Polar Night text. Its link and accent are nord10
   and nord12 darkened until they pass: as specified they reach only 3.5:1 and
   2.5:1 on nord6. Dark --muted sits between nord3, unreadable on nord0, and
   nord4, barely dimmer than --fg. Dark --accent is nord12 lightened. */
:root[data-theme="nord"] {
    --bg: #eceff4;        /* nord6  */
    --bg-soft: #e5e9f0;   /* nord5  */
    --fg: #2e3440;        /* nord0  */
    --muted: #4c566a;     /* nord3  */
    --link: #456285;      /* nord10, darkened */
    --accent: #974a32;    /* nord12, darkened */
    --border: #d8dee9;    /* nord4  */
    --code-bg: #d8dee9;   /* nord4  */
}

@media (prefers-color-scheme: dark) {
    :root[data-theme="nord"] {
        --bg: #2e3440;        /* nord0  */
        --bg-soft: #3b4252;   /* nord1  */
        --fg: #eceff4;        /* nord6  */
        --muted: #b1b8c5;     /* derived */
        --link: #88c0d0;      /* nord8  */
        --accent: #d8a495;    /* derived */
        --border: #434c5e;    /* nord2  */
        --code-bg: #272b35;   /* derived */
    }
}

/* Dracula, dark only. https://draculatheme.com
   --muted is a lighter Comment, the original is too dim for small text. */
:root[data-theme="dracula"] {
    color-scheme: dark;
    --bg: #282a36;        /* Background   */
    --bg-soft: #343746;   /* derived      */
    --fg: #f8f8f2;        /* Foreground   */
    --muted: #98a0bd;     /* derived      */
    --link: #8be9fd;      /* Cyan         */
    --accent: #ff79c6;    /* Pink         */
    --border: #44475a;    /* Current Line */
    --code-bg: #21222c;   /* Darker       */
}

/* Solarized, light and dark. https://ethanschoonover.com/solarized
   Text is two steps stronger than the spec, which is dim, and link and
   accent are nudged until they pass. Light --muted is base01 a touch darker,
   so it also clears base2, the code background. Dark accent is yellow since
   orange is too dim on base03. */
:root[data-theme="solarized"] {
    --bg: #fdf6e3;        /* base3   */
    --bg-soft: #f5efdc;   /* derived */
    --fg: #073642;        /* base02  */
    --muted: #556b72;     /* base01, darkened */
    --link: #1b6b9c;      /* blue, darkened   */
    --accent: #ae461a;    /* orange, darkened */
    --border: #e0d8c2;    /* derived */
    --code-bg: #eee8d5;   /* base2   */
}

@media (prefers-color-scheme: dark) {
    :root[data-theme="solarized"] {
        --bg: #002b36;        /* base03  */
        --bg-soft: #073642;   /* base02  */
        --fg: #eee8d5;        /* base2   */
        --muted: #93a1a1;     /* base1   */
        --link: #3faaa0;      /* cyan, lightened   */
        --accent: #bd9519;    /* yellow, lightened */
        --border: #0b4351;    /* derived */
        --code-bg: #001f27;   /* derived */
    }
}

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

html {
    font-size: 106.25%; /* 17px by default, but follows the reader's font size setting */
    line-height: 1.65;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-block-size: 100dvh;
    display: flex;
    flex-direction: column;
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--fg);
    overflow-wrap: break-word; /* long URLs wrap instead of widening the page */
}
/* Text is held to 40rem, with a gutter of at least 1rem on narrow screens */
body > :is(header, main, footer) { padding-inline: max(1rem, (100% - 40rem) / 2); }

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

/* Links in running text are underlined: in most palettes the link colour is
   under 3:1 against the text around it, too close for colour alone (WCAG
   1.4.1). The line is half strength until hover or keyboard focus, so prose
   stays quiet. Nav rows and the page lists (ul.links) are lists of links,
   where every row is a link anyway, so theirs aren't. */
a {
    color: var(--link);
    text-decoration-line: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 0.2em;
    text-decoration-color: color-mix(in srgb, currentColor 50%, transparent);
}
a:is(:hover, :focus-visible) {
    color: var(--accent);
    text-decoration-thickness: 2px;
    text-decoration-color: currentColor;
}
:is(nav, ul.links) a { text-decoration-line: none; }
:is(nav, ul.links) a:is(:hover, :focus-visible) { text-decoration-line: underline; }

/* Header: where you are on the left, where you can go on the right. The <p>
   in each nav gives it a line of its own in w3m. */
body > header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.25rem 1.5rem;
    padding-block: 1.1rem;
    font-family: var(--font-mono);
    font-size: 0.85rem;

    p { margin: 0; }
}
nav[aria-label="Breadcrumb"] {
    [aria-hidden] { color: var(--muted); }
    a[aria-current] { color: var(--fg); }
}
nav[aria-label="Site"] p {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 1.1rem;

    a { color: var(--muted); }
    a[aria-current] {
        color: var(--fg);
        text-decoration: underline 2px var(--accent);
        text-underline-offset: 0.45em;
    }
}

body > footer {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding-block: 1.25rem 2rem;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 0.8rem;

    p { margin: 0; }
    nav p { display: flex; flex-wrap: wrap; gap: 0.25rem 1rem; }
    nav a { color: var(--muted); }
}

main { flex: 1; padding-block: 0.75rem 2.5rem; }

h1, h2, h3 { line-height: 1.2; text-wrap: balance; }
p, li, dd { text-wrap: pretty; }

h1 { font-size: clamp(1.9rem, 1.4rem + 2vw, 2.5rem); letter-spacing: -0.02em; margin: 0 0 1rem; }
h2 { font-size: clamp(1.3rem, 1.15rem + 0.7vw, 1.5rem); margin: 2.75rem 0 0.6rem; }
h3 { font-size: 1.05rem; margin: 1.75rem 0 0.35rem; }
p { margin: 0 0 1rem; }

/* The paragraph right under a page or section heading is its summary */
main > h1 + p { font-size: 1.15rem; line-height: 1.5; }
section > h2 + p { color: var(--muted); }

/* Article dateline, matched on <time> to avoid a class */
article > h1 + p:has(> time) {
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 0.8rem;
}

time, dt {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
}

/* Term and value side by side: the about page's stack, the page details.
   Stacked when narrow. */
dl {
    display: grid;
    grid-template-columns: 8.5rem 1fr;
    align-items: baseline;
    gap: 0.6rem 1.25rem;
    margin: 1.5rem 0 2rem;

    dt { color: var(--muted); }
    dd { margin: 0; }

    @media (width < 34rem) {
        grid-template-columns: 1fr;
        gap: 0;

        dd { margin-block-end: 0.8rem; }
    }
}

/* Listings are plain rows. Prose lists in articles keep their markers. */
main > ul, section ul {
    list-style: none;
    padding-inline-start: 0;

    > li { margin-block: 0.35rem; }
}

/* Dated rows: the date in its own column, the title beside it. A page
   without a date keeps to the title column. */
ul.links:has(time) > li {
    display: grid;
    grid-template-columns: 6.5rem 1fr;
    gap: 0 1rem;
    align-items: baseline;

    > time { color: var(--muted); }
    > a:only-child { grid-column: 2; }
}

/* Reference groups: names flow in a line under each heading */
section > :is(h2, h3) + ul {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 1.1rem;

    > li { margin-block: 0; }
}

article :is(ul, ol) { padding-inline-start: 1.5rem; }
article li {
    margin: 0.3rem 0;

    &::marker { color: var(--muted); }
}

/* A cheatsheet's table of contents, quieter than the sections it lists */
nav[aria-label="Contents"] {
    font-size: 0.95rem;

    h2 { margin-block: 1.5rem 0.35rem; font-size: 1.05rem; }
    ul { margin-block: 0; }
    li { margin-block: 0.1rem; }
}

/* Cheatsheet filter, added by cheatsheet-filter.js. The status keeps its line
   while empty, so typing doesn't shift the list. */
search {
    display: block;
    margin-block: 1.5rem;

    p { margin: 0; }
    input { margin-inline-start: 0.5rem; }
    [role="status"] { min-block-size: 1lh; color: var(--muted); font-size: 0.85rem; }
}

/* Page details under an article */
aside {
    margin-block-start: 3rem;
    padding-block-start: 1rem;
    border-block-start: 1px solid var(--border);
    font-size: 0.95rem;

    dl { margin-block: 0; }
}

/* Code */
code {
    font-family: var(--font-mono);
    font-size: 0.88em;
    background: var(--code-bg);
    border: 1px solid var(--border);
    padding: 0.05rem 0.3rem;
    border-radius: 0.25rem;
}
pre {
    background: var(--code-bg); /* highlighted <pre class="giallo"> sets its own inline bg */
    border: 1px solid var(--border);
    padding: 0.9rem 1rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    tab-size: 2;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;

    code { background: none; border: none; padding: 0; font-size: 0.85rem; }

    /* Language label. Zola puts data-lang on <code>, not <pre>. Unlabelled
       fences get `plain`, which is hidden. */
    > code[data-lang] {
        display: block;

        &::before {
            content: attr(data-lang);
            display: block;
            margin-block-end: 0.5rem;
            color: var(--muted);
            font-size: 0.72rem;
            letter-spacing: 0.06em;
        }
        &[data-lang="plain"]::before { content: none; }
        /* Zola's name for bash, sh and shell fences */
        &[data-lang="shellscript"]::before { content: "shell"; }
    }
}

/* Highlighted blocks are dark in every palette and scheme, since Zola writes
   the gruvbox-dark-hard colours inline. So their label, border and copy button
   take the full gruvbox dark token set, or light mode puts light chrome on a
   dark block. A <pre> without highlighting keeps the page's tokens. */
pre.giallo,
.code-block:has(> pre.giallo) {
    color-scheme: dark;
    --bg: #1d2021;
    --bg-soft: #32302f;
    --fg: #ebdbb2;
    --muted: #a89984;
    --link: #83a598;
    --accent: #fe8019;
    --border: #3c3836;
    --code-bg: #1d2021;
}

/* gruvbox-dark-hard has a few colours under 4.5:1 on its own background.
   Swap in gruvbox's lighter variants; !important beats the inline styles.
   Screen only, so print stays black. */
@media screen {
    pre.giallo {
        [style*="#928374" i] { color: #a89984 !important; }
        [style*="#cc241d" i] { color: #fb4934 !important; }
        [style*="#b16286" i] { color: #d3869b !important; }
    }
}

/* Code blocks a bit wider than the text on wide screens */
@media (width >= 56rem) {
    article > pre, article > .code-block { margin-inline: -3rem; }
}

/* Copy-to-clipboard button, added by copy-code.js (progressive enhancement). */
.code-block { position: relative; }
.copy-code {
    position: absolute;
    inset-block-start: 0.4rem;
    inset-inline-end: 0.4rem;
    padding: 0.2rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 0.3rem;
    background: var(--bg);
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 0.72rem;
    cursor: pointer;

    &:hover { color: var(--accent); border-color: var(--accent); }
    &.copied { color: var(--link); border-color: var(--link); }

    @media (hover: hover) {
        opacity: 0;

        .code-block:hover &, &:focus-visible { opacity: 1; }
    }
}
/* Touch screens always show the button. Labelled blocks have room for it on
   the label line; unlabelled ones get a line of their own. */
@media (hover: none) {
    .code-block > pre:has(> code[data-lang="plain"]) { padding-block-start: 2.25rem; }
}

blockquote {
    margin: 1.5rem 0;
    padding: 0.6rem 1rem;
    border-inline-start: 3px solid var(--accent);
    border-start-end-radius: 0.3rem;
    border-end-end-radius: 0.3rem;
    background: var(--bg-soft);
    color: var(--muted);

    > :first-child { margin-block-start: 0; }
    > :last-child { margin-block-end: 0; }
}

table {
    inline-size: 100%;
    border-collapse: collapse;
    margin: 1.5rem 0;

    @media (width <= 37.5rem) { display: block; overflow-x: auto; }

    th, td { padding: 0.5rem; text-align: start; border-block-end: 1px solid var(--border); }
    th {
        font-family: var(--font-mono);
        font-size: 0.85rem;
        font-weight: 600;
        background: var(--bg-soft);
        border-block-end: 2px solid var(--border);
    }
}

hr { border: none; border-block-start: 1px solid var(--border); margin: 2.5rem 0; }
img, svg { max-inline-size: 100%; block-size: auto; }
/* Graphviz leaves label text unfilled, which is black on a dark palette */
.diagram text { fill: currentColor; }

/* Draft banner (only in a --drafts preview build). */
.draft-notice {
    padding: 0.5rem 1rem;
    border: 1px dashed var(--accent);
    border-radius: 4px;
    font-weight: 600;
}

/* Interactive tool pages (the uuid generator). */
.tool {
    padding: 1rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    background: var(--bg-soft);

    :is(input, textarea) {
        inline-size: 100%;
        font-family: var(--font-mono);
    }
}
input, textarea, select, button { font: inherit; }
/* --muted, not --border, so control borders clear 3:1 (WCAG 1.4.11) */
input, textarea, select {
    padding: 0.3rem;
    max-inline-size: 100%;
    border: 1px solid var(--muted);
    border-radius: 0.3rem;
    background: var(--bg);
    color: var(--fg);
    caret-color: var(--accent);
    accent-color: var(--accent);
}
button {
    padding: 0.4rem 0.9rem;
    border: 1px solid var(--muted);
    border-radius: 0.3rem;
    background: var(--bg);
    color: var(--fg);
    cursor: pointer;

    &:hover { border-color: var(--accent); color: var(--accent); background: var(--bg-soft); }
}

/* Skip link: off-screen until focused. */
.skip-link {
    position: absolute;
    inset-inline-start: 0.5rem;
    inset-block-start: -3rem;
    padding: 0.5rem 0.9rem;
    border-radius: 0.3rem;
    background: var(--accent);
    color: var(--bg);

    &:focus { inset-block-start: 0.5rem; }
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
:target, article :is(h2, h3) { scroll-margin-block-start: 2rem; }

@media (prefers-reduced-motion: no-preference) {
    html { scroll-behavior: smooth; }
}

/* Print. Black on white whatever the palette, mostly for cheatsheets. The
   breadcrumb stays, so the page says where it came from. */
@media print {
    @page { margin: 1.5cm; }

    /* Light tokens on paper, or dark palettes print light grey text. The
       selectors match the palette rules' specificity and come later. */
    :root[data-theme],
    :root:not([data-theme]) {
        color-scheme: light;
        --bg: #fff;
        --bg-soft: #fff;
        --fg: #000;
        --muted: #444;
        --link: #000;
        --accent: #000;
        --border: #999;
        --code-bg: #fff;
    }

    html { font-size: 11pt; }
    body > :is(header, main, footer) { padding-inline: 0; }
    body > header { padding-block: 0; }

    /* Screen-only bits */
    .skip-link,
    .copy-code,
    nav[aria-label="Site"],
    nav[aria-label="Contents"],
    search,
    aside,
    body > footer { display: none; }

    /* Print the URL after prose links */
    article :is(p, li) a[href^="http"]::after {
        content: ' <' attr(href) '>';
        font-size: 0.85em;
        word-break: break-all;
    }

    /* !important to beat the highlighter's inline colours */
    pre, pre *, pre *::before, code { background: transparent !important; color: #000 !important; }
    /* border-color too, since highlighted blocks keep their dark tokens */
    pre {
        border-color: #999;
        white-space: pre-wrap;   /* wrap instead of clipping */
        overflow: visible;
    }

    /* No page breaks right after headings or inside short blocks */
    h1, h2, h3 { break-after: avoid; }
    li, tr, blockquote { break-inside: avoid; }
}
