/* ==========================================================================
   RESET & VARIABLES (OPTIMIZED FOR 60+ FPS)
   ========================================================================== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-dark: #14110f;
  --gold-warm: #d4a342;
  --ink-color: #261c14;
  --ink-muted: #5e4a3b;
  --font-hand: 'Caveat', cursive, sans-serif;
  --font-serif: 'Cormorant Garamond', Georgia, serif;
  --font-ui: 'Montserrat', system-ui, -apple-system, sans-serif;
  --ease-snappy: cubic-bezier(0.16, 1, 0.3, 1);
  --dur-fast: 0.32s;
}

html, body {
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  background-color: var(--bg-dark);
  font-family: var(--font-ui);
  color: #f3efe9;
  user-select: none;
  -webkit-font-smoothing: antialiased;
}

/* ==========================================================================
   SCENE CONTAINER (NO HEAVY BLURS)
   ========================================================================== */
.scene-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  min-height: 550px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 40%, #261e18 0%, #13100e 70%, #0a0807 100%);
  overflow: hidden;
  contain: paint;
}

.ambient-glow {
  position: absolute;
  top: 35%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  width: min(650px, 110vw);
  height: min(650px, 110vw);
  background: radial-gradient(circle, rgba(230, 175, 55, 0.12) 0%, rgba(0,0,0,0) 65%);
  pointer-events: none;
  z-index: 1;
}

/* ==========================================================================
   FLOWERS CARD
   ========================================================================== */
.flowers-card {
  position: relative;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  outline: none;
  transform: translate3d(0, 0, 0);
  transition: transform var(--dur-fast) var(--ease-snappy), opacity var(--dur-fast) ease;
  will-change: transform, opacity;
}

.flowers-card:hover .photo-frame {
  transform: translate3d(0, -3px, 0);
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.55), 0 0 18px rgba(220, 165, 45, 0.2);
}

.flowers-card:active {
  transform: scale(0.98);
}

.photo-frame {
  position: relative;
  width: clamp(270px, 78vw, 380px);
  height: clamp(270px, 78vw, 380px);
  border-radius: 16px;
  overflow: hidden;
  background: #251e18;
  padding: 6px;
  box-shadow: 0 14px 32px rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 235, 190, 0.12);
  transition: transform 0.25s ease, box-shadow 0.25s ease;
}

.flower-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 10px;
  display: block;
}

.photo-reflection {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 40%);
  pointer-events: none;
}

.caption-tag {
  margin-top: 18px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-serif);
  font-size: clamp(1rem, 3vw, 1.2rem);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #ebd3a7;
  text-shadow: 0 2px 4px rgba(0,0,0,0.6);
}

.caption-tag .sparkle {
  color: var(--gold-warm);
  font-size: 0.8em;
}

/* When flowers pushed back: lightweight opacity + scale, NO heavy blur filter */
.scene-container.state-envelope .flowers-card {
  transform: translate3d(0, -20px, 0) scale(0.88);
  opacity: 0.15;
  pointer-events: none;
}

/* ==========================================================================
   ENVELOPE OVERLAY & CONTAINER
   ========================================================================== */
.envelope-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  pointer-events: none;
  opacity: 0;
  background: rgba(10, 8, 6, 0);
  transition: opacity var(--dur-fast) ease, background var(--dur-fast) ease;
}

.scene-container.state-envelope .envelope-overlay {
  opacity: 1;
  pointer-events: auto;
}

.scene-container.state-letter-reading .envelope-overlay {
  background: rgba(10, 8, 6, 0.85); /* Solid high-contrast overlay, zero GPU blur */
}

.envelope-container {
  position: relative;
  perspective: 900px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ==========================================================================
   REALISTIC PHYSICAL ENVELOPE
   ========================================================================== */
.envelope {
  position: relative;
  width: clamp(300px, 84vw, 390px);
  height: clamp(190px, 54vw, 245px);
  background: #9d7348 url('kraft.jpg') center/cover no-repeat;
  border-radius: 8px;
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.6);
  cursor: pointer;
  outline: none;
  transform: translate3d(0, 20px, 0) scale(0.96);
  opacity: 0;
  transition: transform var(--dur-fast) var(--ease-snappy), opacity var(--dur-fast) ease;
  will-change: transform, opacity;
}

.scene-container.state-envelope .envelope {
  transform: translate3d(0, 0, 0) scale(1);
  opacity: 1;
}

