/* Components — layout shell, sidebar, topbar, page header, cards, kpi tiles,
   badges, alerts, buttons, forms, tables, empty states, toolbar, skeletons.
   Aligned with design-system.md §6 + §7. */

/* ============================================================
   Layout shell
   ============================================================ */

.layout-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 100vh;
  background: var(--color-canvas);
}
.layout-shell.is-collapsed {
  grid-template-columns: var(--sidebar-width-collapsed) 1fr;
}
.main-column {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.page {
  flex: 1;
  padding: var(--space-8) var(--space-8) var(--space-12);
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
  box-sizing: border-box;
}

@media (max-width: 1024px) {
  .layout-shell {
    grid-template-columns: 1fr;
  }
  .layout-shell .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform var(--duration) var(--easing);
    box-shadow: var(--shadow-lg);
  }
  .layout-shell.is-mobile-open .sidebar {
    transform: translateX(0);
  }
  .layout-shell.is-mobile-open::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(15, 17, 22, 0.40);
    z-index: 40;
  }
  .page {
    padding: var(--space-5);
  }
}

/* Auth shell (login) */
.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: var(--color-canvas);
  padding: var(--space-8);
}

/* ============================================================
   Sidebar
   ============================================================ */

.sidebar {
  background: var(--color-surface-muted);
  display: flex;
  flex-direction: column;
  padding: var(--space-4) var(--space-3);
  gap: var(--space-2);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  margin-bottom: var(--space-4);
  font-weight: 700;
  font-size: 15px;
  color: var(--color-text);
  text-decoration: none;
}
.sidebar-brand:hover { text-decoration: none; color: var(--color-text); }
.sidebar-brand-mark {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  background: var(--color-primary);
  color: white;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
  flex: 0 0 auto;
}
.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.sidebar-section-label {
  font-size: var(--text-caption-size);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-subtle);
  padding: var(--space-3) var(--space-3) var(--space-1);
}
.sidebar a.sidebar-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text);
  font-size: var(--text-ui-size);
  font-weight: 500;
  text-decoration: none;
  min-height: 36px;
  transition: background var(--duration) var(--easing), color var(--duration) var(--easing);
  white-space: nowrap;
}
.sidebar a.sidebar-link:hover {
  background: var(--color-surface);
  color: var(--color-text);
  text-decoration: none;
}
.sidebar a.sidebar-link.is-active {
  background: var(--color-primary-soft);
  color: var(--color-primary-soft-text);
}
.sidebar a.sidebar-link.is-active .sidebar-icon { color: var(--color-primary-soft-text); }
.sidebar-icon {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
  color: var(--color-text-muted);
}
.sidebar a.sidebar-link:hover .sidebar-icon { color: var(--color-text); }

.sidebar-footer {
  margin-top: auto;
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border-subtle);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* Collapsed state */
.layout-shell.is-collapsed .sidebar { padding: var(--space-4) var(--space-2); }
.layout-shell.is-collapsed .sidebar-brand-text,
.layout-shell.is-collapsed .sidebar-link-label,
.layout-shell.is-collapsed .sidebar-section-label {
  display: none;
}
.layout-shell.is-collapsed .sidebar a.sidebar-link {
  justify-content: center;
  padding: var(--space-2);
}

/* ============================================================
   Topbar
   ============================================================ */

.topbar {
  height: var(--topbar-height);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border-subtle);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-6);
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-title {
  font-size: var(--text-ui-size);
  font-weight: 600;
  color: var(--color-text);
}
.topbar-spacer { flex: 1; }
.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.topbar-iconbutton {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  display: grid;
  place-items: center;
  background: transparent;
  border: 0;
  cursor: pointer;
  color: var(--color-text-muted);
  transition: background var(--duration) var(--easing), color var(--duration) var(--easing);
}
.topbar-iconbutton:hover {
  background: var(--color-surface-muted);
  color: var(--color-text);
}

