:root {
    color-scheme: light;
    --paper: #eef1ec;
    --paper-end: #e5ded0;
    --paper-strong: #fffdf7;
    --ink: #1c2527;
    --muted: #64706f;
    --rail: #ded8ca;
    --rail-strong: #c8bda9;
    --green: #2c6b5d;
    --blue: #315f87;
    --rust: #a44f34;
    --gold: #9b742f;
    --violet: #675184;
    --shadow: 0 10px 32px rgba(45, 38, 28, 0.09);
    --radius: 8px;
}

body.dark {
    color-scheme: dark;
    --paper: #15191b;
    --paper-end: #111416;
    --paper-strong: #1f2528;
    --ink: #ece8de;
    --muted: #aab2ad;
    --rail: #30373a;
    --rail-strong: #485054;
    --green: #76bba2;
    --blue: #86b4df;
    --rust: #df8c6f;
    --gold: #d7b66b;
    --violet: #b9a2d8;
    --shadow: 0 12px 42px rgba(0, 0, 0, 0.34);
}

* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    margin: 0;
    background: linear-gradient(180deg, var(--paper), var(--paper-end));
    color: var(--ink);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    line-height: 1.62;
}

a {
    color: var(--blue);
}

button,
input {
    font: inherit;
}

button {
    cursor: pointer;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
summary:focus-visible,
.reader-panel:focus-visible {
    outline: 2px solid var(--blue);
    outline-offset: 3px;
}

.skip-link {
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 60;
    transform: translateY(-150%);
    padding: 8px 10px;
    border: 1px solid var(--rail-strong);
    border-radius: var(--radius);
    background: var(--paper-strong);
    color: var(--ink);
    font-weight: 800;
}

.skip-link:focus {
    transform: translateY(0);
}

.progress {
    position: fixed;
    inset: 0 auto auto 0;
    width: 0;
    height: 3px;
    z-index: 30;
    background: linear-gradient(90deg, var(--green), var(--blue), var(--rust));
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: grid;
    grid-template-columns: minmax(140px, 0.75fr) minmax(260px, 1.2fr) minmax(260px, 1fr);
    gap: 18px;
    align-items: center;
    padding: 14px 22px;
    border-bottom: 1px solid var(--rail);
    background: color-mix(in srgb, var(--paper-strong) 92%, transparent);
    backdrop-filter: blur(18px);
}

.home-link {
    display: inline-flex;
    gap: 10px;
    align-items: center;
    width: max-content;
    color: var(--ink);
    text-decoration: none;
    font-weight: 700;
}

.home-mark {
    display: inline-grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border: 1px solid var(--rail-strong);
    border-radius: 50%;
    color: var(--green);
}

.book-title {
    min-width: 0;
}

.eyebrow {
    display: block;
    color: var(--muted);
    font-size: 0.76rem;
    font-weight: 700;
    text-transform: uppercase;
}

.book-title h1 {
    margin: 0;
    font-family: Georgia, "Times New Roman", serif;
    font-size: clamp(1.25rem, 2.8vw, 2rem);
    line-height: 1.08;
}

.top-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    justify-content: flex-end;
    min-width: 0;
}

.search-field {
    display: flex;
    align-items: center;
    gap: 8px;
    width: min(360px, 100%);
    padding: 9px 11px;
    border: 1px solid var(--rail);
    border-radius: var(--radius);
    background: var(--paper);
}

.search-field svg {
    flex: 0 0 18px;
    width: 18px;
    height: 18px;
    color: var(--muted);
}

.search-field input {
    width: 100%;
    min-width: 0;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--ink);
}

.icon-button {
    display: inline-grid;
    place-items: center;
    width: 40px;
    height: 40px;
    border: 1px solid var(--rail);
    border-radius: var(--radius);
    background: var(--paper-strong);
    color: var(--ink);
}

.icon-button svg {
    width: 19px;
    height: 19px;
}

.book-layout {
    display: grid;
    grid-template-columns: minmax(220px, 0.58fr) minmax(0, 900px) minmax(270px, 0.68fr);
    gap: 24px;
    width: min(1500px, calc(100vw - 32px));
    margin: 24px auto;
    align-items: start;
}

