/* ==========================================================================
   Grayscale theme tokens (e-ink friendly: only shades of gray, inverted in
   dark mode).
   ========================================================================== */

:root {
    --gray-050: #f5f5f5;
    --gray-100: #ebebeb;
    --gray-200: #d9d9d9;
    --gray-400: #9e9e9e;
    --gray-600: #616161;
    --gray-800: #2b2b2b;
    --gray-900: #141414;

    /* Page (light) */
    color-scheme: light;
    --bg: #ffffff;
    --fg: var(--gray-900);

    /* Callout box surfaces (neutral default / fallback) */
    --box-bg: var(--gray-050);
    --box-border: var(--gray-200);
    --box-accent: var(--gray-600);
    --box-icon: var(--gray-800);
    --box-stripe: rgba(0, 0, 0, 0.06);

    /* Soft, low-saturation type tints (light) */
    --info-bg: #eef3fa;
    --info-border: #d3e0f2;
    --info-accent: #5a83bd;
    --info-icon: #35619c;

    --warning-bg: #faeeee;
    --warning-border: #f0d4d4;
    --warning-accent: #bd5a5a;
    --warning-icon: #9c3535;

    --tip-bg: #eef6f0;
    --tip-border: #d3e8da;
    --tip-accent: #5a9e73;
    --tip-icon: #357d4f;

    /* Code surfaces */
    --code-bg: var(--gray-100);
    --code-fg: var(--gray-900);
    --code-muted: var(--gray-600);
    --code-border: var(--gray-200);

    /* Sticky nav (light) */
    --nav-bg: rgba(255, 255, 255, 0.82);
    --nav-border: var(--gray-200);

    /* Table surfaces (light) */
    --table-border: var(--gray-200);
    --table-head-bg: var(--gray-100);
    --table-head-border: var(--gray-400);
    --table-stripe: rgba(0, 0, 0, 0.03);

    /* Minimal syntax palette (light) */
    --syn-keyword: #8a3ffc;
    --syn-string: #0f7b6c;
    --syn-number: #b45309;
    --syn-function: #1d63c9;
    --syn-comment: var(--code-muted);
}

:root[data-theme="dark"] {
        /* Page (dark) */
        color-scheme: dark;
        --bg: #121212;
        --fg: var(--gray-100);

        --box-bg: var(--gray-900);
        --box-border: var(--gray-600);
        --box-accent: var(--gray-400);
        --box-icon: var(--gray-100);
        --box-stripe: rgba(255, 255, 255, 0.06);

        /* Soft, low-saturation type tints (dark) */
        --info-bg: #16202c;
        --info-border: #2b3f56;
        --info-accent: #6f9ad6;
        --info-icon: #9cc0ef;

        --warning-bg: #2a1a1a;
        --warning-border: #533232;
        --warning-accent: #d68686;
        --warning-icon: #efb0b0;

        --tip-bg: #16241b;
        --tip-border: #2c4535;
        --tip-accent: #6fb98a;
        --tip-icon: #9cd6b0;

        --code-bg: var(--gray-900);
        --code-fg: var(--gray-100);
        --code-muted: var(--gray-400);
        --code-border: var(--gray-600);

        /* Sticky nav (dark) */
        --nav-bg: rgba(18, 18, 18, 0.82);
        --nav-border: var(--gray-600);

        /* Table surfaces (dark) */
        --table-border: var(--gray-600);
        --table-head-bg: var(--gray-800);
        --table-head-border: var(--gray-400);
        --table-stripe: rgba(255, 255, 255, 0.04);

        /* Minimal syntax palette (dark) — lighter, softer hues */
        --syn-keyword: #c9a6ff;
        --syn-string: #6ec7b6;
        --syn-number: #e0a066;
        --syn-function: #7fb0f0;
        --syn-comment: var(--code-muted);
}

/* Visually hidden but available to screen readers. */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ==========================================================================
   Base typography & layout
   ========================================================================== */

:root {
    /* Comfortable reading measure + a wider track for posts with a TOC. */
    --wrap: 50rem;
    --wrap-wide: 72rem;

    /* Height of the sticky nav, used to offset sticky content & anchors. */
    --nav-h: 3rem;
}

html {
    font-size: 118.75%; /* ~19px base */
    -webkit-text-size-adjust: 100%;
    /* Keep anchor targets clear of the sticky nav. */
    scroll-padding-top: calc(var(--nav-h) + 1rem);
}

body {
    margin: 0;
    font-family: Georgia, "Times New Roman", serif;
    line-height: 1.75;
    background: var(--bg);
    color: var(--fg);
}

/* Center every top-level region on a comfortable reading measure. */
.site-nav__inner,
main,
footer {
    max-width: var(--wrap);
    margin-inline: auto;
    padding-inline: 1.25rem;
    box-sizing: border-box;
}