/* User dropdown */
.user-menu {
  position: relative;
}
.user-menu-trigger {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-pill);
  color: var(--color-text);
  font-size: var(--text-ui-size);
  font-weight: 500;
  cursor: pointer;
  height: 36px;
}
.user-menu-trigger:hover {
  background: var(--color-surface-muted);
}
.user-menu-avatar {
  width: 24px;
  height: 24px;
  border-radius: var(--radius-pill);
  background: var(--color-primary-soft);
  color: var(--color-primary-soft-text);
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}
.user-menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 200px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: var(--space-2);
  z-index: 60;
  display: none;
}
.user-menu.is-open .user-menu-panel { display: block; }
.user-menu-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  width: 100%;
  background: transparent;
  border: 0;
  border-radius: var(--radius-md);
  color: var(--color-text);
  font-size: var(--text-ui-size);
  font-weight: 500;
  text-align: left;
  cursor: pointer;
}
.user-menu-item:hover { background: var(--color-surface-muted); }
.user-menu-divider {
  height: 1px;
  background: var(--color-border-subtle);
  margin: var(--space-1) 0;
}
.user-menu-meta {
  padding: var(--space-2) var(--space-3) var(--space-1);
  font-size: var(--text-caption-size);
  color: var(--color-text-subtle);
}

/* ============================================================
   Page header
   ============================================================ */

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--color-border-subtle);
}
.page-header-title {
  font-size: var(--text-display-size);
  line-height: var(--text-display-lh);
  letter-spacing: -0.4px;
  font-weight: 700;
  margin: 0 0 var(--space-1);
  color: var(--color-text);
}
.page-header-subtitle {
  font-size: var(--text-body-size);
  color: var(--color-text-muted);
  margin: 0;
}
.page-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}
.page-header-tabs {
  display: flex;
  gap: var(--space-1);
  margin-top: var(--space-3);
}
.page-tab {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-size: var(--text-ui-size);
  font-weight: 500;
  text-decoration: none;
  border: 0;
  background: transparent;
  cursor: pointer;
}
.page-tab:hover { background: var(--color-surface-muted); color: var(--color-text); text-decoration: none; }
.page-tab.is-active {
  background: var(--color-primary-soft);
  color: var(--color-primary-soft-text);
}

/* ============================================================
   Cards
   ============================================================ */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-5);
  margin: 0;
}
.card + .card { margin-top: var(--space-4); }
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin: 0 0 var(--space-4);
}
.card-title {
  font-size: var(--text-h2-size);
  line-height: var(--text-h2-lh);
  font-weight: 600;
  margin: 0;
  color: var(--color-text);
}
.card-subtitle {
  font-size: var(--text-caption-size);
  color: var(--color-text-muted);
  margin: var(--space-1) 0 0;
}
.card-actions {
  display: flex;
  gap: var(--space-2);
  align-items: center;
}
.card-footer {
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--color-border-subtle);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
}
.card.is-flush { padding: 0; }
.card.is-flush .card-header { padding: var(--space-4) var(--space-5) 0; margin-bottom: var(--space-3); }

/* Override Pico's <article> so existing markup picks up the new look. */
article {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-5);
  margin: 0 0 var(--space-4);
}
article > header {
  margin: 0 0 var(--space-4);
  padding: 0 0 var(--space-3);
  border-bottom: 1px solid var(--color-border-subtle);
  background: transparent;
}
article > footer {
  margin: var(--space-5) 0 0;
  padding: var(--space-4) 0 0;
  border-top: 1px solid var(--color-border-subtle);
  background: transparent;
}
article header strong {
  font-size: var(--text-caption-size);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  font-weight: 600;
}

