:root {
    --canopy-black: #0B1F14;
    --theorem-green: #1A4D32;
    --morphism-sage: #2D6A4F;
    --proof-glow: #3DA37A;
    --leaf-light: #5ECFA0;
    --parchment-pale: #E8F0E4;
    --chalk-mist: #D4E7D0;
    --amber-annotation: #C9A84C;
    --card-back-bg: #1A5A3A;
    --card-border: #5B7A6E;
    --sub-heading: #8BAF80;
    --section-heading: #C8DCC0;
    --emphasized-code: #7AEBC4;
}

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

html {
    scroll-behavior: smooth;
}

body {
    background-color: var(--canopy-black);
    color: var(--chalk-mist);
    font-family: 'Space Mono', monospace;
    font-weight: 400;
    font-size: clamp(0.9rem, 1.1vw, 1.05rem);
    line-height: 1.75;
    letter-spacing: 0.01em;
    overflow-x: hidden;
}

/* Ribbon Layers */
.ribbon-layer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

.ribbon {
    position: absolute;
    white-space: nowrap;
    font-family: 'Space Mono', monospace;
    font-size: 0.7rem;
    color: var(--chalk-mist);
    opacity: 0.08;
}

.ribbon-1 {
    top: 25%;
    animation: ribbonScroll1 60s linear infinite;
}

.ribbon-2 {
    top: 50%;
    animation: ribbonScroll2 80s linear infinite;
}

.ribbon-3 {
    top: 75%;
    animation: ribbonScroll3 45s linear infinite;
}

@keyframes ribbonScroll1 {
    from { transform: translateX(0); }
    to { transform: translateX(-50%); }
}

@keyframes ribbonScroll2 {
    from { transform: translateX(-50%); }
    to { transform: translateX(0); }
}

@keyframes ribbonScroll3 {
    from { transform: translateX(-20%); }
    to { transform: translateX(-70%); }
}

/* Lambda Scroll Indicator */
.lambda-indicator {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 100;
    opacity: 0.4;
    transition: opacity 0.5s ease;
}

/* Lambda Background Watermarks */
.lambda-bg {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-family: 'Commissioner', sans-serif;
    font-weight: 800;
    font-size: 60vw;
    color: var(--morphism-sage);
    opacity: 0.04;
    pointer-events: none;
    z-index: 0;
    line-height: 1;
}

.lambda-bg-1 { transform: translate(-50%, -50%) rotate(3deg); }
.lambda-bg-2 { transform: translate(-50%, -50%) rotate(-5deg); }
.lambda-bg-3 { transform: translate(-50%, -50%) rotate(7deg); }
.lambda-bg-4 { transform: translate(-50%, -50%) rotate(-2deg); }
.lambda-bg-5 { transform: translate(-50%, -50%) rotate(4deg); }

/* Chapters */
.chapter {
    position: relative;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.chapter-theorems {
    min-height: 150vh;
}

.chapter-content {
    position: relative;
    z-index: 1;
    max-width: 720px;
    width: 100%;
    margin: 0 auto;
    padding: 80px 24px;
}

/* PREAMBLE */
.preamble-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    min-height: 100vh;
}

.hero-title {
    font-family: 'Commissioner', sans-serif;
    font-weight: 800;
    font-size: clamp(2.5rem, 6vw, 5.5rem);
    letter-spacing: -0.025em;
    line-height: 1.05;
    color: var(--parchment-pale);
    opacity: 0;
    transition: opacity 1s cubic-bezier(0.25, 0.1, 0.25, 1);
}

.hero-title.visible {
    opacity: 1;
}

.type-signature {
    font-family: 'Space Mono', monospace;
    font-weight: 400;
    font-size: clamp(0.85rem, 1.2vw, 1.1rem);
    color: var(--leaf-light);
    margin-top: 24px;
    line-height: 1.6;
    min-height: 1.6em;
}

.scroll-chevron {
    position: absolute;
    bottom: 48px;
    opacity: 0.5;
    animation: chevronPulse 2s ease-in-out infinite;
}

@keyframes chevronPulse {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 0.7; }
}

/* Section Headings */
.section-heading {
    font-family: 'Commissioner', sans-serif;
    font-weight: 600;
    font-size: clamp(1.5rem, 3.5vw, 3rem);
    letter-spacing: -0.01em;
    line-height: 1.15;
    color: var(--section-heading);
    margin-bottom: 60px;
    text-align: center;
}

