@property --scroll-hue {
    syntax: '<number>';
    initial-value: 55;
    inherits: true;
}

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

:root {
    --hue: 55;

    /* --- Light --- */
    --ground-l: oklch(0.96 0.012 var(--hue));
    --text-l: oklch(0.15 0.02 var(--hue));
    --text-2-l: oklch(0.45 0.025 var(--hue));
    --rule-l: oklch(0.84 0.02 var(--hue));
    --gold-l: oklch(0.48 0.16 var(--scroll-hue));
    --gold-bright-l: oklch(0.40 0.18 var(--scroll-hue));
    --glow-l: oklch(0.91 0.05 var(--scroll-hue) / 0.2);

    /* --- Dark --- */
    --ground-d: oklch(0.16 0.025 var(--hue));
    --text-d: oklch(0.82 0.02 var(--hue));
    --text-2-d: oklch(0.50 0.025 var(--hue));
    --rule-d: oklch(0.28 0.02 var(--hue));
    --gold-d: oklch(0.65 0.17 var(--scroll-hue));
    --gold-bright-d: oklch(0.75 0.19 var(--scroll-hue));
    --glow-d: oklch(0.20 0.04 var(--scroll-hue) / 0.4);

    /* --- Active palette: light by default --- */
    --ground: var(--ground-l);
    --text: var(--text-l);
    --text-2: var(--text-2-l);
    --rule: var(--rule-l);
    --gold: var(--gold-l);
    --gold-bright: var(--gold-bright-l);
    --glow: var(--glow-l);

    --font-serif: 'Source Serif 4', Georgia, serif;
    --font-mono: 'DM Mono', Menlo, monospace;

    --measure: 60ch;

    --fs-name: clamp(3.5rem, 2.5rem + 5vw, 8rem);
    --fs-project: clamp(1.4rem, 1.1rem + 1vw, 2rem);
    --fs-body: clamp(0.9rem, 0.85rem + 0.2vw, 1rem);
    --fs-label: clamp(0.68rem, 0.64rem + 0.15vw, 0.76rem);

    --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
    --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
    --duration: 0.35s;

    color-scheme: light;
}

/* --- System prefers dark --- */
@media (prefers-color-scheme: dark) {
    :root {
        --ground: var(--ground-d);
        --text: var(--text-d);
        --text-2: var(--text-2-d);
        --rule: var(--rule-d);
        --gold: var(--gold-d);
        --gold-bright: var(--gold-bright-d);
        --glow: var(--glow-d);
        color-scheme: dark;
    }
}

/* --- Toggle flips from system default --- */
:root:has(#t:checked) {
    --ground: var(--ground-d);
    --text: var(--text-d);
    --text-2: var(--text-2-d);
    --rule: var(--rule-d);
    --gold: var(--gold-d);
    --gold-bright: var(--gold-bright-d);
    --glow: var(--glow-d);
    color-scheme: dark;
}

/* --- Toggle + dark system: flip back to light --- */
@media (prefers-color-scheme: dark) {
    :root:has(#t:checked) {
        --ground: var(--ground-l);
        --text: var(--text-l);
        --text-2: var(--text-2-l);
        --rule: var(--rule-l);
        --gold: var(--gold-l);
        --gold-bright: var(--gold-bright-l);
        --glow: var(--glow-l);
        color-scheme: light;
    }
}

::selection {
    background: var(--gold);
    color: var(--ground);
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-serif);
    font-optical-sizing: auto;
    color: var(--text);
    background: var(--ground);
    line-height: 1.6;
    display: grid;
    grid-template-columns:
        minmax(1.5rem, 1fr)
        min(var(--measure), 100% - 3rem)
        minmax(1.5rem, 1.8fr);
    grid-template-rows: auto 1fr auto;
    min-height: 100dvh;
    animation: hue-drift linear;
    animation-timeline: scroll();
    transition: background var(--duration) var(--ease);

    > * {
        grid-column: 2;
    }
}

body::after {
    content: '';
    position: fixed;
    inset: 0;
    background: radial-gradient(
        ellipse 50% 70% at 5% 40%,
        var(--glow),
        transparent
    );
    pointer-events: none;
    z-index: -1;
}

@keyframes hue-drift {
    from { --scroll-hue: 45; }
    to { --scroll-hue: 70; }
}

