/* VendorIQ design system — core components (Phase 3, 18/11/2026)
 * Every class starts with ds- so it never collides with the older stylesheets while
 * screens move over one at a time (Phase 5). Uses only tokens from tokens.css.
 * Catalogue and usage: public/ds/README.md · live page: /design-system.html
 */

/* ── Base (only inside a ds page/region) ──────────────────────────────────── */
.ds-root, .ds-root * { box-sizing: border-box; }
.ds-root {
  font-family: var(--ds-font); font-size: var(--ds-text-md); line-height: var(--ds-leading);
  color: var(--ds-text); background: var(--ds-bg);
}
.ds-root :focus-visible { outline: 3px solid var(--ds-focus); outline-offset: 2px; }
.ds-num, .ds-root td.r, .ds-root .money { font-family: var(--ds-font-num); font-variant-numeric: tabular-nums; }
.ds-sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* ── Page layout ──────────────────────────────────────────────────────────── */
.ds-page { max-width: 1440px; margin: 0 auto; padding: var(--ds-space-6) var(--ds-space-6) var(--ds-space-12); }
.ds-page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: var(--ds-space-4); margin-bottom: var(--ds-space-6); }
.ds-page-title { font-size: var(--ds-text-xl); font-weight: var(--ds-weight-semibold); line-height: var(--ds-leading-tight); margin: 0; }
.ds-page-sub { color: var(--ds-text-muted); font-size: var(--ds-text-sm); margin: var(--ds-space-1) 0 0; }
.ds-grid { display: grid; gap: var(--ds-space-4); grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.ds-stack { display: flex; flex-direction: column; gap: var(--ds-space-4); }
.ds-row { display: flex; flex-wrap: wrap; align-items: center; gap: var(--ds-space-2); }
@media (max-width: 640px) { .ds-page { padding: var(--ds-space-4) var(--ds-gutter) var(--ds-space-10); } }

/* ── Button ───────────────────────────────────────────────────────────────── */
.ds-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--ds-space-2);
  height: var(--ds-control-h); padding: 0 var(--ds-space-4); border-radius: var(--ds-radius-sm);
  border: 1px solid transparent; font: inherit; font-size: var(--ds-text-sm); font-weight: var(--ds-weight-semibold);
  cursor: pointer; white-space: nowrap; text-decoration: none;
  transition: background var(--ds-fast) var(--ds-ease), border-color var(--ds-fast) var(--ds-ease), box-shadow var(--ds-fast) var(--ds-ease);
  background: var(--ds-primary); color: var(--ds-on-primary);
}
.ds-btn:hover { background: var(--ds-primary-hover); }
.ds-btn--secondary { background: var(--ds-surface); color: var(--ds-text); border-color: var(--ds-border); }
.ds-btn--secondary:hover { background: var(--ds-surface-2); }
.ds-btn[aria-pressed="true"] { background: var(--ds-accent-soft); border-color: var(--ds-accent); color: var(--ds-accent-text); box-shadow: inset 0 0 0 1px var(--ds-accent); }
.ds-btn--ghost { background: transparent; color: var(--ds-text); }
.ds-btn--ghost:hover { background: var(--ds-surface-2); }
.ds-btn--danger { background: var(--ds-danger-fill); color: var(--ds-on-danger); }
.ds-btn--danger:hover { background: var(--ds-danger); }
.ds-btn--sm { height: var(--ds-control-h-sm); padding: 0 var(--ds-space-3); font-size: var(--ds-text-xs); }
.ds-btn--icon { width: var(--ds-control-h); padding: 0; }
.ds-btn[disabled], .ds-btn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; }
.ds-btn[aria-busy="true"] { position: relative; color: transparent !important; }
.ds-btn[aria-busy="true"]::after {
  content: ''; position: absolute; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--ds-on-primary); border-right-color: transparent; animation: ds-spin .7s linear infinite;
}
.ds-btn--secondary[aria-busy="true"]::after, .ds-btn--ghost[aria-busy="true"]::after { border-color: var(--ds-text); border-right-color: transparent; }
@media (pointer: coarse) { .ds-btn { min-height: var(--ds-touch); } }