/* Axiom Blocks */
.axioms-container {
    display: flex;
    flex-direction: column;
    gap: 48px;
}

.axiom-block {
    border-left: 1px solid var(--proof-glow);
    padding-left: 24px;
    opacity: 0;
    transform: translateY(30px);
    transition: opacity 600ms cubic-bezier(0.25, 0.1, 0.25, 1), transform 600ms cubic-bezier(0.25, 0.1, 0.25, 1);
}

.axiom-block.revealed {
    opacity: 1;
    transform: translateY(0);
}

.axiom-title {
    font-family: 'Commissioner', sans-serif;
    font-weight: 600;
    font-size: 1.4rem;
    color: var(--section-heading);
    margin-bottom: 12px;
}

.axiom-body {
    font-family: 'Space Mono', monospace;
    font-weight: 400;
    font-size: 0.95rem;
    line-height: 1.75;
    color: var(--chalk-mist);
}

/* Commutative Diagrams */
.commutative-diagram {
    display: flex;
    justify-content: center;
    padding: 16px 0;
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 500ms cubic-bezier(0.25, 0.1, 0.25, 1), transform 500ms cubic-bezier(0.25, 0.1, 0.25, 1);
}

.commutative-diagram.revealed {
    opacity: 1;
    transform: translateY(0);
}

.diagram-arrow {
    stroke-dasharray: 200;
    stroke-dashoffset: 200;
    transition: stroke-dashoffset 400ms ease;
}

.commutative-diagram.revealed .diagram-arrow {
    stroke-dashoffset: 0;
}

.commutative-diagram.revealed .diagram-arrow:nth-child(4) {
    transition-delay: 200ms;
}

.commutative-diagram.revealed .diagram-arrow:nth-child(5) {
    transition-delay: 400ms;
}

.diagram-label {
    font-family: 'Commissioner', sans-serif;
    font-weight: 200;
    font-size: 0.65rem;
    fill: var(--sub-heading);
}

/* Theorem Cards */
.theorems-container {
    display: flex;
    flex-direction: column;
    gap: 80px;
}

.card {
    perspective: 1200px;
    width: 100%;
    max-width: 460px;
    aspect-ratio: 3 / 2;
    margin: 0 auto;
    cursor: pointer;
    opacity: 0;
    transform: translateY(30px);
    transition: opacity 600ms cubic-bezier(0.25, 0.1, 0.25, 1), transform 600ms cubic-bezier(0.25, 0.1, 0.25, 1);
}

.card.revealed {
    opacity: 1;
    transform: translateY(0);
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    transition: transform 600ms cubic-bezier(0.4, 0, 0.2, 1);
    transform-style: preserve-3d;
}

.card:hover .card-inner,
.card.flipped .card-inner {
    transform: rotateY(180deg);
    box-shadow: 0 0 30px rgba(61, 163, 122, 0.15);
}

.card-front, .card-back {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border: 1px solid var(--card-border);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 32px;
    border-radius: 4px;
}

.card-front {
    background-color: var(--theorem-green);
}

.card-back {
    background-color: var(--card-back-bg);
    transform: rotateY(180deg);
}

.card-title {
    font-family: 'Commissioner', sans-serif;
    font-weight: 700;
    font-size: 1.6rem;
    color: var(--parchment-pale);
    margin-bottom: 16px;
}

.card-type-sig {
    font-family: 'Space Mono', monospace;
    font-weight: 700;
    font-size: 0.85rem;
    color: var(--emphasized-code);
    line-height: 1.6;
    text-align: center;
}

.card-explanation {
    font-family: 'Space Mono', monospace;
    font-weight: 400;
    font-size: 0.85rem;
    color: var(--chalk-mist);
    line-height: 1.7;
    text-align: center;
}

.amber-highlight {
    color: var(--amber-annotation);
    font-weight: 700;
}

