/* ====================================================================
   ENVELOPE INTRO (assets/js/envelope.js, markup in app/pages/envelope.php)
   The invitation arrives sealed. A tap lifts the wax seal, opens the
   flap and draws the card out; the card fills the screen and its arch
   opens onto the hero. Loaded only when features.envelope is on and the
   home page is showing the invitation. Uses the tokens in site.css.
   Everything that moves is transform or opacity.
   ==================================================================== */

/* While the envelope is closed: page locked, hero animations held back so they play on reveal */
.env-hold body, .env-lock body { overflow: hidden; }
/* nothing behind the envelope is drawn while it is closed (the reel would otherwise scroll unseen) */
.env-hold .site-head, .env-hold main, .env-hold footer { visibility: hidden; }
.env-hold .hero, .env-hold .hero *, .env-hold .hero *::before, .env-hold .hero *::after { animation-play-state: paused !important; }

/* ---------- the stage ---------- */
.env-stage { display: none; }
.env-hold .env-stage {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center; align-content: center; gap: clamp(1.4rem, 4vh, 2.4rem);
  padding: max(1rem, env(safe-area-inset-top)) 16px max(1rem, env(safe-area-inset-bottom));
  overflow: hidden; color: var(--ink); text-align: center;
  background:
    radial-gradient(60% 45% at 18% 12%, rgba(121, 131, 106, 0.16), transparent 70%),
    radial-gradient(55% 45% at 86% 88%, rgba(121, 131, 106, 0.14), transparent 70%),
    radial-gradient(90% 70% at 50% 50%, var(--linen), var(--beige));
}
.env-bloom { position: absolute; pointer-events: none; opacity: 0.95; transition: transform 1.2s var(--ease-soft), opacity 0.9s var(--ease-soft); }
.env-bloom-a { top: -3rem; left: -3.5rem; width: clamp(190px, 34vw, 430px); animation: env-bloom-a 1.8s var(--ease) both; }
.env-bloom-b { bottom: -2.5rem; right: -2.5rem; width: clamp(150px, 24vw, 320px); transform: scaleX(-1); animation: env-bloom-b 1.8s var(--ease) 0.15s both; }
@keyframes env-bloom-a { from { opacity: 0; transform: translate(-24px, -24px) scale(0.95); } }
@keyframes env-bloom-b { from { opacity: 0; transform: scaleX(-1) translate(-24px, 24px) scale(0.95); } }
.env-stage.is-opening .env-bloom-a { opacity: 0; transform: translate(-60px, -60px) rotate(-5deg); }
.env-stage.is-opening .env-bloom-b { opacity: 0; transform: scaleX(-1) translate(-60px, 60px) rotate(-5deg); }

.env-kicker { position: relative; font-size: 0.8rem; letter-spacing: 0.32em; text-transform: uppercase; color: var(--ink-soft); animation: hero-fade 1.2s var(--ease) 0.3s both; transition: opacity 0.4s var(--ease); }
.env-names { display: block; margin-top: 0.55rem; font-family: var(--serif); font-size: clamp(1.9rem, 6vw, 2.6rem); letter-spacing: 0; text-transform: none; line-height: 1; color: var(--ink); }
.env-names i { font-size: 0.7em; color: var(--sage); padding: 0 0.12em; }
.env-hint { position: relative; font-family: var(--serif); font-style: italic; font-size: clamp(1.1rem, 3vw, 1.3rem); color: var(--ink-soft); animation: hero-fade 1.2s var(--ease) 0.9s both; transition: opacity 0.4s var(--ease); }
.env-hint .stem { height: 34px; margin: 0 auto 0.9rem; }
.env-hint .env-tap { display: inline-block; animation: env-pulse 2.4s var(--ease-soft) 2s infinite; }
@keyframes env-pulse { 0%, 100% { opacity: 0.55; } 50% { opacity: 1; } }
/* their entrance animation holds opacity, so leaving needs an animation of its own */
.env-stage.is-opening .env-kicker, .env-stage.is-opening .env-hint { animation: env-leave 0.45s var(--ease) both; }
@keyframes env-leave { from { opacity: 1; } to { opacity: 0; } }

/* ---------- the envelope ---------- */
.env-float { animation: env-arrive 1.4s var(--ease) 0.1s both; }
.env-float > div { animation: env-bob 5s var(--ease-soft) 1.6s infinite; }
@keyframes env-arrive { from { opacity: 0; transform: translateY(40px) rotate(-3deg) scale(0.96); } }
@keyframes env-bob { 0%, 100% { transform: translateY(0) rotate(-0.8deg); } 50% { transform: translateY(-8px) rotate(0.8deg); } }
.env-stage.is-opening .env-float > div { animation-play-state: paused; }