/* ============================================================
   KPI tile
   ============================================================ */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: var(--space-4);
  margin: 0 0 var(--space-6);
}
.kpi-grid.is-charts {
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
}
.kpi-tile {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-4) var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 0;
}
.kpi-label {
  font-size: var(--text-caption-size);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  margin: 0;
}
.kpi-value {
  font-size: var(--text-display-size);
  line-height: var(--text-display-lh);
  font-weight: 700;
  letter-spacing: -0.4px;
  font-variant-numeric: tabular-nums;
  color: var(--color-text);
  margin: 0;
}
.kpi-value.is-success { color: var(--color-success-text); }
.kpi-value.is-warning { color: var(--color-warning-text); }
.kpi-value.is-danger { color: var(--color-danger-text); }
.kpi-value.is-muted { color: var(--color-text-muted); }
.kpi-meta {
  font-size: var(--text-caption-size);
  color: var(--color-text-subtle);
  margin: 0;
  display: flex;
  align-items: center;
  gap: var(--space-1);
}
.kpi-delta {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-size: var(--text-caption-size);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.kpi-delta.is-up { background: var(--color-success-soft); color: var(--color-success-text); }
.kpi-delta.is-down { background: var(--color-danger-soft); color: var(--color-danger-text); }
.kpi-delta.is-flat { background: var(--color-surface-muted); color: var(--color-text-muted); }

/* ============================================================
   Badges (soft variant only — design-system §6.4)
   ============================================================ */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-size: var(--text-caption-size);
  line-height: var(--text-caption-lh);
  font-weight: 600;
  background: var(--color-surface-muted);
  color: var(--color-text-muted);
  white-space: nowrap;
}
.badge-success { background: var(--color-success-soft); color: var(--color-success-text); }
.badge-info { background: var(--color-info-soft); color: var(--color-info-text); }
.badge-warning { background: var(--color-warning-soft); color: var(--color-warning-text); }
.badge-danger { background: var(--color-danger-soft); color: var(--color-danger-text); }
.badge-primary { background: var(--color-primary-soft); color: var(--color-primary-soft-text); }
.badge-neutral { background: var(--color-surface-muted); color: var(--color-text-muted); }
.badge-violet { background: var(--color-violet-soft); color: var(--color-violet-text); }
/* Track badges: stable aliases that downstream code keys off the policy track */
.badge-track-margin { background: var(--color-primary-soft); color: var(--color-primary-soft-text); }
.badge-track-shipping { background: var(--color-violet-soft); color: var(--color-violet-text); }

/* ============================================================
   Track filter (chip group used on /reorder, /exports, drilldowns
   to scope a Tabulator/table by policy track)
   ============================================================ */

.track-filter {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  background: var(--color-surface-sunken);
  padding: 3px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--color-border-subtle);
}
.track-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 4px var(--space-3);
  border-radius: var(--radius-pill);
  font-size: var(--text-caption-size);
  line-height: var(--text-caption-lh);
  font-weight: 600;
  color: var(--color-text-muted);
  background: transparent;
  border: 0;
  text-decoration: none;
  cursor: pointer;
  transition: background var(--duration) var(--easing),
              color var(--duration) var(--easing);
  white-space: nowrap;
}
.track-chip:hover {
  background: var(--color-surface);
  color: var(--color-text);
  text-decoration: none;
}
.track-chip .count {
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  color: inherit;
  opacity: 0.7;
  margin-left: var(--space-1);
}
.track-chip.is-active {
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: var(--shadow-sm);
}
.track-chip.is-active.track-chip-margin {
  background: var(--color-primary-soft);
  color: var(--color-primary-soft-text);
}
.track-chip.is-active.track-chip-shipping {
  background: var(--color-violet-soft);
  color: var(--color-violet-text);
}
.badge::before {
  content: "";
}
.badge.has-dot::before {
  width: 6px;
  height: 6px;
  border-radius: var(--radius-pill);
  display: inline-block;
}
.badge-success.has-dot::before { background: var(--color-success-accent); }
.badge-info.has-dot::before { background: var(--color-info-accent); }
.badge-warning.has-dot::before { background: var(--color-warning-accent); }
.badge-danger.has-dot::before { background: var(--color-danger-accent); }

/* ============================================================
   Alerts (rail + soft fill — design-system §2.4 anatomy)
   ============================================================ */

