:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c1e21;
  --muted: #6b7280;
  --line: #e3e5e8;
  --accent: #2a7ae2;
  --accent-soft: #e8f1fd;
  --hot: #d9480f;
  --hot-soft: #fff1e8;
  --ok: #2b8a3e;
  --ok-soft: #e9f7ec;
  --danger: #c92a2a;
  --danger-soft: #fdecec;
  --spam: #8a6d00;
  --spam-soft: #fff7d6;
  --op: #5f3dc4;
  --op-soft: #f1edff;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1e2125;
    --text: #e6e8eb;
    --muted: #9aa0a8;
    --line: #2e3238;
    --accent: #5b9cf0;
    --accent-soft: #1c2d44;
    --hot: #ff8a4c;
    --hot-soft: #3a2418;
    --ok: #63c27a;
    --ok-soft: #1b3122;
    --danger: #ff6b6b;
    --danger-soft: #3a1c1c;
    --spam: #e0c055;
    --spam-soft: #332b10;
    --op: #a991ff;
    --op-soft: #2a2244;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.danger { color: var(--danger); }
.empty { color: var(--muted); padding: 40px 0; text-align: center; }

/* шапка */
.top {
  display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; color: var(--text); white-space: nowrap; }
.main-nav { display: flex; gap: 4px; flex: 1; }
.main-nav a {
  padding: 6px 12px; border-radius: 8px; color: var(--text);
  display: inline-flex; align-items: center; gap: 6px;
}
.main-nav a.active, .main-nav a:hover { background: var(--accent-soft); text-decoration: none; }
.badge {
  min-width: 20px; padding: 0 6px; border-radius: 10px; font-size: 12px; line-height: 20px;
  text-align: center; font-weight: 600;
}
.badge-hot { background: var(--hot); color: #fff; }
.logout { margin: 0; }
.link { background: none; border: 0; color: var(--muted); cursor: pointer; font: inherit; }
.link:hover { color: var(--text); }

.page { max-width: 1100px; margin: 0 auto; padding: 24px; }
.back { display: inline-block; margin-bottom: 12px; font-size: 14px; }

/* карточки, кнопки, поля */
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 20px;
}
.btn {
  display: inline-block; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font: inherit; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { border-color: var(--danger); background: var(--danger-soft); }
.btn-small { padding: 4px 10px; font-size: 13px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input[type=radio] { width: auto; }
label { display: block; font-size: 14px; color: var(--muted); }
label input, label select { margin-top: 4px; }

.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.flash-ok { background: var(--ok-soft); color: var(--ok); }
.flash-err { background: var(--danger-soft); color: var(--danger); }

.pill {
  display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px;
  background: var(--bg); color: var(--muted); white-space: nowrap;
}
.pill-need_human, .pill-hot { background: var(--hot-soft); color: var(--hot); }
.pill-operator { background: var(--op-soft); color: var(--op); }
.pill-spam { background: var(--spam-soft); color: var(--spam); }
.pill-ai { background: var(--accent-soft); color: var(--accent); }
.pill-bc-running { background: var(--accent-soft); color: var(--accent); }
.pill-bc-done { background: var(--ok-soft); color: var(--ok); }
.pill-bc-cancelled { background: var(--danger-soft); color: var(--danger); }

/* вход */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: grid; gap: 14px; }
.login-card h1 { margin: 0; }

/* список диалогов */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar h1 { margin: 0; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab { padding: 6px 12px; border-radius: 8px; color: var(--text); }
.tab.active { background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); font-weight: 600; }
.tab .count { color: var(--muted); font-weight: 400; margin-left: 2px; }
.search { display: flex; gap: 8px; min-width: 280px; }
.chat-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.chat-row {
  display: flex; justify-content: space-between; gap: 16px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 16px; color: var(--text);
}
.chat-row:hover { border-color: var(--accent); text-decoration: none; }
.chat-row-main { min-width: 0; flex: 1; }
.chat-name { font-weight: 600; display: flex; gap: 8px; flex-wrap: wrap; align-items: baseline; }
.chat-name .muted { font-weight: 400; }
.chat-last { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.chat-reason { font-size: 13px; margin-top: 4px; color: var(--text); opacity: .8; }
.chat-row-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.pager { display: flex; gap: 8px; justify-content: center; margin-top: 16px; }

/* диалог */
.chat-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 20px; align-items: start; }
.chat-side { position: sticky; top: 76px; }
.chat-side .facts { grid-template-columns: 1fr; gap: 0; }
.chat-side .facts dt { font-size: 12px; }
.chat-side .facts dd { margin-bottom: 8px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0 0 16px; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.actions form { margin: 0; }
.ban-form { display: grid; gap: 8px; border-top: 1px solid var(--line); padding-top: 16px; }
.load-earlier { margin-bottom: 12px; }
.messages {
  display: flex; flex-direction: column; gap: 10px;
  max-height: calc(100vh - 290px); overflow-y: auto; padding: 4px 4px 12px;
}
.msg {
  max-width: 80%; padding: 10px 14px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--line);
}
.msg-user { align-self: flex-start; border-bottom-left-radius: 4px; }
.msg-ai { align-self: flex-end; background: var(--accent-soft); border-color: transparent; border-bottom-right-radius: 4px; }
.msg-operator { align-self: flex-end; background: var(--op-soft); border-color: transparent; border-bottom-right-radius: 4px; }
.msg-system {
  align-self: center; max-width: 90%; padding: 4px 12px; background: transparent;
  border: 0; font-size: 12px; color: var(--muted);
}
.msg-system summary { cursor: pointer; list-style: none; text-align: center; }
.msg-system summary::-webkit-details-marker { display: none; }
.msg-system[open] { background: var(--surface); border: 1px dashed var(--line); padding: 8px 12px; }
.msg-system .msg-text { margin-top: 6px; }
.msg-meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.shot { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin: 4px 0; }
.note { font-size: 13px; color: var(--muted); font-style: italic; margin-bottom: 4px; }
.reply { margin-top: 12px; display: grid; gap: 8px; }
.reply-actions { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

/* таблицы */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: 13px; color: var(--muted); font-weight: 500; }
.table tr:last-child td { border-bottom: 0; }
.table form { margin: 0; }

/* рассылки */
.form { display: grid; gap: 20px; max-width: 720px; }
fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 10px; }
legend { font-weight: 600; margin-bottom: 8px; font-size: 16px; }
.radio { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 15px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.audience { margin: 0; }
.broadcast-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
.preview { white-space: pre-wrap; overflow-wrap: anywhere; background: var(--bg); border-radius: 8px; padding: 12px; }
.preview-button { margin-top: 8px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; text-align: center; }
.progress { height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; margin-bottom: 16px; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .4s; }

@media (max-width: 800px) {
  .top { padding: 0 12px; gap: 12px; }
  .brand { display: none; }
  .page { padding: 16px 12px; }
  .chat-layout, .broadcast-layout { grid-template-columns: 1fr; }
  .chat-side { position: static; order: -1; }
  .messages { max-height: none; }
  .msg { max-width: 92%; }
  .chat-row { flex-direction: column; }
  .chat-row-side { flex-direction: row; align-items: center; flex-wrap: wrap; }
  .search { min-width: 0; width: 100%; }
  .row { grid-template-columns: 1fr; }
}
