/* ==========================================================================
   Envelope intro. Hidden unless envelope-gate.js added html.env-on.
   Sequence classes on .envelope (set by envelope.js):
   is-seal → is-flap → is-flap-behind → is-card → is-fall → is-reveal
   ========================================================================== */

.envelope { display: none; }
html.env-on .envelope {
  position: fixed; inset: 0; z-index: 1000;
  display: grid; place-items: center;
  overflow: hidden;
  padding: max(24px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  background:
    radial-gradient(ellipse 80% 60% at 50% 42%, rgba(184, 134, 47, .16), transparent 70%),
    radial-gradient(ellipse at 50% 40%, #4A3324 0%, #2B1D14 58%, #1B120C 100%);
  transition: opacity 900ms ease, visibility 0s linear 900ms;
}
html.env-on:not(.env-done), html.env-on:not(.env-done) body { overflow: hidden; }
#couple:focus { outline: none; }

/* subtle linen texture */
html.env-on .envelope::before {
  content: ""; position: absolute; inset: 0; pointer-events: none; opacity: .5;
  background:
    repeating-linear-gradient(0deg, rgba(255,255,255,.018) 0 1px, transparent 1px 3px),
    repeating-linear-gradient(90deg, rgba(0,0,0,.05) 0 1px, transparent 1px 4px);
}

.envelope__stage {
  --w: min(88vw, 560px, calc((100dvh - 230px) * 1.5));
  position: relative; z-index: 2;
  display: grid; justify-items: center; gap: 14px;
  text-align: center;
}
.envelope__kicker {
  margin: 0; font-family: var(--f-ui); font-size: .8125rem; font-weight: 500;
  letter-spacing: .34em; text-transform: uppercase; color: #D9B98A;
}
.envelope__names {
  margin: 0 0 6px; font-family: var(--f-script); font-size: clamp(2.6rem, 9vw, 4.25rem); line-height: 1;
  color: #F3E6D0; text-shadow: 0 2px 18px rgba(0,0,0,.35);
}
.envelope__names span { font-family: var(--f-serif); font-style: italic; font-size: .55em; color: #D9B98A; vertical-align: .2em; }
.envelope__hint {
  margin: 10px 0 0; font-family: var(--f-serif); font-style: italic; font-size: 1.25rem; color: #E6D3B5;
  animation: hint-breathe 2.8s ease-in-out infinite;
}
.envelope__skip {
  position: absolute; z-index: 3; right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom));
  min-height: 44px; padding: 8px 14px;
  background: transparent; border: 1px solid rgba(243, 230, 208, .35); border-radius: 999px;
  color: #E6D3B5; font: 500 .8125rem var(--f-ui); letter-spacing: .08em; cursor: pointer;
  transition: background-color 200ms, border-color 200ms;
}
.envelope__skip:hover { background: rgba(243, 230, 208, .08); border-color: rgba(243, 230, 208, .6); }
.envelope__skip:focus-visible, .env-open:focus-visible { outline: 3px solid #E0A458; outline-offset: 6px; }

/* ---- The envelope -------------------------------------------------------- */
.env-open {
  -webkit-appearance: none; appearance: none;
  padding: 0; margin: 0; border: 0; background: none; cursor: pointer;
  border-radius: 8px; touch-action: manipulation;
}
.env {
  position: relative; display: block;
  width: var(--w); aspect-ratio: 3 / 2;
  transition: transform 300ms ease;
}
@media (hover: hover) {
  .env-open:hover .env { transform: translateY(-4px) rotate(-.4deg); }
}
.env-open:not(:disabled) .env { animation: env-float 5s ease-in-out infinite; }

/* No transform/perspective here until the fall, so the card can layer between back and pocket. */
.env__body { position: absolute; inset: 0; display: block; }
.env__back, .env__pocket { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.env__back { z-index: 1; border-radius: 6px; }
/* Envelope shadow lives on the body (not a filter on .env): Safari clips
   filtered elements to their box, which would cut off the opened flap and card. */
.env__body::before {
  content: ""; position: absolute; inset: 0; z-index: 0; border-radius: 6px;
  box-shadow: 0 22px 40px rgba(0,0,0,.45), 0 3px 8px rgba(0,0,0,.3);
}
.env__pocket { z-index: 3; }

.env__flap {
  position: absolute; z-index: 4; left: 0; top: 0;
  width: 100%; height: 58%; display: block;
}
/* The flap is two flat pieces hinged on the same top edge, with no
   backface-visibility or opacity swaps (both unreliable in Safari):
   the paper side turns 0°→90° and stops edge-on (invisible); the liner side
   starts edge-on and turns 90°→180° straight after. Turning 180° about the
   hinge is what makes the liner end up pointing up above the envelope. */
.env__flap-face {
  position: absolute; inset: 0; width: 100%; height: 100%; display: block;
  transform-origin: 50% 0;
}
.env__flap-front { transform: perspective(1200px) rotateX(0deg); filter: drop-shadow(0 2px 2px rgba(59, 42, 28, .3)); }
.env__flap-back { transform: perspective(1200px) rotateX(90deg); }

/* card tucked inside */
.env__card {
  position: absolute; z-index: 2; left: 6%; right: 6%; top: 7%; height: 86%;
  display: grid; align-content: center; justify-items: center; gap: 2%;
  background: #FBF6EE; border-radius: 4px;
  box-shadow: inset 0 0 0 6px #FBF6EE, inset 0 0 0 7px #D3B377, 0 2px 8px rgba(0,0,0,.18);
  transition: transform 950ms cubic-bezier(.2, .7, .2, 1), opacity 900ms ease;
}
.env__card-kicker { font-family: var(--f-ui); font-size: clamp(.55rem, 1.9vw, .75rem); letter-spacing: .32em; text-transform: uppercase; color: var(--c-olive); }
.env__card-names { font-family: var(--f-script); font-size: clamp(1.5rem, 6.4vw, 2.6rem); line-height: 1.1; color: var(--c-burgundy); }
.env__card-date { font-family: var(--f-serif); font-size: clamp(.95rem, 3.4vw, 1.35rem); letter-spacing: .12em; color: var(--c-ink); }

/* wax seal on the flap tip */
.env__seal {
  position: absolute; z-index: 6; left: 50%; top: 58%;
  width: clamp(64px, 20%, 104px); aspect-ratio: 1;
  transform: translate(-50%, -54%);
  display: block;
  filter: drop-shadow(0 4px 5px rgba(40, 10, 10, .45));
}
.env__seal-half {
  position: absolute; inset: 0; width: 100%; height: 100%; display: block;
  transition: transform 600ms cubic-bezier(.3, .1, .4, 1), opacity 600ms ease;
}
.env__seal-l { clip-path: polygon(0 0, 53% 0, 47% 22%, 55% 41%, 46% 60%, 54% 79%, 49% 100%, 0 100%); }
.env__seal-r { clip-path: polygon(53% 0, 100% 0, 100% 100%, 49% 100%, 54% 79%, 46% 60%, 55% 41%, 47% 22%); }
.env-open:not(:disabled) .env__seal { animation: seal-pulse 2.8s ease-in-out infinite; }

/* ---- Leaves -------------------------------------------------------------- */
.envelope__leaves { position: absolute; inset: 0; z-index: 1; pointer-events: none; }
.leaf {
  position: absolute; top: -60px; width: 34px; height: 34px;
  animation: leaf-fall linear infinite;
  opacity: .85;
}
.leaf:nth-of-type(2) { left: 8%;  fill: #A3461F; width: 30px; animation-duration: 15s; animation-delay: -2s; }
.leaf:nth-of-type(3) { left: 22%; fill: #B8862F; width: 22px; animation-duration: 19s; animation-delay: -9s; }
.leaf:nth-of-type(4) { left: 38%; fill: #6B2626; width: 26px; animation-duration: 17s; animation-delay: -5s; }
.leaf:nth-of-type(5) { left: 55%; fill: #C2622D; width: 34px; animation-duration: 14s; animation-delay: -12s; }
.leaf:nth-of-type(6) { left: 70%; fill: #5C5A2A; width: 24px; animation-duration: 21s; animation-delay: -3s; }
.leaf:nth-of-type(7) { left: 84%; fill: #B8862F; width: 30px; animation-duration: 16s; animation-delay: -7s; }
.leaf:nth-of-type(8) { left: 93%; fill: #A3461F; width: 20px; animation-duration: 20s; animation-delay: -14s; }
.leaf:nth-of-type(9) { left: 46%; fill: #8A3A18; width: 18px; animation-duration: 23s; animation-delay: -18s; }

/* ---- Opening sequence ---------------------------------------------------- */
.envelope.is-seal .envelope__kicker,
.envelope.is-seal .envelope__names,
.envelope.is-seal .envelope__hint { opacity: 0; transform: translateY(-6px); transition: opacity 400ms ease, transform 400ms ease; }
.envelope.is-seal .envelope__hint { animation: none; }

.envelope.is-seal .env__seal-l { transform: translate(-28%, 60%) rotate(-28deg); opacity: 0; }
.envelope.is-seal .env__seal-r { transform: translate(28%, 70%) rotate(24deg); opacity: 0; }

.envelope.is-flap .env__flap-front { animation: flap-up 400ms cubic-bezier(.5, 0, .9, .6) forwards; }
.envelope.is-flap .env__flap-back { animation: flap-over 400ms cubic-bezier(.1, .4, .3, 1) 400ms forwards; }
.envelope.is-flap-behind .env__flap { z-index: 1; }

.envelope.is-card .env__card { transform: translateY(-58%); }

.envelope.is-fall .env__body { transition: transform 900ms cubic-bezier(.55, 0, .85, .35), opacity 900ms ease-in; transform: translateY(115vh) rotate(7deg); }
.envelope.is-fall .env__card { transform: translateY(-46%) scale(1.06); z-index: 5; }
.envelope.is-fall .env__card { box-shadow: inset 0 0 0 6px #FBF6EE, inset 0 0 0 7px #D3B377, 0 24px 40px rgba(0,0,0,.4); }

.envelope.is-reveal { opacity: 0; visibility: hidden; }
.envelope.is-reveal .env__card { transform: translateY(-46%) scale(1.25); opacity: 0; }
.envelope.is-reveal .leaf { animation-play-state: paused; }

/* Site content eases in after the envelope */
html.env-done .intro > * { animation: env-rise 900ms cubic-bezier(.2, .7, .2, 1) both; }
html.env-done .intro > *:nth-child(2) { animation-delay: 120ms; }
html.env-done .intro > *:nth-child(3) { animation-delay: 220ms; }
html.env-done .intro > *:nth-child(4) { animation-delay: 300ms; }
html.env-done .intro > *:nth-child(n+5) { animation-delay: 380ms; }

@keyframes flap-up   { from { transform: perspective(1200px) rotateX(0deg); }  to { transform: perspective(1200px) rotateX(90deg); } }
@keyframes flap-over { from { transform: perspective(1200px) rotateX(90deg); } to { transform: perspective(1200px) rotateX(180deg); } }
@keyframes env-float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
@keyframes seal-pulse { 0%, 100% { transform: translate(-50%, -54%) scale(1); } 50% { transform: translate(-50%, -54%) scale(1.05); } }
@keyframes hint-breathe { 0%, 100% { opacity: .65; } 50% { opacity: 1; } }
@keyframes leaf-fall {
  0%   { transform: translate(0, -10vh) rotate(0deg); }
  25%  { transform: translate(30px, 22vh) rotate(90deg); }
  50%  { transform: translate(-20px, 50vh) rotate(180deg); }
  75%  { transform: translate(25px, 78vh) rotate(270deg); }
  100% { transform: translate(-10px, 112vh) rotate(360deg); }
}
@keyframes env-rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

/* Landscape phones: tighter */
@media (max-height: 520px) and (orientation: landscape) {
  .envelope__stage { --w: min(60vw, calc((100dvh - 150px) * 1.5)); gap: 6px; }
  .envelope__names { font-size: 2.2rem; }
  .envelope__hint { margin-top: 4px; font-size: 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  .leaf, .env-open:not(:disabled) .env, .env-open:not(:disabled) .env__seal, .envelope__hint { animation: none !important; }
  .leaf { display: none; }
  html.env-on .envelope { transition-duration: 300ms; }
  html.env-done .intro > * { animation: none; }
}
