:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fa;
  --line: #e3e5e9;
  --line-strong: #cfd3d9;
  --text: #15171a;
  --text-2: #4f5560;
  --muted: #7b818c;
  --accent: #1f7cff;
  --accent-ink: #ffffff;
  --accent-soft: #e5efff;
  --track: #d7e6fb;
  --series: #2a78d6;
  --series-dim: #9ec5f4;
  --good: #0ca30c;
  --good-ink: #0b6e0b;
  --warning: #fab219;
  --warning-ink: #8a5a00;
  --serious: #ec835a;
  --critical: #d03b3b;
  --critical-ink: #b42b2b;
  --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: #111214; --surface: #1a1b1e; --surface-2: #212226; --line: #2c2e33; --line-strong: #3a3d44;
    --text: #eef0f3; --text-2: #b9bec7; --muted: #8b919c;
    --accent: #3b8cff; --accent-ink: #fff; --accent-soft: #1b2a44; --track: #1d3354;
    --series: #3987e5; --series-dim: #1c5cab;
    --good-ink: #5bd35b; --warning-ink: #fab219; --critical-ink: #ff7b7b;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111214; --surface: #1a1b1e; --surface-2: #212226; --line: #2c2e33; --line-strong: #3a3d44;
  --text: #eef0f3; --text-2: #b9bec7; --muted: #8b919c;
  --accent: #3b8cff; --accent-ink: #fff; --accent-soft: #1b2a44; --track: #1d3354;
  --series: #3987e5; --series-dim: #1c5cab;
  --good-ink: #5bd35b; --warning-ink: #fab219; --critical-ink: #ff7b7b;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 "Barlow", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: .01em; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ── Layout ─────────────────────────────────────────── */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side {
  background: #16181c; color: #c9ced6; padding: 18px 12px; position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; color: #fff; font-weight: 600; font-size: 17px; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: #c9ced6; font-weight: 500;
}
.nav a:hover { background: #22252b; color: #fff; }
.nav a.active { background: #1f7cff22; color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.nav svg { width: 18px; height: 18px; flex: none; }
.nav .badge { margin-left: auto; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; padding: 8px; font-size: 13px; }
.side-foot button { background: none; border: 0; color: #8b919c; text-align: left; padding: 4px 0; cursor: pointer; }
.side-foot button:hover { color: #fff; }

main { padding: 24px 28px 60px; max-width: 1400px; width: 100%; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head .spacer { flex: 1; }

.topbar { display: none; }
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; z-index: 40; left: 0; top: 0; width: 240px; transform: translateX(-100%); transition: transform .2s; }
  .side.open { transform: none; }
  .topbar {
    display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: #16181c; color: #fff;
    position: sticky; top: 0; z-index: 30;
  }
  .topbar button { background: none; border: 0; color: #fff; padding: 4px; cursor: pointer; display: flex; }
  .topbar svg { width: 22px; height: 22px; }
  main { padding: 16px 16px 60px; }
}

/* ── Primitives ─────────────────────────────────────── */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; min-width: 0;
}
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.card-head .spacer { flex: 1; }
.grid { display: grid; gap: 14px; }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 1100px) { .cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 760px) { .cols-2, .cols-3 { grid-template-columns: 1fr; } }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--text); padding: 7px 12px; border-radius: 8px; cursor: pointer; font-weight: 500;
  font-size: 14px; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 4px 9px; font-size: 13px; }
.btn svg { width: 16px; height: 16px; flex: none; }
.btn.danger { color: var(--critical-ink); }
.btn:disabled { opacity: .5; cursor: default; }

.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 9px; padding: 3px; }
.seg button { border: 0; background: none; padding: 5px 14px; border-radius: 6px; cursor: pointer; color: var(--text-2); font-weight: 500; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

input[type=text], input[type=number], input[type=date], input[type=password], input[type=search], select, textarea {
  border: 1px solid var(--line-strong); background: var(--surface); border-radius: 8px; padding: 7px 10px; width: 100%;
  min-width: 0;
}
textarea { resize: vertical; min-height: 70px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); font-weight: 500; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }

.badge {
  display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 99px;
  background: var(--surface-2); color: var(--text-2); border: 1px solid var(--line); white-space: nowrap;
}
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.crit { background: color-mix(in srgb, var(--critical) 14%, transparent); color: var(--critical-ink); border-color: transparent; }
.badge.warn { background: color-mix(in srgb, var(--warning) 20%, transparent); color: var(--warning-ink); border-color: transparent; }
.badge.ok { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--good-ink); border-color: transparent; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }

.status { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; font-weight: 600; }
.status svg { width: 15px; height: 15px; flex: none; }
.status.ok { color: var(--good-ink); }
.status.warn { color: var(--warning-ink); }
.status.crit { color: var(--critical-ink); }
.status.off { color: var(--muted); }

table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.tbl td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl td.r, .tbl th.r { text-align: right; }
.table-wrap { overflow-x: auto; }

.empty { color: var(--muted); padding: 18px 4px; text-align: center; font-size: 14px; }

