@scope (chart-legend-ui) {
  :where(:scope) {
    /* ── Layout ── */
    --chart-legend-gap-default:         var(--a-space-2);
    --chart-legend-row-gap-default:     var(--a-space-1);
    --chart-legend-py-default:          var(--a-space-1);
    --chart-legend-px-default:          0;

    /* ── Swatch ── */
    --chart-legend-swatch-size-default: var(--a-space-2-5);
    --chart-legend-line-w-default:      2px;

    /* ── Typography ── */
    --chart-legend-font-size-default:   var(--a-ui-tiny);
    --chart-legend-fg-default:          var(--a-fg-subtle);
    --chart-legend-fg-inactive-default: var(--a-fg-muted);

    /* ── Interaction ── */
    --chart-legend-row-radius-default:  var(--a-radius-sm);
    --chart-legend-row-hover-default:   var(--a-canvas-2);
    --chart-legend-row-fg-hover-default: var(--a-fg);

    /* ── Focus ── */
    --chart-legend-focus-ring-default:  var(--a-focus-ring);
  }

  :scope {
    box-sizing: border-box;
    display: flex;
    flex-wrap: wrap;
    gap: var(--chart-legend-gap, var(--chart-legend-gap-default));
    padding: var(--chart-legend-py, var(--chart-legend-py-default)) var(--chart-legend-px, var(--chart-legend-px-default));
    font-size: var(--chart-legend-font-size, var(--chart-legend-font-size-default));
    color: var(--chart-legend-fg, var(--chart-legend-fg-default));
  }

  /* Position variants — drive flex-direction. DOM placement still wins for
     actual layout; these tune the internal row direction. */
  :scope[position="top"],
  :scope[position="bottom"] {
    flex-direction: row;
    justify-content: center;
  }

  :scope[position="left"],
  :scope[position="right"] {
    flex-direction: column;
    align-items: flex-start;
  }

  /* Rows are <badge-ui> chips. We override badge-ui's default tokens
     to give them a quieter, click-to-toggle appearance — transparent
     bg by default (so they read as inline text not chips), highlighted
     on hover when interactive. The pill chrome (radius, padding,
     font-size, line-height) comes from badge-ui itself. */
  badge-ui[data-row] {
    --badge-bg: transparent;
    --badge-fg: var(--chart-legend-fg, var(--chart-legend-fg-default));
    --badge-radius: var(--chart-legend-row-radius, var(--chart-legend-row-radius-default));
    --badge-px: var(--a-space-1);
    --badge-py: var(--chart-legend-py, var(--chart-legend-py-default));
    --badge-gap: var(--chart-legend-row-gap, var(--chart-legend-row-gap-default));
    --badge-font-size: var(--chart-legend-font-size, var(--chart-legend-font-size-default));
    cursor: pointer;
    transition: background var(--a-duration-fast) var(--a-easing),
                color var(--a-duration-fast) var(--a-easing);
  }

  /* Static rows — no interaction */
  :scope[static] badge-ui[data-row] {
    cursor: default;
  }

  badge-ui[data-row][role="button"]:hover {
    --badge-bg: var(--chart-legend-row-hover, var(--chart-legend-row-hover-default));
    --badge-fg: var(--chart-legend-row-fg-hover, var(--chart-legend-row-fg-hover-default));
  }

  badge-ui[data-row][role="button"]:focus-visible {
    outline: none;
    box-shadow: var(--chart-legend-focus-ring, var(--chart-legend-focus-ring-default));
  }

  /* Inactive (toggled-off) rows — dim the swatch + fade label. Drives
     badge-ui's --badge-fg so the label fades alongside the swatch. */
  badge-ui[data-row]:not([data-active]) {
    --badge-fg: var(--chart-legend-fg-inactive, var(--chart-legend-fg-inactive-default));
  }
  badge-ui[data-row]:not([data-active]) swatch-ui {
    opacity: 0.4;
  }

  /* Swatch is composed via <swatch-ui shape="…">; per-shape styling
     lives in swatch.css. The legend just hands its [shape] attr through
     and writes --swatch-color inline. Pre-2026-05-01 this file owned a
     `<span data-swatch>` element + dot/square/line/dashed CSS rules,
     which collided with the docs-shell's `[data-swatch]` token-demo
     rule (cascade leak). Composing the primitive moves the styling to
     one place. */
}
