/* Components: ui.js (ui-*) and the legacy classes page modules still use, restyled to the design system.
   Legacy element rules use :where() (zero specificity) so any class overrides them. Tokens only; see DESIGN.md. */

:root {
  /* Status glyphs drawn with masks so they take the tone color (currentColor) in both themes. */
  --ui-mask-alert: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M10.27 4.2a2 2 0 0 1 3.46 0l7.36 12.8A2 2 0 0 1 19.36 20H4.64a2 2 0 0 1-1.73-3z'/%3E%3Cpath d='M12 9.5v4'/%3E%3Ccircle cx='12' cy='16.75' r='1' fill='black' stroke='none'/%3E%3C/svg%3E");
  --ui-mask-info: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.75' stroke-linecap='round'%3E%3Ccircle cx='12' cy='12' r='9'/%3E%3Cpath d='M12 11v5'/%3E%3Ccircle cx='12' cy='7.75' r='1' fill='black' stroke='none'/%3E%3C/svg%3E");
  --ui-mask-octagon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M8.27 3h7.46L21 8.27v7.46L15.73 21H8.27L3 15.73V8.27z'/%3E%3Cpath d='M12 7.75v4.75'/%3E%3Ccircle cx='12' cy='15.75' r='1' fill='black' stroke='none'/%3E%3C/svg%3E");
  --ui-mask-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m9 6 6 6-6 6'/%3E%3C/svg%3E");
  --ui-control-h: 40px;
}
@media (pointer: coarse) {
  :root { --ui-control-h: 44px; }
}

/* ============================================================================================================
   Legacy: buttons
   ============================================================================================================ */
:where(button:not([class*="ui-"], [class*="viz-"], [class*="shell-"], [class*="mark-"])),
:where(a.button, .ui-heading-action) {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  min-height: var(--ui-control-h);
  padding: 0 var(--s-4);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-sm);
  background: var(--raised);
  box-shadow: var(--shadow-panel);
  color: var(--text);
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 550;
  line-height: var(--lh-sm);
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color var(--t-micro) var(--ease-move), border-color var(--t-micro) var(--ease-move), color var(--t-micro) var(--ease-move);
}
:where(button:not([class*="ui-"], [class*="viz-"], [class*="shell-"], [class*="mark-"])):hover:not(:disabled),
:where(a.button, .ui-heading-action):hover:not(:disabled) {
  border-color: var(--text-3);
  background: color-mix(in srgb, var(--raised), var(--text) 6%);
  color: var(--text);
  text-decoration: none;
}
:where(button:not([class*="ui-"], [class*="viz-"], [class*="shell-"], [class*="mark-"]), .ui-heading-action):active:not(:disabled) { transform: translateY(1px); }
:where(a.button):visited { color: var(--text); }
:where(button):disabled { cursor: not-allowed; opacity: 0.45; }

.primary, a.button.primary, a.button.primary:visited {
  border-color: var(--accent);
  background: var(--accent);
  box-shadow: none;
  color: var(--accent-ink);
}
.primary:hover:not(:disabled), a.button.primary:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
  color: var(--accent-ink);
}
.quiet, a.button.quiet, a.button.quiet:visited {
  border-color: var(--line);
  background: transparent;
  box-shadow: none;
  color: var(--text-2);
}
.quiet:hover:not(:disabled), a.button.quiet:hover {
  border-color: var(--line-strong);
  background: var(--neutral-soft);
  color: var(--text);
}
.quiet[aria-current="page"], .quiet[aria-pressed="true"], a.button[aria-current="page"] {
  border-color: var(--accent-line);
  background: var(--accent-soft);
  color: var(--text);
}
button.danger, a.button.danger, a.button.danger:visited {
  border-color: color-mix(in srgb, var(--danger) 45%, transparent);
  background: var(--danger-soft);
  box-shadow: none;
  color: var(--danger);
}
button.danger:hover:not(:disabled), a.button.danger:hover {
  border-color: var(--danger);
  background: color-mix(in srgb, var(--danger) 20%, transparent);
  color: var(--danger);
}
.text-button {
  display: inline;
  min-height: 0;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: none;
  color: var(--accent);
  font: inherit;
  font-weight: 550;
  text-underline-offset: 3px;
  cursor: pointer;
}
.text-button:hover:not(:disabled) { border: 0; background: none; color: var(--accent-strong); text-decoration: underline; }

.button-row { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2); }

/* ============================================================================================================
   Legacy: form controls
   ============================================================================================================ */
:where(label) {
  display: block;
  margin: var(--s-4) 0 var(--s-2);
  color: var(--text);
  font-size: var(--fs-sm);
  font-weight: 550;
  line-height: var(--lh-sm);
}
:where(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="submit"], [type="button"], [type="reset"], [type="image"])),
:where(select),
:where(textarea) {
  display: block;
  width: 100%;
  min-width: 0;
  min-height: var(--ui-control-h);
  padding: 0 var(--s-3);
  border: 1px solid var(--line-input);
  border-radius: var(--r-sm);
  background: var(--raised);
  color: var(--text);
  font: inherit;
  font-size: var(--fs-sm);
  line-height: var(--lh-sm);
  transition: border-color var(--t-micro) var(--ease-move), box-shadow var(--t-micro) var(--ease-move);
}
:where(textarea) { min-height: 120px; padding: var(--s-2) var(--s-3); resize: vertical; }
:where(select) { padding-right: var(--s-8); cursor: pointer; }
:where(input, select, textarea)::placeholder { color: var(--text-3); opacity: 1; }
:where(input, select, textarea):hover:not(:disabled, :focus) { border-color: var(--text-3); }
:where(input, select, textarea):focus-visible {
  border-color: var(--accent);
  outline: 2px solid var(--focus);
  outline-offset: 1px;
  border-radius: var(--r-sm);
}
:where(input, select, textarea):disabled { cursor: not-allowed; opacity: 0.55; }
:where(input, select, textarea)[aria-invalid="true"],
:where(input, select, textarea):user-invalid {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px var(--danger-soft);
}
:where(input, select, textarea)[aria-invalid="true"]:focus-visible,
:where(input, select, textarea):user-invalid:focus-visible { outline-color: var(--danger); }
:where(input[type="checkbox"], input[type="radio"]) {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.field-error {
  position: relative;
  margin: var(--s-2) 0;
  padding-left: var(--s-6);
  color: var(--danger);
  font-size: var(--fs-sm);
  line-height: var(--lh-sm);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.field-error::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 0;
  width: 16px;
  height: 16px;
  background: currentColor;
  -webkit-mask: var(--ui-mask-octagon) center / contain no-repeat;
  mask: var(--ui-mask-octagon) center / contain no-repeat;
}
.field-error:empty { display: none; }
.hint { margin: var(--s-1) 0 0; color: var(--text-3); font-size: var(--fs-sm); line-height: var(--lh-sm); }
/* Why a primary control is disabled while the Emergency Stop is engaged (compose, playbook run, approve). */
.halt-reason { color: var(--danger); font-weight: 500; }

/* ============================================================================================================
   Legacy: text utilities
   ============================================================================================================ */
.muted { color: var(--text-2); }
.small { font-size: var(--fs-sm); line-height: var(--lh-sm); }
.eyebrow, .ui-eyebrow {
  margin: 0 0 var(--s-2);
  color: var(--text-3);
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: var(--lh-xs);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* ============================================================================================================
   Legacy: badges and ui pills (tone + icon or status dot, never color alone)
   ============================================================================================================ */
.badge, .ui-pill {
  --tone: var(--text-2);
  --tone-soft: var(--neutral-soft);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 22px;
  padding: 0 var(--s-2);
  border-radius: var(--r-sm);
  background: var(--tone-soft);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--tone) 22%, transparent);
  color: var(--tone);
  font-size: var(--fs-xs);
  font-weight: 550;
  line-height: var(--lh-xs);
  letter-spacing: 0.01em;
  white-space: nowrap;
  vertical-align: middle;
  font-variant-numeric: tabular-nums;
}
.badge::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: var(--r-full);
  background: currentColor;
  flex: none;
}
.ui-pill .icon { margin-left: -2px; }
/* Status tones mirror public/ui/status.js STATUS_TONES (test/ui-status.test.js keeps them in sync). */
.badge.active, .badge.succeeded, .ui-pill[data-tone="ok"] { --tone: var(--ok); --tone-soft: var(--ok-soft); }
.badge.running, .badge.queued, .ui-pill[data-tone="info"] { --tone: var(--info); --tone-soft: var(--info-soft); }
.badge.awaiting_approval, .badge.pending, .badge.paused, .badge.inactive, .badge.reconnect, .ui-pill[data-tone="warn"] { --tone: var(--warn); --tone-soft: var(--warn-soft); }
.badge.needs_attention, .badge.failed, .badge.unavailable, .ui-pill[data-tone="danger"] { --tone: var(--danger); --tone-soft: var(--danger-soft); }
.ui-pill[data-tone="accent"] { --tone: var(--accent); --tone-soft: var(--accent-soft); }
/* Waiting states read as hollow dots so they never depend on hue alone. */
.badge.queued::before, .badge.pending::before, .badge.disconnected::before, .badge.cancelled::before, .badge.skipped::before { background: transparent; box-shadow: inset 0 0 0 1.5px currentColor; }

