/* ============================================================
   DESIGN TOKENS — Dark Immersive Theme
   ============================================================ */
:root {
  /* Background layers */
  --bg-base: #0a0e17;
  --bg-canvas: #0d111c;
  --bg-surface: #131a2a;
  --bg-surface-elevated: #192038;
  --bg-surface-muted: #1e2840;
  --bg-surface-hover: #222d4a;
  --body-bg: var(--bg-base);
  --topbar-bg: rgba(19, 26, 42, 0.85);
  --field-bg: rgba(255, 255, 255, 0.04);
  --table-row-hover-bg: rgba(99, 102, 241, 0.06);
  --table-empty-bg: linear-gradient(135deg, rgba(255,255,255,0.03), rgba(99,102,241,0.04));
  --drawer-backdrop: rgba(0, 0, 0, 0.6);
  --drawer-shadow: -20px 0 60px rgba(0, 0, 0, 0.5);
  --confirm-backdrop: rgba(0, 0, 0, 0.6);
  --pipeline-collector-bg: linear-gradient(135deg, var(--bg-surface), rgba(56, 189, 248, 0.06));
  --pipeline-brain-bg: linear-gradient(135deg, var(--bg-surface), rgba(249, 115, 22, 0.06));
  --pipeline-executor-bg: linear-gradient(135deg, var(--bg-surface), rgba(34, 197, 94, 0.06));

  /* Text */
  --text-primary: #e8ecf4;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --text-inverse: #0a0e17;
  --sidebar-text: #cbd5e1;
  --nav-text: #94a3b8;
  --brand-subtitle: var(--text-muted);
  --nav-group-title: var(--text-muted);
  --nav-icon-color: #cbd5e1;

  /* Borders */
  --border-subtle: rgba(255, 255, 255, 0.05);
  --border-default: rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.14);

  /* Status colors (vibrant for dark mode) */
  --success: #22c55e;
  --success-soft: rgba(34, 197, 94, 0.12);
  --success-glow: rgba(34, 197, 94, 0.25);
  --warning: #f59e0b;
  --warning-soft: rgba(245, 158, 11, 0.12);
  --warning-glow: rgba(245, 158, 11, 0.3);
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.12);
  --danger-glow: rgba(239, 68, 68, 0.3);
  --info: #38bdf8;
  --info-soft: rgba(56, 189, 248, 0.12);

  /* Primary / Action */
  --primary: #6366f1;
  --primary-strong: #818cf8;
  --primary-soft: rgba(99, 102, 241, 0.12);
  --action: #f97316;
  --action-strong: #fb923c;
  --action-soft: rgba(249, 115, 22, 0.12);

  /* Navy sidebar (already dark, refined) */
  --navy: #0f1629;
  --navy-soft: #1a2342;
  --nav-icon-bg: rgba(255, 255, 255, 0.08);
  --nav-active-icon-bg: rgba(255, 255, 255, 0.18);

  /* Shadows (glow-forward for dark mode) */
  --shadow: 0 24px 64px rgba(0, 0, 0, 0.45);
  --shadow-soft: 0 8px 32px rgba(0, 0, 0, 0.3);
  --shadow-glow-green: 0 0 20px var(--success-glow);
  --shadow-glow-orange: 0 0 20px var(--warning-glow);
  --shadow-glow-red: 0 0 20px var(--danger-glow);
  --brand-shadow: 0 10px 28px rgba(249, 115, 22, 0.35);
  --nav-active-shadow: 0 12px 28px rgba(99, 102, 241, 0.28);

  /* Radius */
  --radius: 16px;
  --radius-sm: 12px;
  --radius-xs: 8px;

  /* 8pt Grid Spacing */
  --space-1: 8px;
  --space-2: 16px;
  --space-3: 24px;
  --space-4: 32px;
  --space-5: 40px;
  --space-6: 48px;

  /* Layout */
  --sidebar-width: 220px;

  /* Backward-compatible aliases for older view markup. */
  --bg: var(--bg-base);
  --canvas: var(--bg-canvas);
  --surface: var(--bg-surface);
  --surface-muted: var(--bg-surface-muted);
  --text: var(--text-primary);
  --muted: var(--text-muted);
  --border: var(--border-default);

  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="light"] {
  --bg-base: #f4f7fb;
  --bg-canvas: #edf3fb;
  --bg-surface: #ffffff;
  --bg-surface-elevated: #ffffff;
  --bg-surface-muted: #f3f6fb;
  --bg-surface-hover: #dce6f2;
  --body-bg:
    radial-gradient(circle at 74% 0%, rgba(207, 222, 248, 0.65), transparent 30%),
    linear-gradient(135deg, #f5efe4 0%, #f4f7fb 36%, #edf3fb 100%);
  --topbar-bg: rgba(255, 255, 255, 0.86);
  --field-bg: rgba(255, 255, 255, 0.82);
  --table-row-hover-bg: rgba(40, 85, 246, 0.055);
  --table-empty-bg: linear-gradient(135deg, rgba(255,255,255,0.72), rgba(232,238,252,0.42));
  --drawer-backdrop: rgba(16, 35, 63, 0.4);
  --drawer-shadow: -20px 0 60px rgba(16, 36, 62, 0.18);
  --confirm-backdrop: rgba(16, 35, 63, 0.5);
  --pipeline-collector-bg: linear-gradient(135deg, #ffffff, #eef7ff);
  --pipeline-brain-bg: linear-gradient(135deg, #ffffff, #fff5e8);
  --pipeline-executor-bg: linear-gradient(135deg, #ffffff, #ecfbf1);

  --text-primary: #10233f;
  --text-secondary: #344763;
  --text-muted: #60718b;
  --text-inverse: #ffffff;
  --sidebar-text: #e2e8f0;
  --nav-text: #d8e3f0;
  --brand-subtitle: #94a3b8;
  --nav-group-title: #7f92ad;
  --nav-icon-color: #c8d6ea;

  --border-subtle: #e8eef6;
  --border-default: #dce6f2;
  --border-strong: #cbd5e1;

  --success: #16a34a;
  --success-soft: #dff7e8;
  --success-glow: rgba(22, 163, 74, 0.18);
  --warning: #d97706;
  --warning-soft: #fff0cc;
  --warning-glow: rgba(217, 119, 6, 0.18);
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --danger-glow: rgba(220, 38, 38, 0.18);
  --info: #0891b2;
  --info-soft: #cffafe;

  --primary: #2855f6;
  --primary-strong: #123dba;
  --primary-soft: #e8eefc;
  --action: #ff7f3a;
  --action-strong: #ea580c;
  --action-soft: #fff1e7;

  --navy: #10243e;
  --navy-soft: #1d375c;
  --nav-icon-bg: rgba(255, 255, 255, 0.08);
  --nav-active-icon-bg: rgba(255, 255, 255, 0.18);

  --shadow: 0 22px 60px rgba(47, 68, 102, 0.13);
  --shadow-soft: 0 10px 28px rgba(47, 68, 102, 0.08);
  --brand-shadow: 0 14px 28px rgba(255, 127, 58, 0.28);
  --nav-active-shadow: 0 14px 30px rgba(40, 85, 246, 0.28);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--body-bg);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ============================================================
   LAYOUT
   ============================================================ */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: 100vh;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
}

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
  position: sticky;
  top: var(--space-2);
  height: calc(100vh - var(--space-3));
  padding: var(--space-2) 12px;
  background: var(--navy);
  color: var(--sidebar-text);
  border: 1px solid var(--border-default);
  border-radius: var(--radius);
  box-shadow: var(--shadow-soft);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 8px var(--space-3);
}

.brand-mark {
  display: grid;
  width: 52px;
  height: 52px;
  place-items: center;
  border-radius: 14px;
  background: var(--action);
  color: #ffffff;
  font-weight: 700;
  font-size: 18px;
  box-shadow: var(--brand-shadow);
}

.brand strong, .brand span { display: block; }
.brand strong { font-size: 19px; color: #ffffff; line-height: 1.1; }
.brand span { margin-top: 2px; color: var(--brand-subtitle); font-size: 12px; }

.nav-list { display: grid; gap: var(--space-2); }

.nav-group { display: grid; gap: 8px; }

.nav-group-title {
  margin: 0;
  padding: 0 8px;
  color: var(--nav-group-title);
  font-size: 12px;
  font-weight: 700;
}

.nav-button {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 58px;
  padding: 10px 12px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--nav-text);
  text-align: left;
  transition: background 140ms ease, color 140ms ease, transform 140ms ease;
}

.nav-button:hover, .nav-button.active { background: var(--navy-soft); color: #ffffff; }
.nav-button.active { background: var(--primary); box-shadow: var(--nav-active-shadow); }
.nav-button:hover { transform: translateY(-1px); }

.nav-icon {
  display: grid;
  width: 30px;
  height: 30px;
  place-items: center;
  border-radius: 999px;
  background: var(--nav-icon-bg);
  color: var(--nav-icon-color);
  font-weight: 800;
}

.nav-button.active .nav-icon { background: var(--nav-active-icon-bg); color: #ffffff; }

.nav-copy { display: grid; gap: 2px; min-width: 0; }
.nav-copy span { font-weight: 800; }
.nav-copy small { color: var(--text-muted); font-size: 11px; white-space: nowrap; }
.nav-button.active .nav-copy small { color: rgba(255, 255, 255, 0.86); }

/* ============================================================
   MAIN SHELL
   ============================================================ */
.main-shell { min-width: 0; padding-bottom: var(--space-4); }

.topbar {
  position: sticky;
  top: var(--space-2);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 70px;
  padding: 0 var(--space-2);
  background: var(--topbar-bg);
  border: 1px solid var(--border-default);
  border-radius: var(--radius);
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow-soft);
}

.topbar.topbar-compact {
  justify-content: flex-end;
  min-height: 64px;
}

.menu-toggle {
  display: none;
  width: 40px;
  height: 40px;
  place-items: center;
  gap: 4px;
  padding: 10px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xs);
  background: var(--bg-surface);
}

.menu-toggle span {
  display: block;
  width: 18px;
  height: 2px;
  border-radius: 999px;
  background: var(--text-primary);
}

.global-search {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  max-width: 480px;
  padding: 0 14px;
  border: 1px solid var(--border-default);
  border-radius: 14px;
  background: var(--field-bg);
}

.global-search[hidden] { display: none; }

.global-search span { color: var(--text-muted); font-size: 12px; }
.global-search input {
  width: 100%;
  min-height: 36px;
  border: 0;
  background: transparent;
  color: var(--text-primary);
}
.global-search input:focus-visible { outline: 0; }

.context-tools {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.context-tools[hidden] { display: none; }

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 74px;
  height: 36px;
  padding: 0;
  border: 1px solid var(--border-default);
  border-radius: 999px;
  background: var(--field-bg);
  color: var(--text-muted);
  transition: border-color 140ms ease, background 140ms ease;
}

.theme-toggle:hover {
  border-color: var(--border-strong);
}

.theme-toggle-track {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: 64px;
  height: 28px;
  padding: 2px;
  align-items: center;
}

.theme-toggle-option {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  height: 24px;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  transition: color 160ms ease;
}

.theme-toggle-day { color: var(--text-muted); }
.theme-toggle-night { color: var(--text-primary); }

.theme-toggle-thumb {
  position: absolute;
  top: 2px;
  left: 34px;
  width: 28px;
  height: 24px;
  border-radius: 999px;
  background: var(--primary);
  box-shadow: var(--shadow-soft);
  transition: transform 180ms ease, background 180ms ease;
}

:root[data-theme="light"] .theme-toggle-day { color: #ffffff; }
:root[data-theme="light"] .theme-toggle-night { color: var(--text-muted); }
:root[data-theme="light"] .theme-toggle-thumb {
  transform: translateX(-32px);
  background: var(--action);
}

.context-select {
  min-height: 36px;
  min-width: 150px;
  padding: 0 12px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-xs);
  background: var(--field-bg);
  color: var(--text-primary);
}

.system-pill {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 0 14px;
  border-radius: 999px;
  background: var(--success-soft);
  color: var(--success);
  font-size: 12px;
  font-weight: 700;
}

.system-pill.error { background: var(--danger-soft); color: var(--danger); }

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-3) 0 var(--space-2);
}

.page-header h1 { margin: 2px 0 0; font-size: 34px; line-height: 40px; }
.eyebrow { margin: 0; color: var(--text-muted); font-size: 12px; }

.header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.view-root { padding: 0 0 var(--space-5); }
