:root {
  --bg: #0b1120;
  --bg-soft: #0f172a;
  --panel: #111827;
  --panel-2: #1a2333;
  --border: #1f2937;
  --border-soft: #263043;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --muted-2: #64748b;
  --accent: #3b82f6;
  --accent-soft: rgba(59, 130, 246, 0.14);
  --success: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --purple: #8b5cf6;
  --pink: #ec4899;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --sidebar-w: 244px;
  --sidebar-w-collapsed: 64px;
  --topbar-h: 58px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
}

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

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #263043; border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: #33415a; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Layout (fit to viewport) ---------- */
.layout { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 auto;
  background: linear-gradient(180deg, #0c1424 0%, #0a0f1c 100%);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  overflow-y: auto;
  overflow-x: hidden;
  transition: width .18s ease, padding .18s ease;
  z-index: 40;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 2px 6px 14px; }
.brand .logo {
  width: 34px; height: 34px; border-radius: 10px; flex: 0 0 auto;
  background: linear-gradient(135deg, var(--accent), var(--purple));
  display: grid; place-items: center; font-weight: 800; color: #fff; font-size: 15px;
}
.brand .meta { min-width: 0; }
.brand .name { font-weight: 700; font-size: 14.5px; letter-spacing: .2px; white-space: nowrap; }
.brand .sub { font-size: 10.5px; color: var(--muted-2); white-space: nowrap; }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav .section {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted-2); padding: 13px 10px 5px; white-space: nowrap;
}
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border-radius: var(--radius-sm); color: var(--muted); font-weight: 500;
  text-decoration: none; transition: background .15s, color .15s; white-space: nowrap;
}
.nav a:hover { background: var(--panel); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: #bfdbfe; }
.nav a .ico { width: 18px; height: 18px; flex: 0 0 auto; display: grid; place-items: center; opacity: .9; }
.nav a .ico svg { width: 17px; height: 17px; }
.nav a .count {
  margin-left: auto; font-size: 11px; background: var(--panel-2); color: var(--muted);
  border-radius: 20px; padding: 1px 8px;
}

.sidebar .foot { border-top: 1px solid var(--border); padding-top: 10px; margin-top: 10px; }
.sidebar .foot .who { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar .foot .who b { color: var(--text); }
.sidebar .foot .who .badge { margin-left: 4px; }

.sidebar.collapsed { width: var(--sidebar-w-collapsed); padding-left: 9px; padding-right: 9px; }
.sidebar.collapsed .brand .meta,
.sidebar.collapsed .nav .section,
.sidebar.collapsed .nav a .txt,
.sidebar.collapsed .nav a .count,
.sidebar.collapsed .foot .who { display: none; }
.sidebar.collapsed .brand { justify-content: center; padding-left: 0; padding-right: 0; }
.sidebar.collapsed .nav a { justify-content: center; padding: 9px; }
.sidebar.collapsed .nav .section { padding: 8px 0 2px; height: 1px; }

.sidebar-toggle {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  background: transparent; border: 1px solid var(--border-soft); color: var(--muted);
  border-radius: var(--radius-sm); padding: 6px; cursor: pointer; font: inherit; font-size: 12px;
}
.sidebar-toggle:hover { color: var(--text); border-color: var(--accent); }
.sidebar.collapsed .sidebar-toggle .txt { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

.topbar {
  flex: 0 0 auto; height: var(--topbar-h); z-index: 30;
  display: flex; align-items: center; gap: 14px;
  padding: 0 20px; background: rgba(11, 17, 32, .88);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.topbar .title-block { min-width: 0; }
.topbar h1 { font-size: 17px; margin: 0; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }
.topbar .search { flex: 0 1 320px; min-width: 140px; }
.topbar .search .ac-input { padding: 7px 12px; font-size: 13px; }

.content {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 18px 20px 28px;
  display: flex; flex-direction: column; gap: 14px;
}
.content.fit { overflow: hidden; padding-bottom: 16px; }
.page { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 14px; }
.page > .fill { flex: 1; min-height: 0; }
.fill { min-height: 0; }
.stack { display: flex; flex-direction: column; }
.grow { flex: 1; min-height: 0; }
.scroll-y { overflow-y: auto; }
.hidden { display: none !important; }

.live {
  display: inline-flex; align-items: center; gap: 7px; font-size: 12px;
  color: var(--muted); background: var(--panel); border: 1px solid var(--border);
  padding: 5px 11px; border-radius: 30px; white-space: nowrap;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted-2); flex: 0 0 auto; }
.dot.on { background: var(--success); box-shadow: 0 0 0 0 rgba(34,197,94,.6); animation: pulse 1.8s infinite; }
.dot.busy { background: var(--warn); animation: pulse 1.2s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(34,197,94,.5);} 70% { box-shadow: 0 0 0 8px rgba(34,197,94,0);} 100% { box-shadow: 0 0 0 0 rgba(34,197,94,0);} }

/* ---------- Cards / panels ---------- */
.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 15px;
}
.card + .card { margin-top: 0; }
.card h2, .card h3 { margin: 0 0 10px; font-weight: 620; }
.card h2 { font-size: 15.5px; }
.card h3 { font-size: 14px; }
.card.compact { padding: 12px 14px; }
.card.flat { border-radius: var(--radius-sm); }

.grid { display: grid; gap: 14px; }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
@media (max-width: 1100px) { .grid.cols-4 { grid-template-columns: repeat(2,1fr); } }
@media (max-width: 820px) { .grid.cols-4, .grid.cols-3, .grid.cols-2 { grid-template-columns: 1fr; } }

.stat {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 15px; display: flex; flex-direction: column; gap: 2px;
}
.stat .label { color: var(--muted); font-size: 12px; }
.stat .value { font-size: 25px; font-weight: 720; line-height: 1.15; }
.stat .value.small { font-size: 20px; }
.stats-strip { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 12px; }
@media (max-width: 900px) { .stats-strip { grid-template-columns: repeat(2,1fr); } }

.split { display: flex; gap: 14px; flex: 1; min-height: 0; align-items: stretch; }
.split > .side { flex: 0 0 336px; display: flex; flex-direction: column; min-height: 0; }
.split > .main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
@media (max-width: 1080px) {
  .split { flex-direction: column; }
  .split > .side { flex: 0 0 auto; max-height: 320px; }
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px; justify-content: center;
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border-soft);
  padding: 8px 13px; border-radius: var(--radius-sm); font-weight: 550; font-size: 13px;
  cursor: pointer; transition: filter .15s, background .15s, border-color .15s; white-space: nowrap;
}
.btn:hover { filter: brightness(1.15); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.success { background: var(--success); border-color: transparent; color: #04220f; }
.btn.danger { background: rgba(239,68,68,.14); border-color: rgba(239,68,68,.4); color: #fca5a5; }
.btn.ghost { background: transparent; }
.btn.sm { padding: 5px 9px; font-size: 12px; }
.btn.block { width: 100%; }
.btn.icon { padding: 7px; }

/* ---------- Forms ---------- */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; font-weight: 550; }
input, select, textarea {
  width: 100%; background: var(--bg-soft); color: var(--text);
  border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  padding: 9px 11px; font-size: 13.5px; font-family: inherit; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 96px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; margin-bottom: 9px; }
.check input { width: auto; }
.inline { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; }
.inline > * { flex: 1 1 160px; }
.hint { color: var(--muted-2); font-size: 11.5px; margin-top: 5px; }

/* ---------- Filter bar ---------- */
.filters { display: flex; flex-direction: column; gap: 10px; }
.filters .row { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; }
.filters .row > .field { flex: 1 1 170px; margin: 0; }
.filters .row > .field.wide { flex: 2 1 280px; }
.filters .row > .field.tiny { flex: 0 0 110px; }
.filters-advanced { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 550;
  padding: 4px 10px; border-radius: 30px; background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--border-soft); cursor: pointer; user-select: none;
}
.chip:hover { color: var(--text); }
.chip.active { background: var(--accent-soft); color: #bfdbfe; border-color: rgba(59,130,246,.4); }
.chip .x { opacity: .7; }

/* ---------- Autocomplete ---------- */
.ac-wrap { position: relative; }
.ac-list {
  position: absolute; z-index: 70; left: 0; right: 0; top: calc(100% + 4px);
  background: var(--panel-2); border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  box-shadow: var(--shadow); max-height: 280px; overflow-y: auto; display: none; padding: 4px;
}
.ac-list.open { display: block; }
.ac-item { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 7px; cursor: pointer; font-size: 13px; }
.ac-item:hover, .ac-item.active { background: var(--accent-soft); }
.ac-item .ac-main { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ac-item .ac-type { margin-left: auto; font-size: 10.5px; color: var(--muted-2); text-transform: uppercase; letter-spacing: .04em; flex: 0 0 auto; }
.ac-empty { padding: 8px 10px; color: var(--muted-2); font-size: 12.5px; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin-bottom: 14px; }
.tabs button {
  background: transparent; border: none; color: var(--muted); font: inherit; font-weight: 560;
  padding: 9px 13px; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px;
  white-space: nowrap;
}
.tabs button:hover { color: var(--text); }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.tab-panel.fill.active { display: flex; flex-direction: column; flex: 1; min-height: 0; }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 600;
  padding: 3px 9px; border-radius: 30px; background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--border-soft); white-space: nowrap;
}
.badge.ok { background: rgba(34,197,94,.14); color: #86efac; border-color: rgba(34,197,94,.3); }
.badge.warn { background: rgba(245,158,11,.14); color: #fcd34d; border-color: rgba(245,158,11,.3); }
.badge.danger { background: rgba(239,68,68,.14); color: #fca5a5; border-color: rgba(239,68,68,.3); }
.badge.info { background: rgba(59,130,246,.14); color: #93c5fd; border-color: rgba(59,130,246,.3); }
.badge.purple { background: rgba(139,92,246,.16); color: #c4b5fd; border-color: rgba(139,92,246,.32); }
.badge.muted { color: var(--muted-2); }

/* ---------- Tables ---------- */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); }
.table-wrap.fill { flex: 1; min-height: 0; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted-2); padding: 10px 13px; background: var(--bg-soft);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 1;
}
tbody td { padding: 9px 13px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:hover { background: rgba(255,255,255,.02); }
tbody tr:last-child td { border-bottom: none; }
td.actions { text-align: right; white-space: nowrap; }
.mono { font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; font-size: 12.5px; }

/* ---------- Kanban / funnel ---------- */
.kanban { display: flex; gap: 12px; overflow-x: auto; overflow-y: hidden; padding-bottom: 4px; flex: 1; min-height: 0; align-items: stretch; }
.col {
  flex: 0 0 272px; background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: var(--radius); display: flex; flex-direction: column; min-height: 0;
  transition: border-color .15s, background .15s;
}
.col.col-over { border-color: var(--accent); background: rgba(59,130,246,.06); }
.col .col-head {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  border-bottom: 1px solid var(--border); background: var(--bg-soft);
  border-radius: var(--radius) var(--radius) 0 0; flex: 0 0 auto;
}
.col .col-head .name { font-weight: 620; font-size: 13px; }
.col .col-head .n { margin-left: auto; font-size: 11.5px; color: var(--muted); background: var(--panel-2); border-radius: 20px; padding: 1px 9px; }
.col .col-body { padding: 9px; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1; min-height: 0; }
.lead-card {
  background: var(--panel); border: 1px solid var(--border-soft); border-radius: var(--radius-sm);
  padding: 10px 11px; cursor: grab; transition: border-color .15s, transform .1s, opacity .15s;
}
.lead-card:hover { border-color: var(--accent); }
.lead-card.dragging { opacity: .45; cursor: grabbing; }
.lead-card .t { font-weight: 580; font-size: 13px; margin-bottom: 3px; }
.lead-card .d { font-size: 11.5px; color: var(--muted); word-break: break-all; }
.lead-card .row { display: flex; align-items: center; gap: 6px; margin-top: 7px; flex-wrap: wrap; }
.lead-card .owner { margin-left: auto; font-size: 11px; color: var(--muted-2); }
.col-empty { color: var(--muted-2); font-size: 12.5px; text-align: center; padding: 14px 0; }

/* ---------- Progress ---------- */
.progress { height: 6px; background: var(--panel-2); border-radius: 20px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--purple)); border-radius: 20px; transition: width .4s ease; }
.progress.indet > i { width: 35% !important; animation: slide 1.2s infinite ease-in-out; }
@keyframes slide { 0% { margin-left: -35%; } 100% { margin-left: 100%; } }

/* ---------- Job cards ---------- */
.job { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; }
.job:last-child { margin-bottom: 0; }
.job .head { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.job .title { font-weight: 620; font-size: 13.5px; }
.job .meta { color: var(--muted-2); font-size: 11.5px; }
.job .body { margin-top: 9px; }
.job .items { margin-top: 10px; display: flex; flex-direction: column; gap: 5px; max-height: 200px; overflow-y: auto; }
.job .item { display: flex; align-items: center; gap: 8px; font-size: 12px; padding: 4px 8px; border-radius: 7px; background: var(--bg-soft); }
.job .item .lbl { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job .item .det { color: var(--muted-2); font-size: 11px; }

/* ---------- Console ---------- */
.console {
  background: #05080f; border: 1px solid var(--border); border-radius: var(--radius);
  font-family: ui-monospace, Consolas, monospace; font-size: 12.2px; padding: 11px 13px;
  flex: 1; min-height: 0; overflow-y: auto; line-height: 1.6;
}
.console .ln { white-space: pre-wrap; word-break: break-word; }
.console .ts { color: var(--muted-2); margin-right: 8px; }
.console .info { color: #cbd5e1; }
.console .success { color: #86efac; }
.console .warn { color: #fcd34d; }
.console .error { color: #fca5a5; }

/* ---------- Modal ---------- */
.modal-back {
  position: fixed; inset: 0; background: rgba(3,7,18,.72); backdrop-filter: blur(3px);
  display: none; align-items: flex-start; justify-content: center; z-index: 80; padding: 40px 16px;
}
.modal-back.open { display: flex; }
.modal {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  width: min(680px, 100%); max-height: 86vh; overflow-y: auto; box-shadow: var(--shadow);
}
.modal .modal-head { display: flex; align-items: center; padding: 15px 19px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.modal .modal-head h3 { margin: 0; font-size: 15.5px; }
.modal .modal-head .x { margin-left: auto; background: none; border: none; color: var(--muted); font-size: 20px; cursor: pointer; }
.modal .modal-body { padding: 18px; }
.modal .modal-foot { padding: 13px 18px; border-top: 1px solid var(--border); display: flex; gap: 10px; justify-content: flex-end; position: sticky; bottom: 0; background: var(--panel); }

/* ---------- Toast ---------- */
#toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 120; }
.toast { background: var(--panel); border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: 10px; padding: 11px 15px; min-width: 260px; box-shadow: var(--shadow); animation: fadein .2s; }
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }
.toast.warn { border-left-color: var(--warn); }
@keyframes fadein { from { opacity: 0; transform: translateY(8px);} to { opacity: 1; transform: none; } }

/* ---------- Misc ---------- */
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.muted { color: var(--muted); }
.muted-2 { color: var(--muted-2); }
.small { font-size: 12px; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }
.pill { font-size: 11.5px; padding: 2px 8px; border-radius: 20px; background: var(--panel-2); color: var(--muted); }
.empty { text-align: center; color: var(--muted-2); padding: 34px 10px; }
.timeline { position: relative; padding-left: 20px; }
.timeline::before { content: ""; position: absolute; left: 6px; top: 4px; bottom: 4px; width: 2px; background: var(--border); }
.timeline .entry { position: relative; padding: 0 0 14px; }
.timeline .entry::before { content: ""; position: absolute; left: -18px; top: 5px; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); border: 2px solid var(--bg); }
.timeline .entry .when { font-size: 11.5px; color: var(--muted-2); }
.timeline .entry .what { font-size: 13px; }
.avatar { width: 24px; height: 24px; border-radius: 50%; background: var(--accent-soft); color: #bfdbfe; display: inline-grid; place-items: center; font-size: 11px; font-weight: 700; }
.kv { display: grid; grid-template-columns: 128px 1fr; gap: 5px 12px; font-size: 13px; }
.kv .k { color: var(--muted-2); }
.score-pill { font-weight: 700; }
.score-pill.ok { color: #86efac; }
.score-pill.warn { color: #fcd34d; }
.score-pill.danger { color: #fca5a5; }
.score-pill.muted { color: var(--muted-2); }

@media (max-width: 780px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; transform: translateX(-100%); transition: transform .2s; width: var(--sidebar-w); }
  .sidebar.open { transform: none; }
  .sidebar.collapsed { width: var(--sidebar-w); }
  .sidebar.collapsed .brand .meta, .sidebar.collapsed .nav .section,
  .sidebar.collapsed .nav a .txt, .sidebar.collapsed .nav a .count,
  .sidebar.collapsed .foot .who { display: revert; }
  .main { margin-left: 0; }
  .content { padding: 14px; }
  .menu-btn { display: inline-flex !important; }
  .topbar .search { display: none; }
}
.menu-btn { display: none; }
