/* ==========================================================================
   historygrapher.net — Digital Illuminated Manuscript
   ========================================================================== */

:root {
    /* Palette (analogous warm — parchment, gold, oxblood, walnut) */
    --parchment: #f5efe3;
    --ink: #2c2418;
    --gold: #b8943e;
    --sienna: #a0623a;
    --oxblood: #7a2e2e;
    --olive-gold: #9e946c;
    --gilt: #c4a95a;
    --walnut: #1e1812;

    /* Typography */
    --font-display: "Cormorant Garamond", "Lora", Georgia, serif;
    --font-body: "Crimson Pro", "Crimson Text", Georgia, serif;
    --font-margin: "Alegreya Sans", "Inter", system-ui, sans-serif;

    /* Column width (per DESIGN.md) */
    --column-width: clamp(340px, 55vw, 720px);
    --margin-inset: 80px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
    background: var(--parchment);
    color: var(--ink);
    font-family: var(--font-body);
    font-size: clamp(1rem, 1.8vw, 1.25rem);
    line-height: 1.85;
    font-weight: 400;
    overflow-x: hidden;
    min-height: 100vh;
    position: relative;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
}

/* ==========================================================================
   Paper grain texture
   ========================================================================== */
.paper-grain {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 1;
    opacity: 0.5;
    background-image:
        radial-gradient(circle at 20% 30%, rgba(44, 36, 24, 0.03) 0.5px, transparent 1.2px),
        radial-gradient(circle at 70% 60%, rgba(44, 36, 24, 0.025) 0.4px, transparent 1.1px),
        radial-gradient(circle at 45% 80%, rgba(122, 46, 46, 0.02) 0.3px, transparent 1px),
        radial-gradient(circle at 80% 20%, rgba(184, 148, 62, 0.02) 0.5px, transparent 1.2px);
    background-size: 140px 140px, 90px 90px, 200px 200px, 170px 170px;
    background-position: 0 0, 30px 50px, 60px 10px, 15px 80px;
    mix-blend-mode: multiply;
}

body::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    background:
        radial-gradient(ellipse at 50% 30%, rgba(245, 239, 227, 0) 0%, rgba(160, 98, 58, 0.06) 70%, rgba(122, 46, 46, 0.1) 100%);
}

/* ==========================================================================
   Marginal Apparatus (visible >= 1024px)
   ========================================================================== */
.margin {
    position: fixed;
    top: 0;
    height: 100vh;
    width: calc((100vw - var(--column-width)) / 2);
    z-index: 3;
    pointer-events: none;
    display: none;
}

.margin-left { left: 0; }
.margin-right { right: 0; }

.margin-rule {
    position: absolute;
    top: 8vh;
    bottom: 8vh;
    width: 1px;
    background: linear-gradient(
        to bottom,
        transparent 0%,
        rgba(184, 148, 62, 0) 4%,
        rgba(184, 148, 62, 0.5) 20%,
        rgba(184, 148, 62, 0.5) 80%,
        rgba(184, 148, 62, 0) 96%,
        transparent 100%
    );
}

.margin-left .margin-rule { right: var(--margin-inset); }
.margin-right .margin-rule { left: var(--margin-inset); }

.margin-icon {
    position: absolute;
    width: 32px;
    height: 32px;
    color: var(--gold);
    opacity: 0;
    transition: opacity 900ms ease-out, transform 900ms ease-out, color 220ms ease-out;
    pointer-events: auto;
    cursor: default;
}

.margin-left .margin-icon {
    right: calc(var(--margin-inset) - 16px);
    transform: translateX(-20px);
}
.margin-right .margin-icon {
    left: calc(var(--margin-inset) - 16px);
    transform: translateX(20px);
}

.margin-icon.revealed {
    opacity: 0.6;
    transform: translateX(0);
}

.margin-icon:hover {
    opacity: 0.95 !important;
    color: var(--sienna);
    transform: translateX(0) scale(1.2) !important;
    transition: opacity 220ms ease-out, transform 220ms ease-out, color 220ms ease-out;
}

