/* =============================================================================
   Components — buttons, forms, surfaces, chips, tabs, tables, overlays, toasts,
   menus, palette, feedback states, code/JSON.  Vocabulary: SPEC §5.3.
   ============================================================================= */

/* ---------- Buttons ---------- */
.btn {
  --btn-bg: transparent;
  --btn-fg: var(--text);
  --btn-border: transparent;
  --btn-hover-bg: var(--surface-hover);
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  height: var(--ctl-h);
  padding: 0 14px;
  border: 1px solid var(--btn-border);
  border-radius: var(--r-sm);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-size: 13.5px;
  font-weight: var(--fw-medium);
  line-height: 1;
  white-space: nowrap;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.btn:hover:not(:disabled) { background: var(--btn-hover-bg); }
.btn:active:not(:disabled) { transform: translateY(0.5px); }
.btn:disabled { opacity: 0.5; }
.btn:focus-visible { box-shadow: var(--focus-ring); border-radius: var(--r-sm); }
.btn > .icon { width: 16px; height: 16px; }

.btn--primary {
  --btn-bg: var(--accent-solid);
  --btn-fg: var(--accent-contrast);
  --btn-hover-bg: var(--accent-solid);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14), 0 1px 2px rgba(0, 0, 0, 0.2);
}
.btn--primary:hover:not(:disabled) { filter: brightness(1.1); }
.btn--secondary {
  --btn-bg: var(--surface-2);
  --btn-border: var(--border-strong);
  --btn-hover-bg: var(--surface-3);
}
.btn--ghost { --btn-fg: var(--text-2); }
.btn--ghost:hover:not(:disabled) { color: var(--text); }
.btn--subtle {
  --btn-bg: var(--accent-soft);
  --btn-fg: var(--accent-fg);
  --btn-hover-bg: color-mix(in srgb, var(--accent) 22%, transparent);
}
.btn--danger {
  --btn-bg: #dc2626;
  --btn-fg: #ffffff;
  --btn-hover-bg: #b91c1c;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12);
}
.btn--sm { height: var(--ctl-h-sm); padding: 0 10px; font-size: var(--fs-sm); gap: 6px; }
.btn--sm > .icon { width: 14px; height: 14px; }
.btn--lg { height: var(--ctl-h-lg); padding: 0 18px; font-size: var(--fs-md); }
.btn--icon { width: var(--ctl-h); padding: 0; flex: none; }
.btn--icon.btn--sm { width: var(--ctl-h-sm); }
.btn--icon.btn--lg { width: var(--ctl-h-lg); }
.btn--block { display: flex; width: 100%; }

.btn.is-busy { color: transparent !important; pointer-events: none; }
.btn.is-busy > * { visibility: hidden; }
.btn.is-busy::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--btn-fg);
  border-right-color: transparent;
  animation: spin 0.7s linear infinite;
}

.btn-group { display: inline-flex; align-items: center; gap: var(--s-2); flex-wrap: wrap; }

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  flex: none;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-right-color: transparent;
  animation: spin 0.7s linear infinite;
  opacity: 0.8;
}
.spinner--lg { width: 24px; height: 24px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field__label {
  display: flex;
  align-items: baseline;
  gap: var(--s-1);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  color: var(--text-2);
}
.field__required { color: var(--danger); }
.field__optional { color: var(--text-3); font-weight: var(--fw-regular); font-size: var(--fs-xs); }
.field__hint { font-size: 12px; color: var(--text-3); line-height: 1.45; }
.field__error { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--danger); }
.field__error .icon { width: 13px; height: 13px; }
.field--inline { flex-direction: row; align-items: center; justify-content: space-between; gap: var(--s-4); }
.field--wide { grid-column: 1 / -1; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
  gap: var(--s-4);
}
.form-grid > * { min-width: 0; }

.input, .select, .textarea {
  width: 100%;
  min-width: 0;
  height: var(--ctl-h);
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--bg);
  color: var(--text);
  font-size: var(--fs-base);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.input::placeholder, .textarea::placeholder { color: var(--text-3); opacity: 1; }
.input:hover, .select:hover, .textarea:hover { border-color: color-mix(in srgb, var(--text-3) 55%, transparent); }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--accent); box-shadow: var(--ring); }
.input:disabled, .select:disabled, .textarea:disabled { opacity: 0.55; cursor: not-allowed; }
.input.is-invalid, .select.is-invalid, .textarea.is-invalid { border-color: var(--danger); }
.input.is-invalid:focus, .textarea.is-invalid:focus { box-shadow: 0 0 0 3px var(--danger-soft); }
.input--mono, .textarea--mono { font-family: var(--font-mono); font-size: 12.5px; }
.input--sm { height: var(--ctl-h-sm); font-size: var(--fs-sm); }
.input[type="number"] { font-variant-numeric: tabular-nums; }
.input[type="date"], .input[type="time"], .input[type="datetime-local"] { color-scheme: inherit; }
.select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a93a6' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 16px;
  text-overflow: ellipsis;
}
.textarea { height: auto; min-height: 96px; padding: 10px 12px; line-height: 1.5; resize: vertical; }
@media (max-width: 767px) {
  /* 16px prevents iOS zoom on focus */
  .input, .select, .textarea { font-size: 16px; }
}

