/* ═══════════════════════════════════════════════════════════════════
   QuantSage Shared Components — canonical design primitives.

   Extracted from the best-of-breed patterns in universe.css + symbol.css
   and rewritten to be token-driven. All page CSS should compose these
   instead of redefining cards, headers, KPI cells, etc.

   Naming: everything here is prefixed `qs-` to avoid collision with the
   older platform-wide `c-*` system in /css/components.css (which is not
   currently used by any QS page).

   Rules:
   - No hard-coded colors / spacing. Use tokens from tokens.css.
   - Light theme first. Dark mode additions live at the bottom.
   - Mobile-first: defaults render cleanly at 375px.
   - Additive only — existing pages keep working unchanged. Opt in by
     using `.qs-*` classes when authoring new HTML or migrating a page.

   Version: 1 — 2026-04-11
   ═══════════════════════════════════════════════════════════════════ */

/* ═══════════════════════════════════════════════════════════════════
   1. PAGE HEADER — title + optional subtitle + actions row
   ═══════════════════════════════════════════════════════════════════ */

.qs-page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-5);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.qs-page-header-main {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 0;
  flex: 1;
}

.qs-page-title {
  font-family: var(--font-primary);
  font-size: var(--fs-2xl);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  letter-spacing: var(--ls-tight);
  line-height: var(--lh-tight);
  margin: 0;
}

.qs-page-subtitle {
  font-size: var(--fs-sm);
  font-weight: var(--fw-normal);
  color: var(--text-secondary);
  line-height: var(--lh-normal);
  margin: 0;
}

.qs-page-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

@media (max-width: 640px) {
  .qs-page-header {
    flex-direction: column;
    align-items: stretch;
    padding: var(--space-3) var(--space-4);
  }
  .qs-page-title { font-size: var(--fs-xl); }
  .qs-page-header-actions { justify-content: flex-end; }
}

/* ═══════════════════════════════════════════════════════════════════
   2. SECTION — named region within a page
   ═══════════════════════════════════════════════════════════════════ */

.qs-section {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}

.qs-section:last-child { margin-bottom: 0; }

.qs-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.qs-section-title {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-family: var(--font-primary);
  font-size: var(--fs-lg);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  line-height: var(--lh-tight);
  margin: 0;
}

.qs-section-title i,
.qs-section-title svg {
  width: 16px;
  height: 16px;
  color: var(--text-secondary);
  stroke-width: 2;
}

.qs-section-subtitle {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  line-height: var(--lh-normal);
  margin: 0;
}

/* ═══════════════════════════════════════════════════════════════════
   3. PANEL — card/panel base. Compose with .qs-kpi-card, .qs-chart-section, etc.
   ═══════════════════════════════════════════════════════════════════ */

.qs-panel {
  background: var(--bg-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
  overflow: hidden;
}

.qs-panel-body {
  padding: var(--space-4);
}

.qs-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-canvas);
}

.qs-panel-header .qs-section-title { font-size: var(--fs-md); }

.qs-panel-footer {
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-canvas);
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

/* ═══════════════════════════════════════════════════════════════════
   4. KPI GRID + KPI CARD — stat displays. One naming vocabulary.
   ═══════════════════════════════════════════════════════════════════ */

.qs-kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-3);
}

.qs-kpi-grid-2 { grid-template-columns: repeat(2, 1fr); }
.qs-kpi-grid-3 { grid-template-columns: repeat(3, 1fr); }
.qs-kpi-grid-4 { grid-template-columns: repeat(4, 1fr); }

@media (max-width: 900px) {
  .qs-kpi-grid-3,
  .qs-kpi-grid-4 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 480px) {
  .qs-kpi-grid-4 { grid-template-columns: repeat(2, 1fr); }
}

