/* ===== CSS Custom Properties ===== */
:root {
    --primary-dark: #0d0a07;
    --secondary-dark: #1a1714;
    --parchment: #f5ece0;
    --gold: #c9a94e;
    --copper: #9c6b3a;
    --wax: #8b2500;
    --aged-ivory: #d4c8b0;
    --glitch-red: #ff3d2e;
    --glitch-cyan: #2efff5;

    --font-display: 'Bodoni Moda', serif;
    --font-body: 'Libre Baskerville', serif;
    --font-accent: 'Space Grotesk', sans-serif;

    --h-pad: clamp(2rem, 8vw, 10rem);
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    scroll-snap-type: y proximity;
    scroll-behavior: smooth;
}

body {
    background: var(--primary-dark);
    color: var(--aged-ivory);
    overflow-x: hidden;
    font-family: var(--font-body);
}

.svg-defs {
    position: absolute;
    width: 0;
    height: 0;
    overflow: hidden;
}

/* ===== Scan-line Overlay ===== */
#scanlines {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1000;
    background: repeating-linear-gradient(
        0deg,
        transparent 0px,
        transparent 2px,
        rgba(13, 10, 7, 0.08) 2px,
        rgba(13, 10, 7, 0.08) 4px
    );
}

/* ===== Pen Progress Indicator ===== */
#pen-progress {
    position: fixed;
    right: 1.5rem;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 400px;
    z-index: 999;
    opacity: 0.7;
}

#pen-progress-svg {
    width: 100%;
    height: 100%;
}

/* ===== Movement Base ===== */
.movement {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 var(--h-pad);
    position: relative;
    overflow: hidden;
}

.movement-inner {
    width: 100%;
    max-width: 1200px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
}

/* ===== Movement I — The Uncapping ===== */
#movement-1 {
    min-height: 100vh;
    background: var(--primary-dark);
    scroll-snap-align: start;
}

#nib-slit {
    position: absolute;
    width: 80%;
    height: 2px;
    top: 50%;
    left: 10%;
    transform: translateY(-50%);
    z-index: 5;
}

#nib-line {
    stroke-dasharray: 800;
    stroke-dashoffset: 800;
    animation: drawNibLine 2.4s cubic-bezier(0.25, 0, 0.1, 1) 1.2s forwards;
}

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

#leather-reveal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    clip-path: inset(49.5% 0 49.5% 0);
    animation: revealLeather 3.5s cubic-bezier(0.25, 0, 0.1, 1) 1.8s forwards;
    z-index: 2;
}

@keyframes revealLeather {
    to { clip-path: inset(0 0 0 0); }
}

#leather-surface {
    width: 100%;
    height: 100%;
    background-color: var(--secondary-dark);
    filter: url(#leather-texture);
    background-image:
        repeating-linear-gradient(2deg, transparent, transparent 47px, rgba(156, 107, 58, 0.04) 47px, rgba(156, 107, 58, 0.04) 48px),
        repeating-linear-gradient(-1deg, transparent, transparent 89px, rgba(156, 107, 58, 0.03) 89px, rgba(156, 107, 58, 0.03) 90px),
        repeating-linear-gradient(0.5deg, transparent, transparent 131px, rgba(156, 107, 58, 0.05) 131px, rgba(156, 107, 58, 0.05) 132px);
}

#domain-name {
    position: relative;
    z-index: 10;
    font-size: clamp(3rem, 10vw, 10rem);
    line-height: 0.95;
    letter-spacing: -0.02em;
}

.domain-pen {
    font-family: var(--font-accent);
    font-weight: 500;
    color: var(--aged-ivory);
}

.domain-closer {
    font-family: var(--font-display);
    font-weight: 400;
    font-style: italic;
    color: var(--gold);
}

.letter {
    display: inline-block;
    opacity: 0;
    transform: translateX(calc(var(--random-x, 3px)));
    animation: letterReveal 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
    animation-delay: calc(2.8s + var(--i) * 0.06s);
}

