/* Cognitive: a quiet reference for reading and study. */

:root {
    --bg: #fbfaf6;
    --bg-side: #f4f2ec;
    --bg-soft: #f0ede5;
    --text: #1d2126;
    --muted: #5b6168;
    --rule: #dfdbd1;
    --rule-strong: #a9a59b;
    --link: #1f4f99;
    --accent: #a8431f;
    --fig-low: #f6f3ec;
    --fig-high: #a7b4bf;

    --serif: Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, serif;
    --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;

    --bar: 3.25rem;
    --side: 19rem;
}

[data-theme="dark"] {
    --bg: #15181c;
    --bg-side: #1a1e23;
    --bg-soft: #20252b;
    --text: #e4e2dc;
    --muted: #a3a8ae;
    --rule: #2f353c;
    --rule-strong: #59616a;
    --link: #8fb6f2;
    --accent: #e58a63;
    --fig-low: #1f242a;
    --fig-high: #4d5a67;
    color-scheme: dark;
}

/* Each part of the book has its own color */
[data-part="1"] { --accent: #a3620c; --plate: #f0a93b; }
[data-part="2"] { --accent: #0b7c70; --plate: #35c9b5; }
[data-part="3"] { --accent: #1f5fbf; --plate: #5ea2ff; }
[data-part="4"] { --accent: #6a45c9; --plate: #ab8eff; }
[data-part="5"] { --accent: #27793b; --plate: #6fd483; }
[data-part="6"] { --accent: #b8431f; --plate: #ffb454; }
[data-part="7"] { --accent: #b02f58; --plate: #c9e265; }
[data-part="8"] { --accent: #0e7490; --plate: #5ed3ec; }
[data-theme="dark"] [data-part], [data-theme="dark"] body[data-part] { --accent: var(--plate); }

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

html { -webkit-text-size-adjust: 100%; scroll-padding-top: calc(var(--bar) + 1.25rem); }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 400 1.125rem/1.7 var(--serif);
}

a { color: var(--link); text-underline-offset: 0.16em; text-decoration-thickness: 1px; }
a:hover { text-decoration-thickness: 2px; }
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }

.sr {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

.skip { position: absolute; left: 1rem; top: -4rem; z-index: 60; padding: 0.5rem 1rem; background: var(--text); color: var(--bg); }
.skip:focus { top: 0.5rem; }

/* ---------- Top bar ---------- */

.topbar {
    position: sticky;
    top: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    height: var(--bar);
    padding: 0 1.25rem;
    background: var(--bg);
    border-bottom: 1px solid var(--rule);
    font-family: var(--sans);
}

.wordmark { color: var(--text); font: 600 1.15rem/1 var(--serif); letter-spacing: 0.01em; text-decoration: none; }

.theme-button, .menu-button {
    min-height: 2.25rem;
    padding: 0 0.7rem;
    border: 1px solid var(--rule);
    border-radius: 3px;
    background: transparent;
    cursor: pointer;
    font-size: 0.875rem;
}
.theme-button { margin-left: auto; }
.theme-button:hover, .menu-button:hover { border-color: var(--rule-strong); }

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

.landing { max-width: 68rem; margin: 0 auto; padding: 0 1.5rem 5rem; }

.landing-intro { display: grid; align-items: center; gap: 2.5rem; padding: clamp(3rem, 9vh, 6rem) 0 clamp(3rem, 8vh, 5rem); }
.landing-plate { margin: 0; }
@media (min-width: 960px) { .landing-intro { grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); gap: 3.5rem; } }
.index-grid h3 { color: var(--accent); }
.landing-intro h1 { margin: 0 0 1.25rem; font-size: clamp(2.6rem, 6vw, 4.25rem); font-weight: 400; line-height: 1.08; letter-spacing: -0.02em; }
.landing-intro p { margin: 0 0 2.25rem; color: var(--muted); font-size: 1.3rem; line-height: 1.5; }

.start {
    display: inline-block;
    padding: 0.8rem 2rem;
    border-radius: 3px;
    background: var(--text);
    color: var(--bg);
    font: 600 1.05rem/1.4 var(--sans);
    text-decoration: none;
}
.start:hover { background: var(--accent); color: #fff; }

.landing-index { padding-top: 2rem; border-top: 1px solid var(--rule); }
.landing-index > h2 { margin: 0 0 1.75rem; font: 600 0.875rem/1.4 var(--sans); color: var(--muted); }

.index-grid { columns: 3 17rem; column-gap: 3rem; }
.index-grid section { break-inside: avoid; padding-bottom: 1.75rem; }
.index-grid h3 { margin: 0 0 0.45rem; font: 600 0.95rem/1.35 var(--sans); }
.index-grid ol { margin: 0; padding-left: 1.7rem; color: var(--muted); font: 400 0.95rem/1.45 var(--sans); }
.index-grid li { padding: 0.12rem 0; }
.index-grid a { color: var(--text); text-decoration: none; }
.index-grid a:hover { color: var(--link); text-decoration: underline; }

/* ---------- Reading layout ---------- */

.shell { display: grid; grid-template-columns: minmax(0, 1fr); }

.sidebar {
    position: fixed;
    top: var(--bar);
    bottom: 0;
    left: 0;
    z-index: 30;
    display: flex;
    flex-direction: column;
    width: min(var(--side), 86vw);
    background: var(--bg-side);
    border-right: 1px solid var(--rule);
    font-family: var(--sans);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.18s ease, visibility 0.18s;
}
.sidebar.is-open { transform: none; visibility: visible; }
html:not(.js) .sidebar { position: static; width: auto; transform: none; visibility: visible; }

.sidebar-find { padding: 0.85rem 1rem 0.6rem; border-bottom: 1px solid var(--rule); }
.sidebar-find input {
    width: 100%;
    height: 2.25rem;
    padding: 0 0.6rem;
    border: 1px solid var(--rule);
    border-radius: 3px;
    background: var(--bg);
    font-size: 0.875rem;
}

.sidebar-nav { flex: 1; min-height: 0; padding: 0.4rem 0 2rem; overflow-y: auto; overscroll-behavior: contain; }
.nav-part h2 { margin: 1.1rem 1rem 0.3rem; color: var(--muted); font-size: 0.78rem; font-weight: 600; line-height: 1.3; }
.nav-part ol { margin: 0; padding: 0; list-style: none; }
.nav-part a {
    display: grid;
    grid-template-columns: 1.6rem minmax(0, 1fr);
    padding: 0.3rem 1rem 0.3rem 0.85rem;
    border-left: 3px solid transparent;
    color: var(--text);
    font-size: 0.875rem;
    line-height: 1.35;
    text-decoration: none;
}
.nav-part a:hover { background: var(--bg-soft); }
.nav-part a[aria-current="page"] { border-left-color: var(--accent); background: var(--bg-soft); font-weight: 600; }
.nav-n { color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; font-weight: 400; }
.nav-part[hidden], .nav-part li[hidden] { display: none; }

.scrim { position: fixed; inset: var(--bar) 0 0; z-index: 25; background: rgba(0, 0, 0, 0.35); }

.content { min-width: 0; padding: 2.5rem 1.25rem 3rem; }
.reading { width: 100%; max-width: 44rem; margin: 0 auto; }

@media (min-width: 1000px) {
    .menu-button { display: none; }
    .shell { grid-template-columns: var(--side) minmax(0, 1fr); }
    .sidebar { position: sticky; width: auto; height: calc(100vh - var(--bar)); transform: none; visibility: visible; transition: none; }
    .scrim { display: none; }
    .content { padding: 3.25rem 3rem 4rem; }
}

@media (min-width: 1380px) {
    .content { display: grid; grid-template-columns: minmax(0, 44rem) 13rem; justify-content: center; gap: 3.5rem; }
    .reading { margin: 0; }
}

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

.onpage { display: none; }
@media (min-width: 1380px) {
    .onpage { display: block; position: sticky; top: calc(var(--bar) + 3.25rem); align-self: start; font-family: var(--sans); font-size: 0.82rem; line-height: 1.35; }
    .onpage p { margin: 0 0 0.5rem; color: var(--muted); font-weight: 600; }
    .onpage ol { margin: 0; padding: 0; list-style: none; border-left: 1px solid var(--rule); }
    .onpage a { display: block; margin-left: -1px; padding: 0.22rem 0 0.22rem 0.75rem; border-left: 2px solid transparent; color: var(--muted); text-decoration: none; }
    .onpage a:hover { color: var(--text); }
    .onpage a.is-here { border-left-color: var(--accent); color: var(--text); }
}

/* ---------- Article ---------- */

.crumb { margin: 0 0 0.6rem; color: var(--muted); font: 400 0.875rem/1.4 var(--sans); }

.article h1 { margin: 0 0 1rem; font-size: clamp(2.1rem, 4.5vw, 2.85rem); font-weight: 400; line-height: 1.14; letter-spacing: -0.015em; }
.standfirst { margin: 0 0 2.25rem; color: var(--muted); font-size: 1.3rem; line-height: 1.5; }

.article h2 { margin: 2.75rem 0 0.8rem; padding-bottom: 0.3rem; border-bottom: 1px solid var(--rule); font-size: 1.5rem; font-weight: 600; line-height: 1.25; }
.article h3 { margin: 1.9rem 0 0.4rem; font: 600 1.05rem/1.35 var(--sans); }
.article p, .article ul, .article ol, .article dl { margin: 0 0 1.1rem; }
.article ul, .article ol { padding-left: 1.4rem; }
.article li { margin-bottom: 0.5rem; }
.article li::marker { color: var(--muted); }

.article math { font-size: 1.05em; max-width: 100%; }
.equation { margin: 1.25rem 0; overflow-x: auto; overflow-y: hidden; padding: 0.25rem 0; }

code { padding: 0.08em 0.3em; border-radius: 3px; background: var(--bg-soft); font: 0.84em var(--mono); }

.ev { font-family: var(--sans); font-size: 0.92em; font-weight: 600; }
.ev-established { color: var(--text); }
.ev-debated, .ev-open { color: var(--accent); }

.chapter-meta { padding: 0.7rem 0.9rem; background: var(--bg-soft); color: var(--muted); font: 400 0.9rem/1.55 var(--sans); }
.chapter-meta strong { color: var(--text); }

.misconception { margin: 0 0 1.1rem; }
.misconception em { display: block; margin-bottom: 0.4rem; font-size: 1.2rem; }

.table-wrap { margin: 0 0 1.4rem; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font: 400 0.92rem/1.45 var(--sans); }
th, td { padding: 0.5rem 0.75rem 0.5rem 0; border-bottom: 1px solid var(--rule); text-align: left; vertical-align: top; }
th { border-bottom-color: var(--rule-strong); font-weight: 600; }

.refs { font-size: 1rem; line-height: 1.55; }
.refs li { margin-bottom: 0.45rem; }

.glossary > div { padding: 0.85rem 0; border-bottom: 1px solid var(--rule); }
.glossary dt { font-weight: 600; }
.glossary dd { margin: 0.15rem 0 0; }

/* ---------- Pager ---------- */

.pager { display: grid; gap: 0.75rem; margin-top: 3.5rem; font-family: var(--sans); }
.pager a { display: block; padding: 0.9rem 1.1rem; border: 1px solid var(--rule); border-radius: 3px; color: var(--text); text-decoration: none; }
.pager a:hover { border-color: var(--rule-strong); background: var(--bg-soft); }
.pager span { display: block; color: var(--muted); font-size: 0.82rem; }
.pager strong { display: block; font-size: 1.02rem; font-weight: 600; line-height: 1.35; }
.pager-next { grid-column: 1 / -1; background: var(--text); border-color: var(--text) !important; color: var(--bg) !important; text-align: right; }
.pager-next span { color: inherit; opacity: 0.75; }
.pager-next:hover { background: var(--accent) !important; }

@media (min-width: 640px) {
    .pager { grid-template-columns: 1fr 1fr; }
    .pager-next { grid-column: 2; }
    .pager-prev:last-child { grid-column: 1; }
}

.page-foot { margin-top: 3rem; padding-top: 1rem; border-top: 1px solid var(--rule); color: var(--muted); font: 400 0.85rem/1.5 var(--sans); }
.page-foot p { margin: 0; }

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

.figure { margin: 2rem 0; padding: 1rem 0 0.9rem; border-top: 1px solid var(--rule-strong); border-bottom: 1px solid var(--rule); font-family: var(--sans); }
.figure figcaption { margin-top: 0.8rem; color: var(--muted); font-size: 0.88rem; line-height: 1.5; }
.figure figcaption b { color: var(--text); font-weight: 600; }

.fig-plot svg, .fig-plot canvas { display: block; width: 100%; height: auto; }
.fig-map { position: relative; }
.fig-map canvas + canvas { position: absolute; inset: 0; pointer-events: none; }
.fig-map canvas:first-child { cursor: crosshair; }

.fig-controls { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 0.7rem 1.25rem; margin-top: 0.8rem; font-size: 0.85rem; }
.fig-field { flex: 1 1 11rem; min-width: 0; }
.fig-field span { display: flex; justify-content: space-between; color: var(--muted); }
.fig-field output { color: var(--text); font-family: var(--mono); font-size: 0.82rem; }
.fig-field input { width: 100%; height: 1.75rem; margin: 0; accent-color: var(--accent); }

.fig-buttons { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.fig-button {
    min-height: 2.25rem;
    padding: 0 0.8rem;
    border: 1px solid var(--rule-strong);
    border-radius: 3px;
    background: transparent;
    cursor: pointer;
    font-size: 0.85rem;
}
.fig-button:hover { background: var(--bg-soft); }
.fig-button[aria-pressed="true"] { background: var(--text); border-color: var(--text); color: var(--bg); }
.fig-button.is-primary { background: var(--text); border-color: var(--text); color: var(--bg); }

.fig-readout { min-height: 1.5em; margin: 0.65rem 0 0; font: 400 0.82rem/1.5 var(--mono); }
.fig-legend { display: flex; flex-wrap: wrap; gap: 0.25rem 1.25rem; margin: 0.65rem 0 0; padding: 0; list-style: none; font-size: 0.85rem; }
.fig-legend li { display: flex; align-items: center; gap: 0.4rem; margin: 0; }
.fig-legend svg { flex: none; width: 2rem; height: 0.8rem; }

.f-axis { stroke: var(--rule-strong); stroke-width: 1; fill: none; }
.f-curve { stroke: var(--text); stroke-width: 2; fill: none; }
.f-step { stroke: var(--accent); stroke-width: 1.5; fill: none; }
.f-point { fill: var(--accent); }
.f-now { fill: var(--bg); stroke: var(--accent); stroke-width: 2.5; }
.f-label { fill: var(--muted); font: 400 13px var(--sans); }

.article .att-sentence, .article .bars, .article .fig-legend { padding-left: 0; }
.article .att-sentence, .article .bars { margin-bottom: 0; }

/* Learning aids */
.reading-bar { position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; background: var(--accent); transform: scaleX(0); transform-origin: left; }
.read-meta { display: flex; flex-wrap: wrap; gap: 0.2rem 1.25rem; margin: -1.25rem 0 1.5rem !important; color: var(--muted); font: 400 0.875rem/1.5 var(--sans); }
.read-meta span:first-child { color: var(--text); font-weight: 600; }
.intuition { margin: 0 0 2rem; padding: 1rem 1.2rem 1.1rem; border-left: 3px solid var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--bg)); }
.intuition span { display: block; margin-bottom: 0.2rem; color: var(--accent); font: 600 0.82rem var(--sans); }
.intuition p { margin: 0 !important; font-size: 1.12rem; line-height: 1.5; }
.article .takeaways { display: grid; gap: 0.15rem; padding: 0; list-style: none; }
.article .takeaways li { position: relative; margin: 0; padding: 0.55rem 0 0.55rem 1.8rem; border-bottom: 1px solid var(--rule); }
.article .takeaways li::before { content: "✓"; position: absolute; left: 0.2rem; top: 0.55rem; color: var(--accent); font: 700 1rem var(--sans); }
.article .related { display: grid; grid-template-columns: repeat(auto-fit, minmax(12.5rem, 1fr)); gap: 0.6rem; padding: 0; list-style: none; font-family: var(--sans); }
.article .related li { margin: 0; }
.article .related a { display: block; height: 100%; padding: 0.75rem 0.9rem; border: 1px solid var(--rule); border-radius: 3px; color: var(--text); font-size: 0.95rem; font-weight: 600; line-height: 1.35; text-decoration: none; }
.article .related a:hover { border-color: var(--accent); }
.article .related span { display: block; margin-bottom: 0.15rem; color: var(--accent); font-size: 0.8rem; font-weight: 600; }
.term { padding: 0; border: 0; border-bottom: 1px dotted var(--accent); background: none; color: inherit; cursor: help; font: inherit; }
.term:hover, .term[aria-expanded="true"] { color: var(--accent); }
.term-card { position: absolute; z-index: 70; width: min(20rem, calc(100vw - 20px)); padding: 0.8rem 0.95rem 0.85rem; border: 1px solid var(--rule-strong); border-radius: 4px; background: var(--bg); box-shadow: 0 6px 22px rgba(0, 0, 0, 0.18); font-family: var(--sans); }
.term-card[hidden] { display: none; }
.term-card strong { display: block; font-size: 0.95rem; }
.term-card p { margin: 0.2rem 0 0.45rem; color: var(--text); font-size: 0.88rem; line-height: 1.45; }
.term-card a { font-size: 0.82rem; }
.pager-keys { display: none; grid-column: 1 / -1; margin: 0; color: var(--muted); font-size: 0.8rem; text-align: center; }
@media (min-width: 1000px) { .pager-keys { display: block; } }

/* Animated illustrations */
.plate { position: relative; margin: 1.75rem 0 2.25rem; overflow: hidden; border-radius: 4px; background: #0f1319; font-family: var(--sans); }
.plate canvas { display: block; width: 100%; }
html.js .plate:not(:has(canvas))::before { content: ""; display: block; height: 18rem; }
.plate figcaption { padding: 0.65rem 0.9rem 0.75rem; border-top: 1px solid rgba(255, 255, 255, 0.1); color: rgba(255, 255, 255, 0.78); font-size: 0.86rem; line-height: 1.45; }
.plate-toggle { position: absolute; top: 0.5rem; right: 0.5rem; z-index: 2; min-height: 1.75rem; padding: 0 0.55rem; border: 1px solid rgba(255, 255, 255, 0.28); border-radius: 3px; background: rgba(15, 19, 25, 0.8); color: rgba(255, 255, 255, 0.85); cursor: pointer; font-size: 0.75rem; }
.plate-toggle:hover { border-color: rgba(255, 255, 255, 0.6); }

/* Key ideas and numbered defenses */
.article .concepts { display: grid; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); gap: 0.7rem; padding: 0; list-style: none; }
.article .concepts li { margin: 0; padding: 0.8rem 0.95rem 0.9rem; border-top: 3px solid var(--accent); background: var(--bg-soft); font-size: 1rem; line-height: 1.5; }
.article .steps { display: grid; gap: 0.25rem; padding: 0; list-style: none; counter-reset: step; }
.article .steps li { position: relative; margin: 0; padding: 0.6rem 0 0.7rem 3rem; border-bottom: 1px solid var(--rule); counter-increment: step; }
.article .steps li::before { content: counter(step); position: absolute; left: 0; top: 0.55rem; display: grid; place-items: center; width: 2rem; height: 2rem; border-radius: 50%; background: var(--accent); color: var(--bg); font: 700 0.9rem var(--sans); }
.case { margin: 0 0 1.1rem; padding: 1rem 1.15rem; border-radius: 4px; background: color-mix(in srgb, var(--accent) 10%, var(--bg)); }
.case p { margin: 0 !important; }
.myth { display: grid; gap: 0.7rem; margin: 0 0 1.1rem; }
.myth > div { padding: 0.9rem 1rem 1rem; border-radius: 4px; background: var(--bg-soft); }
.myth > div:last-child { background: color-mix(in srgb, var(--accent) 12%, var(--bg)); }
.myth span { display: block; margin-bottom: 0.3rem; color: var(--muted); font: 600 0.82rem var(--sans); }
.myth > div:last-child span { color: var(--accent); }
.myth p { margin: 0 !important; line-height: 1.5; }
.myth > div:first-child p { font-style: italic; }
@media (min-width: 640px) { .myth { grid-template-columns: 1fr 1fr; } }

/* Step-through diagrams */
.flow-tabs:not(:empty) { margin-bottom: 0.75rem; }
.flow-plot svg { width: 100%; height: auto; }
.flow-edge { fill: none; stroke: var(--rule-strong); stroke-width: 1.5; }
.flow-edge.is-done { stroke: var(--accent); stroke-width: 2; }
.flow-edge.is-blocked { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 5 5; }
.flow-node rect { fill: var(--bg); stroke: var(--rule-strong); stroke-width: 1.25; }
.flow-node text { fill: var(--text); font: 500 12.5px var(--sans); }
.flow-node.k-model rect { fill: var(--bg-soft); stroke: var(--text); }
.flow-node.k-store rect { fill: var(--bg-soft); }
.flow-node.k-threat rect { stroke: var(--accent); stroke-dasharray: 5 4; }
.flow-node.k-threat text { fill: var(--accent); }
.flow-node.k-guard rect { fill: var(--text); stroke: var(--text); }
.flow-node.k-guard text { fill: var(--bg); }
.flow-node.is-on rect { stroke: var(--accent); stroke-width: 2.25; }
.flow-node.k-guard.is-on rect { stroke: var(--accent); }
.flow-marker { fill: var(--accent); stroke: var(--bg); stroke-width: 2; }
.flow-cross { fill: var(--accent); font: 700 22px var(--sans); paint-order: stroke; stroke: var(--bg); stroke-width: 5px; }
.flow-caption { min-height: 4.2em; margin: 0.7rem 0 0.5rem !important; font-size: 0.98rem; line-height: 1.45; }
.flow-controls { align-items: center; }
.flow-count { margin-left: 0.4rem; color: var(--muted); font-size: 0.82rem; }
.fig-button:disabled { cursor: default; opacity: 0.45; }

/* Attention figure */
.att-sentence { display: flex; flex-wrap: wrap; gap: 0.3rem; margin: 0; padding: 0; list-style: none; }
.att-sentence li { margin: 0; }
.att-token { min-height: 2.5rem; padding: 0 0.6rem; border: 1px solid var(--rule); border-radius: 3px; background: color-mix(in srgb, var(--accent) calc(var(--w, 0) * 1%), transparent); cursor: pointer; font: 400 1rem var(--serif); }
.att-token[aria-pressed="true"] { border-color: var(--text); box-shadow: inset 0 0 0 1px var(--text); }

/* Distribution bars */
.bars { display: grid; gap: 0.3rem; margin: 0; padding: 0; list-style: none; }
.bars li { display: grid; grid-template-columns: 5rem minmax(0, 1fr) 3.5rem; align-items: center; gap: 0.6rem; margin: 0; font-size: 0.88rem; }
.bars i { display: block; height: 0.9rem; background: var(--accent); width: calc(var(--p) * 1%); }
.bars b { font: 400 0.82rem var(--mono); text-align: right; }

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