.alert {
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4) var(--space-3) var(--space-5);
  border-radius: var(--radius-md);
  background: var(--color-surface-muted);
  color: var(--color-text);
  margin: 0 0 var(--space-4);
}
.alert::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  border-radius: var(--radius-md) 0 0 var(--radius-md);
  background: var(--color-text-muted);
}
.alert-success { background: var(--color-success-soft); color: var(--color-success-text); }
.alert-success::before { background: var(--color-success-accent); }
.alert-info    { background: var(--color-info-soft); color: var(--color-info-text); }
.alert-info::before { background: var(--color-info-accent); }
.alert-warning { background: var(--color-warning-soft); color: var(--color-warning-text); }
.alert-warning::before { background: var(--color-warning-accent); }
.alert-danger  { background: var(--color-danger-soft); color: var(--color-danger-text); }
.alert-danger::before { background: var(--color-danger-accent); }

.alert-critical {
  border: 1px solid;
}
.alert-danger.alert-critical { border-color: var(--color-danger-accent); }
.alert-warning.alert-critical { border-color: var(--color-warning-accent); }

.alert-icon {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  margin-top: 2px;
}
.alert-icon svg { width: 18px; height: 18px; }
.alert-success .alert-icon { color: var(--color-success-accent); }
.alert-info .alert-icon    { color: var(--color-info-accent); }
.alert-warning .alert-icon { color: var(--color-warning-accent); }
.alert-danger .alert-icon  { color: var(--color-danger-accent); }
.alert-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.alert-title { font-weight: 600; font-size: var(--text-ui-size); }
.alert-message { font-size: var(--text-body-size); line-height: var(--text-body-lh); color: inherit; opacity: 0.95; }

/* ============================================================
   Buttons (override Pico defaults)
   ============================================================ */

/* Base button visual reset — wrapped in :where() so specificity is 0,0,0
   and any utility class (.topbar-iconbutton, .sidebar-link, .btn-ghost, …)
   can override colors without specificity battles. */
:where(button, [role="button"], .btn,
       input[type="submit"], input[type="button"], input[type="reset"]) {
  font-family: var(--font-sans);
  font-size: var(--text-button-size);
  line-height: var(--text-button-lh);
  font-weight: 600;
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-4);
  border: 1px solid transparent;
  background: var(--color-primary);
  color: white;
  cursor: pointer;
  transition: background var(--duration) var(--easing), color var(--duration) var(--easing), border-color var(--duration) var(--easing);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 36px;
  width: auto;
}
/* Width override of Pico's `button[type=submit]{width:100%}` (specificity 0,1,1).
   `:not(.btn-block)` bumps us to 0,2,1 so we win and `.btn-block` opt-in stays. */
button[type="submit"]:not(.btn-block),
button[type="button"]:not(.btn-block),
button[type="reset"]:not(.btn-block),
input[type="submit"]:not(.btn-block) {
  width: auto;
}
button:hover, .btn:hover { background: var(--color-primary-hover); color: white; }
button:disabled, .btn:disabled,
button[aria-busy="true"], .btn[aria-busy="true"] {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-solid {
  background: var(--color-primary);
  color: white;
  border-color: transparent;
}
.btn-solid:hover { background: var(--color-primary-hover); color: white; }

.btn-soft {
  background: var(--color-primary-soft);
  color: var(--color-primary-soft-text);
  border-color: transparent;
}
.btn-soft:hover { background: var(--color-primary-soft); color: var(--color-primary-soft-text); filter: brightness(0.97); }

.btn-outline {
  background: transparent;
  color: var(--color-text);
  border-color: var(--color-border);
}
.btn-outline:hover {
  background: var(--color-surface-muted);
  border-color: var(--color-border-strong);
  color: var(--color-text);
}

.btn-ghost {
  background: transparent;
  color: var(--color-text);
  border-color: transparent;
  padding: var(--space-2) var(--space-3);
}
.btn-ghost:hover { background: var(--color-surface-muted); color: var(--color-text); }

.btn-link {
  background: transparent;
  color: var(--color-primary);
  border-color: transparent;
  padding: 0;
  min-height: auto;
  text-decoration: none;
}
.btn-link:hover { color: var(--color-primary-hover); background: transparent; text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 3px; }

.btn-danger {
  background: var(--color-danger-accent);
  color: white;
  border-color: transparent;
}
.btn-danger:hover { background: var(--color-danger-text); color: white; }

.btn-sm { font-size: 13px; padding: 4px 10px; min-height: 28px; }
.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  min-height: 32px;
}
.btn-block { width: 100%; }

