/* ==========================================================================
   Foundations of Large Language Models — stylesheet
   Design: a printed technical monograph. macOS-native serif body (Charter /
   Iowan) paired with the system sans for structure, an ink-blue accent for
   links and section numbers, and a warm amber reserved for interview notes.
   Fully offline: no web fonts, no external CSS.
   ========================================================================== */

:root {
    --paper: #fbfbf9;
    --sidebar-bg: #f4f3ee;
    --ink: #17181b;
    --ink-soft: #3b3d42;
    --muted: #6a6d73;
    --rule: #e4e3dd;
    --rule-strong: #cfcdc4;
    --accent: #274b6d;        /* Prussian ink blue for links and section numbers. */
    --accent-soft: #eaf0f6;
    --amber: #9c6b12;         /* Reserved for interview callouts. */

    /* Quiz feedback. --quiz-incorrect matches the figures' BRICK and the
       warning callout; keep the three in sync if you retune the palette. */
    --quiz-correct: #3f7d5a;
    --quiz-correct-soft: #eef5f0;
    --quiz-incorrect: #b04a3f;
    --quiz-incorrect-soft: #fbf1ef;

    --serif: "Charter", "Iowan Old Style", "Palatino Linotype", "Palatino",
        Georgia, "Times New Roman", serif;
    --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue",
        system-ui, sans-serif;
    --mono: "SF Mono", "SFMono-Regular", ui-monospace, "Menlo", "Consolas",
        monospace;

    --content-width: 43rem;
    --sidebar-width: 19rem;
    --rail-width: 13rem;
}

* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    background: var(--paper);
    color: var(--ink);
    font-family: var(--serif);
    font-size: 1.0625rem;
    line-height: 1.62;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
}

/* ---- Layout scaffold --------------------------------------------------- */

.layout {
    display: grid;
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
    align-items: start;
}

.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--rule);
    padding: 1.6rem 1.25rem 3rem 1.6rem;
    font-family: var(--sans);
}

.main {
    min-width: 0;
    display: flex;
    justify-content: center;
    padding: 3.2rem 2.5rem 6rem 2.5rem;
}

.content-wrap {
    display: grid;
    grid-template-columns: minmax(0, var(--content-width)) var(--rail-width);
    column-gap: 2.75rem;
    width: 100%;
    max-width: calc(var(--content-width) + var(--rail-width) + 2.75rem);
}

article {
    min-width: 0;
    grid-column: 1;
}

/* ---- Sidebar table of contents ---------------------------------------- */

/* A link back to the wider site, rendered only when SITE_URL is configured. */
.site-backlink {
    display: block;
    font-family: var(--sans);
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.01em;
    color: var(--muted);
    text-decoration: none;
    margin-bottom: 1rem;
}

.site-backlink:hover {
    color: var(--accent);
}

.book-brand {
    display: block;
    font-weight: 650;
    font-size: 1rem;
    line-height: 1.35;
    letter-spacing: -0.01em;
    color: var(--ink);
    text-decoration: none;
    margin-bottom: 1.4rem;
    padding-bottom: 1.2rem;
    border-bottom: 1px solid var(--rule-strong);
}

.book-brand span {
    display: block;
    font-weight: 400;
    font-size: 0.78rem;
    color: var(--muted);
    margin-top: 0.35rem;
    letter-spacing: 0.01em;
}

.toc-part {
    margin-top: 1.5rem;
    margin-bottom: 0.5rem;
    font-size: 0.72rem;
    font-weight: 650;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
}

.toc-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.toc-list li {
    margin: 0;
}

.toc-list a {
    display: flex;
    gap: 0.55rem;
    padding: 0.28rem 0.5rem 0.28rem 0.5rem;
    border-radius: 5px;
    font-size: 0.86rem;
    line-height: 1.3;
    color: var(--ink-soft);
    text-decoration: none;
}

.toc-list a:hover {
    background: #ecebe4;
    color: var(--ink);
}

.toc-list a.active {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 600;
}

.toc-num {
    flex: 0 0 auto;
    min-width: 1.15rem;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.toc-list a.active .toc-num {
    color: var(--accent);
}

/* ---- Chapter header ---------------------------------------------------- */

.chapter-eyebrow {
    font-family: var(--sans);
    font-size: 0.76rem;
    font-weight: 650;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--accent);
    margin-bottom: 0.7rem;
}

h1.chapter-title {
    font-family: var(--sans);
    font-weight: 680;
    font-size: 2.15rem;
    line-height: 1.12;
    letter-spacing: -0.02em;
    margin: 0 0 1.6rem 0;
    color: var(--ink);
}

