/* ==================== ENVELOPE SCENE (fullscreen overlay) ==================== */
.envelope-scene {
  position: fixed;
  inset: 0;
  z-index: 90000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at center, #4a1515 0%, #2a0505 30%, #0a0001 60%, #000000 100%);
  font-family: 'Quicksand', sans-serif;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 700ms ease;
}

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

/* Vignette overlay */
.envelope-scene::before {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, transparent 60%, rgba(10, 0, 0, 0.15) 100%);
  pointer-events: none;
  z-index: 0;
}

/* Noise texture overlay */
.envelope-scene::after {
  content: '';
  position: absolute;
  inset: 0;
  /* Noise inline (data URI) — tránh phụ thuộc host ngoài grainy-gradients.vercel.app hay treo vài giây */
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  opacity: 0.06;
  pointer-events: none;
  z-index: 0;
  filter: contrast(150%);
}

.envelope-scene .main-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  position: relative;
  z-index: 2;
}

/* ==================== ENVELOPE WRAPPER ==================== */
.envelope-wrapper {
  position: relative;
  width: 320px;
  height: 200px;
  cursor: pointer;
  perspective: 1000px;
}

/* ==================== CARD (inside envelope) ==================== */
.card {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) rotateZ(-90deg);
  transform-origin: center center;
  width: 200px;
  height: 320px;
  z-index: 5;
  transform-style: preserve-3d;
  opacity: 0;
  visibility: hidden;
}

.envelope-wrapper.open .card {
  opacity: 1;
  visibility: visible;
  z-index: 30;
  animation: cardFlyOut 1.5s ease-out 0.3s forwards;
}

@keyframes cardFlyOut {
  0%   { bottom: 20px;  transform: translateX(-50%) rotateZ(-90deg); }
  50%  { bottom: 280px; transform: translateX(-50%) rotateZ(-90deg); }
  100% { bottom: 0px;   transform: translateX(-50%) rotateZ(6deg); }
}

.card-inner {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transform-origin: left center;
  transition: transform 0.6s ease;
  z-index: 2;
}

