@layer reset, tokens, base, layout, components, utilities, overrides;

@layer tokens {
  @property --seed { syntax: "<integer>"; inherits: true; initial-value: 4287; }
  @property --shift { syntax: "<number>"; inherits: true; initial-value: 0; }
  @property --n1 { syntax: "<number>"; inherits: true; initial-value: 0.5; }
  @property --n2 { syntax: "<number>"; inherits: true; initial-value: 0.5; }
  @property --n3 { syntax: "<number>"; inherits: true; initial-value: 0.5; }
  @property --plate { syntax: "<integer>"; inherits: true; initial-value: 8; }
  @property --a { syntax: "<number>"; inherits: false; initial-value: 0; }
  @property --b { syntax: "<number>"; inherits: false; initial-value: 0; }
  @property --c { syntax: "<number>"; inherits: false; initial-value: 0; }

  :root {
    color-scheme: light;

    --color-paper: oklch(94% 0.017 95);
    --color-stain: oklch(84% 0.04 85 / 0.34);
    --color-speck: oklch(52% 0.04 70 / 0.5);
    --color-text: oklch(28% 0.03 70);
    --color-crown: oklch(62% 0.05 calc(var(--shift) + 170));
    --color-wing: oklch(58% 0.11 calc(var(--shift) + 225));
    --color-side: oklch(66% 0.18 calc(var(--shift) + 5));
    --color-heart: oklch(68% 0.17 calc(var(--shift) + 355));
    --color-base: oklch(72% 0.17 calc(var(--shift) + 50));
    --color-core: oklch(38% 0.06 calc(var(--shift) + 40));

    --space-xs: 0.35rem;
    --space-s: 0.6rem;
    --space-m: 1rem;
    --type-s: 0.95rem;
    --leading: 1.3;

    --seed: random(--color-seed, 0, 9999, 1);
    --shift: random(--color-shift, 0, 360, 1);
    --n1: random(--color-1, 0, 1);
    --n2: random(--color-2, 0, 1);
    --n3: random(--color-3, 0, 1);
    --plate: calc(mod(var(--seed), 10) + 1);
  }
}

@layer reset {
  * {
    box-sizing: border-box;
  }

  body,
  p {
    margin: 0;
  }

  i {
    display: block;
  }
}

@layer base {
  body {
    background: var(--color-paper);
    color: var(--color-text);
    font: var(--type-s) / var(--leading) Georgia, "Times New Roman", serif;
  }

  a:focus-visible {
    outline: 2px solid var(--color-text);
    outline-offset: 3px;
  }
}

@layer layout {
  .sheet {
    display: grid;
    align-content: center;
    justify-items: center;
    gap: var(--space-xs);
    min-block-size: 100dvh;
    padding: var(--space-m);
    background:
      radial-gradient(circle 1px at 13% 21%, var(--color-speck) 99%, transparent),
      radial-gradient(circle 1px at 71% 9%, var(--color-speck) 99%, transparent),
      radial-gradient(circle 1.5px at 88% 67%, var(--color-speck) 99%, transparent),
      radial-gradient(circle 1px at 37% 88%, var(--color-speck) 99%, transparent),
      radial-gradient(ellipse 34% 26% at 16% 80%, var(--color-stain), transparent),
      radial-gradient(ellipse 30% 40% at 90% 18%, var(--color-stain), transparent),
      var(--color-paper);
  }

  .plate {
    container-type: inline-size;
    position: relative;
    inline-size: min(100%, 86dvh, 44rem);
    aspect-ratio: 1;
  }
}