/* ============================================================================================================
   Legacy: panels, callouts, empty, facts, sections, details, progress, dialogs
   ============================================================================================================ */
.panel, .ui-panel {
  min-width: 0;
  padding: var(--s-6);
  border: 1px solid var(--line-subtle);
  border-radius: var(--r-md);
  background: var(--surface);
  box-shadow: var(--shadow-panel);
}
.panel-header, .ui-panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--s-4);
  margin-bottom: var(--s-4);
}
.panel-header h2, .panel-header h3, .ui-panel-title { margin: 0; font-size: var(--fs-lg); line-height: var(--lh-lg); }
.panel-header p:not(.eyebrow) { margin-top: var(--s-1); color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); }

.callout {
  --tone: var(--warn);
  --tone-soft: var(--warn-soft);
  --tone-mask: var(--ui-mask-alert);
  position: relative;
  margin: 0 0 var(--s-6);
  padding: var(--s-4) var(--s-5) var(--s-4) var(--s-12);
  border: 1px solid color-mix(in srgb, var(--tone) 32%, transparent);
  border-radius: var(--r-md);
  background: var(--tone-soft);
  color: var(--text-2);
  font-size: var(--fs-sm);
  line-height: var(--lh-sm);
}
.callout::before {
  content: '';
  position: absolute;
  top: calc(var(--s-4) + 2px);
  left: var(--s-5);
  width: 20px;
  height: 20px;
  background: var(--tone);
  -webkit-mask: var(--tone-mask) center / contain no-repeat;
  mask: var(--tone-mask) center / contain no-repeat;
}
.callout.info { --tone: var(--info); --tone-soft: var(--info-soft); --tone-mask: var(--ui-mask-info); }
.callout h2, .callout h3 { margin: 0 0 var(--s-1); color: var(--text); font-size: var(--fs-md); line-height: var(--lh-md); }
.callout strong { color: var(--text); }
.callout > .eyebrow { margin-bottom: var(--s-1); color: var(--tone); }
.callout > :is(button, .button, .button-row) { margin-top: var(--s-3); }
.callout p:last-child { margin-bottom: 0; }

.empty {
  padding: var(--s-10) var(--s-6);
  border: 1px dashed var(--line-strong);
  border-radius: var(--r-md);
  color: var(--text-2);
  font-size: var(--fs-sm);
  line-height: var(--lh-sm);
  text-align: center;
}
.empty h2, .empty h3 { margin-bottom: var(--s-2); color: var(--text); font-size: var(--fs-lg); line-height: var(--lh-lg); }
.empty p { max-width: 56ch; margin-inline: auto; }
.empty .button-row { justify-content: center; margin-top: var(--s-5); }

.fact-list {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  margin: var(--s-4) 0 0;
  font-size: var(--fs-sm);
  line-height: var(--lh-sm);
  font-variant-numeric: tabular-nums;
}
.fact-list dt, .fact-list dd { padding: var(--s-2) 0; border-top: 1px solid var(--line-subtle); }
.fact-list dt { padding-right: var(--s-4); color: var(--text-3); }
.fact-list dd { margin: 0; color: var(--text); text-align: right; overflow-wrap: anywhere; }
.fact-list dt:first-of-type, .fact-list dd:first-of-type { border-top: 0; }

.section-space { margin-top: var(--s-8); }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: var(--s-4); margin-bottom: var(--s-4); }
.section-heading h2 { margin: 0; font-size: var(--fs-xl); line-height: var(--lh-xl); }

:where(details:not(.ui-disclosure)) { margin-top: var(--s-4); }
:where(details:not(.ui-disclosure)) > summary {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  width: fit-content;
  min-height: 32px;
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: 550;
  list-style: none;
  cursor: pointer;
  border-radius: var(--r-xs);
}
:where(details:not(.ui-disclosure)) > summary::-webkit-details-marker { display: none; }
:where(details:not(.ui-disclosure)) > summary::before {
  content: '';
  width: 16px;
  height: 16px;
  flex: none;
  background: currentColor;
  -webkit-mask: var(--ui-mask-chevron) center / contain no-repeat;
  mask: var(--ui-mask-chevron) center / contain no-repeat;
  transition: transform var(--t-short) var(--ease-move);
}
:where(details:not(.ui-disclosure))[open] > summary::before { transform: rotate(90deg); }
:where(details:not(.ui-disclosure)) > summary:hover { color: var(--text); }
:where(details:not(.ui-disclosure)) > :not(summary) { margin-top: var(--s-2); }

.progress-track { display: flex; gap: var(--s-2); margin: var(--s-4) 0; }
.progress-track span { flex: 1; height: 4px; border-radius: var(--r-full); background: var(--line); }
.progress-track span.complete { background: var(--accent); }

