/* ═══════════════════════════════════════════
   graduation-glow-v2 — beat 5: the-letter
   Gold ink on midnight stationery. State classes on
   .the-letter-container, toggled by the beat's cues and handler:
   is-dim · is-arrived · is-settled · is-intro · is-hinting · is-open ·
   is-reading · is-folded · is-sealed · is-rising · is-gone
   ═══════════════════════════════════════════ */

.the-letter-container {
    position: absolute;
    inset: 0;
    overflow: visible;
    color: var(--text-primary);
    font-family: var(--font-body);
}

/* ───── The sky dims (and warms back after the fold) ───── */
.the-letter-dim {
    position: absolute;
    inset: -30%;
    z-index: 0;
    pointer-events: none;
    background: radial-gradient(ellipse at 50% 46%, rgba(5, 5, 17, 0.34) 0%, rgba(5, 5, 17, 0.8) 70%);
    opacity: 0;
    transition: opacity 6s var(--ease-silk);
}
.the-letter-container.is-dim .the-letter-dim {
    opacity: 1;
    transition-duration: 2.4s;
}

/* ───── "{fromName} wrote you something." ───── */
.the-letter-intro {
    position: absolute;
    z-index: 3;
    left: 50%;
    top: calc(13vh + env(safe-area-inset-top, 0px));
    width: min(86vw, 26rem);
    margin: 0;
    text-align: center;
    font: italic 400 clamp(1.15rem, 5vw, 1.45rem)/1.35 var(--font-display);
    color: var(--text-primary);
    text-shadow: 0 0 18px var(--accent-warm-glow-soft);
    opacity: 0;
    transform: translate(-50%, 10px);
    transition: opacity 1.2s var(--ease-expo-out), transform 1.2s var(--ease-expo-out);
    pointer-events: none;
}
.the-letter-container.is-intro .the-letter-intro {
    opacity: 1;
    transform: translate(-50%, 0);
}
.the-letter-container.is-open .the-letter-intro,
.the-letter-container.is-folded .the-letter-intro {
    opacity: 0;
    transition-duration: 0.8s;
}

/* ───── The rig: envelope and sheet share one centred cell ───── */
.the-letter-rig {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: grid;
    place-items: center;
    padding-bottom: 6vh;
    pointer-events: none;
}

/* ───── The envelope ───── */
.the-letter-envelope {
    grid-area: 1 / 1;
    position: relative;
    z-index: 2;
    width: min(72vw, 19rem);
    aspect-ratio: 3 / 2;
    perspective: 900px;
    opacity: 0;
    transform: translateY(-40vh) scale(0.07);
    transition: transform 2.8s var(--ease-stage), opacity 0.8s var(--ease-silk);
}
/* The glint it arrives as, and the star it leaves as. */
.the-letter-envelope::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    width: 260%;
    aspect-ratio: 1;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    pointer-events: none;
    background: radial-gradient(circle, var(--light-core) 0%, var(--halo) 12%, var(--halo-far) 32%, transparent 60%);
    opacity: 1;
    transition: opacity 2.4s var(--ease-silk);
}
.the-letter-container.is-arrived .the-letter-envelope {
    opacity: 1;
    transform: none;
}
.the-letter-container.is-arrived .the-letter-envelope::after { opacity: 0; }

.the-letter-env-back,
.the-letter-env-front,
.the-letter-flap {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
}
.the-letter-env-back {
    z-index: 0;
    height: 100%;
    border-radius: 6px;
    background: linear-gradient(170deg, rgba(16, 24, 48, 1), rgba(12, 18, 34, 1));
    border: 1px solid var(--gilt-line);
    box-shadow: var(--paper-glow);
}
.the-letter-env-front {
    z-index: 3;
    height: 100%;
    border-radius: 0 0 6px 6px;
    clip-path: polygon(0 34%, 50% 66%, 100% 34%, 100% 100%, 0 100%);
    background: linear-gradient(180deg, rgba(20, 30, 58, 1) 30%, var(--paper) 100%);
}
.the-letter-flap {
    z-index: 4;
    height: 66%;
    clip-path: polygon(0 0, 100% 0, 50% 100%);
    background: linear-gradient(180deg, rgba(22, 32, 62, 1), rgba(16, 24, 48, 1));
    transform-origin: 50% 0;
    transform: rotateX(0deg);
    transition: transform 1.2s var(--ease-stage), z-index 0s linear 0.6s;
}
.the-letter-edge {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    fill: none;
    stroke: var(--gilt-line-strong);
    stroke-width: 1;
    vector-effect: non-scaling-stroke;
}
.the-letter-flap .the-letter-edge { height: 100%; }

