/* ==========================================================================
   Чат ЦБС — тёмная тема веб-клиента организационного чата.
   Разметка: web/index.html (id сохранены), логика: web/app.js.
   ========================================================================== */

:root {
  /* Палитра в духе Telegram (тёмная тема) */
  --bg: #0e1621;              /* фон ленты сообщений */
  --bg-panel: #17212b;        /* сайдбар, шапка чата, панель ввода */
  --bg-elevated: #182533;     /* чужие пузыри, ховер */
  --bg-elevated-2: #242f3d;   /* поля ввода, чипы, ховер по спискам */
  --bg-input: #242f3d;
  --border: #1c2733;
  --border-strong: #2a3746;
  --text: #eef3f8;
  --text-muted: #7f91a8;
  --text-dim: #64788c;
  --accent: #5288c1;          /* фирменный синий Telegram (dark) */
  --accent-hover: #6aa3d8;
  --accent-soft: rgba(82, 136, 193, 0.18);
  --accent-text: #6ab3f3;     /* имена отправителей в ленте */
  --bubble-mine: #2b5278;     /* свои сообщения */
  --bubble-other: #182533;    /* чужие сообщения */
  --badge-unread: #4fae4e;    /* бейдж непрочитанных */
  --danger: #e5604f;
  --danger-soft: rgba(229, 96, 79, 0.14);
  --ok: #4fae4e;
  --warn: #e0a83f;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --font-scale: 1;            /* коэффициент размера текста (база 14px) */
  --font: "Segoe UI", "Inter", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: calc(14px * var(--font-scale));
  line-height: 1.45;
}

.is-hidden { display: none !important; }
.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip: rect(0 0 0 0);
  overflow: hidden;
}
.svg-sprite { display: none; }

.icon { width: 20px; height: 20px; flex: none; }
.icon--xs { width: 14px; height: 14px; }

/* ── Кнопки и поля ─────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--bg-elevated-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover { background: var(--border-strong); }
.btn:disabled { opacity: 0.5; cursor: default; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }

.btn-quiet { background: transparent; border-color: var(--border); color: var(--text-muted); }
.btn-quiet:hover { color: var(--text); background: var(--bg-elevated); }

.btn-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.btn-danger:hover { background: rgba(229, 96, 79, 0.25); }
.btn-danger:disabled { opacity: 0.4; }

.btn-block { width: 100%; }
.auth-download { display: block; margin-top: 14px; text-align: center; font-size: 13px; color: var(--text-muted); text-decoration: none; }
.auth-download:hover { color: var(--accent); text-decoration: underline; }
.btn-sm { padding: 5px 10px; font-size: calc(12.5px * var(--font-scale)); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--bg-elevated-2); color: var(--text); }
.icon-btn.is-off { opacity: 0.35; }
.icon-btn--xs { width: 26px; height: 26px; border-radius: 6px; }
.icon-btn--text {
  font-size: calc(14px * var(--font-scale));
  font-weight: 700;
  letter-spacing: 0.02em;
}
.icon-btn--xs .icon { width: 15px; height: 15px; }

.field { display: block; margin-bottom: 14px; }
.field-label {
  display: block;
  margin-bottom: 6px;
  font-size: calc(12.5px * var(--font-scale));
  font-weight: 600;
  color: var(--text-muted);
}
.field-hint { display: block; margin-top: 5px; font-size: calc(11.5px * var(--font-scale)); color: var(--text-dim); }

input[type="text"], input[type="password"], textarea, select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text);
  font: inherit;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { resize: none; }

.form-error {
  min-height: 18px;
  margin: 4px 0 10px;
  font-size: calc(12.5px * var(--font-scale));
  color: var(--danger);
}

/* ── Экран авторизации ─────────────────────────────────────────────────────── */
.auth {
  display: grid;
  grid-template-columns: 1.15fr 1fr;
  min-height: 100vh;
}

.auth-identity {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 28px;
  padding: 64px;
  background:
    radial-gradient(60% 50% at 20% 20%, rgba(77, 141, 246, 0.16), transparent 60%),
    radial-gradient(50% 40% at 80% 80%, rgba(63, 191, 127, 0.1), transparent 60%),
    var(--bg);
}

