@scope (code-ui) {
  :where(:scope) {
    /* ── Layout ── */
    --code-px-default:          var(--a-space-3);
    --code-py-default:          var(--a-space-2);
    --code-radius-default:      var(--a-radius-md);
    --code-radius-sm-default:   var(--a-radius-sm);
    --code-copy-px-default:     var(--a-space-1);
    --code-copy-py-default:     var(--a-space-0-5);
    --code-lint-marker-w-default: 3px; /* Vertical lint-marker rule; intrinsic measurement; no --a-space-* equivalent at this width. */

    /* ── Colors ── */
    --code-bg-default:          var(--a-bg);
    --code-fg-default:          var(--a-fg);
    --code-border-default:      var(--a-border-subtle);
    --code-header-bg-default:   var(--a-bg-scrim);
    --code-header-fg-default:   var(--a-fg-muted);
    --code-copy-hover-bg-default: var(--a-bg-muted);
    --code-copy-hover-fg-default: var(--a-fg);

    /* ── Typography ── */
    --code-font-default:        var(--a-font-family-code);
    --code-font-size-default:   var(--a-ui-sm);
    --code-header-font-default: var(--a-ui-sm);

    /* ── Transition ── */
    --code-duration-default:    var(--a-duration-fast);
    --code-easing-default:      var(--a-easing);

    /* ── Editor chrome (CodeMirror 6) ──
       Active only when `[language]` triggers the CM mount. Still safe
       to emit in read-only mode; unused until `.cm-editor` is in the
       DOM. See docs/specs/code-editor.md §6. */
    --code-gutter-bg-default:        var(--a-bg-subtle);
    --code-gutter-fg-default:        var(--a-fg-muted);
    /* Transparency-mixing in oklab — Safari < 18 has an OKLCH bug where
       the transparent operand's hue resolves to 0, red-shifting the mix.
       oklab is hue-agnostic and visually identical in fixed engines. */
    --code-active-line-bg-default:   color-mix(in oklab, var(--a-accent-muted) 40%, transparent);
    --code-selection-bg-default:     color-mix(in oklab, var(--a-accent-muted) 60%, transparent);
    --code-selection-match-default:  color-mix(in oklab, var(--a-accent-muted) 30%, transparent);
    --code-cursor-default:           var(--a-accent-strong);
    --code-focus-ring-default:       var(--a-focus-ring);

    /* ── Syntax highlight tokens ──
       Map Lezer/CodeMirror highlight tag names to AdiaUI semantic
       tokens. Overridable per-instance by setting `--code-tok-*` on
       any ancestor. See docs/specs/code-editor.md §6.3 for the tag →
       class → role map. */
    --code-tok-comment-default:      var(--a-fg-subtle);
    --code-tok-keyword-default:      var(--a-accent-strong);
    --code-tok-string-default:       var(--a-success-strong);
    --code-tok-number-default:       var(--a-info-strong);
    --code-tok-boolean-default:      var(--a-info-strong);
    --code-tok-operator-default:     var(--a-fg);
    --code-tok-punctuation-default:  var(--a-fg-muted);
    --code-tok-function-default:     var(--a-brand-strong);
    --code-tok-variable-default:     var(--a-fg);
    --code-tok-type-default:         var(--a-warning-strong);
    --code-tok-property-default:     var(--a-fg);
    --code-tok-tag-default:          var(--a-accent-strong);
    --code-tok-attribute-default:    var(--a-warning-strong);
    --code-tok-url-default:          var(--a-info-strong);
    --code-tok-invalid-default:      var(--a-danger-strong);

    /* ── Lint markers (Phase 3 — editable JSON) ──
       `@codemirror/lint` paints gutter markers + diagnostic tooltips
       + inline underlines. Only fires in editable mode with a linter
       attached; read-only instances never see these selectors. */
    --code-lint-gutter-bg-default:   var(--a-bg-subtle);
    --code-lint-error-default:       var(--a-danger-strong);
    --code-lint-warning-default:     var(--a-warning-strong);
    --code-lint-info-default:        var(--a-info-strong);
    --code-lint-tooltip-bg-default:  var(--a-bg-strong);  /* §230-bundle (v0.5.9): elevated panel surface. Was orphaned --a-surface-floating. */
    --code-lint-tooltip-fg-default:  var(--a-fg);
    --code-lint-tooltip-border-default: var(--a-border-subtle);
  }

  /* ── Block (default) ── */
  :scope {
    box-sizing: border-box;
    display: block;
    border: 1px solid var(--code-border, var(--code-border-default));
    border-radius: var(--code-radius, var(--code-radius-default));
    overflow: hidden;
    background: var(--code-bg, var(--code-bg-default));
    color: var(--code-fg, var(--code-fg-default));
    font-family: var(--code-font, var(--code-font-default));
    font-size: var(--code-font-size, var(--code-font-size-default));
  }

  /* Header — chrome band above the code block */
  & > header {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    min-height: var(--a-chrome-pane-header-height);
    padding: var(--code-py, var(--code-py-default)) var(--code-px, var(--code-px-default));
    background: var(--code-header-bg, var(--code-header-bg-default));
    border-bottom: 1px solid var(--code-border, var(--code-border-default));
    font-size: var(--code-header-font, var(--code-header-font-default));
    color: var(--code-header-fg, var(--code-header-fg-default));
  }

  & > header [slot="label"] {
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }

  /* Copy button — ghost style */
  & > header [slot="copy"] {
    all: unset;
    cursor: pointer;
    font-size: var(--code-header-font, var(--code-header-font-default));
    color: var(--code-header-fg, var(--code-header-fg-default));
    padding: var(--code-copy-py, var(--code-copy-py-default)) var(--code-copy-px, var(--code-copy-px-default));
    border-radius: var(--code-radius-sm, var(--code-radius-sm-default));
    transition:
      background var(--code-duration, var(--code-duration-default)) var(--code-easing, var(--code-easing-default)),
      color var(--code-duration, var(--code-duration-default)) var(--code-easing, var(--code-easing-default));
  }
  & > header [slot="copy"]:hover {
    background: var(--code-copy-hover-bg, var(--code-copy-hover-bg-default));
    color: var(--code-copy-hover-fg, var(--code-copy-hover-fg-default));
  }

  /* Pre > Code — reset any ambient page styling (margins, borders,
     radii, backgrounds) so the <code-ui> chrome is the single source
     of visual framing. */
  & > pre {
    margin: 0;
    border: none;
    border-radius: 0;
    background: transparent;
    padding: var(--code-py, var(--code-py-default)) var(--code-px, var(--code-px-default));
    overflow-x: auto;
    font-family: var(--code-font, var(--code-font-default));
    font-size: var(--code-font-size, var(--code-font-size-default));
    line-height: 1.5;
    scrollbar-width: none;
  }
  & > pre::-webkit-scrollbar {
    display: none;
  }

  & > pre > code {
    margin: 0;
    padding: 0;
    border: none;
    border-radius: 0;
    background: transparent;
    font-family: inherit;
    font-size: inherit;
    color: inherit;
  }

  /* ── Diff line states (static path) ──
     Active when the block uses `language="diff"` (auto-parse +/- prefix)
     or carries `data-line-states="..."` (explicit per-line). Each line is
     a `[data-line-state]` row whose bg picks up the state token. The
     CodeMirror path doesn't use these markers; line decorations there go
     through CM extensions instead. */
  & > pre > code[data-line-state-mode] {
    display: block;
  }
  & > pre > code[data-line-state-mode] > [data-line-state] {
    display: grid;
    grid-template-columns: 1fr;
    /* Bleed the row tint to the pre's padding edges so it reads as a
       full-width line, not a gap-margined pill. */
    margin-inline: calc(-1 * var(--code-px, var(--code-px-default)));
    padding-inline: var(--code-px, var(--code-px-default));
  }
  & > pre > code[data-line-numbers] > [data-line-state] {
    grid-template-columns: auto 1fr;
    column-gap: var(--a-space-3);
  }
  & > pre > code[data-line-state-mode] [data-line-num] {
    color: var(--a-fg-subtle);
    text-align: end;
    user-select: none;
    min-width: 1.5ch;
  }
  & > pre > code[data-line-state-mode] [data-line-body] {
    white-space: pre;
  }
  /* Empty lines still need height so the diff column counts line up. */
  & > pre > code[data-line-state-mode] [data-line-body]:empty::before {
    content: " ";
  }
  & > pre > code [data-line-state="added"] {
    background: var(--a-success-muted);
  }
  & > pre > code [data-line-state="removed"] {
    background: var(--a-danger-muted);
  }

  /* Footer — optional chrome band below the code block
     (line counts, byte size, language family, etc.) */
  & > footer {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    min-height: var(--a-chrome-pane-footer-height);
    padding: var(--code-py, var(--code-py-default)) var(--code-px, var(--code-px-default));
    background: var(--code-header-bg, var(--code-header-bg-default));
    border-top: 1px solid var(--code-border, var(--code-border-default));
    font-size: var(--code-header-font, var(--code-header-font-default));
    color: var(--code-header-fg, var(--code-header-fg-default));
  }

  /* ── Inline ── */
  :scope[inline] {
    display: inline;
    padding: 0.1em 0.4em;
    background: var(--code-bg, var(--code-bg-default));
    border: none;
    border-radius: var(--code-radius-sm, var(--code-radius-sm-default));
    font-family: var(--code-font, var(--code-font-default));
    font-size: 0.9em;
    overflow: visible;
  }

  /* ── Bare mode — no chrome ──
     Strips border, background, radius, and padding. Header is hidden
     via #stampBlock (it's never added in bare). Used by the A2UI Editor
     and any pane-level context where <code-ui> is framed by a parent
     surface and should not carry its own. Height flows from the parent
     container, so CM's internal scroll handles overflow. */
  :scope[bare] {
    border: none;
    border-radius: 0;
    background: transparent;
    overflow: visible;
    height: 100%;
  }
  :scope[bare] > pre {
    padding: 0;
    height: 100%;
  }
  :scope[bare] > [data-cm-mount] {
    height: 100%;
  }
  :scope[bare] .cm-editor {
    height: 100%;
  }
  :scope[bare] .cm-scroller {
    padding: 0;
  }
  :scope[bare] .cm-gutters {
    background: transparent;
  }

  /* ── Form states (editable-only surfaces) ──
     `[disabled]` — visually muted, pointer-events disabled so the user
     can't click into the editor. CM's readonly compartment handles the
     actual edit-blocking; these rules are the visual cue.
     `[readonly]` — muted but clickable (user can still select + copy).
     `[aria-invalid="true"]` — error ring for form validation failures. */
  :scope[disabled] {
    opacity: 0.6;
    cursor: not-allowed;
  }
  :scope[disabled] .cm-editor {
    pointer-events: none;
  }
  :scope[readonly] .cm-editor {
    background: var(--a-bg-subtle);
  }
  :scope[aria-invalid="true"] {
    border-color: var(--a-danger-strong);
  }
  :scope[aria-invalid="true"] .cm-focused {
    outline: 2px solid var(--a-danger-strong);
    outline-offset: -1px;
  }

  /* ── CodeMirror mount ──
     `<code-ui>` inserts `<div data-cm-mount>` in place of `<pre><code>`
     when `[language]` triggers the CM lazy-load. Styles below apply to
     the CodeMirror DOM that CM renders inside that mount.

     Selector discipline: `.cm-*` classes are CodeMirror's; `.tok-*`
     classes come from our `adiaHighlightStyle` (see core/_cm-theme.js).
     CM's CSS-in-JS stylesheets load first + at lower specificity, so
     these rules override without !important. */

  & > [data-cm-mount] {
    display: block;
    overflow: hidden;
  }

  :scope .cm-editor {
    background: transparent;
    color: var(--code-fg, var(--code-fg-default));
    font-family: var(--code-font, var(--code-font-default));
    font-size: var(--code-font-size, var(--code-font-size-default));
    line-height: 1.5;
  }

  :scope .cm-scroller {
    font-family: inherit;
    line-height: inherit;
    /* No padding — the gutter band ([data-cm-mount] > .cm-gutters has its
       own --code-gutter-bg) reads as the chrome it is when pinned to the
       left edge, and the active-line highlight and selection block get
       full bleed. The header / footer bands frame the editor; this content
       region should not double-inset. */
    padding: 0;
  }

  :scope .cm-content {
    padding: var(--code-py, var(--code-py-default)) var(--code-px, var(--code-px-default));
    caret-color: var(--code-cursor, var(--code-cursor-default));
  }

  :scope .cm-editor.cm-focused {
    outline: 2px solid var(--code-focus-ring, var(--code-focus-ring-default));
    outline-offset: -2px;
  }

  :scope .cm-gutters {
    background: var(--code-gutter-bg, var(--code-gutter-bg-default));
    color: var(--code-gutter-fg, var(--code-gutter-fg-default));
    border-right: 1px solid var(--code-border, var(--code-border-default));
  }

  :scope .cm-activeLine {
    background: var(--code-active-line-bg, var(--code-active-line-bg-default));
  }
  :scope .cm-activeLineGutter {
    background: var(--code-active-line-bg, var(--code-active-line-bg-default));
    color: var(--code-fg, var(--code-fg-default));
  }

  :scope .cm-selectionBackground,
  :scope ::selection {
    background: var(--code-selection-bg, var(--code-selection-bg-default));
  }

  :scope .cm-selectionMatch {
    background: var(--code-selection-match, var(--code-selection-match-default));
  }

  :scope .cm-cursor {
    border-left-color: var(--code-cursor, var(--code-cursor-default));
  }

  /* Syntax highlight — one rule per token role (see core/_cm-theme.js) */
  :scope .tok-comment     { color: var(--code-tok-comment, var(--code-tok-comment-default)); font-style: italic; }
  :scope .tok-keyword     { color: var(--code-tok-keyword, var(--code-tok-keyword-default)); font-weight: 500; }
  :scope .tok-string      { color: var(--code-tok-string, var(--code-tok-string-default)); }
  :scope .tok-number      { color: var(--code-tok-number, var(--code-tok-number-default)); }
  :scope .tok-boolean     { color: var(--code-tok-boolean, var(--code-tok-boolean-default)); }
  :scope .tok-operator    { color: var(--code-tok-operator, var(--code-tok-operator-default)); }
  :scope .tok-punctuation { color: var(--code-tok-punctuation, var(--code-tok-punctuation-default)); }
  :scope .tok-function    { color: var(--code-tok-function, var(--code-tok-function-default)); }
  :scope .tok-variable    { color: var(--code-tok-variable, var(--code-tok-variable-default)); }
  :scope .tok-type        { color: var(--code-tok-type, var(--code-tok-type-default)); font-weight: 500; }
  :scope .tok-property    { color: var(--code-tok-property, var(--code-tok-property-default)); }
  :scope .tok-tag         { color: var(--code-tok-tag, var(--code-tok-tag-default)); }
  :scope .tok-attribute   { color: var(--code-tok-attribute, var(--code-tok-attribute-default)); }
  :scope .tok-url         { color: var(--code-tok-url, var(--code-tok-url-default)); text-decoration: underline; }
  :scope .tok-invalid     { color: var(--code-tok-invalid, var(--code-tok-invalid-default)); text-decoration: wavy underline; }

  /* ── Lint gutter + diagnostics ──
     Styles the `@codemirror/lint` markers in the gutter, the inline
     wavy underline on the diagnostic range, and the tooltip panel. */
  :scope .cm-gutter-lint {
    background: var(--code-lint-gutter-bg, var(--code-lint-gutter-bg-default));
    width: 1em;
  }
  :scope .cm-lint-marker {
    width: 0.7em;
    height: 0.7em;
    margin: 0.15em 0.15em;
  }
  :scope .cm-lint-marker-error   { color: var(--code-lint-error, var(--code-lint-error-default)); }
  :scope .cm-lint-marker-warning { color: var(--code-lint-warning, var(--code-lint-warning-default)); }
  :scope .cm-lint-marker-info    { color: var(--code-lint-info, var(--code-lint-info-default)); }

  :scope .cm-lintRange-error   { background: none; text-decoration: wavy underline var(--code-lint-error, var(--code-lint-error-default)); }
  :scope .cm-lintRange-warning { background: none; text-decoration: wavy underline var(--code-lint-warning, var(--code-lint-warning-default)); }
  :scope .cm-lintRange-info    { background: none; text-decoration: wavy underline var(--code-lint-info, var(--code-lint-info-default)); }

  :scope .cm-tooltip.cm-tooltip-lint {
    background: var(--code-lint-tooltip-bg, var(--code-lint-tooltip-bg-default));
    color: var(--code-lint-tooltip-fg, var(--code-lint-tooltip-fg-default));
    border: 1px solid var(--code-lint-tooltip-border, var(--code-lint-tooltip-border-default));
    border-radius: var(--code-radius-sm, var(--code-radius-sm-default));
    padding: var(--a-space-1) var(--a-space-2);
    font-size: var(--a-ui-sm);
    font-family: var(--a-font-family);
    max-inline-size: 40ch;
  }
  :scope .cm-diagnostic {
    padding: 0;
    border-inline-start: var(--code-lint-marker-w, var(--code-lint-marker-w-default)) solid transparent;
    padding-inline-start: var(--a-space-2);
  }
  :scope .cm-diagnostic-error   { border-inline-start-color: var(--code-lint-error, var(--code-lint-error-default)); }
  :scope .cm-diagnostic-warning { border-inline-start-color: var(--code-lint-warning, var(--code-lint-warning-default)); }
  :scope .cm-diagnostic-info    { border-inline-start-color: var(--code-lint-info, var(--code-lint-info-default)); }
  :scope .cm-diagnosticText { white-space: pre-wrap; }
}