.input-group { position: relative; display: flex; align-items: center; min-width: 0; }
.input-group > .input { padding-left: 34px; }
.input-group__icon {
  position: absolute;
  left: 11px;
  color: var(--text-3);
  pointer-events: none;
}
.input-group__suffix { position: absolute; right: 6px; display: flex; align-items: center; gap: var(--s-1); }
.input-group--suffix > .input { padding-right: 72px; }

.switch {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  width: 36px;
  height: 20px;
  flex: none;
  margin: 0;
  border-radius: var(--r-full);
  background: var(--surface-3);
  box-shadow: inset 0 0 0 1px var(--border-strong);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.switch::before {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-2);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform var(--dur) var(--ease), background var(--dur-fast) var(--ease);
}
.switch:checked { background: var(--accent-solid); box-shadow: none; }
.switch:checked::before { transform: translateX(16px); background: #ffffff; }
.switch:disabled { opacity: 0.5; cursor: not-allowed; }
.switch:focus-visible { box-shadow: var(--focus-ring); border-radius: var(--r-full); }
.switch-row { display: inline-flex; align-items: center; gap: var(--s-3); min-height: var(--ctl-h); cursor: pointer; font-size: var(--fs-base); color: var(--text); }

.segmented {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  max-width: 100%;
  overflow-x: auto;
  scrollbar-width: none;
}
.segmented::-webkit-scrollbar { display: none; }
.segmented__item {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: calc(var(--ctl-h-sm) - 2px);
  padding: 0 12px;
  border-radius: 5px;
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.segmented__item:hover { color: var(--text); }
.segmented__item.is-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm), inset 0 0 0 1px var(--border); }
.segmented__item .icon { width: 14px; height: 14px; }

/* ---------- Surfaces ---------- */
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
}
.card__header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-2) var(--s-3);
  min-height: 52px;
  padding: var(--s-3) var(--card-p);
  border-bottom: 1px solid var(--border);
}
.card__heading { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.card__title {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  font-size: var(--fs-base);
  font-weight: var(--fw-semibold);
  color: var(--text);
  min-width: 0;
}
.card__title .icon { color: var(--text-3); }
.card__subtitle { font-size: var(--fs-sm); color: var(--text-3); }
.card__actions { display: flex; align-items: center; gap: var(--s-1); flex: none; margin-left: auto; }
.card__body { padding: var(--card-p); min-width: 0; }
.card__footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: var(--s-2);
  padding: var(--s-3) var(--card-p);
  border-top: 1px solid var(--border);
}
.card--flush > .card__body { padding: 0; }
.card--interactive { cursor: pointer; transition: border-color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease); }
.card--interactive:hover { border-color: var(--border-strong); background: color-mix(in srgb, var(--surface) 70%, var(--surface-2)); }
.card--interactive:active { transform: scale(0.995); }

