:root {
  --bg: #f4f5f8;
  --panel: #ffffff;
  --panel-2: #f8f9fb;
  --line: #e5e7ee;
  --text: #1b1d24;
  --muted: #6b7080;
  --accent: #5b5bd6;
  --accent-2: #ececfd;
  --in-bubble: #ffffff;
  --out-bubble: #e7e7fc;
  --pending: #fff6df;
  --pending-line: #f1c45b;
  --danger: #d6455d;
  --danger-bg: #fdecef;
  --ok: #1f9d63;
  --ok-bg: #e5f6ee;
  --warn: #b7791f;
  --shadow: 0 6px 24px rgba(20, 22, 40, .08);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --panel: #181b22;
    --panel-2: #1e222b;
    --line: #2a2f3a;
    --text: #e8eaf0;
    --muted: #8d93a3;
    --accent: #8484f0;
    --accent-2: #262848;
    --in-bubble: #222631;
    --out-bubble: #2e2f5a;
    --pending: #3a3220;
    --pending-line: #a98a3e;
    --danger: #f06a80;
    --danger-bg: #3a1e25;
    --ok: #3fc98a;
    --ok-bg: #173126;
    --warn: #e0a84a;
    --shadow: 0 6px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }
kbd {
  display: inline-block; padding: 1px 6px; border: 1px solid var(--line); border-bottom-width: 2px;
  border-radius: 5px; background: var(--panel); font-size: 12px; font-family: inherit;
}

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: 8px 14px;
  cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  width: 34px; height: 34px; border-radius: 9px; border: 0; background: transparent; cursor: pointer; font-size: 16px;
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.icon-btn:hover { background: var(--panel-2); }
.link-btn { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-weight: 600; font-size: 12px; }
.link-btn.danger { color: var(--danger); }

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.login-card label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 13px; }
.login-card input, .login-card select, .modal-card input, .modal-card select, .modal-card textarea {
  border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; background: var(--panel-2); font-weight: 400;
}
.error { color: var(--danger); min-height: 18px; font-size: 13px; }

.brand { display: flex; align-items: center; gap: 9px; font-weight: 750; letter-spacing: -.01em; }
.brand.big { font-size: 20px; }
.logo {
  width: 24px; height: 24px; border-radius: 7px; background: var(--accent); display: inline-block; position: relative;
}
.logo::after {
  content: ""; position: absolute; left: 6px; top: 7px; width: 12px; height: 2.5px; background: #fff; border-radius: 2px;
  box-shadow: 0 4.5px 0 -0px #fff, 0 9px 0 0 #fff; transform-origin: left;
}

/* ---------- app shell ---------- */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  height: 54px; flex: none; display: flex; align-items: center; gap: 10px; padding: 0 14px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.sync { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.sync::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.sync.bad::before { background: var(--danger); }
.me { font-weight: 600; font-size: 13px; padding: 0 4px; }
.chip-btn {
  display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--line); background: var(--panel-2);
  border-radius: 999px; padding: 6px 12px; cursor: pointer; font-weight: 600; font-size: 13px; white-space: nowrap;
}
.chip-btn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.chip-btn.active .dot { background: var(--warn); animation: pulse 1.6s infinite; }
.chip-btn.alarm { border-color: var(--danger); color: var(--danger); }
.chip-btn.alarm .dot { background: var(--danger); }
@keyframes pulse { 50% { opacity: .35; } }

.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 360px minmax(0, 1fr) 340px; }
.layout.no-queue { grid-template-columns: 360px minmax(0, 1fr); }
.layout.no-queue .queue-col { display: none; }

/* ---------- leads ---------- */
.leads-col { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.leads-tools { padding: 12px; display: flex; flex-direction: column; gap: 9px; border-bottom: 1px solid var(--line); }
.search input {
  width: 100%; border: 1px solid var(--line); background: var(--panel-2); border-radius: 9px; padding: 8px 11px;
}
.filters { display: flex; flex-wrap: wrap; gap: 5px; }
.filters button {
  border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 3px 10px; cursor: pointer;
  font-size: 12.5px; font-weight: 600; color: var(--muted);
}
.filters button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.filters button .n { opacity: .75; font-weight: 500; margin-left: 3px; }
.filters-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.filters-row select { border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 5px 7px; font-size: 12.5px; min-width: 0; }
.lead-list { flex: 1; overflow-y: auto; }
.lead {
  display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; gap: 10px; padding: 10px 12px; cursor: pointer;
  border-bottom: 1px solid var(--line); position: relative;
}
.lead:hover { background: var(--panel-2); }
.lead.active { background: var(--accent-2); }
.lead.active::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff;
  font-size: 15px; overflow: hidden; flex: none;
}
.avatar { position: relative; }
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.lead .name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lead.unread .name { font-weight: 800; }
.lead .sub { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); min-width: 0; }
.lead .sub .acc { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lead .preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.lead .preview .you { color: var(--text); opacity: .65; }
.lead.unread .preview { color: var(--text); }
.lead .meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 11.5px; color: var(--muted); }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; background: var(--accent); color: #fff;
  display: inline-grid; place-items: center; font-size: 11.5px; font-weight: 700;
}
.qbadge { background: var(--pending); color: var(--warn); border: 1px solid var(--pending-line); border-radius: 999px; padding: 0 7px; font-size: 11.5px; font-weight: 700; white-space: nowrap; }
.qbadge.fail { background: var(--danger-bg); color: var(--danger); border-color: var(--danger); }
.waiting { color: var(--danger); font-weight: 700; }
.sdot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.more { padding: 14px; text-align: center; }
.list-empty { padding: 30px 16px; text-align: center; color: var(--muted); }