:where(dialog:not([class*="ui-"])) {
  width: min(480px, calc(100% - var(--s-8)));
  max-height: calc(100dvh - var(--s-8));
  padding: var(--s-6);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--overlay);
  box-shadow: var(--shadow-overlay);
  color: var(--text);
  overflow: auto;
}
:where(dialog:not([class*="ui-"]))[open] { animation: ui-dialog-in var(--t-short) var(--ease-enter); }
:where(dialog:not([class*="ui-"]))::backdrop { background: var(--scrim); animation: ui-fade-in var(--t-short) var(--ease-enter); }
:where(dialog:not([class*="ui-"])) h2 { margin: 0 0 var(--s-2); font-size: var(--fs-xl); line-height: var(--lh-xl); }
:where(dialog:not([class*="ui-"])) .button-row { justify-content: flex-end; margin-top: var(--s-6); }
#confirm-description { color: var(--text-2); overflow-wrap: anywhere; }
.business-dialog label { margin-top: var(--s-4); }
.business-dialog label:first-of-type { margin-top: var(--s-5); }

@keyframes ui-fade-in { from { opacity: 0; } }
@keyframes ui-fade-out { to { opacity: 0; } }
@keyframes ui-dialog-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
@keyframes ui-rise-in { from { opacity: 0; transform: translateY(4px); } }

/* ============================================================================================================
   ui: shared affordances
   ============================================================================================================ */
.ui-icon-button {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  padding: 0;
  border: 0;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  transition: background-color var(--t-micro) var(--ease-move), color var(--t-micro) var(--ease-move);
}
.ui-icon-button:hover { background: var(--neutral-soft); color: var(--text); }
@media (pointer: coarse) { .ui-icon-button { width: 44px; height: 44px; } }

/* A link or button stretched over its card, tile or table row; siblings that must stay clickable sit above it. */
.ui-stretch {
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: inherit;
  text-decoration: none;
  cursor: pointer;
}
.ui-stretch:visited { color: inherit; }
.ui-stretch:hover { color: inherit; text-decoration: none; }
.ui-stretch::after { content: ''; position: absolute; inset: 0; border-radius: inherit; }
.ui-stretch:focus-visible { outline: none; }

/* ============================================================================================================
   ui: section, card, panel
   ============================================================================================================ */
.ui-section + .ui-section { margin-top: var(--s-10); }
.ui-section-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--s-3) var(--s-6);
  margin-bottom: var(--s-4);
}
.ui-section-titles { min-width: 0; }
.ui-section-title { font-size: var(--fs-xl); line-height: var(--lh-xl); }
.ui-section-description { max-width: 72ch; margin-top: var(--s-1); color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); }
.ui-section-actions, .ui-panel-actions, .ui-card-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2); }

.ui-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
  min-width: 0;
  padding: var(--s-5);
  border: 1px solid var(--line-subtle);
  border-radius: var(--r-md);
  background: var(--surface);
  box-shadow: var(--shadow-panel);
  transition: border-color var(--t-micro) var(--ease-move), background-color var(--t-micro) var(--ease-move);
}
.ui-card.is-interactive { border-color: var(--line); }
.ui-card.is-interactive:hover { border-color: var(--line-strong); background: var(--raised); }
.ui-card:has(.ui-stretch:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.ui-card[data-tone="ok"] { border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.ui-card[data-tone="warn"] { border-color: color-mix(in srgb, var(--warn) 45%, transparent); background: color-mix(in srgb, var(--surface), var(--warn) 4%); }
.ui-card[data-tone="danger"] { border-color: color-mix(in srgb, var(--danger) 45%, transparent); background: color-mix(in srgb, var(--surface), var(--danger) 4%); }
.ui-card[data-tone="info"] { border-color: color-mix(in srgb, var(--info) 40%, transparent); }
.ui-card[data-tone="accent"] { border-color: var(--accent-line); }
.ui-card-head { display: flex; align-items: flex-start; gap: var(--s-3); }
.ui-card-title { flex: 1; min-width: 0; font-size: var(--fs-lg); line-height: var(--lh-lg); }
.ui-card-head > .icon { margin-top: 4px; color: var(--text-3); transition: transform var(--t-micro) var(--ease-move), color var(--t-micro) var(--ease-move); }
.ui-card.is-interactive:hover .ui-card-head > .icon { color: var(--accent); transform: translate(2px, -2px); }
.ui-card-actions, .ui-card-footer :is(a, button) { position: relative; z-index: 1; }
.ui-card-body { color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); }
.ui-card-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-2) var(--s-4);
  margin-top: auto;
  padding-top: var(--s-3);
  border-top: 1px solid var(--line-subtle);
  color: var(--text-3);
  font-size: var(--fs-xs);
  line-height: var(--lh-xs);
}
.ui-panel-actions { margin-left: auto; }
/* Chart in a panel: give the chart the title (panel({}, lineChart({ title, … }))) and its header, with the "View as
   table" toggle, becomes the panel's title row, typeset exactly like a panel title. */
.ui-panel-body > .viz:first-child > .viz-head { min-height: var(--lh-lg); margin-bottom: var(--s-4); }
.ui-panel-body > .viz:first-child > .viz-head .viz-title { font-size: var(--fs-lg); line-height: var(--lh-lg); font-weight: 600; }

/* Page-heading actions: icon + label; below 640px secondary actions become 40px icon buttons (44px on touch) with a
   tooltip, and the primary action spans the width. */
.ui-heading-action { flex: none; }
.ui-heading-action > .icon { margin-left: -2px; }
@media (max-width: 639px) {
  .ui-heading-action:not(.is-primary) { width: var(--ui-control-h); padding: 0; }
  .ui-heading-action:not(.is-primary) > .icon { margin: 0; }
  .ui-heading-action:not(.is-primary) .ui-heading-action-label { display: none; }
  .ui-heading-action.is-primary { flex: 1 1 100%; width: 100%; }
}

.ui-panel-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-2);
  margin-top: var(--s-4);
  padding-top: var(--s-4);
  border-top: 1px solid var(--line-subtle);
}

/* ============================================================================================================
   ui: tabs, segmented, disclosure
   ============================================================================================================ */
.ui-tabs { min-width: 0; }
.ui-tablist {
  position: relative;
  display: flex;
  gap: var(--s-1);
  overflow-x: auto;
  border-bottom: 1px solid var(--line-subtle);
  scrollbar-width: none;
}
/* On touch there is no wheel to hint at sideways scroll, and the strip hides its scrollbar: a 4px track
  under the tabs says so without stealing the glance layer. */