/* --- Controls --- */

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

.controls {
    position: fixed;
    top: 1.5rem;
    right: clamp(1.5rem, 4vw, 4rem);
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.toggle {
    width: 0.85rem;
    height: 0.85rem;
    border-radius: 50%;
    border: 1.5px solid var(--text-2);
    background: transparent;
    cursor: pointer;
    transition:
        border-color var(--duration) var(--ease),
        background var(--duration) var(--ease);
}

.toggle:hover {
    border-color: var(--gold);
}

:root:has(#t:focus-visible) .toggle {
    outline: 1px solid var(--gold);
    outline-offset: 3px;
}

.controls input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 4rem;
    height: 1px;
    background: var(--rule);
    outline: none;
    cursor: pointer;

    &::-webkit-slider-thumb {
        -webkit-appearance: none;
        width: 0.5rem;
        height: 0.5rem;
        border-radius: 50%;
        background: var(--gold);
        border: none;
    }

    &::-moz-range-thumb {
        width: 0.5rem;
        height: 0.5rem;
        border-radius: 50%;
        background: var(--gold);
        border: none;
    }
}

.cycle {
    position: absolute;
    top: 0;
    right: 0;
}

.rotate {
    display: none;
    font-family: var(--font-mono);
    font-size: var(--fs-body);
    color: var(--text-2);
    cursor: pointer;
    line-height: 1;
    user-select: none;
    letter-spacing: 0.06em;
    transition: color var(--duration) var(--ease);
}

.rotate:hover {
    color: var(--gold);
}