.margin-icon svg {
    width: 100%;
    height: 100%;
    display: block;
}

.folio {
    position: absolute;
    bottom: 4vh;
    font-family: var(--font-margin);
    font-size: clamp(0.7rem, 1.2vw, 0.85rem);
    font-weight: 700;
    letter-spacing: 0.2em;
    text-transform: lowercase;
    color: var(--olive-gold);
    opacity: 0.75;
    font-variant: small-caps;
}

.folio-left {
    right: var(--margin-inset);
    writing-mode: vertical-rl;
    transform: rotate(180deg);
}
.folio-right {
    left: var(--margin-inset);
    writing-mode: vertical-rl;
}

/* ==========================================================================
   Codex Column
   ========================================================================== */
.codex {
    position: relative;
    z-index: 2;
    width: var(--column-width);
    margin: 0 auto;
    padding: 0;
}

.section {
    position: relative;
    padding: clamp(4rem, 10vh, 8rem) 0;
}

/* ==========================================================================
   Frontispiece
   ========================================================================== */
.frontispiece {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 6vh 0 4vh;
    text-align: center;
}

.frontispiece-frame {
    position: relative;
    width: 100%;
    padding: clamp(2rem, 6vh, 5rem) clamp(1rem, 4vw, 3rem);
    opacity: 0;
    animation: frame-fade-in 600ms ease-out 200ms forwards;
}

@keyframes frame-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

.frontispiece-frame::before,
.frontispiece-frame::after {
    content: "";
    position: absolute;
    pointer-events: none;
    border: 1px solid var(--gold);
}

/* Outer double-rule frame */
.frontispiece-frame::before {
    inset: 0;
    border-width: 1px;
    clip-path: inset(0 0 0 0);
    animation: frame-draw 900ms cubic-bezier(0.2, 0.7, 0.3, 1) 300ms both;
}

.frontispiece-frame::after {
    inset: 10px;
    opacity: 0.5;
    animation: frame-draw 900ms cubic-bezier(0.2, 0.7, 0.3, 1) 500ms both;
}

@keyframes frame-draw {
    from { clip-path: inset(50% 50% 50% 50%); }
    to { clip-path: inset(0 0 0 0); }
}

.corner {
    position: absolute;
    width: 40px;
    height: 40px;
    color: var(--gold);
    opacity: 0;
    animation: corner-appear 500ms ease-out 1100ms forwards;
}
.corner svg { width: 100%; height: 100%; }
.corner-tl { top: -10px; left: -10px; }
.corner-tr { top: -10px; right: -10px; transform: scaleX(-1); }
.corner-bl { bottom: -10px; left: -10px; transform: scaleY(-1); }
.corner-br { bottom: -10px; right: -10px; transform: scale(-1, -1); }

@keyframes corner-appear {
    from { opacity: 0; transform: scale(0.5) var(--corner-extra, translateZ(0)); }
    to { opacity: 1; }
}
.corner-tr { animation-name: corner-appear-tr; }
.corner-bl { animation-name: corner-appear-bl; }
.corner-br { animation-name: corner-appear-br; }

@keyframes corner-appear-tr {
    from { opacity: 0; transform: scale(0.5) scaleX(-1); }
    to { opacity: 1; transform: scale(1) scaleX(-1); }
}
@keyframes corner-appear-bl {
    from { opacity: 0; transform: scale(0.5) scaleY(-1); }
    to { opacity: 1; transform: scale(1) scaleY(-1); }
}
@keyframes corner-appear-br {
    from { opacity: 0; transform: scale(0.5) scale(-1, -1); }
    to { opacity: 1; transform: scale(1) scale(-1, -1); }
}

.frontispiece-inner {
    position: relative;
    z-index: 2;
    padding: clamp(1rem, 4vh, 3rem) clamp(0.5rem, 2vw, 1.5rem);
}

