:root {
  --bg: #f5f6f8;
  --card: #fff;
  --text: #1c1f24;
  --muted: #6b7280;
  --line: #e3e6ea;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --danger: #c0262d;
  --ok: #15803d;
  --radius: 10px;
  color-scheme: light;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); margin: 8px 0 0; }

.primary {
  background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 8px 14px; font-weight: 600;
}
.ghost { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; }
.danger { background: none; border: 1px solid var(--danger); color: var(--danger); border-radius: 8px; padding: 6px 12px; }
input, select, textarea {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; min-width: 0;
}
textarea { width: 100%; resize: vertical; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }

/* Вход */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; display: grid; gap: 10px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }

/* Шапка */
.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px;
  padding: 10px 16px; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; font-size: 17px; }
.tabs { display: flex; gap: 4px; }
.tab { background: none; border: 0; border-radius: 8px; padding: 6px 10px; color: var(--muted); }
.tab.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.top-right { margin-left: auto; display: flex; gap: 12px; align-items: center; }

main { max-width: 980px; margin: 0 auto; padding: 16px; }

/* Фильтры */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.toolbar input[type="search"] { flex: 1 1 200px; }
.tag-filter { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; align-items: center; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 2px 10px; font-size: 13px;
}
button.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip .count { opacity: .7; }
.chip .x { border: 0; background: none; padding: 0 0 0 2px; color: var(--muted); font-size: 14px; line-height: 1; }
.chip .x:hover { color: var(--danger); }
.chip-name { border: 0; background: none; padding: 0; }

/* Список лидов */
.leads { display: grid; gap: 10px; }
.lead { cursor: pointer; transition: border-color .2s; }
.lead:hover { border-color: #c7cdd5; }
.lead.fresh { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.lead-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; font-size: 13px; }
.lead-id { font-weight: 700; }
.lead-head select { margin-left: auto; padding: 3px 6px; font-size: 13px; }
.lead-who { margin-top: 6px; }
.lead-req { margin-top: 4px; color: #3b4048; white-space: pre-wrap; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.lead-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; align-items: center; }
.tag-input { width: 110px; padding: 2px 10px; border-radius: 999px; font-size: 13px; }

.badge { border-radius: 6px; padding: 1px 7px; font-size: 12px; background: #eef0f3; }
.src-bot { background: #e8efff; color: #1d4ed8; }
.src-telegram { background: #e6f6ee; color: #15803d; }
.src-manual { background: #f3eefe; color: #6d28d9; }
.repeat { background: #fff4e0; color: #9a5b00; }
.st-new { color: var(--accent); }
.st-won { color: var(--ok); }
.st-lost { color: var(--muted); }

.empty { text-align: center; padding: 40px 16px; }

/* Карточка лида */
dialog {
  border: 0; border-radius: 14px; padding: 0; width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 24px);
}
dialog::backdrop { background: rgba(20, 24, 31, .45); }
#dlg-body { padding: 18px; display: grid; gap: 14px; }
.dlg-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.dlg-head h2 { margin: 0; font-size: 18px; }
.dlg-head .close { margin-left: auto; }
.form { display: grid; gap: 8px; }
.form label { display: grid; gap: 3px; font-size: 13px; color: var(--muted); }
.form label > * { color: var(--text); font-size: 15px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.section-title { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; margin: 0 0 6px; }
.msgs { display: grid; gap: 6px; max-height: 260px; overflow: auto; }
.msg { padding: 7px 10px; border-radius: 10px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.in { background: #eef0f3; }
.msg.out { background: var(--accent-soft); margin-left: auto; }
.msg time { display: block; font-size: 11px; color: var(--muted); }
.related a { margin-right: 10px; }

table.conn { width: 100%; border-collapse: collapse; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); }
table.conn th, table.conn td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); font-size: 14px; }

@media (max-width: 560px) {
  .top-right { margin-left: 0; width: 100%; justify-content: space-between; }
  .lead-head select { margin-left: 0; }
}

.tag-add { display: inline-flex; gap: 4px; align-items: center; }
.chip.add { padding: 2px 9px; font-weight: 700; color: var(--accent); }
.page-error { padding: 16px; text-align: center; }
#connections { overflow-x: auto; }

/* iOS увеличивает страницу при фокусе на поле со шрифтом меньше 16px — на сенсорных экранах держим 16px */
@media (pointer: coarse) {
  input, select, textarea { font-size: max(16px, 1em) !important; }
  .tag-input { width: 130px; }
}
@media (max-width: 560px) {
  table.conn th:last-child, table.conn td:last-child { display: none; }
}
