/* ═══════════════════════════════════════════════════════════════
   Agillic Analytics — Design System
   Dark analytics theme inspired by Plotly Dash / Amplitude / Grafana
   ═══════════════════════════════════════════════════════════════ */

/* ── Google Font ──────────────────────────────────────────────── */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

/* ── Tokens ───────────────────────────────────────────────────── */
:root {
  /* Surfaces */
  --bg-canvas:        #0f1117;
  --bg-surface:       #181c27;
  --bg-raised:        #1f2437;
  --bg-hover:         #262d42;

  /* Borders */
  --border:           #2a3147;
  --border-subtle:    #1e2538;

  /* Brand accent — indigo */
  --accent-700:       #3730a3;
  --accent-600:       #4f46e5;
  --accent-500:       #6366f1;
  --accent-400:       #818cf8;
  --accent-300:       #a5b4fc;
  --accent-glow:      rgba(99, 102, 241, 0.15);

  /* Text */
  --text-primary:     #f1f5f9;
  --text-secondary:   #94a3b8;
  --text-muted:       #4b5675;

  /* Semantic */
  --success:          #22c55e;
  --success-bg:       rgba(34, 197, 94, 0.12);
  --danger:           #ef4444;
  --danger-bg:        rgba(239, 68, 68, 0.12);
  --warning:          #f59e0b;
  --warning-bg:       rgba(245, 158, 11, 0.12);
  --info:             #38bdf8;
  --info-bg:          rgba(56, 189, 248, 0.12);

  /* Chart categorical palette */
  --chart-1:  #6366f1;  /* indigo  */
  --chart-2:  #22d3ee;  /* cyan    */
  --chart-3:  #f59e0b;  /* amber   */
  --chart-4:  #4ade80;  /* green   */
  --chart-5:  #f472b6;  /* pink    */
  --chart-6:  #a78bfa;  /* violet  */

  /* Shape */
  --radius-sm:  6px;
  --radius-md:  10px;
  --radius-lg:  14px;

  /* Type */
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

/* ── Light theme override ─────────────────────────────────────── */
/*
 * Activated by `data-theme="light"` on <html>. Only tokens that differ
 * from the dark defaults are redefined — everything else (radius, type,
 * chart pattern order) is inherited from :root above.
 *
 * The chart palette shifts to deeper shades of the same hues so lines
 * and bars keep enough contrast against a near-white surface. Chart-3
 * (amber) becomes a warmer orange for the same reason; chart-4 (green)
 * moves from a bright lime to a slate-safe emerald.
 */
:root[data-theme="light"] {
  /* Surfaces */
  --bg-canvas:        #f4f6fb;
  --bg-surface:       #ffffff;
  --bg-raised:        #f8fafc;
  --bg-hover:         #eef2f7;

  /* Borders */
  --border:           #e2e8f0;
  --border-subtle:    #eef2f7;

  /* Brand accent stays indigo but shifts one step darker where used on light bg */
  --accent-700:       #4338ca;
  --accent-600:       #4f46e5;
  --accent-500:       #6366f1;
  --accent-400:       #818cf8;
  --accent-300:       #a5b4fc;
  --accent-glow:      rgba(99, 102, 241, 0.10);

  /* Text — flip the scale */
  --text-primary:     #0f172a;
  --text-secondary:   #475569;
  --text-muted:       #94a3b8;

  /* Semantic (backgrounds go opaque-soft so they read on white) */
  --success:          #16a34a;
  --success-bg:       rgba(22, 163, 74, 0.10);
  --danger:           #dc2626;
  --danger-bg:        rgba(220, 38, 38, 0.10);
  --warning:          #d97706;
  --warning-bg:       rgba(217, 119, 6, 0.10);
  --info:             #0284c7;
  --info-bg:          rgba(2, 132, 199, 0.10);

  /* Chart categorical palette — deeper on white for legible line/bar contrast */
  --chart-1:  #4f46e5;  /* indigo  */
  --chart-2:  #0891b2;  /* cyan    */
  --chart-3:  #d97706;  /* amber   */
  --chart-4:  #059669;  /* emerald */
  --chart-5:  #db2777;  /* pink    */
  --chart-6:  #7c3aed;  /* violet  */
}

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

/* ── Base ─────────────────────────────────────────────────────── */
html, body { height: 100%; }
body {
  background: var(--bg-canvas);
  color: var(--text-primary);
  display: flex;
  min-height: 100vh;
}
a { color: var(--accent-400); text-decoration: none; }
a:hover { color: var(--accent-300); text-decoration: none; }

/* ── App shell ────────────────────────────────────────────────── */
.app-shell {
  display: flex;
  width: 100%;
  min-height: 100vh;
}

/* ── Sidebar nav ──────────────────────────────────────────────── */
.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding-bottom: 24px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar-logo {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: 8px;
}
.sidebar-logo .wordmark {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--text-primary);
}
.sidebar-logo .tenant-badge {
  display: inline-block;
  margin-top: 8px;
  padding: 2px 8px;
  background: var(--accent-glow);
  border: 1px solid rgba(99, 102, 241, .3);
  border-radius: 99px;
  font-size: 11px;
  color: var(--accent-400);
  font-weight: 500;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.nav-section-label {
  padding: 8px 20px 4px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 20px;
  font-size: 13.5px;
  color: var(--text-secondary);
  position: relative;
  transition: background .12s, color .12s;
}
.nav-link:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}
.nav-link.active {
  color: var(--accent-400);
  background: var(--accent-glow);
}
.nav-link.active::before {
  content: '';
  position: absolute;
  left: 0; top: 5px; bottom: 5px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--accent-500);
}
.nav-link svg { flex-shrink: 0; opacity: .8; }
.nav-spacer { flex: 1; }
.sidebar-footer {
  padding: 0 12px;
  border-top: 1px solid var(--border-subtle);
  padding-top: 12px;
  margin-top: 8px;
}