h1.chapter-title .chapter-number {
    color: var(--rule-strong);
    font-weight: 680;
    margin-right: 0.6rem;
}

/* ---- Body typography --------------------------------------------------- */

article h2 {
    font-family: var(--sans);
    font-weight: 650;
    font-size: 1.4rem;
    line-height: 1.2;
    letter-spacing: -0.01em;
    margin: 2.6rem 0 0.9rem 0;
    padding-top: 0.6rem;
    color: var(--ink);
}

article h3 {
    font-family: var(--sans);
    font-weight: 620;
    font-size: 1.1rem;
    margin: 1.9rem 0 0.7rem 0;
    color: var(--ink);
}

.heading-number {
    color: var(--accent);
    font-variant-numeric: tabular-nums;
    margin-right: 0.5rem;
    font-weight: 600;
}

article p {
    margin: 0 0 1.1rem 0;
}

article a {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid var(--rule-strong);
}

article a:hover {
    border-bottom-color: var(--accent);
}

article ul,
article ol {
    margin: 0 0 1.1rem 0;
    padding-left: 1.4rem;
}

article li {
    margin-bottom: 0.4rem;
}

strong {
    font-weight: 650;
}

blockquote {
    margin: 1.2rem 0;
    padding: 0.2rem 0 0.2rem 1.2rem;
    border-left: 3px solid var(--rule-strong);
    color: var(--ink-soft);
    font-style: italic;
}

/* ---- Code -------------------------------------------------------------- */

code {
    font-family: var(--mono);
    font-size: 0.85em;
    background: #f0efe9;
    padding: 0.12em 0.34em;
    border-radius: 4px;
}

pre {
    background: #1c2733;
    color: #e6edf3;
    border-radius: 8px;
    padding: 1rem 1.15rem;
    overflow-x: auto;
    font-size: 0.83rem;
    line-height: 1.55;
    margin: 1.3rem 0;
}

pre code {
    background: none;
    padding: 0;
    font-size: inherit;
    color: inherit;
}

/* ---- Callout boxes ----------------------------------------------------- */

.admonition {
    margin: 1.5rem 0;
    padding: 0.9rem 1.15rem 0.95rem 1.15rem;
    border-radius: 8px;
    border: 1px solid var(--rule);
    border-left-width: 4px;
    background: #ffffff;
    font-size: 0.98rem;
}

.admonition > .admonition-title {
    font-family: var(--sans);
    font-weight: 650;
    font-size: 0.72rem;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    margin-bottom: 0.45rem;
}

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

.admonition.intuition {
    border-left-color: var(--accent);
    background: #f7fafc;
}
.admonition.intuition > .admonition-title {
    color: var(--accent);
}

.admonition.analogy {
    border-left-color: #6b4f9c;
    background: #f8f6fc;
}
.admonition.analogy > .admonition-title {
    color: #6b4f9c;
}

.admonition.interview {
    border-left-color: var(--amber);
    background: #fdf9f0;
}
.admonition.interview > .admonition-title {
    color: var(--amber);
}

.admonition.note {
    border-left-color: var(--rule-strong);
    background: #faf9f5;
}
.admonition.note > .admonition-title {
    color: var(--muted);
}

.admonition.warning {
    border-left-color: #b04a3f;
    background: #fdf4f2;
}
.admonition.warning > .admonition-title {
    color: #b04a3f;
}

.admonition.figure {
    border: 1px dashed var(--rule-strong);
    border-left-width: 1px;
    background: #faf9f5;
    text-align: center;
    color: var(--muted);
    font-family: var(--sans);
    font-size: 0.88rem;
}
.admonition.figure > .admonition-title {
    color: var(--ink-soft);
    text-transform: none;
    letter-spacing: 0;
    font-size: 0.82rem;
}

/* ---- Figures ----------------------------------------------------------- */

article figure {
    margin: 2rem 0;
    padding: 0;
}

article figure img,
article figure svg {
    display: block;
    width: 100%;
    height: auto;
    max-width: 100%;
}

article figcaption {
    margin-top: 0.7rem;
    padding-top: 0.55rem;
    border-top: 1px solid var(--rule);
    font-family: var(--sans);
    font-size: 0.83rem;
    line-height: 1.45;
    color: var(--muted);
}

.fig-label {
    display: inline;
    font-weight: 650;
    color: var(--accent);
    margin-right: 0.45rem;
}

