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

@layer tokens {
  :root {
    color-scheme: dark;
    --color-bg: oklch(16% 0.01 250);
    --color-surface: oklch(22% 0.01 250);
    --color-text: oklch(95% 0.005 250);
    --color-muted: oklch(72% 0.01 250);
    --color-border: oklch(34% 0.01 250);
    --color-accent: oklch(80% 0.12 85);
    --space-xs: .35rem;
    --space-s: .6rem;
    --space-m: 1rem;
    --space-l: 1.5rem;
    --space-xl: 2.5rem;
    --radius-s: .4rem;
    --radius-m: .75rem;
    --leading-tight: 1.25;
    --leading-body: 1.5;
    --text-s: .85rem;
    --text-m: 1rem;
    --size-target-min: 24px;
  }

  @function --when-supported(--value) {
    result: var(--value);
  }
}

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

  body {
    margin: 0;
  }

  :where(input, button) {
    font: inherit;
  }
}

@layer base {
  html,
  body {
    background: var(--color-bg);
    color: var(--color-text);
    font: var(--text-m) / var(--leading-body) system-ui, sans-serif;
  }

  header {
    max-inline-size: 900px;
    margin-block: var(--space-xl) 0;
    margin-inline: auto;
    padding-inline: var(--space-m);

    & h1 {
      margin-block-end: var(--space-xs);
      line-height: var(--leading-tight);
    }

    & p {
      max-inline-size: 70ch;
      margin-block-start: 0;
      color: var(--color-muted);
    }

    & a {
      color: var(--color-accent);
    }
  }

  :is(h1, h2, h3) {
    text-wrap: balance;
  }

  p {
    text-wrap: pretty;
  }

  :where(a, button, input, [tabindex]):focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
  }
}

@layer components {
  .support-banner {
    display: block;
    display: --when-supported(none);
    max-inline-size: 900px;
    margin-block: var(--space-m);
    margin-inline: auto;
    padding: var(--space-s) var(--space-m);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-s);
    background: var(--color-surface);
    color: var(--color-muted);
    font-size: var(--text-s);

    & strong {
      color: var(--color-text);
    }
  }

  .chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-xs);
    margin: 0;
    padding: 0;
    border: 0;

    & legend {
      inline-size: 100%;
      margin-block-end: var(--space-xs);
      color: var(--color-muted);
      font-size: var(--text-s);
    }

    & label {
      display: inline-flex;
      align-items: center;
      gap: var(--space-xs);
      min-block-size: var(--size-target-min);
      padding: var(--space-xs) var(--space-s);
      border: 1px solid var(--color-border);
      border-radius: 999px;
      background: var(--color-surface);
      font-size: var(--text-s);
      cursor: pointer;

      &:has(:checked) {
        border-color: var(--color-accent);
        color: var(--color-accent);
      }
    }

    & input {
      accent-color: var(--color-accent);
      margin: 0;
    }
  }
}

@layer tokens {
  :root {
    --card-bg: oklch(98.5% 0.006 95);
    --card-text: oklch(26% 0.02 60);
    --card-muted: oklch(50% 0.03 60);
    --card-rule: oklch(86% 0.03 80);
    --card-qty: oklch(45% 0.12 45);
    --font-recipe: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  }

  @property --serves { syntax: "<integer>"; inherits: true; initial-value: 4; }
  @property --us { syntax: "<integer>"; inherits: true; initial-value: 0; }
  @property --kind { syntax: "<integer>"; inherits: false; initial-value: 1; }
  @property --rho { syntax: "<number>"; inherits: false; initial-value: 0; }
  @property --unit { syntax: "<integer>"; inherits: false; initial-value: 0; }
  @property --e { syntax: "<integer>"; inherits: false; initial-value: 8; }
  @property --whole { syntax: "<integer>"; inherits: false; initial-value: 0; }
  @property --frac { syntax: "<integer>"; inherits: false; initial-value: 0; }
  @property --label { syntax: "<integer>"; inherits: false; initial-value: 0; }
  @property --plural { syntax: "<integer>"; inherits: true; initial-value: 0; }

  @counter-style eighths {
    system: fixed 0;
    symbols: "\200B" "⅛" "¼" "⅜" "½" "⅝" "¾" "⅞";
  }

  @counter-style blank {
    system: cyclic;
    symbols: "\200B";
  }

  @counter-style whole {
    system: extends decimal;
    range: 1 infinite;
    fallback: blank;
  }

  @counter-style unit-label {
    system: fixed 0;
    symbols: "g" "ml" "cup" "cups" "tbsp" "tsp" "oz" "\200B";
  }

  @function --is(--a <number>, --b <number>) returns <integer> {
    result: calc(1 - min(1, max(var(--a) - var(--b), var(--b) - var(--a)) * 1000));
  }

  @function --at-least(--x <number>, --min <number>) returns <integer> {
    result: clamp(0, (var(--x) - var(--min)) * 1000000 + 1, 1);
  }

  @function --us-unit(--ml <number>) returns <integer> {
    --cup: --at-least(var(--ml), 59);
    --tbsp: --at-least(var(--ml), 14.7);
    result: calc(var(--cup) * 2 + (1 - var(--cup)) * (var(--tbsp) * 4 + (1 - var(--tbsp)) * 5));
  }

  @function --unit-size(--unit <integer>) returns <number> {
    result: calc(
      --is(var(--unit), 0) + --is(var(--unit), 1) + --is(var(--unit), 7)
      + --is(var(--unit), 2) * 236.59
      + --is(var(--unit), 4) * 14.787
      + --is(var(--unit), 5) * 4.929
      + --is(var(--unit), 6) * 28.35
    );
  }

  @function --unit-step(--unit <integer>, --value <number>) returns <number> {
    --metric: calc(--is(var(--unit), 0) + --is(var(--unit), 1));
    result: calc(
      var(--metric) * (2 - --at-least(var(--value), 10) - 0.8 * --at-least(var(--value), 50))
      + --is(var(--unit), 2) * 4
      + --is(var(--unit), 4) * 2
      + --is(var(--unit), 5) * 8
      + --is(var(--unit), 6) * 4
      + --is(var(--unit), 7) * 2
    );
  }

  @function --eighths(--value <number>, --step <number>) returns <integer> {
    result: calc(max(1, round(var(--value) * var(--step))) * 8 / var(--step));
  }
}