/* Posts with a TOC need a wider track so the sidebar fits. */
body.layout-wide .site-nav__inner,
body.layout-wide main,
body.layout-wide footer {
    max-width: var(--wrap-wide);
}

main {
    padding-block: 2rem 3rem;
}

/* --- Sticky top navigation --- */
.site-nav {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--nav-bg);
    border-bottom: 1px solid var(--nav-border);
    backdrop-filter: saturate(180%) blur(8px);
    -webkit-backdrop-filter: saturate(180%) blur(8px);
}

.site-nav__inner {
    display: flex;
    align-items: center;
    gap: 1.25rem;
    height: var(--nav-h);
}

.site-nav__brand {
    font-weight: 700;
    font-size: 1rem;
    text-decoration: none;
}

.site-nav__menu {
    display: flex;
    gap: 1.25rem;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 0.9rem;
}

.site-nav__menu a {
    text-decoration: none;
    opacity: 0.7;
    padding-bottom: 0.15rem;
    border-bottom: 2px solid transparent;
}

.site-nav__menu a:hover {
    opacity: 1;
}

.site-nav__menu a.is-active {
    opacity: 1;
    border-bottom-color: currentColor;
}

/* Support / donate button */
.nav-support {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.3rem 0.75rem;
    font-size: 0.82rem;
    font-weight: 600;
    text-decoration: none;
    color: var(--bg);
    background: var(--fg);
    border-radius: 999px;
    opacity: 0.9;
}

.nav-support:hover {
    opacity: 1;
}

.nav-support svg {
    display: block;
}

/* Nav search trigger (looks like a small input) */
.nav-search {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    margin-left: auto; /* pushes search + toggle group to the right */
    padding: 0.28rem 0.55rem;
    color: inherit;
    background: transparent;
    border: 1px solid var(--nav-border);
    border-radius: 0.4rem;
    font: inherit;
    font-size: 0.82rem;
    cursor: pointer;
    opacity: 0.75;
}

.nav-search:hover {
    opacity: 1;
}

.nav-search__label {
    color: var(--code-muted);
}

kbd.nav-search__key,
kbd.search__esc {
    font-family: inherit;
    font-size: 0.72rem;
    line-height: 1;
    padding: 0.15rem 0.35rem;
    border: 1px solid var(--nav-border);
    border-radius: 0.3rem;
    opacity: 0.7;
}

/* Theme toggle button */
.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.3rem;
    color: inherit;
    background: transparent;
    border: 0;
    border-radius: 0.375rem;
    cursor: pointer;
    opacity: 0.7;
}

.theme-toggle:hover {
    opacity: 1;
}

.theme-toggle svg {
    display: block;
}

/* Show the moon in light mode, the sun in dark mode. */
.theme-toggle .icon-sun {
    display: none;
}

:root[data-theme="dark"] .theme-toggle .icon-sun {
    display: block;
}

:root[data-theme="dark"] .theme-toggle .icon-moon {
    display: none;
}

/* ==========================================================================
   Search modal (command-palette style)
   ========================================================================== */

.search[hidden] {
    display: none;
}

.search {
    position: fixed;
    inset: 0;
    z-index: 1000;
}

.search__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
}

.search__dialog {
    position: relative;
    max-width: 34rem;
    margin: 12vh auto 0;
    background: var(--bg);
    color: var(--fg);
    border: 1px solid var(--nav-border);
    border-radius: 0.6rem;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

.search__field {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--nav-border);
    color: var(--code-muted);
}

.search__input {
    flex: 1;
    min-width: 0;
    border: 0;
    background: transparent;
    color: var(--fg);
    font: inherit;
    font-size: 1.05rem;
    outline: none;
}

.search__results {
    list-style: none;
    margin: 0;
    padding: 0.4rem;
    max-height: min(60vh, 24rem);
    overflow-y: auto;
}

.search__result {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    padding: 0.55rem 0.7rem;
    border-radius: 0.4rem;
    cursor: pointer;
}

.search__result.is-active {
    background: rgba(127, 127, 127, 0.18);
}

.search__title {
    font-weight: 600;
}

.search__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    font-size: 0.78rem;
    color: var(--code-muted);
}

.search__empty {
    margin: 0;
    padding: 1.25rem;
    text-align: center;
    color: var(--code-muted);
}

footer {
    margin-top: 4rem;
    padding-block: 2.5rem 3rem;
    border-top: 1px solid var(--nav-border);
    font-size: 0.9rem;
}

.footer-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 2.5rem 3rem;
}

.footer-brand {
    flex: 2 1 16rem;
}

.footer-brand__name {
    font-weight: 700;
    font-size: 1.1rem;
    text-decoration: none;
}