/* ── KPI tiles ──────────────────────────────────────── */
.section-title { display: flex; align-items: baseline; gap: 10px; margin: 22px 0 10px; }
.section-title h2 { font-size: 15px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-2); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.tile { padding: 14px 16px; display: flex; flex-direction: column; gap: 6px; cursor: default; }
.tile.clickable { cursor: pointer; }
.tile.clickable:hover { border-color: var(--accent); }
.tile-label { font-size: 13px; color: var(--text-2); font-weight: 500; display: flex; gap: 6px; align-items: center; }
.tile-value { font-size: 28px; font-weight: 600; line-height: 1.1; }
.tile-value .of { font-size: 15px; color: var(--muted); font-weight: 500; }
.tile-foot { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); flex-wrap: wrap; min-height: 18px; }
.delta { font-weight: 600; }
.delta.good { color: var(--good-ink); }
.delta.bad { color: var(--critical-ink); }
.meter { height: 6px; border-radius: 3px; background: var(--track); overflow: hidden; }
.meter > i { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.meter.warn > i { background: var(--warning); }
.meter.crit > i { background: var(--critical); }
.meter.done > i { background: var(--good); }

.hero { display: flex; align-items: flex-end; gap: 18px; flex-wrap: wrap; }
.hero-value { font-size: 48px; font-weight: 600; line-height: 1; }

/* ── Chart ──────────────────────────────────────────── */
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .target-line { stroke: var(--text-2); stroke-width: 1.5; stroke-dasharray: 4 4; }
.chart .bar { fill: var(--series); }
.chart .bar.dim { fill: var(--series-dim); }
.chart .hit { fill: transparent; cursor: crosshair; }
.chart .hit:hover + .bar, .chart .bar.hover { filter: brightness(1.15); }
.tooltip {
  position: absolute; pointer-events: none; background: var(--text); color: var(--bg); padding: 6px 9px; border-radius: 6px;
  font-size: 12.5px; white-space: nowrap; transform: translate(-50%, -110%); z-index: 5;
}
details.table-view summary { cursor: pointer; color: var(--muted); font-size: 13px; margin-top: 8px; }

/* ── Funnel ─────────────────────────────────────────── */
.funnel-row { display: grid; grid-template-columns: 110px 1fr 70px 90px; align-items: center; gap: 10px; padding: 5px 0; font-size: 14px; }
.funnel-bar { height: 14px; background: var(--track); border-radius: 0 4px 4px 0; position: relative; }
.funnel-bar > i { position: absolute; left: 0; top: 0; bottom: 0; background: var(--series); border-radius: 0 4px 4px 0; min-width: 2px; }

/* ── Checklist & tasks ──────────────────────────────── */
.check { display: flex; align-items: flex-start; gap: 10px; padding: 9px 4px; border-bottom: 1px solid var(--line); }
.check:last-child { border-bottom: 0; }
.check input[type=checkbox] { width: 18px; height: 18px; margin: 2px 0 0; accent-color: var(--accent); flex: none; cursor: pointer; }
.check .body { flex: 1; min-width: 0; }
.check .title { font-weight: 500; }
.check.done .title { color: var(--muted); text-decoration: line-through; }
.check .sub { font-size: 12.5px; color: var(--muted); display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 2px; }
.prio-1 { box-shadow: inset 3px 0 0 var(--critical); padding-left: 10px; }
.progress-ring { font-size: 13px; color: var(--text-2); font-weight: 600; }

/* ── Pipeline board ─────────────────────────────────── */
.board { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(250px, 1fr); gap: 12px; overflow-x: auto; padding-bottom: 12px; }
.col { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; max-height: calc(100vh - 170px); min-width: 0; }
.col-head { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.col-head .row { justify-content: space-between; }
.col-body { padding: 8px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; min-height: 80px; }
.col.drop { outline: 2px dashed var(--accent); outline-offset: -4px; }
.lead-card { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; cursor: pointer; font-size: 13.5px; }
.lead-card:hover { border-color: var(--accent); }
.lead-card .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead-card .meta { color: var(--muted); font-size: 12.5px; display: flex; gap: 8px; flex-wrap: wrap; margin-top: 3px; }
.lead-card.stale { box-shadow: inset 3px 0 0 var(--warning); }

/* ── Drawer ─────────────────────────────────────────── */
.scrim { position: fixed; inset: 0; background: rgba(10, 12, 16, .45); z-index: 50; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100vw); background: var(--bg); z-index: 51;
  overflow-y: auto; box-shadow: -8px 0 30px rgba(0,0,0,.2); display: flex; flex-direction: column;
}
.drawer-head { position: sticky; top: 0; background: var(--surface); border-bottom: 1px solid var(--line); padding: 14px 18px; display: flex; gap: 10px; align-items: flex-start; z-index: 2; }
.drawer-body { padding: 16px 18px 40px; display: flex; flex-direction: column; gap: 14px; }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 4px 10px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.stage-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.stage-picker button { border: 1px solid var(--line-strong); background: var(--surface); border-radius: 99px; padding: 4px 11px; font-size: 13px; cursor: pointer; }
.stage-picker button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.timeline { display: flex; flex-direction: column; gap: 8px; }
.tl-item { border-left: 2px solid var(--line-strong); padding: 2px 0 2px 10px; font-size: 13.5px; }
.tl-item.in { border-color: var(--good); }
.tl-item.out { border-color: var(--accent); }
.tl-item .when { color: var(--muted); font-size: 12px; }
.tl-item .txt { white-space: pre-wrap; overflow-wrap: anywhere; }

/* ── Engine ─────────────────────────────────────────── */
.prov { display: flex; flex-direction: column; gap: 8px; }
.prov .summary { font-size: 14px; }
.prov.crit { border-color: color-mix(in srgb, var(--critical) 50%, var(--line)); }
.prov.warn { border-color: color-mix(in srgb, var(--warning) 60%, var(--line)); }
.stat-row { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.stat-row > div { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
.stat-row .k { font-size: 12px; color: var(--muted); }
.stat-row .v { font-size: 17px; font-weight: 600; }

/* ── Login ──────────────────────────────────────────── */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(380px, 100%); padding: 28px; display: flex; flex-direction: column; gap: 14px; }

.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 9px 16px; border-radius: 8px; z-index: 60; font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
.toast.err { background: var(--critical); color: #fff; }
