/**
 * Vintage film stock overlay — grain, scratches, dust, flicker, gate weave.
 * CSS-only; respects reduced motion and perf-lite / fx-disabled.
 */

.film-stock {
  position: fixed;
  inset: 0;
  /* Above ambient page layers; below open folios (280) and modals (300+) */
  z-index: 50;
  pointer-events: none;
  overflow: hidden;
}

/* ── Base grain (animated crawl) ── */
.film-grain {
  position: absolute;
  inset: -25%;
  opacity: 0.14;
  mix-blend-mode: soft-light;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 512 512' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='g'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.82' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23g)' opacity='0.6'/%3E%3C/svg%3E");
  background-size: 240px 240px;
  background-repeat: repeat;
  animation: filmGrainCrawl 0.55s steps(6) infinite;
}

@keyframes filmGrainCrawl {
  0% { transform: translate(0, 0); }
  100% { transform: translate(-48px, 36px); }
}

.film-grain-vignette {
  position: absolute;
  inset: 0;
  z-index: 1;
  background:
    radial-gradient(ellipse at center, transparent 32%, rgba(0, 0, 0, 0.5) 100%),
    linear-gradient(180deg, rgba(0, 0, 0, 0.35) 0%, transparent 12%, transparent 88%, rgba(0, 0, 0, 0.4) 100%);
  opacity: 0.28;
}

/* ── Scratch fields (diagonal + vertical hairlines) ── */
.film-scratches {
  position: absolute;
  inset: -15%;
  z-index: 2;
  opacity: 0.14;
  mix-blend-mode: screen;
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='320' height='320'%3E%3Cline x1='0' y1='42' x2='320' y2='44' stroke='%23fff' stroke-opacity='0.09' stroke-width='1'/%3E%3Cline x1='0' y1='128' x2='320' y2='125' stroke='%23fff' stroke-opacity='0.06' stroke-width='0.5'/%3E%3Cline x1='0' y1='210' x2='320' y2='214' stroke='%23fff' stroke-opacity='0.11' stroke-width='1'/%3E%3Cline x1='0' y1='280' x2='320' y2='277' stroke='%23fff' stroke-opacity='0.05' stroke-width='0.5'/%3E%3Cline x1='88' y1='0' x2='90' y2='320' stroke='%23fff' stroke-opacity='0.07' stroke-width='0.5'/%3E%3Cline x1='200' y1='0' x2='198' y2='320' stroke='%23fff' stroke-opacity='0.04' stroke-width='0.5'/%3E%3C/svg%3E");
  background-size: 320px 320px;
  animation: filmScratchDrift 7s linear infinite;
}

.film-scratches-b {
  opacity: 0.18;
  mix-blend-mode: overlay;
  background-image:
    repeating-linear-gradient(
      97deg,
      transparent 0,
      transparent 118px,
      rgba(255, 252, 240, 0.04) 118px,
      rgba(255, 252, 240, 0.09) 119px,
      transparent 120px,
      transparent 240px
    );
  background-size: 360px 200%;
  animation: filmScratchDriftB 11s linear infinite reverse;
}

@keyframes filmScratchDrift {
  0% { transform: translate(0, -8%) rotate(0deg); }
  100% { transform: translate(-6%, 8%) rotate(0.3deg); }
}

@keyframes filmScratchDriftB {
  0% { background-position: 0 0; }
  100% { background-position: 0 200%; }
}

/* Occasional vertical hairline scratch */
.film-scratches::after {
  content: '';
  position: absolute;
  top: -30%;
  left: 18%;
  width: 2px;
  height: 160%;
  background: linear-gradient(
    180deg,
    transparent 0%,
    rgba(255, 248, 230, 0.35) 18%,
    rgba(255, 248, 230, 0.15) 45%,
    transparent 70%
  );
  filter: blur(0.3px);
  opacity: 0;
  animation: filmHairline 14s ease-in-out infinite;
}

.film-scratches-b::after {
  content: '';
  position: absolute;
  top: -20%;
  left: 72%;
  width: 1px;
  height: 140%;
  background: linear-gradient(180deg, transparent, rgba(255, 255, 255, 0.2) 30%, transparent 65%);
  opacity: 0;
  animation: filmHairline 19s ease-in-out infinite 4s;
}