.footer-brand__desc {
    margin: 0.6rem 0 0;
    max-width: 34ch;
    color: var(--code-muted);
    font-size: 0.85rem;
    line-height: 1.6;
}

.footer-col {
    flex: 1 1 8rem;
}

.footer-col__title {
    margin: 0 0 0.7rem;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--code-muted);
}

.footer-col ul {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
    margin: 0;
    padding: 0;
    list-style: none;
}

.footer-col a {
    text-decoration: none;
    opacity: 0.8;
}

.footer-col a:hover {
    opacity: 1;
    text-decoration: underline;
}

.footer-bottom {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 0.5rem;
    margin-top: 2.5rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--nav-border);
    color: var(--code-muted);
    font-size: 0.8rem;
}

.footer-bottom p {
    margin: 0;
}

h1, h2, h3, h4 {
    line-height: 1.25;
}

a {
    color: inherit;
}

img {
    max-width: 100%;
    height: auto;
}

/* Justify body copy like a printed book, with hyphenation to avoid rivers. */
.post-content p {
    text-align: justify;
    hyphens: auto;
    -webkit-hyphens: auto;
}

/* Center images and mermaid diagrams within the content. */
.post-content img {
    display: block;
    margin-inline: auto;
}

.post-content pre.mermaid {
    display: flex;
    justify-content: center;
    margin: 1.5rem 0;
    background: none;
    border: 0;
}

.post-content pre.mermaid svg {
    max-width: 100%;
    height: auto;
}

/* Don't justify things that shouldn't stretch. */
.post-content pre,
.post-content li,
.post-content table {
    text-align: left;
    hyphens: manual;
}

/* ==========================================================================
   Callout boxes (info / warning / tip) — grayscale with a type icon
   ========================================================================== */

.box {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.75rem;
    align-items: start;
    margin: 1.5rem 0;
    padding: 0.9rem 1.1rem;
    background-color: var(--box-bg);
    background-image: repeating-linear-gradient(
        45deg,
        var(--box-stripe) 0,
        var(--box-stripe) 1px,
        transparent 1px,
        transparent 8px
    );
    border: 1px solid var(--box-border);
    border-left: 3px solid var(--box-accent);
    border-radius: 0.375rem;
}

/* Per-type tints feed into the generic box variables. */
.box-info {
    --box-bg: var(--info-bg);
    --box-border: var(--info-border);
    --box-accent: var(--info-accent);
    --box-icon: var(--info-icon);
}

.box-warning {
    --box-bg: var(--warning-bg);
    --box-border: var(--warning-border);
    --box-accent: var(--warning-accent);
    --box-icon: var(--warning-icon);
}

.box-tip {
    --box-bg: var(--tip-bg);
    --box-border: var(--tip-border);
    --box-accent: var(--tip-accent);
    --box-icon: var(--tip-icon);
}

.box-icon {
    display: inline-flex;
    align-items: center;
    /* Match the body's line-height so the icon centers on the first text line. */
    height: 1.6em;
    color: var(--box-icon);
}

.box-icon svg {
    display: block;
}

.box-body {
    line-height: 1.6;
}

.box-body > :first-child {
    margin-top: 0;
}

.box-body > :last-child {
    margin-bottom: 0;
}

/* ==========================================================================
   Code blocks — monochrome (no color highlighting). Tokens inherit the text
   color; only weight/italic distinguish keywords and comments.
   ========================================================================== */

/* Fenced code block container. */
.highlight {
    margin: 1.5rem 0;
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    border-radius: 0.375rem;
    overflow-x: auto;
}

.highlight pre.chroma {
    margin: 0;
    padding: 0.9rem 1.1rem;
    background: transparent;
    color: var(--code-fg);
    font-family: ui-monospace, "SFMono-Regular", "Menlo", "Consolas", monospace;
    font-size: 0.95rem;
    line-height: 1.6;
}

.highlight .chroma {
    color: var(--code-fg);
    background: transparent;
}

/* Minimal, adaptive syntax colors. */

/* Keywords / tags. */
.chroma .k,
.chroma .kc, .chroma .kd, .chroma .kn, .chroma .kp, .chroma .kr, .chroma .kt,
.chroma .nt {
    color: var(--syn-keyword);
    font-weight: 600;
}

/* Function & builtin names. */
.chroma .nf, .chroma .nb, .chroma .nx {
    color: var(--syn-function);
}

/* Strings. */
.chroma .s, .chroma .sb, .chroma .sc, .chroma .sd, .chroma .s2, .chroma .se,
.chroma .sh, .chroma .si, .chroma .sx, .chroma .sr, .chroma .s1, .chroma .ss {
    color: var(--syn-string);
}

