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

@layer tokens {
  @property --seed {
    syntax: "<integer>";
    inherits: true;
    initial-value: 4242;
  }

  @property --saw {
    syntax: "<integer>";
    inherits: true;
    initial-value: 2;
  }

  @property --impression {
    syntax: "<integer>";
    inherits: true;
    initial-value: 9;
  }

  @property --plate {
    syntax: "<integer>";
    inherits: true;
    initial-value: 7;
  }

  @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;
  }

  @counter-style ink-seen {
    system: fixed 0;
    suffix: "";
    symbols:
      "Two bears high-fiving"
      "A moth with a mortgage"
      "Your landlord, from behind"
      "The ring a mug leaves"
      "A pelvis. Everyone says pelvis."
      "Two waiters in a dispute"
      "An angry bat, or a calm one"
      "Nothing. You were rude about it."
      "Your childhood kitchen"
      "A crab doing its taxes"
      "The same thing as last time"
      "A very small cathedral"
      "Twin ghosts sharing a coat"
      "Someone who owes you money"
      "A lobster at a funeral"
      "Ink";
  }

  @counter-style ink-impressions {
    system: fixed 0;
    suffix: "";
    symbols:
      "Has not called their mother."
      "Needs a nap and knows it."
      "Overwatered."
      "Will reload this page again."
      "Trusts printers too much."
      "Reads the terms and conditions. Enjoys them."
      "Still thinking about that email."
      "Afraid of geese, with reason."
      "Claims to be fine."
      "Owns too many tote bags."
      "Talks to the toaster. Toaster declines comment."
      "Has strong opinions about fonts."
      "Fine. Suspiciously fine."
      "Refer to a second inkblot."
      "Holds grudges against furniture."
      "Would make a good lighthouse keeper.";
  }

  :root {
    color-scheme: light;

    --color-desk: oklch(72% 0.04 172);
    --color-form: oklch(93% 0.1 102);
    --color-card: oklch(97% 0.003 250);
    --color-crease: oklch(88% 0.004 250);
    --color-ink-wash: oklch(24% 0.012 270);
    --color-print: oklch(18% 0 0);
    --color-rule: oklch(30% 0.02 100);
    --color-type: oklch(16% 0 0);
    --color-stamp: oklch(50% 0.2 28);
    --color-steel: oklch(62% 0.006 250);
    --color-steel-light: oklch(84% 0.004 250);

    --space-xs: 0.35rem;
    --space-s: 0.6rem;
    --space-m: 1rem;
    --radius-s: 0.1rem;
    --shadow-paper: 0 0.1rem 0.3rem oklch(25% 0.03 172 / 0.45);
    --shadow-card: 0 0.08rem 0.22rem oklch(20% 0.02 100 / 0.4);
    --type-xs: 0.75rem;
    --type-s: 0.875rem;
    --type-typed: clamp(1rem, 0.94rem + 0.35vw, 1.15rem);
    --leading: 1.3;
    --font-print: Helvetica, Arial, sans-serif;
    --font-typed: "Courier New", Courier, monospace;

    --seed: random(--ink-seed, 0, 9999, 1);
    --saw: mod(var(--seed), 16);
    --impression: mod(round(down, var(--seed) / 16, 1), 16);
    --plate: calc(mod(round(down, var(--seed) / 256, 1), 10) + 1);
  }
}

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

  body,
  h1,
  dl,
  dd,
  p {
    margin: 0;
  }
}

