/* ============================================================
   Self-hosted variable fonts (latin subset)
   ============================================================ */

/* Inter — headings */
@font-face {
    font-family: "Inter";
    font-style: normal;
    font-weight: 100 900;
    font-display: swap;
    src: url("/fonts/inter-var.woff2") format("woff2");
}

/* Literata — body text (with optical sizing) */
@font-face {
    font-family: "Literata";
    font-style: normal;
    font-weight: 200 900;
    font-display: swap;
    src: url("/fonts/literata-var.woff2") format("woff2");
}
@font-face {
    font-family: "Literata";
    font-style: italic;
    font-weight: 200 900;
    font-display: swap;
    src: url("/fonts/literata-italic-var.woff2") format("woff2");
}

/* JetBrains Mono — code */
@font-face {
    font-family: "JetBrains Mono";
    font-style: normal;
    font-weight: 100 800;
    font-display: swap;
    src: url("/fonts/jetbrains-mono-var.woff2") format("woff2");
}

/* ============================================================
   Palette — derived from storm.webp via matugen
   Converted to OKLCH for perceptual uniformity, better gradients,
   and relative color manipulation.

   Modern CSS: logical properties, oklch(), oklab gradient
   interpolation, relative color syntax.
   ============================================================ */

:root {
    /* Surfaces */
    --bg: oklch(18.8% 0.0103 276.4);
    --bg-surface: oklch(24.1% 0.0115 278);
    --bg-raised: oklch(28.6% 0.0092 276.8);
    --bg-code: oklch(16.5% 0.0106 276.3);
    --bg-highest: oklch(32.7% 0.0105 285.8);

    /* Text */
    --text: oklch(91.4% 0.011 297.6);
    --text-secondary: oklch(83% 0.0138 286.1);
    --text-muted: oklch(65.6% 0.0148 281.4);
    --text-dim: oklch(39.7% 0.0151 280.6);
    --heading: oklch(91.4% 0.011 297.6);

    /* Primary — indigo-blue */
    --primary: oklch(83% 0.0847 273.4);
    --primary-dim: oklch(66.1% 0.1268 270.4);
    --primary-container: oklch(40.1% 0.0917 269.3);
    --on-primary-container: oklch(91.5% 0.0412 278.1);

    /* Tertiary — dusty mauve */
    --tertiary: oklch(83.5% 0.064 332.9);
    --tertiary-dim: oklch(66.4% 0.0604 332);
    --tertiary-container: oklch(40.4% 0.0578 331.3);

    /* Secondary — muted complement */
    --secondary: oklch(82.8% 0.0338 278.1);
    --secondary-dim: oklch(65.6% 0.0317 274.9);
    --secondary-container: oklch(39.7% 0.0328 273.3);

    /* Error */
    --error: oklch(83.8% 0.0891 26.8);

    /* Structural */
    --border: oklch(39.7% 0.0151 280.6);
    --border-subtle: oklch(31% 0.0146 274.4);
    --surface-bright: oklch(34.6% 0.0105 278.3);

    /* Type */
    --font-body: "Literata", "Georgia", serif;
    --font-head: "Inter", "Helvetica Neue", sans-serif;
    --font-mono: "JetBrains Mono", "Consolas", monospace;
    --font-size: 18px;
    --line-height: 1.72;
    --measure: 66ch;

    /* Layout */
    --content-width: 720px;
    --sidenote-width: 220px;
    --page-width: 1080px;
}

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

html {
    font-size: var(--font-size);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    margin: 0;
    padding: 0;
    font-family: var(--font-body);
    line-height: var(--line-height);
    color: var(--text);
    background-color: var(--bg);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.025'/%3E%3C/svg%3E");
}

/* ============================================================
   Header
   ============================================================ */

.site-header {
    border-block-end: 1px solid var(--border-subtle);
}

.site-header nav {
    max-inline-size: var(--page-width);
    margin-inline: auto;
    padding: 1.2rem 1.5rem;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 2rem;
}

.site-title {
    font-family: var(--font-head);
    font-weight: 600;
    font-size: 1.2rem;
    letter-spacing: -0.01em;
    color: var(--heading);
    text-decoration: none;
    transition: color 0.15s;
}

.site-title:hover {
    color: var(--primary);
}

.nav-links {
    display: flex;
    gap: 1.5rem;
}

.nav-links a {
    font-family: var(--font-head);
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-muted);
    text-decoration: none;
    letter-spacing: 0.01em;
    transition: color 0.15s;
    display: inline-flex;
    align-items: center;
}

.nav-links a:hover {
    color: var(--text-secondary);
}

/* ============================================================
   Main
   ============================================================ */

