:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f1f3f6;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #1f4fd6;
  --accent-soft: #e8eefc;
  --danger: #c2341b;
  --ok: #157a4a;
  --warn: #a55f00;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 10px;
  --s-lead: #6b7280; --s-audited: #7c3aed; --s-contacted: #1f4fd6; --s-replied: #0e7490;
  --s-call_booked: #a55f00; --s-proposal_sent: #c2410c; --s-won: #157a4a; --s-lost: #9f1239; --s-retainer: #065f46;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0d1117; --panel: #161b22; --panel-2: #1f252e; --text: #e6e8eb; --muted: #9aa3ae; --border: #2b323c;
    --accent: #6d93ff; --accent-soft: #1b2847; --danger: #ff7b63; --ok: #4ccb8a; --warn: #f0b354;
    --shadow: none;
    --s-lead: #9aa3ae; --s-audited: #b794ff; --s-contacted: #6d93ff; --s-replied: #4cc3dc;
    --s-call_booked: #f0b354; --s-proposal_sent: #ff9a5a; --s-won: #4ccb8a; --s-lost: #ff7096; --s-retainer: #3fd6a8;
    color-scheme: dark;
  }
}
:root[data-theme='dark'] {
  --bg: #0d1117; --panel: #161b22; --panel-2: #1f252e; --text: #e6e8eb; --muted: #9aa3ae; --border: #2b323c;
  --accent: #6d93ff; --accent-soft: #1b2847; --danger: #ff7b63; --ok: #4ccb8a; --warn: #f0b354; --shadow: none;
  --s-lead: #9aa3ae; --s-audited: #b794ff; --s-contacted: #6d93ff; --s-replied: #4cc3dc;
  --s-call_booked: #f0b354; --s-proposal_sent: #ff9a5a; --s-won: #4ccb8a; --s-lost: #ff7096; --s-retainer: #3fd6a8;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, Inter, sans-serif; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.boot { padding: 40px; color: var(--muted); }

/* layout */
.shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; background: linear-gradient(to right, var(--panel) 220px, var(--bg) 220px); }
.shell > div { min-width: 0; }
.side { background: var(--panel); border-right: 1px solid var(--border); padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 4px 10px 18px; font-size: 15px; }
.brand .logo { width: 26px; height: 26px; border-radius: 7px; background: var(--text); color: var(--bg); display: grid; place-items: center; font-size: 14px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text); font-weight: 500; }
.nav a:hover { background: var(--panel-2); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav .count { margin-left: auto; background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; font-weight: 600; }
.side .spacer { flex: 1; }
main { padding: 24px 28px 60px; min-width: 0; max-width: 1400px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .grow { flex: 1; }

/* panels */
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.lead-layout { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .value { font-size: 26px; font-weight: 650; letter-spacing: -0.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: 12px; }
.kpi.alert .value { color: var(--danger); }

/* pipeline */
.pipeline { display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); gap: 8px; }
.pipe { border: 1px solid var(--border); border-radius: 8px; padding: 10px; background: var(--panel-2); color: var(--text); display: block; border-top: 3px solid var(--c); }
.pipe:hover { text-decoration: none; border-color: var(--c); }
.pipe .n { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; }
.pipe .l { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bars { display: flex; align-items: flex-end; gap: 3px; height: 90px; }
.bars div { flex: 1; background: var(--accent); border-radius: 3px 3px 0 0; min-height: 2px; opacity: 0.85; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); align-items: baseline; }
.list li:last-child { border-bottom: 0; }
.list .grow { flex: 1; min-width: 0; }

/* table */
.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--panel-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--panel-2); }
td.name a { color: var(--text); font-weight: 600; }
.ellipsis { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* pills */
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 1px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: color-mix(in srgb, var(--c) 14%, transparent); color: var(--c); }
.pill::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--c); }
.tag { display: inline-block; padding: 0 8px; border-radius: 6px; background: var(--panel-2); border: 1px solid var(--border); font-size: 12px; margin: 0 4px 4px 0; }
.score { font-variant-numeric: tabular-nums; font-weight: 600; }
.score.hi { color: var(--ok); } .score.mid { color: var(--warn); } .score.lo { color: var(--muted); }
.st-created { color: var(--ok); } .st-merged { color: var(--accent); } .st-rejected { color: var(--danger); } .st-error { color: var(--danger); } .st-pending { color: var(--warn); }
.overdue { color: var(--danger); font-weight: 600; }