@layer layout {
  .kitchen {
    display: grid;
    gap: var(--space-l);
    max-inline-size: 720px;
    margin-block: var(--space-l) var(--space-xl);
    margin-inline: auto;
    padding-inline: var(--space-m);

    &:has([value="s1"]:checked) { --serves: 1; }
    &:has([value="s2"]:checked) { --serves: 2; }
    &:has([value="s6"]:checked) { --serves: 6; }
    &:has([value="s8"]:checked) { --serves: 8; }
    &:has([value="s12"]:checked) { --serves: 12; }
    &:has([value="us"]:checked) { --us: 1; }
  }

  .controls {
    display: grid;
    gap: var(--space-m);
  }
}

@layer components {
  .recipe {
    --base: attr(data-serves type(<number>), 4);
    --factor: calc(var(--serves) / var(--base));
    padding: var(--space-l) clamp(var(--space-m), 5vi, var(--space-xl));
    border-radius: var(--radius-m);
    background: var(--card-bg);
    color: var(--card-text);
    font-family: var(--font-recipe);

    & h2 {
      margin: 0;
      font-size: 1.75rem;
      line-height: var(--leading-tight);
    }

    & h3 {
      margin-block: var(--space-l) var(--space-s);
      padding-block-end: var(--space-xs);
      border-block-end: 1px solid var(--card-rule);
      color: var(--card-muted);
      font-size: var(--text-s);
      letter-spacing: 0.12em;
      text-transform: uppercase;
    }
  }

  .serves {
    margin-block: var(--space-xs) 0;
    color: var(--card-muted);
    font-style: italic;
    counter-reset: serves var(--serves);

    &::before {
      content: "Serves " counter(serves);
    }
  }

  .ingredients {
    display: grid;
    gap: var(--space-xs);
    margin: 0;
    padding: 0;
    list-style: none;

    & li {
      --qty: attr(data-qty type(<number>), 0);
      --kind: attr(data-kind type(<integer>), 1);
      --rho: attr(data-density type(<number>), 0);
      --amt: calc(var(--qty) * var(--factor));
      --ml: if(style(--kind: 2): var(--amt); else: calc(var(--amt) / max(var(--rho), 0.0001)));
      --unit: if(
        style(--us: 0) and style(--kind: 1): 0;
        style(--us: 0) and style(--kind: 2): 1;
        style(--kind: 3): 7;
        style(--rho: 0) and style(--kind: 1): 6;
        else: --us-unit(var(--ml))
      );
      --value: calc(if(style(--unit: 2) or style(--unit: 4) or style(--unit: 5): var(--ml); else: var(--amt)) / --unit-size(var(--unit)));
      --e: --eighths(var(--value), --unit-step(var(--unit), var(--value)));
      --whole: round(down, var(--e) / 8, 1);
      --frac: mod(var(--e), 8);
      --label: calc(var(--unit) + --is(var(--unit), 2) * --at-least(var(--e), 9));
      --plural: --at-least(var(--e), 9);
      display: grid;
      grid-template-columns: minmax(6.5rem, auto) 1fr;
      gap: var(--space-m);
      align-items: baseline;
      counter-reset: whole var(--whole) frac var(--frac) label var(--label);

      &::before {
        content: counter(whole, whole) counter(frac, eighths) " " counter(label, unit-label);
        color: var(--card-qty);
        font-weight: 700;
        font-variant-numeric: tabular-nums;
        text-align: end;
      }
    }

    & .one {
      display: if(style(--plural: 1): none; else: inline);
    }

    & .many {
      display: if(style(--plural: 1): inline; else: none);
    }
  }

  .method {
    display: grid;
    gap: var(--space-s);
    margin: 0;
    padding-inline-start: 1.25rem;
    line-height: var(--leading-body);
  }
}