@media (pointer: coarse) {
  .ui-tablist { scrollbar-width: thin; }
  .ui-tablist::-webkit-scrollbar { height: 4px; }
  .ui-tablist::-webkit-scrollbar-thumb { border-width: 1px; background: var(--line-strong) padding-box; }
}
.ui-tab {
  position: relative;
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: var(--s-2);
  height: var(--ui-control-h);
  padding: 0 var(--s-3);
  border: 0;
  border-radius: var(--r-sm) var(--r-sm) 0 0;
  background: none;
  color: var(--text-2);
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 550;
  white-space: nowrap;
  cursor: pointer;
  transition: color var(--t-micro) var(--ease-move);
}
.ui-tab::after {
  content: '';
  position: absolute;
  right: var(--s-3);
  bottom: 0;
  left: var(--s-3);
  height: 2px;
  border-radius: 2px 2px 0 0;
  background: var(--accent);
  transform: scaleX(0);
  transition: transform var(--t-short) var(--ease-move);
}
.ui-tab:hover { color: var(--text); }
.ui-tab[aria-selected="true"] { color: var(--text); }
.ui-tab[aria-selected="true"]::after { transform: scaleX(1); }
.ui-tab:focus-visible { outline-offset: -2px; border-radius: var(--r-sm); }
.ui-tab .icon { color: var(--text-3); }
.ui-tab[aria-selected="true"] .icon { color: var(--accent); }
/* The one count badge: tab counts and filter-chip (segmented) counts. */
.ui-count {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--r-xs);
  background: var(--neutral-soft);
  color: var(--text-2);
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: 20px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.ui-tab[aria-selected="true"] .ui-count, .ui-segment input:checked + .ui-segment-face .ui-count { color: var(--text); }
.ui-tabpanel { padding-top: var(--s-5); }
.ui-tabpanel:focus-visible { outline-offset: 4px; }

.ui-segmented {
  display: inline-flex;
  justify-self: start;
  width: fit-content;
  max-width: 100%;
  flex-wrap: wrap;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--line-subtle);
  border-radius: var(--r-sm);
  background: var(--sunken);
}
.ui-segment { position: relative; display: block; margin: 0; }
.ui-segment input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.ui-segment-face {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 var(--s-3);
  border-radius: var(--r-xs);
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: 550;
  white-space: nowrap;
  transition: background-color var(--t-micro) var(--ease-move), color var(--t-micro) var(--ease-move);
}
.ui-segment-face:has(.ui-count) { padding-right: 6px; }
.ui-segment-face > .icon { color: var(--text-3); }
.ui-segment:hover .ui-segment-face { color: var(--text); }
.ui-segment input:checked + .ui-segment-face { background: var(--overlay); box-shadow: 0 0 0 1px var(--line), var(--shadow-panel); color: var(--text); }
.ui-segment input:checked + .ui-segment-face > .icon { color: var(--accent); }
.ui-segment input:focus-visible + .ui-segment-face { outline: 2px solid var(--focus); outline-offset: 1px; }
@media (pointer: coarse) { .ui-segment-face { height: 44px; } }

.ui-disclosure > summary {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  width: fit-content;
  min-height: 32px;
  border-radius: var(--r-xs);
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: 550;
  list-style: none;
  cursor: pointer;
}
.ui-disclosure > summary::-webkit-details-marker { display: none; }
.ui-disclosure > summary:hover { color: var(--text); }
.ui-disclosure > summary .icon { color: var(--text-3); transition: transform var(--t-short) var(--ease-move); }
.ui-disclosure[open] > summary .icon { transform: rotate(90deg); }
.ui-disclosure-body { padding: var(--s-2) 0 var(--s-1) var(--s-6); color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); }

/* ============================================================================================================
   ui: drawer
   ============================================================================================================ */
.ui-drawer {
  position: fixed;
  inset: 0 0 0 auto;
  width: min(480px, 100vw);
  max-width: 100vw;
  padding-right: var(--safe-right);
  max-width: 100vw;
  height: 100dvh;
  max-height: 100dvh;
  margin: 0;
  /* A notched phone clips the drawer's head under the notch and its actions under the home indicator. */
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  border: 0;
  border-left: 1px solid var(--line);
  background: var(--overlay);
  box-shadow: var(--shadow-overlay);
  color: var(--text);
}
.ui-drawer[data-size="lg"] { width: min(720px, 100vw); }
.ui-drawer[open] { animation: ui-drawer-in var(--t-short) var(--ease-enter); }
.ui-drawer::backdrop { background: var(--scrim); animation: ui-fade-in var(--t-short) var(--ease-enter); }
.ui-drawer.is-closing { animation: ui-drawer-out var(--t-short) var(--ease-exit) forwards; }
.ui-drawer.is-closing::backdrop { animation: ui-fade-out var(--t-short) var(--ease-exit) forwards; }
.ui-drawer-sheet { display: flex; flex-direction: column; height: 100%; }
.ui-drawer-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--s-4);
  padding: var(--s-5) var(--s-5) var(--s-4) var(--s-6);
  border-bottom: 1px solid var(--line-subtle);
}
.ui-drawer-titles { min-width: 0; padding-top: 2px; }
.ui-drawer-title { font-size: var(--fs-lg); line-height: var(--lh-lg); overflow-wrap: anywhere; }
.ui-drawer-subtitle { margin-top: 2px; color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); }
.ui-drawer-body { flex: 1; min-height: 0; padding: var(--s-6); overflow: auto; overscroll-behavior: contain; }
@keyframes ui-drawer-in { from { transform: translateX(100%); } }
@keyframes ui-drawer-out { to { transform: translateX(100%); } }
@media (max-width: 639px) {
  .ui-drawer, .ui-drawer[data-size="lg"] { width: 100vw; border-left: 0; }
  .ui-drawer-head { padding: var(--s-3) var(--s-3) var(--s-3) var(--s-4); }
  .ui-drawer-body { padding: var(--s-4); }
}

/* Provenance drawer: one column, longest words wrap rather than scroll, so 320px never overflows sideways. */
.prov-sheet { display: grid; gap: var(--s-5); }
.prov-sheet .ui-section + .ui-section { margin-top: 0; }
.prov-sheet .ui-section-title { font-size: var(--fs-lg); line-height: var(--lh-lg); }
.prov-definition { margin: 0; color: var(--text); font-size: var(--fs-md); line-height: var(--lh-md); max-width: 72ch; }
.prov-notes { display: grid; gap: var(--s-2); margin: var(--s-4) 0 0; padding: 0; list-style: none; }
.prov-notes li { display: flex; align-items: flex-start; gap: var(--s-2); color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); overflow-wrap: anywhere; }
.prov-notes .icon { flex: none; margin-top: 2px; color: var(--text-3); }
.prov-meta { display: flex; flex-wrap: wrap; gap: var(--s-1) var(--s-3); color: var(--text-3); font-size: var(--fs-xs); line-height: var(--lh-xs); overflow-wrap: anywhere; }
.prov-seq { font-family: var(--font-mono); font-variant-numeric: tabular-nums; color: var(--text-2); }
.prov-receipt { display: block; font-family: var(--font-mono); font-size: var(--fs-xs); overflow-wrap: anywhere; }
/* The trigger lives inside footnotes and definition rows: it keeps the shared text-button look and their size. */
.prov-open { display: inline-flex; align-items: center; gap: var(--s-1); font-size: inherit; line-height: inherit; white-space: normal; text-align: start; }
.prov-open .icon { flex: none; }
.gov-spend-head .prov-open, .ui-stat-foot .prov-open, .ui-kv dd .prov-open { margin-left: var(--s-2); }

