@font-face {
    font-family: 'Priestacy';
    src: url('Priestacy.ttf') format('truetype'), url('Priestacy.otf') format('opentype');
    font-weight: normal; font-style: normal;
}

:root {
    --canvas-color: #f4f1ea;
    --parchment: #fdf5e6;
}

body, html {
    margin: 0; padding: 0; height: 100%;
    background-color: var(--canvas-color);
    background-image: url('https://www.transparenttextures.com/patterns/stucco.png');
    font-family: 'Crimson Pro', serif; overflow: hidden;
}

#canvas-bg {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    z-index: 0; pointer-events: none;
}
.doodle {
    position: absolute; opacity: 0.15; font-size: 20px; user-select: none;
}

.scene {
    position: relative; width: 100vw; height: 100vh;
    display: flex; justify-content: center; align-items: center;
    z-index: 1; perspective: 1000px;
}

/* THE LIGHTING TRICK (Cinematic Vignette) */
.cinematic-overlay {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: radial-gradient(circle at center, transparent 30%, rgba(10, 5, 0, 0.7) 100%);
    opacity: 0; transition: opacity 2s ease; z-index: 2; pointer-events: none;
}

.envelope-wrapper {
    position: absolute; width: 320px; height: 220px;
    cursor: pointer; transition: all 1s ease-in-out; z-index: 5;
    filter: drop-shadow(0 15px 20px rgba(0,0,0,0.4));
}
.envelope-back {
    position: absolute; width: 100%; height: 100%;
    background: linear-gradient(to bottom, #6b4728, #5c3b1e); 
    border-radius: 5px; box-shadow: inset 0 0 30px rgba(0,0,0,0.5);
}
.envelope-front {
    position: absolute; bottom: 0; width: 0; height: 0;
    border-left: 160px solid transparent; border-right: 160px solid transparent;
    border-bottom: 110px solid #8b6242; z-index: 3;
    filter: drop-shadow(0 -4px 4px rgba(0,0,0,0.2));
}
.envelope-flap {
    position: absolute; top: 0; width: 0; height: 0; border-top: 130px solid #9c7351;
    border-left: 160px solid transparent; border-right: 160px solid transparent;
    transform-origin: top; transition: transform 0.8s cubic-bezier(0.4, 0, 0.2, 1); z-index: 4;
}

.seal {
    position: absolute; top: 50%; left: 50%; width: 55px; height: 55px;
    background: radial-gradient(circle at 35% 35%, #f2d5cb, #f5c4c4); border-radius: 50%;
    transform: translate(-50%, -20%); z-index: 6; transition: all 0.5s ease; 
    box-shadow: 0 4px 8px rgba(0,0,0,0.5), inset 0 0 6px rgba(0,0,0,0.4);
    display: flex; justify-content: center; align-items: center;
}
.seal-heart { color: #b32202; font-size: 26px; line-height: 1; text-shadow: 0 1px 2px rgba(0,0,0,0.5); }

/* FRAMING FIX: Made slightly wider, and adjusted padding */
.letter {
    position: absolute; width: 310px; height: 210px; 
    background: var(--parchment); background-image: url('https://www.transparenttextures.com/patterns/cream-paper.png');
    bottom: calc(50% - 110px); left: 50%; transform: translateX(-50%);
    z-index: 2; padding: 20px 15px; box-sizing: border-box;
    border-radius: 2px 255px 3px 255px / 255px 5px 225px 3px;
    border: 1px solid rgba(139, 69, 19, 0.15); box-shadow: 0 5px 15px rgba(0,0,0,0.15);
    transition: all 1.5s cubic-bezier(0.25, 1, 0.5, 1);
    font-family: 'Priestacy', 'Caveat', cursive; color: #2b1d0e;
}

.letter-preview {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    height: 100%; transition: opacity 0.5s ease; text-align: center;
}

/* FRAMING FIX: Reduced font size slightly and locked line-height */
.greeting { font-size: 2.2rem; margin: 0; color: #8b4100; line-height: 1.1; }

/* THE BLINKING CUE */
.cupcake-text {
    margin-top: 15px; font-family: 'Priestacy', 'Caveat', cursive; 
    font-size: 1.6rem; color: #5a3d22; cursor: pointer; 
    opacity: 0; pointer-events: none;
}
.cupcake-text:hover { color: #8b0000; }

.letter-content { display: none; opacity: 0; font-size: 1.8rem; line-height: 1.3; }
.closing { margin-top: 20px; margin-bottom: 0; }
.signature { margin-top: 5px; font-size: 2rem; }

/* ========================================= */
/* THE MAGICAL ANIMATION STATES              */
/* ========================================= */

.scene.is-opened .envelope-flap { transform: rotateX(180deg); z-index: 1; }
.scene.is-opened .seal { opacity: 0; transform: translate(-50%, -20%) scale(0); }
.scene.is-opened .cinematic-overlay { opacity: 1; } /* Dims the background */

/* The Magical Push-in & Glow */
.scene.is-opened .letter { 
    transform: translate(-50%, -100px) scale(1.05); /* Pushes toward the camera */
    box-shadow: 0 0 40px rgba(255, 215, 0, 0.3), 0 10px 20px rgba(0,0,0,0.3); /* Golden glow */
    z-index: 6; 
}

/* Chain animations: Fade in, then pulse forever */
.scene.is-opened .cupcake-text {
    animation: fadeInText 1s forwards 3s, textPulse 2s ease-in-out infinite 4s;
}

@keyframes fadeInText { to { opacity: 1; pointer-events: auto; } }
@keyframes textPulse {
    0%, 100% { opacity: 1; text-shadow: 0 0 8px rgba(139, 0, 0, 0.2); }
    50% { opacity: 0.4; text-shadow: none; }
}

.scene.is-reading .envelope-wrapper { transform: translateY(150vh); opacity: 0; filter: none; }
.scene.is-reading .letter {
    bottom: 50%; transform: translate(-50%, 50%) scale(1);
    width: 90vw; max-width: 600px; height: 85vh;
    box-shadow: 0 20px 60px rgba(0,0,0,0.6); overflow-y: auto; padding: 40px;
}

.scene.is-reading .letter-preview { display: none; }
.scene.is-reading .letter-content { display: block; animation: fadeInInk 1.5s forwards 0.8s; }
@keyframes fadeInInk { to { opacity: 1; } }

/* ========================================= */
/* MOBILE VIEW FIXES (Vertical framing)      */
/* ========================================= */
@media screen and (max-width: 600px) {
    /* Scale the envelope down to fit mobile screens perfectly */
    .envelope-wrapper { transform: scale(0.85); }
    .letter { width: 280px; padding: 15px; }
    .greeting { font-size: 2rem; }
    
    /* Full screen letter adjustments for mobile reading */
    .scene.is-reading .letter { 
        width: 92vw; height: 85vh; padding: 25px; 
        font-size: 1.4rem; 
    }
}