.qs-kpi-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding: var(--space-3) var(--space-4);
  background: var(--bg-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.qs-kpi-card:hover {
  border-color: var(--border-strong);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.qs-kpi-label {
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--ls-wide);
  line-height: var(--lh-tight);
}

.qs-kpi-value {
  font-family: var(--font-mono);
  font-size: var(--fs-2xl);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  line-height: var(--lh-tight);
  letter-spacing: var(--ls-tight);
}

.qs-kpi-value-sm { font-size: var(--fs-xl); }
.qs-kpi-value-lg { font-size: var(--fs-3xl); }

.qs-kpi-delta {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  line-height: var(--lh-tight);
}

.qs-kpi-delta-positive { color: var(--color-positive, #22C55E); }
.qs-kpi-delta-negative { color: var(--color-negative, #EF4444); }
.qs-kpi-delta-neutral  { color: var(--text-muted); }

.qs-kpi-subtext {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  line-height: var(--lh-tight);
}

/* Loading / unavailable state — honest nulls */
.qs-kpi-value.is-empty {
  color: var(--text-muted);
  font-weight: var(--fw-normal);
}

/* ═══════════════════════════════════════════════════════════════════
   5. EMPTY STATE — consistent no-data display
   ═══════════════════════════════════════════════════════════════════ */

.qs-empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-8) var(--space-5);
  text-align: center;
  color: var(--text-muted);
  background: var(--bg-canvas);
  border-radius: 10px;
  border: 1px dashed var(--border-subtle);
}

.qs-empty-state-icon {
  width: 40px;
  height: 40px;
  color: var(--text-muted);
  opacity: 0.6;
}

.qs-empty-state-title {
  font-size: var(--fs-md);
  font-weight: var(--fw-semibold);
  color: var(--text-secondary);
}

.qs-empty-state-description {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  max-width: 360px;
  line-height: var(--lh-normal);
}

.qs-empty-state-action { margin-top: var(--space-2); }

/* ═══════════════════════════════════════════════════════════════════
   6. CHART SECTION — chart container with header
   ═══════════════════════════════════════════════════════════════════ */

.qs-chart-section {
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
}

.qs-chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  flex-wrap: wrap;
}

.qs-chart-body {
  padding: var(--space-4);
  min-height: 280px;
  position: relative;
}

.qs-chart-body--tight { padding: var(--space-2); }

/* ═══════════════════════════════════════════════════════════════════
   7. BADGE — small status chips
   ═══════════════════════════════════════════════════════════════════ */

.qs-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  line-height: 1.4;
  border-radius: 999px;
  white-space: nowrap;
  vertical-align: middle;
}

.qs-badge-success { background: rgba(34, 197, 94, 0.1);  color: #16A34A; }
.qs-badge-warning { background: rgba(245, 158, 11, 0.1); color: #D97706; }
.qs-badge-danger  { background: rgba(239, 68, 68, 0.1);  color: #DC2626; }
.qs-badge-info    { background: rgba(59, 130, 246, 0.1); color: #2563EB; }
.qs-badge-accent  { background: var(--accent-light);    color: var(--accent); }
.qs-badge-neutral { background: var(--bg-secondary);    color: var(--text-muted); }

.qs-badge i,
.qs-badge svg { width: 10px; height: 10px; }

/* ═══════════════════════════════════════════════════════════════════
   8. BUTTON — thin wrapper around tokens. Use for QS-specific pages.
   Prefer this over reinventing .xxx-btn on every page.
   ═══════════════════════════════════════════════════════════════════ */

.qs-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  font-family: var(--font-primary);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  line-height: 1;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  user-select: none;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  white-space: nowrap;
}

.qs-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.qs-btn i,
.qs-btn svg { width: 14px; height: 14px; }

/* Primary — teal */
.qs-btn-primary {
  background: var(--accent);
  color: var(--text-inverse);
  border-color: var(--accent);
}
.qs-btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* Secondary — outline */
.qs-btn-secondary {
  background: var(--bg-panel);
  color: var(--text-primary);
  border-color: var(--border-subtle);
}
.qs-btn-secondary:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}

/* Ghost — minimal */
.qs-btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}
.qs-btn-ghost:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* Danger */
.qs-btn-danger {
  background: #DC2626;
  color: var(--text-inverse);
  border-color: #DC2626;
}
.qs-btn-danger:hover:not(:disabled) { background: #B91C1C; border-color: #B91C1C; }

/* Sizes */
.qs-btn-sm { padding: var(--space-1) var(--space-3); font-size: var(--fs-xs); }
.qs-btn-lg { padding: var(--space-3) var(--space-5); font-size: var(--fs-md); }

/* Icon-only */
.qs-btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 8px;
}

/* ═══════════════════════════════════════════════════════════════════
   9. STATS BAR — horizontal stat strip (from universe.css hero pattern)
   ═══════════════════════════════════════════════════════════════════ */

.qs-stats-bar {
  display: flex;
  align-items: stretch;
  gap: 0;
  padding: 0;
  background: var(--bg-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
  flex-shrink: 0;
}

.qs-stats-bar-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  padding: var(--space-2) var(--space-5);
  background: linear-gradient(135deg, var(--accent-light), rgba(0, 190, 140, 0.03));
  border-right: 1px solid var(--border-subtle);
  flex-shrink: 0;
  min-width: 96px;
}

.qs-stats-bar-hero-value {
  font-family: var(--font-mono);
  font-size: var(--fs-xl);
  font-weight: var(--fw-semibold);
  color: var(--accent);
  line-height: var(--lh-tight);
}

.qs-stats-bar-hero-label {
  font-size: var(--fs-2xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--ls-wide);
}

.qs-stats-bar-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.qs-stats-bar-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1-5) var(--space-4);
}