.toc-panel,
.reader-panel,
.atlas-section,
.glossary-section,
.sources-section,
.search-results {
    border: 1px solid var(--rail);
    border-radius: var(--radius);
    background: var(--paper-strong);
    box-shadow: var(--shadow);
}

.toc-panel {
    position: sticky;
    top: 84px;
    max-height: calc(100vh - 106px);
    overflow: auto;
    padding: 14px;
}

.panel-heading,
.section-title-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    color: var(--muted);
    font-size: 0.83rem;
    font-weight: 800;
    text-transform: uppercase;
}

.chapter-nav {
    display: grid;
    gap: 8px;
}

.part-heading {
    margin: 12px 4px 2px;
    color: var(--rust);
    font-size: 0.72rem;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 0;
}

.chapter-link {
    display: grid;
    gap: 3px;
    width: 100%;
    padding: 10px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    background: transparent;
    color: var(--ink);
    text-align: left;
}

.chapter-link:hover,
.chapter-link.active {
    border-color: var(--rail-strong);
    background: color-mix(in srgb, var(--green) 8%, transparent);
}

.chapter-link strong {
    font-size: 0.95rem;
    line-height: 1.25;
}

.chapter-link span {
    color: var(--muted);
    font-size: 0.78rem;
}

.reader-panel {
    position: relative;
    padding: clamp(34px, 5.5vw, 76px);
    min-width: 0;
    background: linear-gradient(90deg, rgba(34, 26, 14, 0.032), transparent 8%, transparent 92%, rgba(34, 26, 14, 0.04)), var(--paper-strong);
}

.running-head {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    margin: 0 auto 30px;
    max-width: 68ch;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--rail);
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0;
}

.chapter-kicker {
    max-width: 68ch;
    margin: 0 auto 8px;
    color: var(--rust);
    font-size: 0.82rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0;
}

.chapter-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    max-width: 68ch;
    margin: 0 auto 14px;
    color: var(--muted);
    font-size: 0.82rem;
}

.chapter-meta span {
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
}

.chapter-meta span:not(:last-child)::after {
    content: "/";
    margin-left: 8px;
    color: var(--rail-strong);
}

#chapterTitle {
    max-width: 20ch;
    margin: 0 auto;
    font-family: Georgia, "Times New Roman", serif;
    font-size: clamp(2rem, 3.6vw, 3.9rem);
    line-height: 1.02;
    text-wrap: balance;
}

.chapter-dek {
    max-width: 68ch;
    margin: 20px auto 28px;
    color: var(--muted);
    font-family: Georgia, "Times New Roman", serif;
    font-size: clamp(1.05rem, 1.28vw, 1.22rem);
    line-height: 1.58;
}

.chapter-controls {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    max-width: 68ch;
    margin: 0 auto 34px;
    border-top: 1px solid var(--rail);
    border-bottom: 1px solid var(--rail);
    padding: 12px 0;
}

.chapter-controls button {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
    padding: 8px 12px;
    border: 1px solid var(--rail);
    border-radius: var(--radius);
    background: var(--paper);
    color: var(--ink);
    font-weight: 700;
}

.chapter-controls button:disabled {
    cursor: not-allowed;
    opacity: 0.42;
}

.chapter-controls svg {
    width: 17px;
    height: 17px;
}

.chapter-content {
    max-width: 68ch;
    margin: 0 auto;
    font-family: Georgia, "Times New Roman", serif;
}

.chapter-content section {
    margin: 0 0 42px;
}

.chapter-content section + section {
    padding-top: 6px;
}

.chapter-content h3 {
    margin: 0 0 16px;
    font-family: Georgia, "Times New Roman", serif;
    font-size: clamp(1.35rem, 2vw, 2.05rem);
    line-height: 1.12;
}

.chapter-content p {
    margin: 0 0 16px;
    font-size: clamp(1.05rem, 1.08vw, 1.16rem);
    line-height: 1.78;
}

.chapter-content p + p {
    text-indent: 1.25em;
}

.chapter-content section:first-child p:first-of-type::first-letter {
    float: left;
    padding: 0.08em 0.1em 0 0;
    color: var(--rust);
    font-size: 4.25em;
    line-height: 0.76;
}

.chapter-content ul {
    margin: 10px 0 0 20px;
    padding: 0;
}

.chapter-content li {
    margin: 8px 0;
}

