:root {
  --bg: #f4f5f7; --surface: #fff; --text: #1d2129; --muted: #6b7280; --border: #e3e5e8;
  --accent: #2563eb; --accent-text: #fff; --in: #fff; --out: #dbeafe; --bot: #f1f5f9;
  --ok: #15803d; --warn: #b45309; --err: #b91c1c; --warn-bg: #fef3c7; --err-bg: #fee2e2; --ok-bg: #dcfce7;
  --st-new: #2563eb; --st-in_progress: #7c3aed; --st-waiting: #b45309; --st-closed: #6b7280;
  --radius: 10px; --gutter: 16px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318; --surface: #1a1d24; --text: #e6e8eb; --muted: #9aa1ac; --border: #2b303a;
    --accent: #3b82f6; --in: #1f232b; --out: #1e3a5f; --bot: #20252e;
    --warn-bg: #3b2f12; --err-bg: #3d1a1a; --ok-bg: #133022;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-size: .9em; background: var(--bot); padding: 1px 4px; border-radius: 4px; }
h1 { font-size: 1.4rem; margin: 0 0 12px; }
h2 { font-size: 1rem; margin: 20px 0 8px; }
h2 small { font-weight: normal; color: var(--muted); }
.muted, .hint { color: var(--muted); }
main { max-width: 1280px; margin: 0 auto; padding: var(--gutter); }

input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; max-width: 100%;
}
textarea { width: 100%; resize: vertical; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 8px 14px; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.small { padding: 4px 10px; font-size: .9em; }
button.linklike { border: 0; background: none; color: var(--accent); padding: 0; }
button:disabled { opacity: .6; cursor: progress; }
form.inline { display: inline; }
.stack { display: grid; gap: 8px; }
.stack label, .row-form label, .login-box label { display: grid; gap: 4px; font-size: .9em; color: var(--muted); }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px var(--gutter);
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.topbar .brand { font-weight: 600; color: var(--text); }
.topbar nav { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }

.flash { padding: 10px 14px; border-radius: var(--radius); margin: 10px auto; max-width: 1280px; }
main .flash { margin: 0 0 12px; }
.flash-ok { background: var(--ok-bg); }
.flash-warn { background: var(--warn-bg); }
.flash-error { background: var(--err-bg); }
.form-error { color: var(--err); }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.panel.narrow { max-width: 560px; margin: 40px auto; }

/* Вход */
.page-login main { display: grid; place-items: center; min-height: 80vh; }
.login-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px;
  width: 100%; max-width: 360px; display: grid; gap: 12px; }

