/* ============================================================================
   ROGUE AI — COMPONENT LAYER
   Standards CSS. Classes only, semantic tokens only, both grounds, no framework.
   Load order:  fonts.css  ->  tokens.css  ->  components.css

   Every rule lives in a cascade layer (TDR-075 S2). Every colour is a tier-2
   semantic token — there is no hex and no raw colour function in this file, so it
   is correct on the dark ground and the light ground without a single
   ground-specific rule. Ground is selected by the data-ground attribute; nothing
   here knows which one is active.

   No measured figure appears in this file, not even in a comment (R-1). Ratios
   and separation figures live in export/contrast-report.json and
   export/cvd-report.json and are read from there by any surface that displays
   them.
   ============================================================================ */

/* Layer order is declared before any layer is used, so it cannot depend on
   first appearance. Repeated from tokens.css deliberately: the declaration is
   idempotent, and either file must be able to load first. */
@layer tokens, base, components, utilities;

@layer base {

  *, *::before, *::after { box-sizing: border-box; }

  /* The canvas is painted on html as well as body. A host page or embedder that
     injects an UNLAYERED body rule outranks every layered rule in this system —
     that is how cascade layers work, and it is worth knowing rather than
     discovering. Painting html too means the ground survives that case. */
  html { -webkit-text-size-adjust: 100%; background: var(--color-page); color: var(--color-text); }

  body {
    margin: 0;
    background: var(--color-page);
    color: var(--color-text);
    font-family: var(--font-ui);
    font-size: var(--text-body);
    line-height: var(--leading-body);
  }

  h1, h2, h3, h4, h5, h6 { margin: 0; font-weight: var(--font-weight-semibold); line-height: var(--leading-snug); }
  h1 { font-size: var(--text-heading-lg); letter-spacing: var(--tracking-tight); }
  h2 { font-size: var(--text-heading); letter-spacing: var(--tracking-tight); }
  h3 { font-size: var(--text-heading-sm); }
  h4 { font-size: var(--text-body-lg); }
  p { margin: 0; text-wrap: pretty; }

  a { color: var(--color-text-link); text-decoration-thickness: 1px; text-underline-offset: 2px; }
  a:hover { color: var(--color-accent-solid-hover); }

  code, kbd, samp, pre { font-family: var(--font-mono); }
  table { border-collapse: collapse; }
  img, svg { max-width: 100%; }
  hr { border: 0; border-top: var(--rogue-border-hairline) solid var(--color-separator); margin: 0; }

  /* One focus treatment, applied once, inherited everywhere. Never removed
     without replacement. The ring token is measured against both grounds in
     contrast-report.json under 'focus-ring vs page'. */
  :where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
    outline: var(--rogue-border-rule) solid var(--color-focus-ring);
    outline-offset: var(--rogue-space-1);
    border-radius: var(--radius-control);
  }

  ::selection { background: var(--color-accent-surface); color: var(--color-text); }

}