/* A figure that needs room may bleed into the rail's gutter on wide screens. */
article figure.wide {
    width: calc(100% + var(--rail-width));
    max-width: calc(100% + var(--rail-width));
}

@media (max-width: 60rem) {
    article figure.wide {
        width: 100%;
        max-width: 100%;
    }
}

/* ---- Tables ------------------------------------------------------------ */

/* A table wider than the column scrolls inside this box rather than pushing the
   whole page sideways on a phone. */
.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 1.4rem 0;
}

article table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
    font-size: 0.92rem;
    font-family: var(--sans);
}

article th,
article td {
    text-align: left;
    padding: 0.5rem 0.7rem;
    border-bottom: 1px solid var(--rule);
}

article th {
    border-bottom: 2px solid var(--rule-strong);
    font-weight: 650;
}

/* ---- Citations and references ------------------------------------------ */

/* In-text author-year citations, e.g. (Vaswani et al., 2017). The link is
   quieter than a body link so citations do not stripe the paragraph. */
.citation a {
    border-bottom: none;
    color: var(--accent);
}

.citation a:hover {
    border-bottom: 1px solid var(--accent);
}

.references {
    margin-top: 3rem;
    padding-top: 1.4rem;
    border-top: 1px solid var(--rule);
}

.references h2 {
    font-size: 1.05rem;
}

.reference-list {
    list-style: none;
    padding-left: 0;
    font-size: 0.88rem;
    color: var(--ink-soft);
}

.reference-list li {
    /* Hanging indent, so multi-line entries read as one unit. */
    padding-left: 1.6rem;
    text-indent: -1.6rem;
    margin-bottom: 0.55rem;
    scroll-margin-top: 1.5rem;
}

/* Briefly spotlight the entry a citation just jumped to. */
.reference-list li:target {
    background: var(--accent-soft);
}

/* ---- Check-yourself quiz ----------------------------------------------- */

.quiz {
    margin-top: 3rem;
    padding-top: 1.4rem;
    border-top: 1px solid var(--rule);
}

.quiz h2 {
    font-size: 1.05rem;
}

.quiz-intro {
    font-family: var(--sans);
    font-size: 0.9rem;
    color: var(--muted);
    margin: 0 0 1.4rem 0;
}

.quiz-list {
    list-style: none;
    padding-left: 0;
    counter-reset: quiz;
}

.quiz-q {
    margin-bottom: 1.8rem;
}

.quiz-prompt {
    font-weight: 600;
    margin: 0 0 0.7rem 0;
}

