/* ──────────────────────────────────────────────────────────────────────────────
 * CORTEX Shared Design System
 * All templates link this file. Component styles here override template-local
 * <style> blocks via CSS cascade (this file loads first, templates may append).
 * ────────────────────────────────────────────────────────────────────────────── */

/* ── Design tokens ───────────────────────────────────────────────────────────── */
:root {
  /* Brand */
  --primary:        #0b1d32;
  --primary-dark:   #07111e;
  --primary-light:  #f5f7fa;
  --primary-border: #d6dde6;

  /* Semantic */
  --success:        #16a34a;
  --success-light:  #f0fdf4;
  --success-border: #bbf7d0;
  --warning:        #d97706;
  --warning-light:  #fffbeb;
  --warning-border: #fde68a;
  --danger:         #dc2626;
  --danger-light:   #fef2f2;
  --danger-border:  #fecaca;
  --purple:         #ff6b6b;
  --purple-light:   #fff1f0;
  --purple-border:  #ffc9c9;

  /* Confidence bands */
  --band-high:      #10b981;  /* emerald-500 */
  --band-medium:    #f59e0b;  /* amber-500   */
  --band-low:       #f97316;  /* orange-500  */
  --band-critical:  #ef4444;  /* red-500     */

  /* Surfaces */
  --surface:          #ffffff;
  --surface-elevated: #f5f7fa;
  --surface-muted:    #edf2f7;
  --border:           #e1e7ef;
  --border-strong:    #cfd8e3;

  /* Text — all WCAG AA on white (contrast ≥ 4.5:1) */
  --text-primary:   #0b1d32;
  --text-secondary: #314255;
  --text-muted:     #66768a;
  --text-subtle:    #9aa7b5;

  /* Legacy aliases (keep for backward compat with existing templates) */
  --white:    #ffffff; /* was missing — sticky th using var(--white) rendered transparent */
  --gray-50:  var(--surface-elevated);
  --gray-100: var(--surface-muted);
  --gray-200: var(--border);
  --gray-300: var(--border-strong);
  --gray-500: var(--text-muted);
  --gray-600: #4b5563;
  --gray-700: var(--text-secondary);
  --gray-900: var(--text-primary);

  /* Service accents */
  --chronicle-accent: #61eed2;
  --compass-accent:   #0891b2;
  --cortex-accent:    var(--primary);

  /* Generic accent (used by dynamic JS components) */
  --accent:        var(--primary);
  --accent-light:  var(--primary-light);
  --accent-border: var(--primary-border);

  /* Aliases for templates that use these */
  --bg-card:          var(--surface-elevated);
  --card:             var(--surface);

  /* Spacing scale */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;

  /* Radii */
  --radius-sm: 4px;
  --radius:    6px;
  --radius-lg: 8px;
  --radius-xl: 12px;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0,0,0,.07);
  --shadow:    0 1px 3px rgba(0,0,0,.10), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.05);
  --shadow-ring: 0 0 0 2px var(--primary);

  /* Motion */
  --transition-fast: 100ms ease;
  --transition:      150ms ease;
  --transition-slow: 250ms ease;
}

/* ── Reset & base ────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--surface);
  color: var(--text-primary);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ──────────────────────────────────────────────────────────────────── */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--space-6);
}

/* ── Typography ──────────────────────────────────────────────────────────────── */
h1 { font-size: 1.375rem; font-weight: 700; line-height: 1.3; }
h2 { font-size: 1.0625rem; font-weight: 600; line-height: 1.4; }
h3 { font-size: 0.9375rem; font-weight: 600; }
small { font-size: 0.75rem; }
code {
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.83em;
  background: var(--surface-muted);
  border-radius: var(--radius-sm);
  padding: 0.1em 0.3em;
}

