:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --line: #e3e6ea;
  --text: #1d2329;
  --muted: #6b7580;
  --accent: #2563eb;
  --accent-soft: #e8effd;
  --accent2: #0EA5E0;
  --danger: #c43b3b;
  --in: #ffffff;
  --out: #e3f3fc;
  --sys: #fff6dd;
  --wa: #1fa855;
  --tg: #229ed9;
  --max: #6a4cf5;
  --avito: #e6332a;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181c;
    --panel: #1c2126;
    --line: #2c333a;
    --text: #e6e9ec;
    --muted: #8c96a0;
    --accent: #7aa2f7;
    --accent-soft: #1f2a44;
    --in: #232a30;
    --out: #17364a;
    --sys: #3a3420;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1em; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: none; font-size: 18px; cursor: pointer; padding: 4px 8px; border-radius: 6px; color: var(--muted); }
.icon-btn:hover { background: var(--bg); color: var(--text); }
a { color: var(--accent); }

/* Вход */
.login { display: grid; place-items: center; min-height: 100%; padding: 16px 0; }
.login-card { background: var(--panel); padding: 32px; border-radius: 14px; width: min(360px, 92vw); display: grid; gap: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.08); }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.sso-box { display: grid; gap: 8px; text-align: center; }
.sso-box a.btn { text-decoration: none; }
.sso-box p { margin: 0; }
.login-card label { display: grid; gap: 6px; color: var(--muted); }

/* Раскладка */
.app { display: grid; grid-template-columns: 340px 1fr auto; height: 100%; }
.list-pane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.top { display: flex; align-items: center; gap: 6px; padding: 12px 10px 8px 16px; }
.brand { font-weight: 700; font-size: 16px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.channel-dots { display: flex; gap: 6px; }
.channel-dots span { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.connected { background: #2bb673; }
.dot.connecting { background: #e5a50a; }
.dot.need_login, .dot.error { background: var(--danger); }
.dot.not_configured, .dot.disabled { background: var(--line); }

.filters { padding: 0 12px 8px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.filters input { width: 100%; }
.tabs, .chips { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button, .chips button { border: 1px solid transparent; background: none; padding: 4px 8px; border-radius: 6px; cursor: pointer; color: var(--muted); font-size: 13px; display: inline-flex; align-items: center; gap: 5px; }
.tabs button.active, .chips button.active { background: var(--accent-soft); color: var(--accent); }
.ch { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }
.ch-whatsapp { background: var(--wa); } .ch-telegram { background: var(--tg); } .ch-max { background: var(--max); } .ch-avito { background: var(--avito); }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--line); }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--accent-soft); }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; position: relative; flex-shrink: 0; }
.avatar .badge { position: absolute; right: -2px; bottom: -2px; font-size: 9px; font-weight: 700; padding: 1px 3px; border-radius: 4px; border: 2px solid var(--panel); line-height: 1; }
.bg-whatsapp { background: var(--wa); } .bg-telegram { background: var(--tg); } .bg-max { background: var(--max); } .bg-avito { background: var(--avito); }
.conv .mid { min-width: 0; }
.conv .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .prev { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.conv .meta { text-align: right; font-size: 12px; color: var(--muted); display: grid; gap: 4px; justify-items: end; align-content: start; }
.unread { background: var(--accent); color: #fff; border-radius: 10px; padding: 1px 7px; font-size: 11px; font-weight: 700; }
.owner { font-size: 11px; color: var(--muted); }

/* Чат */
.chat-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.empty { margin: auto; color: var(--muted); }
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-title { flex: 1; min-width: 0; }
.chat-title .name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title .sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 16px 18px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: min(640px, 80%); padding: 7px 10px 5px; border-radius: 10px; background: var(--in); box-shadow: 0 1px 1px rgba(0,0,0,.05); white-space: pre-wrap; word-wrap: break-word; }
.msg.out { align-self: flex-end; background: var(--out); }
.msg.system { align-self: center; background: var(--sys); font-size: 12px; color: var(--muted); max-width: 90%; }
.msg .att { display: inline-block; font-size: 12px; color: var(--muted); border: 1px dashed var(--line); border-radius: 6px; padding: 2px 6px; margin-bottom: 3px; }
.msg .foot { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.msg.failed { border: 1px solid var(--danger); }
.msg.failed .foot { color: var(--danger); }
.day { align-self: center; font-size: 12px; color: var(--muted); padding: 6px 0; }
.composer { display: flex; gap: 8px; padding: 10px 14px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 180px; min-height: 38px; }

/* Карточка клиента */
.info-pane { width: 340px; overflow: hidden; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.info-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-bottom: 1px solid var(--line); font-weight: 600; }
.info-body { padding: 14px; overflow-y: auto; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; align-content: start; }
.field { display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; }
.field label { font-size: 12px; color: var(--muted); }
.row { display: flex; gap: 6px; align-items: center; }
.row > input { flex: 1; min-width: 0; }
.block { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.block h3 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.order { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 13px; }
.order:first-of-type { border-top: 0; }
.state { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--bg); justify-self: start; }
.results { list-style: none; margin: 0; padding: 0; max-height: 220px; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; }
.results:empty { display: none; }
.results li { padding: 7px 10px; cursor: pointer; border-top: 1px solid var(--line); font-size: 13px; }
.results li:first-child { border-top: 0; }
.results li:hover { background: var(--accent-soft); }
.small { font-size: 12px; }

/* Модальное окно */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; z-index: 10; padding: 16px; }
.modal-card { background: var(--panel); border-radius: 14px; width: min(720px, 100%); max-height: 90vh; overflow-y: auto; }
.modal-card > header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); }
.modal-card h2 { margin: 0; font-size: 17px; }
#modal-body { padding: 18px; display: grid; gap: 16px; }
.chan { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 10px; }
.chan-head { display: flex; align-items: center; gap: 10px; }
.chan-head b { flex: 1; }
.qr img { display: block; }
.qr { background: #fff; padding: 12px; border-radius: 8px; justify-self: start; }
table { width: 100%; border-collapse: collapse; }
td, th { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: left; font-size: 13px; }
td input { width: 80px; padding: 5px 6px; }
.total { font-size: 16px; font-weight: 700; text-align: right; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel); padding: 10px 16px; border-radius: 8px; z-index: 20; max-width: 90vw; }
.toast.err { background: var(--danger); color: #fff; }

/* Телефон */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .app.chat-open .list-pane { display: none; }
  .app:not(.chat-open) .chat-pane { display: none; }
  .back { display: inline-block; }
  .info-pane { position: fixed; inset: 0; width: auto; z-index: 5; }
  #assign { max-width: 110px; }
}