/* The wax seal, pressed with {fromName}'s initial. */
.the-letter-seal {
    position: absolute;
    z-index: 5;
    left: 50%;
    top: 66%;
    width: 3.4rem;
    height: 3.4rem;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    transform: translate(-50%, -50%);
    cursor: pointer;
    pointer-events: auto;
    transition: opacity 0.6s var(--ease-silk), transform 0.6s var(--ease-expo-out);
    -webkit-tap-highlight-color: transparent;
}
.the-letter-seal:disabled { cursor: default; }
.the-letter-seal:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 4px;
}
.the-letter-wax {
    position: relative;
    display: grid;
    place-items: center;
    width: 100%;
    height: 100%;
    overflow: hidden;
    border-radius: 50%;
    background: radial-gradient(circle at 34% 30%, rgba(242, 196, 160, 0.6) 0%, var(--seal) 42%, rgba(74, 25, 66, 0.95) 100%);
    box-shadow: 0 4px 14px rgba(5, 5, 17, 0.6), inset 0 0 0 3px rgba(196, 138, 96, 0.55), 0 0 18px var(--accent-warm-glow-soft);
}
.the-letter-initial {
    font: 700 1.45rem/1 var(--font-display);
    color: var(--text-primary);
    text-shadow: 0 1px 0 rgba(5, 5, 17, 0.45), 0 0 8px var(--ink-glow);
}
/* Breathing only while it is waiting to be opened (parked otherwise). */
.the-letter-container.is-settled:not(.is-open):not(.is-folded) .the-letter-wax {
    animation: the-letter-breathe 3.2s var(--ease-breath) infinite;
}
@keyframes the-letter-breathe {
    0%, 100% { transform: scale(1); box-shadow: 0 4px 14px rgba(5, 5, 17, 0.6), inset 0 0 0 3px rgba(196, 138, 96, 0.55), 0 0 14px var(--accent-warm-glow-soft); }
    50% { transform: scale(1.05); box-shadow: 0 4px 14px rgba(5, 5, 17, 0.6), inset 0 0 0 3px rgba(196, 138, 96, 0.55), 0 0 28px var(--accent-warm-glow); }
}

/* ───── Open: the flap lifts, the seal parts, the envelope sinks away ───── */
.the-letter-container.is-open .the-letter-flap {
    z-index: 0;
    transform: rotateX(180deg);
}
.the-letter-container.is-open .the-letter-seal {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.25);
    pointer-events: none;
}
.the-letter-container.is-open .the-letter-envelope {
    opacity: 0;
    transform: translateY(46vh) scale(0.92);
    transition: transform 1.2s var(--ease-stage) 1.8s, opacity 1s var(--ease-silk) 2s;
}

/* ───── The sheet ───── */
.the-letter-sheet {
    grid-area: 1 / 1;
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    width: min(86vw, 27rem);
    min-height: 40vh;
    max-height: 56vh;
    border-radius: 4px;
    border: 1px solid var(--paper-edge);
    background:
        radial-gradient(ellipse at 50% 0%, rgba(232, 168, 124, 0.06), transparent 60%),
        linear-gradient(180deg, var(--paper), rgba(12, 18, 34, 1));
    box-shadow: var(--paper-glow), inset 0 0 0 1px rgba(242, 196, 160, 0.05);
    opacity: 0;
    transform: translateY(6%) scale(0.8, 0.22);
    pointer-events: none;
}
.the-letter-container.is-open .the-letter-sheet {
    opacity: 1;
    transform: none;
    z-index: 6;
    animation: the-letter-sheet-rise 2s var(--ease-silk) 1s both;
}
@keyframes the-letter-sheet-rise {
    0% { opacity: 0; transform: translateY(6%) scale(0.8, 0.22); z-index: 1; }
    10% { opacity: 1; }
    42% { transform: translateY(-22vh) scale(0.8, 0.22); z-index: 1; }
    43% { z-index: 6; }
    100% { opacity: 1; transform: none; z-index: 6; }
}
.the-letter-container.is-folded .the-letter-sheet {
    animation: the-letter-sheet-fold 1.6s var(--ease-silk) both;
}
@keyframes the-letter-sheet-fold {
    0% { opacity: 1; transform: none; z-index: 6; }
    50% { transform: translateY(-22vh) scale(0.8, 0.22); z-index: 6; }
    51% { z-index: 1; }
    85% { opacity: 1; }
    100% { opacity: 0; transform: translateY(6%) scale(0.8, 0.22); z-index: 1; }
}

