:root {
  --bg: #f4f5f7; --panel: #fff; --line: #e3e5e8; --text: #1d2126; --muted: #6b7280;
  --accent: #2563eb; --accent-2: #dbe7ff; --in: #fff; --out: #e3f0ff; --bad: #c62828; --ok: #2e7d32;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #15171a; --panel: #1e2125; --line: #30343a; --text: #e8eaed; --muted: #9aa0a6;
    --accent: #5b8def; --accent-2: #25324a; --in: #262a2f; --out: #22324a; --bad: #ef6b6b; --ok: #6bcf7a; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
textarea { resize: vertical; }
label { display: block; margin: 10px 0; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--text); font-size: 15px; }
label.check { display: flex; gap: 8px; align-items: center; }
label.check input { width: auto; }
code { background: var(--accent-2); padding: 1px 5px; border-radius: 4px; word-break: break-all; }
.btn { display: inline-block; padding: 8px 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); cursor: pointer; text-decoration: none; color: var(--text); white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .6; cursor: wait; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.bad { color: var(--bad); }
.grow { flex: 1; }
.hidden { display: none !important; }
.alert { padding: 10px 12px; border-radius: 8px; background: #fdecea; color: #8a1c1c; margin: 10px 0; }
.alert.ok { background: #e7f5ea; color: #1e5a2a; }
.placeholder { color: var(--muted); padding: 40px 20px; text-align: center; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; align-items: end; }
.row { display: flex; gap: 8px; }

/* Простые страницы */
.page { padding: 24px 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 16px; }
.narrow { max-width: 420px; margin: 40px auto; }
.wide { max-width: 860px; margin: 0 auto; }
.checks { list-style: none; padding: 0; }
.checks .ok { color: var(--ok); } .checks .bad { color: var(--bad); }
.tbl { width: 100%; border-collapse: collapse; }
.tbl td { padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
form.inline { display: flex; gap: 6px; }
form.inline input { width: 150px; }

/* Приложение */
.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }
.top { display: flex; gap: 8px; align-items: center; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.top .brand { font-weight: 700; font-size: 17px; margin-right: 6px; }
.top input[type=search] { max-width: 280px; }
.top select { width: auto; }
.spacer { flex: 1; }
.panes { flex: 1; display: flex; min-height: 0; }
.list { width: 340px; flex-shrink: 0; overflow-y: auto; border-right: 1px solid var(--line); background: var(--panel); }
.item { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.item:hover { background: var(--bg); }
.item.active { background: var(--accent-2); }
.item-top { display: flex; justify-content: space-between; gap: 8px; }
.item-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item.unread .item-name::before { content: "● "; color: var(--accent); }
.item-time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.item-mid { display: flex; gap: 6px; align-items: center; margin: 4px 0; flex-wrap: wrap; }
.item-prev { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { margin-left: auto; background: var(--accent); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; }
.ch, .st { font-size: 11px; padding: 1px 7px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); white-space: nowrap; }
.ch-telegram { color: #1e88e5; border-color: #90caf9; }
.ch-email { color: #8e24aa; border-color: #ce93d8; }
.ch-manual { color: #6d4c41; border-color: #bcaaa4; }
.st-new { background: #fff3cd; color: #7a5b00; border-color: #f3d98b; }
.st-ready { background: #e7f5ea; color: #1e5a2a; border-color: #a5d6a7; }
.st-waiting { background: #fdecea; color: #8a1c1c; border-color: #f5b5b0; }
.st-done, .st-rejected { opacity: .7; }

.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat.empty { justify-content: center; }
.chat-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-title { font-weight: 600; }
.back { display: none; }
.msgs { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 75%; padding: 8px 12px; border-radius: 12px; background: var(--in); border: 1px solid var(--line); align-self: flex-start; }
.msg.out { background: var(--out); align-self: flex-end; }
.msg-subj { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.msg-body { white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg-atts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.msg-atts img { max-width: 240px; max-height: 240px; border-radius: 8px; display: block; }
.msg-atts video { max-width: 280px; border-radius: 8px; }
.msg-meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 3px; }
.composer { padding: 10px 14px; background: var(--panel); border-top: 1px solid var(--line); }
.composer-bar { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.tpl { width: auto; max-width: 200px; }

.side { width: 300px; flex-shrink: 0; overflow-y: auto; background: var(--panel); border-left: 1px solid var(--line); padding: 4px 14px 14px; }
.card-head { display: flex; justify-content: space-between; align-items: center; }
.card-head .btn { display: none; }

dialog { border: none; padding: 0; background: transparent; width: min(440px, 94vw); }
dialog::backdrop { background: rgba(0,0,0,.35); }

@media (max-width: 1100px) {
  .side { position: fixed; right: 0; top: 0; bottom: 0; z-index: 5; box-shadow: -4px 0 20px rgba(0,0,0,.15); display: none; }
  body.show-card .side { display: block; }
  .card-head .btn { display: inline-block; }
}
@media (min-width: 1101px) { .chat-head .btn.ghost:last-child { display: none; } }
@media (max-width: 760px) {
  .list { width: 100%; border-right: none; }
  .chat { display: none; }
  body.show-chat .list { display: none; }
  body.show-chat .chat { display: flex; }
  .back { display: inline-block; }
  .top input[type=search] { max-width: none; order: 10; flex-basis: 100%; }
  .msg { max-width: 90%; }
  .side { width: 100%; }
}
.acc { color: var(--muted); white-space: nowrap; }

/* Вложения и смайлики в поле ответа */
.composer { position: relative; }
.composer.drag { outline: 2px dashed var(--accent); outline-offset: -4px; }
.btn.icon { padding: 6px 10px; font-size: 18px; line-height: 1; }
.files-box { display: flex; flex-wrap: wrap; gap: 6px; }
.files-box:not(:empty) { margin-bottom: 8px; }
.file-chip { display: flex; align-items: center; gap: 6px; max-width: 220px; padding: 4px 6px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.file-chip img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; }
.file-ico { font-size: 24px; }
.file-name { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-x { border: none; background: none; cursor: pointer; color: var(--muted); padding: 0 2px; }
.emoji-box { position: absolute; bottom: 56px; left: 14px; z-index: 4; width: 312px; max-width: calc(100% - 28px); display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; padding: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.15); }
.emoji { border: none; background: none; font-size: 22px; padding: 4px 0; cursor: pointer; border-radius: 6px; }
.emoji:hover { background: var(--bg); }