.identity-mark { display: flex; align-items: center; gap: 12px; }
.mark-glyph {
  display: grid;
  place-items: center;
  width: 44px; height: 44px;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: calc(24px * var(--font-scale));
  font-weight: 800;
}
.mark-text { font-size: calc(20px * var(--font-scale)); font-weight: 700; letter-spacing: 0.2px; }

.identity-copy h1 {
  margin: 0 0 14px;
  font-size: calc(34px * var(--font-scale));
  line-height: 1.2;
  font-weight: 800;
}
.identity-copy p {
  margin: 0;
  max-width: 46ch;
  color: var(--text-muted);
  font-size: calc(15px * var(--font-scale));
}

.identity-routes { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; max-width: 380px; }
.route {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: rgba(22, 32, 47, 0.6);
  color: var(--text-muted);
}
.route.is-active { border-color: var(--accent); color: var(--text); background: var(--accent-soft); }
.route-hash { color: var(--accent); font-weight: 700; }
.route-name { font-weight: 600; }
.route-note { margin-left: auto; font-size: calc(12px * var(--font-scale)); color: var(--text-dim); }

.identity-foot { margin: 0; font-size: calc(12.5px * var(--font-scale)); color: var(--text-dim); }
.apk-link { color: #a5b4fc; text-decoration: none; font-weight: 600; }
.apk-link:hover { text-decoration: underline; color: #c7d2fe; }

.auth-panel {
  display: grid;
  place-items: center;
  padding: 40px;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  padding: 32px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
}

.auth-title { margin: 0 0 6px; font-size: calc(20px * var(--font-scale)); font-weight: 700; }
.auth-sub { margin: 0 0 20px; font-size: calc(13px * var(--font-scale)); color: var(--text-muted); }

/* ── Рабочий экран: сетка ───────────────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w, 280px) 1fr 280px;
  height: 100vh;
  position: relative;
}
.app.right-open { grid-template-columns: var(--sidebar-w, 280px) 1fr 280px; }
.app:not(.right-open) { grid-template-columns: var(--sidebar-w, 280px) 1fr 0; }
.app:not(.right-open) .right-panel { display: none; }

/* Полоса-разделитель: тянем мышью, меняем ширину левой панели (как в Telegram) */
.sidebar-resizer {
  position: absolute;
  top: 0; bottom: 0;
  left: calc(var(--sidebar-w, 280px) - 3px);
  width: 6px;
  cursor: col-resize;
  background: transparent;
  z-index: 5;
}
.app.sidebar-collapsed .sidebar-resizer { left: 61px; }
.sidebar-resizer:hover, .sidebar-resizer.dragging { background: var(--accent-soft); }

/* ── Сайдбар ───────────────────────────────────────────────────────────────── */
.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: calc(17px * var(--font-scale));
}
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { font-size: calc(14.5px * var(--font-scale)); }
.brand-text span {
  font-size: calc(11.5px * var(--font-scale));
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar-toggle { margin-left: auto; }

.presence {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 10px 16px;
  font-size: calc(12.5px * var(--font-scale));
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}
.presence-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 6px var(--ok);
}
.presence-label { color: var(--text-dim); }
#onlineCount { font-weight: 700; color: var(--text); }

.side-scroll { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px 10px; }

.side-block { margin-bottom: 18px; }
.side-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 8px;
}
.side-head h3 {
  margin: 0;
  font-size: calc(11.5px * var(--font-scale));
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}
.side-head .icon-btn { margin-left: auto; width: 28px; height: 28px; }
.side-head .icon-btn .icon { width: 16px; height: 16px; }
.side-head .btn { margin-left: auto; padding: 4px 9px; font-size: calc(12px * var(--font-scale)); }
.side-head .btn .icon { width: 14px; height: 14px; }
.side-hint {
  margin-left: auto;
  font-size: calc(10.5px * var(--font-scale));
  color: var(--text-dim);
  opacity: 0.75;
}
.side-head .count-chip { margin-left: 0; }

.count-chip {
  min-width: 21px;
  height: 21px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--badge-unread);
  color: #fff;
  font-size: calc(11.5px * var(--font-scale));
  font-weight: 700;
  text-align: center;
  line-height: 21px;
}

/* Списки: одна колонка, ширина ограничена контейнером — горизонтального
   ползунка быть не должно, длинные названия обрезаются многоточием */
.channel-list, .people-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 2px;
}