/* PicoCSS class compatibility */
button.contrast { background: var(--color-text); color: var(--color-canvas); }
button.contrast:hover { background: var(--color-text-muted); color: var(--color-canvas); }
button.secondary {
  background: transparent;
  color: var(--color-text);
  border: 1px solid var(--color-border);
}
button.secondary:hover { background: var(--color-surface-muted); color: var(--color-text); }

/* ============================================================
   Forms
   ============================================================ */

.form-grid {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: 1fr;
}
.form-grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.form-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 768px) {
  .form-grid.cols-2, .form-grid.cols-3 { grid-template-columns: 1fr; }
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.form-field label,
.form-field > .form-label {
  font-size: var(--text-ui-size);
  line-height: var(--text-ui-lh);
  font-weight: 500;
  color: var(--color-text);
  margin: 0;
}
.form-field .form-help {
  font-size: var(--text-caption-size);
  color: var(--color-text-muted);
  margin: 0;
}
.form-field.has-error .form-help { color: var(--color-danger-text); }
.form-field.is-required > label::after {
  content: "•";
  color: var(--color-danger-accent);
  margin-left: 4px;
}

input:not([type="range"]):not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]),
select,
textarea {
  background: var(--color-surface-sunken);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3);
  font-family: var(--font-sans);
  font-size: var(--text-body-size);
  line-height: var(--text-body-lh);
  width: 100%;
  box-sizing: border-box;
  min-height: 36px;
  transition: border-color var(--duration) var(--easing), box-shadow var(--duration) var(--easing);
}
input::placeholder, textarea::placeholder { color: var(--color-text-subtle); }
input:focus, select:focus, textarea:focus {
  border-color: var(--color-primary);
  outline: none;
  box-shadow: 0 0 0 2px var(--color-primary-soft);
}
.form-field.has-error input,
.form-field.has-error select,
.form-field.has-error textarea {
  background: var(--color-danger-soft);
  border-color: var(--color-danger-accent);
}

textarea { min-height: 96px; resize: vertical; }

/* Range input (slider) */
input[type="range"] {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  background: transparent;
  height: 28px;
  padding: 0;
  border: 0;
  min-height: auto;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  background: var(--color-surface-sunken);
  border-radius: var(--radius-pill);
  border: 1px solid var(--color-border);
}
input[type="range"]::-moz-range-track {
  height: 6px;
  background: var(--color-surface-sunken);
  border-radius: var(--radius-pill);
  border: 1px solid var(--color-border);
}
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: var(--radius-pill);
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  box-shadow: var(--shadow-sm);
  margin-top: -7px;
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: var(--radius-pill);
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}

/* Fieldset */
fieldset {
  border: 0;
  padding: 0;
  margin: 0;
}
fieldset legend {
  font-size: var(--text-h3-size);
  font-weight: 600;
  margin: 0 0 var(--space-2);
  color: var(--color-text);
  padding: 0;
}

/* Checkbox/radio */
input[type="checkbox"], input[type="radio"] {
  accent-color: var(--color-primary);
  width: 16px;
  height: 16px;
  margin: 0;
}
.form-check {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-ui-size);
  font-weight: 500;
  color: var(--color-text);
  cursor: pointer;
  user-select: none;
}

/* Output */
output {
  font-family: var(--font-sans);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--color-text);
}