.frontispiece-eyebrow {
    font-family: var(--font-margin);
    font-size: clamp(0.7rem, 1.2vw, 0.85rem);
    font-weight: 400;
    font-style: italic;
    letter-spacing: 0.4em;
    color: var(--olive-gold);
    margin-bottom: clamp(1rem, 3vh, 2rem);
    text-transform: lowercase;
    font-variant: small-caps;
    opacity: 0;
    animation: subtle-fade 600ms ease-out 1200ms forwards;
}

.title {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: clamp(3.5rem, 10vw, 9rem);
    line-height: 0.95;
    letter-spacing: 0.04em;
    color: var(--ink);
    margin-bottom: clamp(1.5rem, 4vh, 3rem);
    opacity: 0;
    transform: scale(1.2);
    animation: zoom-focus 500ms cubic-bezier(0.2, 0.7, 0.3, 1) 1400ms forwards;
}

.title-line { display: block; }
.title-line-1 {
    font-style: italic;
    color: var(--ink);
}
.title-dot {
    display: block;
    color: var(--gold);
    font-size: 0.4em;
    line-height: 0.3;
    margin: 0.1em 0;
}
.title-line-2 {
    font-weight: 700;
    color: var(--oxblood);
    letter-spacing: 0.08em;
}

@keyframes zoom-focus {
    from { opacity: 0; transform: scale(1.2); }
    to { opacity: 1; transform: scale(1); }
}

.frontispiece-ornament {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin: clamp(1rem, 3vh, 2rem) auto;
    max-width: 80%;
    opacity: 0;
    animation: subtle-fade 600ms ease-out 1800ms forwards;
}

.frontispiece-ornament .rule-line {
    flex: 1;
    height: 1px;
    background: linear-gradient(to right, transparent, var(--gilt), transparent);
}

.frontispiece-ornament svg {
    width: clamp(40px, 6vw, 64px);
    height: auto;
    color: var(--gold);
    flex-shrink: 0;
}

.subtitle {
    font-family: var(--font-margin);
    font-weight: 400;
    font-size: clamp(0.95rem, 1.6vw, 1.15rem);
    letter-spacing: 0.15em;
    color: var(--sienna);
    font-style: italic;
    min-height: 1.8em;
    margin-bottom: clamp(1.5rem, 4vh, 3rem);
    font-variant: small-caps;
    text-transform: lowercase;
}

.subtitle::after {
    content: "|";
    display: inline-block;
    margin-left: 2px;
    color: var(--gold);
    animation: caret-blink 800ms steps(2, end) infinite;
    opacity: 0;
}

.subtitle.typing::after {
    opacity: 1;
}

.subtitle.done::after {
    opacity: 0;
}

@keyframes caret-blink {
    0%, 50% { opacity: 1; }
    50.01%, 100% { opacity: 0; }
}

.frontispiece-attribution {
    font-family: var(--font-body);
    font-style: italic;
    font-size: clamp(0.85rem, 1.4vw, 1rem);
    line-height: 1.6;
    color: var(--olive-gold);
    opacity: 0;
    animation: subtle-fade 600ms ease-out 3800ms forwards;
}

