/* Mobile touch feedback */
.envelope-container.touching {
    transform: scale(0.95);
    transition: transform 0.1s ease-out;
}

.envelope-container.touching .envelope-side {
    box-shadow: 0 5px 15px rgba(0,0,0,0.4);
}

body {
    margin: 0;
    padding: 0;
    background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 50%, #fecfef 100%);
    font-family: 'Comic Sans MS', cursive, sans-serif;
    perspective: 1600px;
    min-height: 100vh;
    overflow-x: hidden;
    position: relative;
    /* Improve mobile performance */
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    /* Mobile touch improvements */
    -webkit-overflow-scrolling: touch;
    touch-action: manipulation;
}

/* Sparkle Container */
.sparkles {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
}

/* Individual Sparkle */
.sparkle {
    position: absolute;
    color: rgba(255, 255, 255, 0.8);
    animation: sparkle 3s infinite;
    font-size: 12px;
}

.sparkle:nth-child(odd) {
    animation-delay: -1s;
    color: rgba(255, 255, 255, 0.6);
}

.sparkle:nth-child(3n) {
    animation-delay: -2s;
    font-size: 16px;
    color: rgba(255, 255, 255, 0.9);
}

.sparkle:nth-child(4n) {
    animation-delay: -0.5s;
    font-size: 8px;
}

.sparkle:nth-child(5n) {
    animation-delay: -2.5s;
    font-size: 14px;
    color: rgba(255, 255, 255, 0.7);
}

@keyframes sparkle {
    0%, 100% {
        opacity: 0;
        transform: scale(0) rotate(0deg);
    }
    50% {
        opacity: 1;
        transform: scale(1) rotate(180deg);
    }
}

@keyframes sway {
    0%, 100% {
        transform: rotate(-2deg);
    }
    50% {
        transform: rotate(2deg);
    }
}

@keyframes swaySmooth {
    0% {
        transform: rotate(0deg);
    }
    25% {
        transform: rotate(1deg);
    }
    50% {
        transform: rotate(2deg);
    }
    75% {
        transform: rotate(1deg);
    }
    100% {
        transform: rotate(-2deg);
    }
}

