/* WORKS2 — frame-by-frame analogue-style grain, videos only.
   CSS-only. Panel strength + fineness controls remain unchanged. */

.works2-media:has(.works2-video),
.works2-media:has(.works2-video-static) {
  position: relative;
}

.works2-grain {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;

  width: 100%;
  height: 100%;
  opacity: var(--works2-grain-opacity, 0.35);

  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='503' height='479'%3E%3Cfilter id='a'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.92' numOctaves='4' seed='11' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23a)'/%3E%3C/svg%3E"),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='457' height='521'%3E%3Cfilter id='b'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.77' numOctaves='3' seed='37' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23b)'/%3E%3C/svg%3E"),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='541' height='443'%3E%3Cfilter id='c'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.08' numOctaves='2' seed='83' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23c)'/%3E%3C/svg%3E");

  background-repeat: repeat;
  background-size:
    calc(var(--works2-grain-size, 120px) * 4.1917) calc(var(--works2-grain-size, 120px) * 3.9917),
    calc(var(--works2-grain-size, 120px) * 3.8083) calc(var(--works2-grain-size, 120px) * 4.3417),
    calc(var(--works2-grain-size, 120px) * 4.5083) calc(var(--works2-grain-size, 120px) * 3.6917);

  mix-blend-mode: overlay;

  /* 15 discrete grain states per second: flicker/cut, never glide. */
  animation: works2-grain-frame 1s steps(1, end) infinite;
}

@keyframes works2-grain-frame {
  0%    { background-position:    0px    0px,   37px   83px,  -71px   29px; }
  6.67% { background-position:   97px  -53px,  -61px  137px,   43px -101px; }
  13.33%{ background-position: -131px   71px,  113px  -47px,  -89px  149px; }
  20%   { background-position:   59px  163px, -127px   31px,  137px  -67px; }
  26.67%{ background-position: -173px -109px,   71px  181px,   19px   97px; }
  33.33%{ background-position:  149px   43px,  -31px -157px, -143px   61px; }
  40%   { background-position:  -47px  197px,  167px   89px,   73px -181px; }
  46.67%{ background-position:  191px  -79px, -181px  121px,  -23px  173px; }
  53.33%{ background-position: -103px -191px,   53px   17px,  181px   41px; }
  60%   { background-position:   31px  109px,  197px -113px, -167px  -37px; }
  66.67%{ background-position:  173px  181px, -149px  -71px,   97px  127px; }
  73.33%{ background-position: -197px   23px,   89px  157px,   31px -149px; }
  80%   { background-position:  113px -167px,  -97px   61px,  157px   83px; }
  86.67%{ background-position:  -71px  137px,  181px  -19px, -121px  197px; }
  93.33%{ background-position:  157px   97px, -173px -137px,   61px  -53px; }
  100%  { background-position:    0px    0px,   37px   83px,  -71px   29px; }
}

/* Beim Ein-/Ausblenden der Flammen am Seitenende kurz anhalten. */
body.is-end-transition .works2-grain {
  animation-play-state: paused;
}

/* Ausserhalb des Bildschirms wird das Korn nicht berechnet (works2.js). */
.works2-grain.is-idle {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .works2-grain {
    animation: none;
  }
}
