/* ============ Envelope intro overlay ============ */
body.env-locked { overflow: hidden; overscroll-behavior: none; }

#envelope {
  position: fixed; inset: 0;
  z-index: 10000;
  display: flex; justify-content: center;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  color: var(--ink);
}

/* ---------- backdrop ---------- */
#envelope .env-bg {
  position: absolute; inset: 0;
  background:
    radial-gradient(ellipse 80% 55% at 50% 42%, rgba(255,253,248,.95) 0%, rgba(255,253,248,0) 70%),
    radial-gradient(ellipse at 20% 0%, #F3E7D2 0%, rgba(243,231,210,0) 55%),
    radial-gradient(ellipse at 90% 100%, #EFE2C6 0%, rgba(236,222,196,0) 55%),
    linear-gradient(180deg, #FBF5EC 0%, var(--cream) 55%, #F2E8D6 100%);
  transition: opacity .9s var(--ease-out);
}
#envelope .env-scenery {
  position: absolute; left: 50%; top: 50%;
  width: max(100vw, 56.25vh); height: auto; max-width: none;
  transform: translate(-50%, -50%) scale(1.06);
  opacity: .42;
  filter: blur(3px) saturate(.9);
}
#envelope .env-veil {
  position: absolute; inset: 0;
  background: radial-gradient(ellipse 70% 50% at 50% 48%, rgba(255,253,248,.75), rgba(255,253,248,.15) 75%);
}

/* phone-width column that holds corners / text / envelope */
#envelope .env-frame {
  position: relative;
  width: 100%; max-width: var(--page-max); height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: max(24px, env(safe-area-inset-top)) var(--gutter) max(24px, env(safe-area-inset-bottom));
  gap: clamp(18px, 4.5vh, 40px);
}

#envelope .env-corner {
  position: absolute; width: min(48vw, 220px); height: auto;
  pointer-events: none; opacity: .95;
  transition: opacity .8s var(--ease-out), transform .9s var(--ease-out);
}
#envelope .env-c-bl { left: -6%; bottom: -3%; }
#envelope .env-c-br { right: -6%; bottom: -3%; transform: scaleX(-1); }
#envelope .env-c-tl { left: -6%; top: -3%; transform: scaleY(-1); width: min(38vw, 170px); opacity: .8; }
#envelope .env-c-tr { right: -6%; top: -3%; transform: scale(-1, -1); width: min(38vw, 170px); opacity: .8; }

/* ---------- falling gold hearts (thay cánh hoa) ---------- */
#envelope .env-petals { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
#envelope .env-heart {
  position: absolute; top: -30px;
  width: 14px; height: 13px;
  background: var(--fx-heart-foil) center / contain no-repeat;
  filter: drop-shadow(0 1px 2px rgba(138,98,36,.22));
  opacity: 0;
  will-change: transform, opacity;
  animation: env-fall var(--d) linear var(--dl) infinite;
}
#envelope .env-heart--line { background-image: var(--fx-heart-line); filter: none; }
@keyframes env-fall {
  0%   { transform: translate3d(0, 0, 0) rotate(var(--r0)) scale(var(--s)); opacity: 0; }
  10%  { opacity: .78; }
  30%  { transform: translate3d(calc(var(--dx) * .5), 30vh, 0) rotate(calc(var(--r0) * -1)) scale(var(--s)); }
  55%  { transform: translate3d(calc(var(--dx) * -.2), 56vh, 0) rotate(var(--r0)) scale(var(--s)); }
  85%  { opacity: .6; }
  100% { transform: translate3d(var(--dx), calc(100vh + 60px), 0) rotate(calc(var(--r0) * -1)) scale(var(--s)); opacity: 0; }
}

/* ---------- mở thiệp: trái tim vàng bung nhẹ từ dấu sáp ---------- */
#envelope .env-hburst {
  position: absolute; left: 50%; top: 57%; z-index: 7;
  width: 0; height: 0; pointer-events: none;
}
#envelope .env-hb {
  position: absolute; left: calc(var(--z) / -2); top: calc(var(--z) / -2);
  width: var(--z); height: calc(var(--z) * .9);
  background: var(--fx-heart-foil) center / contain no-repeat;
  opacity: 0;
  animation: env-hb 1.25s cubic-bezier(.2,.8,.3,1) var(--hd) forwards;
}
#envelope .env-hb.env-heart--line { background-image: var(--fx-heart-line); }
@keyframes env-hb {
  0%   { opacity: 0; transform: translate3d(0, 0, 0) rotate(0) scale(.35); }
  15%  { opacity: 1; }
  100% { opacity: 0; transform: translate3d(var(--x), var(--y), 0) rotate(var(--r)) scale(1); }
}
#envelope .env-hb-ring {
  position: absolute; left: -40px; top: -40px; width: 80px; height: 80px; border-radius: 50%;
  border: 1px solid rgba(217,182,110,.6);
  opacity: 0;
  animation: env-hb-ring 1s var(--ease-out) forwards;
}
@keyframes env-hb-ring { 0% { opacity: .5; transform: scale(.5); } 100% { opacity: 0; transform: scale(1.9); } }

