:root {
  --brand: #1D3FA5;
  --brand-2: #0EA5E9;
  --bg: #F4F7FB;
  --panel: #FFFFFF;
  --line: #E4E9F2;
  --text: #0F172A;
  --muted: #64748B;
  --in: #FFFFFF;
  --out: #DCEBFF;
  --chat-bg: #EEF3F9;
  --ok: #10B981;
  --warn: #F59E0B;
  --err: #DC2626;
  --hover: #F1F5FB;
  --active: #E6EEFC;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0B1120; --panel: #111A2E; --line: #1F2A44; --text: #E2E8F0; --muted: #94A3B8;
    --in: #1A2540; --out: #1D3A6E; --chat-bg: #0D1527; --hover: #16213A; --active: #1C2B4D;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 9px 16px; font-weight: 600; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .6; cursor: default; }
.icon-btn { display: inline-grid; place-items: center; width: 38px; height: 38px; border: 0; background: transparent; border-radius: 10px; color: var(--muted); }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.error { color: var(--err); font-size: 13px; min-height: 1em; margin: 0; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; background: radial-gradient(1200px 600px at 20% -10%, rgba(14,165,233,.18), transparent), var(--bg); }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.login-card label { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.login-card input { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 11px 12px; }
.login-card input:focus, .search input:focus, textarea:focus, .dialog input:focus, .dialog select:focus { outline: 2px solid var(--brand-2); outline-offset: -1px; }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.brand div { display: grid; line-height: 1.2; }
.brand strong { font-size: 18px; }
.brand span { font-size: 13px; color: var(--muted); }
.brand.small { margin: 0; gap: 10px; }
.brand.small strong { font-size: 16px; }

/* layout */
.app { display: grid; grid-template-columns: 380px 1fr; height: 100dvh; }
.sidebar { 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: 14px 14px 10px 16px; }
.side-actions { display: flex; gap: 2px; }
.live { display: inline-flex; align-items: center; gap: 6px; }
.live::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--warn); }
.live.on::before { background: var(--ok); }
.search { margin: 0 14px 8px; display: flex; align-items: center; gap: 8px; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 0 12px; color: var(--muted); }
.search input { border: 0; background: transparent; padding: 10px 0; width: 100%; outline: none; }
.tabs { display: flex; gap: 6px; padding: 4px 14px 10px; border-bottom: 1px solid var(--line); }
.tabs button { border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 5px 12px; font-size: 13px; font-weight: 600; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.tabs button.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.count { background: var(--brand-2); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 6px; line-height: 18px; min-width: 18px; text-align: center; }
.tabs button.active .count { background: #fff; color: var(--brand); }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { display: grid; grid-template-columns: 44px 1fr auto; gap: 12px; align-items: center; padding: 11px 16px; cursor: pointer; border-bottom: 1px solid var(--line); }
.conv:hover { background: var(--hover); }
.conv.active { background: var(--active); }
.conv .meta { min-width: 0; }
.conv .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .last { color: var(--muted); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .right { display: grid; justify-items: end; gap: 4px; font-size: 12px; color: var(--muted); }
.conv.unread .name { font-weight: 800; }
.conv.unread .last { color: var(--text); font-weight: 600; }
.conv.unread .time { color: var(--brand-2); font-weight: 700; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--brand-2); }
.tag { font-size: 11px; font-weight: 700; color: var(--warn); }
.avatar { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; background: linear-gradient(135deg, var(--brand), var(--brand-2)); font-size: 15px; flex: none; }
.more { margin: 10px auto; display: block; border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 6px 14px; font-size: 13px; color: var(--muted); }
.list-empty { padding: 30px 20px; color: var(--muted); text-align: center; font-size: 14px; }

/* chat */
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--chat-bg); }
.empty { margin: auto; text-align: center; color: var(--muted); padding: 24px; max-width: 420px; }
.empty h1 { color: var(--text); font-size: 22px; margin: 16px 0 6px; }
.chat-view { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-head .avatar { width: 40px; height: 40px; }
.chat-title { display: grid; min-width: 0; flex: 1; line-height: 1.25; }
.chat-title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title span { color: var(--muted); font-size: 13px; }
.pill { font-size: 12px; font-weight: 700; padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.pill.open { background: rgba(16,185,129,.12); color: #059669; }
.pill.closed { background: rgba(245,158,11,.14); color: #B45309; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px clamp(12px, 4vw, 48px) 8px; min-height: 0; }
.day { text-align: center; margin: 14px 0 10px; }
.day span { background: var(--panel); color: var(--muted); font-size: 12px; font-weight: 600; padding: 4px 12px; border-radius: 999px; box-shadow: var(--shadow); }
.msg { display: flex; margin: 3px 0; }
.msg.out { justify-content: flex-end; }
.bubble { max-width: min(560px, 78%); background: var(--in); border-radius: 14px; padding: 7px 10px 5px; box-shadow: 0 1px 1px rgba(15,23,42,.06); position: relative; overflow-wrap: anywhere; }
.msg.in .bubble { border-top-left-radius: 4px; }
.msg.out .bubble { background: var(--out); border-top-right-radius: 4px; }
.bubble .text { white-space: pre-wrap; }
.bubble .foot { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.bubble .tpl { font-size: 11px; font-weight: 700; color: var(--brand); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 3px; }
.bubble img.media { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; margin-bottom: 4px; cursor: zoom-in; background: var(--hover); min-height: 80px; min-width: 120px; }
.bubble video, .bubble audio { max-width: 100%; display: block; margin-bottom: 4px; }
.bubble .doc { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: var(--hover); text-decoration: none; color: inherit; margin-bottom: 4px; }
.bubble .quote { border-left: 3px solid var(--brand-2); background: rgba(14,165,233,.08); border-radius: 6px; padding: 4px 8px; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.bubble .btns { display: grid; gap: 4px; margin-top: 6px; }
.bubble .btns span { text-align: center; font-size: 13px; font-weight: 600; color: var(--brand-2); border-top: 1px solid var(--line); padding-top: 5px; }
.bubble .reply-act { position: absolute; top: 4px; right: -34px; opacity: 0; width: 28px; height: 28px; }
.msg.out .bubble .reply-act { right: auto; left: -34px; }
.bubble:hover .reply-act, .bubble:focus-within .reply-act { opacity: 1; }
.ticks { display: inline-flex; }
.ticks svg { width: 16px; height: 16px; }
.ticks.read { color: var(--brand-2); }
.ticks.failed { color: var(--err); }
.failed-note { color: var(--err); font-size: 12px; margin-top: 3px; }
.pending { opacity: .65; }

.reply-bar { display: flex; align-items: center; gap: 10px; background: var(--panel); border-top: 1px solid var(--line); padding: 8px 16px; }
.reply-bar div { flex: 1; border-left: 3px solid var(--brand-2); padding-left: 10px; min-width: 0; }
.reply-bar span { font-size: 12px; font-weight: 700; color: var(--brand-2); }
.reply-bar p { margin: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); position: relative; }
.composer .open { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); background: var(--bg); border-radius: 14px; padding: 10px 14px; max-height: 160px; min-height: 42px; outline: none; overflow-y: hidden; }
.send { width: 42px; height: 42px; border: 0; border-radius: 50%; background: var(--brand); color: #fff; display: grid; place-items: center; margin-left: 4px; }
.send:disabled { opacity: .5; }
.closed { display: flex; align-items: center; gap: 14px; justify-content: space-between; flex-wrap: wrap; }
.closed p { margin: 0; font-size: 14px; color: var(--muted); flex: 1; min-width: 220px; }
.quick { position: absolute; bottom: calc(100% + 6px); left: 12px; right: 12px; max-width: 520px; max-height: 320px; overflow-y: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 6px; }
.quick button { display: block; width: 100%; text-align: left; border: 0; background: transparent; border-radius: 10px; padding: 9px 12px; }
.quick button:hover { background: var(--hover); }
.quick button strong { display: block; font-size: 13px; }
.quick button span { display: block; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* dialog */
.dialog { border: 1px solid var(--line); border-radius: 18px; padding: 0; width: min(520px, calc(100vw - 24px)); background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(2, 6, 23, .45); }
.dialog form { display: grid; gap: 14px; padding: 18px 20px 20px; }
.dialog header, .dialog footer { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.dialog footer { justify-content: flex-end; }
.dialog h2 { margin: 0; font-size: 18px; }
.dialog label { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.dialog input, .dialog select { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px; }
.params { display: grid; gap: 10px; }
.preview { white-space: pre-wrap; background: var(--chat-bg); border-radius: 12px; padding: 12px 14px; font-size: 14px; border: 1px solid var(--line); }
.preview:empty { display: none; }
.preview mark { background: rgba(14,165,233,.2); color: inherit; border-radius: 4px; padding: 0 2px; }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: #0F172A; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; box-shadow: var(--shadow); z-index: 50; max-width: calc(100vw - 32px); }
.toast.err { background: var(--err); }

.lightbox { position: fixed; inset: 0; background: rgba(2,6,23,.85); display: grid; place-items: center; z-index: 60; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 92vh; border-radius: 10px; }

/* mobile */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .app .chat { display: none; }
  .app.show-chat .sidebar { display: none; }
  .app.show-chat .chat { display: flex; }
  .back { display: inline-grid; }
  .pill { display: none; }
  .bubble { max-width: 86%; }
  .bubble .reply-act { display: none; }
}