.callout {
    margin: 18px 0;
    padding: 16px;
    border-left: 4px solid var(--rust);
    border-radius: 0 var(--radius) var(--radius) 0;
    background: color-mix(in srgb, var(--rust) 9%, transparent);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.cite {
    display: inline-block;
    margin-left: 2px;
    color: var(--blue);
    font-size: 0.76rem;
    font-weight: 800;
    text-decoration: none;
    vertical-align: super;
}

.lesson-strip {
    max-width: 68ch;
    margin: 34px auto 0;
    padding-top: 22px;
    border-top: 1px solid var(--rail);
}

.lesson-strip h3,
.chapter-biblio h3 {
    margin: 0 0 12px;
    color: var(--muted);
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 0;
}

#chapterLessons {
    display: grid;
    gap: 10px;
}

.lesson-card {
    padding: 12px 0 12px 14px;
    border-left: 3px solid var(--green);
    background: transparent;
}

.lesson-card strong {
    display: block;
    margin-bottom: 4px;
    color: var(--green);
}

.chapter-biblio {
    max-width: 68ch;
    margin: 30px auto 0;
    padding-top: 22px;
    border-top: 1px solid var(--rail);
}

#chapterReferences {
    display: grid;
    gap: 8px;
}

.chapter-reference {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 3px 10px;
    align-items: start;
    padding: 10px 0;
    border-bottom: 1px solid var(--rail);
    color: var(--ink);
    text-decoration: none;
}

.chapter-reference span {
    grid-row: 1 / 3;
    display: inline-grid;
    place-items: center;
    min-width: 30px;
    height: 30px;
    border: 1px solid var(--rail);
    border-radius: 50%;
    color: var(--blue);
    font-size: 0.8rem;
    font-weight: 800;
}

.chapter-reference strong {
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 3px;
}

.chapter-reference small {
    color: var(--muted);
}

.atlas-panel {
    display: grid;
    gap: 18px;
    position: sticky;
    top: 84px;
    max-height: calc(100vh - 106px);
    overflow: auto;
}

.atlas-section {
    padding: 15px;
    box-shadow: none;
    background: color-mix(in srgb, var(--paper-strong) 88%, transparent);
}

.atlas-section h2,
.glossary-section h2,
.sources-section h2 {
    margin: 0;
    color: var(--ink);
    font-size: 0.95rem;
}

#lineageCanvas {
    display: block;
    width: 100%;
    height: 260px;
    border: 1px solid var(--rail);
    border-radius: var(--radius);
    background: var(--paper);
}

.model-detail {
    min-height: 74px;
    margin-top: 12px;
    padding: 12px;
    border: 1px solid var(--rail);
    border-radius: var(--radius);
    background: var(--paper);
    color: var(--muted);
    font-size: 0.9rem;
}

.model-detail strong {
    display: block;
    color: var(--ink);
}

.era-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
}

.era-filter {
    min-height: 34px;
    padding: 6px 9px;
    border: 1px solid var(--rail);
    border-radius: 999px;
    background: var(--paper);
    color: var(--ink);
    font-size: 0.82rem;
    font-weight: 700;
}

.era-filter.active {
    background: var(--green);
    border-color: var(--green);
    color: var(--paper-strong);
}

.timeline-list {
    display: grid;
    gap: 10px;
}

.timeline-event {
    display: grid;
    gap: 4px;
    padding: 10px 10px 10px 14px;
    border-left: 3px solid var(--blue);
    border-radius: 0 var(--radius) var(--radius) 0;
    background: var(--paper);
}

.timeline-jump {
    display: grid;
    gap: 4px;
    width: 100%;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--ink);
    text-align: left;
}

.timeline-event strong,
.timeline-jump strong {
    line-height: 1.25;
}

.timeline-event span,
.timeline-jump span {
    color: var(--muted);
    font-size: 0.8rem;
    font-weight: 800;
}

.timeline-event p,
.timeline-jump p {
    margin: 0;
    color: var(--muted);
    font-size: 0.88rem;
}

.timeline-source {
    font-size: 0.78rem;
    font-weight: 800;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
}

.lineage-text {
    margin-top: 10px;
    color: var(--muted);
    font-size: 0.82rem;
}

.lineage-text summary {
    width: max-content;
    cursor: pointer;
    color: var(--blue);
    font-weight: 800;
}