/* Поиск по чатам */
.chat-search {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 2px 8px;
  padding: 8px 14px;
  border: 0;
  border-radius: 999px;
  background: var(--bg-elevated-2);
  color: var(--text-dim);
}
.chat-search:focus-within { box-shadow: 0 0 0 2px var(--accent); color: var(--text-muted); }
.chat-search input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: calc(13px * var(--font-scale));
  outline: none;
}
.chat-search input::placeholder { color: var(--text-dim); }
.side-empty {
  margin: 6px 10px;
  font-size: calc(12.5px * var(--font-scale));
  color: var(--text-dim);
}

/* Карточка чата — как список бесед в Telegram */
.channel-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
}
.channel-item:hover { background: var(--bg-elevated-2); color: var(--text); }
.channel-item.is-active { background: var(--accent); color: #fff; }
.channel-item.is-active .channel-sub,
.channel-item.is-active .count-chip { color: rgba(255, 255, 255, 0.75); }
.channel-item.is-active .channel-del { color: rgba(255, 255, 255, 0.8); }
.channel-item.is-hidden { display: none; }
.channel-del {
  margin-left: auto;
  flex: none;
  opacity: 0;
  color: var(--text-dim);
  transition: opacity 0.12s, color 0.12s, background 0.12s;
}
.channel-item:hover .channel-del,
.channel-del:focus-visible { opacity: 1; }
.channel-del:hover { color: var(--danger); background: rgba(229, 96, 79, 0.14); }
.channel-item .count-chip + .channel-del { margin-left: 6px; }
.channel-item .count-chip:not(:last-of-type) { margin-left: auto; }
.channel-avatar {
  display: grid;
  place-items: center;
  flex: none;
  width: 48px; height: 48px;
  border-radius: 50%;
  background: var(--channel-accent, var(--accent));
  color: #fff;
  font-weight: 800;
  font-size: calc(19px * var(--font-scale));
}
.channel-avatar--lock { background: var(--bg-elevated-2); color: var(--text-muted); }
.channel-avatar .icon { width: 16px; height: 16px; }
.channel-main {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  gap: 1px;
}
.channel-name {
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 600;
  font-size: calc(14.5px * var(--font-scale));
  color: var(--text);
}
.channel-sub {
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: calc(12px * var(--font-scale));
  color: var(--text-dim);
}
.channel-hash { color: var(--accent); font-weight: 700; }

.person {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
}
.person:hover { background: var(--bg-elevated); }
.person--clickable { cursor: pointer; }
.person--clickable:hover { background: var(--accent-soft); }
.person--clickable .person-text strong::after {
  content: '✎';
  margin-left: 6px;
  font-size: calc(11px * var(--font-scale));
  opacity: 0.5;
}

.avatar {
  display: grid;
  place-items: center;
  flex: none;
  width: 32px; height: 32px;
  border-radius: 50%;
  color: #fff;
  font-size: calc(14px * var(--font-scale));
  font-weight: 700;
  user-select: none;
}
.avatar--lg { width: 40px; height: 40px; font-size: calc(17px * var(--font-scale)); }

.person-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.person-text strong {
  font-size: calc(13.5px * var(--font-scale));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.person-sub { font-size: calc(11.5px * var(--font-scale)); color: var(--text-dim); }

.person-right { display: flex; align-items: center; gap: 6px; }
.online-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 5px var(--ok);
}

.role-badge {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-elevated-2);
  color: var(--text-muted);
  font-size: calc(10.5px * var(--font-scale));
  font-weight: 700;
}
.role-badge--owner { background: rgba(224, 168, 63, 0.16); color: var(--warn); }
.role-badge--admin { background: var(--accent-soft); color: var(--accent-hover); }
.role-badge--guest { background: rgba(93, 111, 136, 0.25); color: var(--text-dim); }

