:root {
  /* Composition unit: portrait-first. 1u ≈ 1vw on phones, capped by height on wide screens. */
  --u: min(1vw, 0.5lvh);
  --ink: #f1e4cf;          /* warm ivory for type */
  --ink-dim: #cdb89a;
  --burgundy: #3a0710;
  --night: #05060f;

  /* driven by JS (0..1) */
  --pre-out: 0;
  --title-out: 0;
  --credits-out: 0;
  --iris: 1;
  --scene: 0;

  /* timeline length in "beats": 1 beat = the original title → castle sequence (360lvh of scrolling) */
  --beats: 2.4;
}

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

html, body {
  background: #0b0306;
  color: var(--ink);
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body { min-height: 100%; }

/* + (lvh − svh): when Safari's toolbar re-expands at the very end, the stage must still stay pinned */
#scroller {
  position: relative;
  height: calc(100lvh + 360lvh * var(--beats) + 100lvh - 100svh);
}
/* probe so JS knows the small viewport height (Safari with toolbar) */
#svh { position: absolute; top: 0; width: 1px; height: 100svh; visibility: hidden; pointer-events: none; }

#stage {
  position: sticky;
  top: 0;
  height: 100lvh;
  overflow: hidden;
  isolation: isolate;
  background: #0b0306;
}

#stage > * { position: absolute; inset: 0; }

