:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #d97706;
  --nav-bg: #0f172a;
  --nav-text: #e2e8f0;
  --nav-text-muted: #94a3b8;
  --nav-active: #60a5fa;
  --sc-green-bg: #dcfce7;
  --sc-green-fg: #166534;
  --sc-yellow-bg: #fef3c7;
  --sc-yellow-fg: #92400e;
  --sc-red-bg: #fee2e2;
  --sc-red-fg: #991b1b;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Nav ---------- */
nav.topbar {
  background: var(--nav-bg);
  color: var(--nav-text);
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 4px;
  position: sticky;
  top: 0;
  z-index: 100;
  height: 56px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
nav.topbar .brand {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.01em;
  margin-right: 24px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #fff;
}
nav.topbar .brand-badge {
  background: var(--primary);
  color: #fff;
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
}
nav.topbar a {
  color: var(--nav-text-muted);
  text-decoration: none;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: 6px;
  transition: all 0.15s;
  font-size: 13px;
}
nav.topbar a:hover { color: #fff; background: rgba(255,255,255,0.06); }
nav.topbar a.active { color: var(--nav-active); background: rgba(96, 165, 250, 0.12); }
nav.topbar .spacer { flex: 1; }
nav.topbar .nav-user {
  color: var(--nav-text-muted);
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
nav.topbar .nav-divider {
  width: 1px;
  height: 22px;
  background: rgba(255,255,255,0.1);
  margin: 0 8px;
}
nav.topbar .nav-group-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #64748b;
  padding: 0 6px 0 8px;
  font-weight: 600;
  user-select: none;
}

/* ---------- Nav dropdown ---------- */
.nav-dropdown { position: relative; }
.nav-dropdown-menu {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  min-width: 180px;
  padding: 6px;
  box-shadow: 0 8px 20px rgba(15,23,42,0.15);
  z-index: 150;
}
.nav-dropdown:hover .nav-dropdown-menu,
.nav-dropdown:focus-within .nav-dropdown-menu { display: block; }
.nav-dropdown-menu a {
  display: block;
  padding: 8px 12px;
  color: var(--text) !important;
  border-radius: var(--radius-sm);
  font-size: 13px;
  background: transparent !important;
}
.nav-dropdown-menu a:hover { background: var(--surface-2) !important; color: var(--text) !important; }
.nav-dropdown-menu a.active { background: var(--primary-soft) !important; color: var(--primary) !important; }

/* ---------- Layout ---------- */
main { padding: 24px; max-width: 1680px; margin: 0 auto; }
h1 { font-size: 22px; font-weight: 700; margin: 0 0 4px; letter-spacing: -0.02em; }
h2 { font-size: 16px; font-weight: 600; margin: 0 0 12px; letter-spacing: -0.01em; }
h3 { font-size: 14px; font-weight: 600; margin: 0 0 10px; color: var(--text); letter-spacing: -0.005em; }
.page-header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 12px;
}
.page-header .subtitle { color: var(--text-muted); font-size: 13px; margin-top: 2px; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow-sm);
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 12px;
}
.card-header h3 { margin: 0; }
.card-title-icon {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.card-count {
  background: var(--surface-2);
  color: var(--text-muted);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

/* ---------- KPI ---------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
}
.kpi-label {
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
  margin-bottom: 6px;
}
.kpi-value {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}
.kpi-sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.kpi-value.good { color: var(--success); }
.kpi-value.bad { color: var(--danger); }
.kpi-value.warn { color: var(--warning); }

/* ---------- Filters ---------- */
.filters {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  background: var(--surface);
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 16px;
  flex-wrap: wrap;
  box-shadow: var(--shadow-sm);
}
.filters label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.filters input, .filters select, .filters textarea {
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 13px;
  background: #fff;
  color: var(--text);
  text-transform: none;
  font-weight: 400;
  min-width: 140px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.filters input:focus, .filters select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
.filter-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-left: auto;
}
.date-presets {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.date-presets button {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 500;
}
.date-presets button:hover { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }

/* ---------- Buttons ---------- */
button, .btn {
  padding: 8px 14px;
  background: var(--primary);
  color: #fff;
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s, transform 0.05s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: inherit;
}
button:hover, .btn:hover { background: var(--primary-hover); }
button:active, .btn:active { transform: translateY(1px); }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border-strong); }
.btn-secondary:hover { background: #eef2f7; color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }
.btn-ghost { background: transparent; color: var(--text-muted); border: 0; padding: 4px 8px; }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius-sm); border: 1px solid var(--border); background: #fff; }
table {
  border-collapse: collapse;
  width: 100%;
  background: var(--surface);
  font-size: 13px;
}
th, td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
  white-space: nowrap;
}
th {
  background: var(--surface-2);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  position: sticky;
  top: 0;
}
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap, th.wrap { white-space: normal; max-width: 320px; }
td a { color: var(--primary); text-decoration: none; font-weight: 500; }
td a:hover { text-decoration: underline; }