.the-letter-scroll {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.the-letter-page {
    position: relative;
    margin: auto 0;
    padding: 2rem 1.4rem 2.2rem;
    /* The auto-scroll glide is a Web Animation (the-letter.js), not a transition. */
}
.the-letter-container.is-reading .the-letter-sheet { pointer-events: auto; }
.the-letter-container.is-reading .the-letter-scroll {
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
}

/* ───── The hand: Pinyon, gold ink, never under 1.45rem ───── */
.the-letter-body,
.the-letter-salute,
.the-letter-sign {
    font-family: var(--font-script);
    font-size: clamp(1.45rem, 6.2vw, 1.9rem);
    line-height: 1.55;
    color: var(--ink);
    text-shadow: 0 0 10px var(--ink-glow);
}
.the-letter-para + .the-letter-para { margin-top: 0.55em; }
.the-letter-hard { white-space: normal; overflow-wrap: anywhere; }

.the-letter-line {
    position: relative;
    margin: 0;
    white-space: nowrap;
}
.the-letter-salute {
    margin-bottom: 0.35em;
    font-size: clamp(1.6rem, 6.8vw, 2.1rem);
    white-space: normal;
}
.the-letter-sign {
    margin-top: 0.7em;
    text-align: right;
    font-size: clamp(1.9rem, 8vw, 2.45rem);
    line-height: 1.3;
    white-space: normal;
}
/* A 30-character fromName or a 24-character toName wraps inside the sheet;
   the wipe still clears the whole box, so every wrapped line is revealed. */
.the-letter-salute .the-letter-ink,
.the-letter-sign .the-letter-ink {
    overflow-wrap: anywhere;
}
.the-letter-sign .the-letter-ink { text-align: right; }
/* The buyer signed it themselves: no second signature (R-M5b). */
.the-letter-sign.is-omitted { display: none; }
.the-letter-pen {
    position: relative;
    display: inline-block;
    max-width: 100%;
}
.the-letter-ink {
    display: inline-block;
    max-width: 100%;
    clip-path: inset(-0.5em calc(100% + 0.6em) -0.5em -0.6em);
}
.the-letter-line[data-ink="done"] .the-letter-ink { clip-path: none; }
/* The wipe (clip-path) and the nib's run are Web Animations started by the
   writing cue; this is only the nib's look. One nib for the whole sheet. */
.the-letter-nib {
    position: absolute;
    left: -3px;
    top: -3px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--nib);
    box-shadow: 0 0 10px 3px var(--ink-glow), 0 0 3px var(--nib);
    opacity: 0;
    pointer-events: none;
}

/* ───── The hint and "Keep it": screen space ─────
   #the-letter-controls is moved into #stage (outside the camera's world)
   by the module, so neither ever travels with a camera move. Once shown
   they only fade; the badge guard lifts above "Keep it" (badge rule). */
.the-letter-controls {
    position: absolute;
    inset: 0;
    z-index: 6;
    pointer-events: none;
    visibility: hidden;
    transition: visibility 0s linear 1s; /* lets "Keep it" fade out first */
}
.the-letter-controls.is-hinting,
.the-letter-controls.is-reading {
    visibility: visible;
    transition-delay: 0s;
}

.the-letter-hint {
    position: absolute;
    left: 50%;
    bottom: calc(17vh + env(safe-area-inset-bottom, 0px));
    margin: 0;
    white-space: nowrap;
    font: 500 0.78rem/1 var(--font-body);
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--text-secondary);
    opacity: 0;
    transform: translateX(-50%);
    transition: opacity 0.9s var(--ease-expo-out);
    pointer-events: none;
}
.the-letter-controls.is-hinting .the-letter-hint {
    opacity: 0.9;
    animation: the-letter-hint-breathe 3.2s var(--ease-breath) 1s infinite;
}
@keyframes the-letter-hint-breathe {
    0%, 100% { opacity: 0.9; }
    50% { opacity: 0.55; }
}

.the-letter-keep {
    position: absolute;
    left: 50%;
    bottom: calc(24px + env(safe-area-inset-bottom, 0px));
    min-height: 44px;
    margin: 0;
    padding: 0.75rem 1.7rem;
    font: 500 0.9rem/1 var(--font-body);
    letter-spacing: 0.08em;
    color: var(--text-primary);
    background: var(--glass);
    border: 1px solid var(--glass-border-hover);
    border-radius: 999px;
    box-shadow: 0 0 22px var(--accent-warm-glow-soft);
    cursor: pointer;
    opacity: 0;
    transform: translateX(-50%);
    transition: opacity 1s var(--ease-expo-out);
    pointer-events: none;
    -webkit-tap-highlight-color: transparent;
}
.the-letter-keep:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 3px;
}
.the-letter-controls.is-reading .the-letter-keep {
    opacity: 1;
    pointer-events: auto;
}

