/*
  Grainy gradient backdrop: soft colour blobs, heavily blurred, with film grain on top.
  Tuned in /lab/gradients.html.

  Usage:
    <body class="palette-tide">
      <div class="backdrop" aria-hidden="true"><i></i><i></i><i></i><i></i></div>

  Pick a page's look by swapping the palette class on <body>; --text is available
  to the whole page for matching text colour.
*/

/* ---------- palettes ---------- */
.palette-tide        { --base: #9fbcae; --b1: #155d89; --b2: #e8c39a; --b3: #1f2a44; --b4: #274e39; --text: #000000; }
.palette-lagoon      { --base: #39a6b6; --b1: #808be1; --b2: #7cd6b6; --b3: #bc665a; --b4: #1b4d55; --text: #ffffff; }
.palette-verdigris   { --base: #70bcb5; --b1: #7896ce; --b2: #6fbc8c; --b3: #a25f65; --b4: #20453b; --text: #ffffff; }
.palette-robespierre { --base: #9fbcae; --b1: #8a4b2a; --b2: #e8c39a; --b3: #1f2a44; --b4: #dde8e2; --text: #000000; }
.palette-midnight    { --base: #232b45; --b1: #3a3f86; --b2: #10648e; --b3: #0f0b16; --b4: #6e7fa8; --text: #ffffff; }

/* ---------- the effect ---------- */
.backdrop {
  --grain: 0.65;
  --softness: 9;
  --speed: 1.5;
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  container-type: inline-size;
  background: var(--base);
}
.backdrop > i {
  position: absolute;
  aspect-ratio: 1;
  translate: -50% -50%;
  border-radius: 50%;
  filter: blur(calc(var(--softness) * 1cqi));
  will-change: transform;
  animation: backdrop-drift calc(var(--dur) / var(--speed)) ease-in-out infinite alternate;
  animation-delay: var(--delay);
}
.backdrop > i:nth-child(1) { background: var(--b1); left: 20%; top: 75%; width: 75%; --dx: 18%;  --dy: -12%; --dur: 11s; --delay: 0s; }
.backdrop > i:nth-child(2) { background: var(--b2); left: 75%; top: 25%; width: 70%; --dx: -15%; --dy: 18%;  --dur: 14s; --delay: -4s; }
.backdrop > i:nth-child(3) { background: var(--b3); left: 85%; top: 85%; width: 60%; --dx: -10%; --dy: -15%; --dur: 17s; --delay: -8s; }
.backdrop > i:nth-child(4) { background: var(--b4); left: 30%; top: 15%; width: 55%; --dx: 20%;  --dy: 10%;  --dur: 20s; --delay: -12s; }

@keyframes backdrop-drift {
  from { transform: translate(0, 0) scale(1) rotate(0deg); }
  to   { transform: translate(var(--dx), var(--dy)) scale(1.25) rotate(25deg); }
}

/* film grain: SVG turbulence noise blended over the colours */
.backdrop::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size: 220px;
  mix-blend-mode: overlay;
  opacity: var(--grain);
}

@media (prefers-reduced-motion: reduce) {
  .backdrop > i { animation: none; }
}

/* the same grain for anything else (images, cards): add class="grainy" */
.grainy { position: relative; isolation: isolate; }
.grainy::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  border-radius: inherit;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size: 160px;
  mix-blend-mode: overlay;
  opacity: var(--grain, 0.55);
}