/* ── Form fields: input, select, date ─────────────────────────────────────── */
.ds-field { display: flex; flex-direction: column; gap: var(--ds-space-1); min-width: 0; }
.ds-label { font-size: var(--ds-text-sm); font-weight: var(--ds-weight-medium); color: var(--ds-text-2); }
.ds-hint { font-size: var(--ds-text-xs); color: var(--ds-text-muted); }
.ds-error { font-size: var(--ds-text-xs); color: var(--ds-danger); font-weight: var(--ds-weight-medium); }
.ds-input, .ds-select, .ds-date {
  height: var(--ds-control-h); width: 100%; padding: 0 var(--ds-space-3);
  border: 1px solid var(--ds-border); border-radius: var(--ds-radius-sm);
  background: var(--ds-surface); color: var(--ds-text); font: inherit; font-size: var(--ds-text-md);
}
textarea.ds-input { height: auto; min-height: 88px; padding: var(--ds-space-2) var(--ds-space-3); resize: vertical; }
.ds-select { appearance: none; padding-right: var(--ds-space-8);
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }
.ds-input:focus-visible, .ds-select:focus-visible, .ds-date:focus-visible { outline: 3px solid var(--ds-focus); outline-offset: 0; border-color: var(--ds-focus); }
.ds-input[aria-invalid="true"], .ds-select[aria-invalid="true"], .ds-date[aria-invalid="true"] { border-color: var(--ds-danger); }
.ds-input::placeholder { color: var(--ds-text-muted); }
.ds-date { font-family: var(--ds-font-num); }
.ds-date-wrap { position: relative; display: flex; gap: var(--ds-space-2); }
.ds-date-wrap .ds-date { flex: 1; min-width: 0; }

/* ── Card ─────────────────────────────────────────────────────────────────── */
.ds-card { background: var(--ds-surface); border: 1px solid var(--ds-divider); border-radius: var(--ds-radius); box-shadow: var(--ds-shadow-sm); padding: var(--ds-space-5); min-width: 0; }
.ds-card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--ds-space-3); margin-bottom: var(--ds-space-4); }
.ds-card-title { font-size: var(--ds-text-lg); font-weight: var(--ds-weight-semibold); margin: 0; }

/* ── KPI tile (with sparkline) ────────────────────────────────────────────── */
.ds-kpi { position: relative; display: flex; flex-direction: column; gap: var(--ds-space-1); padding: var(--ds-space-4) var(--ds-space-5);
  background: var(--ds-surface); border: 1px solid var(--ds-divider); border-radius: var(--ds-radius); min-width: 0; text-decoration: none; color: inherit; }
a.ds-kpi:hover, button.ds-kpi:hover { border-color: var(--ds-accent); }
button.ds-kpi { font: inherit; text-align: left; cursor: pointer; width: 100%; color: var(--ds-text); }
button.ds-kpi:focus-visible, a.ds-kpi:focus-visible { outline: 3px solid var(--ds-focus); outline-offset: 2px; }
.ds-kpi .ds-kpi-value { color: var(--ds-text); }
.ds-kpi-label { font-size: var(--ds-text-xs); font-weight: var(--ds-weight-semibold); text-transform: uppercase; letter-spacing: .04em; color: var(--ds-text-muted); }
.ds-kpi-value { font-family: var(--ds-font-num); font-size: var(--ds-text-xl); font-weight: var(--ds-weight-semibold); line-height: var(--ds-leading-tight); }
.ds-kpi-meta { display: flex; align-items: center; gap: var(--ds-space-2); font-size: var(--ds-text-xs); color: var(--ds-text-muted); }
.ds-kpi-delta { font-family: var(--ds-font-num); font-weight: var(--ds-weight-semibold); }
.ds-kpi-delta--up { color: var(--ds-success); }
.ds-kpi-delta--down { color: var(--ds-danger); }
.ds-kpi-spark { display: block; width: 100%; height: 32px; margin-top: auto; padding-top: var(--ds-space-2); color: var(--ds-accent); }

/* ── Progress ─────────────────────────────────────────────────────────────── */
.ds-progress { display: flex; flex-direction: column; gap: var(--ds-space-1); }
.ds-progress-track { position: relative; height: 8px; border-radius: var(--ds-radius-pill); background: var(--ds-surface-2); overflow: hidden; }
.ds-progress-bar { height: 100%; border-radius: inherit; background: var(--ds-accent); transition: width var(--ds-slow) var(--ds-ease); }
.ds-progress-marker { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ds-text); }
.ds-progress--green .ds-progress-bar { background: var(--ds-success); }
.ds-progress--amber .ds-progress-bar { background: var(--ds-warning-fill); }
.ds-progress--red .ds-progress-bar { background: var(--ds-danger-fill); }
.ds-progress-text { display: flex; justify-content: space-between; font-size: var(--ds-text-xs); color: var(--ds-text-muted); }