/* ── Card ────────────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: var(--space-5);
  margin-bottom: var(--space-5);
}
.card h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: var(--space-4);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.card h2 .count {
  background: var(--surface-muted);
  padding: 0.1rem 0.45rem;
  border-radius: var(--radius-full);
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
}

/* ── Buttons ─────────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  font-weight: 500;
  font-size: 0.875rem;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all var(--transition);
  text-decoration: none;
  white-space: nowrap;
  user-select: none;
}
.btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn:disabled, .btn[disabled] { opacity: 0.5; cursor: not-allowed; pointer-events: none; }

.btn-xs  { padding: 0.2rem 0.45rem;  font-size: 0.7rem; }
.btn-sm  { padding: 0.35rem 0.7rem;  font-size: 0.78rem; }
.btn-lg  { padding: 0.625rem 1.25rem; font-size: 0.95rem; }

.btn-primary   { background: var(--primary);   color: #fff; border-color: var(--primary); }
.btn-primary:hover  { background: var(--primary-dark); border-color: var(--primary-dark); }

.btn-success   { background: var(--success); color: #fff; }
.btn-success:hover  { background: #15803d; }

.btn-warning   { background: var(--warning); color: #fff; }
.btn-warning:hover  { background: #b45309; }

.btn-danger    { background: var(--danger); color: #fff; }
.btn-danger:hover   { background: #b91c1c; }

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

.btn-outline {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  color: var(--text-secondary);
}
.btn-outline:hover { background: var(--surface-elevated); border-color: var(--primary); color: var(--primary); }

/* Ghost: no background, no border — for tertiary actions */
.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}
.btn-ghost:hover { background: var(--surface-muted); color: var(--text-primary); }

/* Icon-only button — square, no padding asymmetry */
.btn-icon {
  padding: 0.4rem;
  border-radius: var(--radius);
  aspect-ratio: 1;
  justify-content: center;
}
.btn-icon.btn-sm { padding: 0.25rem; }

/* ── Badges ──────────────────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius-sm);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
/* Pill shape */
.badge-pill { border-radius: var(--radius-full); padding: 0.15rem 0.5rem; }