/* ── Main area ────────────────────────────────────────────────── */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* ── Topbar ───────────────────────────────────────────────────── */
.topbar {
  height: 56px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 12px;
  background: var(--bg-surface);
  position: sticky;
  top: 0;
  z-index: 100;
}
.topbar-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}
.topbar-tenant {
  font-size: 13px;
  color: var(--text-muted);
}
.topbar-spacer { flex: 1; }
.topbar-meta {
  font-size: 12px;
  color: var(--text-muted);
}

/* ── Page content ─────────────────────────────────────────────── */
.page-content {
  flex: 1;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ── Flash alerts ─────────────────────────────────────────────── */
.alert {
  padding: 10px 14px;
  border-radius: var(--radius-md);
  font-size: 13px;
  border: 1px solid transparent;
}
.alert-error   { background: var(--danger-bg);  color: var(--danger);  border-color: rgba(239,68,68,.25); }
.alert-success { background: var(--success-bg); color: var(--success); border-color: rgba(34,197,94,.25); }

/* ── Typography ───────────────────────────────────────────────── */
h1 { font-size: 1.2rem; font-weight: 700; color: var(--text-primary); }
h2 { font-size: .95rem; font-weight: 600; color: var(--text-secondary); margin: 4px 0; }

/* ── Buttons ──────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  font-family: inherit;
  transition: background .12s, border-color .12s, color .12s;
}
.btn-primary { background: var(--accent-600); color: #fff; }
.btn-primary:hover { background: var(--accent-500); color: #fff; }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.1); }
.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--bg-raised); color: var(--text-primary); }

/* ── Forms ────────────────────────────────────────────────────── */
.form-group { margin-bottom: 14px; }
label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 5px;
}
input[type=text],
input[type=email],
input[type=password],
input[type=date],
select,
textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  background: var(--bg-raised);
  color: var(--text-primary);
  transition: border-color .12s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent-500);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
select option { background: var(--bg-raised); }

/* ── Filter bar ───────────────────────────────────────────────── */
.filter-bar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: flex-end;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
}
.filter-bar select,
.filter-bar input { width: auto; min-width: 140px; }

/* ── Range strip (date quick-picks) ──────────────────────────── */
.range-strip {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.range-strip-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-right: 4px;
}
.range-btn {
  padding: 5px 13px;
  border-radius: 99px;
  border: 1px solid var(--border);
  background: var(--bg-raised);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  text-decoration: none;
  transition: all .12s;
}
.range-btn:hover { background: var(--bg-hover); color: var(--text-primary); text-decoration: none; }
.range-btn.active {
  background: var(--accent-600);
  color: #fff;
  border-color: var(--accent-600);
}