.qs-stats-bar-row + .qs-stats-bar-row {
  border-top: 1px solid var(--border-subtle);
}

/* ═══════════════════════════════════════════════════════════════════
   10. LOADING — unified skeleton primitive (thin)
   ═══════════════════════════════════════════════════════════════════ */

@keyframes qs-skeleton-pulse {
  0%, 100% { opacity: 0.55; }
  50%      { opacity: 0.9; }
}

.qs-skeleton {
  background: var(--bg-secondary);
  border-radius: 4px;
  animation: qs-skeleton-pulse 1.4s ease-in-out infinite;
}

.qs-skeleton-text  { height: 12px; width: 100%; }
.qs-skeleton-title { height: 18px; width: 60%; }
.qs-skeleton-value { height: 24px; width: 80px; }
.qs-skeleton-card  { height: 96px; width: 100%; border-radius: 10px; }

/* ═══════════════════════════════════════════════════════════════════
   11. ICON HYGIENE — Lucide-only policy + consistent sizing hierarchy.
   ═══════════════════════════════════════════════════════════════════

   Size hierarchy (match text baseline at 1em x-height):
   - 14px: inline with body text, table cells, small labels
   - 16px: section titles, sidebar filter headers
   - 18px: panel headers, card titles
   - 20px: global nav icons (already in terminal.css)

   Lucide's default is 24px — always too big for text. These rules apply
   a 14px baseline globally and upgrade in specific contexts.
*/

/* Global default: 14px for any bare Lucide SVG inside content area */
main [data-lucide],
main svg.lucide,
.context-sidebar [data-lucide],
.context-sidebar svg.lucide {
  width: 14px;
  height: 14px;
  stroke-width: 2;
  flex-shrink: 0;
  vertical-align: -0.15em; /* Align with text baseline */
}

/* Section titles / headers — 16px */
.qs-section-title [data-lucide],
.qs-section-title svg.lucide,
.qs-panel-header [data-lucide],
.qs-panel-header svg.lucide,
.filter-group-title [data-lucide],
.filter-group-title svg.lucide,
.stats-group-label [data-lucide],
.stats-group-label svg.lucide,
h1 [data-lucide], h2 [data-lucide], h3 [data-lucide],
h1 svg.lucide, h2 svg.lucide, h3 svg.lucide {
  width: 16px !important;
  height: 16px !important;
}