/* ============================================================================================================
   ui: tooltip, popover, menu (top layer, positioned through --ui-x / --ui-y)
   ============================================================================================================ */
.ui-tooltip, .ui-popover {
  position: fixed;
  inset: auto;
  top: var(--ui-y, 0);
  left: var(--ui-x, 0);
  margin: 0;
  overflow: visible;
}
.ui-tooltip {
  max-width: 260px;
  padding: 6px var(--s-2);
  border: 0;
  border-radius: var(--r-sm);
  background: var(--text);
  box-shadow: var(--shadow-overlay);
  color: var(--bg);
  font-size: var(--fs-xs);
  font-weight: 500;
  line-height: var(--lh-xs);
  pointer-events: auto;
}
.ui-tooltip:popover-open { animation: ui-fade-in var(--t-micro) var(--ease-enter); }
.ui-popover {
  max-width: min(360px, calc(100vw - var(--s-4)));
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--overlay);
  box-shadow: var(--shadow-overlay);
  color: var(--text);
}
.ui-popover:popover-open { animation: ui-rise-in var(--t-short) var(--ease-enter); }
.ui-popover:focus-visible { outline: none; }
.ui-popover-text { color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); }
.ui-menu-popover { padding: var(--s-1); }
.ui-menu, .ui-menu-group { display: flex; flex-direction: column; }
.ui-menu { min-width: 220px; }
.ui-menu-item {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  width: 100%;
  min-height: 36px;
  padding: var(--s-2) var(--s-3);
  border: 0;
  border-radius: var(--r-xs);
  background: none;
  color: var(--text);
  font: inherit;
  font-size: var(--fs-sm);
  line-height: var(--lh-sm);
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}
.ui-menu-item:visited { color: var(--text); }
.ui-menu-item:hover, .ui-menu-item:focus { background: var(--neutral-soft); color: var(--text); text-decoration: none; }
.ui-menu-item:focus-visible { outline: none; box-shadow: inset 0 0 0 1px var(--accent-line); background: var(--accent-soft); }
.ui-menu-item > .icon { color: var(--text-3); }
.ui-menu-item.is-danger, .ui-menu-item.is-danger > .icon { color: var(--danger); }
.ui-menu-item.is-danger:focus-visible { box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--danger) 40%, transparent); background: var(--danger-soft); }
.ui-menu-check { display: grid; place-items: center; width: 16px; height: 16px; flex: none; color: var(--accent); }
.ui-menu-text { display: grid; flex: 1; min-width: 0; }
.ui-menu-description { color: var(--text-3); font-size: var(--fs-xs); line-height: var(--lh-xs); }
.ui-menu-separator { height: 1px; margin: var(--s-1) calc(var(--s-1) * -1); background: var(--line-subtle); }
@media (pointer: coarse) { .ui-menu-item { min-height: 44px; } }

/* ============================================================================================================
   ui: command palette
   ============================================================================================================ */
.ui-palette {
  width: min(640px, calc(100vw - var(--s-8)));
  max-width: none;
  max-height: min(560px, 76dvh);
  margin: 12dvh auto auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--overlay);
  box-shadow: var(--shadow-overlay);
  color: var(--text);
  overflow: hidden;
}
.ui-palette[open] { display: flex; flex-direction: column; animation: ui-dialog-in var(--t-micro) var(--ease-enter); }
.ui-palette::backdrop { background: var(--scrim); }
.ui-palette-search {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: 0 var(--s-4);
  border-bottom: 1px solid var(--line-subtle);
  color: var(--text-3);
}
.ui-palette-input {
  flex: 1;
  height: 56px;
  min-height: 56px;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: none;
  color: var(--text);
  font-size: var(--fs-md);
}
.ui-palette-input:focus-visible { outline: none; }
/* The input's own ring is replaced by an accent rule under the whole search row (always visible while typing). */
.ui-palette-search:focus-within { box-shadow: inset 0 -2px 0 var(--focus); }
.ui-palette-list { flex: 1; min-height: 0; padding: var(--s-2); overflow-y: auto; overscroll-behavior: contain; }
.ui-palette-group + .ui-palette-group { margin-top: var(--s-2); }
.ui-palette-group-label {
  padding: var(--s-2) var(--s-3) var(--s-1);
  color: var(--text-3);
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: var(--lh-xs);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.ui-palette-option {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  min-height: 44px;
  padding: var(--s-1) var(--s-3) var(--s-1) var(--s-2);
  border-radius: var(--r-sm);
  cursor: pointer;
}
.ui-palette-option > .icon { color: var(--accent); opacity: 0; transition: opacity var(--t-micro) var(--ease-move); }
.ui-palette-option[aria-selected="true"] { background: var(--accent-soft); }
.ui-palette-option[aria-selected="true"] > .icon { opacity: 1; }
.ui-palette-icon {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: var(--r-sm);
  background: var(--neutral-soft);
  color: var(--text-2);
}
.ui-palette-option[aria-selected="true"] .ui-palette-icon { color: var(--accent); }
.ui-palette-text { display: grid; flex: 1; min-width: 0; }
.ui-palette-title, .ui-palette-subtitle { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ui-palette-title { font-size: var(--fs-sm); font-weight: 500; line-height: var(--lh-sm); }
.ui-palette-title mark { background: none; color: var(--accent); font-weight: 650; }
.ui-palette-subtitle { color: var(--text-3); font-size: var(--fs-xs); line-height: var(--lh-xs); }
.ui-palette-empty { padding: var(--s-8) var(--s-4); color: var(--text-3); font-size: var(--fs-sm); text-align: center; }
.ui-palette-foot {
  display: flex;
  gap: var(--s-5);
  padding: var(--s-2) var(--s-4);
  border-top: 1px solid var(--line-subtle);
  color: var(--text-3);
  font-size: var(--fs-xs);
  line-height: var(--lh-xs);
}
.ui-palette-foot span { display: inline-flex; align-items: center; gap: 4px; }
@media (max-width: 639px) {
  .ui-palette { width: calc(100vw - var(--s-4)); margin-top: var(--s-2); max-height: 70dvh; }
  .ui-palette-foot, .ui-palette-search kbd { display: none; }
}

/* ============================================================================================================
   ui: stat tile
   ============================================================================================================ */
/* KPI row: four across on wide content, two across below 1024px; on phones (<400px) one across when a label would
   otherwise be truncated (ui.js toggles .is-stacked). */
.ui-stat-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--s-4); }
@media (max-width: 1023px) { .ui-stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 639px) { .ui-stat-grid { gap: var(--s-3); } }
@media (max-width: 399px) { .ui-stat-grid.is-stacked { grid-template-columns: minmax(0, 1fr); } }
/* Top-anchored: head and figure have fixed heights, so values share one baseline across a row whether or not a
   tile has a status, a trend, a delta or a footnote; the meta row sits at the foot. */