@layer base {
  body {
    background: var(--color-desk);
    color: var(--color-print);
    font-family: var(--font-print);
    line-height: var(--leading);
  }

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

@layer layout {
  .desk {
    display: grid;
    place-items: center;
    min-block-size: 100dvh;
    padding: var(--space-m);
  }

  .plate {
    container-type: inline-size;
    display: grid;
    gap: var(--space-s);
    inline-size: min(100%, 34rem);
    padding: var(--space-m);
    border-radius: var(--radius-s);
    box-shadow: var(--shadow-paper);
    background: var(--color-form);
  }
}

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

  .plate-head {
    counter-reset: plate var(--plate) file var(--seed);
    display: grid;
    gap: var(--space-xs);

    h1 {
      padding-block-end: var(--space-xs);
      border-block-end: 0.15rem solid var(--color-print);
      font-size: var(--type-s);
      font-weight: 700;
    }
  }

  .typed-line {
    display: flex;
    justify-content: space-between;
    gap: var(--space-m);
    color: var(--color-type);
    font: var(--type-s) / var(--leading) var(--font-typed);
  }

  .plate-no::before {
    content: "PLATE " counter(plate, upper-roman);
  }

  .file-no::before {
    content: "FILE " counter(file);
  }

  .mount {
    position: relative;
  }

  .clip {
    position: absolute;
    inset-block-start: -0.85rem;
    inset-inline-start: 1.5rem;
    inline-size: 0.95rem;
    block-size: 3.4rem;
    border: 0.14rem solid var(--color-steel);
    border-block-start-color: var(--color-steel-light);
    border-radius: 0.6rem;

    &::after {
      content: "";
      position: absolute;
      inset-block: 0.5rem -0.14rem;
      inset-inline: 0.14rem;
      border: 0.14rem solid var(--color-steel);
      border-block-end: 0;
      border-radius: 0.4rem 0.4rem 0 0;
    }
  }

  .field {
    position: relative;
    isolation: isolate;
    aspect-ratio: 5 / 4;
    overflow: clip;
    box-shadow: var(--shadow-card);
    background:
      linear-gradient(90deg, transparent calc(50% - 1px), var(--color-crease) calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px)),
      var(--color-card);
  }

  .blot {
    position: absolute;
    inset: 0;
    background: white;
    filter: blur(0.85cqi) contrast(30);
    mix-blend-mode: multiply;
  }

  .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.35);
    --rise: var(--b);
    --size: calc((1 - var(--reach) * 0.72) * (11cqi + var(--c) * 17cqi));

    position: absolute;
    inset-block-start: calc(9% + var(--rise) * 82%);
    inset-inline-end: calc(var(--reach) * 74%);
    inline-size: var(--size);
    aspect-ratio: calc(0.65 + var(--c) * 0.95);
    translate: 50% -50%;
    rotate: calc(var(--b) * 180deg);
    border-radius: 50%;
    background: black;

    &:nth-child(1) { --i: 1; --reach: 0; --rise: 0.06; }
    &:nth-child(2) { --i: 2; --reach: 0.03; --rise: 0.26; }
    &:nth-child(3) { --i: 3; --reach: 0.02; --rise: 0.46; }
    &:nth-child(4) { --i: 4; --reach: 0.04; --rise: 0.66; }
    &:nth-child(5) { --i: 5; --reach: 0.02; --rise: 0.86; }
    &:nth-child(6) { --i: 6; }
    &:nth-child(7) { --i: 7; }
    &: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; --size: calc(2.5cqi + var(--c) * 5cqi); background: white; }
    &:nth-child(18) { --i: 18; --size: calc(2.5cqi + var(--c) * 6cqi); background: white; }
    &:nth-child(19) { --i: 19; --size: calc(2cqi + var(--c) * 4.5cqi); background: white; }
    &:nth-child(20) { --i: 20; --size: calc(2cqi + var(--c) * 4cqi); background: white; }
    &:nth-child(21) { --i: 21; --reach: calc(0.72 + var(--a) * 0.26); --size: calc(1.2cqi + var(--c) * 2cqi); }
    &:nth-child(22) { --i: 22; --reach: calc(0.72 + var(--a) * 0.26); --size: calc(1.2cqi + var(--c) * 1.8cqi); }
    &:nth-child(23) { --i: 23; --reach: calc(0.8 + var(--a) * 0.18); --size: calc(1.1cqi + var(--c) * 1.6cqi); }
    &:nth-child(24) { --i: 24; --reach: calc(0.8 + var(--a) * 0.18); --size: calc(1.1cqi + var(--c) * 1.4cqi); }
  }

  .wash {
    --x1: calc(mod(sin(var(--seed) * 0.031) * 43758.5453, 1) * 26cqi + 3cqi);
    --y1: calc(mod(sin(var(--seed) * 0.043) * 43758.5453, 1) * 70% + 15%);
    --x2: calc(mod(sin(var(--seed) * 0.057) * 43758.5453, 1) * 30cqi + 2cqi);
    --y2: calc(mod(sin(var(--seed) * 0.071) * 43758.5453, 1) * 70% + 15%);

    position: absolute;
    inset: 0;
    mix-blend-mode: lighten;
    background:
      radial-gradient(circle 13cqi at calc(50% - var(--x1)) var(--y1), var(--color-ink-wash), transparent),
      radial-gradient(circle 13cqi at calc(50% + var(--x1)) var(--y1), var(--color-ink-wash), transparent),
      radial-gradient(circle 10cqi at calc(50% - var(--x2)) var(--y2), var(--color-ink-wash), transparent),
      radial-gradient(circle 10cqi at calc(50% + var(--x2)) var(--y2), var(--color-ink-wash), transparent);
  }

  .form {
    counter-reset: saw var(--saw) impression var(--impression);
    display: grid;
    gap: var(--space-s);

    dt {
      font-size: var(--type-xs);
      font-weight: 700;
    }

    dd {
      min-block-size: 2lh;
      padding-block-start: var(--space-xs);
      border-block-end: 1px solid var(--color-rule);
      color: var(--color-type);
      font: var(--type-typed) / var(--leading) var(--font-typed);
      text-wrap: balance;
    }
  }

  .seen::before {
    content: counter(saw, ink-seen);
  }

  .impression::before {
    content: counter(impression, ink-impressions);
  }

  .plate-foot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-m);
  }

  .stamp {
    padding: 0.15rem var(--space-xs);
    rotate: -6deg;
    rotate: random(--ink-stamp, -11deg, -3deg);
    border: 0.15rem solid var(--color-stamp);
    color: var(--color-stamp);
    font: 700 var(--type-s) / 1.1 var(--font-print);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    mix-blend-mode: multiply;
  }

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

  .no-random {
    display: none;
    font-size: var(--type-xs);
  }

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

  @media (prefers-reduced-motion: no-preference) {
    @keyframes unfold {
      from {
        scale: 0.02 1;
      }
    }

    .half + .half {
      animation: unfold 1.1s 0.25s cubic-bezier(0.3, 0.1, 0.2, 1) backwards;
    }
  }
}
