:root {
    --deep-parchment: #F2E8D5;
    --ink-sepia: #3B2F20;
    --faded-notation: #7A6B5A;
    --beeswax-gold: #C4963A;
    --burnt-umber: #6B3A2A;
    --foxing-spot: #D4C4A8;
    --candleshadow: #2A2014;
    --static-green: #7A9E7E;
    --elastic: cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
    background: var(--deep-parchment);
    color: var(--ink-sepia);
    font-family: 'Cormorant Garamond', serif;
    font-weight: 400;
    font-size: clamp(1.05rem, 1.8vw, 1.25rem);
    line-height: 1.85;
    letter-spacing: 0.01em;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

/* Paper grain overlay */
body::before {
    content: '';
    position: fixed;
    inset: 0;
    background:
        repeating-linear-gradient(
            135deg,
            transparent,
            transparent 2px,
            rgba(212, 196, 168, 0.02) 2px,
            rgba(212, 196, 168, 0.02) 3px
        );
    pointer-events: none;
    z-index: 1000;
}

/* Vignette */
body::after {
    content: '';
    position: fixed;
    inset: 0;
    background: radial-gradient(
        ellipse at 50% 40%,
        transparent 40%,
        rgba(42, 32, 20, 0.15) 100%
    );
    pointer-events: none;
    z-index: 999;
}

/* Scan Line */
.scan-line {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: var(--static-green);
    opacity: 0.04;
    z-index: 1001;
    animation: scan-drift 12s linear infinite;
    pointer-events: none;
}

@keyframes scan-drift {
    0% { top: -2px; }
    100% { top: 100vh; }
}

/* Candle Progress Indicator */
.candle-progress {
    position: fixed;
    right: 24px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 120px;
    z-index: 998;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.candle-body {
    width: 6px;
    flex: 1;
    background: var(--burnt-umber);
    border-radius: 1px;
    position: relative;
}

.candle-flame {
    width: 8px;
    height: 12px;
    background: var(--beeswax-gold);
    border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
    position: relative;
    top: -2px;
    animation: flame-flicker 2.4s ease-in-out infinite;
}

@keyframes flame-flicker {
    0% { transform: scaleY(1) scaleX(1); opacity: 0.9; }
    37% { transform: scaleY(1.1) scaleX(0.92) translateX(1px); opacity: 1; }
    68% { transform: scaleY(0.9) scaleX(1.05) translateX(-1px); opacity: 0.85; }
    100% { transform: scaleY(1) scaleX(1); opacity: 0.9; }
}

/* Section Base */
.ms-section {
    min-height: 80vh;
    padding: clamp(3rem, 8vh, 6rem) clamp(1.5rem, 4vw, 4rem);
    display: flex;
    position: relative;
}

/* The Manuscript Column */
.ms-column {
    width: clamp(320px, 55vw, 680px);
    margin-left: auto;
    margin-right: 30%;
}

.ms-narrow {
    width: clamp(280px, 48vw, 580px);
}

/* Title Page */
.title-page {
    min-height: 100vh;
    align-items: center;
}

.ms-title {
    font-family: 'Playfair Display', serif;
    font-weight: 900;
    font-size: clamp(4rem, 12vw, 9rem);
    color: var(--ink-sepia);
    letter-spacing: 0.02em;
    line-height: 1.1;
    margin-bottom: 1.5rem;
}

.ms-subtitle {
    font-family: 'Cormorant Garamond', serif;
    font-weight: 400;
    font-style: italic;
    font-size: clamp(1.1rem, 2vw, 1.35rem);
    color: var(--faded-notation);
    line-height: 1.85;
    max-width: 45ch;
    margin-bottom: 2rem;
}

.ms-heading {
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    font-size: clamp(1.6rem, 3.5vw, 2.8rem);
    color: var(--burnt-umber);
    letter-spacing: 0.02em;
    line-height: 1.1;
    margin-bottom: clamp(1rem, 3vh, 2rem);
}

.ms-body {
    margin-bottom: clamp(1.5rem, 4vh, 3rem);
    max-width: 55ch;
}

.ms-italic {
    font-style: italic;
}

/* Data Labels (Space Mono) */
.data-label {
    display: block;
    font-family: 'Space Mono', monospace;
    font-weight: 400;
    font-size: clamp(0.75rem, 1.2vw, 0.9rem);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--static-green);
    opacity: 0.5;
    margin-bottom: 1.5rem;
}

/* Margin Canal */
.margin-canal {
    position: absolute;
    right: clamp(2rem, 5vw, 6rem);
    top: 50%;
    transform: translateY(-50%);
    width: clamp(60px, 8vw, 120px);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
}

.margin-canal.margin-active {
    gap: 1rem;
}

.margin-candle {
    width: 20px;
    height: auto;
}

.margin-candle.candle-sm {
    width: 16px;
}

.margin-candle.candle-stub {
    width: 18px;
}

.candle-flame-shape {
    animation: flame-flicker 2.4s ease-in-out infinite;
    transform-origin: center bottom;
}

.margin-candle.candle-sm .candle-flame-shape {
    animation-delay: -0.8s;
}

.flame-gutter {
    animation: gutter-flame 1.6s ease-in-out infinite;
}

@keyframes gutter-flame {
    0% { transform: scaleY(1) scaleX(1); opacity: 0.7; }
    25% { transform: scaleY(0.7) scaleX(1.15) translateX(1.5px); opacity: 0.9; }
    50% { transform: scaleY(1.15) scaleX(0.8) translateX(-1px); opacity: 0.6; }
    75% { transform: scaleY(0.85) scaleX(1.1) translateX(0.5px); opacity: 0.8; }
    100% { transform: scaleY(1) scaleX(1); opacity: 0.7; }
}

.margin-annotation {
    font-family: 'Cormorant Garamond', serif;
    font-weight: 400;
    font-style: italic;
    font-size: clamp(0.8rem, 1.2vw, 0.95rem);
    color: var(--faded-notation);
    text-align: center;
    line-height: 1.6;
}

/* Page Fold Dividers */
.page-fold {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(
        to bottom,
        transparent,
        rgba(59, 47, 32, 0.08) 30%,
        rgba(59, 47, 32, 0.15) 50%,
        rgba(59, 47, 32, 0.08) 70%,
        transparent
    );
    display: flex;
    align-items: center;
    justify-content: center;
}

.fold-seal {
    display: block;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: radial-gradient(circle at 40% 35%,
        #8B4A30,
        var(--burnt-umber) 60%,
        #4A2215
    );
    box-shadow: 0 1px 3px rgba(42, 32, 20, 0.2);
    position: relative;
    top: -8px;
}

.fold-drip {
    display: block;
    width: 6px;
    height: 14px;
    background: var(--burnt-umber);
    border-radius: 3px 3px 50% 50%;
    position: relative;
    top: -6px;
    opacity: 0.6;
}

/* Watermark */
.watermark {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(15deg);
    width: clamp(200px, 40vw, 400px);
    height: auto;
    opacity: 0.04;
    pointer-events: none;
}

.watermark svg {
    width: 100%;
    height: auto;
}

/* Foxing Spots for Marginalia Section */
.marginalia-section::before {
    content: '';
    position: absolute;
    inset: 0;
    background:
        radial-gradient(circle at 15% 25%, var(--foxing-spot) 0%, transparent 3%),
        radial-gradient(circle at 72% 18%, var(--foxing-spot) 0%, transparent 2.5%),
        radial-gradient(circle at 45% 65%, var(--foxing-spot) 0%, transparent 4%),
        radial-gradient(circle at 82% 55%, var(--foxing-spot) 0%, transparent 3%),
        radial-gradient(circle at 28% 80%, var(--foxing-spot) 0%, transparent 2%),
        radial-gradient(circle at 60% 40%, var(--foxing-spot) 0%, transparent 3.5%),
        radial-gradient(circle at 90% 75%, var(--foxing-spot) 0%, transparent 2.5%);
    opacity: 0.12;
    pointer-events: none;
}

/* Diagram Section */
.diagram-section {
    min-height: 100vh;
    align-items: center;
    justify-content: center;
    padding: 2rem;
}

.diagram-canvas {
    width: 100%;
    max-width: 700px;
    margin: 0 auto;
}

.schematic {
    width: 100%;
    height: auto;
}

.diagram-el {
    opacity: 0;
    transition: opacity 600ms var(--elastic);
}

.diagram-el.visible {
    opacity: 1;
}

.diagram-line {
    stroke-dasharray: 200;
    stroke-dashoffset: 200;
    transition: stroke-dashoffset 1.2s ease;
}

.diagram-line.drawn {
    stroke-dashoffset: 0;
}

.node-glow {
    animation: glow-pulse 4s ease-in-out infinite;
}

@keyframes glow-pulse {
    0%, 100% { opacity: 0.08; }
    50% { opacity: 0.15; }
}

/* Final Entry */
.final-entry {
    min-height: 60vh;
    align-items: center;
}

.final-entry::after {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(
        ellipse at 50% 40%,
        transparent 30%,
        rgba(42, 32, 20, 0.25) 100%
    );
    pointer-events: none;
}

/* Colophon */
.colophon {
    min-height: 40vh;
    align-items: center;
    justify-content: center;
    background: linear-gradient(to bottom, var(--deep-parchment), var(--candleshadow));
}

.colophon-content {
    text-align: center;
    position: relative;
    z-index: 2;
}

.colophon-title {
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    font-size: clamp(1.4rem, 3vw, 2.2rem);
    color: var(--foxing-spot);
    margin-bottom: 1.5rem;
}

.candle-stub-final {
    width: 30px;
    margin: 0 auto 1.5rem;
}

.colophon-stamp {
    color: var(--static-green);
    opacity: 0.3;
}

/* Elastic Entrance Animation */
.elastic-in {
    opacity: 0;
    transform: translateY(30px);
    transition:
        opacity 600ms ease,
        transform 800ms var(--elastic);
}

.elastic-in.visible {
    opacity: 1;
    transform: translateY(0);
}

/* Stagger Delays */
.elastic-in:nth-child(2) { transition-delay: 120ms; }
.elastic-in:nth-child(3) { transition-delay: 240ms; }
.elastic-in:nth-child(4) { transition-delay: 360ms; }
.elastic-in:nth-child(5) { transition-delay: 480ms; }
.elastic-in:nth-child(6) { transition-delay: 600ms; }

/* Responsive */
@media (max-width: 768px) {
    .ms-column {
        width: 100%;
        margin-left: 0;
        margin-right: 0;
    }

    .ms-narrow {
        width: 100%;
    }

    .margin-canal {
        position: relative;
        right: auto;
        top: auto;
        transform: none;
        width: 100%;
        flex-direction: row;
        justify-content: center;
        margin-top: 2rem;
    }

    .candle-progress {
        display: none;
    }

    .ms-section {
        flex-direction: column;
    }

    .watermark {
        display: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    .scan-line { animation: none; display: none; }
    .candle-flame { animation: none; }
    .candle-flame-shape { animation: none; }
    .flame-gutter { animation: none; }
    .node-glow { animation: none; }
    .elastic-in {
        opacity: 1;
        transform: none;
        transition: none;
    }
    .diagram-el { opacity: 1; transition: none; }
    .diagram-line { stroke-dashoffset: 0; transition: none; }
}