.ui-stat {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
  min-width: 0;
  padding: var(--s-5);
  border: 1px solid var(--line-subtle);
  border-radius: var(--r-md);
  background: var(--surface);
  box-shadow: var(--shadow-panel);
  container-type: inline-size;
  transition: border-color var(--t-micro) var(--ease-move), background-color var(--t-micro) var(--ease-move);
}
.ui-stat.is-interactive { border-color: var(--line); }
.ui-stat.is-interactive:hover { border-color: var(--line-strong); background: var(--raised); }
.ui-stat:has(.ui-stretch:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.ui-stat.is-unknown { border: 1px dashed var(--line-input); background: transparent; box-shadow: none; }
.ui-stat-head { display: flex; align-items: center; gap: var(--s-2); height: 24px; min-width: 0; }
.ui-stat-label {
  display: flex;
  flex: 1 1 0;
  align-items: center;
  gap: var(--s-2);
  min-width: 0;
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: 500;
  line-height: var(--lh-sm);
}
.ui-stat-label > .icon { color: var(--text-3); }
.ui-stat-label > :last-child { min-width: 0; }
.ui-stat-label-text { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* A label that runs out of room wraps to a second line rather than ending in an unexplained ellipsis. */
@media (max-width: 639px) {
  .ui-stat-head { height: auto; min-height: 24px; align-items: flex-start; }
  .ui-stat-label { align-items: flex-start; }
  .ui-stat-label-text { overflow: visible; white-space: normal; overflow-wrap: anywhere; }
}
.ui-stat-head > .ui-pill, .ui-stat-help { position: relative; z-index: 1; flex: none; }
.ui-stat-help { width: 24px; height: 24px; margin: -4px -4px -4px 0; color: var(--text-3); }
.ui-stat-main { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s-3); min-width: 0; }
.ui-stat-figure { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 6px; min-width: 0; min-height: var(--fs-kpi); margin: 0; white-space: normal; overflow-wrap: anywhere; }
.ui-stat-value {
  color: var(--text);
  font-size: var(--fs-kpi);
  font-weight: 600;
  line-height: 1;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}
.ui-stat-unit { color: var(--text-2); font-size: var(--fs-sm); font-weight: 500; }
.ui-stat.is-unknown .ui-stat-value { color: var(--text-3); font-weight: 500; }
.ui-stat-trend { display: flex; flex: none; height: 32px; }
.ui-stat-meta { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-1) var(--s-2); min-height: 22px; margin-top: auto; }
.ui-stat-delta {
  --tone: var(--text-2);
  --tone-soft: var(--neutral-soft);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 22px;
  padding: 3px var(--s-2);
  border-radius: var(--r-sm);
  background: var(--tone-soft);
  color: var(--tone);
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: var(--lh-xs);
  overflow-wrap: break-word;
  font-variant-numeric: tabular-nums;
}
.ui-stat-delta::before { content: ''; width: 8px; height: 6px; flex: none; background: currentColor; clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.ui-stat-delta[data-direction="down"]::before { transform: rotate(180deg); }
.ui-stat-delta[data-direction="flat"]::before { height: 2px; border-radius: 1px; clip-path: none; }
.ui-stat-delta[data-tone="ok"] { --tone: var(--ok); --tone-soft: var(--ok-soft); }
.ui-stat-delta[data-tone="warn"] { --tone: var(--warn); --tone-soft: var(--warn-soft); }
.ui-stat-delta[data-tone="danger"] { --tone: var(--danger); --tone-soft: var(--danger-soft); }
.ui-stat-foot { color: var(--text-3); font-size: var(--fs-xs); line-height: var(--lh-xs); }
/* Narrow tiles: the trend moves under the value (the value keeps its place) and the value steps down a size. */
@container (max-width: 200px) {
  .ui-stat-main { flex-direction: column; align-items: flex-start; gap: var(--s-2); }
  .ui-stat-value { font-size: 1.75rem; }
  .ui-stat-figure { min-height: 1.75rem; }
}
@media (max-width: 639px) { .ui-stat { padding: var(--s-4); } }

/* ============================================================================================================
   ui: meter
   ============================================================================================================ */
.ui-meter { --tone: var(--accent); display: grid; gap: var(--s-2); min-width: 0; }
.ui-meter[data-tone="ok"] { --tone: var(--ok); }
.ui-meter[data-tone="warn"] { --tone: var(--warn); }
.ui-meter[data-tone="danger"] { --tone: var(--danger); }
.ui-meter[data-tone="info"] { --tone: var(--info); }
.ui-meter[data-tone="neutral"] { --tone: var(--text-3); }
.ui-meter-head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--s-3); font-size: var(--fs-sm); line-height: var(--lh-sm); }
.ui-meter-label { min-width: 0; color: var(--text-2); }
.ui-meter-value { color: var(--text); font-weight: 550; white-space: nowrap; font-variant-numeric: tabular-nums; }
.ui-meter-track { height: 6px; overflow: hidden; border-radius: var(--r-full); background: var(--neutral-soft); }
.ui-meter-fill {
  display: block;
  width: calc(var(--ui-ratio, 0) * 100%);
  height: 100%;
  border-radius: inherit;
  background: var(--tone);
  transition: width var(--t-medium) var(--ease-move);
}
.ui-meter.is-unknown .ui-meter-track { background: none; border: 1px dashed var(--line-input); }
.ui-meter.is-unknown .ui-meter-value { color: var(--text-3); font-weight: 500; }

/* ============================================================================================================
   ui: data table
   ============================================================================================================ */