/* ───────── 1 · Scene ───────── */
#scene {
  z-index: 1;
  transform-origin: 50% 70%;
  transform: scale(calc(1.14 - 0.14 * var(--scene)));
  will-change: transform;
}
/* darkening via an overlay instead of filter: brightness → no per-frame repaint of the whole scene */
#scene::after {
  content: "";
  position: absolute;
  inset: 0;
  background: #000;
  opacity: calc(0.65 * (1 - var(--scene)));
}
#scene > *, .world > * { position: absolute; }
#scene::after, #snow, .shade, .world { pointer-events: none; }   /* only the door takes taps */
/* camera: JS zooms the world towards the castle door (transform set per frame) */
.world { inset: 0; transform-origin: 0 0; will-change: transform; }
/* night closes in around the door */
.shade { inset: 0; background: #020103; opacity: var(--dark, 0); }

.sky {
  inset: 0;
  background:
    radial-gradient(120% 60% at 50% 100%, #3b2340 0%, rgba(59, 35, 64, 0) 60%),
    linear-gradient(180deg, #04050c 0%, #0a0f24 45%, #1b1a36 75%, #2c1f38 100%);
}
#stars, #snow { inset: 0; width: 100%; height: 100%; }

.moon {
  top: 9lvh;
  right: calc(14 * var(--u));
  width: calc(14 * var(--u));
  aspect-ratio: 1;
}
.moon svg {
  width: 100%;
  height: 100%;
  overflow: visible;
  filter: drop-shadow(0 0 calc(2.5 * var(--u)) rgba(255, 240, 210, 0.45))
          drop-shadow(0 0 calc(10 * var(--u)) rgba(150, 160, 220, 0.18));
}

.castle {
  left: 50%;
  bottom: 0;
  width: max(142 * var(--u), 100vw);
  aspect-ratio: 4 / 3;
  transform: translateX(-47%);
}
.castle img {
  display: block;
  width: 100%;
  height: 100%;
  /* soften the hard top edge of the photo crop (tree tops) into the sky */
  -webkit-mask-image: linear-gradient(180deg, transparent 0%, #000 14%);
          mask-image: linear-gradient(180deg, transparent 0%, #000 14%);
}

.windows { position: absolute; inset: 0; }
.castle img.foreground { position: absolute; inset: 0; pointer-events: none; }
.win {
  position: absolute;
  transform: translate(-50%, -50%);
  border-radius: 1px;
  opacity: 0;
  transition: opacity 0.9s ease;
  background:
    /* mullions */
    linear-gradient(90deg, transparent 46%, rgba(40, 18, 10, 0.75) 46% 54%, transparent 54%),
    linear-gradient(180deg, transparent 31%, rgba(40, 18, 10, 0.7) 31% 36%, transparent 36% 64%, rgba(40, 18, 10, 0.7) 64% 69%, transparent 69%),
    /* warm interior light */
    radial-gradient(90% 70% at 50% 60%, #ffe2a6 0%, #ffb257 55%, #d9702a 100%);
  box-shadow: 0 0 calc(1.6 * var(--u)) calc(0.5 * var(--u)) rgba(255, 168, 80, 0.55),
              0 0 calc(5 * var(--u)) calc(1.5 * var(--u)) rgba(255, 140, 60, 0.18);
}
.win.arch { border-radius: 50% 50% 2px 2px / 28% 28% 2px 2px; }
.win.on { opacity: 1; }
.win.debug { opacity: 0.8; outline: 1px solid #0f0; }

.win .couple {
  position: absolute;
  left: 50%;
  bottom: 4%;
  width: 78%;
  height: 74%;
  transform-origin: 50% 100%;
  animation: slowdance 5.5s ease-in-out infinite;
  fill: #2a120c;
}
@keyframes slowdance {
  0%, 100% { transform: translateX(-58%) rotate(-3deg); }
  50%      { transform: translateX(-42%) rotate(3deg); }
}

/* ───────── 2 · Curtain ───────── */
#curtain {
  z-index: 2;
  width: 100%;
  height: 100%;
  pointer-events: none;
  /* camera dollies through the proscenium: drapes + valance slide out of frame */
  transform: scale(calc(1 + 0.5 * var(--curtain-out, 0)));
  will-change: transform;
}
.no-webgl #curtain {
  background:
    repeating-linear-gradient(90deg, #2a0409 0, #5a0c16 3.5%, #2a0409 7%);
}

/* ───────── 3 · Heart spotlight ───────── */
.spot {
  z-index: 3;
  pointer-events: none;
  opacity: 0;
}
.spot-inner {
  position: absolute;
  left: 50%;
  top: 47.5%;
  /* heart is 106u wide; the bitmap has 15-unit padding on each side → 130/100 of that */
  width: calc(106 * 1.3 * var(--u));
  aspect-ratio: 130 / 122;
  transform: translate(-50%, -50%) scale(var(--iris));
  transform-origin: 50% 47%;
  will-change: transform, opacity;
}
.spot-inner img { display: block; width: 100%; height: 100%; }
/* Light: color-dodge multiplies the curtain's own folds → light reads as falling ON the fabric */
.spot-light { mix-blend-mode: color-dodge; }
/* Haze: a faint glow in the air, independent of fabric */
.spot-haze { mix-blend-mode: screen; }

/* ───────── 4 · Titles ───────── */
#titles {
  z-index: 4;
  pointer-events: none;
}
#titles > * {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}
#titles span { display: block; }

.pre {
  top: 15.5%;
  width: 100%;
  text-align: center;
  font-family: "IM Fell English SC", "Times New Roman", serif;
  font-size: calc(5.4 * var(--u));
  letter-spacing: 0.14em;
  color: var(--ink-dim);
  text-shadow: 0 0 calc(2 * var(--u)) rgba(0, 0, 0, 0.6);
}

/* Hand-drawn lettering via SVG mask → fill can be any paint (here: warm foil gradient) */
.title { aspect-ratio: var(--ar); }
.title-move {
  width: 100%;
  height: 100%;
  /* shadow lives on the moving layer itself, so the compositor moves it as one bitmap */
  filter: drop-shadow(0 calc(0.4 * var(--u)) calc(0.8 * var(--u)) rgba(40, 0, 6, 0.55));
  will-change: transform, opacity;
}
.title-ink {
  width: 100%;
  height: 100%;
  background: linear-gradient(170deg, #fff6e6 0%, #f1dfc0 40%, #d9bf96 70%, #f6e8cf 100%);
  -webkit-mask: var(--src) center / contain no-repeat;
          mask: var(--src) center / contain no-repeat;
}
.title {
  --ar: 1210 / 1015;
  --src: url("assets/title.svg");
  top: 46.5%;
  width: calc(76 * var(--u));
  translate: -1.5% -50%;
}
.credits {
  top: 78%;
  width: 100%;
  text-align: center;
  font-family: "IM Fell English SC", "Times New Roman", serif;
  font-size: calc(6.6 * var(--u));
  letter-spacing: 0.06em;
  line-height: 1.15;
  color: var(--ink);
  text-shadow: 0 0 calc(2 * var(--u)) rgba(0, 0, 0, 0.6);
}
.credits small {
  display: block;
  font-size: calc(4.4 * var(--u));
  letter-spacing: 0.24em;
  color: var(--ink-dim);
  margin-bottom: calc(1 * var(--u));
}

/* scroll-driven exit (old-film title card: dissolve + drift up + soften) */
.pre > span, .credits > span { will-change: transform, opacity; }
.pre     > span { opacity: calc(1 - var(--pre-out));     transform: translateY(calc(-6 * var(--u) * var(--pre-out))); }
.title-move     { opacity: calc(1 - var(--title-out));   transform: translateY(calc(-8 * var(--u) * var(--title-out))) scale(calc(1 + 0.04 * var(--title-out))); }
.credits > span { opacity: calc(1 - var(--credits-out)); transform: translateY(calc(-6 * var(--u) * var(--credits-out))); }

/* ───────── 4b · Premiere date + countdown ───────── */
#info {
  z-index: 4;
  inset: 19lvh 0 auto 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: calc(2.6 * var(--u));
  pointer-events: none;
  font-family: "IM Fell English SC", "Times New Roman", serif;
  text-align: center;
  filter: drop-shadow(0 0 calc(1.6 * var(--u)) rgba(4, 5, 14, 0.9));
}
.premiere, .rule { opacity: var(--date-a, 0); transform: translateY(calc(var(--date-y, 0) * var(--u))); will-change: transform, opacity; }
.countdown       { opacity: var(--count-a, 0); transform: translateY(calc(var(--count-y, 0) * var(--u))); will-change: transform, opacity; }

.premiere small {
  display: block;
  font-size: calc(4 * var(--u));
  letter-spacing: 0.34em;
  color: var(--ink-dim);
  margin-bottom: calc(0.6 * var(--u));
}
.foil, .premiere .date, .countdown b span {
  background: linear-gradient(170deg, #fff6e6 0%, #f1dfc0 40%, #d9bf96 70%, #f6e8cf 100%);
  -webkit-background-clip: text;
          background-clip: text;
  color: transparent;
}
.premiere .date {
  display: block;
  font-size: calc(8 * var(--u));
  letter-spacing: 0.03em;
  line-height: 1.1;
}
.rule svg {
  display: block;
  width: calc(64 * var(--u));
  height: auto;
  overflow: visible;
}
.countdown { display: flex; justify-content: center; }
.countdown > div { position: relative; width: calc(18.6 * var(--u)); }
.countdown > div + div::before {             /* hairline between the units */
  content: "";
  position: absolute;
  left: 0;
  top: calc(0.6 * var(--u));
  width: 1px;
  height: calc(10.4 * var(--u));
  background: linear-gradient(180deg, rgba(201, 173, 128, 0), #c9ad80 25%, #c9ad80 75%, rgba(201, 173, 128, 0));
  opacity: 0.8;
}
.countdown b {
  display: block;
  font-weight: normal;
  font-size: calc(9 * var(--u));
  line-height: 1;
}
.countdown b span { display: inline-block; width: 0.56em; }   /* fixed digit cells → no jitter */
.countdown i {
  display: block;
  margin-top: calc(1.2 * var(--u));
  font-style: normal;
  font-size: calc(2.6 * var(--u));
  letter-spacing: 0.12em;
  color: var(--ink-dim);
}

/* ───────── 4c · The door ───────── */
#door {
  /* image box: the arch fills ~70% of the visible height (or ~88% of the width on narrow phones),
     leaving room underneath for the note + key */
  left: 50%;
  top: 42svh;
  bottom: auto;
  right: auto;
  height: min(74.5svh, 143vw);
  aspect-ratio: 785 / 1400;
  transform-origin: 50% 50%;                      /* = centre of the frame */
  transform: translate(-50%, -50%) scale(0.05);
  opacity: 0;
  will-change: transform, opacity;
  pointer-events: none;
  -webkit-tap-highlight-color: transparent;
  outline: none;
}
body.door-ready #door { pointer-events: auto; cursor: pointer; }
.door-body { position: absolute; inset: 0; will-change: transform; container-type: size; }
.door-body img { display: block; width: 100%; height: 100%; }
.door-body > :not(img) { position: absolute; pointer-events: none; }

/* light leaking under the door */
.door-gap {
  left: 12%; right: 12%; top: 97.9%; height: 2.4%;
  background: radial-gradient(50% 50% at 50% 50%, rgba(255, 196, 96, 0.55), rgba(255, 160, 60, 0) 70%);
  mix-blend-mode: screen;
  animation: shimmer 4.3s ease-in-out infinite reverse;
}
/* golden glow around the frame: an arch-shaped box (= the door's outline in door.webp) whose
   outer box-shadow paints only outside the door, onto the dark stone around it */
.door-aura {
  left: 1.66%; right: 1.66%; top: 0.93%; bottom: 0.93%;
  border-radius: 49.7% 49.7% 0 0 / 25.1% 25.1% 0 0;
  box-shadow:
    0 0 0.5cqw 0.1cqw rgba(255, 228, 160, 0.6),
    0 0 3.5cqw 1.2cqw rgba(255, 190, 90, 0.42),
    0 0 12cqw 4cqw rgba(255, 160, 60, 0.2);
  mix-blend-mode: screen;
  animation: shimmer 3.6s ease-in-out infinite;
}
.door-aura.flare { animation: none; opacity: calc((var(--boost, 1) - 1) * 0.55); }
.keyhole {
  left: 47.27%;
  top: 77.54%;
  width: 1.7%;
  transform: translate(-50%, -42%);
}
/* easter egg (20th knock): the leaves part a little, golden light pours through the gap */
.door-crack {
  left: 50.08%; top: 6.6%; bottom: 1.1%; width: 0.6%;
  background: linear-gradient(90deg, rgba(255, 170, 70, 0), #ffe3a0 25%, #fff6dc 50%, #ffe3a0 75%, rgba(255, 170, 70, 0));
  box-shadow: 0 0 2.5cqw 0.8cqw rgba(255, 196, 100, 0.55), 0 0 9cqw 3cqw rgba(255, 160, 60, 0.28);
  -webkit-mask-image: linear-gradient(transparent, #000 12%, #000 94%, transparent);
  mask-image: linear-gradient(transparent, #000 12%, #000 94%, transparent);
  mix-blend-mode: screen;
  opacity: 0;
  transform: translateX(-50%) scaleX(0);
}
body.egg-crack .door-crack { animation: crack 4.6s ease-in-out forwards; }
@keyframes crack {
  0%, 10% { opacity: 0; transform: translateX(-50%) scaleX(0); }
  18% { opacity: 1; transform: translateX(-50%) scaleX(1.4); }
  30% { transform: translateX(-50%) scaleX(2.8); }
  72% { opacity: 1; transform: translateX(-50%) scaleX(3.3); }
  80%, 100% { opacity: 0; transform: translateX(-50%) scaleX(0); }
}
/* easter egg (10th knock): a cat paw pokes out under the door and scratches */
.door-paw { left: 27%; top: 99%; width: 15%; height: 7.4%; overflow: hidden; }
.door-paw svg { display: block; width: 100%; height: 100%; transform: translateY(-105%); }
.door-paw .claws { fill: none; stroke: #efe4d4; stroke-width: 0.9; stroke-linecap: round; opacity: 0; }
body.egg-paw .door-paw svg { animation: paw 3.6s ease-in-out forwards; }
body.egg-paw .door-paw .claws { animation: claws 3.6s step-end forwards; }
@keyframes paw {
  0%, 8% { transform: translateY(-105%); }
  24% { transform: translateY(-4%); }
  30% { transform: translate(-7%, -16%); }
  38% { transform: translate(5%, 0); }
  45% { transform: translate(-7%, -16%); }
  53% { transform: translate(5%, 0); }
  62% { transform: translateY(-4%) rotate(4deg); }
  72% { transform: translateY(-2%) rotate(-3deg); }
  84%, 100% { transform: translateY(-105%); }
}
@keyframes claws { 0% { opacity: 0; } 27% { opacity: 1; } 56% { opacity: 0; } }
/* the paw needs the space under the door: the note steps aside meanwhile */
body.egg-paw #door-note .lines { opacity: 0; transition-duration: 0.4s; }

/* opacity-only flicker (composited, no repaint) */
@keyframes shimmer {
  0%, 100% { opacity: 0.8; }
  18% { opacity: 0.94; }
  31% { opacity: 0.7; }
  47% { opacity: 0.9; }
  63% { opacity: 0.74; }
  80% { opacity: 1; }
}

/* note + empty key under the door */
#door-note {
  z-index: 5;
  inset: auto 0 auto 0;
  top: calc(42svh + 0.4907 * min(74.5svh, 143vw) + 6 * var(--u));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: calc(3 * var(--u));
  font-family: "IM Fell English SC", "Times New Roman", serif;
  color: var(--ink-dim);
  text-align: center;
  pointer-events: none;
}
#door-note .lines {
  display: grid;
  justify-items: center;
  padding: 0 calc(6 * var(--u));
  font-size: calc(3.8 * var(--u));
  letter-spacing: 0.16em;
  line-height: 1.35;
  transition: opacity 0.7s ease;
}
#door-note .lines > span { grid-area: 1 / 1; opacity: 0; transition: opacity 1.2s ease; }
#reply-line { display: block; transition: opacity 0.35s ease; }
#reply-line.swap { opacity: 0; }
#door-note small {
  display: block;
  margin-top: calc(1 * var(--u));
  font-size: calc(3 * var(--u));
  letter-spacing: 0.32em;
  opacity: 0.75;
}
.key-outline {
  width: calc(36 * var(--u));
  height: auto;
  overflow: visible;
  opacity: 0;
  transition: opacity 1.6s ease;
  filter: drop-shadow(0 0 1.5px rgba(0, 0, 0, 0.9));   /* stays readable when the snow drift reaches it */
}
body.door-ready:not(.knocked) #door-note .knock-hint { opacity: 0.85; transition-delay: 1.4s; }
body.door-ready.knocked #door-note .knock-reply { opacity: 1; transition-delay: 0.9s; }
body.door-ready.knocked #door-note .key-outline { opacity: 0.6; transition-delay: 1.8s; }

/* ───────── hint ───────── */
#hint {
  z-index: 4;
  inset: auto 0 calc(100lvh - 100dvh + 1.2svh) 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-family: "IM Fell English SC", serif;
  font-size: calc(3.4 * var(--u));
  letter-spacing: 0.3em;
  color: var(--ink-dim);
  opacity: 0;
  pointer-events: none;
  filter: drop-shadow(0 0 3px rgba(0, 0, 0, 0.85)) drop-shadow(0 0 10px rgba(0, 0, 0, 0.6));
}
#hint svg {
  width: calc(6 * var(--u));
  height: calc(6 * var(--u));
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  animation: nudge 2.2s ease-in-out infinite;
}
@keyframes nudge { 0%, 100% { transform: translateY(0) } 50% { transform: translateY(5px) } }

/* ───────── 5 · Film overlays ───────── */
#grain {
  z-index: 6;
  width: 100%;
  height: 100%;
  opacity: 0.11;
  mix-blend-mode: overlay;
  pointer-events: none;
  image-rendering: pixelated;
}
#vignette {
  z-index: 6;
  pointer-events: none;
  background: radial-gradient(120% 90% at 50% 45%, transparent 55%, rgba(0, 0, 0, 0.55) 100%);
}
#flicker {
  z-index: 7;
  pointer-events: none;
  background: #000;
  opacity: 0;
}
#blackout {
  z-index: 8;
  pointer-events: none;
  background: #000;
  transition: opacity 1.4s ease;
}

/* ───────── intro sequence (projector warms up) ───────── */
.pre, .title, .credits { opacity: 0; }
body.intro #blackout { opacity: 0; }
body.intro .spot { animation: spot-on 1.1s steps(1, end) 0.9s forwards; }
body.intro .pre     { animation: fade-in 1.4s ease 1.9s forwards; }
body.intro .title   { animation: title-in 1.8s ease 2.4s forwards; }
body.intro .credits { animation: fade-in 1.4s ease 3.4s forwards; }
body.intro #hint    { animation: fade-in 1.2s ease 4.4s forwards; }

@keyframes spot-on {
  0% { opacity: 0.55 } 12% { opacity: 0.05 } 22% { opacity: 0.8 } 30% { opacity: 0.2 }
  42% { opacity: 1 } 52% { opacity: 0.75 } 60%, 100% { opacity: 1 }
}
@keyframes fade-in { from { opacity: 0 } to { opacity: 1 } }
@keyframes title-in {
  from { opacity: 0; filter: blur(6px); }
  to   { opacity: 1; filter: none; }   /* ends with no filter on the parent of the moving layer */
}

@media (prefers-reduced-motion: reduce) {
  #hint svg, .win .couple, .door-aura, .door-gap { animation: none; }
  body.intro .spot { animation: fade-in 0.6s ease forwards; }
}

/* scroll-driven fades for elements that also have intro animations */
#hint > * { opacity: calc(1 - var(--hint-out, 0)); }
.spot-inner { opacity: var(--spot-a, 1); }

/* ?p= debug: skip the intro, show final title-card state */
body.still #blackout { opacity: 0; transition: none; }
body.still .pre, body.still .title, body.still .credits, body.still .spot, body.still #hint { opacity: 1; }