/* ============================================================
   Tables (native — Tabulator skin in integrations.css)
   ============================================================ */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-body-size);
}
table th, table td {
  text-align: left;
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--color-border-subtle);
  vertical-align: middle;
}
table th {
  font-size: var(--text-caption-size);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
  background: transparent;
  border-bottom: 1px solid var(--color-border);
  width: auto;
}
table tbody tr:hover { background: var(--color-surface-muted); }
table.tnum td { font-variant-numeric: tabular-nums; }
table .num { text-align: right; font-variant-numeric: tabular-nums; }

/* ============================================================
   Toolbar
   ============================================================ */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  margin: 0 0 var(--space-4);
}
.toolbar > * { flex: 0 0 auto; }
.toolbar .toolbar-spacer { flex: 1; }
.toolbar .form-field { flex: 1 1 220px; max-width: 320px; }

/* ============================================================
   Empty state
   ============================================================ */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-12) var(--space-6);
  gap: var(--space-3);
}
.empty-state .empty-icon {
  width: 32px; height: 32px;
  color: var(--color-text-subtle);
}
.empty-state h2 { margin: 0; }
.empty-state p { margin: 0; color: var(--color-text-muted); max-width: 420px; }

/* ============================================================
   Skeletons / loading
   ============================================================ */

.skeleton {
  background: linear-gradient(90deg,
    var(--color-surface-muted) 0%,
    var(--color-surface-sunken) 50%,
    var(--color-surface-muted) 100%);
  background-size: 200% 100%;
  animation: skeleton-pulse 1.4s linear infinite;
  border-radius: var(--radius-sm);
  display: block;
}
@keyframes skeleton-pulse {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

.htmx-indicator {
  opacity: 0;
  transition: opacity var(--duration) var(--easing);
}
.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
  opacity: 1;
}

/* Spinner used inside indicators */
.spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: var(--radius-pill);
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================
   Misc helpers
   ============================================================ */

.cluster { display: flex; align-items: center; flex-wrap: wrap; gap: var(--space-3); }
.stack { display: flex; flex-direction: column; gap: var(--space-3); }
.stack-tight { display: flex; flex-direction: column; gap: var(--space-2); }
.stack-loose { display: flex; flex-direction: column; gap: var(--space-6); }
.between { justify-content: space-between; }
.right { margin-left: auto; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--space-4); }
@media (max-width: 768px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

.divider { height: 1px; background: var(--color-border-subtle); border: 0; margin: var(--space-4) 0; }

/* Chart container */
.chart {
  width: 100%;
  height: 280px;
}
.chart-lg { height: 340px; }
.chart-sm { height: 200px; }

/* Sticky footer for actions */
.sticky-actions {
  position: sticky;
  bottom: 0;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border-subtle);
  padding: var(--space-3) var(--space-4);
  margin: var(--space-6) calc(-1 * var(--space-5)) calc(-1 * var(--space-5));
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

/* Pico <hgroup> compat */
hgroup { margin: 0 0 var(--space-3); }
hgroup > :last-child { color: var(--color-text-muted); font-size: var(--text-body-size); }

/* Pico <details> dropdown override (used in user menu) */
details.dropdown summary {
  cursor: pointer;
  list-style: none;
}
details.dropdown summary::-webkit-details-marker { display: none; }

/* ============================================================
   Product page (épica 009)
   Layout: main column + sticky right rail. Collapses below 1200px.
   ============================================================ */

.product-page {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: var(--space-6);
  align-items: start;
}
@media (max-width: 1199px) {
  .product-page { grid-template-columns: minmax(0, 1fr); }
}

.product-rail {
  position: sticky;
  top: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-width: 0;
}
.product-rail .rail-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-4);
}
.product-rail .rail-cta {
  background: var(--color-primary-soft);
  border: 1px solid var(--color-primary);
  color: var(--color-primary-soft-text);
}
.product-rail .rail-cta .cta-label {
  font-size: var(--text-caption-size);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-primary-soft-text);
  margin: 0 0 var(--space-1);
}
.product-rail .rail-cta .cta-value {
  font-size: 28px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin: 0 0 var(--space-3);
  color: var(--color-text);
  line-height: 1.1;
}
.product-rail .rail-meta {
  font-size: var(--text-caption-size);
  color: var(--color-text-muted);
  margin: 0 0 var(--space-1);
}
.product-rail .rail-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--space-2);
  font-size: 13px;
  padding: 4px 0;
}
.product-rail .rail-row .rail-row-label { color: var(--color-text-muted); }
.product-rail .rail-row .rail-row-value {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* KPI strip — variant of kpi-grid for the hero band, 6 cols on wide */
.kpi-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--space-3);
  margin: 0 0 var(--space-5);
}
.kpi-strip .kpi-tile {
  padding: var(--space-3) var(--space-4);
}
.kpi-strip .kpi-value { font-size: 22px; line-height: 1.2; }