@layer components {
  .defs {
    position: absolute;
    inline-size: 0;
    block-size: 0;
  }

  .ink {
    position: absolute;
    inset: 0;
    isolation: isolate;
    overflow: clip;
    background: white;
    mix-blend-mode: multiply;
  }

  .blot {
    position: absolute;
    inset: 0;
    background: white;
    filter: blur(0.75cqi) contrast(30);
  }

  .half {
    position: absolute;
    inset-block: 0;
    inset-inline-start: 0;
    inline-size: 50%;
    transform-origin: 100% 50%;
    filter: url(#bleed);

    & + & {
      scale: -1 1;
    }
  }

  :where(.half) i {
    --a: mod(sin(var(--seed) * 0.0137 + var(--i) * 12.9898) * 43758.5453, 1);
    --b: mod(sin(var(--seed) * 0.0211 + var(--i) * 78.233) * 43758.5453, 1);
    --c: mod(sin(var(--seed) * 0.0173 + var(--i) * 37.719) * 43758.5453, 1);
    --reach: pow(var(--a), 1.3);
    --rise: var(--b);
    --size: calc((1 - var(--reach) * 0.7) * (9cqi + var(--c) * 15cqi));
    --shape: calc(0.6 + var(--c) * 1);
    --turn: calc(var(--b) * 180deg);

    position: absolute;
    inset-block-start: calc(10% + var(--rise) * 78%);
    inset-inline-end: calc(var(--reach) * 72%);
    inline-size: var(--size);
    aspect-ratio: var(--shape);
    translate: 50% -50%;
    rotate: var(--turn);
    border-radius: 50%;
    background: black;

    &:nth-child(1) { --i: 1; --reach: 0; --rise: 0.02; --size: 7cqi; }
    &:nth-child(2) { --i: 2; --reach: 0.02; --rise: 0.16; }
    &:nth-child(3) { --i: 3; --reach: 0.03; --rise: 0.32; }
    &:nth-child(4) { --i: 4; --reach: 0.02; --rise: 0.48; }
    &:nth-child(5) { --i: 5; --reach: 0.03; --rise: 0.64; }
    &:nth-child(6) { --i: 6; --reach: 0.02; --rise: 0.8; }
    &:nth-child(7) { --i: 7; --reach: 0.01; --rise: 0.96; --size: 6cqi; }
    &:nth-child(8) { --i: 8; }
    &:nth-child(9) { --i: 9; }
    &:nth-child(10) { --i: 10; }
    &:nth-child(11) { --i: 11; }
    &:nth-child(12) { --i: 12; }
    &:nth-child(13) { --i: 13; }
    &:nth-child(14) { --i: 14; }
    &:nth-child(15) { --i: 15; }
    &:nth-child(16) { --i: 16; }
    &:nth-child(17) { --i: 17; }
    &:nth-child(18) { --i: 18; }
    &:nth-child(n + 19):nth-child(-n + 24) {
      --reach: calc(0.42 + var(--a) * 0.5);
      --size: calc(10cqi + var(--c) * 15cqi);
      --shape: calc(3.2 + var(--b) * 2.4);
      --turn: calc(var(--b) * 130deg - 65deg);
    }
    &:nth-child(19) { --i: 19; }
    &:nth-child(20) { --i: 20; }
    &:nth-child(21) { --i: 21; }
    &:nth-child(22) { --i: 22; }
    &:nth-child(23) { --i: 23; --size: calc(2.5cqi + var(--c) * 4.5cqi); --shape: 1; background: white; --reach: calc(var(--a) * 0.42); }
    &:nth-child(24) { --i: 24; --size: calc(2cqi + var(--c) * 4cqi); --shape: 1; background: white; --reach: calc(var(--a) * 0.42); }
    &:nth-child(25) { --i: 25; --size: calc(2cqi + var(--c) * 4cqi); background: white; --reach: calc(var(--a) * 0.5); }
    &:nth-child(26) { --i: 26; --size: calc(2cqi + var(--c) * 3cqi); background: white; --reach: calc(var(--a) * 0.5); }
    &:nth-child(27) { --i: 27; --reach: calc(0.78 + var(--a) * 0.2); --size: calc(1.1cqi + var(--c) * 1.9cqi); }
    &:nth-child(28) { --i: 28; --reach: calc(0.78 + var(--a) * 0.2); --size: calc(1cqi + var(--c) * 1.6cqi); }
    &:nth-child(29) { --i: 29; --reach: calc(0.84 + var(--a) * 0.14); --size: calc(0.9cqi + var(--c) * 1.3cqi); }
    &:nth-child(30) { --i: 30; --reach: calc(0.84 + var(--a) * 0.14); --size: calc(0.9cqi + var(--c) * 1.2cqi); }
  }

  .color {
    position: absolute;
    inset: 0;
    mix-blend-mode: screen;
  }

  .wash {
    --y1: calc(16% + var(--n1) * 8%);
    --y2: calc(36% + var(--n2) * 8%);
    --y3: calc(48% + var(--n3) * 12%);
    --y4: calc(60% + var(--n1) * 8%);
    --y5: calc(78% + var(--n2) * 8%);
    --spill: 14cqi;

    position: absolute;
    inset-block: calc(var(--spill) * -1);
    inset-inline-start: calc(var(--spill) * -1);
    inline-size: calc(50% + var(--spill) * 2);
    transform-origin: calc(100% - var(--spill)) 50%;
    clip-path: inset(var(--spill));
    filter: url(#pigment);

    & + & {
      scale: -1 1;
    }

    background:
      radial-gradient(ellipse 2.4% 19% at calc(100% - var(--spill)) 36%, var(--color-core) 58%, transparent),
      radial-gradient(ellipse 70% 20% at 86% var(--y1), var(--color-crown) 58%, transparent),
      radial-gradient(ellipse 34% 40% at 12% var(--y3), var(--color-side) 62%, transparent),
      radial-gradient(ellipse 76% 18% at 72% var(--y2), var(--color-wing) 62%, transparent),
      radial-gradient(ellipse 60% 15% at 86% var(--y4), var(--color-heart) 60%, transparent),
      radial-gradient(ellipse 96% 30% at 72% var(--y5), var(--color-base) 62%, transparent),
      var(--color-wing);
  }

  .caption {
    counter-reset: plate var(--plate);
    display: grid;
    justify-items: center;
    gap: var(--space-xs);
    text-align: center;
  }

  .plate-no::before {
    content: "Plate " counter(plate, upper-roman) ". What do you see?";
  }

  .next {
    padding: var(--space-xs) var(--space-s);
    color: var(--color-text);
    text-underline-offset: 0.2em;
  }

  .no-random {
    display: none;
    font-size: 0.8rem;
  }

  @supports not (order: random(1, 2)) {
    .no-random {
      display: block;
    }
  }
}