/* ── Badge (status words always shown with colour) ────────────────────────── */
.ds-badge { display: inline-flex; align-items: center; gap: var(--ds-space-1); height: 22px; padding: 0 var(--ds-space-2);
  border-radius: var(--ds-radius-pill); font-size: var(--ds-text-xs); font-weight: var(--ds-weight-semibold); white-space: nowrap;
  background: var(--ds-surface-2); color: var(--ds-text-2); }
.ds-badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.ds-badge--success { background: var(--ds-success-soft); color: var(--ds-success); }
.ds-badge--warning { background: var(--ds-warning-soft); color: var(--ds-warning); }
.ds-badge--danger  { background: var(--ds-danger-soft);  color: var(--ds-danger); }
.ds-badge--info    { background: var(--ds-info-soft);    color: var(--ds-info); }
.ds-badge--accent  { background: var(--ds-accent-soft);  color: var(--ds-accent-text); }

/* ── Table ────────────────────────────────────────────────────────────────── */
.ds-table-wrap { overflow-x: auto; border: 1px solid var(--ds-divider); border-radius: var(--ds-radius); background: var(--ds-surface); }
.ds-table { width: 100%; border-collapse: collapse; font-size: var(--ds-text-sm); }
/* text-transform / letter-spacing / min-width reset: the older stylesheets style every th/td on a page */
.ds-table th { position: sticky; top: 0; z-index: var(--ds-z-sticky); text-align: left; font-weight: var(--ds-weight-semibold); font-size: var(--ds-text-xs);
  text-transform: none; letter-spacing: normal; min-width: 0;
  color: var(--ds-text-2); background: var(--ds-surface-2); padding: var(--ds-space-2) var(--ds-space-3); border-bottom: 1px solid var(--ds-divider); white-space: nowrap; }
.ds-table td { padding: var(--ds-space-2) var(--ds-space-3); border-bottom: 1px solid var(--ds-divider); vertical-align: middle; min-width: 0; color: var(--ds-text); background: transparent; }
.ds-table td.r { white-space: nowrap; }
/* Legacy guard (remove when style.css / ciq.css are retired): those set every table to min-width 640px and every
   thead th with !important (background, colour, 10px uppercase, padding), which breaks ds tables inside drawers. */
table.ds-table { min-width: 0; }
.ds-table thead th {
  background: var(--ds-surface-2) !important; color: var(--ds-text-2) !important; font-size: var(--ds-text-xs) !important;
  font-weight: var(--ds-weight-semibold) !important; letter-spacing: normal !important; text-transform: none !important;
  padding: var(--ds-space-2) var(--ds-space-3) !important; border-bottom: 1px solid var(--ds-divider) !important; font-family: var(--ds-font) !important;
}
.ds-table--dense thead th { padding: 6px var(--ds-space-2) !important; }
/* Stacked table on phones: each row becomes a card; td[data-label] shows its column name */
@media (max-width: 640px) {
  .ds-table--stack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .ds-table--stack tr { display: block; padding: var(--ds-space-2) 0; border-bottom: 1px solid var(--ds-divider); }
  .ds-table--stack tr:last-child { border-bottom: 0; }
  .ds-table--stack td { display: flex; gap: var(--ds-space-3); justify-content: space-between; align-items: flex-start; border: 0; padding: 4px var(--ds-space-3); text-align: left; }
  .ds-table--stack td[data-label]::before { content: attr(data-label); flex: 0 0 38%; font-size: var(--ds-text-xs); font-weight: var(--ds-weight-semibold); color: var(--ds-text-muted); font-family: var(--ds-font); text-align: left; }
  .ds-table--stack td > * { text-align: right; }
}
.ds-table tr:last-child td { border-bottom: 0; }
.ds-table tbody tr:hover { background: var(--ds-accent-soft); }
.ds-table tr[aria-selected="true"] { background: var(--ds-accent-soft); }
.ds-table .r { text-align: right; font-family: var(--ds-font-num); font-variant-numeric: tabular-nums; }
.ds-table th.r { text-align: right; font-family: inherit; }
.ds-table th button { all: unset; cursor: pointer; display: inline-flex; gap: var(--ds-space-1); }
.ds-table th button:focus-visible { outline: 3px solid var(--ds-focus); }
.ds-table th[aria-sort="ascending"] button::after { content: '▲'; font-size: .6rem; }
.ds-table th[aria-sort="descending"] button::after { content: '▼'; font-size: .6rem; }
.ds-table--dense td, .ds-table--dense th { padding: 6px var(--ds-space-2); }
.ds-bulkbar { display: none; align-items: center; gap: var(--ds-space-3); padding: var(--ds-space-2) var(--ds-space-3);
  background: var(--ds-primary); color: var(--ds-on-primary); border-radius: var(--ds-radius-sm); margin-bottom: var(--ds-space-2); }