/* ---------- greeting + hint ---------- */
#envelope .env-greet {
  position: relative; z-index: 2;
  display: flex; flex-direction: column; align-items: center; text-align: center;
  transition: opacity .5s var(--ease-out), transform .6s var(--ease-out);
  animation: env-rise 1.1s var(--ease-out) .15s backwards;
}
#envelope .env-greet-small {
  font-family: var(--font-sans); font-weight: 300;
  font-size: 12px; letter-spacing: .32em; text-transform: uppercase;
  color: var(--ink-soft);
}
#envelope .env-greet-name {
  font-size: clamp(38px, 11vw, 50px); line-height: 1.25;
  padding: 0 .15em .05em;
  max-width: 100%; overflow-wrap: anywhere;
  word-spacing: .16em; /* khoảng trắng của Great Vibes quá hẹp: "ChúNguyễn" */
}
#envelope .env-hint {
  position: relative; z-index: 2; margin: 0;
  font-family: var(--font-serif); font-style: italic;
  font-size: 16px; color: var(--ink-soft); letter-spacing: .02em;
  transition: opacity .4s var(--ease-out);
  animation: env-rise 1.1s var(--ease-out) .45s backwards;
}
@keyframes env-rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

/* ---------- envelope ---------- */
#envelope .env-stage { position: relative; z-index: 3; animation: env-rise 1.2s var(--ease-out) .25s backwards; }
#envelope .env-float { position: relative; animation: env-bob 4.2s var(--ease-in-out) infinite; }
@keyframes env-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-9px); } }

#envelope .env-shadow {
  position: absolute; left: 10%; right: 10%; bottom: -26px; height: 22px;
  border-radius: 50%;
  background: radial-gradient(ellipse at center, rgba(138,98,36,.28) 0%, rgba(138,98,36,0) 70%);
  animation: env-shadow 4.2s var(--ease-in-out) infinite;
  transition: opacity .6s var(--ease-out);
}
@keyframes env-shadow { 0%, 100% { transform: scaleX(1); opacity: 1; } 50% { transform: scaleX(.88); opacity: .7; } }

#envelope .env {
  position: relative;
  width: min(82vw, 340px);
  aspect-ratio: 10 / 7;
  perspective: 1100px;
  cursor: pointer;
}
#envelope .env-back,
#envelope .env-pocket,
#envelope .env-card,
#envelope .env-flap { position: absolute; }

#envelope .env-back {
  inset: 0; z-index: 0;
  border-radius: 6px;
  background: linear-gradient(180deg, #EADCC2 0%, #F1E6D2 60%, #F5ECDD 100%);
  box-shadow: 0 18px 40px -18px rgba(105,85,55,.45), 0 2px 6px rgba(105,85,55,.12);
  transition: opacity .55s var(--ease-out), transform .7s var(--ease-out);
}
#envelope .env-back::after,
#envelope .env-pocket::before,
#envelope .env-flap-inner::after,
#envelope .env-card-in::before {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: url("../assets/envelope-pattern.svg") repeat;
  background-size: 60px 60px;
  opacity: .14;
}

#envelope .env-pocket {
  inset: 0; z-index: 3;
  border-radius: 0 0 6px 6px;
  -webkit-clip-path: polygon(0 0, 50% 54%, 100% 0, 100% 100%, 0 100%);
  clip-path: polygon(0 0, 50% 54%, 100% 0, 100% 100%, 0 100%);
  background:
    linear-gradient(115deg, rgba(255,255,255,.55) 0%, rgba(255,255,255,0) 40%),
    linear-gradient(175deg, #FFF8F1 0%, #FAF3E7 55%, #F2E8D7 100%);
  transition: opacity .55s var(--ease-out), transform .7s var(--ease-out);
}
#envelope .env-pocket svg,
#envelope .env-flap-front svg { position: absolute; inset: 0; width: 100%; height: 100%; }

