:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f8f9fb;
  --line: #e3e6eb;
  --text: #1b1f24;
  --muted: #6b7380;
  --accent: #3b6ef5;
  --accent-text: #ffffff;
  --hover: #eef1f6;
  --active: #e3eafe;
  --bubble-in: #ffffff;
  --bubble-out: #dfe8ff;
  --danger: #d23b3b;
  --ok: #1f9d55;
  --warn: #d68a00;
  --tg: #2aa3e0;
  --max: #7a4cf0;
  --shadow: 0 8px 30px rgba(15, 20, 30, .14);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --panel: #181b20;
    --panel-2: #1d2026;
    --line: #2a2e36;
    --text: #e6e8eb;
    --muted: #8b929c;
    --accent: #5b86ff;
    --hover: #22262d;
    --active: #24304d;
    --bubble-in: #22262d;
    --bubble-out: #2a3a66;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }

.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 8px 14px; cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; display: block; text-align: center; text-decoration: none; box-sizing: border-box; }
.btn:disabled { opacity: .6; cursor: default; }
.icon-btn {
  border: 0; background: none; width: 32px; height: 32px; border-radius: 8px;
  cursor: pointer; font-size: 16px; color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }

/* --- вход --- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px; display: grid; gap: 14px;
}
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 16px;
}
.auth-card p { margin: -6px 0 0; }
.tabs { display: flex; gap: 4px; background: var(--panel-2); border-radius: 9px; padding: 3px; }
.tab { flex: 1; border: 0; background: none; padding: 7px; border-radius: 7px; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.field { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.field input, .field select {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 9px 11px; font-size: 14px;
}
.field input:focus, .search input:focus, .composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-error { color: var(--danger); font-size: 13px; min-height: 0; }
.form-error:empty { display: none; }

/* --- раскладка приложения --- */
.app { height: 100%; display: grid; grid-template-columns: 68px 340px 1fr; }

.rail {
  background: var(--panel-2); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 0; overflow-y: auto;
}
.rail-accounts { display: contents; }
.rail-spacer { flex: 1; }
.rail-item {
  position: relative; width: 46px; height: 46px; border-radius: 12px; border: 2px solid transparent;
  background: var(--panel); cursor: pointer; display: grid; place-items: center; padding: 0; flex-shrink: 0;
}
.rail-item:hover { background: var(--hover); }
.rail-item.active { border-color: var(--accent); }
.rail-all { font-size: 12px; font-weight: 600; }
.rail-add { font-size: 22px; color: var(--muted); border: 2px dashed var(--line); background: none; }
.rail-user { border-radius: 50%; font-weight: 600; font-size: 13px; }
.rail-item .initials { font-weight: 600; font-size: 14px; }
.rail-item .kind-badge { position: absolute; left: -3px; bottom: -3px; }
.rail-item .status-dot { position: absolute; right: -2px; top: -2px; }
.rail-item .count {
  position: absolute; right: -6px; bottom: -4px; background: var(--accent); color: #fff;
  font-size: 10px; border-radius: 9px; padding: 0 5px; min-width: 18px; line-height: 16px; text-align: center;
}

.kind-badge {
  width: 18px; height: 18px; border-radius: 50%; color: #fff; font-size: 9px; font-weight: 700;
  display: inline-grid; place-items: center; border: 2px solid var(--panel-2);
}
.kind-badge.tg { background: var(--tg); }
.kind-badge.max { background: var(--max); }
.status-dot { width: 11px; height: 11px; border-radius: 50%; border: 2px solid var(--panel-2); background: var(--muted); display: inline-block; }
.status-dot.online { background: var(--ok); }
.status-dot.pending, .status-dot.password, .status-dot.connecting { background: var(--warn); }
.status-dot.offline, .status-dot.expired { background: var(--danger); }