.ds-bulkbar.is-on { display: flex; }
.ds-bulkbar .ds-btn { background: var(--ds-surface); color: var(--ds-text); }

/* ── Tabs ─────────────────────────────────────────────────────────────────── */
.ds-tabs { display: flex; gap: var(--ds-space-1); border-bottom: 1px solid var(--ds-divider); overflow-x: auto; margin-bottom: var(--ds-space-4); }
.ds-tab { all: unset; cursor: pointer; padding: var(--ds-space-2) var(--ds-space-3); font-size: var(--ds-text-sm); font-weight: var(--ds-weight-medium);
  color: var(--ds-text-muted); border-bottom: 3px solid transparent; white-space: nowrap; }
.ds-tab:hover { color: var(--ds-text); }
.ds-tab[aria-selected="true"] { color: var(--ds-text); border-bottom-color: var(--ds-accent); font-weight: var(--ds-weight-semibold); }
.ds-tab:focus-visible { outline: 3px solid var(--ds-focus); outline-offset: -3px; }
.ds-tab .ds-count { margin-left: var(--ds-space-1); font-family: var(--ds-font-num); font-size: var(--ds-text-xs); color: var(--ds-text-muted); }

/* ── Drawer (side panel instead of pop-up windows) ────────────────────────── */
.ds-drawer-overlay { position: fixed; inset: 0; background: var(--ds-overlay); z-index: var(--ds-z-drawer); opacity: 0; transition: opacity var(--ds-base) var(--ds-ease); }
.ds-drawer-overlay.is-open { opacity: 1; }
.ds-drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100vw); z-index: calc(var(--ds-z-drawer) + 1);
  display: flex; flex-direction: column; background: var(--ds-surface-raised); color: var(--ds-text); box-shadow: var(--ds-shadow-lg);
  transform: translateX(100%); transition: transform var(--ds-base) var(--ds-ease); }
.ds-drawer.is-open { transform: none; }
.ds-drawer--wide { width: min(880px, 100vw); }
.ds-drawer-head { display: flex; align-items: center; justify-content: space-between; gap: var(--ds-space-3); padding: var(--ds-space-4) var(--ds-space-5); border-bottom: 1px solid var(--ds-divider); }
.ds-drawer-title { font-size: var(--ds-text-lg); font-weight: var(--ds-weight-semibold); margin: 0; }
.ds-drawer-body { flex: 1; overflow-y: auto; padding: var(--ds-space-5); }
.ds-drawer-foot { display: flex; justify-content: flex-end; gap: var(--ds-space-2); padding: var(--ds-space-3) var(--ds-space-5); border-top: 1px solid var(--ds-divider); }

/* ── Toast (with optional Undo) ───────────────────────────────────────────── */
.ds-toasts { position: fixed; right: var(--ds-space-4); bottom: var(--ds-space-4); z-index: var(--ds-z-toast); display: flex; flex-direction: column; gap: var(--ds-space-2); max-width: min(420px, calc(100vw - 32px)); }
.ds-toast { display: flex; align-items: center; gap: var(--ds-space-3); padding: var(--ds-space-3) var(--ds-space-4); border-radius: var(--ds-radius-sm);
  background: var(--ds-primary); color: var(--ds-on-primary); box-shadow: var(--ds-shadow-lg); font-size: var(--ds-text-sm); animation: ds-rise var(--ds-base) var(--ds-ease); }