.ui-table-wrap {
  max-height: 70vh;
  overflow: auto;
  border: 1px solid var(--line-subtle);
  border-radius: var(--r-md);
  /* Scroll shadows: covers move with the content, shadows stay at the edges. */
  background:
    linear-gradient(90deg, var(--surface) 30%, transparent) left / 40px 100% no-repeat local,
    linear-gradient(270deg, var(--surface) 30%, transparent) right / 40px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, var(--line-strong), transparent) left / 12px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, var(--line-strong), transparent) right / 12px 100% no-repeat scroll,
    var(--surface);
  box-shadow: var(--shadow-panel);
}
.ui-table-wrap:focus-visible { outline-offset: 2px; border-radius: var(--r-md); }
.ui-table { min-width: 100%; font-size: var(--fs-sm); line-height: var(--lh-sm); font-variant-numeric: tabular-nums; }
.ui-table caption {
  padding: var(--s-3) var(--s-4);
  border-bottom: 1px solid var(--line-subtle);
  color: var(--text);
  font-weight: 600;
  text-align: left;
  caption-side: top;
}
.ui-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  width: var(--ui-col-w, auto);
  height: 36px;
  padding: 0 var(--s-4);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  color: var(--text-3);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.02em;
  text-align: left;
  white-space: nowrap;
}
.ui-table :is(th, td)[data-align="end"] { text-align: right; }
.ui-table :is(th, td)[data-align="center"] { text-align: center; }
.ui-sort {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 28px;
  margin: 0 -4px;
  padding: 0 4px;
  border: 0;
  border-radius: var(--r-xs);
  background: none;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  cursor: pointer;
}
[data-align="end"] > .ui-sort { flex-direction: row-reverse; }
.ui-sort:hover, th[aria-sort="ascending"] .ui-sort, th[aria-sort="descending"] .ui-sort { color: var(--text); }
.ui-sort .icon { opacity: 0.45; }
th[aria-sort="ascending"] .ui-sort .icon, th[aria-sort="descending"] .ui-sort .icon { opacity: 1; color: var(--accent); }
th[aria-sort="ascending"] .ui-sort path:first-of-type, th[aria-sort="descending"] .ui-sort path:last-of-type { opacity: 0.25; }
.ui-table tbody :is(th, td) {
  padding: var(--s-3) var(--s-4);
  border-bottom: 1px solid var(--line-subtle);
  color: var(--text);
  font-weight: 400;
  text-align: left;
  vertical-align: middle;
}
.ui-table tbody th { font-weight: 500; }
.ui-table tbody tr:last-child > * { border-bottom: 0; }
.ui-table.is-dense tbody :is(th, td) { padding: var(--s-2) var(--s-3); }
.ui-table.is-dense thead th { height: 32px; padding: 0 var(--s-3); }
.ui-table tr.is-interactive { position: relative; cursor: pointer; }
.ui-table tr.is-interactive:hover > * { background: var(--neutral-soft); }
.ui-table tr.is-interactive:has(.ui-stretch:focus-visible) { outline: 2px solid var(--focus); outline-offset: -2px; }
.ui-table tbody .ui-stretch { color: var(--text); font-weight: 500; }
.ui-table .ui-table-empty { padding: var(--s-8) var(--s-4); color: var(--text-3); text-align: center; }

/* ============================================================================================================
   ui: key values, timeline, stepper
   ============================================================================================================ */
.ui-kv { display: grid; grid-template-columns: fit-content(50%) minmax(0, 1fr); column-gap: var(--s-6); font-size: var(--fs-sm); line-height: var(--lh-sm); }
.ui-kv-row { display: grid; grid-column: 1 / -1; grid-template-columns: subgrid; padding: var(--s-2) 0; border-top: 1px solid var(--line-subtle); }
.ui-kv-row:first-child { border-top: 0; }
.ui-kv dt { color: var(--text-3); }
.ui-kv dd { min-width: 0; color: var(--text); overflow-wrap: break-word; font-variant-numeric: tabular-nums; }

.ui-timeline { margin: 0; padding: 0; list-style: none; }
.ui-timeline-item {
  --tone: var(--text-3);
  --tone-soft: var(--neutral-soft);
  position: relative;
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  gap: var(--s-3);
  margin: 0;
  padding-bottom: var(--s-5);
}
.ui-timeline-item + .ui-timeline-item { margin-top: 0; }
.ui-timeline-item:last-child { padding-bottom: 0; }
.ui-timeline-item::before { content: ''; position: absolute; top: 28px; bottom: 4px; left: 11.5px; width: 1px; background: var(--line); }
.ui-timeline-item:last-child::before { display: none; }
.ui-timeline-item[data-tone="ok"] { --tone: var(--ok); --tone-soft: var(--ok-soft); }
.ui-timeline-item[data-tone="warn"] { --tone: var(--warn); --tone-soft: var(--warn-soft); }
.ui-timeline-item[data-tone="danger"] { --tone: var(--danger); --tone-soft: var(--danger-soft); }
.ui-timeline-item[data-tone="info"] { --tone: var(--info); --tone-soft: var(--info-soft); }
.ui-timeline-item[data-tone="accent"] { --tone: var(--accent); --tone-soft: var(--accent-soft); }
.ui-timeline-marker { display: grid; place-items: center; width: 24px; height: 24px; border-radius: var(--r-full); background: var(--tone-soft); color: var(--tone); }
.ui-timeline-marker:empty::after { content: ''; width: 8px; height: 8px; border-radius: var(--r-full); background: currentColor; }
.ui-timeline-content { min-width: 0; padding-top: 2px; }
.ui-timeline-head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--s-3); }
.ui-timeline-title { min-width: 0; color: var(--text); font-size: var(--fs-sm); font-weight: 550; line-height: var(--lh-sm); }
.ui-timeline-head > :is(time, .ui-timeline-time) { flex: none; color: var(--text-3); font-size: var(--fs-xs); line-height: var(--lh-xs); white-space: nowrap; font-variant-numeric: tabular-nums; }
.ui-timeline-body { margin-top: 2px; color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); }

/* The stepper is its own container: horizontal when it has room, vertical when it doesn't. */
.ui-stepper { container-type: inline-size; }
.ui-stepper-list { display: flex; margin: 0; padding: 0; list-style: none; }
.ui-step {
  display: grid;
  flex: 1 1 0;
  grid-template-columns: 24px auto minmax(var(--s-4), 1fr);
  column-gap: var(--s-3);
  align-items: start;
  margin: 0;
}
.ui-step + .ui-step { margin-top: 0; }
.ui-step:last-child { flex: 0 1 auto; grid-template-columns: 24px auto; }
.ui-step::after { content: ''; grid-column: 3; grid-row: 1; height: 1px; margin: 11.5px var(--s-3) 0 0; background: var(--line); }
.ui-step:last-child::after { display: none; }
.ui-step[data-state="complete"]::after { background: var(--accent); }
.ui-step-marker {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: var(--r-full);
  box-shadow: inset 0 0 0 1.5px var(--line-strong);
  color: var(--text-2);
  font-size: var(--fs-xs);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.ui-step[data-state="complete"] .ui-step-marker { background: var(--accent); box-shadow: none; color: var(--accent-ink); }
.ui-step[data-state="current"] .ui-step-marker { box-shadow: inset 0 0 0 1.5px var(--accent), 0 0 0 4px var(--accent-soft); color: var(--accent); }
.ui-step[data-state="failed"] .ui-step-marker { background: var(--danger-soft); box-shadow: inset 0 0 0 1.5px var(--danger); color: var(--danger); }
.ui-step-text { min-width: 0; }
.ui-step-label { color: var(--text); font-size: var(--fs-sm); font-weight: 550; line-height: 24px; white-space: nowrap; }
.ui-step[data-state="upcoming"] .ui-step-label { color: var(--text-2); font-weight: 500; }
.ui-step-description { max-width: 24ch; color: var(--text-3); font-size: var(--fs-xs); line-height: var(--lh-xs); }
@container (max-width: 559px) {
  .ui-stepper-list { flex-direction: column; }
  .ui-step, .ui-step:last-child { position: relative; grid-template-columns: 24px minmax(0, 1fr); padding-bottom: var(--s-4); }
  .ui-step:last-child { padding-bottom: 0; }
  .ui-step::after { position: absolute; grid-area: auto; top: 28px; bottom: 4px; left: 11.5px; width: 1px; height: auto; margin: 0; }
  .ui-step-label { white-space: normal; }
}

/* ============================================================================================================
   ui: skeleton, empty state, toasts
   ============================================================================================================ */
.ui-skeleton { display: grid; gap: var(--s-3); }
.ui-skeleton-line, .ui-skeleton-block {
  display: block;
  height: 12px;
  border-radius: var(--r-xs);
  background: linear-gradient(90deg, var(--neutral-soft) 0%, color-mix(in srgb, var(--neutral-soft), var(--text) 6%) 50%, var(--neutral-soft) 100%) 0 0 / 300% 100%;
  animation: ui-shimmer 1.6s linear infinite;
}
.ui-skeleton-line:first-of-type { width: 40%; height: 16px; }
.ui-skeleton-line:last-child:not(:first-of-type) { width: 64%; }
.ui-skeleton-block { height: var(--ui-h); border-radius: var(--r-md); }
@keyframes ui-shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } }
@media (prefers-reduced-motion: reduce) { .ui-skeleton-line, .ui-skeleton-block { animation: none; } }