.new-chat { padding: 12px 14px; border-bottom: 1px solid var(--line); background: var(--accent-soft); display: grid; gap: 8px; }
.new-chat .row { flex-wrap: wrap; }

.att-img { display: block; max-width: min(320px, 100%); max-height: 320px; border-radius: 8px; margin-bottom: 4px; }
.att-audio { display: block; width: 260px; max-width: 100%; margin-bottom: 2px; }
.att-video { display: block; max-width: min(360px, 100%); border-radius: 8px; margin-bottom: 4px; }
.att-file { display: inline-block; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); text-decoration: none; margin-bottom: 4px; }
#btn-voice.recording { color: var(--danger); background: var(--accent-soft); }
.composer .icon-btn { align-self: center; }

.search-row { display: flex; gap: 6px; }
.search-row input { flex: 1; min-width: 0; }

.channel-dots { flex-shrink: 1; min-width: 0; overflow: hidden; }
details.keys { border-top: 1px dashed var(--line); padding-top: 8px; }
details.keys summary { cursor: pointer; color: var(--accent); }
.keys-form { display: grid; gap: 8px; margin-top: 8px; }
pre.logs { font-size: 11px; line-height: 1.4; white-space: pre-wrap; word-break: break-all; max-height: 60vh; overflow: auto; background: var(--bg); padding: 10px; border-radius: 8px; margin: 0; }