.lineage-fallback-list {
    display: grid;
    gap: 6px;
    margin: 8px 0 0;
    padding: 0;
    list-style: none;
}

.lineage-fallback-list a {
    color: var(--ink);
    font-weight: 700;
}

.reference-layout {
    display: grid;
    grid-template-columns: minmax(0, 0.92fr) minmax(0, 1.08fr);
    gap: 22px;
    width: min(1500px, calc(100vw - 32px));
    margin: 0 auto 42px;
    align-items: start;
}

.glossary-section,
.sources-section {
    padding: 20px;
    box-shadow: none;
}

.glossary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 10px;
}

.glossary-term,
.source-row {
    border: 1px solid var(--rail);
    border-radius: var(--radius);
    padding: 12px;
    background: var(--paper);
}

.glossary-term strong {
    display: block;
    color: var(--green);
}

.glossary-term p {
    margin: 5px 0 0;
    color: var(--muted);
    font-size: 0.9rem;
}

.sources-list {
    display: grid;
    gap: 9px;
}

.source-row {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 10px;
    align-items: start;
}

.source-row span {
    display: inline-grid;
    place-items: center;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: color-mix(in srgb, var(--blue) 12%, transparent);
    color: var(--blue);
    font-size: 0.8rem;
    font-weight: 800;
}

.source-row a {
    font-weight: 800;
    text-decoration-thickness: 1px;
    text-underline-offset: 3px;
}

.source-row small {
    display: block;
    color: var(--muted);
}

.source-meta,
.source-usage {
    display: block;
    margin-top: 4px;
    color: var(--muted);
    font-size: 0.78rem;
}

.source-usage a {
    font-weight: 700;
}

.search-results {
    position: fixed;
    top: 76px;
    right: 22px;
    z-index: 25;
    width: min(520px, calc(100vw - 32px));
    max-height: min(560px, calc(100vh - 96px));
    overflow: auto;
    padding: 12px;
}

.search-result {
    display: grid;
    gap: 4px;
    width: 100%;
    padding: 12px;
    border: 1px solid var(--rail);
    border-radius: var(--radius);
    background: var(--paper);
    color: var(--ink);
    text-align: left;
}

.search-result + .search-result {
    margin-top: 8px;
}

.search-result span {
    color: var(--muted);
    font-size: 0.8rem;
    font-weight: 800;
}

@media (max-width: 1220px) {
    .topbar {
        grid-template-columns: auto 1fr;
    }

    .top-actions {
        grid-column: 1 / -1;
        justify-content: stretch;
    }

    .search-field {
        width: 100%;
    }

    .book-layout {
        grid-template-columns: minmax(190px, 0.55fr) minmax(0, 1fr);
    }

    .atlas-panel {
        grid-column: 1 / -1;
        position: static;
        max-height: none;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    }
}

@media (max-width: 820px) {
    .topbar {
        position: static;
        grid-template-columns: 1fr;
        padding: 14px;
    }

    .book-layout,
    .reference-layout {
        width: min(100% - 20px, 760px);
        grid-template-columns: 1fr;
        gap: 14px;
        margin-top: 14px;
    }

    .toc-panel,
    .atlas-panel {
        position: static;
        max-height: none;
    }

    .reader-panel {
        order: 1;
    }

    .toc-panel {
        order: 2;
    }

    .atlas-panel {
        order: 3;
    }

    .chapter-nav {
        grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    }

    .reader-panel {
        padding: 20px;
    }

    .running-head {
        margin-bottom: 22px;
    }

    .atlas-panel {
        grid-template-columns: 1fr;
    }

    .chapter-controls {
        align-items: stretch;
    }

    .chapter-controls button {
        justify-content: center;
        width: 100%;
    }

    .search-results {
        top: 12px;
        right: 10px;
    }
}

@media (max-width: 520px) {
    .home-link {
        width: 100%;
    }

    .top-actions {
        flex-wrap: wrap;
    }

    .icon-button {
        width: 100%;
    }

    .chapter-controls {
        flex-direction: column;
    }

    .chapter-content p {
        font-size: 1rem;
    }

    .chapter-content p + p {
        text-indent: 0;
    }

    .chapter-content section:first-child p:first-of-type::first-letter {
        float: none;
        padding: 0;
        color: inherit;
        font-size: inherit;
        line-height: inherit;
    }
}
