/* The VHS post-processing layer.
   =============================================================================
   THE ONE RULE THAT MATTERS (PLAN.md 2.2):

   `.vhs` is a SIBLING of `.deck`, never an ancestor, and nothing in here uses
   `backdrop-filter`. An ancestor `filter` / `mix-blend-mode` / `backdrop-filter`
   pulls the reel's `translateX` off the compositor and onto the main thread, and the
   5400 ms roll — the one thing this app exists to do — starts dropping frames.

   That is also why the "bloom" is a static screen-blended radial gradient plus a
   text-shadow, not a real blur of the scene behind it: a full-screen backdrop blur
   costs a readback every single frame of the roll.
   ============================================================================= */

.vhs {
  position: fixed;
  inset: 0;
  z-index: 900;
  pointer-events: none;
  overflow: hidden;
  contain: strict;
}

/* 1 ─ phosphor haze. Static, screen-blended, no readback.
   Kept tight on purpose: a broad screen wash lifts a near-black shell to brown, which
   made `nightshift` render as a slightly dim `oxide`. It is a glow around the picture,
   not a light over the whole deck. */
.vhs-bloom {
  position: absolute;
  inset: 0;
  mix-blend-mode: screen;
  opacity: var(--bloom-op);
  background:
    radial-gradient(62% 44% at 50% 30%, rgba(255, 236, 200, 0.42), transparent 70%),
    radial-gradient(50% 26% at 50% 100%, rgba(255, 190, 120, 0.22), transparent 72%);
}

/* 2 ─ grain. Position-stepped at ~12 fps on purpose: it should look cheap, not smooth. */
.vhs-grain {
  position: absolute;
  inset: -64px;
  opacity: var(--grain-op);
  mix-blend-mode: overlay;
  background-image: url("../assets/noise.png");
  background-repeat: repeat;
  will-change: transform;
}

[data-motion="on"] .vhs-grain {
  animation: grain-shift 0.66s steps(1, end) infinite;
}

@keyframes grain-shift {
  0% { transform: translate3d(0, 0, 0); }
  12.5% { transform: translate3d(-13px, 9px, 0); }
  25% { transform: translate3d(7px, -17px, 0); }
  37.5% { transform: translate3d(-21px, -6px, 0); }
  50% { transform: translate3d(15px, 12px, 0); }
  62.5% { transform: translate3d(-5px, 21px, 0); }
  75% { transform: translate3d(19px, -11px, 0); }
  87.5% { transform: translate3d(-9px, -19px, 0); }
}

/* 3 ─ scanlines. Static texture; survives motion-off. */
.vhs-scan {
  position: absolute;
  inset: 0;
  opacity: var(--scan-op);
  mix-blend-mode: multiply;
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.55) 0 1px,
    rgba(0, 0, 0, 0) 1px 3px
  );
}

/* 4 ─ tube vignette. */
.vhs-vign {
  position: absolute;
  inset: 0;
  mix-blend-mode: multiply;
  background: radial-gradient(130% 100% at 50% 50%, transparent 52%, rgba(0, 0, 0, 0.72) 100%);
}

/* 5 ─ the tracking band. This IS the page transition; nothing else animates during it. */
.vhs-band {
  position: absolute;
  left: -2%;
  width: 104%;
  height: 74px;
  top: 0;
  opacity: 0;
  transform: translate3d(0, -110px, 0);
  mix-blend-mode: screen;
  background-image:
    linear-gradient(
      to bottom,
      transparent,
      rgba(255, 255, 255, 0.05) 16%,
      rgba(255, 255, 255, 0.17) 50%,
      rgba(255, 255, 255, 0.04) 84%,
      transparent
    ),
    url("../assets/noise.png");
  background-blend-mode: overlay;
  will-change: transform, opacity;
}

.vhs-band.run {
  animation: band-sweep var(--t-band) linear 1;
}

@keyframes band-sweep {
  0% { opacity: 0; transform: translate3d(0, -110px, 0); }
  10% { opacity: 1; }
  90% { opacity: 1; }
  100% { opacity: 0; transform: translate3d(0, 102vh, 0); }
}

/* the shell shears with the band */
.deck.tracking {
  animation: track-jitter var(--t-band) steps(1, end) 1;
}

@keyframes track-jitter {
  0%, 100% { transform: translate3d(0, 0, 0); }
  14% { transform: translate3d(-5px, 0, 0); }
  28% { transform: translate3d(4px, 0, 0); }
  44% { transform: translate3d(-2px, 0, 0); }
  62% { transform: translate3d(5px, 0, 0); }
  82% { transform: translate3d(-1px, 0, 0); }
}

/* 6 ─ head-switching noise, permanently pinned to the bottom edge. */
.vhs-head {
  position: absolute;
  left: -20%;
  width: 140%;
  bottom: 0;
  height: 7px;
  opacity: 0.55;
  mix-blend-mode: screen;
  background-image: url("../assets/noise.png");
  background-size: 220px 7px;
  will-change: transform;
}

[data-motion="on"] .vhs-head {
  animation: head-jump 0.42s steps(1, end) infinite;
}

@keyframes head-jump {
  0% { transform: translate3d(0, 0, 0); }
  33% { transform: translate3d(-37px, 0, 0); }
  66% { transform: translate3d(24px, 0, 0); }
}

/* ── chromatic fringe ─────────────────────────────────────────────────────
   Applied per-element, not as a layer — a whole-screen colour split would smear the
   reel's text at exactly the moment it needs to be readable. Tightens to nothing on
   the focused control so keyboard users get a crisp target. */
.fringe {
  text-shadow: -1px 0 var(--fringe-r), 1px 0 var(--fringe-c);
}

.fringe:focus-visible,
.fringe:hover {
  text-shadow: none;
}

/* ── motion off ───────────────────────────────────────────────────────────
   Kills the band, the head strip and the grain stepping. Keeps the scanlines, the
   vignette and the fringe: those are surface, not movement. */
[data-motion="off"] .vhs-grain,
[data-motion="off"] .vhs-head,
[data-motion="off"] .deck.tracking {
  animation: none !important;
}

[data-motion="off"] .vhs-band {
  display: none;
}