.brand { display: flex; align-items: center; gap: 8px; }
.brand-logo { height: 34px; width: auto; border-radius: 4px; background: #fff; }
.login-logo { height: 90px; width: auto; justify-self: center; }
.unread { background: var(--accent2); }
.conv:has(.unread) .name { color: var(--accent); }
.picker { position: relative; margin: 0 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; max-height: 260px; overflow-y: auto; box-shadow: 0 6px 24px rgba(0,0,0,.12); }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); padding: 6px; }
.emoji-grid button { font-size: 22px; background: none; border: 0; padding: 4px; cursor: pointer; border-radius: 6px; }
.emoji-grid button:hover, .sticker-grid button:hover { background: var(--accent-soft); }
.sticker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 4px; padding: 8px; }
.sticker-grid button { background: none; border: 0; padding: 4px; cursor: pointer; border-radius: 8px; }
.sticker-grid img { width: 100%; height: auto; display: block; }
.sticker-btn img { width: 26px; height: 26px; display: block; }
#btn-read { white-space: nowrap; }

.att-sticker { max-width: 150px; max-height: 150px; }

.list-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); background: var(--panel); }
.list-foot .me { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.list-foot .btn { flex-shrink: 0; }

.msg .author { font-size: 12px; font-weight: 600; color: var(--accent2); margin-bottom: 2px; white-space: normal; }

.seg { display: flex; background: var(--bg); border-radius: 8px; padding: 2px; gap: 2px; }
.seg button { flex: 1; border: 0; background: none; padding: 5px 6px; border-radius: 6px; cursor: pointer; font-size: 13px; color: var(--muted); white-space: nowrap; }
.seg button.active { background: var(--panel); color: var(--accent); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
/* Шапка чата: название не должно исчезать, кнопки переносятся на вторую строку */
.chat-head { flex-wrap: wrap; row-gap: 6px; }
.chat-title { flex: 1 1 180px; }

.tab-badge:not(:empty) { margin-left: 4px; background: var(--accent2); color: #fff; border-radius: 9px; padding: 0 6px; font-size: 11px; font-weight: 700; }
#folder { max-width: 150px; }
.tpl-head { display: flex; gap: 6px; padding: 8px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); }
.tpl-head input { flex: 1; min-width: 0; }
.tpl-item { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-top: 1px solid var(--line); }
.tpl-list .tpl-item:first-child, .tpl-manage .tpl-item:first-child { border-top: 0; }
.tpl-main { flex: 1; min-width: 0; }
.tpl-main .small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tpl-btns { display: flex; gap: 4px; flex-shrink: 0; }
.tpl-manage { border: 1px solid var(--line); border-radius: var(--radius); }
.tpl-files { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.tpl-file { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 8px; padding: 3px 4px 3px 8px; font-size: 13px; }
.tpl-file img { width: 32px; height: 32px; object-fit: cover; border-radius: 4px; }

/* подсказки нейросети и источники товаров */
.picker.ai-box { max-height: 55vh; }
.ai-wait { padding: 14px; }
.ai-variant { white-space: pre-wrap; font-size: 14px; line-height: 1.4; }
.ai-tip { margin: 6px 10px; padding: 8px 10px; background: var(--accent-soft); border-radius: 8px; font-size: 13px; }
.ai-stock { border-top: 1px solid var(--line); padding: 8px 10px; display: flex; flex-direction: column; gap: 3px; }
.ai-form { display: flex; flex-direction: column; gap: 10px; }
.ai-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.ai-form label.row { flex-direction: row; align-items: center; }
.ai-form textarea { width: 100%; font: inherit; padding: 8px; border: 1px solid var(--line); border-radius: 8px; resize: vertical; box-sizing: border-box; }
.ai-sub { display: flex; flex-direction: column; gap: 10px; padding: 10px; border: 1px dashed var(--line); border-radius: 8px; }
.ai-sub.hidden { display: none; }

/* Приложение на телефоне: вырезы и «чёлка» iPhone, полоска «Домой» */
.grid-gap { display: grid; gap: 8px; grid-template-columns: minmax(0, 1fr); }
.grid-gap .btn { white-space: normal; }
.top { padding-top: max(12px, env(safe-area-inset-top)); }
.chat-head { padding-top: max(10px, env(safe-area-inset-top)); }
.composer { padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.list-foot { padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.app, .login { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
@media (max-width: 900px) {
  /* Пальцем удобнее: крупнее зоны нажатия, поле ввода 16px — иначе iPhone приближает страницу при наборе */
  input, select, textarea { font-size: 16px; }
  .btn { min-height: 38px; }
  .info-pane { padding-top: env(safe-area-inset-top); }
}
@media (display-mode: standalone) {
  body { overscroll-behavior-y: none; }
}
/* Строка ввода на телефоне: поле и «Отправить» — сверху во всю ширину, значки — отдельной строкой под ними */
@media (max-width: 600px) {
  .composer { flex-wrap: wrap; gap: 6px 2px; padding: 8px 10px max(8px, env(safe-area-inset-bottom)); }
  .composer textarea { order: 1; flex: 1 1 calc(100% - 120px); min-width: 0; }
  .composer button[type="submit"] { order: 2; }
  .composer .icon-btn { order: 3; flex: 1 0 auto; font-size: 22px; padding: 6px 4px; }
  .composer .sticker-btn img { width: 28px; height: 28px; margin: 0 auto; }
}
/* Окна (настройки и т.п.) не шире экрана; на телефоне — во весь экран с учётом выреза */
.modal { grid-template-columns: minmax(0, 720px); justify-content: center; }
.modal-card { min-width: 0; }
#modal-body > * { min-width: 0; }
.chan { grid-template-columns: minmax(0, 1fr); }
input[type="file"] { max-width: 100%; }
@media (max-width: 600px) {
  .modal { padding: 0; place-items: stretch; }
  .modal-card { width: 100%; max-height: none; height: 100%; border-radius: 0; }
  .modal-card > header { padding-top: max(14px, env(safe-area-inset-top)); }
  #modal-body { padding: 14px 14px max(18px, env(safe-area-inset-bottom)); }
  #modal-body .row { flex-wrap: wrap; }
  #modal-body input[type="file"] { flex: 1 1 100%; min-width: 0; max-width: 100%; }
}

/* Галочки доставки и прочтения */
.ticks { display: inline-block; margin-left: 4px; font-size: 12px; letter-spacing: -3px; padding-right: 3px; color: var(--muted); font-weight: 600; }
.ticks.read { color: #2f9bf0; }
.prev .ticks { margin-left: 0; margin-right: 2px; }
#messages .ticks { cursor: help; }

/* Имя автора над своими сообщениями и в списке диалогов */
.msg.out .out-author { font-size: 12px; font-weight: 600; margin-bottom: 2px; }
.prev-author { font-weight: 600; color: var(--text); }
@media (prefers-color-scheme: dark) { :root { --name-l: 70%; } }
#sig-mode, #sig-name { max-width: 100%; min-width: 0; }

.sig-sample { white-space: pre-wrap; margin-top: 4px; padding: 8px 10px; border-radius: 10px; background: var(--bg); font-weight: 600; max-width: 320px; }
#sig-company { display: block; width: 100%; margin-top: 4px; }

/* ---------- платформа: бренд компании, подписка, новые каналы ---------- */
.brand-name { font-weight: 700; font-size: 17px; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.brand-name.big { font-size: 24px; text-align: center; margin-bottom: 4px; }
.login-links { display: flex; flex-direction: column; gap: 6px; align-items: center; font-size: 14px; margin-top: 8px; }
.bill-bar { background: #fff7e6; color: #8a5300; font-size: 13px; padding: 8px 12px; border-bottom: 1px solid var(--line, #e5e7eb); }
.bill-bar a { font-weight: 600; }
@media (prefers-color-scheme: dark) { .bill-bar { background: #3a2e12; color: #f3c46b; } }
.switch { display: inline-flex; align-items: center; gap: 4px; margin-left: auto; cursor: pointer; white-space: nowrap; }
.switch input { width: auto; }
.role-card { border: 1px solid var(--line, #e5e7eb); border-radius: 10px; padding: 8px; display: grid; gap: 6px; }
.role-card .row input { flex: 1; }
.logo-prev { max-height: 40px; max-width: 160px; object-fit: contain; }
.ch-vk, .ch-vkuser { background: #0077ff; }
.ch-viber { background: #7360f2; }
.ch-cian { background: #0468ff; }
.ch-instagram { background: linear-gradient(45deg, #f9ce34, #ee2a7b 55%, #6228d7); }
.ai-form label.row { display: flex; gap: 8px; align-items: center; justify-content: flex-start; }
.ai-form label.row input[type=checkbox] { flex: none; }
.bg-vk, .bg-vkuser { background: #0077ff; }