/* flap: two faces, 3D hinge at the top edge */
#envelope .env-flap {
  left: 0; right: 0; top: 0; height: 57%;
  z-index: 4;
  transform-origin: 50% 0;
  transform-style: preserve-3d;
  transform: rotateX(0deg);
  transition: transform .85s var(--ease-in-out), opacity .5s var(--ease-out);
  will-change: transform;
}
#envelope .env-flap-face {
  position: absolute; inset: 0;
  -webkit-backface-visibility: hidden; backface-visibility: hidden;
  -webkit-clip-path: polygon(0 0, 100% 0, 50% 100%);
  clip-path: polygon(0 0, 100% 0, 50% 100%);
}
#envelope .env-flap-front {
  background:
    linear-gradient(180deg, rgba(255,255,255,.5) 0%, rgba(255,255,255,0) 45%),
    linear-gradient(180deg, #FCF6EC 0%, #F6EDDE 70%, #EFE3CC 100%);
}
#envelope .env-flap-inner {
  transform: rotateX(180deg);
  background: linear-gradient(0deg, #EBDDC4 0%, #F3E8D5 100%);
}

/* inner card */
#envelope .env-card {
  left: 6.5%; right: 6.5%; top: 5%; height: 90%;
  z-index: 2;
  border-radius: 4px;
  background: linear-gradient(180deg, var(--ivory) 0%, #FFF9F0 100%);
  box-shadow: 0 10px 26px -12px rgba(105,85,55,.4);
  overflow: hidden;
  transform: translate3d(0, 0, 0);
  transition: transform 1s var(--ease-out), opacity .6s var(--ease-out);
  will-change: transform;
}
#envelope .env-card-in {
  position: absolute; inset: 7px;
  border: 1px solid rgba(201,154,75,.55);
  border-radius: 2px;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  padding: 7% 6% 0;
  text-align: center;
}
#envelope .env-card-in > * { position: relative; }
#envelope .env-card-kicker {
  font-family: var(--font-sans); font-weight: 400;
  font-size: 9.5px; letter-spacing: .34em; text-transform: uppercase;
  color: var(--gold-deep);
}
#envelope .env-card-names {
  display: flex; flex-direction: column; align-items: center;
  font-size: clamp(26px, 8vw, 33px); line-height: 1.12;
  margin-top: 4px; padding: 0 .2em .06em;
}
#envelope .env-amp { font-style: normal; font-size: .7em; line-height: 1; margin: .04em 0; color: var(--gold); -webkit-text-fill-color: var(--gold); }
#envelope .env-card-rule {
  width: 54px; height: 1px; margin: 6px 0 5px;
  background: var(--gold-gradient);
}
#envelope .env-card-date {
  font-family: var(--font-display); font-size: 14px; letter-spacing: .22em;
  color: var(--gold-deep);
}
/* foil sheen sweeping across the card once */
#envelope .env-card::after {
  content: ""; position: absolute; top: -20%; bottom: -20%; left: 0; width: 45%;
  background: linear-gradient(100deg, rgba(255,255,255,0) 0%, rgba(255,240,205,.75) 50%, rgba(255,255,255,0) 100%);
  transform: translateX(-160%) skewX(-12deg);
  opacity: 0; pointer-events: none;
}

/* ---------- wax seal button ---------- */
#envelope .env-seal {
  position: absolute; z-index: 6;
  left: 50%; top: 57%;
  width: 78px; height: 78px;
  margin: -39px 0 0 -39px;
  padding: 0; border: 0; border-radius: 50%;
  background: transparent;
  transition: transform .5s var(--ease-out), opacity .45s var(--ease-out);
}
#envelope .env-seal:focus { outline: none; }
#envelope .env-seal:focus-visible .env-seal-disc { box-shadow: 0 0 0 3px var(--ivory), 0 0 0 5px var(--gold); }
#envelope .env-seal-disc {
  position: absolute; inset: 0; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  /* Dấu sáp GIỮ màu hồng đậm nguyên bản (ngoại lệ duy nhất của palette kem–vàng) — không dùng var(--rose) vì token đó giờ là đồng */
  background: radial-gradient(circle at 35% 30%, #E58FA6 0%, #C96F8A 45%, #9E4A63 100%);
  box-shadow: 0 6px 14px -4px rgba(110,40,60,.5), inset 0 -3px 6px rgba(80,20,40,.25);
  transition: transform .25s var(--ease-out);
}
#envelope .env-seal-disc img {
  position: absolute; inset: -6%; width: 112%; height: 112%; max-width: none;
}
#envelope .env-seal-mono {
  position: relative;
  font-size: 23px; line-height: 1; white-space: nowrap;
  padding: .1em .12em .14em;
  filter: drop-shadow(0 1px 0 rgba(80,20,40,.35));
}
#envelope .env-seal:hover .env-seal-disc,
#envelope .env-seal:active .env-seal-disc { transform: scale(1.05); }
#envelope .env-seal-ring,
#envelope .env-seal-ring::after {
  position: absolute; inset: -4px; border-radius: 50%;
  border: 1.5px solid var(--gold-light);
  opacity: 0;
  animation: env-pulse 2.2s var(--ease-out) infinite;
  pointer-events: none;
}
#envelope .env-seal-ring::after { content: ""; inset: -5.5px; animation-delay: 1.1s; }
@keyframes env-pulse {
  0%   { transform: scale(.92); opacity: .9; }
  100% { transform: scale(1.55); opacity: 0; }
}