@keyframes subtle-fade {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.scroll-hint {
    margin-top: clamp(2rem, 6vh, 5rem);
    font-family: var(--font-margin);
    font-size: 0.75rem;
    letter-spacing: 0.3em;
    color: var(--olive-gold);
    text-transform: lowercase;
    font-variant: small-caps;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    opacity: 0;
    animation: scroll-hint-pulse 2400ms ease-in-out 4200ms infinite;
}

.scroll-hint svg {
    width: 18px;
    height: 18px;
    color: var(--gold);
}

@keyframes scroll-hint-pulse {
    0%, 100% { opacity: 0.35; transform: translateY(0); }
    50% { opacity: 0.9; transform: translateY(4px); }
}

/* ==========================================================================
   Chapter Dividers
   ========================================================================== */
.chapter-divider {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin: 0 auto;
    padding: 3rem 0;
    width: 60%;
    opacity: 0;
    transition: opacity 800ms ease-out;
}

.chapter-divider.in-view {
    opacity: 1;
}

.divider-rule {
    flex: 1;
    height: 1px;
    background: var(--gilt);
    opacity: 0.6;
    transform: scaleX(0);
    transform-origin: center;
    transition: transform 800ms cubic-bezier(0.2, 0.7, 0.3, 1);
}

.chapter-divider.in-view .divider-rule {
    transform: scaleX(1);
}

.divider-rule-left { transform-origin: right; }
.divider-rule-right { transform-origin: left; }

.divider-ornament {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    color: var(--gold);
    transform: scale(0.6) rotate(0deg);
    opacity: 0.4;
    transition: transform 800ms cubic-bezier(0.2, 0.7, 0.3, 1), opacity 800ms ease-out;
}

.chapter-divider.in-view .divider-ornament {
    transform: scale(1) rotate(360deg);
    opacity: 1;
}

.divider-ornament svg {
    width: 100%;
    height: 100%;
}

/* ==========================================================================
   Chapters
   ========================================================================== */
.chapter {
    opacity: 0;
    transform: scale(0.97);
    transition: opacity 400ms ease-out, transform 400ms ease-out;
}

.chapter.in-view {
    opacity: 1;
    transform: scale(1);
}

.chapter-head {
    text-align: center;
    margin-bottom: clamp(2rem, 5vh, 4rem);
}

.chapter-mark {
    font-family: var(--font-margin);
    font-weight: 700;
    font-size: clamp(0.7rem, 1.2vw, 0.85rem);
    letter-spacing: 0.35em;
    color: var(--oxblood);
    text-transform: lowercase;
    font-variant: small-caps;
    margin-bottom: clamp(0.75rem, 2vh, 1.5rem);
}

.chapter-title {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: clamp(2rem, 5vw, 4rem);
    line-height: 1.05;
    letter-spacing: 0.03em;
    color: var(--ink);
    font-style: italic;
}

.chapter-body {
    font-family: var(--font-body);
    font-size: clamp(1rem, 1.8vw, 1.25rem);
    line-height: 1.85;
    color: var(--ink);
}

.chapter-body p {
    margin-bottom: 1.2em;
    text-align: justify;
    hyphens: auto;
}

.chapter-body p:last-child {
    margin-bottom: 0;
}

.chapter-lede {
    position: relative;
}

/* ==========================================================================
   Drop Caps
   ========================================================================== */
.dropcap {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 5.2em;
    line-height: 0.85;
    color: var(--oxblood);
    float: left;
    margin: 0.08em 0.12em -0.05em 0;
    padding: 0.05em 0.15em 0 0;
    font-style: normal;
    letter-spacing: 0;
    display: inline-block;
    transform-origin: center;
    transition: transform 300ms ease-out, color 300ms ease-out;
    cursor: default;
    shape-outside: margin-box;
    border-left: 1px solid var(--gold);
    border-top: 1px solid var(--gold);
    background:
        linear-gradient(135deg, rgba(184, 148, 62, 0.08) 0%, transparent 60%);
}

.dropcap:hover {
    transform: scale(1.05);
}

/* ==========================================================================
   Pull quote
   ========================================================================== */
.pull-quote {
    font-family: var(--font-body);
    font-style: italic;
    font-size: clamp(1.15rem, 2.2vw, 1.55rem);
    line-height: 1.5;
    color: var(--oxblood);
    text-align: center;
    margin: clamp(2rem, 5vh, 3.5rem) auto !important;
    padding: clamp(1.25rem, 3vh, 2rem) clamp(1rem, 3vw, 2rem);
    max-width: 90%;
    position: relative;
    border-top: 1px solid var(--gilt);
    border-bottom: 1px solid var(--gilt);
    letter-spacing: 0.01em;
}

.pull-quote::before,
.pull-quote::after {
    content: "";
    position: absolute;
    left: 50%;
    width: 20px;
    height: 20px;
    transform: translateX(-50%) rotate(45deg);
    background: var(--parchment);
    border: 1px solid var(--gilt);
}

.pull-quote::before {
    top: -11px;
    border-right: none;
    border-bottom: none;
}

.pull-quote::after {
    bottom: -11px;
    border-left: none;
    border-top: none;
}

/* ==========================================================================
   Apparatus list
   ========================================================================== */
.apparatus-list {
    list-style: none;
    margin: clamp(1.5rem, 4vh, 2.5rem) 0;
    padding: 0;
}

.apparatus-list li {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    padding: 0.75rem 0;
    border-bottom: 1px solid rgba(196, 169, 90, 0.3);
    font-family: var(--font-body);
    font-size: clamp(0.95rem, 1.6vw, 1.1rem);
    line-height: 1.6;
    color: var(--ink);
}

.apparatus-list li:last-child { border-bottom: none; }

.apparatus-icon {
    flex-shrink: 0;
    width: 24px;
    height: 24px;
    color: var(--sienna);
    margin-top: 0.15em;
    display: inline-block;
}

.apparatus-icon svg {
    width: 100%;
    height: 100%;
}

/* ==========================================================================
   Nocturnal Folio (dark section)
   ========================================================================== */
.chapter-nocturnal {
    background: var(--walnut);
    color: var(--parchment);
    margin: clamp(3rem, 8vh, 6rem) calc(-1 * clamp(2rem, 8vw, 6rem));
    padding: clamp(5rem, 12vh, 9rem) clamp(2rem, 8vw, 6rem);
    position: relative;
    box-shadow:
        0 0 60px rgba(30, 24, 18, 0.5),
        inset 0 0 80px rgba(122, 46, 46, 0.15);
}

.nocturnal-gradient {
    position: absolute;
    top: -100px;
    left: 0;
    right: 0;
    height: 100px;
    background: linear-gradient(to bottom, transparent 0%, var(--walnut) 100%);
    pointer-events: none;
}

.chapter-nocturnal::after {
    content: "";
    position: absolute;
    bottom: -100px;
    left: 0;
    right: 0;
    height: 100px;
    background: linear-gradient(to top, transparent 0%, var(--walnut) 100%);
    pointer-events: none;
}

.chapter-nocturnal .chapter-mark {
    color: var(--gilt);
}

.chapter-nocturnal .chapter-title {
    color: var(--parchment);
}

.chapter-nocturnal .chapter-body {
    color: var(--parchment);
}

.chapter-nocturnal .chapter-body p {
    color: rgba(245, 239, 227, 0.88);
}

.chapter-nocturnal .dropcap {
    color: var(--gold);
    border-color: var(--gilt);
    background: linear-gradient(135deg, rgba(184, 148, 62, 0.2) 0%, transparent 60%);
    text-shadow: 0 0 20px rgba(184, 148, 62, 0.3);
}

.chapter-nocturnal .pull-quote {
    color: var(--gilt);
    border-color: rgba(196, 169, 90, 0.5);
}

.chapter-nocturnal .pull-quote::before,
.chapter-nocturnal .pull-quote::after {
    background: var(--walnut);
    border-color: rgba(196, 169, 90, 0.5);
}

.nocturnal-figures {
    display: flex;
    justify-content: center;
    gap: clamp(1.5rem, 5vw, 3rem);
    margin: clamp(2rem, 5vh, 3.5rem) 0;
}

.nocturnal-figure {
    width: clamp(40px, 6vw, 56px);
    height: clamp(40px, 6vw, 56px);
    color: var(--gold);
    opacity: 0.85;
    transition: transform 400ms ease-out, opacity 400ms ease-out;
}

.nocturnal-figure svg {
    width: 100%;
    height: 100%;
}

.nocturnal-figure:hover {
    transform: scale(1.1);
    opacity: 1;
    color: var(--gilt);
}

/* ==========================================================================
   Colophon
   ========================================================================== */
.colophon {
    text-align: center;
    padding: clamp(6rem, 15vh, 10rem) 0 clamp(4rem, 10vh, 7rem);
}

.colophon-mark {
    width: clamp(60px, 10vw, 90px);
    height: clamp(60px, 10vw, 90px);
    margin: 0 auto clamp(1.5rem, 4vh, 3rem);
    color: var(--gold);
    opacity: 0.85;
    transition: transform 600ms ease-out;
}

.colophon-mark:hover {
    transform: rotate(180deg);
}

.colophon-mark svg {
    width: 100%;
    height: 100%;
}

.colophon-title {
    font-family: var(--font-display);
    font-weight: 600;
    font-style: italic;
    font-size: clamp(1.5rem, 3vw, 2.25rem);
    color: var(--oxblood);
    letter-spacing: 0.15em;
    text-transform: lowercase;
    font-variant: small-caps;
    margin-bottom: clamp(1.5rem, 4vh, 2.5rem);
}

.colophon-body {
    font-family: var(--font-body);
    font-size: clamp(0.95rem, 1.6vw, 1.1rem);
    line-height: 1.9;
    color: var(--ink);
    margin-bottom: 1.25em;
}

.small-caps {
    font-variant: small-caps;
    letter-spacing: 0.1em;
    font-weight: 600;
}

.swatch {
    display: inline-block;
    width: 0.75em;
    height: 0.75em;
    border-radius: 50%;
    vertical-align: middle;
    margin: 0 0.15em;
    border: 1px solid rgba(44, 36, 24, 0.3);
    box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2);
    transform: translateY(-1px);
}