/* Confidence bands */
.badge-high     { background: #d1fae5; color: #065f46; }
.badge-medium   { background: #fef3c7; color: #78350f; }
.badge-low      { background: #ffedd5; color: #9a3412; }
.badge-critical { background: #fee2e2; color: #7f1d1d; }

/* Outline variant */
.badge-outline {
  background: transparent;
  border: 1px solid currentColor;
}
.badge-outline.badge-high     { color: var(--band-high); }
.badge-outline.badge-medium   { color: var(--band-medium); }
.badge-outline.badge-low      { color: var(--band-low); }
.badge-outline.badge-critical { color: var(--band-critical); }

/* ── Status badges (inherited from all templates) ────────────────────────────── */
.status-badge {
  display: inline-block;
  padding: 0.25rem 0.625rem;
  border-radius: var(--radius-sm);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.03em;
}
.status-QUEUED             { background: var(--surface-muted);  color: var(--text-secondary); }
.status-PROCESSING         { background: var(--primary-light); color: var(--primary); animation: pulse-border 1.5s ease infinite; }
.status-CANCELLED          { background: var(--border); color: var(--text-muted); text-decoration: line-through; }
.status-COMPLETED          { background: #dcfce7; color: #166534; }
.status-FAILED             { background: #fee2e2; color: #991b1b; }
.status-APPROVED           { background: #dcfce7; color: #166534; }
.status-NEEDS_REVIEW       { background: #fef3c7; color: #92400e; }
.status-PENDING_VALIDATION { background: var(--surface-muted); color: var(--text-muted); }
.status-REJECTED           { background: #fee2e2; color: #991b1b; }

@keyframes pulse-border {
  0%, 100% { box-shadow: 0 0 0 0 rgba(11,29,50,.18); }
  50%       { box-shadow: 0 0 0 3px rgba(11,29,50,.10); }
}

/* ── Data table ──────────────────────────────────────────────────────────────── */
.data-table {
  width: 100%;
  table-layout: auto;
  border-collapse: collapse;
  font-size: 0.82rem;
}
.data-table th,
.data-table td {
  word-break: break-word;
  overflow-wrap: break-word;
  max-width: 300px;
}
.data-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-elevated);
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.5rem 0.65rem;
  text-align: left;
  border-bottom: 2px solid var(--border);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.data-table thead th:hover { color: var(--text-primary); }
.data-table thead th.sort-asc::after  { content: ' ▲'; opacity: 0.7; font-size: 0.65rem; }
.data-table thead th.sort-desc::after { content: ' ▼'; opacity: 0.7; font-size: 0.65rem; }

.data-table tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background var(--transition-fast);
}
.data-table tbody tr:nth-child(even) { background: var(--surface-elevated); }
.data-table tbody tr:hover           { background: var(--primary-light); }

.data-table td {
  padding: 0.45rem 0.65rem;
  vertical-align: middle;
  color: var(--text-secondary);
}

/* Compact mode */
.data-table.compact td,
.data-table.compact th { padding: 0.3rem 0.5rem; font-size: 0.75rem; }

/* ── Table containers (safety-net horizontal scroll) ─────────────────────────── */
.table-responsive,
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ── Service matrix (13-column compact layout) ───────────────────────────────── */
.service-matrix-table th,
.service-matrix-table td {
  font-size: 0.8rem;
  padding: 0.35rem 0.5rem;
  max-width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.service-matrix-table td:hover {
  white-space: normal;
  overflow: visible;
}

/* ── Generic .table (less opinionated, used by older panels) ─────────────────── */
.table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.table th, .table td {
  border-bottom: 1px solid var(--border);
  padding: 0.45rem 0.65rem;
  text-align: left;
  vertical-align: top;
}
.table th {
  background: var(--surface-elevated);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.74rem;
}
.table tbody tr:hover { background: var(--primary-light); }

/* ── Forms ───────────────────────────────────────────────────────────────────── */
input[type="text"],
input[type="email"],
input[type="number"],
input[type="search"],
select,
textarea {
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 0.4rem 0.6rem;
  font-size: 0.875rem;
  font-family: inherit;
  color: var(--text-primary);
  background: var(--surface);
  transition: border-color var(--transition), box-shadow var(--transition);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}

/* ── Progress bar ────────────────────────────────────────────────────────────── */
.progress-bar {
  height: 8px;
  background: var(--border);
  border-radius: var(--radius-full);
  overflow: hidden;
  flex: 1;
  min-width: 150px;
}
.progress-bar-fill {
  height: 100%;
  background: var(--primary);
  border-radius: var(--radius-full);
  transition: width 0.35s ease;
}

/* ── Step tracker ────────────────────────────────────────────────────────────── */
.step-tracker {
  display: flex;
  gap: var(--space-1);
  margin-top: var(--space-4);
}
.step {
  flex: 1;
  padding: 0.5rem 0.35rem;
  text-align: center;
  font-size: 0.73rem;
  background: var(--surface-muted);
  border-radius: var(--radius);
  color: var(--text-muted);
  position: relative;
  transition: background var(--transition), color var(--transition);
}
.step.active {
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 600;
}
.step.completed {
  background: #dcfce7;
  color: #166534;
}
.step-clickable { cursor: pointer; }
.step-clickable:hover { filter: brightness(0.96); }
.step.panel-active {
  box-shadow: var(--shadow-ring);
  font-weight: 700;
}

/* ── Stat boxes ──────────────────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-4);
  margin-top: var(--space-4);
}
.stat-box {
  text-align: center;
  padding: var(--space-4);
  background: var(--surface-elevated);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.stat-box .number {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-primary);
}
.stat-box .label {
  font-size: 0.74rem;
  color: var(--text-muted);
  margin-top: var(--space-1);
}
.stat-box.approved .number { color: var(--success); }
.stat-box.review   .number { color: var(--warning); }
.stat-box.failed   .number { color: var(--danger); }

/* ── Loading spinner ─────────────────────────────────────────────────────────── */
.loading-spinner {
  display: inline-block;
  width: 13px;
  height: 13px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }
/* .loading is an alias for backward compat with inline <span class="loading"> usage in app.js */
.loading {
  display: inline-block;
  width: 13px;
  height: 13px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
  margin-right: 0.35rem;
}

/* ── Empty state ─────────────────────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 2rem var(--space-4);
  color: var(--text-muted);
  font-size: 0.875rem;
}
.empty-state svg, .empty-state .icon { display: block; margin: 0 auto 0.5rem; opacity: 0.4; }

/* ── Callout / note box ──────────────────────────────────────────────────────── */
.callout {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  border-left: 3px solid var(--warning);
  background: var(--warning-light);
  font-size: 0.8125rem;
  color: #78350f;
  margin-bottom: var(--space-3);
}
.callout.callout-info   { border-color: var(--primary); background: var(--primary-light); color: var(--primary); }
.callout.callout-success { border-color: var(--success); background: var(--success-light); color: #166534; }
.callout.callout-danger  { border-color: var(--danger);  background: var(--danger-light);  color: #991b1b; }

/* ── Evidence box ────────────────────────────────────────────────────────────── */
.evidence-box {
  background: var(--surface-elevated);
  padding: var(--space-3);
  border-radius: var(--radius);
  font-size: 0.8125rem;
  color: var(--text-secondary);
  border-left: 3px solid var(--primary);
}
.evidence-box .label {
  font-size: 0.6875rem;
  color: var(--text-muted);
  margin-bottom: 0.375rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ── Design agent layout ─────────────────────────────────────────────────────── */
.design-layout {
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
  gap: var(--space-4);
  margin-top: var(--space-3);
}
.design-list-pane {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--surface);
}
.design-records-list {
  max-height: 560px;
  overflow: auto;
  padding: var(--space-2);
  background: var(--surface);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.design-record-item {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.65rem;
  margin-bottom: var(--space-2);
  background: var(--surface);
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
}
.design-record-item:hover { border-color: var(--primary); background: var(--primary-light); }
.design-record-item.active { border-color: var(--primary); background: var(--primary-light); }

.design-detail-pane {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-3);
  background: var(--surface);
}

/* ── Runtime mode badge ──────────────────────────────────────────────────────── */
.runtime-mode-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius-full);
  border: 1px solid var(--primary-border);
  background: var(--primary-light);
  color: var(--primary);
}
.runtime-mode-badge.hybrid {
  border-color: var(--success-border);
  background: var(--success-light);
  color: #166534;
}

/* ── Domain group header (design panel) ──────────────────────────────────────── */
.design-domain-header {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--primary-light);
  color: var(--primary);
  border: 1px solid var(--primary-border);
  border-radius: var(--radius);
  padding: 0.3rem 0.5rem;
  font-size: 0.74rem;
  font-weight: 700;
  margin-bottom: var(--space-2);
}

/* ── Confidence bar ──────────────────────────────────────────────────────────── */
.confidence-bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: 0.75rem;
  margin-top: var(--space-2);
}
.confidence-bar .bar {
  flex: 1;
  height: 4px;
  background: var(--border);
  border-radius: var(--radius-full);
  overflow: hidden;
}
.confidence-bar .bar-fill { height: 100%; border-radius: var(--radius-full); }
.confidence-bar .bar-fill.high   { background: var(--band-high); }
.confidence-bar .bar-fill.medium { background: var(--band-medium); }
.confidence-bar .bar-fill.low    { background: var(--band-low); }

/* ── System field styling ───────────────────────────────────────────────────── */
.system-field-row { opacity: 0.6; font-style: italic; }
.system-field-row:hover { opacity: 0.85; }
.badge-system {
    background: #f3e8ff; color: #6b21a8;
    border: 1px solid #d8b4fe;
    padding: 0.08rem 0.3rem; border-radius: 3px;
    font-size: 0.65rem; font-weight: 600;
}

/* ── Extraction mode badges ─────────────────────────────────────────────────── */
.badge-mode-always   { background: #dcfce7; color: #166534; border: 1px solid #86efac; }
.badge-mode-cond     { background: #fef9c3; color: #854d0e; border: 1px solid #fde68a; }
.badge-mode-optional { background: #f3f4f6; color: #4b5563; border: 1px solid #d1d5db; }
.badge-mode-condprot { background: #f5f7fa; color: #0b1d32; border: 1px solid #d6dde6; }
.badge-mode-system   { background: #f3e8ff; color: #6b21a8; border: 1px solid #d8b4fe; }

/* ── Validation result color-coding ─────────────────────────────────────────── */
.match-challenge { background: #eff6ff; border-left: 3px solid #3b82f6; }
.match-system    { background: #f5f3ff; color: #6b7280; font-style: italic; }

/* ── Responsive ──────────────────────────────────────────────────────────────── */
@media (max-width: 1200px) {
  .design-layout {
    grid-template-columns: 1fr;
  }
  .design-list-pane,
  .design-detail-pane {
    width: 100%;
    max-width: 100%;
  }
}
@media (max-width: 960px) {
  .design-layout { grid-template-columns: 1fr; }
  .design-records-list { max-height: 260px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
  .container { padding: var(--space-4); }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .step { font-size: 0.65rem; padding: 0.4rem 0.2rem; }
}