/* ---------- chat ---------- */
.chat-col { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.empty-chat { flex: 1; display: grid; place-items: center; padding: 24px; }
.empty-inner { max-width: 440px; text-align: center; }
.empty-icon { font-size: 40px; }
.kbd-help { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; text-align: left; font-size: 13px; color: var(--muted); margin-top: 18px; }
.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-head {
  background: var(--panel); border-bottom: 1px solid var(--line); padding: 10px 16px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.chat-head .who { min-width: 0; flex: 1 1 200px; }
.chat-head .who .name { font-weight: 750; font-size: 15px; display: flex; gap: 8px; align-items: center; }
.chat-head .who .name a { color: inherit; text-decoration: none; }
.chat-head .who .name a:hover { text-decoration: underline; }
.chat-head .who .sub { font-size: 12.5px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.chat-head select { border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 5px 8px; font-size: 13px; max-width: 200px; }
.window-warn { color: var(--danger); font-weight: 600; }

.messages { flex: 1; overflow-y: auto; padding: 18px 18px 8px; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); padding: 2px 10px; border-radius: 999px; margin: 8px 0; }
.msg { max-width: min(560px, 78%); display: flex; flex-direction: column; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble {
  padding: 8px 12px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
  background: var(--in-bubble); border: 1px solid var(--line);
}
.msg.in .bubble { border-bottom-left-radius: 4px; }
.msg.out .bubble { background: var(--out-bubble); border-color: transparent; border-bottom-right-radius: 4px; }
.msg.pending .bubble { background: var(--pending); border: 1px dashed var(--pending-line); }
.msg.failed .bubble { background: var(--danger-bg); border: 1px solid var(--danger); }
.msg .time { font-size: 11px; color: var(--muted); margin: 2px 4px 0; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.msg.in .time { justify-content: flex-start; }
.bubble img, .bubble video { max-width: 260px; max-height: 300px; border-radius: 9px; display: block; margin: 2px 0; }
.bubble .att-link { color: var(--accent); }
.bubble .reply { font-size: 12px; color: var(--muted); border-left: 2px solid var(--accent); padding-left: 7px; margin-bottom: 4px; }
.status-line { font-weight: 600; color: var(--warn); }
.msg.failed .status-line { color: var(--danger); }
.edit-area { width: min(480px, 70vw); border: 1px solid var(--pending-line); border-radius: 10px; padding: 8px; background: var(--panel); resize: vertical; min-height: 60px; }
.load-earlier { align-self: center; }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 12px 8px; position: relative; }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 11px; background: var(--panel-2); padding: 9px 12px;
  max-height: 220px; min-height: 40px; line-height: 1.4; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.urgent { display: flex; align-items: center; gap: 5px; font-size: 12.5px; color: var(--muted); padding: 0 4px 10px; cursor: pointer; white-space: nowrap; user-select: none; }
.composer .btn.primary { height: 40px; }
.composer-hint { font-size: 12px; color: var(--muted); padding: 6px 4px 0; min-height: 22px; display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.composer-hint b { color: var(--text); }
.attach-preview { display: flex; align-items: center; gap: 10px; padding: 6px 8px; margin-bottom: 8px; background: var(--panel-2); border-radius: 10px; border: 1px solid var(--line); font-size: 13px; }
.attach-preview img { height: 48px; border-radius: 6px; }
.tpl-pop {
  position: absolute; left: 12px; right: 12px; bottom: calc(100% - 4px); background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow); max-height: 320px; overflow-y: auto; z-index: 10; padding: 6px;
}
.tpl-pop input { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; background: var(--panel-2); margin-bottom: 4px; }
.tpl-item { padding: 8px 10px; border-radius: 8px; cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.tpl-item.sel, .tpl-item:hover { background: var(--accent-2); }
.tpl-item .t { font-weight: 650; }
.tpl-item .x { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tpl-item img { height: 36px; border-radius: 5px; }

/* ---------- queue ---------- */
.queue-col { background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.queue-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 4px 14px; }
.queue-head h3 { margin: 0; font-size: 15px; }
.queue-stats { display: flex; gap: 6px; flex-wrap: wrap; padding: 4px 14px 10px; border-bottom: 1px solid var(--line); font-size: 12px; }
.queue-stats span { background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px; padding: 2px 9px; }
.queue-body { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 10px; }
.acc-card { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel-2); overflow: hidden; }
.acc-card.paused { border-color: var(--danger); }
.acc-top { display: flex; align-items: center; gap: 8px; padding: 9px 10px; }
.acc-top .avatar { width: 28px; height: 28px; font-size: 12px; }
.acc-top .login { font-weight: 700; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.acc-top .next { font-size: 12px; color: var(--muted); white-space: nowrap; }
.acc-top .next b { color: var(--text); font-variant-numeric: tabular-nums; }
.progress { height: 3px; background: var(--line); }
.progress i { display: block; height: 100%; background: var(--accent); transition: width 1s linear; }
.pause-reason { background: var(--danger-bg); color: var(--danger); font-size: 12px; padding: 6px 10px; }
.q-item { padding: 7px 10px; border-top: 1px solid var(--line); font-size: 12.5px; cursor: pointer; display: grid; grid-template-columns: 22px minmax(0, 1fr) auto; gap: 6px; }
.q-item:hover { background: var(--panel); }
.q-item .pos { color: var(--muted); font-weight: 700; }
.q-item .who { font-weight: 650; }
.q-item .txt { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.q-item .eta { font-variant-numeric: tabular-nums; color: var(--warn); font-weight: 700; white-space: nowrap; }
.q-item.sending .eta { color: var(--accent); }
.q-item .urg { color: var(--danger); }
.recent { margin-top: 6px; }
.recent h4 { margin: 6px 4px; font-size: 12.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.recent .q-item { border: 0; border-radius: 8px; }
.ok { color: var(--ok); }
.bad { color: var(--danger); }

/* ---------- modal, toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(10, 12, 20, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--panel); border-radius: 16px; box-shadow: var(--shadow); width: 100%; max-width: 640px; max-height: 88vh; overflow-y: auto; padding: 22px; }
.modal-card h2 { margin: 0 0 12px; font-size: 18px; }
.modal-card .row { display: flex; gap: 10px; align-items: center; margin: 8px 0; flex-wrap: wrap; }
.modal-card label.field { display: flex; flex-direction: column; gap: 5px; font-weight: 600; font-size: 13px; margin: 10px 0; }
.modal-card textarea { width: 100%; min-height: 90px; resize: vertical; }
.modal-card table { width: 100%; border-collapse: collapse; font-size: 13px; }
.modal-card td, .modal-card th { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: left; }
.modal-card td input[type=number] { width: 80px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.tpl-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.tpl-row .x { color: var(--muted); font-size: 13px; white-space: pre-wrap; max-height: 60px; overflow: hidden; }
.hint { font-size: 12.5px; color: var(--muted); }
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; max-width: min(380px, calc(100vw - 32px)); }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); font-size: 13.5px; cursor: pointer; }
.toast.err { background: var(--danger); color: #fff; }

/* ---------- responsive ---------- */
.back-btn { display: none; }
@media (max-width: 1280px) {
  .layout, .layout.no-queue { grid-template-columns: 320px minmax(0, 1fr); }
  .queue-col { position: fixed; right: 0; top: 54px; bottom: 0; width: min(360px, 100vw); z-index: 20; box-shadow: var(--shadow); }
}
@media (max-width: 900px) {
  .layout, .layout.no-queue { grid-template-columns: 280px minmax(0, 1fr); }
  .chat-head select { max-width: 140px; }
}
@media (max-width: 720px) {
  .layout, .layout.no-queue { grid-template-columns: minmax(0, 1fr); }
  .layout.chat-open .leads-col { display: none; }
  .layout:not(.chat-open) .chat-col { display: none; }
  .back-btn { display: inline-flex; }
  .topbar .me, .topbar .sync, #logoutBtn { display: none; }
  .topbar { gap: 4px; padding: 0 8px; }
  .topbar .brand { font-size: 0; gap: 0; }
  .chip-btn { padding: 5px 9px; }
  .urgent { display: none; }
  .msg { max-width: 88%; }
}
