:root {
  color-scheme: light dark;
  --bg: #f6f7f9; --panel: #fff; --text: #16181d; --muted: #667085; --line: #e4e7ec;
  --accent: #2563eb; --good: #16a34a; --bad: #dc2626; --warn: #d97706;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1115; --panel: #171a21; --text: #e6e8ec; --muted: #98a2b3; --line: #2a2f3a; --accent: #60a5fa; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; }
[hidden] { display: none !important; }
a { color: var(--accent); }
button, select, input, textarea { font: inherit; color: inherit; }
button { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 8px 14px; cursor: pointer; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
button:disabled { opacity: .6; cursor: wait; }
select, input, textarea { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; }
textarea { width: 100%; min-height: 70px; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.2em; }

.login-card { max-width: 340px; margin: 15vh auto; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: grid; gap: 12px; }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card label { display: grid; gap: 6px; }

.topbar { position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--line); }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { padding: 6px 12px; border-radius: 8px; text-decoration: none; color: var(--muted); }
.topbar nav a.active { background: var(--bg); color: var(--text); font-weight: 600; }
.filters { margin-left: auto; display: flex; gap: 8px; }
main { padding: 20px; max-width: 1280px; margin: 0 auto; display: grid; gap: 20px; }
main > * { min-width: 0; }
@media (max-width: 600px) { main { padding: 12px; } .filters { margin-left: 0; flex-wrap: wrap; } .fields { grid-template-columns: 1fr; } }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }
.kpi .v { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; }
.kpi .l { color: var(--muted); font-size: 12px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; overflow-x: auto; }
.panel h2 { margin: 0 0 12px; font-size: 15px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr)); gap: 20px; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
td.n, th.n { text-align: right; }
tr.click { cursor: pointer; }
tr.click:hover { background: var(--bg); }
.empty { color: var(--muted); padding: 12px 0; }

.chart svg { width: 100%; height: 180px; display: block; }
.chart .legend { display: flex; gap: 16px; color: var(--muted); font-size: 12px; margin-top: 6px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; background: var(--bg); border: 1px solid var(--line); }
.badge.new { color: var(--accent); } .badge.won { color: var(--good); } .badge.lost { color: var(--bad); } .badge.quoted { color: var(--warn); }
.score { font-weight: 650; }
.score.hot { color: var(--good); } .score.cold { color: var(--muted); }

