:root {
  --accent: #1a6fb5; --accent-dark: #14548a; --accent-soft: #e6f0fa;
  --bg: #eef2f6; --panel: #fff; --ink: #17212b; --muted: #677585; --line: #dfe5ec;
  --in: #fff; --out: #dcebfa; --ok: #1e8e4f; --bad: #c0392b; --warn-bg: #fff4e0;
  --chat-bg: #f4f7fa;
}
@media (prefers-color-scheme: dark) {
  :root {
    --accent: #3d8fd6; --accent-dark: #2f78b8; --accent-soft: #1b2b3b;
    --bg: #0f151b; --panel: #161e26; --ink: #e5ebf1; --muted: #8b99a8; --line: #263240;
    --in: #1d2731; --out: #1b3a57; --ok: #3fbf74; --bad: #ec6b5e; --warn-bg: #3a2e16;
    --chat-bg: #11181f;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14.5px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 8px 14px; cursor: pointer; font-weight: 600; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; font-size: 22px; line-height: 1; width: 36px; height: 36px; border-radius: 50%; cursor: pointer; color: var(--accent); }
.icon-btn:hover { background: var(--accent-soft); }
.link { border: 0; background: none; padding: 0; color: var(--accent); cursor: pointer; }
.err { color: var(--bad); margin: 0; }

/* login */
.login-page { display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: grid; gap: 14px; }
.login-card label { display: grid; gap: 4px; font-weight: 600; font-size: 13px; }
.login-card input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); font-weight: 400; }
.login-brand { display: flex; gap: 10px; align-items: center; margin-bottom: 6px; }
.login-brand div { display: grid; }
.login-brand span { color: var(--muted); font-size: 13px; }

/* layout */
.app { display: grid; grid-template-columns: 340px 1fr; height: 100dvh; }
.side { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 8px 16px; }
.brand { display: flex; gap: 10px; align-items: center; }
.brand div { display: grid; line-height: 1.25; }
.brand span { color: var(--muted); font-size: 12.5px; }
.search { padding: 0 12px 10px; }
.search input { width: 100%; padding: 9px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); }
.config-warn { margin: 0 12px 10px; padding: 8px 10px; border-radius: 8px; background: var(--warn-bg); font-size: 13px; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 10px 14px; cursor: pointer; border-top: 1px solid var(--line); }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--accent-soft); }
.conv .none, .none { padding: 24px 16px; color: var(--muted); text-align: center; }
.avatar { flex: none; width: 42px; height: 42px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; font-size: 14px; }
.conv-main { min-width: 0; flex: 1; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; }
.conv-top b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.conv-top time { color: var(--muted); font-size: 12px; flex: none; }
.conv.unread .conv-top time { color: var(--accent); font-weight: 600; }
.conv.unread .conv-top b { font-weight: 800; }
.conv-bottom { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.conv-bottom .pv { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--line); flex: none; }
.dot.open { background: var(--ok); }
.badge { background: var(--accent); color: #fff; border-radius: 999px; min-width: 20px; height: 20px; padding: 0 6px; display: grid; place-items: center; font-size: 11.5px; font-weight: 700; }
.side-foot { display: flex; justify-content: space-between; padding: 10px 16px; border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }

/* chat */
.chat { display: flex; min-width: 0; min-height: 0; background: var(--chat-bg); }
.empty { margin: auto; text-align: center; color: var(--muted); }
.chat-inner { display: flex; flex-direction: column; flex: 1; min-width: 0; min-height: 0; }
.chat-inner[hidden], .empty[hidden] { display: none; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-head .back { display: none; }
.who { display: grid; min-width: 0; flex: 1; line-height: 1.3; }
.who b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who span { color: var(--muted); font-size: 12.5px; }
.window { font-size: 12.5px; padding: 4px 10px; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.window.open { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.window.closed { background: color-mix(in srgb, var(--bad) 13%, transparent); color: var(--bad); }

.messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 4px; }
.day { text-align: center; margin: 10px 0; }
.day span { background: var(--panel); color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 8px; border: 1px solid var(--line); }
.msg { display: flex; }
.msg.out { justify-content: flex-end; }
.bubble { max-width: min(560px, 78%); padding: 7px 10px 5px; border-radius: 10px; background: var(--in); border: 1px solid var(--line); overflow-wrap: anywhere; }
.msg.out .bubble { background: var(--out); border-color: transparent; }
.msg.tpl .bubble { border-left: 3px solid var(--accent); }
.text { white-space: pre-wrap; }
.text.muted { color: var(--muted); font-style: italic; }
.meta { display: flex; justify-content: flex-end; gap: 6px; font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.st.read { color: #2a9df4; }
.st.fail { color: var(--bad); cursor: help; font-weight: 600; }
.src { display: inline-block; font-size: 11px; font-weight: 700; color: var(--accent); margin-bottom: 2px; }
.src.rl { color: #9b59b6; }
.src.app { color: var(--ok); }
.media { display: block; max-width: 100%; max-height: 320px; border-radius: 6px; margin: 2px 0 4px; }
.media.sticker { max-height: 140px; }
audio { max-width: 100%; }
.doc { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 6px; background: color-mix(in srgb, var(--ink) 6%, transparent); text-decoration: none; margin-bottom: 4px; }
.doc span { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.doc small { color: var(--muted); }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 12px; }
.closed-note { font-size: 13px; background: var(--warn-bg); padding: 8px 10px; border-radius: 8px; margin-bottom: 8px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; padding: 9px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); max-height: 160px; }

/* dialog */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--panel); color: var(--ink); width: min(520px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / .45); }
.dlg { padding: 20px; display: grid; gap: 12px; max-height: 85dvh; overflow-y: auto; }
.dlg h3 { margin: 0; }
.dlg label { display: grid; gap: 4px; font-size: 13px; font-weight: 600; }
.dlg input, .dlg select { padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); font-weight: 400; }
.dlg input[readonly] { opacity: .75; }
#tplParams { display: grid; gap: 10px; }
.tpl-preview { white-space: pre-wrap; background: var(--out); padding: 10px 12px; border-radius: 10px; font-size: 13.5px; }
.tpl-preview:empty { display: none; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.kv { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.kv th { text-align: left; font-weight: 500; color: var(--muted); padding: 5px 8px 5px 0; vertical-align: top; }
.kv td { padding: 5px 0; overflow-wrap: anywhere; }
.ok { color: var(--ok); } .bad { color: var(--bad); }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--panel); padding: 10px 16px; border-radius: 10px; max-width: calc(100vw - 32px); z-index: 50; }
.toast.err { background: var(--bad); color: #fff; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .app .chat { display: none; }
  .app.in-chat .side { display: none; }
  .app.in-chat .chat { display: flex; }
  .chat-head .back { display: block; }
  .chat-head { padding: 8px 10px; flex-wrap: wrap; }
  .window { order: 5; width: 100%; text-align: center; }
  .messages { padding: 12px 10px; }
  .bubble { max-width: 88%; }
  .composer-row .btn { padding: 8px 10px; }
}
