/* ============================================================
   THEME — tweak colors / sizes here
   ============================================================ */
:root {
  --bg-top: #fdf6ec;
  --bg-bottom: #cfe3ff;
  --spotlight: rgba(255, 255, 255, 0.85);

  --env-body: #f6c453;          /* envelope front */
  --env-body-dark: #eab234;     /* envelope shading */
  --env-flap: #f7cf6b;          /* the flap */
  --env-back: #e9b53f;
  --env-edge: rgba(120, 80, 0, 0.18);

  --seal: #c0392b;
  --seal-dark: #962d22;

  --card-bg: #ffffff;
  --card-cover: #ff6b6b;
  --card-cover-2: #ff8e72;
  --ink: #3a2c20;
  --ink-soft: #6d5b4b;

  --accent-1: #ff6b6b;
  --accent-2: #ffd93d;
  --accent-3: #6bcb77;
  --accent-4: #4d96ff;
  --accent-5: #c77dff;

  /* geometry */
  --env-w: min(92vw, 60vh, 600px);
  --env-h: calc(var(--env-w) * 0.64);
  --card-w: calc(var(--env-w) * 0.9);
  --card-h: calc(var(--env-h) * 0.96);

  --ease-pop: cubic-bezier(0.34, 1.56, 0.64, 1);
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  font-family: "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

/* ============================================================
   STAGE
   ============================================================ */
.stage {
  position: relative;
  min-height: 100svh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background:
    radial-gradient(120% 90% at 50% 38%, var(--spotlight) 0%, transparent 55%),
    linear-gradient(165deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

/* faint confetti sprinkle baked into the backdrop */
.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.5;
  background-image:
    radial-gradient(circle, var(--accent-1) 2px, transparent 3px),
    radial-gradient(circle, var(--accent-4) 2px, transparent 3px),
    radial-gradient(circle, var(--accent-3) 2px, transparent 3px);
  background-size: 140px 140px, 190px 190px, 240px 240px;
  background-position: 10px 30px, 80px 120px, 150px 60px;
}

/* ============================================================
   SCENE + ENVELOPE
   ============================================================ */
.scene {
  position: relative;
  /* sealed: sit a touch below center; on open we slide further down (below) */
  transform: translateY(20%);
  transition: transform 0.7s var(--ease-pop);
}

/* as the card pops up, slide the whole scene down so the CARD ends up centered */
.stage[data-state="cardOut"] .scene,
.stage[data-state="cardOpen"] .scene,
.stage[data-state="cardGift"] .scene {
  transform: translateY(58%);
}

.envelope {
  position: relative;
  width: var(--env-w);
  height: var(--env-h);
  filter: drop-shadow(0 22px 28px rgba(60, 40, 0, 0.28));
}

.stage[data-state="sealed"] .envelope {
  animation: bob 3.4s ease-in-out infinite;
}

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-8px); }
}

.env-back {
  position: absolute;
  inset: 0;
  border-radius: 12px;
  background: var(--env-back);
  z-index: 1;
  pointer-events: none;
}

/* solid front face — fully hides the card until it slides out the top */
.env-front {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  border-radius: 12px;
  overflow: hidden;
  background: linear-gradient(160deg, var(--env-body) 0%, var(--env-body-dark) 100%);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.25);
}

/* the four flap seams meeting in the center (corner-to-corner X) */
.env-front::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(to bottom right, transparent calc(50% - 1px), var(--env-edge) 50%, transparent calc(50% + 1px)),
    linear-gradient(to bottom left, transparent calc(50% - 1px), var(--env-edge) 50%, transparent calc(50% + 1px));
}

/* the bottom flap, a touch darker for depth */
.env-front::after {
  content: "";
  position: absolute;
  inset: 0;
  clip-path: polygon(0% 100%, 100% 100%, 50% 50%);
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.03), rgba(0, 0, 0, 0.12));
}

/* top flap, hinged at the top edge, opens up & back */
.env-flap {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 58%;
  z-index: 4;
  transform-origin: top center;
  transform: perspective(900px) rotateX(0deg);
  transition: transform 0.6s var(--ease-pop), z-index 0s linear 0.3s;
  clip-path: polygon(0% 0%, 100% 0%, 50% 100%);
  background: linear-gradient(160deg, var(--env-flap) 0%, var(--env-body-dark) 120%);
  border-bottom: 1px solid var(--env-edge);
  cursor: grab;
  touch-action: none;
}
.env-flap:active { cursor: grabbing; }

.stage[data-state="flapOpen"] .env-flap,
.stage[data-state="cardOut"] .env-flap,
.stage[data-state="cardOpen"] .env-flap {
  transform: perspective(900px) rotateX(178deg);
  z-index: 0;
}

