body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #1a472a; /* Christmas Green */
    margin: 0;
    font-family: 'Georgia', serif;
    perspective: 1000px;
    overflow: hidden;
}

.container {
    position: relative;
    width: 100%;
    height: 100vh;
}

.envelope-wrapper {
    width: 300px;
    height: 200px;
    position: absolute;
    left: 50%;
    cursor: pointer;
    transform-style: preserve-3d;
    transition: transform 0.6s ease-in-out, opacity 0.6s ease-in-out, top 0.6s ease-in-out;
    transform: translateX(-50%) scale(1); 
}

/* Initial Positions */
#envelope-popo {
    top: 20%;
}

#envelope-magan {
    top: 60%;
}

/* Hover effect for envelopes */
.envelope-wrapper:hover {
    transform: translateX(-50%) scale(1.05);
}

.envelope {
    width: 100%;
    height: 100%;
    position: relative;
    transform-style: preserve-3d;
    transition: transform 0.6s ease-in-out; /* Add transition for envelope movement */
}

/* Common styles */
.back, .front, .front-bg, .side-left, .side-right, .bottom-flap, .top-flap, .letter {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: 5px;
}

/* Add smooth return animation for detachable parts */
.back, .front-bg, .side-left, .side-right, .bottom-flap, .top-flap {
    transition: transform 0.6s ease-in-out, opacity 0.6s ease-in-out;
}