/* Numbers. */
.chroma .m, .chroma .mb, .chroma .mf, .chroma .mh, .chroma .mi, .chroma .mo,
.chroma .il {
    color: var(--syn-number);
}

/* Comments. */
.chroma .c, .chroma .ch, .chroma .cm, .chroma .c1, .chroma .cs,
.chroma .cp, .chroma .cpf {
    color: var(--syn-comment);
    font-style: italic;
}

/* Highlighted lines (hl_lines) — subtle grayscale band, no color. */
.chroma .hl {
    display: block;
    background: var(--box-stripe);
}

/* Inline code (not inside a highlighted block). */
:not(pre) > code {
    padding: 0.12em 0.35em;
    font-family: ui-monospace, "SFMono-Regular", "Menlo", "Consolas", monospace;
    font-size: 0.9em;
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    border-radius: 0.25rem;
}

/* ==========================================================================
   Tables — clean, professional, grayscale
   ========================================================================== */

/* Horizontal scroll wrapper so wide tables stay mobile-friendly. */
.table-wrap {
    margin: 1.5rem 0;
    overflow-x: auto;
    border: 1px solid var(--table-border);
    border-radius: 0.375rem;
}

.table-wrap table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
    line-height: 1.5;
}

.table-wrap th,
.table-wrap td {
    padding: 0.6rem 0.9rem;
    text-align: left;
    vertical-align: top;
    border-bottom: 1px solid var(--table-border);
}

.table-wrap thead th {
    background: var(--table-head-bg);
    font-weight: 600;
    border-bottom: 2px solid var(--table-head-border);
    white-space: nowrap;
}

/* Zebra striping for readability. */
.table-wrap tbody tr:nth-child(even) {
    background: var(--table-stripe);
}

/* Remove the trailing border so it doesn't double up with the wrapper. */
.table-wrap tbody tr:last-child td {
    border-bottom: 0;
}

/* ==========================================================================
   Post header (title / meta / tags)
   ========================================================================== */

.post-header {
    margin-bottom: 2.25rem;
}

/* Post title: larger than any heading in the body. */
.post-header h2 {
    margin: 0 0 0.6rem;
    font-size: 2.6rem;
    line-height: 1.12;
    letter-spacing: -0.01em;
}

.post-meta {
    margin: 0 0 0.7rem;
    font-size: 0.85rem;
    color: var(--code-muted);
}

/* Tags: inline, small, muted. */
.post-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem 0.75rem;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 0.78rem;
}

.post-tags a {
    color: var(--code-muted);
    text-decoration: none;
}

.post-tags a:hover {
    color: var(--fg);
    text-decoration: underline;
}

/* Body headings: clearly smaller than the post title. */
.post-content h2 {
    font-size: 1.55rem;
}

.post-content h3 {
    font-size: 1.25rem;
}

.post-content h4 {
    font-size: 1.05rem;
}

/* ==========================================================================
   Post layout: content with a right-hand table-of-contents sidebar
   ========================================================================== */

.post-layout.has-toc {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 16rem;
    gap: 3rem;
    align-items: start;
}

/* Keep prose comfortably readable within the content column. */
.post-layout .post {
    min-width: 0;
}

/* --- Table of contents sidebar --- */
.toc {
    position: sticky;
    top: calc(var(--nav-h) + 1.5rem);
    align-self: start;
    max-height: calc(100vh - var(--nav-h) - 3rem);
    overflow-y: auto;
    font-size: 0.9rem;
    line-height: 1.4;
}

.toc h3 {
    margin: 0 0 0.5rem;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    opacity: 0.6;
}

.toc #TableOfContents ul {
    list-style: none;
    margin: 0;
    padding-left: 1rem;
}

.toc #TableOfContents > ul {
    padding-left: 0;
}

.toc #TableOfContents li {
    margin: 0.2rem 0;
}

.toc a {
    text-decoration: none;
    color: inherit;
    opacity: 0.8;
}

.toc a:hover {
    opacity: 1;
    text-decoration: underline;
}

/* Hide the TOC sidebar on small / mobile screens. */
@media (max-width: 900px) {
    .post-layout.has-toc {
        display: block;
    }

    .toc {
        display: none;
    }
}

/* Phone refinements: slightly smaller type and tighter gutters. */
@media (max-width: 600px) {
    html {
        font-size: 112.5%; /* ~18px */
    }

    .site-nav__inner,
    main,
    footer {
        padding-inline: 1rem;
    }

    .box,
    .highlight pre.chroma {
        padding: 0.8rem 0.9rem;
    }

    .post-header h2 {
        font-size: 2rem;
    }

    /* Icon-only search trigger to save space. */
    .nav-search__label,
    .nav-search__key {
        display: none;
    }

    .search__dialog {
        margin-top: 8vh;
        max-width: none;
        margin-inline: 1rem;
    }
}
