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

@layer tokens {
  :root {
    --tbl-bg: oklch(99% 0.002 250);
    --tbl-text: oklch(25% 0.01 250);
    --tbl-muted: oklch(50% 0.012 250);
    --tbl-line: oklch(90% 0.005 250);
    --tbl-track: oklch(93% 0.005 250);
    --tbl-progress: oklch(62% 0.12 245);
    --tbl-good: oklch(58% 0.14 150);
    --tbl-bad: oklch(56% 0.18 28);
    --tbl-cold: oklch(95% 0.03 240);
    --tbl-hot: oklch(48% 0.15 265);
    --tbl-mark: oklch(30% 0.01 250);
  }

  @function --on(--bg <color>, --threshold <number>: 0.62) returns <color> {
    result: oklch(from var(--bg) clamp(0.15, (var(--threshold) - l) * infinity, 0.99) 0 0);
  }
  @function --percent(--value <number>, --max <number>: 100) returns <percentage> {
    result: clamp(0%, var(--value) / var(--max) * 100%, 100%);
  }
  @function --scale(--value <number>, --min <number>, --max <number>, --from <color>, --to <color>) returns <color> {
    --p: clamp(0%, (var(--value) - var(--min)) / (var(--max) - var(--min)) * 100%, 100%);
    result: color-mix(in oklch, var(--to) var(--p), var(--from));
  }
  @function --sign(--value <number>) returns <number> {
    result: clamp(-1, var(--value) * infinity, 1);
  }
  @function --stagger(--step <time>: 50ms, --max <time>: 600ms) returns <time> {
    result: min(var(--max), (sibling-index() - 1) * var(--step));
  }
}

@layer layout {
  .report {
    max-inline-size: 820px;
    margin-block: var(--space-l) var(--space-xl);
    margin-inline: auto;
    padding-inline: var(--space-m);
  }

  .scroller {
    overflow-x: auto;
    border-radius: var(--radius-m);
    background: var(--tbl-bg);
  }
}

@layer components {
  table {
    inline-size: 100%;
    min-inline-size: 34rem;
    border-collapse: collapse;
    color: var(--tbl-text);
    font-variant-numeric: tabular-nums;
  }

  caption {
    padding: var(--space-m) var(--space-m) var(--space-s);
    font-weight: 700;
    text-align: start;
  }

  th,
  td {
    padding: var(--space-s) var(--space-m);
    border-block-end: 1px solid var(--tbl-line);
    text-align: start;
  }

  thead th {
    color: var(--tbl-muted);
    font-size: var(--text-s);
    font-weight: 600;
  }

  tbody tr {
    @media (prefers-reduced-motion: no-preference) {
      animation: row-in 0.5s ease-out both;
      animation-delay: --stagger(60ms);
    }
  }

  @keyframes row-in {
    from {
      opacity: 0;
      translate: 0 0.5rem;
    }
  }

  .bar {
    --v: attr(data-value type(<number>), 0);
    inline-size: 40%;

    &::after {
      content: "";
      display: block;
      block-size: 0.4rem;
      margin-block-start: var(--space-xs);
      border-radius: 999px;
      background:
        linear-gradient(90deg, transparent calc(100% / 1.2 - 1px), var(--tbl-mark) 0 calc(100% / 1.2 + 1px), transparent 0),
        linear-gradient(90deg, --scale(var(--v), 99.9, 100, var(--tbl-progress), var(--tbl-good)) --percent(var(--v), 120), var(--tbl-track) 0);
    }
  }

  .delta {
    --v: attr(data-value type(<number>), 0);
    color: --scale(--sign(var(--v)), -1, 1, var(--tbl-bad), var(--tbl-good));
    font-weight: 600;
    white-space: nowrap;

    &::before {
      content: "\25B2";
      display: inline-block;
      margin-inline-end: var(--space-xs);
      font-size: 0.7em;
      rotate: calc((1 - --sign(var(--v))) * 90deg);
    }
  }

  .heat {
    --v: attr(data-value type(<number>), 0);
    --cell: --scale(var(--v), 30, 90, var(--tbl-cold), var(--tbl-hot));
    background: var(--cell);
    color: --on(var(--cell));
    font-weight: 600;
    text-align: center;
  }
}