.quiz-prompt::before {
    counter-increment: quiz;
    content: "Q" counter(quiz) ". ";
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

.quiz-options {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.quiz-option {
    text-align: left;
    width: 100%;
    padding: 0.6rem 0.85rem;
    font-family: var(--sans);
    font-size: 0.92rem;
    color: var(--ink);
    background: #fff;
    border: 1px solid var(--rule-strong);
    border-radius: 8px;
    cursor: pointer;
    transition: border-color 0.12s ease, background 0.12s ease;
}

.quiz-option:hover:not(:disabled) {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.quiz-option:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.quiz-option:disabled {
    cursor: default;
}

.quiz-option.is-correct {
    border-color: var(--quiz-correct);
    background: var(--quiz-correct-soft);
    color: var(--quiz-correct);
    font-weight: 600;
}

.quiz-option.is-incorrect {
    border-color: var(--quiz-incorrect);
    background: var(--quiz-incorrect-soft);
    color: var(--quiz-incorrect);
    font-weight: 600;
}

.quiz-explain {
    margin-top: 0.7rem;
    padding: 0.7rem 0.9rem;
    font-size: 0.9rem;
    color: var(--ink-soft);
    background: var(--accent-soft);
    border-left: 3px solid var(--accent);
    border-radius: 0 6px 6px 0;
}

/* ---- Stub banner ------------------------------------------------------- */

.stub-banner {
    margin: 0 0 1.8rem 0;
    padding: 0.7rem 1rem;
    border-radius: 8px;
    background: #fdf9f0;
    border: 1px solid #efe3c6;
    font-family: var(--sans);
    font-size: 0.86rem;
    color: var(--amber);
}

.stub-note {
    color: var(--muted);
    font-style: italic;
}

/* ---- On-this-page rail ------------------------------------------------- */

.rail {
    grid-column: 2;
    position: sticky;
    top: 3.2rem;
    align-self: start;
    font-family: var(--sans);
    font-size: 0.8rem;
    line-height: 1.4;
    border-left: 1px solid var(--rule);
    padding-left: 1.1rem;
    max-height: calc(100vh - 5rem);
    overflow-y: auto;
}

.rail-title {
    font-size: 0.68rem;
    font-weight: 650;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
    margin-bottom: 0.6rem;
}

.rail a {
    display: block;
    color: var(--ink-soft);
    text-decoration: none;
    padding: 0.2rem 0;
    border: none;
}

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

/* ---- Prev / next ------------------------------------------------------- */

.pager {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    margin-top: 3.5rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--rule);
}

.pager a {
    flex: 1 1 0;
    padding: 0.85rem 1rem;
    border: 1px solid var(--rule);
    border-radius: 8px;
    text-decoration: none;
    color: var(--ink);
    font-family: var(--sans);
    background: #fff;
}

.pager a:hover {
    border-color: var(--accent);
}

.pager .pager-prev {
    text-align: left;
}

.pager .pager-next {
    text-align: right;
}

.pager .pager-dir {
    display: block;
    font-size: 0.72rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--muted);
    margin-bottom: 0.2rem;
}

.pager .pager-title {
    font-weight: 600;
    font-size: 0.92rem;
}

.pager .spacer {
    flex: 1 1 0;
}

/* ---- Landing page ------------------------------------------------------ */

.landing h1 {
    font-family: var(--sans);
    font-size: 2.6rem;
    line-height: 1.08;
    letter-spacing: -0.025em;
    font-weight: 700;
    margin: 0 0 0.6rem 0;
}

.landing .subtitle {
    font-size: 1.2rem;
    color: var(--ink-soft);
    margin-bottom: 2rem;
}

.landing-hero {
    display: flex;
    align-items: center;
    gap: 2.4rem;
    margin-bottom: 2.4rem;
}

.landing-hero .subtitle {
    margin-bottom: 0;
}

.landing-cover {
    width: 200px;
    height: auto;
    flex-shrink: 0;
    border: 1px solid var(--rule);
    box-shadow: 0 12px 28px rgba(23, 24, 27, 0.12);
}

@media (max-width: 640px) {
    .landing-hero {
        flex-direction: column;
        align-items: flex-start;
        gap: 1.6rem;
    }

    .landing-cover {
        width: 160px;
    }
}

.landing .part-block {
    margin: 2rem 0;
}

.landing .part-block h2 {
    font-size: 0.78rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--accent);
    border-bottom: 1px solid var(--rule);
    padding-bottom: 0.4rem;
    margin-bottom: 0.7rem;
}

.landing .chapter-row {
    display: flex;
    gap: 0.9rem;
    padding: 0.35rem 0;
    text-decoration: none;
    border: none;
    color: var(--ink);
}

.landing .chapter-row:hover .chapter-row-title {
    color: var(--accent);
}

.landing .chapter-row-num {
    flex: 0 0 1.8rem;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
    font-family: var(--sans);
    font-size: 0.95rem;
}

/* ---- Mobile menu toggle ------------------------------------------------ */

.menu-toggle {
    display: none;
}

@media (max-width: 60rem) {
    .rail {
        display: none;
    }
    .content-wrap {
        grid-template-columns: minmax(0, 1fr);
    }
}

@media (max-width: 48rem) {
    .layout {
        grid-template-columns: 1fr;
    }
    .sidebar {
        position: fixed;
        z-index: 40;
        /* Leave a generous strip of scrim so the drawer is easy to dismiss. */
        width: min(19rem, 78vw);
        transform: translateX(-100%);
        transition: transform 0.22s ease;
        box-shadow: 0 0 40px rgba(0, 0, 0, 0.12);
    }
    body.nav-open .sidebar {
        transform: translateX(0);
    }
    /* Stop the page behind the drawer from scrolling under the reader's thumb. */
    body.nav-open {
        overflow: hidden;
    }
    .main {
        padding: 4.5rem 1.4rem 4rem 1.4rem;
    }
    .menu-toggle {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        position: fixed;
        top: 0.8rem;
        left: 0.8rem;
        z-index: 50;
        padding: 0.5rem 0.85rem;
        background: #fff;
        border: 1px solid var(--rule-strong);
        border-radius: 8px;
        font-family: var(--sans);
        font-size: 0.85rem;
        font-weight: 600;
        cursor: pointer;
    }
    .nav-scrim {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.25);
        z-index: 30;
    }
    body.nav-open .nav-scrim {
        display: block;
    }
}

@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }
    .sidebar {
        transition: none;
    }
}