.callout {
  --c: var(--neutral); --c-soft: var(--neutral-soft); --c-border: var(--neutral-border);
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--c-border);
  border-radius: var(--r);
  background: var(--c-soft);
  color: var(--text);
  font-size: var(--fs-sm);
  min-width: 0;
}
.callout > .icon { color: var(--c); margin-top: 2px; }
.callout__content { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.callout__title { font-weight: var(--fw-semibold); }
.callout__body { color: var(--text-2); overflow-wrap: anywhere; }
.callout--info { --c: var(--info); --c-soft: var(--info-soft); --c-border: var(--info-border); }
.callout--success { --c: var(--success); --c-soft: var(--success-soft); --c-border: var(--success-border); }
.callout--warning { --c: var(--warning); --c-soft: var(--warning-soft); --c-border: var(--warning-border); }
.callout--danger { --c: var(--danger); --c-soft: var(--danger-soft); --c-border: var(--danger-border); }
.callout--accent { --c: var(--accent-fg); --c-soft: var(--accent-softer); --c-border: var(--accent-border); }
.callout--muted { --c: var(--muted); --c-soft: var(--muted-soft); --c-border: var(--muted-border); }

/* Tone variables for any element with .tone--x (page icons, modal icons, avatars) */
.tone--neutral { --tone-fg: var(--neutral); --tone-soft: var(--neutral-soft); --tone-border: var(--neutral-border); }
.tone--info { --tone-fg: var(--info); --tone-soft: var(--info-soft); --tone-border: var(--info-border); }
.tone--success { --tone-fg: var(--success); --tone-soft: var(--success-soft); --tone-border: var(--success-border); }
.tone--warning { --tone-fg: var(--warning); --tone-soft: var(--warning-soft); --tone-border: var(--warning-border); }
.tone--danger { --tone-fg: var(--danger); --tone-soft: var(--danger-soft); --tone-border: var(--danger-border); }
.tone--accent { --tone-fg: var(--accent-fg); --tone-soft: var(--accent-soft); --tone-border: var(--accent-border); }
.tone--muted { --tone-fg: var(--muted); --tone-soft: var(--muted-soft); --tone-border: var(--muted-border); }

/* ---------- Badges, chips, kbd, tags ---------- */
.badge {
  --b: var(--neutral); --b-soft: var(--neutral-soft); --b-border: var(--neutral-border);
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 20px;
  max-width: 100%;
  padding: 0 7px;
  border: 1px solid var(--b-border);
  border-radius: 6px;
  background: var(--b-soft);
  color: var(--b);
  font-size: 11.5px;
  font-weight: var(--fw-medium);
  line-height: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: middle;
}
.badge__dot { width: 6px; height: 6px; flex: none; border-radius: 50%; background: currentColor; }
.badge .icon { width: 12px; height: 12px; }
.badge--info { --b: var(--info); --b-soft: var(--info-soft); --b-border: var(--info-border); }
.badge--success { --b: var(--success); --b-soft: var(--success-soft); --b-border: var(--success-border); }
.badge--warning { --b: var(--warning); --b-soft: var(--warning-soft); --b-border: var(--warning-border); }
.badge--danger { --b: var(--danger); --b-soft: var(--danger-soft); --b-border: var(--danger-border); }
.badge--accent { --b: var(--accent-fg); --b-soft: var(--accent-soft); --b-border: var(--accent-border); }
.badge--muted { --b: var(--text-3); --b-soft: var(--muted-soft); --b-border: var(--muted-border); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: var(--ctl-h-sm);
  padding: 0 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-full);
  color: var(--text-2);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  white-space: nowrap;
  flex: none;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.chip:hover { background: var(--surface-hover); color: var(--text); }
.chip.is-active { background: var(--accent-soft); border-color: var(--accent-border); color: var(--text); }
.chip.is-active .icon { color: var(--accent-fg); }
.chip .icon { width: 14px; height: 14px; }
.chip__count {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--r-full);
  background: var(--surface-3);
  color: var(--text-2);
  font-size: 10.5px;
  line-height: 18px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.chips { display: flex; gap: var(--s-2); overflow-x: auto; scrollbar-width: none; padding: 1px; }
.chips::-webkit-scrollbar { display: none; }

.kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
  color: var(--text-3);
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: var(--fw-medium);
  line-height: 1;
}
.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--r-xs);
  background: var(--surface-2);
  color: var(--text-2);
  font-family: var(--font-mono);
  font-size: 11px;
  white-space: nowrap;
}
.tag .icon { width: 12px; height: 12px; }

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: var(--s-1);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
  min-width: 0;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  height: 40px;
  padding: 0 var(--s-3);
  color: var(--text-2);
  font-size: 13.5px;
  font-weight: var(--fw-medium);
  white-space: nowrap;
  flex: none;
  transition: color var(--dur-fast) var(--ease);
}
.tab .icon { width: 15px; height: 15px; color: var(--text-3); }
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--text); }
.tab.is-active .icon { color: var(--accent-fg); }
.tab.is-active::after {
  content: '';
  position: absolute;
  left: var(--s-2);
  right: var(--s-2);
  bottom: -1px;
  height: 2px;
  border-radius: 2px 2px 0 0;
  background: var(--accent);
}
.tab__count {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--r-full);
  background: var(--surface-3);
  color: var(--text-2);
  font-size: 10.5px;
  line-height: 18px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* ---------- Tables ---------- */
