:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e1e4ea;
  --text: #1b1f27;
  --muted: #667085;
  --accent: #2f5bd3;
  --accent-soft: #e8eefc;
  --danger: #c93a3a;
  --danger-soft: #fdecec;
  --warn: #a8660a;
  --warn-soft: #fdf3e2;
  --ok: #1f7a4d;
  --ok-soft: #e5f4ec;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121418; --surface: #1a1d23; --surface-2: #22262e; --border: #2e333c;
    --text: #e7e9ee; --muted: #9aa3b2; --accent: #7196ff; --accent-soft: #1f2a47;
    --danger: #ff7b7b; --danger-soft: #3a1f22; --warn: #f0b35a; --warn-soft: #3a2d18;
    --ok: #5fd39a; --ok-soft: #173327; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121418; --surface: #1a1d23; --surface-2: #22262e; --border: #2e333c;
  --text: #e7e9ee; --muted: #9aa3b2; --accent: #7196ff; --accent-soft: #1f2a47;
  --danger: #ff7b7b; --danger-soft: #3a1f22; --warn: #f0b35a; --warn-soft: #3a2d18;
  --ok: #5fd39a; --ok-soft: #173327; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; }
.sub { color: var(--muted); margin: 4px 0 0; }
.muted { color: var(--muted); font-weight: 400; }

.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 56px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; white-space: nowrap; }
.logo {
  display: grid; place-items: center; width: 26px; height: 26px; border-radius: 7px;
  background: var(--accent); color: #fff; font-size: 14px;
}
.tabs { display: flex; gap: 4px; height: 100%; overflow-x: auto; }
.tabs a {
  display: flex; align-items: center; padding: 0 12px; color: var(--muted); text-decoration: none;
  font-weight: 500; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs a:hover { color: var(--text); }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }

main { max-width: 1400px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px;
}
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
@media (min-width: 1100px) { .kpis { grid-template-columns: repeat(6, 1fr); } }
@media (max-width: 640px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 26px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi.alert .value { color: var(--danger); }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.grid2 .card { margin-bottom: 0; }
.grid2 { margin-bottom: 16px; }

table { border-collapse: collapse; width: 100%; }
.summary td, .summary th { padding: 7px 8px; text-align: left; border-bottom: 1px solid var(--border); }
.summary th { color: var(--muted); font-weight: 500; font-size: 12px; }
.summary .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.summary tr.total td { font-weight: 650; border-bottom: none; }
.summary .bar-cell { width: 40%; }
.bar { height: 8px; border-radius: 4px; background: var(--accent); min-width: 2px; opacity: .85; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }
.toolbar input[type=search] { flex: 1 1 240px; max-width: 360px; }
.toolbar .count { color: var(--muted); margin-left: auto; font-size: 13px; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
textarea { resize: vertical; min-height: 64px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.btn {
  font: inherit; font-weight: 550; cursor: pointer; border-radius: 8px; padding: 7px 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger.ghost { color: var(--danger); border-color: transparent; background: transparent; }
.btn.danger.ghost:hover { background: var(--danger-soft); }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { border: none; background: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; box-shadow: var(--shadow); }
.data th, .data td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.data th {
  position: sticky; top: 0; background: var(--surface-2); font-size: 12px; font-weight: 600;
  color: var(--muted); cursor: pointer; user-select: none;
}
.data th:hover { color: var(--text); }
.data th .arrow { font-size: 10px; margin-left: 3px; }
.data tbody tr { cursor: pointer; }
.data tbody tr:hover { background: var(--accent-soft); }
.data tbody tr:last-child td { border-bottom: none; }
.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
.data td.notes { max-width: 260px; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.data td.id { color: var(--muted); font-variant-numeric: tabular-nums; }
.data td.strong { font-weight: 550; }
.data .overdue { color: var(--danger); font-weight: 600; }
.empty { padding: 32px; text-align: center; color: var(--muted); }

.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 550; background: var(--surface-2); color: var(--text); }
.pill.New { background: var(--accent-soft); color: var(--accent); }
.pill.Contacted, .pill.Qualified { background: var(--surface-2); }
.pill.Proposal, .pill.At-Risk { background: var(--warn-soft); color: var(--warn); }
.pill.Won, .pill.Active { background: var(--ok-soft); color: var(--ok); }
.pill.Lost, .pill.Churned { background: var(--danger-soft); color: var(--danger); }

dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(720px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
dialog.small { width: min(420px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(15, 18, 25, .45); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 0; }
.dlg-head h2 { margin: 0; font-size: 17px; }
.dlg-body { padding: 16px 20px; max-height: 70vh; overflow: auto; }
.dlg-foot { display: flex; gap: 8px; padding: 12px 20px 16px; border-top: 1px solid var(--border); align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.wide { grid-column: 1 / -1; }
.field label { font-size: 12px; font-weight: 550; color: var(--muted); }
.field .ro { padding: 7px 0; color: var(--muted); }
.form-error { margin: 0 20px 12px; padding: 8px 12px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 500;
  transition: .2s; pointer-events: none; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); color: #fff; }

@media (max-width: 640px) {
  .topbar { padding: 0 16px; gap: 12px; }
  .brand { font-size: 0; gap: 0; }
  .brand .logo { font-size: 14px; }
  main { padding: 16px; }
  .fields { grid-template-columns: 1fr; }
  .toolbar input[type=search] { max-width: none; }
  .toolbar .count { margin-left: 0; width: 100%; }
}