/* ───── Keep: the envelope comes home, is sealed, and rises as a star ───── */
.the-letter-container.is-folded .the-letter-envelope {
    opacity: 1;
    transform: none;
    transition: transform 0.8s var(--ease-expo-out), opacity 0.5s var(--ease-silk);
}
.the-letter-container.is-folded .the-letter-flap {
    transition: transform 0.5s var(--ease-silk) 1.1s, z-index 0s linear 1.35s;
}
.the-letter-container.is-folded .the-letter-seal {
    opacity: 0;
    pointer-events: none;
}
.the-letter-container.is-sealed .the-letter-seal { opacity: 1; }
.the-letter-container.is-sealed .the-letter-wax {
    animation: the-letter-press 0.5s var(--ease-expo-out) both;
}
@keyframes the-letter-press {
    from { transform: scale(1.3); }
    to { transform: scale(1); }
}
.the-letter-container.is-rising .the-letter-envelope {
    transform: translateY(-52vh) scale(0.1);
    transition: transform 5s var(--ease-stage), opacity 1.5s var(--ease-silk);
}
/* The paper melts into the light; what stays is a bright new star in the
   upper sky, large enough to hold the frame until the cap toss. */
.the-letter-container.is-rising .the-letter-env-back,
.the-letter-container.is-rising .the-letter-env-front,
.the-letter-container.is-rising .the-letter-flap,
.the-letter-container.is-rising .the-letter-seal {
    opacity: 0;
    transition: opacity 2.4s var(--ease-silk) 1.6s;
}
.the-letter-container.is-rising .the-letter-envelope::after {
    width: 760%;
    opacity: 1;
    background: radial-gradient(circle, var(--light-core) 0%, var(--light-core) 3.5%, rgba(242, 196, 160, 0.85) 7%, var(--halo) 16%, var(--halo-far) 34%, transparent 62%);
    transition: opacity 3s var(--ease-silk) 0.8s;
    animation: the-letter-star 3.6s var(--ease-breath) 5s infinite;
}
@keyframes the-letter-star {
    0%, 100% { transform: translate(-50%, -50%) scale(1); }
    50% { transform: translate(-50%, -50%) scale(1.14); }
}
.the-letter-container.is-gone .the-letter-envelope { opacity: 0.85; }

/* Entry states are applied instantly (prepare), never animated in. */
.the-letter-instant,
.the-letter-instant *,
.the-letter-instant *::after {
    transition: none !important;
    animation: none !important;
}

/* ───── Reduced motion: every step still plays, calmly ─────
   The envelope fades in, the flap cross-fades, each line fades in whole
   (400 ms) on the same schedule with no nib; the fold is a fade to the
   closed envelope, which then fades up and away. */
@media (prefers-reduced-motion: reduce) {
    .the-letter-envelope {
        transform: none;
        transition: opacity 1.2s var(--ease-silk);
    }
    .the-letter-envelope::after { display: none; }
    .the-letter-flap { transition: opacity 0.8s var(--ease-silk); }
    .the-letter-container.is-open .the-letter-flap {
        transform: none;
        opacity: 0;
    }
    .the-letter-container.is-open .the-letter-envelope {
        transform: none;
        transition: opacity 0.8s var(--ease-silk) 0.4s;
    }
    .the-letter-container.is-open .the-letter-seal { transform: translate(-50%, -50%); }
    .the-letter-sheet { transform: none; }
    .the-letter-container.is-open .the-letter-sheet {
        animation: the-letter-fade-in 0.8s var(--ease-silk) 0.8s both;
    }
    .the-letter-container.is-folded .the-letter-sheet {
        animation: the-letter-fade-out 0.8s var(--ease-silk) both;
    }
    .the-letter-container.is-folded .the-letter-envelope {
        transform: none;
        transition: opacity 0.8s var(--ease-silk) 0.6s;
    }
    .the-letter-container.is-folded .the-letter-flap {
        transform: none;
        opacity: 1;
        transition: opacity 0.6s var(--ease-silk) 0.8s;
    }
    .the-letter-container.is-rising .the-letter-envelope {
        opacity: 0;
        transform: translateY(-4vh);
        transition: transform 2.4s var(--ease-silk), opacity 2.4s var(--ease-silk);
    }
    /* Each line fades in whole (a 400 ms Web Animation on opacity). */
    .the-letter-ink {
        clip-path: none;
        opacity: 0;
    }
    .the-letter-line[data-ink="done"] .the-letter-ink { opacity: 1; }
    .the-letter-nib { display: none; }

    .the-letter-container.is-settled:not(.is-open):not(.is-folded) .the-letter-wax,
    .the-letter-container.is-sealed .the-letter-wax,
    .the-letter-controls.is-hinting .the-letter-hint { animation: none; }
    .the-letter-intro,
    .the-letter-hint,
    .the-letter-keep { transition: opacity 0.8s var(--ease-silk); }
}

@keyframes the-letter-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}
@keyframes the-letter-fade-out {
    from { opacity: 1; }
    to { opacity: 0; }
}