/* Panel-level large headings — 18px */
.qs-panel-title [data-lucide],
.qs-panel-title svg.lucide,
.section-header h2 [data-lucide],
.section-header h2 svg.lucide {
  width: 18px !important;
  height: 18px !important;
}

/* Buttons — 14px (already set in section 8, but defend against main override) */
.qs-btn [data-lucide],
.qs-btn svg.lucide,
button [data-lucide],
button svg.lucide {
  width: 14px !important;
  height: 14px !important;
}

/* Badges — 10px (already set in section 7) */
.qs-badge [data-lucide],
.qs-badge svg.lucide {
  width: 10px !important;
  height: 10px !important;
}

/* Global nav (terminal sidebar) — 20px */
.global-nav-item [data-lucide],
.global-nav-item svg.lucide,
.sidebar-toggle-btn [data-lucide],
.sidebar-close-btn [data-lucide],
.sidebar-expand-btn [data-lucide],
.sidebar-toggle-btn svg.lucide,
.sidebar-close-btn svg.lucide,
.sidebar-expand-btn svg.lucide {
  width: 20px !important;
  height: 20px !important;
}

/* Loading spinner rotation */
.qs-spin {
  animation: qs-spin 1s linear infinite;
}

@keyframes qs-spin {
  to { transform: rotate(360deg); }
}

/* Lucide always stroke-width 2 for consistency */
[data-lucide],
svg.lucide {
  stroke-width: 2;
}
/* Compact command surfaces for data-first QS workspaces. */
.qs-command-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--s-2);
  min-width: 0;
  margin: var(--s-3) 0 var(--s-4);
  padding: var(--s-2);
  border: 1px solid var(--ui-border);
  border-radius: var(--ui-radius-md);
  background: var(--ui-surface);
}

.qs-command-group {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
  min-width: 0;
  padding: 3px;
  border: 1px solid var(--ui-border);
  border-radius: var(--ui-radius-sm);
  background: var(--ui-surface-2);
}

.qs-command-label,
.qs-command-chip,
.qs-command-ai {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 6px 10px;
  border-radius: var(--ui-radius-sm);
  color: var(--ui-ink-3);
  font-size: var(--f-12);
  font-weight: 700;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
}

.qs-command-label {
  color: var(--ui-ink-4);
  font-family: var(--font-mono, "JetBrains Mono", monospace);
  text-transform: uppercase;
  letter-spacing: 0;
}

.qs-command-chip {
  border: 1px solid transparent;
  background: transparent;
  color: var(--ui-ink-2);
}

.qs-command-chip[data-active="true"] {
  border-color: color-mix(in srgb, var(--ui-accent) 42%, var(--ui-border));
  background: color-mix(in srgb, var(--ui-accent) 9%, var(--ui-surface));
  color: var(--ui-ink);
}

.qs-command-ai {
  margin-left: auto;
  border: 1px solid color-mix(in srgb, var(--ui-accent) 42%, var(--ui-border));
  background: color-mix(in srgb, var(--ui-accent) 8%, var(--ui-surface));
  color: var(--ui-ink);
}

.qs-command-ai svg {
  width: 15px;
  height: 15px;
}

.qs-data-canvas {
  border: 1px solid var(--ui-border);
  border-radius: var(--ui-radius-md);
  background: var(--ui-surface);
  overflow: hidden;
}

.qs-data-canvas-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  min-width: 0;
  padding: var(--s-3);
  border-bottom: 1px solid var(--ui-border);
  background: var(--ui-surface-2);
}

.qs-data-canvas-title {
  margin: 0;
  color: var(--ui-ink);
  font-size: var(--f-14);
  line-height: 1.25;
}

.qs-data-canvas-meta {
  color: var(--ui-ink-3);
  font-family: var(--font-mono, "JetBrains Mono", monospace);
  font-size: var(--f-11);
}

@media (max-width: 720px) {
  .qs-command-bar {
    align-items: stretch;
  }

  .qs-command-group,
  .qs-command-ai {
    width: 100%;
  }

  .qs-command-ai {
    justify-content: center;
    margin-left: 0;
  }
}