/* Hero block on product page */
.product-hero {
  margin: 0 0 var(--space-5);
}
.product-hero .breadcrumb {
  font-size: var(--text-caption-size);
  color: var(--color-text-muted);
  margin: 0 0 var(--space-2);
}
.product-hero .breadcrumb a { color: var(--color-text-muted); }
.product-hero .hero-title {
  font-size: 28px;
  line-height: 1.15;
  font-weight: 700;
  margin: 0 0 4px;
  font-variant-numeric: tabular-nums;
}
.product-hero .hero-subtitle {
  font-size: var(--text-h2-size);
  color: var(--color-text-muted);
  margin: 0 0 var(--space-3);
}
.product-hero .hero-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
  margin: 0 0 var(--space-3);
}
.product-hero .hero-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

/* Inventory ladder (inline SVG container) */
.inventory-ladder {
  width: 100%;
  height: 56px;
  margin: var(--space-2) 0 var(--space-3);
}
.inventory-ladder .ladder-track {
  fill: var(--color-surface-sunken);
  stroke: var(--color-border);
  stroke-width: 1;
}
.inventory-ladder .ladder-band-min {
  fill: var(--color-warning-soft);
}
.inventory-ladder .ladder-band-target {
  fill: var(--color-success-soft);
}
.inventory-ladder .ladder-tick {
  stroke: var(--color-text-muted);
  stroke-width: 1;
}
.inventory-ladder .ladder-tick-label {
  font-size: 10px;
  fill: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}
.inventory-ladder .ladder-marker {
  stroke-width: 2;
}
.inventory-ladder .ladder-marker-on-hand { stroke: var(--color-primary); }
.inventory-ladder .ladder-marker-position { stroke: var(--color-text); stroke-dasharray: 3 2; }
.inventory-ladder .ladder-marker-label {
  font-size: 11px;
  fill: var(--color-text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Coverage gauge (donut) */
.coverage-gauge {
  width: 120px;
  height: 120px;
  margin: 0 auto;
  display: block;
}
.coverage-gauge .gauge-track {
  fill: none;
  stroke: var(--color-surface-sunken);
  stroke-width: 12;
}
.coverage-gauge .gauge-fill {
  fill: none;
  stroke-width: 12;
  stroke-linecap: round;
  transform: rotate(-90deg);
  transform-origin: center;
}
.coverage-gauge .gauge-fill.is-success { stroke: var(--color-success-accent); }
.coverage-gauge .gauge-fill.is-warning { stroke: var(--color-warning-accent); }
.coverage-gauge .gauge-fill.is-danger  { stroke: var(--color-danger-accent); }
.coverage-gauge .gauge-value {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  fill: var(--color-text);
  text-anchor: middle;
}
.coverage-gauge .gauge-meta {
  font-size: 10px;
  fill: var(--color-text-muted);
  text-anchor: middle;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Intra-week mini bar chart (7 bars) */
.intra-week {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  align-items: end;
  height: 60px;
  margin: var(--space-2) 0;
}
.intra-week .iw-bar {
  background: var(--color-primary-soft);
  border-radius: 2px 2px 0 0;
  min-height: 2px;
  position: relative;
}
.intra-week .iw-day-label {
  font-size: 10px;
  text-align: center;
  color: var(--color-text-muted);
  margin-top: 2px;
}

/* Audit timeline */
.timeline {
  list-style: none;
  margin: 0;
  padding: 0 0 0 var(--space-4);
  position: relative;
}
.timeline::before {
  content: "";
  position: absolute;
  left: 6px;
  top: 6px;
  bottom: 6px;
  width: 1px;
  background: var(--color-border);
}
.timeline-item {
  position: relative;
  padding: 0 0 var(--space-3) var(--space-3);
}
.timeline-item::before {
  content: "";
  position: absolute;
  left: -14px;
  top: 6px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--color-surface);
  border: 2px solid var(--color-primary);
}
.timeline-item .ti-time {
  font-size: var(--text-caption-size);
  color: var(--color-text-muted);
}
.timeline-item .ti-actor { font-weight: 600; }
.timeline-item .ti-diff {
  display: block;
  font-family: var(--font-mono, monospace);
  font-size: 12px;
  background: var(--color-surface-sunken);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
  margin-top: 4px;
  white-space: pre-wrap;
}

/* Two-col policy block (inputs + outputs) */
.policy-cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-5);
}
@media (max-width: 768px) {
  .policy-cols { grid-template-columns: 1fr; }
}