.envelope {
  --w: min(84vw, 520px, 88svh);
  position: relative; display: block; width: var(--w); height: calc(var(--w) / 1.42);
  margin: 0; padding: 0; border: 0; background: none; cursor: pointer;
  perspective: 1500px; -webkit-tap-highlight-color: transparent;
  transition: transform 1s var(--ease-soft), opacity 0.8s var(--ease-soft);
}
.envelope:focus-visible { outline: none; }
.envelope:focus-visible .env-seal { outline: 2px solid var(--sage); outline-offset: 2px; border-radius: 50%; }
/* as the card rises the envelope sinks a little, so the pair stays centred on short screens */
.envelope.is-drawn { transform: translateY(24%); }
.envelope.is-away { transform: translateY(85vh) rotate(5deg); opacity: 0; }
.env-stage.is-opening .envelope, .env-stage.is-opening .env-bloom { will-change: transform, opacity; }

.envelope {
  /* the paper: an on-theme warm beige with a fine fibre texture (assets/brand/paper.webp,
     neutral grey, multiplied over each panel's own tint so one file dresses the whole envelope) */
  --paper: url("../brand/paper.webp");
  --grain: 256px;
}
.env-back, .env-side-l, .env-side-r, .env-bottom, .env-flap-out {
  background-image: var(--paper), var(--tint);
  background-size: var(--grain), 100% 100%;
  background-blend-mode: multiply, normal;
}
.env-back {
  --tint: linear-gradient(180deg, #d9cdad, #e0d5b8);
  position: absolute; inset: 0; border-radius: 6px; overflow: hidden;
  box-shadow: 0 1px 0 rgba(31, 35, 25, 0.04), 0 40px 60px -30px rgba(61, 70, 48, 0.55), 0 18px 30px -22px rgba(61, 70, 48, 0.45);
}
/* the liner: a sage botanical print (assets/brand/liner.svg) with a whisper of the paper through it */
.env-back::after, .env-flap-in {
  background-color: var(--sage);
  background-image: var(--paper), url("../brand/liner.svg");
  background-size: var(--grain), calc(var(--w) * 0.36);
  background-blend-mode: multiply, normal;
}
.env-back::after { content: ""; position: absolute; left: 3.5%; right: 3.5%; top: 0; height: 57%; clip-path: polygon(0 0, 100% 0, 50% 100%); }

.env-card {
  position: absolute; left: 5.5%; right: 5.5%; top: 6%; bottom: 5%; z-index: 2;
  display: block; overflow: hidden; border-radius: 3px;
  background: var(--linen); box-shadow: 0 2px 10px rgba(31, 35, 25, 0.16);
  transition: transform 1.05s cubic-bezier(0.3, 0.7, 0.2, 1);
}
.env-card img { position: absolute; inset: 7% 12%; width: 76%; height: 86%; max-width: none; object-fit: contain; }
.envelope.is-drawn .env-card { transform: translateY(-62%); }

/* the pocket casts a soft shadow up into its opening, onto the liner and the card */
.env-front { position: absolute; inset: 0; z-index: 3; border-radius: 6px; overflow: hidden; pointer-events: none; filter: drop-shadow(0 -1px 2px rgba(61, 70, 48, 0.2)); }
.env-front i { position: absolute; inset: 0; }
.env-side-l { --tint: linear-gradient(90deg, #e6dbbd, #eee4c9); clip-path: polygon(0 0, 52% 55%, 0 100%); }
.env-side-r { --tint: linear-gradient(270deg, #e6dbbd, #eee4c9); clip-path: polygon(100% 0, 48% 55%, 100% 100%); }
.env-bottom { --tint: linear-gradient(0deg, #f3ebd3, #ebe1c5); clip-path: polygon(0 100%, 50% 46%, 100% 100%); }
.env-for {
  position: absolute; left: 0; right: 0; bottom: 9%; z-index: 4; pointer-events: none;
  font-family: var(--serif); font-style: italic; font-size: calc(var(--w) * 0.056); color: var(--ink-soft);
  transition: opacity 0.4s var(--ease);
}
.envelope.is-open .env-for { opacity: 0; }

.env-flap {
  position: absolute; left: 0; right: 0; top: 0; height: 57%; z-index: 5;
  transform-origin: 50% 0; transform-style: preserve-3d;
  transition: transform 0.85s cubic-bezier(0.5, 0, 0.3, 1);
}
.env-flap i { position: absolute; inset: 0; -webkit-backface-visibility: hidden; backface-visibility: hidden; }
.env-flap-out { --tint: linear-gradient(180deg, #f0e7cc, #e3d8b9); clip-path: polygon(0 0, 100% 0, 50% 100%); border-radius: 6px 6px 0 0; }
/* the closed flap's shadow on the pocket: a blurred copy of the flap just behind it, gone once it lifts */
.env-flap::before {
  content: ""; position: absolute; inset: 0; transform: translate3d(0, 3px, -1px);
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpath d='M0 0H100L50 100Z' fill='%233d4630' fill-opacity='.3'/%3E%3C/svg%3E") 0 0 / 100% 100% no-repeat;
  filter: blur(2.5px);
  -webkit-backface-visibility: hidden; backface-visibility: hidden; transition: opacity 0.25s var(--ease);
}
.envelope.is-open .env-flap::before { opacity: 0; }
.env-flap-in { transform: rotateX(180deg); clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.envelope.is-open .env-flap { transform: rotateX(180deg); }
.envelope.is-tucked .env-flap { z-index: 1; }

/* the wax seal: the couple's own seal artwork (assets/brand/wax-seal.webp, shadow baked in) */
.env-seal {
  position: absolute; left: 50%; top: 57%; z-index: 6;
  width: calc(var(--w) * 0.25); margin: calc(var(--w) * -0.125) 0 0 calc(var(--w) * -0.125);
  transform-origin: 50% 50%;
  transition: transform 0.45s var(--spring), opacity 0.4s var(--ease), filter 0.25s var(--ease);
}
.env-seal img { width: 100%; height: auto; }
.envelope:hover .env-seal { transform: scale(1.06) rotate(-3deg); }
.envelope.is-unsealed .env-seal { transform: translateY(-14px) scale(1.3) rotate(-14deg); opacity: 0; transition-timing-function: var(--ease), var(--ease), var(--ease); }

/* ---------- the card leaving the envelope and becoming the page ---------- */
.env-sheet, .env-logo, .env-window { position: fixed; left: 0; top: 0; pointer-events: none; }
/* stacking: the linen sheet, then the arch, then the monogram on top so it can dissolve over the arch */
.env-sheet { z-index: 210; inset: 0; background: var(--linen); transform-origin: 0 0; will-change: transform; }
.env-window { z-index: 211; }
.env-logo { z-index: 212; max-width: none; object-fit: contain; transform-origin: 0 0; will-change: transform, opacity; }
.env-sheet, .env-logo, .env-window { opacity: 0; }
.env-sheet.is-ready, .env-logo.is-ready, .env-window.is-ready { opacity: 1; }
.env-sheet.is-flying, .env-logo.is-flying { transition: transform 1s cubic-bezier(0.65, 0, 0.2, 1); }
.env-logo.is-gone { opacity: 0; transition: opacity 1.1s cubic-bezier(0.4, 0, 0.2, 1); }

/* The arch that opens onto the site: a hole in a linen veil (its huge shadow), then scaled up.
   Its size, place and line weights come from the logo's own frame (envelope.js), and the
   radius 50% / 23% is that frame's shape measured from logo-640.png: rounded fully across,
   23% of the height down each side. The double line is a border plus an outline outside it. */
.env-window {
  --frame: rgb(214, 207, 194);
  border-radius: 50% / 23%;
  box-shadow: 0 0 0 150vmax var(--linen);
  transform-origin: 50% 50%; will-change: transform;
}
.env-window::before { content: ""; position: absolute; inset: 0; border-radius: inherit; background: var(--linen); transition: opacity 1.2s cubic-bezier(0.4, 0, 0.2, 1); }
.env-window::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit;
  border: var(--line-in, 1px) solid var(--frame);
  outline: var(--line-out, 1.5px) solid var(--frame); outline-offset: var(--gap, 5px);
  transition: opacity 0.6s var(--ease);
}
.env-window.is-clear::before { opacity: 0; }
.env-window.is-wide { transition: transform 1.6s cubic-bezier(0.45, 0, 0.12, 1); }
.env-window.is-wide::after { opacity: 0; }

/* ---------- landscape phones: the words sit either side of the envelope ---------- */
@media (orientation: landscape) and (max-height: 540px) {
  .env-hold .env-stage { grid-template-columns: 1fr auto 1fr; align-content: center; gap: 0 clamp(1rem, 4vw, 2.6rem); }
  .env-kicker { justify-self: end; text-align: right; font-size: 0.7rem; }
  .env-names { font-size: clamp(1.5rem, 4.2vw, 2rem); }
  .env-hint { justify-self: start; text-align: left; font-size: 1.05rem; }
  .env-hint .stem { display: none; }
  .envelope { --w: min(50vw, 84svh); }
  .env-bloom-a { width: clamp(120px, 20vw, 210px); top: -2rem; left: -2rem; }
  .env-bloom-b { width: clamp(100px, 16vw, 170px); }
}

/* ---------- reduced motion: a quiet crossfade instead of the full sequence ---------- */
@media (prefers-reduced-motion: reduce) {
  .env-bloom, .env-kicker, .env-hint, .env-float, .env-float > div, .env-tap { animation: none !important; }
  .env-hold .env-stage.is-fading { opacity: 0; transition: opacity 0.7s ease !important; }
}