/* Address Side (Back) */
.back {
    background-color: #c0392b; /* Christmas Red */
    transform: rotateY(0deg) translateZ(20px); /* Furthest forward initially */
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

.back::after {
    font-size: 24px;
    color: #fff;
    font-family: 'Georgia', serif;
    font-style: italic;
    text-align: center;
}

.back-popo::after {
    content: "To: Popo";
}

.back-magan::after {
    content: "To: Magan & Viola";
}

/* Front Container */
.front {
    transform: rotateY(180deg);
    transform-style: preserve-3d;
    background-color: transparent;
    pointer-events: none;
    z-index: 10;
}

/* Front Background (Inside of envelope) */
.front-bg {
    background-color: #a93226; /* Darker red inside */
    transform: translateZ(0px);
    z-index: 0;
}

/* Side Flaps */
.side-left {
    width: 0;
    height: 0;
    border-top: 100px solid transparent;
    border-bottom: 100px solid transparent;
    border-left: 150px solid #b03a2e;
    transform: translateZ(10px);
    z-index: 20;
}

.side-right {
    width: 0;
    height: 0;
    border-top: 100px solid transparent;
    border-bottom: 100px solid transparent;
    border-right: 150px solid #b03a2e;
    left: auto;
    right: 0;
    transform: translateZ(10px);
    z-index: 20;
}

/* Bottom Flap */
.bottom-flap {
    width: 0;
    height: 0;
    border-left: 150px solid transparent;
    border-right: 150px solid transparent;
    border-bottom: 110px solid #c0392b;
    bottom: 0;
    top: auto;
    transform: translateZ(12px);
    z-index: 21;
}

/* Top Flap */
.top-flap {
    width: 0;
    height: 0;
    border-left: 150px solid transparent;
    border-right: 150px solid transparent;
    border-top: 110px solid #c0392b;
    transform-origin: top;
    transform: translateZ(14px);
    transition: transform 0.6s ease-in-out 0.6s, z-index 0s linear 0s; /* Delay closing for letter slide-in */
    z-index: 22;
    backface-visibility: visible; /* Ensure visible when flipped up */
}

/* Gold Seal */
.top-flap::after {
    content: "";
    position: absolute;
    top: -60px;
    left: -20px;
    width: 40px;
    height: 40px;
    background: radial-gradient(circle, #f1c40f 0%, #d4ac0d 100%);
    border-radius: 50%;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* Letter */
.letter {
    position: absolute;
    top: 5px;
    left: 10px;
    width: 280px;
    height: 150px; /* Folded height */
    background-color: #fdfbf7;
    z-index: 5; /* Inside the envelope (below flaps) */
    transform: translateZ(2px);
    transition: transform 0.6s ease, top 0.6s ease, opacity 0.1s ease, width 0.6s ease, height 0.6s ease, left 0.6s ease;
    transform-style: preserve-3d;
    overflow: hidden;
    display: none; /* Hidden initially */
    border-radius: 3px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    
    /* Fix for interaction and centering */
    pointer-events: auto; 
    transform-origin: center;
}

.letter-content {
    padding: 20px;
    height: 100%;
    box-sizing: border-box;
    overflow-y: auto; /* Scrollable content */
    font-size: 14px;
    line-height: 1.6;
    color: #2c3e50;
    opacity: 0; /* Hide content while folded */
    transition: opacity 0.4s ease;
}

.stanza {
    margin-bottom: 1.5em;
    margin-top: 0;
    line-height: 1.4;
}

/* Fold Crease / Cover */
.letter::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(to bottom, rgba(255,255,255,0) 40%, rgba(0,0,0,0.05) 50%, rgba(255,255,255,0) 60%);
    pointer-events: none;
    opacity: 1;
    transition: opacity 0.5s ease;
}

/* Close Button */
.close-btn {
    position: absolute;
    top: 10px;
    right: 15px;
    font-size: 24px;
    cursor: pointer;
    color: #c0392b;
    z-index: 100;
    font-weight: bold;
    display: none; /* Hidden until letter is fully open */
    pointer-events: auto;
}

.close-btn:hover {
    color: #a93226;
}

/* Animation States */

/* Centered State: Moves the active envelope to the center */
.envelope-wrapper.centered {
    top: 50% !important; /* Override initial top */
    transform: translate(-50%, -50%) scale(1.2); /* Center and scale up */
    z-index: 1000;
}

/* Slide Away States */
.envelope-wrapper.slide-up {
    top: -50% !important;
    opacity: 0;
}

.envelope-wrapper.slide-down {
    top: 150% !important;
    opacity: 0;
}

/* 1. Flip */
.envelope-wrapper.flip {
    /* Maintain centering and scale, add rotation */
    transform: translate(-50%, -50%) scale(1.2) rotateY(180deg);
}

/* Letter Display - Controlled by JS now */
.envelope-wrapper.show-letter .letter {
    display: block;
    opacity: 1;
    /* Ensure it stays inside initially */
    z-index: 5; 
}

/* 2. Open Flap */
.envelope-wrapper.open .top-flap {
    transform: rotateX(180deg) translateZ(0px);
    transition: transform 0.6s ease-in-out 0s, z-index 0s linear 0.6s; /* Match return animation duration */
    z-index: 0;
}

/* Hide seal when flap is open */
.envelope-wrapper.open .top-flap::after {
    opacity: 0;
    transition: opacity 0.2s ease; /* Disappear quickly as it flips */
}

/* 3. Letter slides out, Envelope slides down */
.envelope-wrapper.open .envelope {
    transform: translateY(50px); /* Move envelope down */
    transition-delay: 0.8s; /* Sync with letter slide (wait for flap + pause) */
}

.envelope-wrapper.open .letter {
    /* Move up and OUT of the envelope */
    /* Still folded (small height) */
    /* Keep translateZ low (2px) so it stays behind flaps visually if they overlap */
    transform: translateY(-100px) translateZ(2px); 
    /* Keep z-index low until it clears the envelope */
    z-index: 5; 
    transition-delay: 0.8s; /* Wait for flap to open (0.6s) + 0.2s pause */
}

/* 4. Envelope Slides Away */
.envelope-wrapper.detach .front-bg,
.envelope-wrapper.detach .side-left,
.envelope-wrapper.detach .side-right,
.envelope-wrapper.detach .bottom-flap,
.envelope-wrapper.detach .top-flap,
.envelope-wrapper.detach .back {
    transform: translateY(500px);
    opacity: 0;
    transition: transform 0.8s ease-in, opacity 0.8s ease-in;
}

/* 5. Expand Letter (Unfold) */
.envelope-wrapper.expand .letter {
    /* Unfold animation */
    width: 340px; /* Fixed width, slightly wider than envelope */
    height: 500px; /* Fixed height */
    max-height: 80vh; /* Responsive max height */
    
    /* Center on screen */
    /* Counter-scale the wrapper's 1.5 scale */
    /* Use transform-origin: center (default) to scale from center */
    transform: translate(-50%, -50%) translateZ(50px) scale(0.833); 
    
    left: 50%;
    top: 40%; /* Moved up to be optically centered */
    
    transition: all 0.8s cubic-bezier(0.25, 1, 0.5, 1); /* Smooth unfold */
    cursor: default;
    z-index: 2000; /* Now on top of everything */
}

.envelope-wrapper.expand .letter-content {
    opacity: 1; /* Show content */
    transition-delay: 0.4s; /* Wait for unfold */
}

.envelope-wrapper.expand .letter::after {
    opacity: 0; /* Hide crease */
}

.envelope-wrapper.expand .close-btn {
    display: block;
    transition-delay: 0.8s;
}

/* Voucher Styles */
.continue-btn {
    background-color: #c0392b;
    color: white;
    border: none;
    padding: 10px 25px;
    border-radius: 20px;
    cursor: pointer;
    font-family: 'Georgia', serif;
    font-size: 16px;
    transition: background-color 0.3s ease;
}

.continue-btn:hover {
    background-color: #a93226;
}

.voucher-img {
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    object-fit: cover;
    height: 120px;
}

.send-gift-btn:hover {
    background-color: #a93226;
}

/* Spinner */
.spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid rgba(255,255,255,0.3);
    border-radius: 50%;
    border-top-color: #fff;
    animation: spin 1s ease-in-out infinite;
    margin-right: 8px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Snow Effect */
.snow {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
    background-image: 
        radial-gradient(4px 4px at 100px 50px, #fff, transparent), 
        radial-gradient(6px 6px at 200px 150px, #fff, transparent), 
        radial-gradient(3px 3px at 300px 250px, #fff, transparent), 
        radial-gradient(4px 4px at 400px 350px, #fff, transparent), 
        radial-gradient(6px 6px at 500px 100px, #fff, transparent), 
        radial-gradient(3px 3px at 600px 400px, #fff, transparent), 
        radial-gradient(4px 4px at 800px 500px, #fff, transparent), 
        radial-gradient(6px 6px at 900px 150px, #fff, transparent);
    background-size: 550px 550px;
    animation: snow 10s linear infinite;
    opacity: 0.8;
}

@keyframes snow {
    0% { background-position: 0px 0px, 0px 0px, 0px 0px, 0px 0px, 0px 0px, 0px 0px, 0px 0px, 0px 0px; }
    100% { background-position: 550px 550px, 400px 400px, 300px 300px, 200px 200px, 100px 100px, 150px 150px, 250px 250px, 350px 350px; }
}

/* Typography Updates */
body {
    font-family: 'Playfair Display', serif;
}

.letter-content {
    font-family: 'Merriweather', serif;
    font-size: 16px; /* Merriweather is naturally larger/wider, so 16px is usually good */
    line-height: 1.8;
    color: #1a1a1a;
}

.letter-content p:first-child {
    font-family: 'Dancing Script', cursive;
    font-size: 28px;
    color: #c0392b;
    font-weight: 700; /* Make it bolder */
}

.stanza {
    font-style: italic;
    line-height: 1.6;
    margin-bottom: 1.8em;
}

/* Voucher Section Styling */
.voucher-section h2 {
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    margin-bottom: 10px;
    letter-spacing: 1px;
}

.voucher-section h3 a {
    font-family: 'Playfair Display', serif;
    font-weight: 400;
    color: #2c3e50 !important;
    text-decoration: none !important;
    border-bottom: 1px solid #c0392b;
    transition: color 0.3s ease, border-color 0.3s ease;
}

.voucher-section h3 a:hover {
    color: #c0392b !important;
    border-color: #2c3e50;
}

.voucher-images {
    background: #fff;
    padding: 10px;
    border: 1px solid #eee;
    box-shadow: 0 4px 6px rgba(0,0,0,0.05);
    border-radius: 8px;
}

.voucher-img {
    transition: transform 0.3s ease;
    width: 100%;
    max-width: 100%;
    border-radius: 5px;
    margin-bottom: 10px;
}

.voucher-img:hover {
    transform: scale(1.02);
}

.send-gift-btn {
    margin-top: 20px;
    background: linear-gradient(135deg, #c0392b 0%, #a93226 100%);
    box-shadow: 0 4px 15px rgba(192, 57, 43, 0.3);
    font-weight: bold;
    letter-spacing: 0.5px;
    transform: translateY(0);
    transition: all 0.3s ease;
    padding: 12px 30px;
    font-size: 16px;
    border-radius: 30px;
    color: white;
    border: none;
    cursor: pointer;
    font-family: 'Merriweather', serif;
}

.send-gift-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(192, 57, 43, 0.4);
}

.send-gift-btn:active {
    transform: translateY(1px);
}

.voucher-images {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
    margin: 15px 0;
}

.voucher-section {
    text-align: center;
}
