@layer tokens {
  :root {
    --color-swatch-1: #e5f2ff;
    --color-swatch-2: #0f172a;
    --color-swatch-3: #581c87;
    --color-swatch-4: #2563eb;
    --color-swatch-5: #0d9488;
    --color-swatch-6: #eab308;
    --color-swatch-7: #f8fafc;

    /* Local to this demo's card redesign — the rest of the lab keeps the
       shared dashed "experiment box" look in _shared.css, only this page's
       .stage is restyled as a polished panel (see the .stage override below). */
    --radius-card: 1.25rem;
    --radius-control: 1rem;
  }
}

@layer overrides {
  .stage:has(#brand-1:checked) { --brand: var(--color-swatch-1); --brand-hex: "#e5f2ff"; }
  .stage:has(#brand-2:checked) { --brand: var(--color-swatch-2); --brand-hex: "#0f172a"; }
  .stage:has(#brand-3:checked) { --brand: var(--color-swatch-3); --brand-hex: "#581c87"; }
  .stage:has(#brand-4:checked) { --brand: var(--color-swatch-4); --brand-hex: "#2563eb"; }
  .stage:has(#brand-5:checked) { --brand: var(--color-swatch-5); --brand-hex: "#0d9488"; }
  .stage:has(#brand-6:checked) { --brand: var(--color-swatch-6); --brand-hex: "#eab308"; }
  .stage:has(#brand-7:checked) { --brand: var(--color-swatch-7); --brand-hex: "#f8fafc"; }

  /* Swap the lab's shared dashed debug-box look for a polished panel on this
     page only — later layer wins over _shared.css's .stage without needing
     extra specificity. */
  .stage {
    padding: var(--space-11) var(--space-10);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
    background: var(--stage-bg);
    box-shadow:
      0 24px 48px -24px var(--shadow-color),
      0 4px 12px -6px var(--shadow-color);
  }
}

@layer components {
  .section-label {
    margin: 0 0 var(--space-6);
    font-size: .8rem;
    font-weight: 600;
    color: var(--color-muted);
  }

  .swatch-row {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-6);
    padding: 0;
    margin: 0 0 var(--space-11);
    border: 0;
  }

  .brand-radio {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;

    @media (prefers-reduced-motion: no-preference) {
      &:checked + .brand-swatch {
        transform: scale(1.08);
      }
    }

    /* Selection is shown with a checkmark badge (below), not this ring alone —
       :checked is still native, so screen readers get the state either way.
       Specificity (0,3,1): the radio-hack's class+:checked+class chain is the
       selector's minimum necessary form for a no-JS "if this input is checked,
       style that sibling" rule — can't be reduced without extra markup or JS. */
    &:checked + .brand-swatch::after {
      --badge-size: 1.125rem;
      --badge-offset: -.2rem;

      content: "\2713";
      position: absolute;
      inset-block-end: var(--badge-offset);
      inset-inline-end: var(--badge-offset);
      inline-size: var(--badge-size);
      block-size: var(--badge-size);
      display: grid;
      place-items: center;
      border-radius: 50%;
      border: 2px solid var(--stage-bg);
      background: var(--color-accent);
      color: contrast-color(var(--color-accent));
      font-size: .65rem;
      font-weight: 700;
      line-height: 1;
    }

    &:focus-visible + .brand-swatch {
      outline: 2px solid var(--color-accent);
      outline-offset: 3px;
    }
  }

  .brand-swatch {
    --size: 2.75rem;

    position: relative;
    inline-size: var(--size);
    block-size: var(--size);
    border-radius: 50%;
    border: 1px solid var(--color-border);
    cursor: pointer;
    transition: transform 150ms ease;

    &.brand-swatch--1 { background: var(--color-swatch-1); }
    &.brand-swatch--2 { background: var(--color-swatch-2); }
    &.brand-swatch--3 { background: var(--color-swatch-3); }
    &.brand-swatch--4 { background: var(--color-swatch-4); }
    &.brand-swatch--5 { background: var(--color-swatch-5); }
    &.brand-swatch--6 { background: var(--color-swatch-6); }
    &.brand-swatch--7 { background: var(--color-swatch-7); }
  }

  .palette {
    --col-min: 7rem;

    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(var(--col-min), 1fr));
    gap: var(--space-6);
  }

  .palette-swatch {
    --size: 6rem;

    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    gap: .15rem;
    block-size: var(--size);
    padding: var(--space-6);
    border-radius: var(--radius-control);
    border: 1px solid var(--color-border);
    font-weight: 600;
    font-size: .8rem;
    background: var(--swatch-color);
    color: contrast-color(var(--swatch-color));

    .palette-value {
      font-family: ui-monospace, monospace;
      font-size: .7rem;
      font-weight: 400;
      opacity: .75;
    }

    &.palette-swatch--base  { --swatch-color: var(--brand); }
    &.palette-swatch--light { --swatch-color: color-mix(in srgb, var(--brand), #fff 30%); }
    &.palette-swatch--dark  { --swatch-color: color-mix(in srgb, var(--brand), #000 30%); }
    &.palette-swatch--r     { --swatch-color: rgb(from var(--brand) r 0 0); }
    &.palette-swatch--g     { --swatch-color: rgb(from var(--brand) 0 g 0); }
    &.palette-swatch--b     { --swatch-color: rgb(from var(--brand) 0 0 b); }
  }

  .code-block {
    margin: var(--space-11) 0 0;
    padding: var(--space-8) var(--space-9);
    border-radius: var(--radius-control);
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-muted);
    font-family: ui-monospace, monospace;
    font-size: .85rem;
    line-height: 1.6;
    overflow-x: auto;

    .hex-echo {
      color: var(--color-accent);
    }
  }

  .hex-echo::before {
    content: var(--brand-hex);
  }
}