/* forms */
input, select, textarea, button { font: inherit; color: inherit; }
input[type='text'], input[type='search'], input[type='email'], input[type='password'], input[type='url'], input[type='number'], input[type='date'], input[type='datetime-local'], select, textarea {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-width: 0; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; min-height: 70px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filters > * { width: auto; }
.filters input[type='search'] { min-width: 240px; flex: 1; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
label.field > span { padding-left: 2px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 12px; }
.form-grid .full { grid-column: 1 / -1; }
button, .btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 7px 14px; cursor: pointer; font-weight: 550; white-space: nowrap; color: var(--text); }
button:hover, .btn:hover { background: var(--panel-2); text-decoration: none; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; padding: 4px 8px; }
button.sm { padding: 3px 10px; font-size: 12px; }
button:disabled { opacity: 0.55; cursor: default; }
.dl { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; margin: 0; }
.dl dt { color: var(--muted); font-size: 13px; }
.dl dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

/* timeline */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 14px 22px; }
.timeline li::before { content: ''; position: absolute; left: 5px; top: 7px; width: 8px; height: 8px; border-radius: 50%; background: var(--c, var(--muted)); }
.timeline li::after { content: ''; position: absolute; left: 8.5px; top: 18px; bottom: 0; width: 1px; background: var(--border); }
.timeline li:last-child::after { display: none; }
.timeline .meta { font-size: 12px; color: var(--muted); }
.timeline .body { white-space: pre-wrap; background: var(--panel-2); border-radius: 8px; padding: 8px 10px; margin-top: 4px; font-size: 13px; }

/* misc */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; flex-wrap: wrap; }
.tabs a { padding: 8px 12px; color: var(--muted); font-weight: 550; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }
.tabs a:hover { text-decoration: none; color: var(--text); }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.drop { border: 2px dashed var(--border); border-radius: var(--radius); padding: 28px; text-align: center; cursor: pointer; background: var(--panel-2); }
.drop.over { border-color: var(--accent); background: var(--accent-soft); }
.progress { height: 8px; background: var(--panel-2); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width 0.3s; }
.secret { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px; font-family: ui-monospace, Menlo, monospace; font-size: 12px; overflow-wrap: anywhere; }
pre.json { background: var(--panel-2); border-radius: 8px; padding: 10px; font-size: 12px; overflow: auto; max-height: 260px; margin: 6px 0 0; }
.callout { background: var(--accent-soft); border-radius: 8px; padding: 10px 12px; font-size: 13px; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 12px; }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 500; opacity: 0; pointer-events: none; transition: opacity 0.2s; z-index: 100; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; }
#toast.error { background: var(--danger); color: #fff; }
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 20px; background: var(--panel); color: var(--text); width: min(640px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25); }
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .panel { width: min(360px, 100%); padding: 28px; }
.mobile-nav { display: none; }

@media (max-width: 1100px) {
  .pipeline { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .lead-layout, .cols-3 { grid-template-columns: 1fr; }
}
@media (max-width: 780px) {
  .shell { grid-template-columns: minmax(0, 1fr); background: var(--bg); }
  .side { display: none; }
  .mobile-nav { display: flex; gap: 4px; overflow-x: auto; background: var(--panel); border-bottom: 1px solid var(--border); padding: 8px 16px; position: sticky; top: 0; z-index: 5; }
  .mobile-nav a { padding: 6px 10px; border-radius: 8px; color: var(--text); white-space: nowrap; font-weight: 500; }
  .mobile-nav a.active { background: var(--accent-soft); color: var(--accent); }
  main { padding: 16px 16px 60px; }
  .cols-2, .form-grid { grid-template-columns: 1fr; }
  .pipeline { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters input[type='search'] { min-width: 0; width: 100%; }
  .dl { grid-template-columns: 1fr; gap: 0 0; }
  .dl dt { margin-top: 8px; }
}