@layer components {

  /* ---------------------------------------------------------------- button -- */
  .rg-button {
    --button-radius: var(--radius-control);
    --button-padding-block: var(--rogue-space-3);
    --button-padding-inline: var(--rogue-space-5);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--spacing-inline);
    padding: var(--button-padding-block) var(--button-padding-inline);
    border: var(--rogue-border-hairline) solid transparent;
    border-radius: var(--button-radius);
    font-family: inherit;
    font-size: var(--text-body);
    font-weight: var(--font-weight-medium);
    line-height: 1;
    text-decoration: none;
    cursor: pointer;
    transition: background-color var(--duration-enter) var(--ease-standard),
                border-color var(--duration-enter) var(--ease-standard),
                color var(--duration-enter) var(--ease-standard);
  }

  .rg-button--primary { background: var(--color-accent-solid); color: var(--color-text-on-solid); }
  .rg-button--primary:hover { background: var(--color-accent-solid-hover); }
  .rg-button--primary:active { background: var(--color-accent-solid); }

  .rg-button--secondary { background: var(--color-surface-raised); color: var(--color-text); border-color: var(--color-border-control); }
  .rg-button--secondary:hover { background: var(--color-surface-overlay); border-color: var(--color-border-strong); }

  .rg-button--ghost { background: transparent; color: var(--color-accent-text); }
  .rg-button--ghost:hover { background: var(--color-accent-surface); }

  .rg-button--danger { background: var(--color-danger-solid); color: var(--color-danger-on-solid); }
  .rg-button--danger:hover { filter: brightness(1.06); }

  .rg-button[aria-disabled="true"], .rg-button:disabled {
    background: var(--color-surface-raised);
    color: var(--color-text-disabled);
    border-color: var(--color-border);
    cursor: not-allowed;
  }

  .rg-button--sm { --button-padding-block: var(--rogue-space-2); --button-padding-inline: var(--rogue-space-4); font-size: var(--text-caption); }
  .rg-button--lg { --button-padding-block: var(--rogue-space-4); --button-padding-inline: var(--rogue-space-6); font-size: var(--text-body-lg); }

  .rg-icon-button {
    display: inline-flex; align-items: center; justify-content: center;
    width: var(--rogue-space-7); height: var(--rogue-space-7);
    padding: 0;
    background: transparent; color: var(--color-text-secondary);
    border: var(--rogue-border-hairline) solid transparent;
    border-radius: var(--radius-control);
    cursor: pointer;
    transition: background-color var(--duration-enter) var(--ease-standard);
  }
  .rg-icon-button:hover { background: var(--color-surface-raised); color: var(--color-text); }

  /* ------------------------------------------------------------------ link -- */
  .rg-link { color: var(--color-text-link); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; }
  .rg-link--standalone { display: inline-flex; align-items: center; gap: var(--rogue-space-2); font-weight: var(--font-weight-medium); text-decoration: none; }
  .rg-link--standalone:hover { text-decoration: underline; }
  .rg-link--external::after { content: "↗"; margin-inline-start: var(--rogue-space-2); font-size: .85em; }
  .rg-link:visited { color: var(--color-accent-text); }

  /* ----------------------------------------------------------------- field -- */
  .rg-field { display: flex; flex-direction: column; gap: var(--rogue-space-2); }
  .rg-field__label { font-size: var(--text-caption); font-weight: var(--font-weight-medium); color: var(--color-text); }
  .rg-field__required { color: var(--color-danger-text); margin-inline-start: var(--rogue-space-1); }
  .rg-field__hint { font-size: var(--text-caption); color: var(--color-text-muted); }
  .rg-field__error { display: flex; align-items: flex-start; gap: var(--rogue-space-2); font-size: var(--text-caption); color: var(--color-danger-text); }

  .rg-input, .rg-textarea, .rg-select {
    width: 100%;
    padding: var(--rogue-space-3) var(--rogue-space-4);
    background: var(--color-surface-sunken);
    color: var(--color-text);
    border: var(--rogue-border-hairline) solid var(--color-border-control);
    border-radius: var(--radius-control);
    font-family: inherit;
    font-size: var(--text-body);
    transition: border-color var(--duration-enter) var(--ease-standard);
  }
  .rg-input::placeholder, .rg-textarea::placeholder { color: var(--color-text-muted); }
  .rg-input:hover, .rg-textarea:hover, .rg-select:hover { border-color: var(--color-border-strong); }
  .rg-input:focus, .rg-textarea:focus, .rg-select:focus { border-color: var(--color-accent-solid); }
  .rg-textarea { min-height: calc(var(--rogue-space-10) + var(--rogue-space-5)); resize: vertical; }

  .rg-input[aria-invalid="true"], .rg-textarea[aria-invalid="true"], .rg-select[aria-invalid="true"] {
    border-color: var(--color-danger-solid);
    border-width: var(--rogue-border-rule);
  }
  .rg-input:disabled, .rg-textarea:disabled, .rg-select:disabled {
    background: var(--color-surface);
    color: var(--color-text-disabled);
    border-color: var(--color-border);
    cursor: not-allowed;
  }
  .rg-input[readonly] { background: var(--color-surface); border-style: dashed; }

  /* A written-answer area for printed questionnaires: ruled, not boxed, so a
     pen has somewhere to go and toner is not spent on a frame. */
  .rg-write-area {
    min-height: calc(var(--rogue-space-9) + var(--rogue-space-4));
    border: 0;
    border-bottom: var(--rogue-border-hairline) solid var(--color-border);
    background:
      repeating-linear-gradient(
        to bottom,
        transparent 0,
        transparent calc(1.5 * var(--text-doc-body) - 1px),
        var(--color-border-subtle) calc(1.5 * var(--text-doc-body) - 1px),
        var(--color-border-subtle) calc(1.5 * var(--text-doc-body))
      );
  }

  /* -------------------------------------------------- checkbox, radio, toggle */
  .rg-choice { display: flex; align-items: flex-start; gap: var(--spacing-inline); font-size: var(--text-body); }
  .rg-choice__control {
    flex: none;
    width: var(--rogue-space-5); height: var(--rogue-space-5);
    margin: 0;
    accent-color: var(--color-accent-solid);
  }
  .rg-choice--disabled { color: var(--color-text-disabled); }

  .rg-toggle { display: inline-flex; align-items: center; gap: var(--spacing-inline); }
  .rg-toggle__track {
    position: relative;
    width: calc(var(--rogue-space-7) + var(--rogue-space-3));
    height: var(--rogue-space-5);
    background: var(--color-surface-raised);
    border: var(--rogue-border-hairline) solid var(--color-border-control);
    border-radius: var(--radius-pill);
    transition: background-color var(--duration-enter) var(--ease-standard);
  }
  .rg-toggle__thumb {
    position: absolute;
    inset-block-start: 1px;
    inset-inline-start: 1px;
    width: calc(var(--rogue-space-5) - 4px);
    height: calc(var(--rogue-space-5) - 4px);
    background: var(--color-text-secondary);
    border-radius: var(--radius-pill);
    transition: translate var(--duration-enter) var(--ease-standard), background-color var(--duration-enter) var(--ease-standard);
  }
  .rg-toggle[data-state="on"] .rg-toggle__track { background: var(--color-accent-solid); border-color: var(--color-accent-solid); }
  .rg-toggle[data-state="on"] .rg-toggle__thumb { translate: var(--rogue-space-5) 0; background: var(--color-text-on-solid); }
  /* State is also spelled out in words beside the control — the track position
     alone is a single channel (R-6). */
  .rg-toggle__state { font-size: var(--text-caption); color: var(--color-text-secondary); font-variant-numeric: tabular-nums; }

  /* ----------------------------------------------------------------- table -- */
  .rg-table { width: 100%; font-size: var(--text-body); }
  .rg-table caption {
    caption-side: top;
    text-align: start;
    padding-block-end: var(--rogue-space-3);
    font-size: var(--text-caption);
    color: var(--color-text-muted);
  }
  .rg-table th, .rg-table td {
    padding: var(--rogue-space-3) var(--rogue-space-4);
    text-align: start;
    vertical-align: top;
    border-block-end: var(--rogue-border-hairline) solid var(--color-border-subtle);
  }
  .rg-table thead th {
    font-weight: var(--font-weight-medium);
    color: var(--color-text-secondary);
    border-block-end: var(--rogue-border-hairline) solid var(--color-border);
    white-space: nowrap;
  }
  .rg-table tbody tr:last-child th, .rg-table tbody tr:last-child td { border-block-end: 0; }

  .rg-table--dense th, .rg-table--dense td { padding: var(--rogue-space-2) var(--rogue-space-3); font-size: var(--text-caption); }
  .rg-table--zebra tbody tr:nth-child(even) { background: var(--color-surface-sunken); }
  .rg-table--ruled th, .rg-table--ruled td { border-inline-end: var(--rogue-border-hairline) solid var(--color-border-subtle); }
  .rg-table--ruled th:last-child, .rg-table--ruled td:last-child { border-inline-end: 0; }

  .rg-table__num { text-align: end; font-variant-numeric: tabular-nums; white-space: nowrap; }
  .rg-table__group { background: var(--color-surface-sunken); font-weight: var(--font-weight-medium); }

  .rg-table tfoot th, .rg-table tfoot td {
    border-block-start: var(--rogue-border-rule) solid var(--color-border-strong);
    border-block-end: 0;
    font-weight: var(--font-weight-semibold);
  }

  .rg-table--sticky thead th { position: sticky; inset-block-start: 0; background: var(--color-surface); z-index: 1; }

  .rg-table__sort { display: inline-flex; align-items: center; gap: var(--rogue-space-2); background: none; border: 0; padding: 0; font: inherit; color: inherit; cursor: pointer; }
  .rg-table__sort::after { content: "↕"; color: var(--color-text-muted); }
  .rg-table__sort[aria-sort="ascending"]::after { content: "↑"; color: var(--color-accent-text); }
  .rg-table__sort[aria-sort="descending"]::after { content: "↓"; color: var(--color-accent-text); }

  /* Wide content scrolls in its own container; the page body never scrolls
     sideways. */
  .rg-scroll-x { overflow-x: auto; border: var(--rogue-border-hairline) solid var(--color-border); border-radius: var(--radius-card); }
  .rg-scroll-x > .rg-table { min-width: 44rem; }

  /* --------------------------------------------------------------- callout -- */
  .rg-callout {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: var(--spacing-inline) var(--spacing-inline);
    padding: var(--rogue-space-5) var(--rogue-space-5);
    border: var(--rogue-border-hairline) solid var(--color-border);
    border-inline-start-width: var(--rogue-border-heavy);
    border-radius: var(--radius-control);
    background: var(--color-surface);
  }
  .rg-callout__icon { font-size: var(--text-body); line-height: var(--leading-snug); }
  .rg-callout__label { font-size: var(--text-caption); font-weight: var(--font-weight-semibold); letter-spacing: var(--tracking-wide); text-transform: uppercase; }
  .rg-callout__body { grid-column: 2; font-size: var(--text-body); color: var(--color-text-secondary); }

  .rg-callout--note { background: var(--color-info-surface); border-color: var(--color-info-border); border-inline-start-color: var(--color-info-solid); }
  .rg-callout--note .rg-callout__icon, .rg-callout--note .rg-callout__label { color: var(--color-info-text); }
  .rg-callout--warning { background: var(--color-warning-surface); border-color: var(--color-warning-border); border-inline-start-color: var(--color-warning-solid); }
  .rg-callout--warning .rg-callout__icon, .rg-callout--warning .rg-callout__label { color: var(--color-warning-text); }
  .rg-callout--critical { background: var(--color-danger-surface); border-color: var(--color-danger-border); border-inline-start-color: var(--color-danger-solid); }
  .rg-callout--critical .rg-callout__icon, .rg-callout--critical .rg-callout__label { color: var(--color-danger-text); }
  .rg-callout--success { background: var(--color-success-surface); border-color: var(--color-success-border); border-inline-start-color: var(--color-success-solid); }
  .rg-callout--success .rg-callout__icon, .rg-callout--success .rg-callout__label { color: var(--color-success-text); }

  /* ------------------------------------------------------- badge / status --- */
  /* Anatomy is fixed: glyph + label + colour, all three, always. The label
     wears a text token; the glyph carries identity. A bare coloured dot is a
     defect — see cvd-report.json separationStandards. */
  .rg-badge {
    display: inline-flex;
    align-items: center;
    gap: var(--rogue-space-2);
    padding: var(--rogue-space-1) var(--rogue-space-3);
    border: var(--rogue-border-hairline) solid var(--color-border);
    border-radius: var(--radius-pill);
    background: var(--color-surface-raised);
    color: var(--color-text);
    font-size: var(--text-caption);
    font-weight: var(--font-weight-medium);
    white-space: nowrap;
  }
  .rg-badge__glyph { font-size: .85em; line-height: 1; }
  .rg-badge--good { background: var(--color-success-surface); border-color: var(--color-success-border); }
  .rg-badge--good .rg-badge__glyph { color: var(--color-success-solid); }
  .rg-badge--watch { background: var(--color-warning-surface); border-color: var(--color-warning-border); }
  .rg-badge--watch .rg-badge__glyph { color: var(--color-warning-solid); }
  .rg-badge--off { background: var(--color-danger-surface); border-color: var(--color-danger-border); }
  .rg-badge--off .rg-badge__glyph { color: var(--color-danger-solid); }
  .rg-badge--info { background: var(--color-info-surface); border-color: var(--color-info-border); }
  .rg-badge--info .rg-badge__glyph { color: var(--color-info-solid); }
  .rg-badge--error { background: var(--color-surface); border-color: var(--color-border); color: var(--color-text-muted); }
  .rg-badge--error .rg-badge__glyph { color: var(--color-text-muted); }
  .rg-badge--solid { background: var(--color-accent-solid); border-color: var(--color-accent-solid); color: var(--color-text-on-solid); }

  /* An unmeasurable value renders ERROR in the value cell itself. Never blank,
     never a dash, never zero. */
  .rg-value-error { color: var(--color-danger-text); font-weight: var(--font-weight-medium); font-family: var(--font-mono); font-size: .9em; }

  .rg-tag {
    display: inline-flex; align-items: center; gap: var(--rogue-space-2);
    padding: var(--rogue-space-1) var(--rogue-space-3);
    background: var(--color-surface-sunken);
    border: var(--rogue-border-hairline) solid var(--color-border-subtle);
    border-radius: var(--radius-control);
    font-size: var(--text-caption);
    color: var(--color-text-secondary);
    font-family: var(--font-mono);
  }

  /* ------------------------------------------------------------------ card -- */
  .rg-card {
    background: var(--color-surface);
    border: var(--rogue-border-hairline) solid var(--color-border);
    border-radius: var(--radius-card);
    padding: var(--rogue-space-6);
  }
  .rg-card--raised { background: var(--color-surface-raised); box-shadow: var(--shadow-raised); }
  .rg-card--flat { background: transparent; border-color: var(--color-border-subtle); }
  .rg-card--interactive { cursor: pointer; transition: border-color var(--duration-enter) var(--ease-standard), background-color var(--duration-enter) var(--ease-standard); }
  .rg-card--interactive:hover { border-color: var(--color-accent-border); background: var(--color-surface-raised); }
  .rg-card__header {
    display: flex; align-items: baseline; justify-content: space-between; gap: var(--spacing-gutter);
    margin-block-end: var(--rogue-space-4);
    padding-block-end: var(--rogue-space-3);
    border-block-end: var(--rogue-border-hairline) solid var(--color-border-subtle);
  }
  .rg-card__title { font-size: var(--text-body-lg); font-weight: var(--font-weight-semibold); }
  .rg-card__footer {
    margin-block-start: var(--rogue-space-5);
    padding-block-start: var(--rogue-space-3);
    border-block-start: var(--rogue-border-hairline) solid var(--color-border-subtle);
    font-size: var(--text-caption);
    color: var(--color-text-muted);
  }

  /* ------------------------------------------------- tabs, breadcrumb, pager */
  .rg-tabs { display: flex; gap: var(--rogue-space-1); border-block-end: var(--rogue-border-hairline) solid var(--color-border); }
  .rg-tab {
    padding: var(--rogue-space-3) var(--rogue-space-4);
    background: none; border: 0;
    border-block-end: var(--rogue-border-rule) solid transparent;
    margin-block-end: -1px;
    font: inherit; font-size: var(--text-body);
    color: var(--color-text-secondary);
    cursor: pointer;
  }
  .rg-tab:hover { color: var(--color-text); }
  .rg-tab[aria-selected="true"] { color: var(--color-text); border-block-end-color: var(--color-accent-solid); font-weight: var(--font-weight-medium); }
  .rg-tab[aria-disabled="true"] { color: var(--color-text-disabled); cursor: not-allowed; }

  .rg-breadcrumb { display: flex; flex-wrap: wrap; align-items: center; gap: var(--rogue-space-2); font-size: var(--text-caption); color: var(--color-text-muted); }
  .rg-breadcrumb li { display: flex; align-items: center; gap: var(--rogue-space-2); }
  .rg-breadcrumb li + li::before { content: "/"; color: var(--color-border-strong); }
  .rg-breadcrumb, .rg-pagination { list-style: none; margin: 0; padding: 0; }

  .rg-pagination { display: flex; gap: var(--rogue-space-2); align-items: center; }
  .rg-pagination__item {
    min-width: var(--rogue-space-7);
    padding: var(--rogue-space-2) var(--rogue-space-3);
    border: var(--rogue-border-hairline) solid var(--color-border);
    border-radius: var(--radius-control);
    background: var(--color-surface);
    color: var(--color-text-secondary);
    font-size: var(--text-caption);
    text-align: center;
    text-decoration: none;
    font-variant-numeric: tabular-nums;
  }
  .rg-pagination__item[aria-current="page"] { background: var(--color-accent-solid); border-color: var(--color-accent-solid); color: var(--color-text-on-solid); font-weight: var(--font-weight-medium); }
  .rg-pagination__item[aria-disabled="true"] { color: var(--color-text-disabled); }

  /* ---------------------------------------------------------- modal, popover */
  .rg-modal-scrim { position: fixed; inset: 0; background: var(--color-scrim); display: grid; place-items: center; padding: var(--spacing-gutter); }
  .rg-modal {
    width: min(34rem, 100%);
    background: var(--color-surface-overlay);
    border: var(--rogue-border-hairline) solid var(--color-border);
    border-radius: var(--radius-card);
    box-shadow: var(--shadow-overlay);
  }
  .rg-modal__header {
    display: flex; align-items: flex-start; justify-content: space-between; gap: var(--spacing-gutter);
    padding: var(--rogue-space-5) var(--rogue-space-6);
    border-block-end: var(--rogue-border-hairline) solid var(--color-border-subtle);
  }
  .rg-modal__body { padding: var(--rogue-space-6); color: var(--color-text-secondary); }
  .rg-modal__footer {
    display: flex; justify-content: flex-end; gap: var(--spacing-inline);
    padding: var(--rogue-space-5) var(--rogue-space-6);
    border-block-start: var(--rogue-border-hairline) solid var(--color-border-subtle);
  }

  .rg-popover {
    position: relative;
    max-width: 20rem;
    padding: var(--rogue-space-4);
    background: var(--color-surface-overlay);
    border: var(--rogue-border-hairline) solid var(--color-border);
    border-radius: var(--radius-control);
    box-shadow: var(--shadow-overlay);
    font-size: var(--text-caption);
    color: var(--color-text-secondary);
  }
  .rg-popover::after {
    content: "";
    position: absolute;
    inset-block-start: 100%;
    inset-inline-start: var(--rogue-space-6);
    border: var(--rogue-space-2) solid transparent;
    border-block-start-color: var(--color-border);
  }
  .rg-tooltip {
    display: inline-block;
    padding: var(--rogue-space-2) var(--rogue-space-3);
    background: var(--color-text);
    color: var(--color-page);
    border-radius: var(--radius-control);
    font-size: var(--text-caption);
  }

  /* ------------------------------------------- progress, meter, skeleton ---- */
  .rg-progress { display: flex; flex-direction: column; gap: var(--rogue-space-2); }
  .rg-progress__track { height: var(--rogue-space-3); background: var(--color-surface-raised); border-radius: var(--radius-pill); overflow: hidden; }
  .rg-progress__fill { height: 100%; background: var(--color-accent-solid); border-radius: var(--radius-pill); }
  /* The percentage is always printed as text beside the bar: bar length alone
     is one channel, and it is unreadable in a scan. */
  .rg-progress__value { font-size: var(--text-caption); color: var(--color-text-secondary); font-variant-numeric: tabular-nums; }
  .rg-progress--indeterminate .rg-progress__fill {
    width: 35%;
    animation: rg-indeterminate var(--duration-slow) var(--ease-standard) infinite alternate;
  }

  .rg-meter__track { display: flex; height: var(--rogue-space-4); border-radius: var(--radius-control); overflow: hidden; gap: 2px; }
  .rg-meter__segment { background: var(--viz-deemphasis); }
  .rg-meter__segment--filled { background: var(--color-accent-solid); }

  .rg-skeleton {
    background: var(--color-surface-raised);
    border-radius: var(--radius-control);
    min-height: var(--rogue-space-5);
    animation: rg-pulse var(--duration-slow) var(--ease-standard) infinite alternate;
  }

  @keyframes rg-indeterminate { from { translate: -30% 0; } to { translate: 220% 0; } }
  @keyframes rg-pulse { from { opacity: .55; } to { opacity: 1; } }

  /* -------------------------------------------------------------- KPI tile -- */
  .rg-kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr)); gap: var(--spacing-gutter); }
  .rg-kpi {
    display: flex; flex-direction: column; gap: var(--rogue-space-2);
    padding: var(--rogue-space-5);
    background: var(--color-surface);
    border: var(--rogue-border-hairline) solid var(--color-border);
    border-radius: var(--radius-card);
  }
  .rg-kpi__label { font-size: var(--text-caption); color: var(--color-text-muted); }
  .rg-kpi__value { font-size: var(--text-heading); font-weight: var(--font-weight-semibold); letter-spacing: var(--tracking-tight); }
  .rg-kpi__delta { display: inline-flex; align-items: center; gap: var(--rogue-space-2); font-size: var(--text-caption); color: var(--color-text-secondary); }
  /* Direction is carried by the arrow, sentiment by the colour. They are
     separate because "up" is good for revenue and bad for labour percentage —
     the metric declares which, the tile does not assume. */
  .rg-kpi__delta--good { color: var(--color-success-text); }
  .rg-kpi__delta--bad { color: var(--color-danger-text); }
  .rg-kpi__delta--flat { color: var(--color-text-muted); }
  .rg-kpi__period { font-size: var(--text-caption); color: var(--color-text-muted); }
  .rg-kpi__spark { block-size: var(--rogue-space-7); }

  .rg-hero-figure { font-size: var(--text-hero); font-weight: var(--font-weight-semibold); letter-spacing: var(--tracking-tight); line-height: var(--leading-tight); }

  /* ------------------------------------------------------------------ code -- */
  .rg-code { font-family: var(--font-mono); font-size: .92em; background: var(--color-surface-raised); padding: .1em .35em; border-radius: var(--radius-control); }
  .rg-codeblock { border: var(--rogue-border-hairline) solid var(--color-border); border-radius: var(--radius-control); overflow: hidden; background: var(--color-surface-sunken); }
  .rg-codeblock__header {
    display: flex; align-items: center; justify-content: space-between; gap: var(--spacing-inline);
    padding: var(--rogue-space-2) var(--rogue-space-4);
    background: var(--color-surface-raised);
    border-block-end: var(--rogue-border-hairline) solid var(--color-border-subtle);
    font-family: var(--font-mono); font-size: var(--text-caption); color: var(--color-text-muted);
  }
  .rg-codeblock pre { margin: 0; padding: var(--rogue-space-4); overflow-x: auto; font-size: var(--text-caption); line-height: var(--leading-loose); color: var(--color-text-secondary); }

  /* ------------------------------------------- quote, definition, clauses --- */
  .rg-pullquote { margin: 0; padding-inline-start: var(--rogue-space-5); border-inline-start: var(--rogue-border-heavy) solid var(--color-accent-solid); }
  .rg-pullquote p { font-size: var(--text-body-lg); color: var(--color-text); }
  .rg-pullquote footer { margin-block-start: var(--rogue-space-3); font-size: var(--text-caption); color: var(--color-text-muted); }

  .rg-deflist { display: grid; grid-template-columns: minmax(8rem, 14rem) 1fr; gap: var(--rogue-space-3) var(--spacing-gutter); margin: 0; }
  .rg-deflist dt { font-weight: var(--font-weight-semibold); }
  .rg-deflist dd { margin: 0; color: var(--color-text-secondary); }

  /* Clause numbering to three levels, counter-driven so a re-order cannot
     produce two clause 4.2s. */
  .rg-clauses { counter-reset: rg-l1; list-style: none; margin: 0; padding: 0; }
  .rg-clauses > li { counter-increment: rg-l1; counter-reset: rg-l2; margin-block-start: var(--rogue-space-6); }
  .rg-clauses > li > .rg-clause__heading::before { content: counter(rg-l1) ". "; }
  .rg-clause__heading { font-size: var(--text-doc-h2); font-weight: var(--font-weight-semibold); margin-block-end: var(--rogue-space-3); }
  .rg-clauses ol { counter-reset: rg-l3; list-style: none; margin: 0; padding: 0; }
  .rg-clauses ol > li { counter-increment: rg-l2; counter-reset: rg-l3; display: grid; grid-template-columns: 3.4rem 1fr; gap: var(--spacing-inline); margin-block-start: var(--rogue-space-3); }
  .rg-clauses ol > li::before { content: counter(rg-l1) "." counter(rg-l2); font-variant-numeric: tabular-nums; color: var(--color-text-secondary); }
  .rg-clauses ol ol > li { counter-increment: rg-l3; grid-template-columns: 3.4rem 1fr; }
  .rg-clauses ol ol > li::before { content: "(" counter(rg-l3, lower-alpha) ")"; }
  .rg-defined { font-weight: var(--font-weight-semibold); }
  .rg-xref { font-variant-numeric: tabular-nums; white-space: nowrap; }

  /* --------------------------------------------------------- page furniture */
  .rg-doc { max-inline-size: 7in; margin-inline: auto; font-size: var(--text-doc-body); }
  .rg-running-head, .rg-running-foot {
    display: flex; align-items: baseline; justify-content: space-between; gap: var(--spacing-gutter);
    font-size: var(--text-doc-fine);
    color: var(--color-text-muted);
  }
  .rg-running-head { padding-block-end: var(--rogue-space-2); border-block-end: var(--rogue-border-hairline) solid var(--color-border-subtle); margin-block-end: var(--rogue-space-6); }
  .rg-running-foot { padding-block-start: var(--rogue-space-2); border-block-start: var(--rogue-border-hairline) solid var(--color-border-subtle); margin-block-start: var(--rogue-space-6); }
  .rg-appendix-divider {
    display: flex; align-items: center; gap: var(--spacing-gutter);
    margin-block: var(--rogue-space-9);
    font-size: var(--text-doc-h2); font-weight: var(--font-weight-semibold);
    letter-spacing: var(--tracking-wide); text-transform: uppercase;
  }
  .rg-appendix-divider::after { content: ""; flex: 1; border-block-start: var(--rogue-border-rule) solid var(--color-accent-solid); }

  .rg-rule-accent { border-block-start: var(--rogue-border-rule) solid var(--color-accent-solid); }

  /* Watermark: a repeating diagonal band behind the content plane. Kept at a
     low ink cost and never over a value the reader must transcribe. */
  .rg-watermark { position: relative; isolation: isolate; }
  .rg-watermark::before {
    content: attr(data-watermark);
    position: absolute; inset: 0;
    display: grid; place-items: center;
    font-size: calc(var(--text-doc-title) * 2.2);
    font-weight: var(--font-weight-bold);
    letter-spacing: var(--tracking-caps);
    color: var(--color-text);
    opacity: .06;
    rotate: -32deg;
    pointer-events: none;
    z-index: -1;
  }

  /* ------------------------------------------ empty, error, loading states -- */
  .rg-state {
    display: flex; flex-direction: column; align-items: center; gap: var(--rogue-space-3);
    padding: var(--rogue-space-9) var(--spacing-gutter);
    text-align: center;
    border: var(--rogue-border-hairline) dashed var(--color-border);
    border-radius: var(--radius-card);
  }
  .rg-state__glyph { font-size: var(--text-heading); color: var(--color-text-muted); }
  .rg-state__title { font-size: var(--text-body-lg); font-weight: var(--font-weight-semibold); }
  .rg-state__body { max-inline-size: 34rem; color: var(--color-text-secondary); font-size: var(--text-body); }
  .rg-state--error { border-style: solid; border-color: var(--color-danger-border); background: var(--color-danger-surface); }
  .rg-state--error .rg-state__glyph { color: var(--color-danger-solid); }
  .rg-state--loading { border-style: solid; border-color: var(--color-border-subtle); }

  /* ---------------------------------------------------------------- avatar -- */
  .rg-avatar {
    display: inline-grid; place-items: center;
    width: var(--rogue-space-7); height: var(--rogue-space-7);
    border-radius: var(--radius-pill);
    background: var(--color-accent-surface);
    color: var(--color-accent-text);
    border: var(--rogue-border-hairline) solid var(--color-accent-border);
    font-size: var(--text-caption);
    font-weight: var(--font-weight-medium);
  }

  /* -------------------------------------------------------------- toast ----- */
  .rg-toast {
    display: flex; align-items: flex-start; gap: var(--spacing-inline);
    padding: var(--rogue-space-4) var(--rogue-space-5);
    background: var(--color-surface-overlay);
    border: var(--rogue-border-hairline) solid var(--color-border);
    border-inline-start: var(--rogue-border-heavy) solid var(--color-accent-solid);
    border-radius: var(--radius-control);
    box-shadow: var(--shadow-overlay);
    font-size: var(--text-body);
  }

  /* ------------------------------------------------------------- chart bits -- */
  /* Marks only. Series colour comes from --viz-series-N, and every chart also
     ships either a direct label or a table view — the palette's separation
     floor assumes colour is the only channel, so the relief is structural. */
  .rg-chart { display: flex; flex-direction: column; gap: var(--rogue-space-4); }
  .rg-chart__plot { position: relative; }
  .rg-chart__grid { stroke: var(--viz-grid); stroke-width: 1; }
  .rg-chart__axis { stroke: var(--viz-axis); stroke-width: 1; }
  .rg-chart__label { fill: var(--viz-ink-muted); font-size: var(--text-caption); }
  .rg-chart__legend { display: flex; flex-wrap: wrap; gap: var(--spacing-inline) var(--spacing-gutter); font-size: var(--text-caption); color: var(--color-text-secondary); }
  .rg-chart__key { display: inline-flex; align-items: center; gap: var(--rogue-space-2); }
  .rg-chart__swatch { width: var(--rogue-space-4); height: var(--rogue-space-3); border-radius: 2px; }
  .rg-bar { border-radius: var(--radius-control) var(--radius-control) 0 0; }
  .rg-bar-row { display: grid; grid-template-columns: minmax(6rem, 12rem) 1fr auto; gap: var(--spacing-inline); align-items: center; font-size: var(--text-caption); }
  .rg-bar-row__track { background: var(--color-surface-sunken); border-radius: var(--radius-control); overflow: hidden; }
  .rg-bar-row__fill { height: var(--rogue-space-5); background: var(--viz-series-1); }
  .rg-bar-row__value { font-variant-numeric: tabular-nums; color: var(--color-text-secondary); }

  /* --------------------------------------------------------------- layout --- */
  /* Every delivered artefact wraps its content in .rg-page.
     RULING, recorded in DEVIATIONS.md D-13 — read the scope, it is narrower than
     it looks. Unlayered declarations outrank layered ones by specification, so a
     host page that injects unlayered CSS beats this entire system. Painting the
     ground on a real element the host does not target defends THE GROUND and
     nothing else: an unlayered host rule on p, a, table, li, h2 or any other
     element matching inside .rg-page still wins, so typography, link colour and
     table treatment are NOT protected.

     The position: a STANDALONE artefact is authoritative — it owns the document,
     nothing else is loaded, and .rg-page plus these layers are sufficient. An
     EMBEDDED fragment adapts to its host, and that is intended rather than
     tolerated: a Rogue table inside a client's portal should take the client's
     body copy. If a fragment must be authoritative, it needs a shadow root, not
     more specificity — and that is a different deliverable. */
  .rg-page { background: var(--color-page); color: var(--color-text); min-block-size: 100vb; }

  .rg-stack { display: flex; flex-direction: column; gap: var(--spacing-stack); }
  .rg-stack--gutter { gap: var(--spacing-gutter); }
  .rg-stack--section { gap: var(--spacing-section); }
  .rg-row { display: flex; flex-wrap: wrap; gap: var(--spacing-inline); align-items: center; }
  .rg-row--between { justify-content: space-between; }
  .rg-grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr)); gap: var(--spacing-gutter); }
  .rg-grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr)); gap: var(--spacing-gutter); }
  .rg-muted { color: var(--color-text-muted); }
  .rg-secondary { color: var(--color-text-secondary); }
  .rg-mono { font-family: var(--font-mono); }
  .rg-num { font-variant-numeric: tabular-nums; }
  .rg-caption { font-size: var(--text-caption); }
  .rg-visually-hidden {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
  }

  /* --------------------------------------------------------- signature ------ */
  /* Sized for a pen and for a scanner: the rule is the affordance, the printed
     name below it is what survives a fax. */
  .rg-signature { display: grid; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); gap: var(--spacing-section) var(--rogue-space-10); }
  .rg-signature__block { display: flex; flex-direction: column; gap: var(--rogue-space-2); break-inside: avoid; }
  .rg-signature__party { font-size: var(--text-doc-caption); font-weight: var(--font-weight-semibold); letter-spacing: var(--tracking-wide); text-transform: uppercase; color: var(--color-text-secondary); }
  .rg-signature__line { margin-block-start: var(--rogue-space-8); border-block-start: var(--rogue-border-hairline) solid var(--color-text); }
  .rg-signature__meta { display: grid; grid-template-columns: auto 1fr; gap: var(--rogue-space-2) var(--spacing-inline); font-size: var(--text-doc-caption); color: var(--color-text-secondary); }
  .rg-signature__conditional { font-size: var(--text-doc-fine); color: var(--color-text-muted); }

}