.table-wrap { position: relative; overflow: auto; min-width: 0; max-width: 100%; -webkit-overflow-scrolling: touch; }
.table {
  width: 100%;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.table th, .table td {
  padding: var(--cell-py) var(--cell-px);
  text-align: left;
  white-space: nowrap;
  vertical-align: middle;
  border-bottom: 1px solid var(--border);
}
.table td { max-width: 420px; overflow: hidden; text-overflow: ellipsis; color: var(--text); }
.table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface);
  color: var(--text-3);
  font-size: 12px;
  font-weight: var(--fw-medium);
  box-shadow: inset 0 -1px 0 var(--border);
  border-bottom: 0;
}
.table th.is-sortable { cursor: pointer; user-select: none; }
.table th.is-sortable:hover { color: var(--text); }
.table th.is-sorted { color: var(--text); }
.table .th__inner { display: inline-flex; align-items: center; gap: 4px; }
.table .th__sort { width: 12px; height: 12px; opacity: 0; }
.table th.is-sorted .th__sort { opacity: 1; color: var(--accent-fg); }
.table th.is-sortable:hover .th__sort { opacity: 0.6; }
.table tbody tr { transition: background var(--dur-fast) var(--ease); }
.table tbody tr:hover { background: color-mix(in srgb, var(--surface-hover) 60%, transparent); }
.table tbody tr:last-child td { border-bottom: 0; }
.table tr.is-clickable { cursor: pointer; }
.table tr.is-clickable:hover { background: var(--surface-hover); }
.table .num, .table [data-align="right"] { text-align: right; }
.table .col-actions { width: 1%; padding-block: 0; text-align: right; }
.table .col-index { width: 1%; color: var(--text-3); text-align: right; }
.table--dense { --cell-py: 6px; font-size: 12.5px; }

/* Datatable wrapper (ui/datatable.js) */
.dt { display: flex; flex-direction: column; min-width: 0; }
.dt__toolbar {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  flex-wrap: wrap;
  padding: var(--s-2) var(--s-3);
  border-bottom: 1px solid var(--border);
  font-size: var(--fs-sm);
  color: var(--text-3);
}
.dt__toolbar .input-group { flex: 1 1 200px; max-width: 320px; }
.dt__more { display: flex; justify-content: center; padding: var(--s-3); border-top: 1px solid var(--border); }
.dt--framed { border: 1px solid var(--border); border-radius: var(--r); background: var(--surface); overflow: hidden; }

.dt-cards { display: flex; flex-direction: column; gap: var(--s-2); padding: var(--s-2); }
.dt-card {
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface);
  min-width: 0;
}
.dt-card.is-clickable { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.dt-card.is-clickable:active { background: var(--surface-hover); }
.dt-card__head { display: flex; align-items: flex-start; gap: var(--s-2); min-width: 0; }
.dt-card__title { flex: 1; min-width: 0; font-weight: var(--fw-semibold); font-size: var(--fs-base); overflow-wrap: anywhere; }
.dt-card__rows {
  display: grid;
  grid-template-columns: minmax(88px, 38%) minmax(0, 1fr);
  gap: 6px var(--s-3);
  font-size: var(--fs-sm);
}
.dt-card__label { color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dt-card__value { min-width: 0; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }

.cell-link { color: var(--accent-fg); border-radius: var(--r-xs); }
.cell-link:hover { text-decoration: underline; text-underline-offset: 2px; }
.id-chip {
  display: inline-flex;
  align-items: center;
  height: 20px;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-2);
  color: var(--text-2);
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1;
  cursor: copy;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.id-chip:hover { color: var(--text); border-color: var(--border-strong); }
.id-chip--copied { background: var(--success-soft); border-color: var(--success-border); color: var(--success); }
.bool {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 5px;
  vertical-align: middle;
}
.bool .icon { width: 12px; height: 12px; stroke-width: 2.5; }
.bool--yes { background: var(--success-soft); color: var(--success); }
.bool--no { background: var(--muted-soft); color: var(--text-3); }
.json-inline {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--text-2);
  white-space: nowrap;
}

/* ---------- Feedback ---------- */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--s-2);
  padding: var(--s-12) var(--s-6);
  min-width: 0;
}
.empty__icon {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  margin-bottom: var(--s-2);
  border-radius: 14px;
  background: var(--surface-2);
  color: var(--text-3);
  box-shadow: inset 0 0 0 1px var(--border);
}
.empty__icon .icon { width: 22px; height: 22px; }
.empty__title { font-size: var(--fs-md); font-weight: var(--fw-semibold); color: var(--text); }
.empty__text { font-size: var(--fs-base); color: var(--text-2); max-width: 52ch; }
.empty__action { margin-top: var(--s-3); display: flex; gap: var(--s-2); flex-wrap: wrap; justify-content: center; }
.empty--compact { padding: var(--s-8) var(--s-4); }

.skeleton {
  display: block;
  border-radius: var(--r-xs);
  background: linear-gradient(90deg, var(--surface-2) 0%, var(--surface-3) 40%, var(--surface-2) 80%);
  background-size: 300% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}
