:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-alt: #fafbfd;
  --border: #e5e8f0;
  --text: #1a1d29;
  --text-muted: #6b7285;
  --text-faint: #9aa1b2;
  --accent: #4f5fee;
  --accent-hover: #3f4de0;
  --accent-soft: #eef0ff;
  --ok: #0f9d58;
  --ok-soft: #e6f6ec;
  --warn: #b7791f;
  --warn-soft: #fdf3df;
  --err: #d64545;
  --err-soft: #fdecec;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow-sm: 0 1px 2px rgba(20, 24, 40, 0.06);
  --shadow-md: 0 8px 24px rgba(20, 24, 40, 0.10);
  --sidebar-w: 232px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1016;
    --surface: #161923;
    --surface-alt: #1b1f2b;
    --border: #2a2f3f;
    --text: #e7e9f2;
    --text-muted: #a2a8ba;
    --text-faint: #6e7386;
    --accent: #6c78ff;
    --accent-hover: #7d88ff;
    --accent-soft: #232748;
    --ok: #3ecf8e;
    --ok-soft: #133328;
    --warn: #f2b84b;
    --warn-soft: #382c12;
    --err: #f2545b;
    --err-soft: #3a1c1e;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Shell layout ---------- */

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 22px;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.brand-text strong { font-size: 14.5px; }
.brand-text span { font-size: 11.5px; color: var(--text-muted); }

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 13.5px;
  font-weight: 500;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: left;
  width: 100%;
}

.nav-item svg { width: 17px; height: 17px; flex-shrink: 0; }

.nav-item:hover { background: var(--surface-alt); color: var(--text); }

.nav-item.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.nav-badge {
  margin-left: auto;
  background: var(--border);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 999px;
}

.nav-item.active .nav-badge {
  background: var(--accent);
  color: #fff;
}

.sidebar-footer { padding-top: 12px; border-top: 1px solid var(--border); }

.main {
  flex: 1;
  min-width: 0;
  padding: 28px 36px 60px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
}

.topbar h1 { font-size: 20px; margin: 0; }

/* ---------- Views ---------- */

.view { display: none; flex-direction: column; gap: 20px; }
.view.active { display: flex; }

/* ---------- Panels & cards ---------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 20px 22px;
}

.panel-header { margin-bottom: 16px; }
.panel-header h2 { margin: 0 0 4px; font-size: 15.5px; }
.muted { color: var(--text-muted); font-size: 12.5px; margin: 0; }

.panel-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

.panel-header-row h2 { margin: 0; font-size: 15.5px; }

/* ---------- Stat cards ---------- */

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 14px 16px;
}

.stat-card .stat-value { font-size: 22px; font-weight: 700; line-height: 1.2; }
.stat-card .stat-label { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ---------- Forms ---------- */

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 16px;
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field-wide { grid-column: 1 / -1; }

.field-label { font-size: 12.5px; font-weight: 600; color: var(--text); }
.field-label em { font-weight: 400; font-style: normal; color: var(--text-faint); margin-left: 4px; }

.field-hint { font-size: 11.5px; color: var(--text-faint); line-height: 1.4; }

input[type="text"],
input[type="number"],
input[type="search"] {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 11px;
  font-size: 13.5px;
  width: 100%;
}

input:focus { outline: none; border-color: var(--accent); background: var(--surface); }

.form-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 2px;
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: filter 0.1s ease;
}

.btn-sm { padding: 6px 11px; font-size: 12.5px; }

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

.btn-secondary { background: var(--accent-soft); color: var(--accent); }
.btn-secondary:hover { filter: brightness(0.97); }

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

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

.btn-danger { background: var(--err-soft); color: var(--err); }
.btn-danger:hover { filter: brightness(0.95); }

.btn-block { width: 100%; }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
}
.icon-btn:hover { background: var(--surface-alt); color: var(--text); }

/* ---------- Toolbar / search ---------- */

.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.input-search { width: 220px; }
.export-group { display: flex; gap: 6px; }

select.input-search {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 11px;
  font-size: 13.5px;
  width: 160px;
}

.checkbox-inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ---------- Bulk action bar ---------- */

.bulk-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  color: var(--text);
}

.bulk-bar.hidden { display: none; }
.bulk-bar span { font-weight: 600; }

.checkbox-col { width: 34px; }

/* ---------- Tables ---------- */

.table-scroll { overflow-x: auto; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th {
  position: sticky;
  top: 0;
  background: var(--surface);
  text-align: left;
  padding: 9px 12px;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  max-width: 260px;
}

.data-table td.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.data-table th.num, .data-table td.num { text-align: right; }

.data-table tbody tr:hover { background: var(--surface-alt); }

.data-table a { color: var(--accent); text-decoration: none; }
.data-table a:hover { text-decoration: underline; }

.empty-row td, .loading-row td {
  color: var(--text-faint);
  font-style: italic;
  text-align: center;
  padding: 28px 12px;
}

/* ---------- Badges ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}

.badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.badge-running { background: var(--accent-soft); color: var(--accent); }
.badge-complete { background: var(--ok-soft); color: var(--ok); }
.badge-attention { background: var(--err-soft); color: var(--err); }
.badge-pending { background: var(--warn-soft); color: var(--warn); }
.badge-neutral { background: var(--border); color: var(--text-muted); }

/* ---------- Pagination ---------- */

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 14px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--text-muted);
}

.pagination-controls { display: flex; align-items: center; gap: 6px; }

.pagination button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  font-size: 12.5px;
  cursor: pointer;
}

.pagination button:disabled { opacity: 0.4; cursor: not-allowed; }
.pagination button:not(:disabled):hover { background: var(--surface-alt); }

/* ---------- Recent list (search view) ---------- */

.recent-list { display: flex; flex-direction: column; gap: 8px; }

.recent-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-alt);
}

.recent-item-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.recent-item-main strong { font-size: 13px; }
.recent-item-main span { font-size: 11.5px; color: var(--text-muted); }

/* ---------- Status text ---------- */

.status-text { font-size: 12.5px; color: var(--text-muted); }
.status-text.error { color: var(--err); }
.status-text.success { color: var(--ok); }

/* ---------- Modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 17, 26, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 6vh 20px;
  z-index: 50;
  overflow-y: auto;
}

.modal-backdrop.hidden { display: none; }

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: min(880px, 100%);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
}

.modal.hidden { display: none; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 { margin: 0; font-size: 14.5px; }
.modal-header code { font-size: 12px; color: var(--text-muted); }

.modal-body { padding: 18px 20px; overflow: auto; }

#lead-detail-json {
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  font-size: 12px;
  margin: 0;
}

/* ---------- Toasts ---------- */

.toast-stack {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 60;
}

.toast {
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  box-shadow: var(--shadow-md);
  max-width: 340px;
}

.toast.toast-error { background: var(--err); color: #fff; }
.toast.toast-success { background: var(--ok); color: #fff; }

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    height: auto;
    position: static;
    flex-direction: row;
    align-items: center;
    padding: 10px 14px;
  }
  .brand { padding: 0 10px 0 0; }
  .nav { flex-direction: row; overflow-x: auto; }
  .nav-item { white-space: nowrap; }
  .sidebar-footer { display: none; }
  .main { padding: 18px; }
  .input-search { width: 160px; }
}