.profile {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.profile-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.profile-text strong {
  font-size: calc(13.5px * var(--font-scale));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Центр: чат ─────────────────────────────────────────────────────────────── */
.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  position: relative;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}
.chat-head-main { min-width: 0; flex: 1; }
.chat-head-main h1 {
  margin: 0;
  font-size: calc(16.5px * var(--font-scale));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-head-main p {
  margin: 2px 0 0;
  font-size: calc(12.5px * var(--font-scale));
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-head-actions { display: flex; align-items: center; gap: 10px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: calc(11.5px * var(--font-scale));
  font-weight: 700;
}
.badge--private { background: var(--danger-soft); color: var(--danger); }
.badge--private .icon { width: 13px; height: 13px; }

.conn { font-size: calc(12px * var(--font-scale)); font-weight: 600; }
.conn--on { color: var(--ok); }
.conn--off { color: var(--danger); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  /* лёгкий «обойный» узор как в Telegram */
  background-color: var(--bg);
  background-image: radial-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1.5px);
  background-size: 24px 24px;
}

.message {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 72%;
}
.message-body { min-width: 0; }
.message-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.message-head strong { font-size: calc(13px * var(--font-scale)); color: var(--accent-text); }
.message-time { font-size: calc(11px * var(--font-scale)); color: var(--text-dim); }

/* «Пузырь» сообщения: скруглённый, с «хвостиком» у аватара */
.message-bubble {
  display: flex;
  flex-direction: column;
  padding: 8px 12px 6px;
  border-radius: 14px 14px 14px 4px;
  background: var(--bubble-other);
  overflow-wrap: break-word;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);
}
.message-text {
  overflow-wrap: break-word;
  white-space: pre-wrap;
}
.message-text a { color: var(--accent-hover); }
.message-time--in-bubble {
  margin-top: 2px;
  text-align: right;
}

/* Свои сообщения — пузырь справа, как в мобильном приложении */
.message--mine {
  align-self: flex-end;
  flex-direction: row-reverse;
}
.message--mine .message-bubble {
  color: #fff;
  background: var(--bubble-mine);
  border-radius: 14px 14px 4px 14px;
}
.message--mine .message-time { color: rgba(255, 255, 255, 0.6); }
.message--mine .message-text a { color: #cfe3ff; text-decoration: underline; }
.message--mine .message-actions { justify-content: flex-end; }
.message--mine .inline-edit {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.45);
  color: #fff;
}

.message-actions {
  display: none;
  gap: 2px;
  margin-top: 3px;
}
.message:hover .message-actions { display: flex; }

.message-file {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--accent);
}
.message-bubble .message-file {
  padding: 0;
  border: none;
  background: transparent;
}
.message-file .icon { color: var(--accent); }
.message-file-info { display: flex; flex-direction: column; min-width: 0; }
.message-file-name { color: var(--text); font-weight: 600; text-decoration: none; }
.message-file-name:hover { color: var(--accent-hover); text-decoration: underline; }
.message-file-size { font-size: calc(11.5px * var(--font-scale)); color: var(--text-dim); }