.skeleton--line { height: 12px; }
.skeleton--block { height: 120px; border-radius: var(--r); }
.skeleton-stack { display: flex; flex-direction: column; gap: var(--s-3); padding: var(--s-1) 0; }
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }

.loading-inline { display: inline-flex; align-items: center; gap: var(--s-2); color: var(--text-3); font-size: var(--fs-sm); padding: var(--s-2) 0; }
.loading-inline .spinner { width: 14px; height: 14px; }

.error-box {
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
  padding: var(--s-4);
  border: 1px solid var(--danger-border);
  border-radius: var(--r);
  background: var(--danger-soft);
  min-width: 0;
}
.error-box__head { display: flex; align-items: center; gap: var(--s-2); color: var(--danger); font-weight: var(--fw-semibold); }
.error-box__message {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.error-box__actions { display: flex; gap: var(--s-2); flex-wrap: wrap; }

.progress { height: 4px; border-radius: var(--r-full); background: var(--surface-3); overflow: hidden; }
.progress__bar { height: 100%; border-radius: inherit; background: var(--accent); transition: width var(--dur) var(--ease); }

/* ---------- Facts ---------- */
.facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr));
  gap: var(--s-4) var(--s-6);
  min-width: 0;
}
.fact { display: flex; flex-direction: column; gap: var(--s-1); min-width: 0; }
.fact__label { font-size: 12px; color: var(--text-3); }
.fact__value { font-size: 13.5px; color: var(--text); overflow-wrap: anywhere; min-width: 0; }

/* ---------- Menus / popovers ---------- */
.popover {
  position: fixed;
  z-index: calc(var(--z-palette) + 2);
  min-width: 200px;
  max-width: min(340px, calc(100vw - 16px));
  max-height: min(60vh, 480px);
  overflow-x: hidden;
  overflow-y: auto;
  padding: var(--s-1);
  border: 1px solid var(--border-strong);
  border-radius: var(--r);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-lg);
  transform-origin: top left;
  animation: pop-in var(--dur-fast) var(--ease);
}
.popover.is-above { transform-origin: bottom left; }
@keyframes pop-in { from { opacity: 0; transform: scale(0.97) translateY(-2px); } }
.menu { display: flex; flex-direction: column; min-width: 0; }
.menu__item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 32px;
  padding: 0 10px;
  border-radius: 6px;
  color: var(--text);
  font-size: 13.5px;
  text-align: left;
  white-space: nowrap;
}
.menu__item .icon { color: var(--text-3); }
.menu__item:hover, .menu__item.is-active { background: var(--surface-hover); }
.menu__item:focus-visible { box-shadow: inset 0 0 0 2px var(--accent-border); border-radius: 6px; }
.menu__label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.menu__hint { flex: none; margin-left: auto; padding-left: var(--s-3); color: var(--text-3); font-size: 12px; }
/* Item with a second line (e.g. why an action is unavailable): the text wraps, icon/hint stay on top. */
.menu__item--desc { align-items: flex-start; padding-block: 7px; white-space: normal; }
.menu__item--desc > .icon, .menu__item--desc > .menu__hint { margin-top: 1px; }
.menu__text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.menu__text > .menu__label { white-space: nowrap; }
.menu__desc { font-size: 12px; line-height: 1.35; color: var(--text-3); }
/* Menus that explain items get room so reasons wrap in 2–3 lines instead of a narrow column. */
.popover:has(.menu__item--desc) { min-width: min(340px, calc(100vw - 16px)); }
.menu__check { color: var(--accent-fg) !important; }
.menu__swatch { width: 10px; height: 10px; margin: 0 3px; flex: none; border-radius: 3px; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.15); }
.menu__item--danger, .menu__item--danger .icon { color: var(--danger); }
.menu__item--danger:hover { background: var(--danger-soft); }
.menu__item.is-disabled { cursor: not-allowed; }
.menu__item.is-disabled > .icon, .menu__item.is-disabled .menu__label, .menu__item.is-disabled > .menu__hint { opacity: 0.5; }
.menu__item.is-disabled:hover { background: transparent; }
.menu__sep { height: 1px; margin: var(--s-1) calc(var(--s-1) * -1); background: var(--border); }
.menu__header {
  padding: var(--s-2) 10px var(--s-1);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-3);
}
.menu--sheet .menu__item { min-height: 48px; font-size: var(--fs-md); padding: 0 var(--s-3); }
.menu--sheet .menu__item .icon { width: 18px; height: 18px; }
.menu--sheet .menu__item--desc { padding-block: var(--s-2); }
.menu--sheet .menu__desc { font-size: var(--fs-sm); }