/* Section anchor offset (in case we add jump links later) */
.product-page section[id] { scroll-margin-top: var(--space-6); }

/* Print styles: hide chrome, expand collapsibles, single column */
@media print {
  .sidebar, .topbar, .product-rail, .btn-link[href]::after { display: none !important; }
  .product-page { grid-template-columns: 1fr !important; }
  details { open: open; }
  details > summary { display: none; }
  .card { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
  .chart { height: 240px !important; }
}

/* ===================================================================
   Brand-mix view (feature 012)
   =================================================================== */

/* KPI tile dual: actual → propuesto + delta inline */
.kpi-tile-dual {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.kpi-tile-dual .kpi-dual-row {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  flex-wrap: wrap;
  font-variant-numeric: tabular-nums;
}
.kpi-tile-dual .kpi-dual-current {
  font-size: var(--text-body-size);
  color: var(--color-text-muted);
}
.kpi-tile-dual .kpi-dual-arrow {
  color: var(--color-text-muted);
  font-size: var(--text-body-size);
}
.kpi-tile-dual .kpi-dual-proposed {
  font-size: var(--text-h2-size, 22px);
  font-weight: 600;
  color: var(--color-text-strong);
}

/* Brand-mix two-column layout: main + sticky rail */
.brand-mix-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: var(--space-5);
  align-items: start;
}
@media (max-width: 1100px) {
  .brand-mix-layout { grid-template-columns: 1fr; }
}
.brand-mix-main { min-width: 0; }
.brand-rail {
  position: sticky;
  top: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.brand-rail .rail-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.brand-rail .rail-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-body-size);
}
.brand-rail .rail-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.brand-rail .rail-value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--color-success-strong, #2e7d32);
}
.brand-rail .rail-value-negative {
  color: var(--color-danger-strong, #c62828);
}
.brand-rail .rail-meta {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: var(--space-3);
  row-gap: var(--space-1);
  margin: 0;
  font-size: var(--text-caption-size);
}
.brand-rail .rail-meta dt { color: var(--color-text-muted); }
.brand-rail .rail-meta dd { margin: 0; font-variant-numeric: tabular-nums; }

/* Drill-down modal */
.brand-drilldown-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 1000;
  display: flex;
  justify-content: flex-end;
  align-items: stretch;
}
.brand-drilldown-modal[hidden] { display: none; }
.brand-drilldown-card {
  width: min(900px, 100%);
  background: var(--color-surface, #fff);
  padding: var(--space-5);
  overflow-y: auto;
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.15);
}
.brand-drilldown-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--color-border, #ddd);
}
.brand-drilldown-header h2 {
  margin: 0;
  font-size: var(--text-h2-size, 22px);
}
