:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface2: #f1f5f9;
  --border: #e2e8f0;
  --accent: #4f6ef7;
  --accent2: #7c3aed;
  --green: #16a34a;
  --yellow: #ca8a04;
  --red: #dc2626;
  --orange: #ea580c;
  --text: #0f172a;
  --muted: #64748b;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
}

[data-theme="dark"] {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: #2a2e45;
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
  --orange: #f97316;
  --text: #e2e8f0;
  --muted: #64748b;
  --shadow: none;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  min-height: 100vh;
}

/* NAV */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 52px;
  position: sticky;
  top: 0;
  z-index: 100;
}
.logo { font-weight: 700; font-size: 16px; color: var(--accent); letter-spacing: 0.5px; }
.topbar-right { display: flex; gap: 4px; align-items: center; }
.theme-toggle {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  width: 32px; height: 32px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 15px;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
  margin-left: 8px;
}
.theme-toggle:hover { color: var(--text); border-color: var(--accent); }
.tab-btn {
  background: none;
  border: none;
  color: var(--muted);
  padding: 8px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}
.tab-btn:hover { color: var(--text); background: var(--surface2); }
.tab-btn.active { color: var(--accent); background: rgba(79,110,247,0.12); }

/* TABS */
.tab { display: none; padding: 24px; max-width: 1400px; margin: 0 auto; }
.tab.active { display: block; }

/* SECTION HEADER */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.section-header h2 { font-size: 20px; font-weight: 600; }
.btn-refresh {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}
.btn-refresh:hover { color: var(--text); }

/* CARDS */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}
.card h3 { font-size: 15px; font-weight: 600; margin-bottom: 16px; color: var(--muted); }

/* STATS GRID */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.stat-card .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.stat-card .value { font-size: 28px; font-weight: 700; margin-top: 6px; }
.stat-card .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* CHARTS ROW */
.charts-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 20px; }
@media (max-width: 768px) { .charts-row { grid-template-columns: 1fr; } }
.chart-card { margin-bottom: 0; }