main {
    max-inline-size: var(--page-width);
    margin-inline: auto;
    padding: 2rem 1.5rem 4rem;
}

/* ============================================================
   Masthead — homepage image band
   ============================================================ */

.masthead {
    position: relative;
    inline-size: 100vw;
    max-block-size: 280px;
    overflow: hidden;
    margin-block-end: 2.5rem;
    margin-inline-start: calc(-50vw + 50%);
}

.masthead img {
    inline-size: 100%;
    block-size: 280px;
    object-fit: cover;
    object-position: center 90%;
    display: block;
}

/* Fade into background at bottom */
.masthead-fade {
    position: absolute;
    inset-block-end: 0;
    inset-inline: 0;
    block-size: 80px;
    background: linear-gradient(to bottom, transparent, var(--bg));
    pointer-events: none;
}

/* Subtle vignette at edges */
.masthead::before {
    content: "";
    position: absolute;
    inset: 0;
    background: radial-gradient(
        ellipse at center,
        transparent 50%,
        var(--bg) 100%
    );
    opacity: 0.4;
    pointer-events: none;
    z-index: 1;
}

@media (max-width: 600px) {
    .masthead {
        max-block-size: 180px;
    }
    .masthead img {
        block-size: 180px;
    }
    .masthead-fade {
        block-size: 50px;
    }
}

/* ============================================================
   Home
   ============================================================ */

.home-intro h1 {
    font-family: var(--font-body);
    font-weight: 400;
    font-size: 1.3rem;
    color: var(--text-secondary);
    line-height: 1.5;
    margin: 0 0 3rem;
    max-inline-size: var(--measure);
}

/* ============================================================
   Post list
   ============================================================ */

.post-list-page > h1 {
    font-family: var(--font-head);
    font-size: 1.6rem;
    font-weight: 600;
    color: var(--heading);
    margin-block-end: 2.5rem;
}

.post-preview {
    margin-block-end: 2.5rem;
    padding-block-end: 2.5rem;
    border-block-end: 1px solid var(--border-subtle);
}

.post-preview:last-child {
    border-block-end: none;
}

.post-preview time {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--text-dim);
    letter-spacing: 0.02em;
}

.post-preview h2 {
    font-family: var(--font-head);
    font-size: 1.35rem;
    font-weight: 600;
    line-height: 1.3;
    margin: 0.3rem 0 0;
    color: var(--heading);
}

.post-preview h2 a {
    color: inherit;
    text-decoration: none;
    transition: color 0.15s;
}

.post-preview h2 a:hover {
    color: var(--primary);
}

.post-preview .subtitle {
    font-style: italic;
    color: var(--text-secondary);
    margin: 0.3rem 0 0;
}

.post-preview .excerpt {
    color: var(--text-secondary);
    margin: 0.5rem 0 0;
    font-size: 0.95rem;
}

.tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-block-start: 0.6rem;
}

.tag {
    font-family: var(--font-mono);
    font-size: 0.72rem;
    color: var(--secondary);
    background: var(--secondary-container);
    padding: 0.15rem 0.5rem;
    border-radius: 3px;
    letter-spacing: 0.02em;
}

/* ============================================================
   Article header
   ============================================================ */

.post-header {
    margin-block-end: 3rem;
    padding-block-end: 2rem;
    border-block-end: 1px solid var(--border-subtle);
    max-inline-size: var(--content-width);
}

.post-header time {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    color: var(--text-muted);
    letter-spacing: 0.02em;
}

.post-header h1 {
    font-family: var(--font-head);
    font-size: 2.4rem;
    font-weight: 700;
    line-height: 1.15;
    color: var(--heading);
    margin: 0.5rem 0 0;
    letter-spacing: -0.025em;
}

.post-header .subtitle {
    font-family: var(--font-body);
    font-style: italic;
    font-size: 1.15rem;
    color: var(--text-secondary);
    margin: 0.6rem 0 0;
    line-height: 1.5;
}

/* ============================================================
   Post content
   ============================================================ */

.post-content {
    max-inline-size: var(--content-width);
    position: relative;
    counter-reset: sidenote-counter;
}

.post-content h2 {
    font-family: var(--font-head);
    font-size: 1.45rem;
    font-weight: 600;
    color: var(--heading);
    margin: 3rem 0 1rem;
    letter-spacing: -0.01em;
}

.post-content h3 {
    font-family: var(--font-head);
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--heading);
    margin: 2.5rem 0 0.8rem;
}

.post-content h4 {
    font-family: var(--font-head);
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--secondary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin: 2rem 0 0.6rem;
}

.post-content p {
    margin: 0 0 1.4rem;
}