/* Composition */
.golden-spiral {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 80%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.composition-diagram {
    margin: 40px 0;
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 800ms cubic-bezier(0.25, 0.1, 0.25, 1), transform 800ms cubic-bezier(0.25, 0.1, 0.25, 1);
}

.composition-diagram.revealed {
    opacity: 1;
    transform: translateY(0);
}

.comp-arrow {
    stroke-dasharray: 300;
    stroke-dashoffset: 300;
}

.composition-diagram.revealed .comp-arrow {
    animation: drawArrow 600ms ease forwards;
}

.composition-diagram.revealed .comp-arrow:nth-child(4) { animation-delay: 300ms; }
.composition-diagram.revealed .comp-arrow:nth-child(5) { animation-delay: 600ms; }
.composition-diagram.revealed .comp-arrow:nth-child(6) { animation-delay: 900ms; }
.composition-diagram.revealed .comp-arrow:nth-child(7) { animation-delay: 1200ms; }
.composition-diagram.revealed .comp-arrow:nth-child(8) { animation-delay: 1500ms; }
.composition-diagram.revealed .comp-arrow:nth-child(9) { animation-delay: 1800ms; }
.composition-diagram.revealed .comp-arrow:nth-child(10) { animation-delay: 2100ms; }

@keyframes drawArrow {
    to { stroke-dashoffset: 0; }
}

.comp-label {
    font-family: 'Commissioner', sans-serif;
    font-weight: 300;
    font-size: 0.8rem;
    fill: var(--sub-heading);
    opacity: 0;
}

.composition-diagram.revealed .comp-label {
    animation: fadeInLabel 400ms ease forwards;
}

.composition-diagram.revealed .comp-label:nth-of-type(1) { animation-delay: 600ms; }
.composition-diagram.revealed .comp-label:nth-of-type(2) { animation-delay: 900ms; }
.composition-diagram.revealed .comp-label:nth-of-type(3) { animation-delay: 1200ms; }
.composition-diagram.revealed .comp-label:nth-of-type(4) { animation-delay: 1500ms; }
.composition-diagram.revealed .comp-label:nth-of-type(5) { animation-delay: 1800ms; }
.composition-diagram.revealed .comp-label:nth-of-type(6) { animation-delay: 2100ms; }
.composition-diagram.revealed .comp-label:nth-of-type(7) { animation-delay: 2400ms; }

@keyframes fadeInLabel {
    to { opacity: 1; }
}

.composition-text {
    font-family: 'Space Mono', monospace;
    font-weight: 400;
    font-size: 0.95rem;
    line-height: 1.75;
    color: var(--chalk-mist);
    margin-top: 48px;
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 600ms cubic-bezier(0.25, 0.1, 0.25, 1), transform 600ms cubic-bezier(0.25, 0.1, 0.25, 1);
}

.composition-text.revealed {
    opacity: 1;
    transform: translateY(0);
}

/* QED */
.qed-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    min-height: 100vh;
}

.qed-statement {
    font-family: 'Commissioner', sans-serif;
    font-weight: 800;
    font-size: 2.5rem;
    line-height: 1.2;
    color: var(--parchment-pale);
    max-width: 600px;
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 800ms cubic-bezier(0.25, 0.1, 0.25, 1), transform 800ms cubic-bezier(0.25, 0.1, 0.25, 1);
}

.qed-statement.revealed {
    opacity: 1;
    transform: translateY(0);
}

.qed-marker {
    width: 16px;
    height: 16px;
    background-color: var(--amber-annotation);
    margin-top: 48px;
    opacity: 0;
    transition: opacity 1s cubic-bezier(0.25, 0.1, 0.25, 1);
}

.qed-marker.revealed {
    opacity: 1;
}

/* QED background warm transition */
#qed {
    transition: background-color 1s ease;
}

/* Responsive */
@media (max-width: 768px) {
    .chapter-content {
        padding: 60px 16px;
    }

    .card {
        max-width: 100%;
    }

    .qed-statement {
        font-size: 1.8rem;
    }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
    .ribbon { animation: none; }
    .lambda-bg { display: none; }
    .scroll-chevron { animation: none; opacity: 0.5; }

    .axiom-block,
    .commutative-diagram,
    .card,
    .composition-diagram,
    .composition-text,
    .qed-statement,
    .qed-marker {
        opacity: 1;
        transform: none;
        transition: none;
    }

    .diagram-arrow {
        stroke-dashoffset: 0;
        transition: none;
    }

    .comp-arrow {
        stroke-dashoffset: 0;
        animation: none;
    }

    .comp-label {
        opacity: 1;
        animation: none;
    }

    .card-inner {
        transition: none;
    }
}