/* wax seal — a full circle on the flap-to-body seam, holding it shut.
   Built from two halves so it tears apart when the flap lifts. */
.seal {
  position: absolute;
  left: 50%;
  top: 58%;
  width: 15.5%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.35);
  display: grid;
  place-items: center;
  font-size: calc(var(--env-w) * 0.078);
  z-index: 6;
  cursor: grab;
  touch-action: none;
}
.seal:active { cursor: grabbing; }

.seal-half {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--seal) 0%, var(--seal-dark) 80%);
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.25);
  transition: transform 0.6s var(--ease-pop), opacity 0.4s ease 0.42s;
}
.seal-top { clip-path: inset(0 0 50% 0); }
.seal-bottom { clip-path: inset(50% 0 0 0); }
.seal-mono {
  position: relative;
  z-index: 2;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.stage[data-state="sealed"] .seal { animation: sealPulse 2.2s ease-in-out infinite; }

@keyframes sealPulse {
  0%, 100% { box-shadow: 0 3px 6px rgba(0,0,0,0.35), 0 0 0 0 rgba(192,57,43,0.5); }
  50%      { box-shadow: 0 3px 6px rgba(0,0,0,0.35), 0 0 0 8px rgba(192,57,43,0); }
}

/* once it's no longer sealed the seal stays torn: top half lifted, bottom dropped */
.stage:not([data-state="sealed"]) .seal { pointer-events: none; box-shadow: none; animation: none; }
.stage:not([data-state="sealed"]) .seal-top    { transform: translateY(-58%) rotate(-18deg); opacity: 0; }
.stage:not([data-state="sealed"]) .seal-bottom { transform: translateY(34%) rotate(15deg); opacity: 0; }
.stage:not([data-state="sealed"]) .seal-mono   { transform: scale(0.5); opacity: 0; }

/* ============================================================
   THE CARD
   ============================================================ */
.card {
  position: absolute;
  left: 50%;
  bottom: 4%;
  width: var(--card-w);
  height: var(--card-h);
  margin-left: calc(var(--card-w) / -2);
  z-index: 2;
  perspective: 1100px;
  transform: translateY(0);
  transition: transform 0.65s var(--ease-pop), z-index 0s;
  cursor: grab;
  touch-action: none;
}
.card:active { cursor: grabbing; }

.stage[data-state="flapOpen"] .card { transform: translateY(-20%); }

.stage[data-state="cardOut"] .card,
.stage[data-state="cardOpen"] .card,
.stage[data-state="cardGift"] .card {
  transform: translateY(-74%) scale(1.22);
  z-index: 30;
}

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transform: rotateY(0deg);
  transition: transform 0.75s var(--ease-pop);
}

.stage[data-state="cardOpen"] .card-inner { transform: rotateY(180deg); }
.stage[data-state="cardGift"] .card-inner { transform: rotateY(360deg); }

.card-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 14px 24px rgba(40, 20, 0, 0.32);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.cover-content {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  background:
    radial-gradient(circle at 18% 20%, rgba(255,255,255,0.35) 0 6px, transparent 7px),
    radial-gradient(circle at 82% 28%, rgba(255,255,255,0.3) 0 5px, transparent 6px),
    radial-gradient(circle at 30% 82%, rgba(255,255,255,0.3) 0 5px, transparent 6px),
    radial-gradient(circle at 74% 78%, rgba(255,255,255,0.3) 0 6px, transparent 7px),
    linear-gradient(155deg, var(--card-cover) 0%, var(--card-cover-2) 100%);
  color: #fff;
  gap: 0.15em;
  padding: 1em;
  transition: opacity 0.25s;
}

.cover-emoji { font-size: clamp(2rem, 9vw, 3rem); line-height: 1; }
.cover-title {
  font-family: "Baloo 2", system-ui, sans-serif;
  font-weight: 600;
  font-size: clamp(1.1rem, 5.2vw, 1.6rem);
  margin: 0.2em 0 0;
  text-shadow: 0 2px 0 rgba(0,0,0,0.12);
}
.cover-name {
  font-family: "Baloo 2", system-ui, sans-serif;
  font-weight: 800;
  font-size: clamp(2rem, 12vw, 3.4rem);
  margin: 0;
  line-height: 1;
  text-shadow: 0 3px 0 rgba(0,0,0,0.15);
}
.cover-sub {
  margin-top: 0.9em;
  font-size: 0.8rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.85;
  animation: nudge 1.8s ease-in-out infinite;
}
@keyframes nudge { 0%,100%{ opacity: 0.5; } 50%{ opacity: 0.95; } }