/* ── Panel / card ─────────────────────────────────────────────── */
.panel {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}
.panel-body { padding: 20px; }
.panel-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-subtle);
}
.panel-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}
.panel-subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: auto;
}

/* chart wrapper inside a panel */
.chart-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
}
.chart-panel h2 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  text-transform: none;
  letter-spacing: 0;
  margin-bottom: 14px;
}

/* ── KPI tiles ────────────────────────────────────────────────── */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
  gap: 14px;
}
.kpi-tile {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 20px 16px;
  transition: border-color .15s;
  position: relative;
  overflow: hidden;
}
.kpi-tile:hover { border-color: var(--accent-500); }
.kpi-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 6px;
}
.kpi-value {
  font-size: 1.55rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.1;
}
.kpi-delta {
  font-size: 12px;
  margin-top: 5px;
  color: var(--text-muted);
}
.kpi-delta.up   { color: var(--success); }
.kpi-delta.down { color: var(--danger); }
/* risk tiles — up is bad */
.kpi-tile.risk .kpi-delta.up   { color: var(--danger); }
.kpi-tile.risk .kpi-delta.down { color: var(--success); }

/* health bar */
.health-bar-wrap {
  margin-top: 8px;
  height: 3px;
  border-radius: 99px;
  background: var(--border);
  overflow: hidden;
}
.health-bar { height: 100%; border-radius: 99px; transition: width .4s; }
.health-green { background: var(--success); }
.health-amber { background: var(--warning); }
.health-red   { background: var(--danger); }

/* ── Chart row (two-column layout) ───────────────────────────── */
.chart-row {
  display: grid;
  grid-template-columns: 1fr 400px;
  gap: 16px;
}
@media (max-width: 960px) {
  .chart-row { grid-template-columns: 1fr; }
}

/* ── Tables ───────────────────────────────────────────────────── */
.table-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.table-panel-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
  padding: 9px 16px;
}
td {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-secondary);
}
tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--bg-hover); color: var(--text-primary); }
tfoot td {
  font-weight: 600;
  color: var(--text-primary);
  background: var(--bg-raised);
  border-top: 1px solid var(--border);
}

/* ── Badges ───────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
}
.badge-done       { background: var(--success-bg); color: var(--success); }
.badge-pending    { background: var(--warning-bg); color: var(--warning); }
.badge-processing { background: var(--info-bg);    color: var(--info); }
.badge-error      { background: var(--danger-bg);  color: var(--danger); }

/* ── Misc utilities ───────────────────────────────────────────── */
.text-muted    { color: var(--text-muted); }
.text-secondary { color: var(--text-secondary); }
.font-mono     { font-family: ui-monospace, 'SF Mono', Consolas, monospace; font-size: 12px; }
.rate-cell     { font-variant-numeric: tabular-nums; }
.exec-table td:first-child {
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-primary);
  font-weight: 500;
}


/* ── Theme toggle button ──────────────────────────────────────── */
.theme-toggle {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  background: var(--bg-raised);
  color: var(--text-secondary);
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}
.theme-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--accent-500);
}
.theme-toggle:focus-visible {
  outline: 2px solid var(--accent-500);
  outline-offset: 2px;
}
.theme-toggle-icon { display: none; }
/* Dark mode → show sun (click to go light). Default (no attr) is dark. */
:root:not([data-theme="light"]) .theme-toggle-sun { display: inline; }
:root[data-theme="light"]        .theme-toggle-moon { display: inline; }


/* ── Segmented control (pill button group) ───────────────────── */
.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--bg-raised);
}
.seg-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  border-right: 1px solid var(--border);
  transition: background 120ms ease, color 120ms ease;
}
.seg-btn:last-child { border-right: none; }
.seg-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}
.seg-btn-active,
.seg-btn[aria-pressed="true"] {
  background: var(--accent-glow);
  color: var(--accent-400);
}
.seg-btn:focus-visible {
  outline: 2px solid var(--accent-500);
  outline-offset: -2px;
}