/* ---------- final gold glow ---------- */
#envelope .env-glow {
  position: absolute; left: 50%; top: 45%;
  width: 140vmax; height: 140vmax; margin: -70vmax 0 0 -70vmax;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,246,225,.95) 0%, rgba(243,222,164,.55) 22%, rgba(243,222,164,0) 55%);
  opacity: 0; transform: scale(.2);
  pointer-events: none; z-index: 5;
}

/* =================== choreography =================== */
/* 0ms: seal cracks, texts fade, bob pauses */
#envelope.is-opening .env-float,
#envelope.is-opening .env-shadow { animation-play-state: paused; }
#envelope.is-opening .env-seal { transform: scale(1.35) rotate(-8deg); opacity: 0; pointer-events: none; }
#envelope.is-opening .env-seal-ring { animation: none; opacity: 0; }
#envelope.is-opening .env-hint { opacity: 0; }
#envelope.is-opening .env-greet { opacity: 0; transform: translateY(-10px); }
#envelope.is-opening .env { cursor: default; }

/* 180ms: flap swings open */
#envelope.is-flap .env-flap { transform: rotateX(-180deg); }
/* ~600ms: flap past 90° → drop behind the card */
#envelope.is-flap-back .env-flap { z-index: 1; }

/* 950ms: card slides up out of the envelope */
#envelope.is-card .env-card { transform: translate3d(0, -62%, 0); }
#envelope.is-card .env-card::after { animation: env-sheen 1.1s var(--ease-out) .35s forwards; }
@keyframes env-sheen {
  0%   { transform: translateX(-160%) skewX(-12deg); opacity: 0; }
  20%  { opacity: 1; }
  100% { transform: translateX(330%) skewX(-12deg); opacity: 0; }
}

/* 2050ms: card zooms toward viewer, envelope sinks, backdrop fades, gold glow */
#envelope.is-leaving { pointer-events: none; }
#envelope.is-leaving .env-bg { opacity: 0; }
#envelope.is-leaving .env-back,
#envelope.is-leaving .env-pocket,
#envelope.is-leaving .env-flap { opacity: 0; transform: translateY(36px); transition-duration: .55s; }
#envelope.is-leaving .env-flap { transform: translateY(36px) rotateX(-180deg); }
#envelope.is-leaving .env-shadow { opacity: 0; }
#envelope.is-leaving .env-card {
  transform: translate3d(0, -30%, 0) scale(1.9);
  opacity: 0;
  transition: transform .9s var(--ease-out), opacity .55s var(--ease-out) .3s;
}
#envelope.is-leaving .env-corner { opacity: 0; }
#envelope.is-leaving .env-c-bl { transform: translate(-30px, 30px); }
#envelope.is-leaving .env-c-br { transform: scaleX(-1) translate(-30px, 30px); }
#envelope.is-leaving .env-c-tl { transform: scaleY(-1) translate(-30px, 30px); }
#envelope.is-leaving .env-c-tr { transform: scale(-1, -1) translate(-30px, 30px); }
#envelope.is-leaving .env-petals { opacity: 0; transition: opacity .9s var(--ease-out); }
#envelope.is-leaving .env-glow { animation: env-glow .9s var(--ease-out) forwards; }
@keyframes env-glow {
  0%   { opacity: 0; transform: scale(.2); }
  35%  { opacity: .9; }
  100% { opacity: 0; transform: scale(1); }
}

/* reduced motion: one simple fade */
#envelope.is-fade { opacity: 0; transition: opacity .4s ease; pointer-events: none; }
@media (prefers-reduced-motion: reduce) {
  #envelope .env-float, #envelope .env-shadow, #envelope .env-seal-ring, #envelope .env-seal-ring::after { animation: none; }
  #envelope.is-fade { transition-duration: .4s !important; }
}

/* short / landscape screens */
@media (max-height: 560px) {
  #envelope .env-frame { gap: 12px; }
  #envelope .env-greet-name { font-size: 34px; }
  #envelope .env { width: min(60vh, 300px); }
}