/* ---------- Score cells & badges ---------- */
.score {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  min-width: 40px;
  text-align: center;
  font-size: 12px;
}
.score.sc-green { background: var(--sc-green-bg); color: var(--sc-green-fg); }
.score.sc-yellow { background: var(--sc-yellow-bg); color: var(--sc-yellow-fg); }
.score.sc-red { background: var(--sc-red-bg); color: var(--sc-red-fg); }
.score.sc-none { background: var(--surface-2); color: var(--text-muted); }
/* Plan-tracking cell backgrounds (used on funnel detail table) */
td.cell-ahead   { background: rgba(22, 163, 74, 0.10) !important; }
td.cell-ontrack { background: rgba(217, 119, 6, 0.10) !important; }
td.cell-behind  { background: rgba(220, 38, 38, 0.10) !important; }

/* Compact fact/plan/delta cell */
td.fact-cell { padding: 8px 10px; line-height: 1.15; vertical-align: middle; }
td.fact-cell .fact-main {
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  color: var(--text);
}
td.fact-cell .fact-sub {
  margin-top: 3px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  display: flex;
  gap: 6px;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: nowrap;
  white-space: nowrap;
}
td.fact-cell .plan-value {
  color: var(--text-muted);
}
td.fact-cell .plan-inline {
  cursor: pointer;
  border-bottom: 1px dotted var(--border-strong);
  padding: 0 1px;
}
td.fact-cell .plan-inline:hover { color: var(--primary); border-bottom-color: var(--primary); }
td.fact-cell .delta-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 4px;
  border-radius: 3px;
  background: rgba(255,255,255,0.6);
}
td.fact-cell.cell-ahead   .delta-badge { color: var(--success); }
td.fact-cell.cell-behind  .delta-badge { color: var(--danger); }
td.fact-cell.cell-ontrack .delta-badge { color: var(--warning); }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: none;
  letter-spacing: 0;
}
.badge-done { background: var(--sc-green-bg); color: var(--sc-green-fg); }
.badge-pending { background: #dbeafe; color: #1e40af; }
.badge-error { background: var(--sc-red-bg); color: var(--sc-red-fg); }
.badge-short { background: #f1f5f9; color: var(--text-muted); }
.badge-review { background: var(--sc-yellow-bg); color: var(--sc-yellow-fg); }
.badge-neutral { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }
.light-emoji { font-size: 16px; line-height: 1; }

/* ---------- Forms ---------- */
input[type=text], input[type=date], input[type=number], input[type=password], input[type=email], select, textarea {
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 13px;
  background: #fff;
  color: var(--text);
  font-family: inherit;
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
textarea { min-height: 100px; resize: vertical; font-size: 13px; line-height: 1.5; }
label.field { display: block; margin-bottom: 12px; }
label.field > .label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); font-weight: 600; margin-bottom: 4px; display: block; }

/* ---------- Toast ---------- */
#toast-area {
  position: fixed;
  top: 72px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
  pointer-events: none;
}
.toast {
  background: var(--nav-bg);
  color: #fff;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  font-size: 13px;
  font-weight: 500;
  animation: toast-in 0.2s ease-out;
  pointer-events: auto;
  min-width: 200px;
  max-width: 400px;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.info { background: var(--primary); }
@keyframes toast-in {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

/* ---------- HTMX loading ---------- */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline-block; }
.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Empty state ---------- */
.empty {
  padding: 30px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}
.empty-icon { font-size: 32px; margin-bottom: 8px; opacity: 0.4; }

.muted { color: var(--text-muted); font-size: 12px; }
pre { background: var(--surface-2); padding: 12px; border-radius: var(--radius-sm); overflow: auto; font-size: 12px; line-height: 1.5; border: 1px solid var(--border); }

/* ---------- Detail page ---------- */
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.detail-block { padding: 12px 16px; background: var(--surface-2); border-radius: var(--radius-sm); }
.detail-block dt { font-size: 11px; text-transform: uppercase; color: var(--text-muted); font-weight: 600; letter-spacing: 0.03em; }
.detail-block dd { margin: 2px 0 10px; font-size: 14px; }
.transcript-box {
  background: var(--surface-2);
  padding: 16px;
  border-radius: var(--radius-sm);
  max-height: 500px;
  overflow-y: auto;
  white-space: pre-wrap;
  font-size: 13px;
  line-height: 1.6;
  border: 1px solid var(--border);
}

/* ---------- Chart cards ---------- */
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(400px, 1fr)); gap: 16px; }
.chart-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow-sm); }
.chart-card h4 { margin: 0 0 12px; font-size: 13px; font-weight: 600; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  nav.topbar { padding: 0 12px; gap: 0; overflow-x: auto; height: auto; padding-top: 8px; padding-bottom: 8px; flex-wrap: wrap; }
  nav.topbar a { padding: 6px 10px; font-size: 12px; }
  main { padding: 14px; }
  .filters { padding: 12px; }
  .filter-actions { margin-left: 0; width: 100%; }
  h1 { font-size: 18px; }
  th, td { padding: 7px 8px; font-size: 12px; }
}