/* Envelope interior back */
.envelope-back {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #734e2b url('kraft.jpg') center/cover no-repeat;
  filter: brightness(0.72);
  border-radius: 8px;
  box-shadow: inset 0 0 20px rgba(0,0,0,0.5);
  z-index: 1;
}

/* ==========================================================================
   ENVELOPE POCKET (OPTIMIZED FOLDS)
   ========================================================================== */
.envelope-pocket {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 4;
  pointer-events: none;
}

.pocket-fold {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('kraft.jpg') center/cover no-repeat;
}

.pocket-left {
  clip-path: polygon(0 0, 52% 50%, 0 100%);
  filter: brightness(0.92);
}

.pocket-right {
  clip-path: polygon(100% 0, 48% 50%, 100% 100%);
  filter: brightness(0.95);
}

.pocket-bottom {
  clip-path: polygon(0 100%, 50% 48%, 100% 100%);
  filter: brightness(1.02);
  border-bottom: 2px solid rgba(0,0,0,0.15);
}

/* ==========================================================================
   TOP FLAP (3D FOLD)
   ========================================================================== */
.envelope-flap {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 60%;
  transform-origin: top center;
  transform: rotateX(0deg);
  transition: transform 0.38s var(--ease-snappy), z-index 0.15s ease;
  z-index: 5;
  will-change: transform;
}

.flap-triangle {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('kraft.jpg') center/cover no-repeat;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  filter: brightness(1.06);
}

.envelope.is-open .envelope-flap {
  transform: rotateX(180deg);
  z-index: 2;
}

/* ==========================================================================
   WAX SEAL (LIGHTWEIGHT 40KB ASSET)
   ========================================================================== */
.envelope-seal {
  position: absolute;
  top: 58%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  width: 62px;
  height: 62px;
  z-index: 6;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.6));
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
  will-change: transform, opacity;
}

.seal-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.envelope:hover .envelope-seal {
  transform: translate3d(-50%, -50%, 0) scale(1.05);
}

.envelope.is-open .envelope-seal {
  opacity: 0;
  transform: translate3d(-50%, -50%, 0) scale(0.6);
}

/* ==========================================================================
   THE LETTER (TRANSFORM-ONLY 60FPS ACCELERATION)
   ========================================================================== */
.letter {
  position: absolute;
  top: 50%;
  left: 50%;
  width: min(460px, 90vw);
  height: min(580px, 82vh);
  background: #fbf7ee url('parchment.jpg') center/cover no-repeat;
  border-radius: 10px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.3);
  z-index: 3;
  transform: translate3d(-50%, -46%, 0) scale(0.66);
  opacity: 0;
  transition: transform 0.38s var(--ease-snappy), opacity 0.25s ease, box-shadow 0.35s ease;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  pointer-events: none;
  will-change: transform, opacity;
}

.letter-texture {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    to bottom,
    rgba(255,255,255,0.2) 0%,
    transparent 48%,
    rgba(70, 50, 30, 0.07) 50%,
    rgba(255,255,255,0.35) 51%,
    transparent 53%,
    rgba(0,0,0,0.03) 100%
  );
  pointer-events: none;
  z-index: 2;
}

.letter-content {
  position: relative;
  z-index: 3;
  padding: 24px 22px 20px 22px;
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow-y: auto;
  opacity: 0;
  transition: opacity 0.25s ease;
  scrollbar-width: thin;
  scrollbar-color: #d1b896 transparent;
}

.letter-content::-webkit-scrollbar {
  width: 4px;
}
.letter-content::-webkit-scrollbar-thumb {
  background: #d1b896;
  border-radius: 4px;
}

.letter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px dashed rgba(110, 80, 50, 0.25);
  padding-bottom: 6px;
  margin-bottom: 14px;
}

.letter-botanical {
  font-size: 1.5rem;
}

.letter-date {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 1rem;
  color: var(--ink-muted);
}

.letter-salutation {
  font-family: var(--font-hand);
  font-size: clamp(1.7rem, 3.8vw, 2.1rem);
  font-weight: 700;
  color: var(--ink-color);
  margin-bottom: 12px;
  line-height: 1.2;
}

.letter-text {
  font-family: var(--font-hand);
  font-size: clamp(1.35rem, 3vw, 1.6rem);
  line-height: 1.42;
  color: var(--ink-color);
  flex-grow: 1;
}