/* ---------- Overlays: modal / sheet / drawer ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--s-6);
}
.overlay__backdrop {
  position: absolute;
  inset: 0;
  background: var(--overlay);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  animation: fade-in var(--dur) var(--ease);
}
.modal {
  --modal-w: 560px;
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(100%, var(--modal-w));
  max-height: calc(100dvh - 48px);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: modal-in var(--dur) var(--ease);
}
.modal--sm { --modal-w: 420px; }
.modal--md { --modal-w: 560px; }
.modal--lg { --modal-w: 760px; }
.modal--xl { --modal-w: 1040px; }
.modal__header {
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
  padding: var(--s-5) var(--s-5) var(--s-3);
}
.modal__icon {
  width: 36px;
  height: 36px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 10px;
  color: var(--tone-fg, var(--accent-fg));
  background: var(--tone-soft, var(--accent-soft));
  box-shadow: inset 0 0 0 1px var(--tone-border, var(--accent-border));
}
.modal__icon .icon { width: 18px; height: 18px; }
.modal__titles { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; padding-top: 1px; }
.modal__title { font-size: var(--fs-lg); font-weight: var(--fw-semibold); letter-spacing: -0.01em; overflow-wrap: anywhere; }
.modal__subtitle { font-size: var(--fs-sm); color: var(--text-2); }
.modal__close { margin: -6px -8px 0 0; }
.modal__body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--s-2) var(--s-5) var(--s-5);
}
.modal__header[hidden] + .modal__body { padding-top: var(--s-5); }
.modal__footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: var(--s-2);
  padding: var(--s-3) var(--s-5);
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 60%, var(--bg-elevated));
}
.modal__footer:empty { display: none; }
.modal__footer .spacer { min-width: 0; }
.modal__progress {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  overflow: hidden;
  opacity: 0;
  transition: opacity var(--dur-fast) var(--ease);
}
.modal__progress::after {
  content: '';
  position: absolute;
  inset: 0;
  width: 40%;
  background: var(--accent-gradient);
  animation: indeterminate 1.1s var(--ease) infinite;
}
.modal.is-busy .modal__progress { opacity: 1; }
.modal.is-busy .modal__body { pointer-events: none; opacity: 0.75; transition: opacity var(--dur) var(--ease); }
@keyframes indeterminate { from { transform: translateX(-100%); } to { transform: translateX(260%); } }
.sheet__handle { display: none; }

/* Bottom sheet (phones) */
.overlay--sheet { align-items: flex-end; padding: 0; }
.modal--sheet {
  width: 100%;
  max-width: none;
  max-height: 92dvh;
  border-radius: 16px 16px 0 0;
  border-bottom: 0;
  padding-bottom: var(--safe-bottom);
  animation: sheet-in var(--dur-slow) var(--ease);
  touch-action: pan-y;
}
.modal--sheet .sheet__handle {
  display: block;
  flex: none;
  width: 100%;
  height: 20px;
  padding-top: 8px;
  cursor: grab;
  touch-action: none;
}
.modal--sheet .sheet__handle::before {
  content: '';
  display: block;
  width: 36px;
  height: 4px;
  margin: 0 auto;
  border-radius: 4px;
  background: var(--border-strong);
}
.modal--sheet .modal__header { padding-top: var(--s-2); }
.modal--sheet .modal__footer { padding: var(--s-3) var(--s-4); }
.modal--sheet .modal__footer > .btn { flex: 1 1 auto; }
.modal--sheet .modal__footer > .spacer { display: none; }
.modal--sheet .modal__body { padding-inline: var(--s-4); }
.modal--sheet .modal__header { padding-inline: var(--s-4); }
.modal--sheet.is-dragging { transition: none; animation: none; }
.modal--sheet-full { height: calc(100dvh - var(--safe-top) - 12px); max-height: none; }

/* Side drawers */
.overlay--drawer { justify-content: flex-end; align-items: stretch; padding: 0; }
.overlay--drawer-left { justify-content: flex-start; }
.modal--drawer {
  --modal-w: 520px;
  width: min(100vw, var(--modal-w));
  max-height: none;
  height: 100dvh;
  border-radius: 0;
  border-width: 0 0 0 1px;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  padding-right: var(--safe-right);
  animation: drawer-in-right var(--dur-slow) var(--ease);
}
.overlay--drawer-left .modal--drawer {
  border-width: 0 1px 0 0;
  padding-right: 0;
  padding-left: var(--safe-left);
  animation: drawer-in-left var(--dur-slow) var(--ease);
}
.modal--drawer .modal__header { border-bottom: 1px solid var(--border); padding-bottom: var(--s-4); }
.modal--drawer .modal__body { padding-top: var(--s-4); }