@keyframes filmHairline {
  0%, 86%, 100% { opacity: 0; transform: translateX(0) scaleY(1); }
  88% { opacity: 0.85; transform: translateX(0) scaleY(1); }
  91% { opacity: 0.5; transform: translateX(12vw) scaleY(1.02); }
  93% { opacity: 0.15; transform: translateX(18vw) scaleY(1); }
  95% { opacity: 0; }
}

/* ── Dust & specks ── */
.film-dust {
  position: absolute;
  inset: -10%;
  z-index: 3;
  opacity: 0.28;
  background-image:
    radial-gradient(circle at 12% 22%, rgba(255, 250, 235, 0.5) 0 0.5px, transparent 1px),
    radial-gradient(circle at 45% 8%, rgba(255, 250, 235, 0.35) 0 0.4px, transparent 0.8px),
    radial-gradient(circle at 78% 35%, rgba(255, 250, 235, 0.4) 0 0.6px, transparent 1px),
    radial-gradient(circle at 33% 67%, rgba(255, 250, 235, 0.45) 0 0.5px, transparent 1px),
    radial-gradient(circle at 91% 72%, rgba(255, 250, 235, 0.3) 0 0.4px, transparent 0.8px),
    radial-gradient(circle at 8% 88%, rgba(255, 250, 235, 0.55) 0 0.7px, transparent 1.2px),
    radial-gradient(circle at 62% 91%, rgba(255, 250, 235, 0.35) 0 0.4px, transparent 0.8px),
    radial-gradient(circle at 55% 44%, rgba(20, 15, 10, 0.7) 0 1px, transparent 2px);
  background-size: 100% 100%;
  animation: filmDustFloat 18s ease-in-out infinite alternate;
}

@keyframes filmDustFloat {
  0% { transform: translate(0, 0); opacity: 0.4; }
  40% { opacity: 0.55; }
  100% { transform: translate(2%, -1.5%); opacity: 0.45; }
}

/* ── Projector flicker + warm cast ── */
.film-flicker {
  position: absolute;
  inset: 0;
  z-index: 4;
  background: rgba(42, 32, 18, 0.02);
  mix-blend-mode: multiply;
  animation: filmFlicker 4.5s steps(8) infinite;
  opacity: 0.16;
}

@keyframes filmFlicker {
  0%, 100% { opacity: 0.06; }
  12% { opacity: 0.1; }
  24% { opacity: 0.05; }
  37% { opacity: 0.12; }
  48% { opacity: 0.07; }
  61% { opacity: 0.09; }
  73% { opacity: 0.05; }
  86% { opacity: 0.11; }
}

/* ── Gate weave (subtle frame jitter) ── */
.film-sprockets {
  position: absolute;
  inset: 0;
  z-index: 5;
  opacity: 0.12;
  box-shadow:
    inset 0 3px 0 rgba(0, 0, 0, 0.5),
    inset 0 -3px 0 rgba(0, 0, 0, 0.5);
  animation: filmGateWeave 4.5s ease-in-out infinite;
}

@keyframes filmGateWeave {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(0.4px, -0.6px); }
  50% { transform: translate(-0.3px, 0.5px); }
  75% { transform: translate(0.5px, 0.3px); }
}

/* ── Accessibility & perf tiers ── */
body.perf-lite .film-grain {
  animation-duration: 1.2s;
  opacity: 0.18;
}
body.perf-lite .film-scratches,
body.perf-lite .film-scratches-b {
  animation-duration: 14s;
  opacity: 0.2;
}
body.perf-lite .film-flicker {
  animation-duration: 5s;
}

body.fx-disabled .film-grain {
  animation: none;
  opacity: 0.12;
}
body.fx-disabled .film-scratches,
body.fx-disabled .film-scratches-b,
body.fx-disabled .film-dust,
body.fx-disabled .film-flicker,
body.fx-disabled .film-sprockets {
  animation: none;
}
body.fx-disabled .film-scratches::after,
body.fx-disabled .film-scratches-b::after {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .film-grain,
  .film-scratches,
  .film-scratches-b,
  .film-dust,
  .film-flicker,
  .film-sprockets {
    animation: none !important;
  }
  .film-grain { opacity: 0.12; }
  .film-scratches::after,
  .film-scratches-b::after { display: none; }
}

@media (max-width: 600px) {
  .film-grain {
    opacity: 0.12;
    background-size: 180px 180px;
  }
  .film-scratches,
  .film-scratches-b {
    opacity: 0.16;
  }
}