.message--mine .message-file { color: #fff; }
.message--mine .message-file .icon { color: #fff; }
.message--mine .message-file-name { color: #fff; }
.message--mine .message-file-name:hover { color: #fff; text-decoration: underline; }
.message--mine .message-file-size { color: rgba(255, 255, 255, 0.72); }

.inline-edit {
  width: 100%;
  margin-top: 4px;
  padding: 7px 10px;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text);
  font: inherit;
  outline: none;
}

.typing {
  margin: 0;
  padding: 4px 20px 6px;
  font-size: calc(12px * var(--font-scale));
  color: var(--text-dim);
  min-height: 24px;
}

/* ── Настройка размера текста ────────────────────────────────────────────── */
.font-scale-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 6px 0 12px;
}
.font-scale-step {
  width: 42px;
  font-size: calc(18px * var(--font-scale));
  line-height: 1;
  padding: 8px 0;
}
.font-scale-input {
  width: 92px;
  text-align: center;
  font-size: calc(18px * var(--font-scale));
  padding: 8px 10px;
}
.font-scale-unit {
  font-size: calc(14px * var(--font-scale));
  color: var(--text-dim);
}
.font-scale-preview {
  margin: 0 0 4px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  font-size: calc(14px * var(--font-scale));
  color: var(--text);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
}
.composer textarea {
  flex: 1;
  min-height: 42px;
  max-height: 160px;
  resize: none;
  padding: 11px 16px;
  border: 0;
  border-radius: 21px;
  background: var(--bg-input);
}
.composer textarea:focus { box-shadow: 0 0 0 2px var(--accent); }
.composer-attach { align-self: center; width: 40px; height: 40px; border-radius: 50%; }
.composer-attach:hover { background: var(--bg-elevated-2); }
.btn-send {
  align-self: center;
  width: 42px; height: 42px;
  padding: 0;
  border-radius: 50%;
  background: var(--accent);
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send .icon { width: 19px; height: 19px; margin-right: -2px; }

.drop-hint {
  position: absolute;
  inset: 12px;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  background: rgba(11, 17, 28, 0.85);
  color: var(--accent-hover);
  font-size: calc(16px * var(--font-scale));
  font-weight: 700;
  pointer-events: none;
  z-index: 5;
}

/* ── Правая панель ─────────────────────────────────────────────────────────── */
.right-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
}
.right-head { padding: 14px 16px 10px; border-bottom: 1px solid var(--border); }
.right-head h3 { margin: 0; font-size: calc(13px * var(--font-scale)); font-weight: 700; }
.right-head .icon-btn { margin-left: auto; width: 28px; height: 28px; }
.right-scroll { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px 10px; }

.add-member {
  margin-top: 14px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
}
.add-member h4 { margin: 0 0 10px; font-size: calc(12.5px * var(--font-scale)); color: var(--text-muted); }
.add-member select { margin-bottom: 8px; }
.add-member-row { display: flex; gap: 8px; }
.add-member-row select { flex: 1; margin-bottom: 0; }

/* ── Модалки ───────────────────────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 8, 14, 0.7);
  backdrop-filter: blur(3px);
}
.modal-card {
  position: relative;
  width: min(440px, calc(100vw - 40px));
  max-height: calc(100vh - 60px);
  overflow-y: auto;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-panel);
  box-shadow: var(--shadow);
}
.modal-card--wide { width: min(620px, calc(100vw - 40px)); }

.modal-head {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: calc(16px * var(--font-scale)); font-weight: 700; }
.modal-head .icon-btn { margin-left: auto; }

.modal-body { padding: 20px; }
.modal-body--center { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.modal-note { margin: 0 0 14px; font-size: calc(12.5px * var(--font-scale)); color: var(--text-muted); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

.switch-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  cursor: pointer;
  margin-bottom: 10px;
}
.switch-row input { width: auto; margin-top: 2px; accent-color: var(--accent); }
.switch-row strong { display: block; font-size: calc(13.5px * var(--font-scale)); }
.switch-row em { display: block; font-style: normal; font-size: calc(12px * var(--font-scale)); color: var(--text-dim); }

.manage-list { display: grid; gap: 8px; }
.manage-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
}
.manage-info { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.manage-info span { font-size: calc(11.5px * var(--font-scale)); color: var(--text-dim); }
.role-select { width: auto; padding: 6px 8px; font-size: calc(12.5px * var(--font-scale)); }

.file-preview { max-width: 100%; max-height: 55vh; border-radius: var(--radius-sm); }

/* ── Тосты ─────────────────────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 100;
  display: grid;
  gap: 8px;
  max-width: 340px;
}
.toast {
  padding: 11px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated-2);
  color: var(--text);
  font-size: calc(13px * var(--font-scale));
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.25s, transform 0.25s;
}
.toast--show { opacity: 1; transform: translateY(0); }
.toast--success { border-color: rgba(63, 191, 127, 0.5); }
.toast--error { border-color: rgba(229, 96, 79, 0.5); }

/* ── Сворачивание paysage ──────────────────────────────────────────────────── */
.app.sidebar-collapsed { grid-template-columns: 64px 1fr 0; }
.app.sidebar-collapsed:not(.right-open) .right-panel { display: none; }
.app.sidebar-collapsed.right-open { grid-template-columns: 64px 1fr 280px; }
.app.sidebar-collapsed .sidebar { overflow: hidden; }
.app.sidebar-collapsed .brand-text,
.app.sidebar-collapsed .presence-label,
.app.sidebar-collapsed .side-head h3,
.app.sidebar-collapsed .side-head .count-chip,
.app.sidebar-collapsed .side-head .btn span,
.app.sidebar-collapsed .channel-name,
.app.sidebar-collapsed .channel-item .count-chip,
.app.sidebar-collapsed .channel-item .icon--xs,
.app.sidebar-collapsed .channel-item .channel-del,
.app.sidebar-collapsed .person-text,
.app.sidebar-collapsed .person-right,
.app.sidebar-collapsed .profile-text,
.app.sidebar-collapsed #onlineCount { display: none; }
.app.sidebar-collapsed .channel-item,
.app.sidebar-collapsed .person { justify-content: center; padding: 8px 4px; }
.app.sidebar-collapsed .side-head { justify-content: center; }
.app.sidebar-collapsed .side-head .icon-btn,
.app.sidebar-collapsed .side-head .btn { margin-left: 0; }
.app.sidebar-collapsed .brand { justify-content: center; padding: 14px 6px; }
.app.sidebar-collapsed .presence { justify-content: center; padding: 10px 6px; }
.app.sidebar-collapsed .profile { justify-content: center; padding: 12px 6px; }

/* ── Адаптив ────────────────────────────────────────────────────────────────── */
@media (max-width: 1080px) {
  .app, .app.right-open { grid-template-columns: var(--sidebar-w, 240px) 1fr 240px; }
  .app:not(.right-open) { grid-template-columns: var(--sidebar-w, 240px) 1fr 0; }
}

/* Кнопка «назад к списку чатов» — только на узких экранах */
.chat-back { display: none; flex: none; }

@media (max-width: 860px) {
  .auth { grid-template-columns: 1fr; }
  .auth-identity { display: none; }
  .auth-panel { border-left: 0; }
  .app, .app.right-open { grid-template-columns: 1fr; height: 100dvh; }
  .app { grid-template-rows: 1fr; }

  /* Список бесед — отдельный экран (как в Telegram), чат открывается поверх */
  .sidebar { display: flex; width: 100%; height: 100%; border-right: 0; }
  body.chat-open .sidebar { display: none; }
  body:not(.chat-open) .chat { display: none; }
  body.chat-open .chat-back { display: inline-flex; }

  .right-panel {
    position: fixed;
    top: 0; right: 0; bottom: 0;
    width: min(300px, 85vw);
    z-index: 40;
    box-shadow: var(--shadow);
  }
  .app:not(.right-open) .right-panel { display: none; }
  .message { max-width: 92%; }

  /* Шапка чата: компактная, без лишних отступов */
  .chat-head { padding: 10px 12px; gap: 10px; }
  .chat-head-actions { gap: 8px; }

  /* Поле ввода не сжимается и остаётся над клавиатурой */
  .composer { padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px)); gap: 8px; }
  .composer textarea { min-height: 44px; font-size: calc(16px * var(--font-scale)); padding: 11px 12px; }
  .composer-attach { width: 40px; height: 40px; }
  .btn-send { width: 40px; height: 40px; }

  /* Список чатов на всю ширину, карточки крупнее */
  .channel-item { padding: 10px 12px; }
  .channel-avatar { width: 44px; height: 44px; font-size: calc(18px * var(--font-scale)); }
  .channel-name { font-size: calc(15px * var(--font-scale)); }
  .channel-sub { font-size: calc(12.5px * var(--font-scale)); }
}