@keyframes letterReveal {
    0% { opacity: 0; transform: translateX(var(--random-x, 3px)); }
    50% { opacity: 1; transform: translateX(calc(var(--random-x, 3px) * -0.3)); }
    100% { opacity: 1; transform: translateX(0); }
}

/* Chromatic effect on letters during animation */
.letter.glitch-active {
    text-shadow:
        -2px 0 var(--glitch-red),
        2px 0 var(--glitch-cyan);
}

/* ===== Movement II — The Instrument ===== */
#movement-2 {
    min-height: 120vh;
    background: linear-gradient(180deg, var(--primary-dark) 0%, var(--secondary-dark) 100%);
    scroll-snap-align: start;
}

#instrument-title {
    font-family: var(--font-display);
    font-weight: 900;
    font-style: italic;
    font-size: clamp(3rem, 12vw, 12rem);
    letter-spacing: -0.02em;
    line-height: 0.95;
    color: var(--secondary-dark);
    transition: color 1.2s ease;
    will-change: transform, opacity;
    text-align: center;
}

#instrument-title.visible {
    color: var(--aged-ivory);
}

.chevron-border {
    width: 100%;
    height: 30px;
    opacity: 0.4;
    margin: 2rem 0;
}

.draw-path {
    stroke-dasharray: 2000;
    stroke-dashoffset: 2000;
    transition: stroke-dashoffset 3s ease;
}

.draw-path.animate {
    stroke-dashoffset: 0;
}

/* Vignettes */
.vignette {
    position: absolute;
    width: 120px;
    height: 120px;
    border-radius: 50%;
    overflow: hidden;
}

.vignette-1 { top: 20%; left: 12%; }
.vignette-2 { top: 55%; right: 15%; }
.vignette-3 { bottom: 15%; left: 30%; }

.vignette-inner {
    width: 100%;
    height: 100%;
    background-color: var(--secondary-dark);
    filter: url(#leather-texture);
    border-radius: 50%;
}

.chromatic-red, .chromatic-blue {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: var(--secondary-dark);
    filter: url(#leather-texture);
    mix-blend-mode: screen;
    opacity: 0.4;
}

.chromatic-red {
    transform: translateX(-2px);
    background-blend-mode: multiply;
    opacity: 0.15;
    background-color: var(--glitch-red);
}

.chromatic-blue {
    transform: translateX(2px);
    opacity: 0.15;
    background-color: var(--glitch-cyan);
}

/* Guilloche */
.guilloche {
    position: absolute;
    left: 0;
    top: 0;
    width: 60px;
    height: 100%;
    opacity: 0;
    transition: opacity 1.5s ease;
}

.guilloche.visible {
    opacity: 1;
}

/* ===== Movement III — The Letter ===== */
#movement-3 {
    min-height: 140vh;
    background: var(--secondary-dark);
    scroll-snap-align: start;
    position: relative;
}

#movement-3.parchment-reveal {
    background: var(--parchment);
    transition: background 3s ease;
}

#herringbone-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0.03;
    background:
        repeating-linear-gradient(45deg, transparent, transparent 10px, var(--copper) 10px, var(--copper) 11px),
        repeating-linear-gradient(-45deg, transparent, transparent 10px, var(--copper) 10px, var(--copper) 11px);
    pointer-events: none;
    z-index: 1;
}

.fan-motif {
    width: 120px;
    height: 60px;
    opacity: 0;
    transition: opacity 1.5s ease;
    z-index: 2;
    margin: 1rem 0;
}

.fan-motif.visible {
    opacity: 0.6;
}

.fan-ray {
    stroke-dasharray: 100;
    stroke-dashoffset: 100;
    transition: stroke-dashoffset 0.6s ease;
    transition-delay: calc(var(--ray) * 0.1s);
}

.fan-motif.visible .fan-ray {
    stroke-dashoffset: 0;
}

#letter-container {
    position: relative;
    width: 100%;
    max-width: 600px;
    z-index: 5;
    margin: 2rem 0;
}

#handwriting {
    width: 100%;
    height: auto;
    display: block;
}

.hw-line {
    stroke-dasharray: 1000;
    stroke-dashoffset: 1000;
    transition: stroke-dashoffset 3s ease;
    will-change: stroke-dashoffset;
}

