/* --------------------------------------------------------------------------
   Zoha & Ibrahim — Save the Date
   One shared .invitation-scene scales the full 780 × 2000 artboard.
   Layers stay locked in relative alignment across all screen sizes.
   GSAP owns transform / opacity / visibility — no CSS transitions on layers.
   -------------------------------------------------------------------------- */

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #d8c9c8;
  --hint: #5c4540;
  /* Shared Illustrator artboard */
  --artboard-w: 780;
  --artboard-h: 2000;
  --artboard-ratio: 780 / 2000;
  /* Empty artboard below the leather pocket — clip only this strip */
  --below-pocket: 28%;
  /* Room under the scene for the hint + gap */
  --hint-space: 3.25rem;
  --page-pad-y: max(20px, env(safe-area-inset-top));
  --page-pad-x: max(16px, env(safe-area-inset-right));
  /*
    Max scene height so opened paper (within the artboard) and hint
    both stay on-screen.
  */
  --scene-max-height: calc(
    100svh - var(--page-pad-y) - max(20px, env(safe-area-inset-bottom)) - var(--hint-space)
  );
}

html,
body {
  height: 100%;
}

body {
  min-height: 100svh;
  background-color: var(--bg);
  background-image:
    radial-gradient(
      ellipse 70% 55% at 50% 48%,
      #e4d6d4 0%,
      var(--bg) 55%,
      #cbb8b6 100%
    );
  font-family: "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  color: var(--hint);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ---- Page shell -------------------------------------------------------- */

.page {
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* Slightly above true vertical center */
  justify-content: center;
  padding:
    max(12px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(28px, calc(env(safe-area-inset-bottom) + 4vh))
    max(16px, env(safe-area-inset-left));
  box-sizing: border-box;
  overflow: hidden;
  gap: 0.85rem;
}

/* ---- Hit target (does not size the artwork) ---------------------------- */

.envelope {
  appearance: none;
  border: none;
  background: transparent;
  padding: 0;
  margin: 0;
  cursor: pointer;
  display: block;
  flex-shrink: 0;
  /* Perspective for the flap’s 3D lift — sized by .invitation-scene */
  perspective: 1200px;
  transform-style: preserve-3d;
  outline: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.envelope:focus-visible {
  outline: 2px solid #5c4540;
  outline-offset: 6px;
}

.envelope.is-open {
  cursor: pointer;
}

/* ---- Shared scene (scales as one unit) --------------------------------- */

.invitation-scene {
  position: relative;
  /* Prefer width caps; fall back to height so short viewports still fit */
  width: min(
    82vw,
    430px,
    calc(var(--scene-max-height) * var(--artboard-w) / var(--artboard-h))
  );
  aspect-ratio: var(--artboard-ratio);
  flex-shrink: 0;
  transform-style: preserve-3d;
  touch-action: manipulation;
}

/* All PNG layers share the same artboard box */
.invitation-scene img,
.invitation-scene .layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  display: block;
  /* GPU / Safari stability — GSAP still owns the animated values */
  will-change: transform, opacity;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transform-style: preserve-3d;
  /* No CSS transitions on GSAP-driven properties */
  transition: none;
}

/*
  z-index (back → front):
  eback (1) → openflap (2) → paper-inside (3) → pocket (4) →
  closeflap (5) → paper-front (6)
*/
.layer--eback {
  z-index: 1;
}

/*
  Closed-state FOUC defaults only. GSAP gsap.set() takes over on init.
  Origin language is “50% 100%” (flap bottom); on these full artboard
  PNGs that hinge falls at ~47.3% of the canvas height.
*/
.layer--openflap {
  z-index: 2;
  opacity: 0;
  transform-origin: 50% 47.3%;
  transform: rotateX(92deg);
}

/* ---- Paper inside (behind pocket) -------------------------------------- */

.paper-well {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 3;
  overflow: hidden;
  pointer-events: none;
  transform-style: preserve-3d;
  /*
    Clip ONLY the strip under the leather while the card is inside.
    Top stays open so the slide-out is never cropped.
    Same inset box as .invitation-scene — shared coordinate system.
  */
  clip-path: inset(0 0 var(--below-pocket) 0);
}

.paper-inside {
  z-index: 3;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform-origin: 50% 50%;
}

.layer--pocket {
  z-index: 4;
}

.layer--closeflap {
  z-index: 5;
}

/* Identical artboard layer — sits above the whole envelope once swapped */
.paper-front {
  z-index: 6;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform-origin: 50% 50%;
}

/* ---- Hint text --------------------------------------------------------- */

.hint {
  flex-shrink: 0;
  font-size: clamp(0.75rem, 2.4vw, 0.95rem);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.7;
  transition: opacity 0.45s ease;
  text-align: center;
  line-height: 1.3;
  min-height: 1.3em;
}

.hint.is-hidden {
  opacity: 0;
  pointer-events: none;
}

/* ---- Mobile (< 600px) -------------------------------------------------- */

@media (max-width: 599px) {
  :root {
    --hint-space: 3rem;
  }

  .invitation-scene {
    width: min(88vw, 520px);
  }

  .hint {
    font-size: 0.72rem;
  }
}

/* ---- Short viewports — keep opened card + hint on screen --------------- */

@media (max-height: 740px) {
  :root {
    --hint-space: 2.75rem;
  }

  .invitation-scene {
    width: min(
      68vw,
      calc(var(--scene-max-height) * var(--artboard-w) / var(--artboard-h))
    );
  }

  .page {
    gap: 0.65rem;
  }
}

@media (max-height: 667px) {
  .invitation-scene {
    width: min(
      62vw,
      calc(var(--scene-max-height) * var(--artboard-w) / var(--artboard-h))
    );
  }
}

@media (prefers-reduced-motion: reduce) {
  .layer,
  .hint {
    transition: none !important;
  }
}

/*
  Final mobile viewport lock (cascade winner).
  Fits exactly in the visible browser chrome — no vertical scroll.
  Envelope + hint stay grouped in flex; scene width unchanged.
*/
@media (max-width: 599px) {
  html,
  body {
    height: 100%;
    min-height: 100%;
    overflow: hidden;
  }

  body {
    min-height: 100dvh;
  }

  .page {
    height: 100dvh;
    min-height: 100dvh;
    overflow: hidden;
    justify-content: center;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    padding-left: max(16px, env(safe-area-inset-left));
  }

  .invitation-scene {
    width: min(88vw, 520px);
  }
}

@media screen and (max-width: 599px) {
  .envelope {
    margin-top: -18vh;
    margin-bottom: -42vh;
  }
}