.colophon-close {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin: clamp(2rem, 5vh, 3rem) auto;
    max-width: 50%;
}

.colophon-close .rule-line {
    flex: 1;
    height: 1px;
    background: var(--gilt);
    opacity: 0.6;
}

.colophon-fleur {
    font-size: 1.5em;
    color: var(--gold);
    line-height: 1;
}

.colophon-finis {
    font-family: var(--font-display);
    font-style: italic;
    font-size: clamp(1.1rem, 2vw, 1.5rem);
    color: var(--olive-gold);
    letter-spacing: 0.3em;
    font-variant: small-caps;
    text-transform: lowercase;
}

/* ==========================================================================
   Body link style
   ========================================================================== */
.chapter-body a,
.chapter-body em {
    color: var(--sienna);
}

.chapter-body a {
    text-decoration: none;
    background-image: linear-gradient(to right, var(--sienna), var(--sienna));
    background-size: 0% 1px;
    background-repeat: no-repeat;
    background-position: 0 100%;
    transition: background-size 300ms ease-out;
    padding-bottom: 1px;
}

.chapter-body a:hover {
    background-size: 100% 1px;
}

/* ==========================================================================
   Responsive behavior
   ========================================================================== */
@media (min-width: 1024px) {
    .margin {
        display: block;
    }
}

@media (max-width: 1023px) {
    :root {
        --column-width: clamp(320px, 80vw, 640px);
    }
    .chapter-nocturnal {
        margin-left: calc(-1 * clamp(1rem, 5vw, 3rem));
        margin-right: calc(-1 * clamp(1rem, 5vw, 3rem));
    }
}

@media (max-width: 767px) {
    :root {
        --column-width: min(88vw, 560px);
    }
    .frontispiece-frame {
        padding: 2rem 1rem;
    }
    .corner {
        width: 28px;
        height: 28px;
    }
    .chapter-nocturnal {
        margin-left: calc(-1 * 1rem);
        margin-right: calc(-1 * 1rem);
        padding-left: 1.25rem;
        padding-right: 1.25rem;
    }
    .dropcap {
        font-size: 4.2em;
    }
    .pull-quote {
        max-width: 100%;
    }
    body {
        line-height: 1.75;
    }
}

@media (max-width: 480px) {
    .title-dot { font-size: 0.5em; }
    .frontispiece-ornament svg { width: 40px; }
    .chapter-title { font-size: clamp(1.75rem, 8vw, 2.5rem); }
}