.hw-line.draw {
    stroke-dashoffset: 0;
}

.hw-line.aged {
    filter: blur(0.3px);
    stroke: var(--secondary-dark);
    opacity: 0.7;
}

#letter-text {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding: 1.5rem 2.5rem;
}

.letter-line {
    font-family: var(--font-body);
    font-size: clamp(1rem, 2.2vw, 1.5rem);
    font-weight: 400;
    font-style: italic;
    letter-spacing: 0.01em;
    line-height: 1.75;
    color: var(--secondary-dark);
    opacity: 0;
    transition: opacity 1s ease 0.5s;
}

.letter-line.visible {
    opacity: 1;
}

.letter-line.aged {
    text-shadow:
        -1px 0 rgba(255, 61, 46, 0.2),
        1px 0 rgba(46, 255, 245, 0.2);
    filter: blur(0.3px);
    transition: all 2s ease;
}

/* ===== Movement IV — The Closing ===== */
#movement-4 {
    min-height: 80vh;
    background: radial-gradient(circle at 50% 50%, var(--secondary-dark) 0%, var(--primary-dark) 100%);
    scroll-snap-align: start;
}

#pen-illustration {
    width: min(80vw, 500px);
    height: auto;
    z-index: 5;
}

#pen-cap-illustration {
    will-change: transform;
    transition: none;
}

#ripple-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 3;
}

.ripple {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    border: 1px solid var(--gold);
    opacity: 0.15;
    animation: rippleExpand 0.6s ease-out forwards;
}

@keyframes rippleExpand {
    0% { width: 0; height: 0; opacity: 0.15; }
    100% { width: 120vmax; height: 120vmax; opacity: 0; }
}

#fan-motif-bottom {
    margin-top: 2rem;
}

/* ===== Movement V — The Seal ===== */
#movement-5 {
    min-height: 60vh;
    background: var(--primary-dark);
    scroll-snap-align: start;
}

#wax-seal {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    background: radial-gradient(circle at 40% 35%, #b33a1a, var(--wax) 60%, #5a1500);
    box-shadow:
        inset 0 2px 6px rgba(0, 0, 0, 0.5),
        0 2px 8px rgba(0, 0, 0, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 2s ease;
    margin-bottom: 2rem;
    /* Herringbone pattern overlay */
    position: relative;
}

#wax-seal::before {
    content: '';
    position: absolute;
    top: 5px;
    left: 5px;
    right: 5px;
    bottom: 5px;
    border-radius: 50%;
    border: 2px solid rgba(201, 169, 78, 0.3);
}

#wax-seal.visible {
    opacity: 0.3;
}

#seal-inner {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border: 1px solid rgba(201, 169, 78, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
}

#seal-initials {
    font-family: var(--font-accent);
    font-size: 1.4rem;
    font-weight: 500;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    color: rgba(201, 169, 78, 0.5);
}

#domain-name-final {
    font-size: clamp(2rem, 6vw, 5rem);
    line-height: 0.95;
    letter-spacing: -0.02em;
    opacity: 0;
    transition: opacity 2s ease 0.5s;
}

#domain-name-final.visible {
    opacity: 1;
}

.domain-pen-final {
    font-family: var(--font-accent);
    font-weight: 500;
    color: var(--aged-ivory);
}

.domain-closer-final {
    font-family: var(--font-display);
    font-weight: 400;
    font-style: italic;
    color: var(--gold);
}

/* ===== Glitch Burst ===== */
.glitch-burst {
    animation: glitchBurst 0.15s step-end;
}

@keyframes glitchBurst {
    0% { transform: translateX(5px); }
    25% { transform: translateX(-3px); }
    50% { transform: translateX(6px); }
    75% { transform: translateX(-2px); }
    100% { transform: translateX(0); }
}

/* ===== Displacement on movement entry ===== */
.displacement {
    animation: displace 0.18s step-end;
}

@keyframes displace {
    0% { transform: translateX(4px); }
    33% { transform: translateX(-3px); }
    66% { transform: translateX(2px); }
    100% { transform: translateX(0); }
}