/* Closing states */
.overlay.is-closing { pointer-events: none; }
.overlay.is-closing .overlay__backdrop { animation: fade-out var(--dur-fast) var(--ease) forwards; }
.overlay.is-closing .modal { animation: modal-out var(--dur-fast) var(--ease) forwards; }
.overlay.is-closing .modal--sheet { animation: sheet-out var(--dur) var(--ease) forwards; }
.overlay.is-closing .modal--drawer { animation: drawer-out-right var(--dur) var(--ease) forwards; }
.overlay.is-closing.overlay--drawer-left .modal--drawer { animation: drawer-out-left var(--dur) var(--ease) forwards; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes modal-in { from { opacity: 0; transform: translateY(6px) scale(0.985); } }
@keyframes modal-out { to { opacity: 0; transform: translateY(4px) scale(0.985); } }
@keyframes sheet-in { from { transform: translateY(100%); } }
@keyframes sheet-out { to { transform: translateY(100%); } }
@keyframes drawer-in-right { from { transform: translateX(100%); } }
@keyframes drawer-out-right { to { transform: translateX(100%); } }
@keyframes drawer-in-left { from { transform: translateX(-100%); } }
@keyframes drawer-out-left { to { transform: translateX(-100%); } }

/* Confirm dialog body */
.confirm__message { color: var(--text-2); font-size: var(--fs-base); overflow-wrap: anywhere; }
.confirm__message strong { color: var(--text); }

/* Key/value impact list used in dialogs */
.kv {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: var(--s-2) var(--s-4);
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface);
  font-size: var(--fs-sm);
}
.kv dt { color: var(--text-3); }
.kv dd { color: var(--text); overflow-wrap: anywhere; min-width: 0; }