/* ── Эмодзи-пикер в поле ввода ─────────────────────────────────────────────── */
.composer-emoji { font-size: 18px; line-height: 1; }
.emoji-picker {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 12px;
  width: 330px;
  max-width: calc(100vw - 24px);
  max-height: 330px;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel, var(--bg-elevated));
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 60;
  overflow: hidden;
}
.emoji-picker input { margin: 8px; }
.emoji-grid { overflow-y: auto; padding: 0 8px 10px; }
.emoji-cat {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 10px 2px 4px;
}
.emoji-row { display: flex; flex-wrap: wrap; gap: 2px; }
.emoji-btn {
  font-size: 22px;
  line-height: 1;
  padding: 5px 6px;
  background: none;
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.emoji-btn:hover { background: var(--bg-elevated-2); }
.emoji-empty { padding: 14px; color: var(--text-dim); font-size: 13px; text-align: center; }

/* ── Реакции (эмодзи) на сообщения ─────────────────────────────────────────── */
.react-btn { font-size: 14px; }
.msg-reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction-chip {
  font-size: calc(12px * var(--font-scale));
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text);
  cursor: pointer;
}
.reaction-chip:hover { border-color: var(--accent); }
.reaction-chip--mine {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.reaction-picker {
  position: absolute;
  z-index: 70;
  display: flex;
  gap: 2px;
  background: var(--bg-panel, var(--bg-elevated));
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  padding: 4px 8px;
  box-shadow: var(--shadow);
}
.reaction-picker-btn {
  font-size: 20px;
  line-height: 1;
  background: none;
  border: 0;
  border-radius: 50%;
  padding: 4px 6px;
  cursor: pointer;
}
.reaction-picker-btn:hover { background: var(--bg-elevated-2); transform: scale(1.18); }

/* ── Профиль и карточка сотрудника ─────────────────────────────────────────── */
.field-row { display: flex; gap: 8px; }
.field-row .field { flex: 1; min-width: 0; }
.person-card-btn { flex-shrink: 0; }
.uc-head { display: flex; gap: 12px; align-items: center; margin-bottom: 10px; }
.uc-id { display: flex; flex-direction: column; gap: 3px; align-items: flex-start; min-width: 0; }
.uc-row {
  display: flex;
  gap: 10px;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
  font-size: calc(13.5px * var(--font-scale));
}
.uc-label { width: 90px; flex-shrink: 0; color: var(--text-dim); }
.uc-val { min-width: 0; word-break: break-word; }
.uc-val a { color: var(--accent-hover); }

/* ==========================================================================
   Telegram-редизайн: светлая тема, закрепы, поиск, папки, контекстное меню
   ========================================================================== */
:root[data-theme="light"] {
  --bg: #e7ebf0;
  --bg-panel: #ffffff;
  --bg-elevated: #ffffff;
  --bg-elevated-2: #f1f4f8;
  --bg-input: #f1f4f8;
  --border: #e2e8f0;
  --border-strong: #d4dce6;
  --text: #17212b;
  --text-muted: #708499;
  --text-dim: #93a5b8;
  --accent: #3390ec;
  --accent-hover: #2b7cd3;
  --accent-soft: rgba(51, 144, 236, 0.14);
  --accent-text: #3390ec;
  --bubble-mine: #eeffde;
  --bubble-other: #ffffff;
  --badge-unread: #4fae4e;
  --shadow: 0 12px 40px rgba(30, 50, 80, 0.18);
}
:root[data-theme="light"] .chat { background: var(--bg); }
:root[data-theme="light"] .message--mine .message-bubble { color: #17212b; }

/* Папки над списком чатов */
.folder-tabs {
  display: flex; gap: 4px; align-items: center;
  padding: 6px 10px; overflow-x: auto;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
}
.folder-tab {
  flex: none; border: 0; background: transparent; color: var(--text-muted);
  font: inherit; font-size: calc(13px * var(--font-scale));
  padding: 4px 10px; border-radius: 999px; cursor: pointer; white-space: nowrap;
}
.folder-tab:hover { background: var(--bg-elevated-2); color: var(--text); }
.folder-tab.is-active { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }
.sidebar-actions { display: flex; gap: 4px; margin-left: auto; flex: none; }
.fav-chip {
  font-size: calc(11px * var(--font-scale)); font-weight: 700;
  background: var(--accent); color: #fff; border-radius: 999px;
  min-width: 18px; height: 18px; line-height: 18px; text-align: center;
  padding: 0 5px;
}

/* Полоса закреплённых сообщений под шапкой */
.pinned-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 14px; cursor: pointer;
  background: var(--bg-panel); border-bottom: 1px solid var(--border);
  color: var(--text-muted); font-size: calc(13px * var(--font-scale));
}
.pinned-bar:hover { background: var(--bg-elevated-2); }
.pinned-bar .icon { width: 16px; height: 16px; color: var(--accent-text); }
.pinned-bar-title { font-weight: 600; color: var(--accent-text); white-space: nowrap; }
.pinned-bar-preview {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1;
}
.pinned-bar-count {
  background: var(--accent); color: #fff; border-radius: 999px;
  font-size: 11px; font-weight: 700; padding: 1px 7px;
}

/* Панель поиска по сообщениям */
.search-panel {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(360px, 90%);
  background: var(--bg-panel); border-left: 1px solid var(--border-strong);
  box-shadow: var(--shadow); z-index: 30;
  display: flex; flex-direction: column;
}
.search-head { display: flex; gap: 8px; padding: 10px; border-bottom: 1px solid var(--border); }
.search-head input {
  flex: 1; min-width: 0; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--bg-input); color: var(--text);
  font: inherit; font-size: calc(14px * var(--font-scale)); padding: 8px 10px; outline: none;
}
.search-head input:focus { border-color: var(--accent); }
.search-results { overflow-y: auto; padding: 8px; flex: 1; }
.search-item {
  display: block; width: 100%; text-align: left; border: 0; background: transparent;
  color: var(--text); font: inherit; padding: 8px 10px; border-radius: var(--radius-sm);
  cursor: pointer; margin-bottom: 2px;
}
.search-item:hover { background: var(--bg-elevated-2); }
.search-item-head { display: flex; justify-content: space-between; gap: 8px; }
.search-item-head strong { color: var(--accent-text); font-size: calc(13px * var(--font-scale)); }
.search-item-head span { color: var(--text-dim); font-size: calc(11.5px * var(--font-scale)); white-space: nowrap; }
.search-item-body {
  color: var(--text-muted); font-size: calc(13px * var(--font-scale));
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px;
}