@layer utilities {

  /* ------------------------------------------------------- reduced motion --- */
  /* The base duration tokens are the FULL-motion values. This override lives in
     CSS only and is never baked into tokens.json — an importer must receive a
     motion system that moves. */
  @media (prefers-reduced-motion: reduce) {
    .rg-skeleton, .rg-progress--indeterminate .rg-progress__fill { animation: none; }
    .rg-button, .rg-input, .rg-textarea, .rg-select, .rg-card--interactive,
    .rg-toggle__track, .rg-toggle__thumb, .rg-icon-button { transition: none; }
  }

  /* ---------------------------------------------------------------- print --- */
  @media print {
    /* PRINT IS THE LIGHT GROUND, ALWAYS — whatever the screen was showing.

       This must REBIND the tier-2 tokens, not set color-scheme. color-scheme does
       not touch custom properties: on a dark artefact --color-text would stay
       white, browsers do not print background colours by default, and the result
       is white text on white paper — a blank page. So the light bindings are
       restated here against the tier-1 light primitives, which is the same thing
       [data-ground="light"] does in tokens.css.

       Ordering note: this block is in the utilities layer, so it wins over the
       tokens layer regardless of selector specificity, including over
       [data-ground="light"] and over :root. */
    :root, [data-ground], [data-ground="light"] {
      --color-page: var(--rogue-color-neutral-l-1);
      --color-surface: var(--rogue-color-neutral-l-1);
      --color-surface-raised: var(--rogue-color-neutral-l-1);
      --color-surface-sunken: var(--rogue-color-neutral-l-2);
      --color-surface-overlay: var(--rogue-color-neutral-l-1);
      --color-border-subtle: var(--rogue-color-neutral-l-5);
      --color-border: var(--rogue-color-neutral-l-7);
      --color-border-strong: var(--rogue-color-neutral-l-10);
      --color-border-control: var(--rogue-color-neutral-l-9);
      --color-separator: var(--rogue-color-neutral-l-6);
      --color-scrim: var(--rogue-color-scrim-l);
      --color-text: var(--rogue-color-neutral-l-12);
      --color-text-secondary: var(--rogue-color-neutral-l-11);
      --color-text-muted: var(--rogue-color-neutral-l-10);
      --color-text-on-solid: var(--rogue-color-neutral-l-1);
      --color-text-link: var(--rogue-color-accent-l-11);
      --color-text-disabled: var(--rogue-color-neutral-l-8);
      --color-accent-subtle: var(--rogue-color-accent-l-1);
      --color-accent-surface: var(--rogue-color-accent-l-2);
      --color-accent-border: var(--rogue-color-accent-l-5);
      --color-accent-solid: var(--rogue-color-accent-l-9);
      --color-accent-solid-hover: var(--rogue-color-accent-l-10);
      --color-accent-text: var(--rogue-color-accent-l-11);
      --color-focus-ring: var(--rogue-color-accent-l-8);
      --color-success-surface: var(--rogue-status-l-success-surface);
      --color-success-border: var(--rogue-status-l-success-border);
      --color-success-solid: var(--rogue-status-l-success-solid);
      --color-success-text: var(--rogue-status-l-success-text);
      --color-success-on-solid: var(--rogue-color-neutral-l-1);
      --color-warning-surface: var(--rogue-status-l-warning-surface);
      --color-warning-border: var(--rogue-status-l-warning-border);
      --color-warning-solid: var(--rogue-status-l-warning-solid);
      --color-warning-text: var(--rogue-status-l-warning-text);
      --color-warning-on-solid: var(--rogue-color-neutral-l-1);
      --color-danger-surface: var(--rogue-status-l-danger-surface);
      --color-danger-border: var(--rogue-status-l-danger-border);
      --color-danger-solid: var(--rogue-status-l-danger-solid);
      --color-danger-text: var(--rogue-status-l-danger-text);
      --color-danger-on-solid: var(--rogue-color-neutral-l-1);
      --color-info-surface: var(--rogue-status-l-info-surface);
      --color-info-border: var(--rogue-status-l-info-border);
      --color-info-solid: var(--rogue-status-l-info-solid);
      --color-info-text: var(--rogue-status-l-info-text);
      --color-info-on-solid: var(--rogue-color-neutral-l-1);
      --viz-surface: var(--rogue-color-neutral-l-1);
      --viz-grid: var(--rogue-color-neutral-l-4);
      --viz-axis: var(--rogue-color-neutral-l-7);
      --viz-ink: var(--rogue-color-neutral-l-12);
      --viz-ink-muted: var(--rogue-color-neutral-l-10);
      --viz-series-1: var(--rogue-viz-l-1);
      --viz-series-2: var(--rogue-viz-l-2);
      --viz-series-3: var(--rogue-viz-l-3);
      --viz-series-5: var(--rogue-viz-l-5);
      --viz-series-6: var(--rogue-viz-l-6);
      --viz-deemphasis: var(--rogue-color-neutral-l-6);
      --viz-status-good: var(--rogue-viz-status-l-good);
      --viz-status-warning: var(--rogue-viz-status-l-warning);
      --viz-status-serious: var(--rogue-viz-status-l-serious);
      --viz-status-critical: var(--rogue-viz-status-l-critical);
      --shadow-raised: none;
      --shadow-overlay: none;
    }

    /* Layer order carries this, so no !important is needed and the Stylelint
       gate's declaration-no-important rule stays satisfied. */
    .rg-no-print { display: none; }

    .rg-doc { max-inline-size: none; }
    .rg-card, .rg-callout, .rg-kpi, .rg-codeblock, .rg-state { break-inside: avoid; }
    .rg-clauses > li, .rg-signature__block, .rg-table tr { break-inside: avoid; }
    h1, h2, h3, .rg-clause__heading, .rg-appendix-divider { break-after: avoid; }
    .rg-appendix-divider, .rg-page-break { break-before: page; }

    /* Hairlines survive; tinted fills are dropped to keep ink down. Structure is
       carried by rules, not by fills. */
    .rg-table--zebra tbody tr:nth-child(even) { background: none; }
    .rg-card--raised { box-shadow: none; }
    .rg-badge { background: none; }
    .rg-callout { background: none; }

    /* A URL that is only a hover target on screen has to be readable on paper. */
    .rg-link--external::after { content: " (" attr(href) ")"; font-family: var(--font-mono); font-size: .8em; }

    .rg-scroll-x { overflow: visible; border: 0; }
    .rg-scroll-x > .rg-table { min-width: 0; }
  }

  /* --------------------------------------------------------- forced colors -- */
  @media (forced-colors: active) {
    .rg-badge, .rg-callout, .rg-card, .rg-input, .rg-textarea, .rg-select { border: 1px solid CanvasText; }
    .rg-button { border: 1px solid ButtonText; }
    .rg-progress__fill, .rg-bar-row__fill { background: Highlight; }
    /* Glyph and label carry status here, which is why they are mandatory. */
  }

}