.letter-text p {
  margin-bottom: 12px;
}

.letter-footer {
  margin-top: 14px;
  text-align: right;
  border-top: 1px dashed rgba(110, 80, 50, 0.2);
  padding-top: 8px;
}

.letter-farewell {
  font-family: var(--font-hand);
  font-size: 1.3rem;
  color: var(--ink-muted);
}

.letter-signature {
  font-family: var(--font-hand);
  font-size: 1.7rem;
  font-weight: 700;
  color: #7d261e;
  margin-top: 2px;
}

.letter-controls {
  margin-top: 16px;
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

.btn-action {
  background: #35281d;
  color: #f7ede0;
  border: 1px solid rgba(255, 230, 180, 0.18);
  padding: 8px 16px;
  border-radius: 20px;
  font-family: var(--font-ui);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.25);
  transition: background 0.2s ease, transform 0.2s ease;
}

.btn-action:hover {
  background: #4a3828;
  transform: translate3d(0, -2px, 0);
}

.btn-action.btn-secondary {
  background: transparent;
  color: #5e4635;
  border-color: rgba(94, 70, 53, 0.35);
}

.btn-action.btn-secondary:hover {
  background: rgba(94, 70, 53, 0.1);
  color: #3b2c21;
}

/* ==========================================================================
   INTERACTION PHASES: SNAPPY & ACCELERATED
   ========================================================================== */
/* Phase 1: Letter peeks out of envelope pocket */
.envelope.is-open .letter {
  transform: translate3d(-50%, -82%, 0) scale(0.66);
  opacity: 1;
  box-shadow: 0 10px 24px rgba(0,0,0,0.3);
}

/* Phase 2: Letter scales and centers in front smoothly */
.scene-container.state-letter-reading .letter {
  transform: translate3d(-50%, -50%, 0) scale(1) !important;
  z-index: 100;
  opacity: 1;
  box-shadow: 0 24px 55px rgba(0, 0, 0, 0.8);
  pointer-events: auto;
}

.scene-container.state-letter-reading .letter-content {
  opacity: 1;
}

/* ==========================================================================
   INSTRUCTION BANNER & RESET BUTTON
   ========================================================================== */
.instruction-banner {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translate3d(-50%, 0, 0);
  z-index: 20;
  background: rgba(26, 20, 16, 0.85);
  border: 1px solid rgba(230, 185, 95, 0.22);
  padding: 9px 20px;
  border-radius: 30px;
  box-shadow: 0 8px 20px rgba(0,0,0,0.35);
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.instruction-banner p {
  font-family: var(--font-serif);
  font-size: clamp(0.92rem, 2.6vw, 1.05rem);
  letter-spacing: 0.06em;
  color: #f7e8cf;
}

.scene-container.state-letter-reading .instruction-banner {
  opacity: 0;
  transform: translate3d(-50%, 10px, 0);
}

/* Reset button */
.btn-reset {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 25;
  background: rgba(32, 25, 20, 0.8);
  border: 1px solid rgba(255, 235, 195, 0.18);
  color: #eed6aa;
  padding: 8px 15px;
  border-radius: 20px;
  font-family: var(--font-ui);
  font-size: 0.8rem;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(0,0,0,0.3);
  opacity: 0;
  pointer-events: none;
  transform: translate3d(0, -8px, 0);
  transition: all 0.25s ease;
}

.btn-reset:hover {
  background: rgba(48, 36, 28, 0.95);
  color: #fff;
  transform: translate3d(0, 0, 0);
}

.scene-container.state-envelope .btn-reset,
.scene-container.state-letter-reading .btn-reset {
  opacity: 1;
  pointer-events: auto;
  transform: translate3d(0, 0, 0);
}


/* ==========================================================================
   RESPONSIVE OPTIMIZATIONS
   ========================================================================== */
@media (max-width: 480px) {
  .photo-frame {
    width: 80vw;
    height: 80vw;
  }
  .envelope {
    width: 86vw;
    height: 56vw;
  }
  .letter {
    width: 92vw;
    height: 84vh;
  }
  .letter-content {
    padding: 16px 14px;
  }
  .letter-salutation {
    font-size: 1.6rem;
  }
  .letter-text {
    font-size: 1.3rem;
  }
  .letter-signature {
    font-size: 1.5rem;
  }
}

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