.envelope-container {
    width: 100%;
    max-width: 350px;
    height: 220px;
    position: relative;
    cursor: pointer;
    transform-style: preserve-3d;
    transition: transform 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    touch-action: manipulation;
    transform-origin: bottom bottom;
    z-index: 10;
    will-change: transform;
    backface-visibility: hidden;
    /* Mobile optimization */
    -webkit-transform-style: preserve-3d;
    -webkit-backface-visibility: hidden;
    -webkit-transition: transform 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    /* Ensure touchable on mobile */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.envelope-container.swaying {
    animation: sway 3s ease-in-out infinite;
}

.envelope-container.swaying-smooth {
    animation: swaySmooth 4s ease-in-out infinite;
}

.envelope-container:hover,
.envelope-container:active {
    transform: scale(1.10) translateY(-5px);
}

.envelope-container.flipped {
    transform: translateY(-20px) rotateY(180deg) scale(1.02);
    animation: none;
}

.envelope-container.flipped:hover,
.envelope-container.flipped:active {
    transform: translateY(-20px) rotateY(180deg) scale(1.05);
}

/* CRITICAL: Handle flipped+opened state to prevent double flip */
.envelope-container.flipped.opened {
    transform: translateY(-20px) rotateY(180deg) scale(1.02) !important;
    animation: none;
}

.envelope-side {
    width: 100%;
    height: 100%;
    position: absolute;
    backface-visibility: hidden;
    border-radius: 8px;
    box-shadow: 0 8px 25px rgba(0,0,0,0.2);
    border: 3px solid #333;
    transition: box-shadow 0.4s ease-out;
    will-change: transform;
    /* Mobile optimization */
    -webkit-backface-visibility: hidden;
}

.envelope-front {
    background: linear-gradient(145deg, #f8f8f8, #e8e8e8);
    display: flex;
    align-items: center;
    justify-content: center;
}

.envelope-back {
    background: linear-gradient(145deg, #f0f0f0, #d8d8d8);
    transform: rotateY(180deg);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    z-index: 2;
}

.envelope-name {
    font-family: 'Kalam', 'Brush Script MT', cursive;
    font-size: clamp(32px, 7vw, 60px);
    font-weight: bold;
    color: #2c2c2c;
    text-shadow: 2px 2px 4px rgba(255,255,255,0.7);
    letter-spacing: 1px;
    user-select: none;
    text-transform: none;
}

.envelope-container.flipped .envelope-side {
    box-shadow: 0 15px 35px rgba(0,0,0,0.3);
}

/* Envelope Flap Styling */
.envelope-flap-outer {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 0;
    border-left: 170px solid transparent;
    border-right: 170px solid transparent;
    border-top: 105px solid #d8d8d8;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.15));
    z-index: 3;
    transform-origin: top center;
    transition: transform 1.2s cubic-bezier(0.4, 0.0, 0.2, 1);
    will-change: transform;
}

.envelope-flap-inner {
    position: absolute;
    top: 3px;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 0;
    border-left: 170px solid transparent;
    border-right: 170px solid transparent;
    border-top: 90px solid #f2f2f2;
    z-index: 4;
    transform-origin: top center;
    transition: transform 1.2s cubic-bezier(0.4, 0.0, 0.2, 1);
    will-change: transform;
}

/* Flap Opening Animation */
.envelope-container.opened .envelope-flap-outer {
    transform: translateX(-50%) rotateX(-180deg);
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.15)) drop-shadow(0 0 0 2px #333);
}

.envelope-container.opened .envelope-flap-inner {
    transform: translateX(-50%) rotateX(-180deg);
    filter: drop-shadow(0 0 0 2px #333);
}

/* Envelope Card Styling */
.envelope-card {
    position: absolute;
    width: 85%;
    height: 75%;
    background: linear-gradient(145deg, #ffffff, #f8f8f8);
    border: 2px solid #ddd;
    border-radius: 8px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) translateY(0px);
    transform-origin: bottom right;
    z-index: 1;
    transition: transform 3.0s cubic-bezier(0.4, 0.0, 0.2, 1) 0.3s, opacity 0.3s ease-in 0.6s, z-index 0s 0.6s, box-shadow 0.5s ease-in-out 3.5s;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    opacity: 0;
    will-change: transform, opacity;
}

.envelope-container.opened .envelope-card {
    transform: translate(-50%, -50%) translateY(-30px) rotate(20deg);
    opacity: 1;
    z-index: 5;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3), 0 8px 25px rgba(0,0,0,0.2), 0 0 0 1px rgba(0,0,0,0.1);
}

.card-content {
    padding: 20px;
    text-align: center;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.card-content h2 {
    font-family: 'Kalam', 'Brush Script MT', cursive;
    color: #2c2c2c;
    margin-bottom: 15px;
    font-size: clamp(18px, 4vw, 28px);
    font-weight: bold;
}

.card-content p {
    font-family: 'Kalam', 'Brush Script MT', cursive;
    color: #555;
    font-size: clamp(14px, 3vw, 18px);
    font-weight: 400;
    margin: 0;
}

.letter {
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}

.letter.visible {
    opacity: 1;
}

/* Music control button */
.music-control {
    position: fixed;
    top: 20px;
    right: 20px;
    background: rgba(255, 255, 255, 0.9);
    border: 2px solid #333;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 1000;
    font-size: 20px;
    transition: all 0.3s ease;
}

.music-control:hover {
    background: rgba(255, 255, 255, 1);
    transform: scale(1.1);
}

.music-control.muted {
    opacity: 0.5;
}

.reset-button {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: rgba(74, 144, 226, 0.9);
    color: white;
    border: none;
    padding: 12px 18px;
    border-radius: 8px;
    cursor: pointer;
    z-index: 1001;
    font-size: 14px;
    font-weight: bold;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    /* Ensure visibility on all devices */
    min-width: 100px;
    text-align: center;
    /* Mobile fixes */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.reset-button:hover {
    background: rgba(74, 144, 226, 1);
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(0,0,0,0.3);
}

.reset-button:disabled {
    background: rgba(74, 144, 226, 0.5);
    cursor: not-allowed;
    transform: none;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* Mobile specific button positioning */
@media (max-width: 768px) {
    .reset-button {
        bottom: 15px;
        right: 15px;
        padding: 10px 15px;
        font-size: 13px;
        min-width: 90px;
    }
}

@media (max-width: 400px) {
    .reset-button {
        bottom: 10px;
        right: 10px;
        padding: 8px 12px;
        font-size: 12px;
        min-width: 80px;
    }
}

/* Mobile responsive sizing */
@media (max-width: 576px) {
    .envelope-container {
        max-width: 280px;
        height: 180px;
    }
    
    .card-content {
        padding: 15px;
    }
    
    /* Mobile responsive flap sizing */
    .envelope-flap-outer {
        border-left: 136px solid transparent !important;
        border-right: 136px solid transparent !important;
        border-top: 84px solid #d8d8d8 !important;
    }
    
    .envelope-flap-inner {
        border-left: 136px solid transparent !important;
        border-right: 136px solid transparent !important;
        border-top: 72px solid #f2f2f2 !important;
    }
}

@media (max-width: 400px) {
    .envelope-container {
        max-width: 250px;
        height: 160px;
    }
    
    .card-content {
        padding: 12px;
    }
    
    /* Small mobile responsive flap sizing */
    .envelope-flap-outer {
        border-left: 121px solid transparent !important;
        border-right: 121px solid transparent !important;
        border-top: 75px solid #d8d8d8 !important;
    }
    
    .envelope-flap-inner {
        border-left: 121px solid transparent !important;
        border-right: 121px solid transparent !important;
        border-top: 64px solid #f2f2f2 !important;
    }
}

/* Improve touch targets for mobile */
@media (hover: none) {
    .envelope-container {
        /* Larger touch target on mobile */
        min-height: 220px;
        min-width: 250px;
    }
    
    .envelope-container:hover {
        transform: none;
    }

    .envelope-container:active {
        transform: scale(0.98);
    }

    .envelope-container.flipped:hover {
        transform: translateY(-20px) rotateY(180deg) scale(1.02);
    }

    .envelope-container.flipped:active {
        transform: translateY(-20px) rotateY(180deg) scale(0.98);
    }
    
    /* Enhanced touch feedback */
    .envelope-container:active .envelope-side {
        box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    }
}

/* Ensure proper touch behavior on all mobile devices */
@media (max-width: 768px) {
    .envelope-container {
        /* Ensure minimum touch target size (44px recommended) */
        min-height: 200px;
        min-width: 280px;
        /* Better touch response */
        -webkit-tap-highlight-color: transparent;
        tap-highlight-color: transparent;
    }
    
    /* Make envelope more prominent on small screens */
    .envelope-side {
        border: 4px solid #333;
    }
}