:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c2330;
  --muted: #667085;
  --border: #e3e6eb;
  --accent: #5b3cc4;
  --accent-text: #ffffff;
  --ok-bg: #e7f6ec;
  --ok-text: #18663a;
  --err-bg: #fdecec;
  --err-text: #a42020;
  --tag-bg: #eef0f4;
  --high: #18663a;
  --mid: #8a5a00;
  --low: #a42020;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --surface: #1c1f26;
    --text: #e6e8ec;
    --muted: #9aa3b2;
    --border: #2c313b;
    --accent: #8d73f0;
    --accent-text: #0f0f14;
    --ok-bg: #16301f;
    --ok-text: #7fd8a0;
    --err-bg: #3a1c1c;
    --err-text: #f1a1a1;
    --tag-bg: #262a33;
    --high: #7fd8a0;
    --mid: #f0c36a;
    --low: #f1a1a1;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 22px; margin: 0 0 6px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
code { font-size: 12px; word-break: break-all; }

.top {
  display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.tabs { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.tab {
  padding: 7px 12px; border-radius: 8px; text-decoration: none; color: var(--text);
}
.tab:hover { background: var(--tag-bg); }
.tab.active { background: var(--accent); color: var(--accent-text); }
.logout { display: flex; gap: 10px; align-items: center; }
button.link { background: none; border: 0; color: var(--accent); cursor: pointer; font: inherit; padding: 0; }

.page { padding: 20px 16px 40px; max-width: 1600px; margin: 0 auto; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 20px; }
.soon { color: var(--muted); }

.toolbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; margin-bottom: 12px; }
.button, button.primary {
  display: inline-block; padding: 9px 16px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); text-decoration: none; font: inherit; cursor: pointer;
}
.button.primary, button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.alert { padding: 10px 14px; border-radius: 8px; margin: 0 0 12px; }
.alert.ok { background: var(--ok-bg); color: var(--ok-text); }
.alert.error { background: var(--err-bg); color: var(--err-text); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
table.rules { border-collapse: collapse; width: 100%; min-width: 1300px; }
.rules th, .rules td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.rules th { position: sticky; top: 0; background: var(--surface); font-size: 13px; color: var(--muted); font-weight: 600; }
.rules td { font-size: 13px; }
.rules td.num { font-weight: 600; font-size: 15px; }
.rules tr.inactive td { opacity: .55; }
.rules .title { font-weight: 600; margin-bottom: 4px; }
.clip {
  display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden;
  white-space: pre-line; max-width: 340px;
}
.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; background: var(--tag-bg); font-size: 12px; white-space: nowrap; }
.chance-высокий { color: var(--high); }
.chance-средний { color: var(--mid); }
.chance-низкий { color: var(--low); }

body.center { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 360px; display: grid; gap: 14px; }
.login h1 { margin: 0; }
.login p { margin: 0; }

label { display: grid; gap: 6px; font-weight: 600; font-size: 13px; }
input, select, textarea {
  font: inherit; font-size: 15px; font-weight: 400; padding: 9px 11px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); width: 100%;
}
textarea { resize: vertical; font-family: inherit; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.edit { display: grid; gap: 16px; max-width: 1100px; }
.edit .row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; align-items: end; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 400; }
label.check input { width: auto; }
.err { color: var(--err-text); font-weight: 400; }
.actions { display: flex; gap: 10px; }

/* ---------- Работа с клиентами ---------- */
h2 { font-size: 15px; margin: 0 0 8px; }
.clients { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 16px; align-items: start; }
@media (max-width: 900px) { .clients { grid-template-columns: 1fr; } }
.search { display: flex; gap: 8px; }
.search .button { white-space: nowrap; }
.client-list {
  --row: 52px;
  list-style: none; margin: 0; padding: 0;
  max-height: calc(var(--row) * 15);  /* 15 rows visible, the rest scrolls */
  overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.client-row {
  display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto auto; column-gap: 8px;
  height: var(--row); padding: 7px 12px; box-sizing: border-box;
  border-bottom: 1px solid var(--border); color: var(--text); text-decoration: none;
}
.client-row:hover { background: var(--tag-bg); }
.client-row.selected { background: var(--accent); color: var(--accent-text); }
.client-row.selected .muted { color: var(--accent-text); opacity: .85; }
.client-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.client-meta { grid-column: 1; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.client-badges { grid-column: 2; grid-row: 1 / span 2; align-self: center; display: flex; gap: 4px; }
.client-list .empty { padding: 12px; }
.tag.unread { background: var(--err-text); color: var(--surface); font-weight: 600; }
.tag.sub { color: var(--high); }
.tag.bad { color: var(--low); }

.clients-right { display: grid; gap: 16px; min-width: 0; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.card-grid .card p { margin: 0 0 6px; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 8px 0 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
ul.plain { list-style: none; padding: 0; margin: 0; }

.chat { max-height: 420px; overflow-y: auto; display: grid; gap: 10px; padding: 4px 2px 8px; }
.msg { max-width: 80%; padding: 8px 12px; border-radius: 12px; background: var(--tag-bg); }
.msg.out { justify-self: end; background: var(--ok-bg); }
.msg.notification { justify-self: center; max-width: 90%; background: transparent; border: 1px dashed var(--border);
  color: var(--muted); font-size: 13px; }
.msg.outside, .msg.command { background: transparent; border: 1px dashed var(--muted); }
.msg.command .msg-text { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center; margin: 0 0 8px; }
.msg-dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.msg-dot.in { background: var(--tag-bg); border: 1px solid var(--border); }
.msg-dot.out { background: var(--ok-bg); border: 1px solid var(--border); }
.msg-dot.outside { border: 1px dashed var(--muted); }
.msg-dot.notification { border: 1px dashed var(--border); }
.msg-text { white-space: pre-wrap; word-break: break-word; }
.reply { display: grid; gap: 8px; margin-top: 12px; }
.reply .actions { align-items: center; }