.ds-toast--error { background: var(--ds-danger-fill); color: var(--ds-on-danger); }
.ds-toast-msg { flex: 1; }
.ds-toast button { all: unset; cursor: pointer; font-weight: var(--ds-weight-semibold); text-decoration: underline; padding: 2px 4px; }
.ds-toast button:focus-visible { outline: 2px solid currentColor; }

/* ── Empty state ──────────────────────────────────────────────────────────── */
.ds-empty { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--ds-space-2); padding: var(--ds-space-10) var(--ds-space-4); color: var(--ds-text-muted); }
.ds-empty-title { font-size: var(--ds-text-md); font-weight: var(--ds-weight-semibold); color: var(--ds-text); margin: 0; }
.ds-empty p { margin: 0; max-width: 46ch; font-size: var(--ds-text-sm); }

/* ── Skeleton (loading placeholder) ───────────────────────────────────────── */
.ds-skeleton { display: block; border-radius: var(--ds-radius-sm); min-height: 12px;
  background: linear-gradient(90deg, var(--ds-skeleton) 25%, var(--ds-skeleton-shine) 50%, var(--ds-skeleton) 75%); background-size: 200% 100%;
  animation: ds-shimmer 1.4s linear infinite; }
.ds-skeleton--line { height: 12px; margin: 6px 0; }
.ds-skeleton--kpi { height: 96px; border-radius: var(--ds-radius); }
.ds-skeleton--row { height: 36px; margin-bottom: 4px; }

/* ── Masked personal information (POPIA) ──────────────────────────────────── */
.ds-masked { font-family: var(--ds-font-num); letter-spacing: .04em; }
.ds-reveal { all: unset; cursor: pointer; margin-left: var(--ds-space-2); font-size: var(--ds-text-xs); color: var(--ds-link); text-decoration: underline; }
.ds-reveal:focus-visible { outline: 2px solid var(--ds-focus); }

/* ── Command search (Ctrl+K) ──────────────────────────────────────────────── */
.ds-cmdk-overlay { position: fixed; inset: 0; z-index: var(--ds-z-cmdk); background: var(--ds-overlay); display: flex; justify-content: center; align-items: flex-start; padding-top: 12vh; }
.ds-cmdk { width: min(640px, calc(100vw - 32px)); background: var(--ds-surface-raised); color: var(--ds-text); border-radius: var(--ds-radius-lg); box-shadow: var(--ds-shadow-lg); overflow: hidden; }
.ds-cmdk-input { width: 100%; height: 52px; border: 0; border-bottom: 1px solid var(--ds-divider); padding: 0 var(--ds-space-5); font: inherit; font-size: var(--ds-text-lg); background: transparent; color: inherit; }
.ds-cmdk-input:focus-visible { outline: none; }
.ds-cmdk-list { list-style: none; margin: 0; padding: var(--ds-space-2); max-height: 50vh; overflow-y: auto; }
.ds-cmdk-item { display: flex; align-items: center; justify-content: space-between; gap: var(--ds-space-3); padding: var(--ds-space-2) var(--ds-space-3); border-radius: var(--ds-radius-sm); cursor: pointer; }
.ds-cmdk-item[aria-selected="true"] { background: var(--ds-accent-soft); }
.ds-cmdk-item small { color: var(--ds-text-muted); font-size: var(--ds-text-xs); }
.ds-cmdk-empty { padding: var(--ds-space-6); text-align: center; color: var(--ds-text-muted); }
.ds-kbd { font-family: var(--ds-font-num); font-size: var(--ds-text-xs); padding: 1px 6px; border: 1px solid var(--ds-border); border-radius: 4px; color: var(--ds-text-2); }

/* ── Motion ───────────────────────────────────────────────────────────────── */
@keyframes ds-spin { to { transform: rotate(360deg); } }
@keyframes ds-shimmer { to { background-position: -200% 0; } }
@keyframes ds-rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .ds-skeleton, .ds-btn[aria-busy="true"]::after, .ds-toast { animation: none; }
  .ds-drawer, .ds-drawer-overlay, .ds-progress-bar { transition: none; }
}

@media print {
  .ds-drawer, .ds-drawer-overlay, .ds-toasts, .ds-cmdk-overlay, .ds-bulkbar { display: none !important; }
  .ds-card, .ds-kpi, .ds-table-wrap { box-shadow: none; break-inside: avoid; }
}