.ui-empty {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-5) var(--s-8);
  padding: var(--s-8);
  border: 1px solid var(--line-subtle);
  border-radius: var(--r-md);
  background: var(--surface);
}
.ui-empty-art { display: grid; place-items: center; flex: none; color: var(--text-3); }
.ui-empty-text { flex: 1 1 280px; min-width: 0; }
.ui-empty-title { color: var(--text); font-size: var(--fs-lg); font-weight: 600; line-height: var(--lh-lg); }
.ui-empty-body { max-width: 56ch; margin-top: var(--s-1); color: var(--text-2); font-size: var(--fs-sm); line-height: var(--lh-sm); }
.ui-empty-action { display: flex; flex-wrap: wrap; gap: var(--s-2); margin-top: var(--s-4); }
@media (max-width: 639px) { .ui-empty:not(.is-compact) { padding: var(--s-6) var(--s-5); } }
/* Compact: inside a panel or chart. Centered column, 72px art, no frame of its own. */
.ui-empty.is-compact {
  flex-direction: column;
  flex-wrap: nowrap;
  justify-content: center;
  gap: var(--s-3);
  padding: var(--s-6) var(--s-4);
  border: 0;
  background: none;
  box-shadow: none;
  text-align: center;
}
/* The art owns a fixed 72px box (any illustration size is fitted into it) plus its own bottom margin, so it can never
   ride over the title whatever the container does to flex, grid or gap. */
.ui-empty.is-compact .ui-empty-art { width: 72px; height: 72px; margin: 0 auto var(--s-1); overflow: hidden; }
.ui-empty.is-compact .ui-empty-art svg { display: block; width: 100%; height: 100%; }
.ui-empty.is-compact .ui-empty-text { flex: none; }
.ui-empty.is-compact .ui-empty-title { font-size: var(--fs-md); line-height: var(--lh-md); }
.ui-empty.is-compact .ui-empty-body { max-width: 44ch; margin-inline: auto; }
.ui-empty.is-compact .ui-empty-action { justify-content: center; margin-top: var(--s-3); }

.ui-toasts {
  position: fixed;
  right: calc(var(--s-6) + var(--safe-right));
  bottom: calc(var(--s-6) + var(--safe-bottom));
  z-index: var(--z-toast);
  width: min(400px, calc(100vw - var(--s-8)));
  pointer-events: none;
}
.ui-toasts ol { display: flex; flex-direction: column; gap: var(--s-2); margin: 0; padding: 0; list-style: none; }
.ui-toast {
  --tone: var(--text-3);
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) auto auto;
  align-items: center;
  gap: var(--s-3);
  margin: 0;
  padding: var(--s-2) var(--s-2) var(--s-2) var(--s-4);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--overlay);
  box-shadow: var(--shadow-overlay);
  color: var(--text);
  pointer-events: auto;
  animation: ui-toast-in var(--t-short) var(--ease-enter);
}
.ui-toast + .ui-toast { margin-top: 0; }
.ui-toast[data-tone="ok"] { --tone: var(--ok); }
.ui-toast[data-tone="warn"] { --tone: var(--warn); }
.ui-toast[data-tone="danger"] { --tone: var(--danger); }
.ui-toast[data-tone="info"] { --tone: var(--info); }
.ui-toast > .icon { color: var(--tone); }
.ui-toast-message { padding: 6px 0; font-size: var(--fs-sm); line-height: var(--lh-sm); }
.ui-toast-action {
  height: 32px;
  padding: 0 var(--s-2);
  border: 0;
  border-radius: var(--r-sm);
  background: none;
  color: var(--accent);
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  cursor: pointer;
}
.ui-toast-action:hover { background: var(--accent-soft); }
.ui-toast.is-leaving { animation: ui-toast-out var(--t-micro) var(--ease-exit) forwards; }
@keyframes ui-toast-in { from { opacity: 0; transform: translateY(8px); } }
@keyframes ui-toast-out { to { opacity: 0; transform: translateY(4px); } }
/* Phones: the stack sits at the top, just under the top bar, so it never covers sticky bottom action bars
   (WCAG 2.4.11); newest stays last, entering from above. */
@media (max-width: 639px) {
  .ui-toasts { top: var(--toast-top, calc(var(--topbar) + var(--safe-top) + var(--s-2))); right: calc(var(--s-4) + var(--safe-right)); bottom: auto; left: calc(var(--s-4) + var(--safe-left)); width: auto; }
  .ui-toast { animation-name: ui-toast-in-top; }
  .ui-toast.is-leaving { animation-name: ui-toast-out-top; }
}
@keyframes ui-toast-in-top { from { opacity: 0; transform: translateY(-8px); } }
@keyframes ui-toast-out-top { to { opacity: 0; transform: translateY(-4px); } }

/* ============================================================================================================
   Small screens: legacy surfaces
   ============================================================================================================ */
@media (max-width: 639px) {
  .panel, .ui-panel { padding: var(--s-4); }
  .panel-header { flex-wrap: wrap; }
  .callout { padding: var(--s-3) var(--s-4) var(--s-3) var(--s-10); }
  .callout::before { top: calc(var(--s-3) + 2px); left: var(--s-3); }
  :where(dialog:not([class*="ui-"])) { padding: var(--s-5); }
  :where(dialog:not([class*="ui-"])) .button-row > * { flex: 1 1 auto; }
}

/* Coarse pointers: every primary control gets a 44px target (DESIGN.md, touch targets). */
@media (pointer: coarse) {
  .ui-stat-help { width: 44px; height: 44px; margin: -10px; }
  a.button, .text-button, .ui-sort, .ui-disclosure > summary, :where(details:not(.ui-disclosure)) > summary { min-height: 44px; }
  .text-button { display: inline-flex; align-items: center; }
  /* Raised specificity (0,3,1) so page rules that size checkboxes smaller (e.g. .gov-check input) still yield on
     touch; custom switches (role=switch) keep their own geometry. */
  :root input:is([type="checkbox"], [type="radio"]):not([role="switch"]) { width: 24px; height: 24px; }
}
