:root {
    --bg-1: #0b0710;
    --bg-2: #1c0f22;
    --gold: #d9ac6d;
    --gold-soft: #f4c9a0;
    --rose: #ff8fab;
    --cream: #f5e6da;
    --seal: #8b1e2f;
    --seal-dark: #5e1220;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
}

body {
    margin: 0;
    overflow: hidden;
    background: radial-gradient(ellipse 120% 80% at 50% 20%, var(--bg-2), var(--bg-1) 70%);
    color: var(--cream);
    font-family: 'Caveat', cursive;
    -webkit-font-smoothing: antialiased;
}

#emberCanvas {
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
}

.scene {
    position: fixed;
    inset: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 1.1s ease;
}

.scene.active {
    opacity: 1;
    pointer-events: auto;
}

.stage {
    position: relative;
    width: min(90vw, 380px);
    height: min(70vh, 520px);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ---------- Envelope ---------- */

.envelope-wrap {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 22px;
    cursor: pointer;
    transition: opacity 0.6s ease, transform 0.6s ease;
}

.envelope {
    position: relative;
    width: min(78vw, 300px);
    aspect-ratio: 3 / 2;
    filter: drop-shadow(0 18px 34px rgba(0, 0, 0, 0.55));
}

.env-back {
    position: absolute;
    inset: 0;
    background: linear-gradient(155deg, #3a2432, #241320);
    border-radius: 6px;
}

.env-front-left,
.env-front-right {
    position: absolute;
    bottom: 0;
    width: 50%;
    height: 62%;
    background: linear-gradient(160deg, #4a2c3c, #2c1826);
    z-index: 3;
}

.env-front-left {
    left: 0;
    clip-path: polygon(0 0, 100% 100%, 0 100%);
    border-bottom-left-radius: 6px;
}

.env-front-right {
    right: 0;
    clip-path: polygon(100% 0, 100% 100%, 0 100%);
    border-bottom-right-radius: 6px;
}

.env-flap {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 58%;
    background: linear-gradient(200deg, #4d2e40, #2a1622);
    clip-path: polygon(0 0, 100% 0, 50% 100%);
    transform-origin: top center;
    transform: rotateX(0deg);
    transition: transform 0.9s cubic-bezier(.6, -0.15, .3, 1.4);
    z-index: 4;
}

.envelope.open .env-flap {
    transform: rotateX(178deg);
    z-index: 1;
}

.wax-seal {
    position: absolute;
    top: 40%;
    left: 50%;
    width: 46px;
    height: 46px;
    transform: translate(-50%, -50%);
    background: radial-gradient(circle at 35% 30%, #b23a4a, var(--seal) 55%, var(--seal-dark));
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--gold-soft);
    font-size: 20px;
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.5), inset 0 2px 3px rgba(255, 255, 255, 0.15);
    z-index: 5;
    transition: transform 0.45s cubic-bezier(.5, -0.4, .5, 1.6), opacity 0.45s ease;
}

.wax-seal.breaking {
    transform: translate(-50%, -50%) scale(1.35) rotate(18deg);
    opacity: 0;
}

.to-name {
    font-family: 'Dancing Script', cursive;
    font-weight: 700;
    font-size: clamp(28px, 7vw, 40px);
    color: var(--gold-soft);
    text-shadow: 0 0 18px rgba(244, 201, 160, 0.35);
    margin: 0;
    text-align: center;
}

.tap-hint {
    margin: 0;
    font-size: 18px;
    letter-spacing: 0.06em;
    color: rgba(245, 230, 218, 0.55);
    animation: pulseHint 2.4s ease-in-out infinite;
}

@keyframes pulseHint {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 0.9; }
}

.envelope-wrap.hidden {
    opacity: 0;
    transform: scale(0.92);
    pointer-events: none;
}

/* ---------- Paper ---------- */

.paper {
    position: absolute;
    width: min(86vw, 340px);
    max-height: 84vh;
    max-width: 100%;
    background:
        radial-gradient(circle, rgba(90, 55, 30, 0.05) 1px, transparent 1px),
        linear-gradient(175deg, #fdf6ec, #f0e0c8);
    background-size: 3px 3px, 100% 100%;
    border-radius: 14px;
    box-shadow:
        0 30px 70px rgba(0, 0, 0, 0.5),
        inset 0 1px 0 rgba(255, 255, 255, 0.5);
    opacity: 0;
    transform: scale(0.2) translateY(40px);
    transform-origin: center;
    pointer-events: none;
    transition: opacity 0.5s ease, transform 0.7s cubic-bezier(.2, .8, .3, 1.2);
    overflow: hidden;
}

.paper.visible {
    opacity: 1;
    transform: scale(1) translateY(0);
    pointer-events: auto;
}

.paper-fold {
    position: absolute;
    left: 0;
    width: 100%;
    height: 18%;
    background: linear-gradient(175deg, #fdf6ec, #f0e0c8);
    transform-origin: center;
    z-index: 2;
    transition: transform 0.8s cubic-bezier(.5, 0, .3, 1), background 0.4s ease 0.4s;
}

.fold-top {
    top: 0;
    transform: rotateX(-90deg);
}

.fold-bottom {
    bottom: 0;
    transform: rotateX(90deg);
}

.paper.unfolded .fold-top,
.paper.unfolded .fold-bottom {
    transform: rotateX(0deg);
}

/* Once flattened, drop the fold panels' own background so the single
   continuous gradient painted on .paper shows through with no seam. */
.paper.unfolded .paper-fold {
    background: transparent;
}

.paper-inner {
    position: relative;
    z-index: 3;
    max-height: 84vh;
    overflow-y: auto;
    padding: 34px 28px 24px;
    color: #3a2418;
}

.paper-content {
    font-family: 'Caveat', cursive;
    font-size: clamp(19px, 4.6vw, 23px);
    line-height: 1.55;
}

.paper-content p {
    margin: 0 0 16px;
}

.paper-content .word {
    display: inline-block;
    margin-right: 0.28em;
    opacity: 0;
    transform: translateY(6px) rotate(var(--wobble, 0deg));
    animation: inkIn 0.5s ease forwards;
}

@keyframes inkIn {
    to {
        opacity: 1;
        transform: translateY(0) rotate(var(--wobble, 0deg));
    }
}

.continue-btn {
    display: block;
    margin: 18px auto 4px;
    padding: 8px 22px;
    background: transparent;
    border: 1.5px solid rgba(58, 36, 24, 0.35);
    border-radius: 999px;
    color: #6b4327;
    font-family: 'Caveat', cursive;
    font-size: 19px;
    letter-spacing: 0.04em;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.8s ease, background 0.2s ease, transform 0.2s ease;
}

.continue-btn.visible {
    opacity: 0.85;
}

.continue-btn:hover,
.continue-btn:active {
    background: rgba(139, 30, 47, 0.08);
    transform: translateY(-1px);
}

/* ---------- Finale ---------- */

#sceneFinale {
    flex-direction: column;
    gap: 6px;
}

#finaleCanvas {
    width: 100%;
    height: 60vh;
    max-height: 420px;
}

.finale-line {
    font-family: 'Dancing Script', cursive;
    font-size: clamp(18px, 4.4vw, 24px);
    color: rgba(245, 230, 218, 0.85);
    text-align: center;
    max-width: 80vw;
    margin: 0;
    opacity: 0;
    transition: opacity 1.4s ease;
}

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

.replay-btn {
    position: absolute;
    bottom: 26px;
    right: 26px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid rgba(245, 230, 218, 0.25);
    background: rgba(255, 255, 255, 0.04);
    color: rgba(245, 230, 218, 0.7);
    font-size: 18px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 1s ease 1s, background 0.2s ease;
}

.replay-btn.visible {
    opacity: 1;
}

.replay-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

@media (max-width: 420px) {
    .stage {
        width: 92vw;
    }
    .paper-inner {
        padding: 26px 20px 18px;
    }
}

/* Desktop/tablet: the letter opens as a landscape sheet instead of a
   narrow phone-width strip. */
@media (min-width: 720px) {
    .stage {
        width: min(70vw, 640px);
        height: min(78vh, 560px);
    }
    .envelope {
        width: min(46vw, 360px);
    }
    .paper {
        width: min(74vw, 680px);
        max-height: min(74vh, 440px);
    }
    .paper-inner {
        max-height: min(74vh, 440px);
        padding: 40px 56px 28px;
    }
    .paper-content {
        font-size: clamp(19px, 1.7vw, 24px);
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