.card-cover {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(145deg, #ffffff 0%, #f8f8f8 100%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  backface-visibility: hidden;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}

.card-back {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #fff0f3;
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  border-radius: 8px;
  transform: rotateY(180deg);
  backface-visibility: hidden;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}

.card-back::after {
  content: '\2764';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 60px;
  color: rgba(233, 30, 99, 0.15);
  pointer-events: none;
}

.card-back.has-image::after { display: none; }

.envelope-wrapper.open .card.animation-done:hover .card-inner {
  transform: rotateY(-150deg);
}

.card-body {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background: linear-gradient(145deg, #ffffff 0%, #fffafa 100%);
  border-radius: 8px;
  padding: 20px 18px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  box-shadow: inset 0 0 20px rgba(0,0,0,0.03);
  z-index: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

/* Fade out envelope pieces after card flies out (removes grey/white phantom behind letter) */
.envelope-wrapper.open .envelope {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.8s ease 1.4s;
}

.card-body::-webkit-scrollbar { width: 4px; }
.card-body::-webkit-scrollbar-track { background: rgba(233, 30, 99, 0.1); border-radius: 2px; }
.card-body::-webkit-scrollbar-thumb { background: rgba(233, 30, 99, 0.4); border-radius: 2px; }

.card-greeting {
  font-family: 'Dancing Script', cursive;
  font-size: 18px;
  font-weight: 700;
  color: #c2185b;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.card-message {
  font-family: 'Dancing Script', cursive;
  font-size: 15px;
  font-weight: 700;
  color: #333;
  line-height: 1.6;
  margin-bottom: 15px;
  flex-grow: 1;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.card-ending {
  font-family: 'Dancing Script', cursive;
  font-size: 16px;
  font-weight: 700;
  color: #c2185b;
  text-align: right;
  flex-shrink: 0;
  margin-top: auto;
}

.card-body .typewriter { overflow: hidden; white-space: normal; }
.card-body .typewriter span { opacity: 0; display: inline; animation: fadeInChar 0.3s forwards; }

@keyframes fadeInChar {
  0%   { opacity: 0; transform: translateY(5px); }
  100% { opacity: 1; transform: translateY(0); }
}

.card-title { text-align: center; }

.card-title-secondary {
  display: block;
  font-family: 'Quicksand', sans-serif;
  font-size: 14px;
  font-weight: 600;
  color: #888;
  letter-spacing: 3px;
  text-transform: uppercase;
}

.card-title-main {
  display: block;
  font-family: 'Pacifico', cursive;
  font-size: 32px;
  color: #e91e63;
  margin: 8px 0;
}

/* ==================== ENVELOPE ==================== */
.envelope {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 320px;
  height: 200px;
  z-index: 10;
}

.envelope-back {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #d5d5d5;
  border-radius: 0 0 10px 10px;
}

.envelope-lid {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  border-left: 160px solid transparent;
  border-right: 160px solid transparent;
  border-top: 120px solid #e0e0e0;
  transform-origin: top center;
  transition: transform 0.5s ease-in-out;
  z-index: 25;
}

.envelope-wrapper.open .envelope-lid { transform: rotateX(180deg); }

.envelope-front {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 20;
  pointer-events: none;
}

.envelope-front::before {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 0;
  border-left: 160px solid transparent;
  border-right: 160px solid transparent;
  border-bottom: 120px solid #f0f0f0;
}

.envelope-front::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  background: linear-gradient(to top, #f5f5f5 0%, #f5f5f5 60%, transparent 100%);
  border-radius: 0 0 10px 10px;
}

/* ==================== HEART SEAL ==================== */
.heart-seal {
  position: absolute;
  top: 68px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 45px;
  z-index: 30;
  transition: all 0.3s ease;
  cursor: pointer;
}

.heart-seal::before,
.heart-seal::after {
  content: '';
  position: absolute;
  width: 25px;
  height: 40px;
  background: linear-gradient(135deg, #ff6b9d 0%, #e91e63 100%);
  border-radius: 25px 25px 0 0;
  box-shadow: 0 2px 8px rgba(233, 30, 99, 0.4);
}

.heart-seal::before { left: 25px; transform: rotate(-45deg); transform-origin: 0 100%; }
.heart-seal::after  { left: 0;    transform: rotate(45deg);  transform-origin: 100% 100%; }

.envelope-wrapper.open .heart-seal {
  opacity: 0;
  transform: translateX(-50%) scale(0);
}

/* ==================== CLICK INSTRUCTION ==================== */
.envelope-scene .click-instruction {
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
  letter-spacing: 1px;
  animation: pulse-text 2s ease-in-out infinite;
  transition: opacity 0.3s;
  margin-top: -10px;
}

@keyframes pulse-text {
  0%, 100% { opacity: 0.7; }
  50%      { opacity: 1; }
}

/* ==================== FLYING HEARTS BACKGROUND ==================== */
.envelope-scene .flying-hearts-container {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  overflow: hidden;
}

.envelope-scene .flying-heart {
  position: absolute;
  background-color: #ff526f;
  transform: rotate(45deg);
  animation: floatUp 8s ease-out forwards;
  filter: brightness(1.2) drop-shadow(0 0 10px rgba(255, 82, 111, 0.8)) drop-shadow(0 0 20px rgba(255, 82, 111, 0.6));
  will-change: transform, opacity, bottom;
}

.envelope-scene .flying-heart::before,
.envelope-scene .flying-heart::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  background-color: inherit;
}

.envelope-scene .flying-heart::before { top: -50%; left: 0;    width: 100%; height: 100%; }
.envelope-scene .flying-heart::after  { top: 0;    left: -50%; width: 100%; height: 100%; }

@keyframes floatUp {
  0%   { transform: rotate(45deg) translateY(0) rotate(0deg)   scale(0); opacity: 0; bottom: -50px; }
  5%   { opacity: 1; transform: rotate(45deg) translateY(0) rotate(18deg)  scale(1); }
  95%  { opacity: 1; transform: rotate(45deg) translateY(0) rotate(342deg) scale(1); }
  100% { transform: rotate(45deg) translateY(0) rotate(360deg) scale(0); opacity: 0; bottom: calc(100vh + 50px); }
}

/* ==================== MODAL ==================== */
.envelope-scene .modal {
  display: none;
  position: fixed;
  z-index: 92000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(20, 20, 40, 0.9);
  backdrop-filter: blur(10px);
}

.envelope-scene .modal.show {
  display: flex;
  align-items: center;
  justify-content: center;
}

.envelope-scene .modal-content {
  background: white;
  border-radius: 20px;
  max-width: 450px;
  width: 90%;
  max-height: 90vh;
  max-height: 90dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  position: relative;
  padding: 40px;
  animation: modal-in 0.5s ease;
}

@keyframes modal-in {
  from { transform: scale(0.8); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}

.envelope-scene .close {
  position: absolute;
  right: 15px;
  top: 15px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #e91e63;
  color: white;
  font-size: 24px;
  cursor: pointer;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.3s;
}

.envelope-scene .close:hover { transform: scale(1.1) rotate(90deg); }

.envelope-scene .polaroid-image {
  margin-bottom: 20px;
  opacity: 0;
  transform: scale(0.95);
  transition: opacity 0.8s ease, transform 0.8s ease;
}

.envelope-scene .polaroid-image.fade-in { opacity: 1; transform: scale(1); }

.envelope-scene .polaroid-image img {
  width: 60%;
  max-height: 250px;
  object-fit: cover;
  border-radius: 10px;
  display: block;
  margin: 0 auto;
}

.envelope-scene .polaroid-text {
  text-align: center;
  overflow-wrap: break-word;
  word-wrap: break-word;
  word-break: break-word;
}

.envelope-scene .polaroid-text h2 {
  color: #e91e63;
  font-family: 'Pacifico', cursive;
  font-size: 24px;
  margin-bottom: 15px;
  overflow-wrap: break-word;
}

.envelope-scene .polaroid-text p {
  color: #555;
  font-family: 'Dancing Script', cursive;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.8;
  margin-bottom: 10px;
  overflow-wrap: break-word;
}

.envelope-scene .polaroid-text .signature {
  color: #e91e63;
  font-family: 'Dancing Script', cursive;
  font-style: italic;
  font-weight: 700;
  margin-top: 20px;
}

.envelope-scene .polaroid-text .typewriter-modal {
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: normal;
}

.envelope-scene .polaroid-text .typewriter-modal span {
  opacity: 0;
  display: inline;
  animation: fadeInCharModal 0.3s forwards;
}

@keyframes fadeInCharModal {
  0%   { opacity: 0; filter: blur(2px); }
  100% { opacity: 1; filter: blur(0); }
}

/* ==================== HEART CLOSE BUTTON ==================== */
.envelope-scene .heart-close-btn {
  text-align: center;
  margin-top: 20px;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.envelope-scene .heart-close-btn img {
  width: 60px;
  height: 60px;
  object-fit: contain;
  filter: drop-shadow(0 2px 8px rgba(233, 30, 99, 0.4));
  transition: transform 0.3s ease;
}

.envelope-scene .heart-close-btn:hover img  { transform: scale(1.15); }
.envelope-scene .heart-close-btn:active img { transform: scale(0.95); }

/* ==================== FLYING IMAGES ==================== */
.envelope-scene .flying-images-container {
  position: fixed;
  inset: 0;
  z-index: 93000;
  pointer-events: none;
  overflow: hidden;
}

.envelope-scene .flying-photo {
  position: fixed;
  top: 110%;
  border-radius: 12px;
  border: 3px solid rgba(255, 255, 255, 0.8);
  box-shadow:
    0 0 10px rgba(233, 30, 99, 0.6),
    0 0 20px rgba(233, 30, 99, 0.4),
    0 0 40px rgba(233, 30, 99, 0.2);
  object-fit: cover;
  opacity: 0;
  z-index: 93001;
  will-change: transform, opacity;
  animation: photoFlyUp 7s linear forwards;
}

@keyframes photoFlyUp {
  0%   { opacity: 0; transform: translateX(-50%) translateY(0)     rotate(0deg);  }
  5%   { opacity: 1; transform: translateX(-50%) translateY(0)     rotate(0deg);  }
  15%  { transform: translateX(-50%) translateY(-33vh)  rotate(4deg);  opacity: 1; }
  30%  { transform: translateX(-50%) translateY(-66vh)  rotate(-3deg); }
  45%  { transform: translateX(-50%) translateY(-99vh)  rotate(3deg);  }
  60%  { transform: translateX(-50%) translateY(-132vh) rotate(-2deg); }
  75%  { transform: translateX(-50%) translateY(-165vh) rotate(2deg);  }
  90%  { opacity: 1; transform: translateX(-50%) translateY(-198vh) rotate(-1deg); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-220vh) rotate(0deg);  }
}

.envelope-scene .main-content.hidden {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s ease;
}

/* ==================== RESPONSIVE ==================== */
@media (max-width: 400px) {
  .envelope-wrapper { width: 280px; height: 450px; }
  .envelope { width: 280px; height: 175px; }
  .envelope-lid {
    border-left-width: 140px;
    border-right-width: 140px;
    border-top-width: 105px;
  }
  .envelope-front::before {
    border-left-width: 140px;
    border-right-width: 140px;
    border-bottom-width: 105px;
  }
  .heart-seal { top: 58px; }
  .heart-seal::before,
  .heart-seal::after { width: 22px; height: 35px; }
  .heart-seal::before { left: 22px; }

  .card { width: 175px; height: 280px; bottom: 15px; }
  .envelope-wrapper.open .card {
    z-index: 30;
    animation: cardFlyOutMobile 1.5s ease-out 0.3s forwards;
  }
  @keyframes cardFlyOutMobile {
    0%   { bottom: 15px;  transform: translateX(-50%) rotateZ(-90deg); }
    50%  { bottom: 240px; transform: translateX(-50%) rotateZ(-90deg); }
    100% { bottom: 0px;   transform: translateX(-50%) rotateZ(6deg); }
  }

  .card-title-main       { font-size: 26px; }
  .card-title-secondary  { font-size: 12px; }
  .card-body             { padding: 20px; }
  .card-greeting         { font-size: 18px; }
  .card-message          { font-size: 13px; }
  .card-ending           { font-size: 14px; }

  .envelope-scene .modal-content { padding: 20px; width: 92%; }
  .envelope-scene .close { width: 32px; height: 32px; font-size: 20px; right: 10px; top: 10px; }
  .envelope-scene .polaroid-text h2 { font-size: 18px; margin-bottom: 10px; }
  .envelope-scene .polaroid-text p  { font-size: 13px; line-height: 1.6; }
  .envelope-scene .polaroid-text .signature { font-size: 13px; margin-top: 12px; }
}

/* Modal responsive cho điện thoại rộng 400–480px (chữ dài không tràn) */
@media (max-width: 480px) {
  .envelope-scene .modal-content {
    padding: 24px 20px;
    width: 92%;
    max-height: 85vh;
    max-height: 85dvh;
  }
  .envelope-scene .polaroid-image img { width: 70%; max-height: 200px; }
  .envelope-scene .polaroid-text h2 { font-size: 20px; margin-bottom: 12px; }
  .envelope-scene .polaroid-text p  { font-size: 14px; line-height: 1.6; }
  .envelope-scene .polaroid-text .signature { font-size: 14px; margin-top: 14px; }
}