.post-content a {
    color: var(--primary);
    text-decoration: underline;
    text-underline-offset: 2px;
    text-decoration-thickness: 1px;
    text-decoration-color: var(--primary-container);
    transition:
        text-decoration-color 0.15s,
        color 0.15s;
}

.post-content a:hover {
    color: var(--on-primary-container);
    text-decoration-color: var(--primary);
}

.post-content strong {
    color: var(--heading);
    font-weight: 600;
}

.post-content blockquote {
    margin: 1.8rem 0;
    padding: 0.2rem 1.5rem;
    border-inline-start: 3px solid var(--tertiary-container);
    color: var(--text-secondary);
    font-style: italic;
}

.post-content blockquote p:last-child {
    margin-block-end: 0;
}

.post-content ul,
.post-content ol {
    margin: 0 0 1.4rem;
    padding-inline-start: 1.5rem;
}

.post-content li {
    margin-block-end: 0.4rem;
}
.post-content li > ul,
.post-content li > ol {
    margin-block-end: 0;
}

.post-content hr {
    border: none;
    border-block-start: 1px solid var(--border-subtle);
    margin: 3rem 0;
}

.post-content img {
    max-inline-size: 100%;
    block-size: auto;
    border-radius: 4px;
    margin: 1.5rem 0;
}

.post-content figure {
    margin: 2rem 0;
    text-align: center;
}

.post-content figure > img {
    display: block;
    margin-inline: auto;
    margin-block: 0;
}

.post-content figcaption {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-align: center;
    margin-block-start: 0.5rem;
}

/* ============================================================
   Inline code
   ============================================================ */

.post-content code:not(pre code) {
    font-family: var(--font-mono);
    font-size: 0.84em;
    background: var(--bg-surface);
    padding: 0.15em 0.4em;
    border-radius: 3px;
    color: var(--on-primary-container);
    border: 1px solid var(--border-subtle);
}

/* ============================================================
   Code blocks
   ============================================================ */

.post-content .highlight {
    margin: 1.8rem 0 2.2rem;
    border-radius: 6px;
    overflow: hidden;
    background: var(--bg-code);
    border: 1px solid var(--border-subtle);
    position: relative;
}

/* Top stripe — interpolated in oklab for smooth transition */
.post-content .highlight::before {
    content: "";
    display: block;
    block-size: 2px;
    background: linear-gradient(
        90deg in oklab,
        var(--primary-container),
        var(--tertiary-container)
    );
    opacity: 0.5;
}

/* Language label */
.post-content .highlight code[data-lang]::before {
    content: attr(data-lang);
    display: block;
    font-family: var(--font-mono);
    font-size: 0.68rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    padding: 0.6rem 1.2rem 0;
    user-select: none;
}

.post-content .highlight pre {
    margin: 0;
    padding: 0.8rem 1.2rem 1.2rem;
    overflow-inline: auto;
    font-family: var(--font-mono);
    font-size: 0.82rem;
    line-height: 1.65;
    tab-size: 4;
    -webkit-overflow-scrolling: touch;
}

.post-content .highlight pre code {
    background: none;
    padding: 0;
    border-radius: 0;
    border: none;
    font-size: inherit;
}

.post-content .highlight table {
    inline-size: 100%;
    border-collapse: collapse;
}

.post-content .highlight .lnt {
    color: var(--text-dim);
    user-select: none;
    padding-inline-end: 1rem;
    text-align: end;
    inline-size: 1%;
    white-space: nowrap;
}

.post-content .highlight pre::-webkit-scrollbar {
    height: 5px;
}
.post-content .highlight pre::-webkit-scrollbar-track {
    background: transparent;
}
.post-content .highlight pre::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

/* ============================================================
   Sidenotes — Tufte style
   ============================================================ */

.sidenote-toggle {
    display: none;
}

.sidenote-number {
    font-size: 0.75em;
    position: relative;
    inset-block-start: -0.4em;
    color: var(--tertiary);
    font-family: var(--font-head);
    font-weight: 600;
}

.sidenote {
    font-size: 0.82rem;
    line-height: 1.5;
    color: var(--text-secondary);
}

.sidenote::before {
    content: attr(data-number) ". ";
    font-family: var(--font-head);
    font-weight: 600;
    color: var(--tertiary-dim);
    font-size: 0.78rem;
}

/* Wide: margin notes */
@media (min-width: 1100px) {
    .sidenote {
        float: inline-end;
        clear: inline-end;
        inline-size: var(--sidenote-width);
        margin-inline-end: calc(-1 * var(--sidenote-width) - 2rem);
        margin-block-end: 1rem;
        padding-inline-start: 1rem;
        border-inline-start: 1px solid var(--border-subtle);
    }

    .sidenote-number {
        cursor: default;
    }
}