.card-message {
  transform: rotateY(180deg);
  background: linear-gradient(180deg, #fffdf8 0%, #fff5e6 100%);
  color: var(--ink);
  padding: 0.8em 0.9em;
}

.message-scroll {
  max-height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2em;
  padding: 0.1em 0.3em;
}
.message-emoji { font-size: 1.3rem; }
.message-text {
  white-space: pre-line;
  font-size: clamp(0.85rem, 3.6vw, 1.02rem);
  line-height: 1.4;
  margin: 0;
}
.message-text del {
  text-decoration-color: var(--accent-1);
  text-decoration-thickness: 2px;
  color: var(--ink-soft);
}
.message-sign {
  align-self: flex-end;
  text-align: right;
  white-space: pre-line;
  font-family: "Baloo 2", system-ui, sans-serif;
  font-weight: 600;
  margin: 0.3em 0.2em 0;
  color: var(--ink-soft);
}

/* ============================================================
   HINT + BUTTONS + CONFETTI
   ============================================================ */
.hint {
  position: absolute;
  bottom: max(4vh, 18px);
  left: 0;
  right: 0;
  text-align: center;
  font-weight: 700;
  font-size: 1rem;
  color: var(--ink-soft);
  margin: 0;
  padding: 0 1rem;
  pointer-events: none;
  transition: opacity 0.4s;
}

.icon-btn {
  position: fixed;
  top: 14px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0 3px 8px rgba(0,0,0,0.15);
  font-size: 20px;
  cursor: pointer;
  z-index: 100;
  display: grid;
  place-items: center;
  backdrop-filter: blur(4px);
  transition: transform 0.15s, background 0.2s;
}
.icon-btn:hover { transform: scale(1.08); background: rgba(255,255,255,0.95); }
.icon-btn:active { transform: scale(0.94); }

.mute { right: 14px; }

.replay {
  right: 14px;
  top: 66px;
  font-size: 24px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s;
}
.stage[data-state="cardOpen"] .replay,
.stage[data-state="cardGift"] .replay { opacity: 1; pointer-events: auto; }

.confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 200;
}

/* focus visibility for keyboard users */
.card:focus-visible,
.env-flap:focus-visible,
.icon-btn:focus-visible {
  outline: 3px solid var(--accent-4);
  outline-offset: 3px;
}

/* while dragging, kill transitions so motion tracks the finger 1:1 */
.stage.dragging .env-flap,
.stage.dragging .card,
.stage.dragging .card-inner,
.stage.dragging .seal-half,
.stage.dragging .seal-mono {
  transition: none !important;
}
.stage.dragging .seal { animation: none; }

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .stage[data-state="sealed"] .envelope,
  .stage[data-state="sealed"] .seal,
  .cover-sub { animation: none; }
  .env-flap, .card, .card-inner { transition-duration: 0.25s; }
}

/* ============================================================
   GIFT PAGE (turn past the message to reveal it)
   ============================================================ */
.gift-content {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #fffdf8 0%, #fff5e6 100%);
  color: var(--ink);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s;
}
.stage[data-state="cardGift"] .cover-content { opacity: 0; visibility: hidden; }
.stage[data-state="cardGift"] .gift-content  { opacity: 1; visibility: visible; }

.gift-scroll {
  max-height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.15em;
  padding: 0.4em 0.8em;
}
.gift-label {
  font-family: "Baloo 2", system-ui, sans-serif;
  font-weight: 600;
  font-size: 0.82rem;
  letter-spacing: 0.03em;
  color: var(--ink-soft);
}
.gift-img {
  width: auto;
  max-width: calc(var(--env-w) * 0.6);
  max-height: calc(var(--env-w) * 0.235);
  border-radius: 9px;
  box-shadow: 0 5px 12px rgba(40, 20, 0, 0.22);
}
.gift-name {
  font-family: "Baloo 2", system-ui, sans-serif;
  font-weight: 700;
  font-size: clamp(0.9rem, 3.6vw, 1.05rem);
  line-height: 1.15;
  margin: 0.05em 0 0;
  color: var(--accent-1);
}
.gift-blurb {
  font-size: clamp(0.68rem, 2.8vw, 0.78rem);
  line-height: 1.3;
  margin: 0;
}
.gift-late {
  font-size: clamp(0.7rem, 2.9vw, 0.8rem);
  line-height: 1.35;
  margin: 0.15em 0 0;
  color: var(--ink-soft);
  font-style: italic;
}
.gift-link {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent-4);
  text-decoration: none;
  margin-top: 0.1em;
}
.gift-link:hover { text-decoration: underline; }
