/* ==========================================================================
   SPACE PORTAL (#bio)
   space.mp4 under a light CRT pass (static, scanlines, tube vignette, rolling
   tracking band, the odd sync slip). js/space-portal.js adds .is-portal and
   drives two scroll-bound values on the section:
     --open   0 → 1  panels part, video settles, duotone rises, title halves
                     split and exit the frame
     --title  0 → 1  title grows while its tracking tightens
   Both default to 1, so the no-JS / reduced-motion render is the open frame.
   (--tv-noise and the tvStatic keyframes live in css/tv-wall.css)
   ========================================================================== */

.space-section {
  --open: 1;
  --title: 1;
  --split: 50%;
  --space-ground: #0a0a0c; /* the stage is a dark screen in either theme */
  --space-amber: #d98f4e;   /* lunar soil, sampled from the footage */
  --space-flare: #3f6fd8;   /* the lens flare */
  position: relative;
  width: 100%;
  background: var(--space-ground);
}

/* Halves travel far enough to clear the frame by the time the panels finish parting */
.space-section.is-portal {
  --split: 55vw;
  height: 250vh;
}

.space-stage {
  position: relative;
  height: 100vh;
  height: 100svh;
  min-height: 480px;
  overflow: hidden;
  isolation: isolate;
  background: var(--space-ground);
}

.is-portal .space-stage {
  position: sticky;
  top: 0;
}

/* ---------- Footage ---------- */

.space-screen {
  position: absolute;
  inset: 0;
  overflow: hidden;
  transform: scale(calc(1.16 - 0.16 * var(--open)));
  will-change: transform;
}

.space-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 62% center;
  filter: contrast(1.08) saturate(1.08) brightness(0.94);
  animation: spaceSyncSlip 9s steps(1, end) infinite;
}

/* Brief horizontal-hold slips, a couple per loop */
@keyframes spaceSyncSlip {
  0%     { transform: none; filter: contrast(1.08) saturate(1.08) brightness(0.94); }
  41%    { transform: translateX(-0.6%) skewX(-0.6deg); filter: contrast(1.2) saturate(1.5) hue-rotate(-12deg) brightness(1.05); }
  41.6%  { transform: translate(0.4%, -0.3%); }
  42.2%  { transform: none; filter: contrast(1.08) saturate(1.08) brightness(0.94); }
  78%    { transform: translateY(0.5%); }
  78.5%  { transform: translateX(0.8%) skewX(0.8deg); filter: contrast(1.25) saturate(0.4) brightness(1.1); }
  79.1%  { transform: none; filter: contrast(1.08) saturate(1.08) brightness(0.94); }
}

.space-static {
  position: absolute;
  inset: -50%;
  pointer-events: none;
  background-image: var(--tv-noise);
  background-size: 180px;
  mix-blend-mode: screen;
  opacity: 0.1;
  animation: tvStatic 0.45s steps(4) infinite;
}

/* Rolling tracking band */
.space-tracking {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 16%;
  pointer-events: none;
  background: linear-gradient(180deg, transparent 0%, rgba(255, 255, 255, 0.05) 45%, rgba(255, 255, 255, 0.14) 52%, rgba(255, 255, 255, 0.04) 58%, transparent 100%);
  mix-blend-mode: screen;
  transform: translateY(-120%);
  animation: spaceTrackingRoll 7s linear infinite;
}

@keyframes spaceTrackingRoll {
  0%   { transform: translateY(-120%); }
  100% { transform: translateY(740%); }
}

/* Duotone wash in the footage's own colors, raised as the portal opens */
.space-duotone {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(160deg, var(--space-flare) 0%, var(--space-amber) 70%);
  mix-blend-mode: overlay;
  opacity: calc(0.22 * var(--open));
}

/* ---------- Portal panels ---------- */

.space-panel {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50.5%;
  background: var(--space-ground);
  will-change: transform;
}

.space-panel-left {
  left: 0;
  border-right: 1px solid rgba(255, 255, 255, 0.08);
  transform: translateX(calc(-104% * var(--open)));
}

.space-panel-right {
  right: 0;
  border-left: 1px solid rgba(255, 255, 255, 0.08);
  transform: translateX(calc(104% * var(--open)));
}

/* ---------- Title ---------- */

.space-title {
  position: absolute;
  top: 50%;
  left: 50%;
  margin: 0;
  font-family: var(--font-hero);
  font-weight: normal;
  font-size: clamp(2.6rem, 9vw, 9.5rem);
  line-height: 1;
  white-space: nowrap;
  color: #f5f5f7;
  letter-spacing: calc(0.06em - 0.08em * var(--title));
  text-shadow: 2px 0 0 rgba(255, 51, 68, 0.55), -2px 0 0 rgba(0, 240, 255, 0.45);
  /* lifts into the empty sky as it opens so it clears the astronaut and lander */
  transform: translate(-50%, calc(-50% - 16vh * var(--title))) scale(calc(1 + 0.2 * var(--title)));
}

.space-title-a,
.space-title-b {
  display: inline-block;
  will-change: transform;
}

/* Split rides --open so the words leave with the panels, not ahead of them */
.space-title-a {
  transform: translateX(calc(-1 * var(--split) * var(--open)));
}

.space-title-b {
  transform: translateX(calc(var(--split) * var(--open)));
}

/* ---------- Glass: scanlines, RGB fringe, vignette, faint flicker ---------- */

.space-crt {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    repeating-linear-gradient(0deg, rgba(0, 0, 0, 0.24) 0 1px, transparent 1px 3px),
    radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, 0.7) 100%);
  box-shadow:
    inset 3px 0 0 rgba(255, 51, 68, 0.18),
    inset -3px 0 0 rgba(0, 240, 255, 0.16),
    inset 0 0 120px rgba(0, 0, 0, 0.55);
  animation: spaceFlicker 3.2s steps(1, end) infinite;
}

@keyframes spaceFlicker {
  0%   { opacity: 1; }
  32%  { opacity: 0.9; }
  33%  { opacity: 1; }
  71%  { opacity: 0.94; }
  72%  { opacity: 1; }
}

@media (max-width: 600px) {
  .space-section {
    --split: 22%;
  }

  .space-video {
    object-position: 72% center; /* centre the lander in a portrait crop */
  }
}

@media (prefers-reduced-motion: reduce) {
  .space-video,
  .space-static,
  .space-tracking,
  .space-crt {
    animation: none;
  }
}