/* FUNNEL */
.funnel-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.funnel-bar .f-label { width: 160px; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.funnel-bar .f-track { flex: 1; background: var(--surface2); border-radius: 4px; height: 22px; position: relative; overflow: hidden; }
.funnel-bar .f-fill { height: 100%; border-radius: 4px; transition: width 0.4s ease; display: flex; align-items: center; padding-left: 8px; font-size: 11px; font-weight: 600; }
.funnel-bar .f-count { width: 40px; text-align: right; font-size: 12px; font-weight: 600; }

/* SOURCE BARS */
.source-bar { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.source-bar .s-label { width: 100px; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.source-bar .s-track { flex: 1; background: var(--surface2); border-radius: 4px; height: 18px; overflow: hidden; }
.source-bar .s-fill { height: 100%; background: var(--accent2); border-radius: 4px; transition: width 0.4s; }
.source-bar .s-count { width: 30px; text-align: right; font-size: 12px; }

/* DAILY CHART */
.bar-chart-wrap { display: flex; align-items: flex-end; gap: 4px; height: 80px; padding-top: 8px; overflow-x: auto; }
.daily-bar { display: flex; flex-direction: column; align-items: center; gap: 2px; flex: 1; min-width: 24px; }
.daily-bar .d-bar { width: 100%; background: var(--accent); border-radius: 3px 3px 0 0; transition: height 0.3s; }
.daily-bar .d-label { font-size: 9px; color: var(--muted); white-space: nowrap; }

/* PIPELINE BOARD */
.pipeline-board {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 16px;
}
.pipeline-col {
  flex: 0 0 220px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
}
.pipeline-col-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.pipeline-col-header .col-title { font-size: 12px; font-weight: 600; }
.pipeline-col-header .col-count {
  background: var(--surface2);
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 11px;
  color: var(--muted);
}
.pipeline-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: border-color 0.15s;
}
.pipeline-card:hover { border-color: var(--accent); }
.pipeline-card .pc-name { font-size: 12px; font-weight: 600; margin-bottom: 3px; }
.pipeline-card .pc-email { font-size: 11px; color: var(--muted); margin-bottom: 5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pipeline-card .pc-meta { font-size: 10px; color: var(--muted); }
.pipeline-card .pc-bar { height: 3px; background: var(--surface); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.pipeline-card .pc-bar-fill { height: 100%; background: var(--green); border-radius: 2px; }

/* FILTERS */
.filters-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
  align-items: center;
}
.filters-bar input[type="text"],
.filters-bar select,
.filters-bar input[type="date"] {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  outline: none;
}
.filters-bar input[type="text"] { width: 220px; }
.filters-bar input[type="text"]:focus,
.filters-bar select:focus { border-color: var(--accent); }
.btn-primary {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 7px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
}
.btn-secondary {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 7px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
}

/* TABLE */
.table-card { padding: 0; overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
thead { background: var(--surface2); }
th { padding: 14px 18px; text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); font-weight: 700; white-space: nowrap; }
td { padding: 15px 18px; border-top: 1px solid var(--border); font-size: 15px; vertical-align: middle; }
tr:hover td { background: var(--surface2); }
.td-name { font-weight: 600; font-size: 15px; }
.td-email { font-size: 13px; color: var(--muted); margin-top: 2px; }

/* STAGE BADGES */
.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge-0 { background: #f1f5f9; color: #475569; }
.badge-1 { background: #fef9c3; color: #854d0e; }
.badge-2 { background: #ede9fe; color: #5b21b6; }
.badge-3 { background: #ffedd5; color: #9a3412; }
.badge-4 { background: #fee2e2; color: #991b1b; }
.badge-5 { background: #fecaca; color: #991b1b; }
.badge-6 { background: #dcfce7; color: #166534; }

[data-theme="dark"] .badge-0 { background: rgba(100,116,139,0.2); color: #94a3b8; }
[data-theme="dark"] .badge-1 { background: rgba(234,179,8,0.15); color: #eab308; }
[data-theme="dark"] .badge-2 { background: rgba(79,110,247,0.15); color: #818cf8; }
[data-theme="dark"] .badge-3 { background: rgba(249,115,22,0.15); color: #f97316; }
[data-theme="dark"] .badge-4 { background: rgba(239,68,68,0.15); color: #ef4444; }
[data-theme="dark"] .badge-5 { background: rgba(239,68,68,0.25); color: #f87171; }
[data-theme="dark"] .badge-6 { background: rgba(34,197,94,0.15); color: #22c55e; }

/* FORM % BAR */
.pct-bar { display: flex; align-items: center; gap: 8px; }
.pct-track { width: 64px; height: 6px; background: var(--surface2); border-radius: 3px; overflow: hidden; }
.pct-fill { height: 100%; background: var(--green); border-radius: 3px; }
.pct-text { font-size: 13px; color: var(--muted); }

/* PAGINATION */
.pagination {
  display: flex;
  gap: 4px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  align-items: center;
}
.page-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 10px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 12px;
}
.page-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.page-btn:disabled { opacity: 0.4; cursor: default; }
.page-info { margin-left: auto; font-size: 12px; color: var(--muted); }

/* ACTION BTNS */
.btn-view, .btn-del {
  border: none;
  border-radius: 5px;
  padding: 6px 14px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  margin-right: 4px;
}
.btn-view { background: rgba(79,110,247,0.15); color: var(--accent); }
.btn-del  { background: rgba(239,68,68,0.1); color: var(--red); }
.btn-view:hover { background: rgba(79,110,247,0.3); }
.btn-del:hover  { background: rgba(239,68,68,0.25); }

/* MODAL */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(15,23,42,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 680px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.15);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
.modal-header h3 { font-size: 16px; font-weight: 600; }
.modal-close {
  background: none; border: none; color: var(--muted);
  font-size: 18px; cursor: pointer; padding: 4px;
}
.modal-body { padding: 24px; }

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 20px;
}
.detail-field { display: flex; flex-direction: column; gap: 3px; }
.detail-field .df-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.detail-field .df-value { font-size: 13px; }

.stage-change-row { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.stage-select {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 12px;
  border-radius: var(--radius);
  flex: 1;
  outline: none;
}
.btn-update-stage {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 7px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
}

.history-list { margin-top: 16px; }
.history-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}
.history-item:last-child { border-bottom: none; }
.history-arrow { color: var(--accent); }

/* API DOCS */
.api-doc code { background: var(--surface2); padding: 2px 6px; border-radius: 4px; font-family: monospace; font-size: 13px; }
.api-doc pre {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  overflow-x: auto;
  font-family: monospace;
  font-size: 12px;
  margin: 12px 0;
  line-height: 1.6;
}
.endpoint-box {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin: 12px 0;
  font-family: monospace;
}
.api-doc h4 { margin: 18px 0 8px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.api-table { width: 100%; border-collapse: collapse; margin: 8px 0; }
.api-table th { background: var(--surface2); padding: 8px 12px; text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }
.api-table td { padding: 8px 12px; border-top: 1px solid var(--border); font-size: 12px; }
.api-table td:first-child { font-family: monospace; color: var(--accent); }

.method {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  font-family: monospace;
}
.method.post { background: rgba(34,197,94,0.15); color: #22c55e; }
.method.get  { background: rgba(79,110,247,0.15); color: #818cf8; }
.method.put  { background: rgba(234,179,8,0.15); color: #eab308; }
.method.del  { background: rgba(239,68,68,0.15); color: #ef4444; }

.stages-list { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.stages-list .badge { font-size: 12px; padding: 4px 12px; }

.api-doc p { color: var(--muted); margin: 8px 0; line-height: 1.6; }