/* cycle: each state reveals only the label that advances to the next */
:root:has(#r1:checked) label[for="r2"].rotate { display: inline; }
:root:has(#r2:checked) label[for="r3"].rotate { display: inline; }
:root:has(#r3:checked) label[for="r1"].rotate { display: inline; }

:root:has([name="order"]:focus-visible) .rotate {
    outline: 1px solid var(--gold);
    outline-offset: 3px;
}

/* --- Header --- */

header {
    padding-block: clamp(4rem, 14vh, 12rem) clamp(1.9rem, 5.1vh, 4.5rem);
    position: relative;

    & h1 {
        font-family: var(--font-serif);
        font-size: var(--fs-name);
        font-weight: 700;
        line-height: 0.9;
        letter-spacing: -0.03em;
        color: var(--text);
    }

    & p {
        font-family: var(--font-mono);
        font-size: var(--fs-label);
        font-weight: 300;
        color: var(--text-2);
        letter-spacing: 0.06em;
    }

    & p:first-of-type {
        margin-top: 1rem;
        font-size: var(--fs-body);
        letter-spacing: 0.02em;
    }

    & p:last-of-type {
        margin-top: 0.4rem;
    }
}

header::before {
    content: '';
    position: absolute;
    top: clamp(2rem, 6vh, 5rem);
    right: -10%;
    width: clamp(100px, 25vw, 280px);
    height: clamp(100px, 25vw, 280px);
    border: 2px solid var(--rule);
    border-radius: 50%;
    opacity: 0.5;
    pointer-events: none;
}

/* --- Main + section ordering --- */

main {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: clamp(2rem, 5vh, 4rem);
}

main > br {
    display: none;
}

/* default: warez → wordz → wavez */
section[aria-label="Projects"] { order: 1; }
section[aria-label="Writings"] { order: 2; }
section[aria-label="Music"]    { order: 3; }

/* r2: wordz → wavez → warez */
:root:has(#r2:checked) section[aria-label="Writings"] { order: 1; }
:root:has(#r2:checked) section[aria-label="Music"]    { order: 2; }
:root:has(#r2:checked) section[aria-label="Projects"] { order: 3; }

/* r3: wavez → warez → wordz */
:root:has(#r3:checked) section[aria-label="Music"]    { order: 1; }
:root:has(#r3:checked) section[aria-label="Projects"] { order: 2; }
:root:has(#r3:checked) section[aria-label="Writings"] { order: 3; }

/* --- Section swap animation --- */

:root:has(#r1:checked) section { animation: shuffle-1 0.45s var(--ease-out); }
:root:has(#r2:checked) section { animation: shuffle-2 0.45s var(--ease-out); }
:root:has(#r3:checked) section { animation: shuffle-3 0.45s var(--ease-out); }

@keyframes shuffle-1 { from { opacity: 0; transform: translateY(1.2rem); } }
@keyframes shuffle-2 { from { opacity: 0; transform: translateY(1.2rem); } }
@keyframes shuffle-3 { from { opacity: 0; transform: translateY(1.2rem); } }

/* --- Sections --- */

section {
    counter-reset: projects;
    container-type: inline-size;
}

section h3 {
    font-family: var(--font-mono);
    font-size: var(--fs-label);
    font-weight: 400;
    color: var(--text-2);
    letter-spacing: 0.1em;
    padding-bottom: clamp(0.25rem, 0.5vh, 0.5rem);
}

/* --- Articles --- */

article {
    counter-increment: projects;
    padding-block: clamp(1.3rem, 2.5vh, 2rem);
    border-top: 1px solid var(--rule);
    position: relative;
    transition: opacity var(--duration) var(--ease);
    animation: enter linear both;
    animation-timeline: view();
    animation-range: entry 0% entry 22%;

    &::before {
        content: counter(projects, decimal-leading-zero);
        font-family: var(--font-mono);
        font-size: var(--fs-label);
        font-weight: 300;
        color: var(--text-2);
        font-variant-numeric: tabular-nums;
        display: block;
        margin-bottom: 0.35rem;
        transition: color var(--duration) var(--ease);
    }

    & h2 {
        font-family: var(--font-serif);
        font-size: var(--fs-project);
        font-weight: 700;
        line-height: 1.2;
        letter-spacing: -0.015em;
        margin-bottom: 0.25rem;

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

    & p {
        font-size: var(--fs-body);
        color: var(--text-2);
        max-width: 44ch;
        transition: color var(--duration) var(--ease);
    }

    /* the line */
    &::after {
        content: '';
        position: absolute;
        left: 0;
        bottom: 0;
        width: 0;
        height: 1px;
        background: var(--gold);
        transition: width 0.5s var(--ease-out);
    }

    &:hover::after {
        width: 100%;
    }

    &:hover {
        & h2 a { color: var(--gold); }
        &::before { color: var(--gold); }
        & p { color: var(--text); }
    }
}

article:last-child {
    border-bottom: 1px solid var(--rule);
}

/* :has() — the rest recedes */
section:has(article:is(:hover, :focus-within)) article:not(:hover, :focus-within) {
    opacity: 0.2;
}

@keyframes enter {
    from {
        opacity: 0;
        transform: translateY(0.8rem) translateX(-0.4rem);
    }
    to {
        opacity: 1;
        transform: translateY(0) translateX(0);
    }
}

/* container query — counter beside title when there's room */
@container (min-width: 38ch) {
    article {
        display: grid;
        grid-template-columns: 3.5ch 1fr;
        column-gap: clamp(0.75rem, 2vw, 1.5rem);
        align-items: baseline;

        &::before {
            grid-column: 1;
            grid-row: 1;
            margin-bottom: 0;
        }

        & h2 {
            grid-column: 2;
            grid-row: 1;
        }

        & p {
            grid-column: 2;
            grid-row: 2;
        }
    }
}

/* --- Footer --- */

footer {
    padding-block: clamp(4rem, 10vh, 8rem) clamp(2rem, 5vh, 3rem);

    & p {
        font-family: var(--font-mono);
        font-size: var(--fs-label);
        font-weight: 300;
        color: var(--text-2);
        letter-spacing: 0.06em;
        opacity: 0.5;
    }
}

/* --- Focus --- */

a:focus-visible {
    outline: 1px solid var(--gold);
    outline-offset: 4px;
}

/* --- Reduced motion --- */

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
    body { --scroll-hue: 58; }
}

/* --- Print --- */

@media print {
    body {
        display: block;
        max-width: var(--measure);
        margin-inline: auto;
        padding-inline: 2rem;
        background: white;
        color: black;
    }
    article { animation: none; break-inside: avoid; }
    header::before, body::after, .controls, .cycle { display: none; }
}

/* --- Breadcrumb --- */

.breadcrumb {
    font-family: var(--font-mono);
    font-size: var(--fs-label);
    font-weight: 300;
    color: var(--text-2);
    letter-spacing: 0.06em;
    padding-top: clamp(2rem, 6vh, 4rem);

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

    & a:hover {
        color: var(--gold);
    }

    & span {
        color: var(--text);
    }
}

/* --- Prose (article detail pages) --- */

article.prose {
    padding-block: clamp(2rem, 5vh, 4rem) clamp(4rem, 10vh, 8rem);

    & > header {
        padding-block: 0 clamp(1.5rem, 3vh, 2.5rem);
        position: relative;
    }

    & > header::before {
        display: none;
    }

    & > header h1 {
        font-family: var(--font-serif);
        font-size: var(--fs-name);
        font-weight: 700;
        line-height: 0.95;
        letter-spacing: -0.03em;
    }

    & > header time {
        display: block;
        margin-top: 0.75rem;
        font-family: var(--font-mono);
        font-size: var(--fs-label);
        font-weight: 300;
        color: var(--text-2);
        letter-spacing: 0.06em;
    }

    & > p {
        font-size: clamp(1rem, 0.95rem + 0.25vw, 1.15rem);
        line-height: 1.7;
        color: var(--text);
        margin-block: 1.2em;
        max-width: 65ch;
    }

    & > h2 {
        font-family: var(--font-serif);
        font-size: var(--fs-project);
        font-weight: 700;
        margin-top: 2.5em;
        margin-bottom: 0.5em;
    }

    & > h3 {
        font-family: var(--font-serif);
        font-size: clamp(1.1rem, 1rem + 0.3vw, 1.3rem);
        font-weight: 700;
        margin-top: 2em;
        margin-bottom: 0.4em;
    }

    & > blockquote {
        border-left: 2px solid var(--rule);
        padding-left: 1.25rem;
        margin-block: 1.5em;
        color: var(--text-2);
        font-style: italic;
    }

    & > pre {
        font-family: var(--font-mono);
        font-size: var(--fs-label);
        background: color-mix(in oklch, var(--ground), var(--text) 6%);
        padding: 1.25rem 1.5rem;
        border-radius: 4px;
        overflow-x: auto;
        margin-block: 1.5em;
        line-height: 1.7;
    }

    & code {
        font-family: var(--font-mono);
        font-size: 0.88em;
    }

    & > p code {
        background: color-mix(in oklch, var(--ground), var(--text) 6%);
        padding: 0.15em 0.35em;
        border-radius: 3px;
    }

    & a {
        color: var(--gold);
        text-decoration-thickness: 1px;
        text-underline-offset: 0.15em;
        transition: color var(--duration) var(--ease);
    }

    & a:hover {
        color: var(--gold-bright);
    }

    & > ul, & > ol {
        padding-left: 1.5em;
        margin-block: 1.2em;
        max-width: 65ch;
    }

    & li {
        font-size: clamp(1rem, 0.95rem + 0.25vw, 1.15rem);
        line-height: 1.7;
        margin-block: 0.3em;
    }

    & > hr {
        border: none;
        border-top: 1px solid var(--rule);
        margin-block: 2.5em;
    }
}

/* --- Waveform player --- */

article.prose .player {
    max-width: 500px;
    margin-top: 2rem;
}

article.prose .player-wave {
    cursor: pointer;
}

article.prose .player-wave svg {
    display: block;
    width: 100%;
    height: auto;
}

.waveform-progress {
    clip-path: inset(0 100% 0 0);
}

article.prose .player-controls {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    font-family: var(--font-mono);
    font-size: var(--fs-label);
    color: var(--text-2);
    margin-top: 0.75rem;
    letter-spacing: 0.06em;
}

article.prose .player-play {
    -webkit-appearance: none;
    appearance: none;
    background: none;
    border: none;
    font: inherit;
    color: inherit;
    cursor: pointer;
    padding: 0;
    margin: 0;
    transition: color var(--duration) var(--ease);
}

article.prose .player-play:hover {
    color: var(--gold);
}

article.prose .player-time {
    font-variant-numeric: tabular-nums;
}

article.prose .player-dl {
    color: inherit;
    text-decoration: none;
    transition: color var(--duration) var(--ease);
}

article.prose .player-dl:hover {
    color: var(--gold);
}

/* Fallback audio element (no waveform) */
article.prose audio {
    display: block;
    width: 100%;
    max-width: 500px;
    margin-top: 2rem;
    border-radius: 4px;
    outline: none;
}

article.prose audio:focus-visible {
    outline: 1px solid var(--gold);
    outline-offset: 3px;
}
