:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1b1f24;
  --muted: #667085;
  --accent: #2f6fde;
  --accent-soft: #e8f0fd;
  --ok: #1f8a4c;
  --ok-soft: #e4f5ea;
  --warn: #b25e09;
  --warn-soft: #fdf0e1;
  --bad: #c0362c;
  --bad-soft: #fbe9e7;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1e232b;
    --border: #2a3039;
    --text: #e6e9ee;
    --muted: #98a2b3;
    --accent: #6b9cf2;
    --accent-soft: #1d2a41;
    --ok: #4cc27f;
    --ok-soft: #15291e;
    --warn: #e5a04f;
    --warn-soft: #2e2214;
    --bad: #ef6b60;
    --bad-soft: #321a18;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
code, pre { font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 10px 24px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); }
.nav { display: flex; gap: 4px; flex-wrap: wrap; }
.nav a { padding: 6px 10px; border-radius: 6px; color: var(--muted); }
.nav a.active, .nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.scan-pill { margin-left: auto; font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: center; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.running { background: var(--accent); animation: pulse 1.2s infinite; }
.dot.done { background: var(--ok); }
.dot.failed { background: var(--bad); }
.dot.stopped { background: var(--warn); }
@keyframes pulse { 50% { opacity: .3; } }

main { max-width: 1400px; margin: 0 auto; padding: 20px 24px 60px; }
.sub { color: var(--muted); margin: 0 0 16px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.stack > * + * { margin-top: 16px; }

.stats { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 14px; min-width: 120px; color: var(--text);
}
a.stat:hover { text-decoration: none; border-color: var(--accent); }
.stat.active { border-color: var(--accent); background: var(--accent-soft); }
.stat b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 12px; }

.banner { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 16px; border: 1px solid; }
.banner.warn { background: var(--warn-soft); border-color: var(--warn); color: var(--text); }
.banner.ok { background: var(--ok-soft); border-color: var(--ok); }
.banner.bad { background: var(--bad-soft); border-color: var(--bad); }

form.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
input, select, textarea, button, .btn {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px;
}
textarea { width: 100%; resize: vertical; }
input[type=search] { min-width: 260px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
button, .btn {
  cursor: pointer; background: var(--surface-2); display: inline-block; color: var(--text);
}
button:hover, .btn:hover { border-color: var(--accent); text-decoration: none; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--bad); }
button.link { background: none; border: none; color: var(--muted); padding: 0 4px; }
button.link.danger { color: var(--bad); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.name { font-weight: 600; overflow-wrap: anywhere; }  /* some names are long unbroken links */
.titles { color: var(--muted); font-size: 12px; max-width: 340px; }
.contacts { font-size: 13px; }
.contacts div { white-space: nowrap; max-width: 300px; overflow: hidden; text-overflow: ellipsis; }  /* long links would push the columns after it off-screen */
.notes-cell { max-width: 220px; color: var(--muted); font-size: 12px; white-space: pre-line; }

.chip {
  display: inline-block; padding: 1px 8px; margin: 1px 2px 1px 0; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-size: 12px; white-space: nowrap;
}
.status-select { padding: 3px 6px; font-size: 13px; }
.s-new { }
.s-contacted { background: var(--accent-soft); }
.s-replied, .s-interested { background: var(--warn-soft); }
.s-client { background: var(--ok-soft); }
.s-not_interested, .s-skip { background: var(--surface-2); color: var(--muted); }
.status-menu { display: inline-flex; align-items: center; vertical-align: 1px; margin-left: 6px; white-space: nowrap; }
.status-btn { padding: 1px 8px; font-size: 12px; border-radius: 999px; }
.status-btn.s-new { background: var(--surface); }
.status-pop {
  position: fixed; z-index: 20; display: flex; flex-direction: column; min-width: 170px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .18);
}
.status-pop[hidden] { display: none; }
.status-pop button { border: none; background: none; text-align: left; padding: 6px 10px; border-radius: 4px; }
.status-pop button:hover { background: var(--surface-2); }
.status-pop button.current { font-weight: 600; }
.status-pop button.current::after { content: " ✓"; color: var(--ok); }
.quick-status { display: flex; flex-direction: column; gap: 4px; }
.quick-status button { padding: 2px 8px; font-size: 12px; white-space: nowrap; text-align: left; }
.quick-status button.on { font-weight: 600; color: var(--text); border-color: var(--accent); }
.quick-status button.on::before { content: "✓ "; color: var(--ok); }
.saved { color: var(--ok); font-size: 12px; margin-left: 6px; opacity: 0; transition: opacity .3s; }
.saved.show { opacity: 1; }

.p3d {
  display: inline-block; min-width: 44px; padding: 1px 7px; border-radius: 999px; text-align: center;
  font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; vertical-align: 1px;
  background: var(--surface-2); color: var(--muted);
}
.p3d.p-hi { background: var(--ok-soft); color: var(--ok); }
.p3d.p-mid { background: var(--warn-soft); color: var(--warn); }
.p3d.manual { outline: 1.5px solid currentColor; outline-offset: -1.5px; }
h2 .p3d { font-size: 13px; margin-left: 6px; }
table.why { margin-bottom: 6px; }
table.why th, table.why td { padding: 5px 8px; }
.evidence { margin: 12px 0; }
.hit { background: var(--surface-2); border-radius: 6px; padding: 6px 10px; margin-top: 6px; font-size: 13px; overflow-wrap: anywhere; }
.hit mark { background: var(--warn-soft); color: var(--text); font-weight: 600; padding: 0 2px; border-radius: 3px; }
.raw { white-space: pre-wrap; background: var(--surface-2); padding: 10px; border-radius: 6px; font-size: 13px; margin: 0 0 8px; }
.kv { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 6px 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.log-item { border-left: 3px solid var(--accent); padding: 4px 0 4px 10px; margin-bottom: 10px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row > * { flex: 0 0 auto; }
pre.logtail { background: var(--surface-2); padding: 12px; border-radius: 6px; overflow-x: auto; max-height: 420px; margin: 0; }
details summary { cursor: pointer; color: var(--muted); }

@media (max-width: 800px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .scan-pill { margin-left: 0; }
  main { padding: 16px 16px 40px; }
  .grid2 { grid-template-columns: minmax(0, 1fr); }
  input[type=search] { min-width: 0; width: 100%; }
}
th a.sort { color: inherit; }
th a.sort.active { color: var(--text); }

.badge {
  display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 5px; border-radius: 999px;
  background: var(--ok); color: #fff; font-size: 11px; font-weight: 600; line-height: 18px; text-align: center;
}
a.badge:hover { text-decoration: none; }
.badge[hidden] { display: none; }
.badge.tg { background: var(--accent); }
.chat-thread {
  display: flex; flex-direction: column; gap: 6px; max-height: 380px; overflow-y: auto;
  padding: 10px; margin-bottom: 12px; background: var(--surface-2); border-radius: var(--radius);
}
.chat-msg { max-width: 85%; padding: 6px 10px; border-radius: 10px; overflow-wrap: anywhere; }
.chat-text { white-space: pre-wrap; }
.chat-msg.in { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); }
.chat-msg.out { align-self: flex-end; background: var(--ok-soft); }
.chat-meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; white-space: nowrap; }
.chat-meta .read { color: var(--accent); }
.chat-meta .chat-fail, .send-result.bad { color: var(--bad); }
.row > .send-result { flex: 1 1 240px; }  /* long errors and the Telegram button wrap instead of overflowing */
.send-result button { margin: 4px 0 0; }
.chat-preview { color: var(--muted); max-width: 520px; }
.chat-preview.unread { color: var(--text); font-weight: 600; }
img.qr { display: block; background: #fff; padding: 8px; border-radius: var(--radius); margin: 8px 0 12px; }
details.tpl { border-top: 1px solid var(--border); padding: 8px 0; }
details.tpl summary { color: var(--text); }
details.tpl summary span { color: var(--muted); }

/* Signed-in user in the top bar, and the sign-in page */
.who { display: flex; align-items: center; gap: 4px; }
.login { max-width: 360px; padding-top: 12vh; }
.login h1 { margin-bottom: 16px; }
.login label { display: block; }
.login input { width: 100%; margin-top: 4px; }
.login .banner code { overflow-wrap: anywhere; }