.list-pane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.pane-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 6px 16px; }
.pane-title { font-weight: 600; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search { padding: 4px 12px 8px; }
.search input { width: 100%; border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 8px 10px; }

.banner {
  margin: 0 12px 8px; padding: 10px 12px; border-radius: 9px; font-size: 13px;
  background: var(--panel-2); border: 1px solid var(--line); display: flex; gap: 8px; align-items: center; justify-content: space-between;
}
.banner.bad { border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }

.chat-list { list-style: none; margin: 0; padding: 0 6px 12px; overflow-y: auto; flex: 1; }
.chat-item {
  display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center;
  padding: 8px 10px; border-radius: 10px; cursor: pointer;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); }
.chat-main { min-width: 0; }
.chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-last { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-side { display: grid; justify-items: end; gap: 4px; font-size: 12px; color: var(--muted); }
.unread { background: var(--accent); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; line-height: 19px; min-width: 20px; text-align: center; }

.avatar {
  position: relative; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 600; color: #fff; flex-shrink: 0; font-size: 15px;
}
.avatar .kind-badge { position: absolute; right: -2px; bottom: -2px; width: 17px; height: 17px; border-color: var(--panel); }

.empty { color: var(--muted); text-align: center; padding: 32px 20px; }

/* --- переписка --- */
.conv-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.conv-empty { margin: auto; }
.conv-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.conv-head .avatar { width: 38px; height: 38px; font-size: 14px; }
.conv-meta { min-width: 0; }
.conv-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-sub { font-size: 12px; color: var(--muted); }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 4px; }
.load-more { align-self: center; margin-bottom: 8px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border-radius: 10px; padding: 2px 10px; margin: 10px 0 6px; }
.msg {
  max-width: min(560px, 80%); padding: 7px 11px 5px; border-radius: 12px; background: var(--bubble-in);
  align-self: flex-start; box-shadow: 0 1px 1px rgba(0,0,0,.06); white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.out { align-self: flex-end; background: var(--bubble-out); }
.msg .who { font-size: 12px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.msg .media { font-size: 13px; color: var(--muted); font-style: italic; }
.msg .time { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.msg.sending { opacity: .6; }
.msg.failed { outline: 1px solid var(--danger); }
.notice { align-self: center; font-size: 12px; color: var(--warn); padding: 6px; }

.composer { display: flex; gap: 8px; padding: 10px 16px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); background: var(--panel-2); border-radius: 10px;
  padding: 9px 12px; max-height: 160px; line-height: 1.4;
}

/* --- модальные окна --- */
.modal { border: 0; padding: 0; border-radius: 14px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); width: min(440px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(10, 12, 16, .45); }
.modal-body { padding: 22px; display: grid; gap: 14px; }
.modal h2 { margin: 0; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choice button {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 12px; padding: 18px 10px; cursor: pointer;
  display: grid; justify-items: center; gap: 8px; font-weight: 600;
}
.choice button:hover:not(:disabled) { border-color: var(--accent); }
.choice button:disabled { opacity: .5; cursor: default; }
.choice .kind-badge { width: 40px; height: 40px; font-size: 14px; border: 0; }
.choice small { font-weight: 400; color: var(--muted); }
.qr { background: #fff; border-radius: 12px; padding: 12px; width: 260px; height: 260px; margin: 0 auto; display: grid; place-items: center; }
.qr svg { width: 100%; height: 100%; }
.steps { margin: 0; padding-left: 20px; color: var(--muted); font-size: 13px; display: grid; gap: 4px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.note { font-size: 12px; color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); max-width: calc(100vw - 32px); z-index: 10;
}

/* --- телефон: одна колонка, переключение список ↔ переписка --- */
@media (max-width: 760px) {
  .app { grid-template-columns: 60px 1fr; }
  .rail-item { width: 42px; height: 42px; }
  .conv-pane { display: none; }
  .app.in-chat .list-pane { display: none; }
  .app.in-chat .conv-pane { display: flex; }
  .back { display: inline-block; }
  .messages { padding: 12px; }
  .msg { max-width: 88%; }
  .composer { padding: 8px; }
  .composer .btn { padding: 8px 10px; }
}