dialog { width: min(760px, 96vw); max-height: 92vh; border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.detail { padding: 20px; display: grid; gap: 16px; }
.detail header { display: flex; justify-content: space-between; gap: 12px; align-items: start; }
.detail h2 { margin: 0; }
.fields { display: grid; grid-template-columns: 140px 1fr; gap: 6px 12px; }
.fields dt { color: var(--muted); }
.fields dd { margin: 0; white-space: pre-wrap; word-break: break-word; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; }
.row label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.note { border-left: 3px solid var(--line); padding: 4px 10px; white-space: pre-wrap; }
.ai-box { border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: grid; gap: 8px; }
.ai-box pre { white-space: pre-wrap; margin: 0; font: inherit; background: var(--bg); padding: 10px; border-radius: 8px; }
.report { line-height: 1.55; }
.report h1, .report h2, .report h3 { font-size: 15px; margin: 16px 0 6px; }

/* Front desk */
.badge.business, .badge.known_contact, .badge.shortlisted, .badge.hired { color: var(--good); }
.badge.job, .badge.interview, .badge.offer { color: var(--accent); }
.badge.spam, .badge.rejected { color: var(--bad); }
.thread { display: grid; gap: 8px; max-height: 50vh; overflow-y: auto; padding: 4px; }
.bubble { max-width: 80%; padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; word-break: break-word; border: 1px solid var(--line); }
.bubble.in { justify-self: start; background: var(--bg); }
.bubble.out { justify-self: end; background: color-mix(in srgb, var(--good) 14%, var(--panel)); }
.bubble .muted { font-size: 11px; margin-top: 4px; }

/* ---------------------------------------------------------------- Polish */
button, select, input, textarea { transition: border-color .15s, background .15s, box-shadow .15s; }
select:focus, input:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
button:hover:not(:disabled) { filter: brightness(1.08); }
button.ghost:hover:not(:disabled) { background: var(--bg); filter: none; }
.brand { font-size: 15px; letter-spacing: .2px; }
.panel { border-radius: 14px; }
th { text-transform: uppercase; letter-spacing: .4px; font-size: 11px; }
td { padding-top: 10px; padding-bottom: 10px; }
.badge.warm { color: var(--warn); } .badge.hot { color: var(--good); } .badge.cold { color: var(--muted); }

/* Lead panel: a drawer from the right instead of a centred box */
dialog.side { margin: 0 0 0 auto; width: min(620px, 100vw); height: 100dvh; max-height: 100dvh; border-radius: 0; border: 0; border-left: 1px solid var(--line); background: var(--bg); }
dialog.side[open] { animation: slide-in .18s ease-out; }
@keyframes slide-in { from { transform: translateX(40px); opacity: .4; } to { transform: none; opacity: 1; } }
dialog.side::backdrop { background: rgba(0,0,0,.55); backdrop-filter: blur(2px); }
dialog.side .detail { background: var(--bg); }

.drawer { display: grid; gap: 14px; padding: 0 20px 28px; }
.drawer-head { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 14px; margin: 0 -20px; padding: 18px 20px; background: var(--panel); border-bottom: 1px solid var(--line); }
.drawer-head .who { flex: 1; min-width: 0; }
.drawer-head h2 { margin: 0 0 2px; font-size: 19px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-head .muted { font-size: 12px; }
.avatar { width: 44px; height: 44px; flex-shrink: 0; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; background: linear-gradient(135deg, var(--accent), #7c3aed); }
.score-pill { display: grid; justify-items: center; line-height: 1.1; padding: 6px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); }
.score-pill strong { font-size: 18px; font-variant-numeric: tabular-nums; }
.score-pill span { font-size: 10px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.score-pill.hot strong { color: var(--good); } .score-pill.warm strong { color: var(--warn); } .score-pill.cold strong { color: var(--muted); }
button.icon { background: transparent; color: var(--muted); border: 1px solid var(--line); width: 36px; height: 36px; padding: 0; border-radius: 50%; font-size: 14px; }
button.icon:hover { color: var(--text); background: var(--bg); }

.actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }
.action { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 11px 14px; border-radius: 10px; font-weight: 600; text-decoration: none; color: var(--text); background: var(--panel); border: 1px solid var(--line); }
.action:hover { border-color: var(--accent); }
.action.call { background: var(--accent); border-color: var(--accent); color: #fff; }
.action.wa { background: #25d366; border-color: #25d366; color: #fff; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: grid; gap: 12px; min-width: 0; }
.card h3 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); font-weight: 600; }
.card h4 { margin: 0; font-size: 13px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.card-head .btns { display: flex; gap: 8px; align-items: center; }
.cards2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }

.stages { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; padding: 4px; background: var(--bg); border-radius: 10px; }
.stage { background: transparent; color: var(--muted); padding: 8px 4px; border-radius: 8px; text-transform: capitalize; font-size: 13px; }
.stage[aria-pressed="true"] { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.stage.won[aria-pressed="true"] { color: var(--good); } .stage.lost[aria-pressed="true"] { color: var(--bad); } .stage.quoted[aria-pressed="true"] { color: var(--warn); } .stage.new[aria-pressed="true"] { color: var(--accent); }
.inline-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.inline-fields label { display: grid; gap: 6px; font-size: 12px; color: var(--muted); }
.inline-fields input { width: 100%; }

.facts { display: grid; grid-template-columns: 110px 1fr; gap: 8px 12px; margin: 0; }
.facts dt { color: var(--muted); font-size: 13px; }
.facts dd { margin: 0; word-break: break-word; }
.message { margin: 0; white-space: pre-wrap; line-height: 1.55; }

.card.ai { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 7%, var(--panel)), var(--panel)); }
.ai-summary { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; line-height: 1.5; }
.next { line-height: 1.5; }
.draft { display: grid; gap: 8px; }
.draft pre { white-space: pre-wrap; margin: 0; font: inherit; background: var(--bg); padding: 12px; border-radius: 8px; border: 1px solid var(--line); line-height: 1.5; }
a.small { font-size: 13px; }

.note-form { display: grid; gap: 8px; }
.note-form button { justify-self: end; }
.timeline { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.timeline li { position: relative; padding-left: 18px; }
.timeline li::before { content: ''; position: absolute; left: 3px; top: 6px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.timeline li::after { content: ''; position: absolute; left: 6.5px; top: 18px; bottom: -14px; width: 1px; background: var(--line); }
.timeline li:last-child::after { display: none; }
.timeline time { font-size: 12px; color: var(--muted); }
.timeline p { margin: 2px 0 0; white-space: pre-wrap; line-height: 1.5; }

@media (max-width: 600px) { .stages { grid-template-columns: repeat(3, 1fr); } .inline-fields { grid-template-columns: 1fr; } .facts { grid-template-columns: 90px 1fr; } }

/* Settings */
.settings { display: grid; gap: 16px; max-width: 900px; }
.settings-intro h2 { margin: 0 0 4px; font-size: 20px; }
.settings-intro p { margin: 0; }
.small { font-size: 12px; margin: 2px 0 0; }
.setting-grid { display: grid; grid-template-columns: minmax(200px, 1fr) 2fr; gap: 12px; }
.setting-grid label { display: grid; gap: 6px; font-size: 12px; color: var(--muted); }
.setting-grid select, .setting-grid input { width: 100%; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.flash { font-size: 13px; color: var(--muted); }
.flash.ok { color: var(--good); } .flash.bad { color: var(--bad); }
.key-list { display: grid; gap: 0; }
.key-row { display: grid; gap: 8px; padding: 12px 0; border-top: 1px solid var(--line); }
.key-row:first-child { border-top: 0; }
.key-name { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.key-input { display: flex; gap: 8px; }
.key-input input { flex: 1; min-width: 0; }
.settings textarea { font-size: 13px; line-height: 1.5; }
@media (max-width: 600px) { .setting-grid { grid-template-columns: 1fr; } .key-input { flex-wrap: wrap; } }
.tag { display: inline-block; margin-left: 8px; padding: 1px 7px; border-radius: 6px; font-size: 11px; font-weight: 600; vertical-align: 1px; }
.tag.warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.warn-card { border-color: color-mix(in srgb, var(--warn) 50%, var(--line)); background: color-mix(in srgb, var(--warn) 8%, var(--panel)); line-height: 1.5; }

/* Settings: blog images table */
table.mini { width: 100%; border-collapse: collapse; font-size: 0.875rem; margin: 0.5rem 0 1rem; }
table.mini th, table.mini td { text-align: left; padding: 0.4rem 0.5rem; border-bottom: 1px solid var(--line, #e5e7eb); vertical-align: middle; }
table.mini th { font-weight: 600; color: var(--muted, #6b7280); }
table.mini .key-input input { min-width: 14rem; }