/* ---------- Toasts ---------- */
.toasts {
  position: fixed;
  right: calc(var(--s-4) + var(--safe-right));
  bottom: calc(var(--s-4) + var(--safe-bottom));
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--s-2);
  width: min(400px, calc(100vw - 2 * var(--s-4)));
  pointer-events: none;
}
.toast {
  --t: var(--neutral); --t-soft: var(--neutral-soft);
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
  width: 100%;
  padding: var(--s-3) var(--s-3) var(--s-3) var(--s-4);
  border: 1px solid var(--border-strong);
  border-radius: var(--r);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
  animation: toast-in var(--dur) var(--ease);
}
.toast__icon {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  flex: none;
  border-radius: 50%;
  background: var(--t-soft);
  color: var(--t);
}
.toast__icon .icon { width: 13px; height: 13px; stroke-width: 2.25; }
.toast__content { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; padding-top: 2px; }
.toast__title { font-size: 13.5px; font-weight: var(--fw-semibold); color: var(--text); }
.toast__message { font-size: 13px; color: var(--text-2); overflow-wrap: anywhere; }
.toast__title + .toast__message { color: var(--text-2); }
.toast__content > .toast__message:only-child { color: var(--text); }
.toast__actions { display: flex; align-items: center; gap: var(--s-1); flex: none; }
.toast--info { --t: var(--info); --t-soft: var(--info-soft); }
.toast--success { --t: var(--success); --t-soft: var(--success-soft); }
.toast--warning { --t: var(--warning); --t-soft: var(--warning-soft); }
.toast--danger { --t: var(--danger); --t-soft: var(--danger-soft); border-color: var(--danger-border); }
.toast--accent { --t: var(--accent-fg); --t-soft: var(--accent-soft); }
.toast.is-leaving { animation: toast-out var(--dur-fast) var(--ease) forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
@keyframes toast-out { to { opacity: 0; transform: translateX(12px); } }
@media (max-width: 767px) {
  .toasts {
    left: calc(var(--s-2) + var(--safe-left));
    right: calc(var(--s-2) + var(--safe-right));
    bottom: calc(var(--tabbar-h) + var(--safe-bottom) + var(--s-2));
    width: auto;
    align-items: stretch;
  }
  body:not(.has-shell) .toasts { bottom: calc(var(--safe-bottom) + var(--s-2)); }
}

/* ---------- Command palette ---------- */
.palette {
  position: fixed;
  inset: 0;
  z-index: var(--z-palette);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 12vh var(--s-4) var(--s-4);
}
.palette__panel {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(640px, 100%);
  max-height: min(72vh, 620px);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: modal-in var(--dur) var(--ease);
}
.palette__input-row {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  height: 54px;
  padding: 0 var(--s-4);
  border-bottom: 1px solid var(--border);
  flex: none;
}
.palette__input-row > .icon { width: 18px; height: 18px; color: var(--text-3); }
.palette__input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: var(--fs-md);
}
.palette__input:focus, .palette__input:focus-visible { outline: none; box-shadow: none; }
.palette__input::placeholder { color: var(--text-3); }
.palette__list { flex: 1; min-height: 0; overflow-y: auto; padding: var(--s-1) var(--s-2) var(--s-2); overscroll-behavior: contain; }
.palette__group {
  padding: var(--s-3) var(--s-2) var(--s-1);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-3);
}
.palette__item {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  width: 100%;
  min-height: 44px;
  padding: var(--s-1) var(--s-2);
  border-radius: var(--r-sm);
  text-align: left;
  color: var(--text);
}
.palette__item.is-active { background: var(--surface-hover); }
.palette__item-icon {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  flex: none;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text-2);
  box-shadow: inset 0 0 0 1px var(--border);
}
.palette__item.is-active .palette__item-icon { color: var(--accent-fg); background: var(--accent-softer); box-shadow: inset 0 0 0 1px var(--accent-border); }
.palette__item-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.palette__item-title { font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette__item-sub { font-size: 12px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette__item-meta { display: flex; align-items: center; gap: var(--s-2); flex: none; color: var(--text-3); font-size: 12px; }
.palette__item-enter { opacity: 0; }
.palette__item.is-active .palette__item-enter { opacity: 1; }
.palette__empty { padding: var(--s-10) var(--s-4); text-align: center; color: var(--text-3); font-size: var(--fs-sm); }
.palette__footer {
  display: flex;
  align-items: center;
  gap: var(--s-4);
  padding: var(--s-2) var(--s-4);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-3);
  flex: none;
}
.palette__footer span { display: inline-flex; align-items: center; gap: 6px; }
@media (max-width: 767px) {
  .palette { padding: 0; }
  .palette__panel {
    width: 100%;
    max-height: none;
    height: 100dvh;
    border-radius: 0;
    border: 0;
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
  }
  .palette__input-row { height: 56px; padding: 0 var(--s-2) 0 var(--s-4); }
  .palette__footer { display: none; }
  .palette__item { min-height: 52px; }
}

/* ---------- Async picker (ref fields) ---------- */
.picker { position: relative; min-width: 0; }
.picker__selected {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  min-height: var(--ctl-h);
  padding: var(--s-1) var(--s-1) var(--s-1) var(--s-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--bg);
  min-width: 0;
}
.picker__selected-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.picker__selected-title { font-size: var(--fs-base); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker__selected-sub { font-size: 11.5px; color: var(--text-3); font-family: var(--font-mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.listbox {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 5;
  max-height: 300px;
  overflow-y: auto;
  padding: var(--s-1);
  border: 1px solid var(--border-strong);
  border-radius: var(--r);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-lg);
  animation: pop-in var(--dur-fast) var(--ease);
}
.listbox__item {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  width: 100%;
  padding: var(--s-2) 10px;
  border-radius: 6px;
  text-align: left;
  min-height: 40px;
}
.listbox__item:hover, .listbox__item.is-active { background: var(--surface-hover); }
.listbox__text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.listbox__title { font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.listbox__sub { font-size: 12px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.listbox__empty { padding: var(--s-3); color: var(--text-3); font-size: var(--fs-sm); text-align: center; }

/* ---------- Code, SQL, JSON ---------- */
.code {
  margin: 0;
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--bg);
  color: var(--text-2);
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.6;
  white-space: pre;
  overflow: auto;
  max-height: 420px;
  tab-size: 2;
}
.sql-kw { color: var(--accent-fg); font-weight: var(--fw-medium); }
.sql-str { color: var(--success); }
.sql-num { color: var(--warning); }
.sql-com { color: var(--text-3); font-style: italic; }
.sql-param { color: var(--info); }

.json-tree { font-family: var(--font-mono); font-size: 12.5px; line-height: 1.65; color: var(--text); min-width: 0; overflow-wrap: anywhere; }
.json-tree details { padding-left: 0; }
.json-tree details > summary { list-style: none; display: inline-flex; align-items: center; gap: 4px; color: var(--text-3); border-radius: 4px; }
.json-tree details > summary::-webkit-details-marker { display: none; }
.json-tree details > summary::before {
  content: '';
  width: 0;
  height: 0;
  border-left: 4px solid currentColor;
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  transition: transform var(--dur-fast) var(--ease);
}
.json-tree details[open] > summary::before { transform: rotate(90deg); }
.json-tree__children { padding-left: 16px; border-left: 1px dashed var(--border); margin-left: 3px; }
.json-tree__row { display: flex; gap: 6px; min-width: 0; }
.json-key { color: var(--info); flex: none; }
.json-str { color: var(--success); }
.json-num { color: var(--warning); }
.json-bool { color: var(--accent-fg); }
.json-null { color: var(--text-3); font-style: italic; }
.json-meta { color: var(--text-3); }