/* Reply-bar над полем ввода */
.reply-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px 0; background: var(--bg-panel);
}
.reply-bar-inner {
  flex: 1; min-width: 0; border-left: 3px solid var(--accent);
  padding: 4px 10px; background: var(--bg-elevated-2); border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}
.reply-bar-title { font-weight: 600; color: var(--accent-text); font-size: calc(13px * var(--font-scale)); }
.reply-bar-preview {
  color: var(--text-muted); font-size: calc(12.5px * var(--font-scale));
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Контекстное меню сообщения */
.ctx-menu {
  position: fixed; z-index: 100; min-width: 200px;
  background: var(--bg-panel); border: 1px solid var(--border-strong);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 6px;
}
.ctx-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  border: 0; background: transparent; color: var(--text);
  font: inherit; font-size: calc(13.5px * var(--font-scale));
  padding: 8px 10px; border-radius: var(--radius-sm); cursor: pointer; text-align: left;
}
.ctx-item:hover { background: var(--bg-elevated-2); }
.ctx-item .icon { width: 17px; height: 17px; color: var(--text-muted); }
.ctx-item--danger { color: var(--danger); }
.ctx-item--danger .icon { color: var(--danger); }

/* Цитата и пересылка внутри пузыря */
.msg-quote {
  border-left: 3px solid var(--accent); padding: 4px 8px; margin-bottom: 6px;
  background: var(--accent-soft); border-radius: 0 6px 6px 0; cursor: pointer;
  display: flex; flex-direction: column; gap: 1px; max-width: 100%;
}
.msg-quote strong { color: var(--accent-text); font-size: calc(12.5px * var(--font-scale)); }
.msg-quote span {
  color: var(--text-muted); font-size: calc(12.5px * var(--font-scale));
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.msg-forwarded {
  color: var(--accent-text); font-size: calc(12.5px * var(--font-scale)); font-weight: 600;
  margin-bottom: 4px;
}

/* Состояния сообщений: закреплено / избранное / подсветка при переходе */
.message.is-pinned .message-bubble { outline: 1px solid var(--accent-soft); }
.message.is-favorite .message-bubble::after {
  content: "⭐"; font-size: 11px; margin-left: 6px; vertical-align: middle;
}
.message.msg-flash .message-bubble { animation: msgFlash 1.5s ease; }
@keyframes msgFlash {
  0%, 60% { background: var(--accent-soft); }
  100% { background: transparent; }
}

/* Модалки избранного/пересылки */
.modal-card--wide { width: min(560px, 94vw); max-height: 82vh; display: flex; flex-direction: column; }
.modal-card--wide .modal-body { overflow-y: auto; }
.fav-item { padding: 10px 4px; border-bottom: 1px solid var(--border); }
.fav-item .btn { margin-top: 6px; }
.forward-list { display: flex; flex-direction: column; gap: 2px; margin-top: 8px; }
.forward-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  border: 0; background: transparent; color: var(--text); font: inherit;
  padding: 9px 10px; border-radius: var(--radius-sm); cursor: pointer; text-align: left;
}
.forward-item:hover { background: var(--bg-elevated-2); }
.avatar--sm { width: 30px; height: 30px; border-radius: 50%; color: #fff; font-size: 13px; display: inline-flex; align-items: center; justify-content: center; flex: none; }

/* Онлайн-точка в списке сотрудников */
.user-row { position: relative; }

/* E2E-шифрование: отметка на сообщении */
.message.is-encrypted .message-bubble { position: relative; }
.message.is-encrypted .message-text { font-style: normal; }