/* Список */
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.filters input[type=search] { flex: 1 1 260px; }
.filters .check { display: flex; align-items: center; gap: 6px; }
.clients { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.client-row + .client-row { border-top: 1px solid var(--border); }
.client-row a { display: grid; gap: 3px; padding: 12px 14px; color: var(--text); }
.client-row a:hover { background: var(--bg); text-decoration: none; }
.client-row .row1, .client-row .row2, .client-row .row3 { display: flex; gap: 8px; align-items: baseline; min-width: 0; }
.client-row .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.client-row.unread .name::before { content: "● "; color: var(--accent); }
.client-row .uname, .client-row .src { color: var(--muted); font-size: .9em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.client-row .time { margin-left: auto; color: var(--muted); font-size: .85em; white-space: nowrap; }
.client-row .preview { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.client-row.unread .preview { color: var(--text); }
.client-row .you { color: var(--muted); }
.client-row .send-problem { color: var(--warn); font-weight: 600; }
.client-row .match { font-size: .85em; color: var(--muted); }
.badge { background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 0 8px; font-size: .8em; font-weight: 600; }
.pill { font-size: .78em; border-radius: 999px; padding: 1px 8px; border: 1px solid currentColor; white-space: nowrap; }
.st-new { color: var(--st-new); } .st-in_progress { color: var(--st-in_progress); }
.st-waiting { color: var(--st-waiting); } .st-closed { color: var(--st-closed); }
.pill.blocked { color: var(--err); }
.empty { color: var(--muted); padding: 24px; text-align: center; }
.more { display: inline-block; margin-top: 8px; }

/* Карточка клиента */
.client-layout { display: grid; gap: var(--gutter); grid-template-columns: minmax(0, 1fr) 340px; align-items: start; }
.chat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column;
  height: calc(100vh - 90px); min-height: 480px; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.chat-head .back { font-size: 1.3em; }
.chat-head .who { min-width: 0; flex: 1; }
.chat-head .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .sub { color: var(--muted); font-size: .85em; }
.chat-head .to-card { display: none; font-size: .9em; }
.chat > .flash { margin: 8px 14px 0; }
.messages { flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; }
.truncated { text-align: center; color: var(--muted); font-size: .9em; }
.day-sep { text-align: center; color: var(--muted); font-size: .8em; margin: 6px 0; }
.day-sep span { background: var(--bg); padding: 2px 10px; border-radius: 999px; }
.msg { max-width: min(78%, 640px); border-radius: 12px; padding: 8px 12px; border: 1px solid var(--border); overflow-wrap: anywhere; }
.msg-in { align-self: flex-start; background: var(--in); }
.msg-out { align-self: flex-end; background: var(--out); border-color: transparent; }
.msg-bot { align-self: flex-end; background: var(--bot); font-size: .9em; color: var(--muted); }
.msg-author { font-size: .8em; color: var(--muted); margin-bottom: 2px; }
.msg-text { white-space: pre-wrap; }
.msg-meta { font-size: .75em; color: var(--muted); text-align: right; margin-top: 2px; }
.msg-event { align-self: center; font-size: .8em; color: var(--muted); text-align: center; max-width: 90%; }
.st { margin-left: 4px; font-weight: 700; }
.st-sent { color: var(--ok); } .st-failed { color: var(--err); } .st-unknown { color: var(--warn); } .st-sending { color: var(--muted); }
.msg-problem { margin-top: 6px; padding: 6px 8px; border-radius: 8px; background: var(--err-bg); font-size: .88em; }
.msg-problem.warn { background: var(--warn-bg); }
.msg-problem .actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.msg-note { margin-top: 4px; font-size: .8em; color: var(--muted); }
.att { margin-bottom: 4px; }
.att img { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; }
.att audio, .att video { display: block; max-width: 100%; }
.att video { max-height: 320px; }
.att-file { font-size: .88em; display: inline-block; margin-top: 2px; }
.att-pending { font-size: .9em; color: var(--muted); }
.att-error { font-size: .9em; color: var(--err); }
.composer { border-top: 1px solid var(--border); padding: 10px 14px; display: grid; gap: 6px; }
.composer-row { display: flex; align-items: center; gap: 10px; }
.composer-row .primary { margin-left: auto; }
.file-btn { cursor: pointer; color: var(--accent); white-space: nowrap; }
.file-btn input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.file-name { font-size: .85em; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hint { font-size: .8em; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 16px 16px; }
.card h2:first-child { margin-top: 12px; }
.status-buttons { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.st-btn { padding: 4px 10px; font-size: .9em; border-color: currentColor; }
.st-btn.current { background: currentColor; }
.st-btn.current { color: var(--surface); }
.st-btn.st-new.current { background: var(--st-new); } .st-btn.st-in_progress.current { background: var(--st-in_progress); }
.st-btn.st-waiting.current { background: var(--st-waiting); } .st-btn.st-closed.current { background: var(--st-closed); }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; }
.facts dt { color: var(--muted); font-size: .9em; }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.visits { margin-top: 8px; font-size: .9em; }
.visits ul { padding-left: 18px; margin: 6px 0; }
.notes { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 8px; }
.notes li { background: var(--bot); border-radius: 8px; padding: 8px 10px; }
.note-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.notes small { color: var(--muted); }

/* Источники и настройки */
.table-wrap { overflow-x: auto; }
table.sources { border-collapse: collapse; width: 100%; font-size: .92em; }
table.sources th, table.sources td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
input.copy { width: 100%; min-width: 220px; font-size: .85em; }
.row-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.bind-box { background: var(--bg); border-radius: var(--radius); padding: 12px; margin-top: 8px; }

@media (max-width: 900px) {
  .client-layout { grid-template-columns: 1fr; }
  .chat { height: auto; min-height: 0; }
  .messages { max-height: 65vh; min-height: 260px; }
  .chat-head .to-card { display: inline; }
  .msg { max-width: 90%; }
}
@media (max-width: 520px) {
  body { font-size: 15px; }
  .topbar nav { gap: 12px; font-size: .92em; }
  .filters select, .filters button { flex: 1 1 auto; }
  .panel { padding: 14px; }
  .client-row .src { display: none; }
}