/* Narrow: collapsible inline */
@media (max-width: 1099px) {
    .sidenote {
        display: none;
        background: var(--bg-raised);
        padding: 0.8rem 1rem;
        border-radius: 4px;
        margin: 0.5rem 0 1rem;
        border-inline-start: 2px solid var(--tertiary-container);
    }

    .sidenote-toggle:checked + .sidenote {
        display: block;
    }

    .sidenote-number {
        cursor: pointer;
    }
    .sidenote-number:hover {
        color: var(--tertiary-dim);
    }
}

/* ============================================================
   Tables
   ============================================================ */

.post-content table {
    inline-size: 100%;
    border-collapse: collapse;
    margin: 1.5rem 0 2rem;
    font-size: 0.9rem;
}

.post-content th,
.post-content td {
    padding: 0.6rem 1rem;
    text-align: start;
    border-block-end: 1px solid var(--border-subtle);
}

.post-content th {
    font-family: var(--font-head);
    font-weight: 600;
    color: var(--heading);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* ============================================================
   Footnotes
   ============================================================ */

.footnotes {
    margin-block-start: 3rem;
    padding-block-start: 1.5rem;
    border-block-start: 1px solid var(--border-subtle);
    font-size: 0.86rem;
    color: var(--text-secondary);
}

.footnotes ol {
    padding-inline-start: 1.5rem;
}

/* ============================================================
   Mermaid diagrams
   ============================================================ */

.mermaid {
    margin: 2rem 0;
    text-align: center;
    background: var(--bg-surface);
    border-radius: 6px;
    padding: 1.5rem;
    border: 1px solid var(--border-subtle);
}

.mermaid svg {
    max-inline-size: 100%;
    block-size: auto;
}

/* ============================================================
   Table of contents
   ============================================================ */

.toc {
    position: fixed;
    inset-inline-end: 2rem;
    inset-block-start: 8rem;
    inline-size: 180px;
    font-size: 0.76rem;
    display: none;
}

@media (min-width: 1400px) {
    .toc {
        display: block;
    }
}

.toc h3 {
    font-family: var(--font-head);
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-dim);
    margin: 0 0 0.8rem;
}

.toc ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.toc li {
    margin-block-end: 0.4rem;
}

.toc a {
    color: var(--text-muted);
    text-decoration: none;
    transition: color 0.15s;
}

.toc a:hover {
    color: var(--text-secondary);
}

/* ============================================================
   Footer
   ============================================================ */

.site-footer {
    border-block-start: 1px solid var(--border-subtle);
    margin-block-start: 4rem;
}

.footer-content {
    max-inline-size: var(--page-width);
    margin-inline: auto;
    padding: 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.footer-content p {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    color: var(--text-dim);
    margin: 0;
}

.social-links {
    display: flex;
    gap: 1rem;
}

.social-links a {
    color: var(--text-dim);
    transition: color 0.2s;
    display: inline-flex;
    align-items: center;
}

.social-links a:hover {
    color: var(--accent);
}

.social-links svg,
.nav-links svg {
    width: 1.1rem;
    height: 1.1rem;
    fill: currentColor;
}

/* ============================================================
   Raw / escape hatch pages
   ============================================================ */

.raw-content {
    max-inline-size: none;
}

/* ============================================================
   Selection — tertiary at 20% via relative color syntax
   ============================================================ */

::selection {
    background: oklch(from var(--tertiary) l c h / 20%);
    color: var(--heading);
}

/* ============================================================
   Responsive
   ============================================================ */

@media (max-width: 600px) {
    :root {
        --font-size: 16px;
    }

    .post-header h1 {
        font-size: 1.8rem;
    }
    .site-header nav {
        padding: 1rem;
        flex-wrap: wrap;
    }
    main {
        padding: 1.5rem 1rem 3rem;
    }

    .post-content .highlight pre {
        padding: 0.8rem;
        font-size: 0.76rem;
    }

    .post-content .highlight code[data-lang]::before {
        padding: 0.5rem 0.8rem 0;
    }
}

/* ============================================================
   Print
   ============================================================ */

@media print {
    body {
        background: #fff;
        color: #000;
    }
    .site-header,
    .site-footer,
    .toc {
        display: none;
    }

    .post-content a::after {
        content: " (" attr(href) ")";
        font-size: 0.8em;
        color: #666;
    }

    .sidenote {
        display: block !important;
        float: none !important;
        inline-size: auto !important;
        margin: 0.5rem 0 !important;
        padding: 0.5rem 1rem !important;
        background: #f5f5f5 !important;
        border-inline-start: 2px solid #999 !important;
        color: #333 !important;
    }
}
