/* ==========================================================================
   Variables — фирменный стиль MSK-SB (перенесено без изменений из рабочей версии)
   ========================================================================== */
:root {
  --navy-950: #060b15;
  --navy-900: #0b1526;
  --navy-800: #0e1c33;
  --navy-700: #173463;
  --navy-600: #1f4586;
  --orange: #ff9900;
  --orange-light: #ffb347;
  --white: #ffffff;
  --gray-100: #f4f6fa;
  --gray-300: #cbd5e1;
  --gray-500: #64748b;
  --gray-700: #334155;
  --text-dark: #10192b;

  --font-main: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --radius-sm: 8px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --transition: .3s cubic-bezier(.4,0,.2,1);
  --shadow-md: 0 10px 30px rgba(6,11,21,.08);
  --shadow-lg: 0 20px 50px rgba(6,11,21,.14);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

/* ПРОБОВАЛИ (Этап 0): html,body{overflow:hidden} как страховка от белой полосы
   сверху при переключении .chat-shell--chat-open. ОТКАТИЛИ — на видео с
   реального iPhone эта правка совпала с появлением нового, более серьёзного
   глюка: пока открыта клавиатура, экран мигает между нормальным видом и
   сломанным (шапка чата пропадает, поле ввода прилипает под статус-бар,
   пустое место до клавиатуры). Похоже, запрет скролла страницы мешает
   собственному механизму iOS "поднять поле ввода над клавиатурой", и вместо
   обычной прокрутки WebKit сдвигает just visual viewport, теряя синхронизацию
   с версткой. Не повторять overflow:hidden на html/body в этом проекте без
   отдельной проверки именно на цикле открытия клавиатуры. */

body {
  font-family: var(--font-main);
  color: var(--text-dark);
  /* Тёмный фон вместо серого/белого — маскировка известного бага с высотой
     вьюпорта в iOS Safari standalone: если .chat-shell/.auth-page не дотягивают
     до края экрана, снизу видно ЭТОТ фон, а не контрастную полосу. Тёмный
     совпадает с сайдбаром списка чатов и фоном страницы входа. Когда на
     телефоне открыт сам чат (белый .chat-container), переопределяем на белый —
     см. правило ниже. Пять других механизмов (см. план переписывания) не
     убирают сам баг, только маскировка подтверждена работающей на устройстве. */
  background: var(--navy-950);
  line-height: 1.5;
}
body:has(.chat-shell--chat-open) {
  background: var(--white);
}

button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, textarea, select { font-family: inherit; font-size: 1rem; }

/* ==========================================================================
   Кнопки и лого
   ========================================================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 8px; padding: 11px 20px; border-radius: 999px;
  font-size: .95rem; font-weight: 600; transition: var(--transition);
}
.btn--primary { background: linear-gradient(135deg, var(--orange), var(--orange-light)); color: var(--white); }
.btn--primary:hover { filter: brightness(1.05); }
.btn--ghost { background: rgba(255,255,255,.08); color: var(--white); }
.btn--ghost:hover { background: rgba(255,255,255,.16); }
/* Диалоги — белые, а .btn--ghost задуман для тёмного сайдбара/шапки — белый
   текст на белом там был бы не виден. Переопределяем внутри диалогов. */
.chat-dialog .btn--ghost { background: var(--gray-100); color: var(--text-dark); }
.chat-dialog .btn--ghost:hover { background: #e9edf5; }
.btn--sm { padding: 7px 14px; font-size: .85rem; }
.btn--block { width: 100%; }
.btn--icon { padding: 0; width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0; font-size: 1rem; }

.logo { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--white); }
.logo__icon { display: inline-flex; }
.logo__text { font-weight: 700; font-size: 1.1rem; color: var(--white); }
.logo__text span { color: var(--orange); }

/* ==========================================================================
   Страница входа
   ========================================================================== */
.auth-page {
  min-height: 100vh;
  /* svh (не dvh) — статическое значение без "динамического" пересчёта,
     см. план переписывания: dvh на iOS Safari standalone иногда навсегда
     занижается на первой отрисовке и ничем программно не пересчитывается. */
  min-height: 100svh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
  background:
    radial-gradient(circle at 20% 20%, rgba(255,153,0,.12), transparent 40%),
    linear-gradient(160deg, var(--navy-950), var(--navy-800));
}
.vvh-enabled .auth-page { min-height: calc(var(--vvh, 1vh) * 100); }

.auth-container { display: flex; flex-direction: column; align-items: center; gap: 28px; width: 100%; max-width: 380px; }
.auth-card {
  width: 100%; background: var(--white); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg); padding: 32px 28px;
  display: flex; flex-direction: column; gap: 18px;
}
.auth-card h1 { font-size: 1.3rem; color: var(--text-dark); }
.auth-card__subtitle { font-size: .88rem; color: var(--gray-500); }
.auth-form { display: flex; flex-direction: column; gap: 16px; }
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: .8rem; color: var(--gray-700); font-weight: 600; }
.form-group input {
  padding: 11px 14px; border: 1px solid var(--gray-300); border-radius: var(--radius-sm);
  font-size: 16px; /* держим от 16px — иначе iOS Safari зумит страницу при фокусе */
  color: var(--text-dark);
}
.form-group input:focus { outline: 2px solid var(--orange-light); outline-offset: 1px; }
.auth-buttons { display: flex; }
.auth-buttons .btn { flex: 1; }
.error { color: #d64545; font-size: .85rem; min-height: 1.1em; }

.app-download { width: 100%; display: flex; flex-direction: column; gap: 10px; }
.app-download__item {
  display: flex; align-items: center; gap: 12px;
  width: 100%; padding: 12px 14px; border-radius: var(--radius-lg);
  background: rgba(255,255,255,.06); border: 1px solid rgba(255,255,255,.1);
  text-decoration: none; color: inherit;
}
a.app-download__item:hover { background: rgba(255,255,255,.1); }
.app-download__item--static { opacity: .75; }
.app-download__icon { font-size: 1.4rem; flex-shrink: 0; }
.app-download__text { display: flex; flex-direction: column; gap: 1px; }
.app-download__text strong { font-size: .88rem; color: var(--white); }
.app-download__text span { font-size: .78rem; color: var(--gray-300); }

/* ==========================================================================
   Каркас чата
   ========================================================================== */
.chat-shell {
  display: flex;
  max-width: 1100px;
  height: 100vh;
  /* svh (не dvh) — см. комментарий у .auth-page выше и план переписывания. */
  height: 100svh;
  margin: 0 auto;
  background: var(--white);
  box-shadow: var(--shadow-lg);
  /* НЕ добавлять border-radius/overflow:hidden сюда — уже пробовали на рабочей
     версии, скругление превращает ровный замаскированный зазор в кривую линию,
     из-за которой белая/тёмная полоса снова становится видна. */
}
/* Единственный по-настоящему новый механизм в этом переписывании: непрерывная
   синхронизация реальной высоты видимой области через window.visualViewport
   (см. js/chat-init.js, ENABLE_VVH_SYNC). Включается отдельным классом на
   <html>, чтобы можно было протестировать на телефоне отдельно от базовой
   маскировки (сначала без этого правила, потом с ним — см. план). Ничего не
   меняет, если JS его не активировал: класса нет — правило не применяется. */
.vvh-enabled .chat-shell { height: calc(var(--vvh, 1vh) * 100); }

.chat-sidebar {
  display: flex; flex-direction: column;
  width: 260px; flex-shrink: 0; min-height: 0;
  background: var(--navy-950);
}
.chat-sidebar__header {
  padding: 18px 20px;
  /* viewport-fit=cover отдаёт странице область под вырезом/статус-баром —
     без этого отступа она красится в дефолтный белый Safari. */
  padding-top: max(18px, env(safe-area-inset-top));
  border-bottom: 1px solid rgba(255,255,255,.1);
}
.chat-list { flex: 1; min-height: 0; overflow-y: auto; padding: 8px; }
.chat-list__loading { padding: 16px; color: var(--gray-300); font-size: .85rem; text-align: center; }
.chat-list__item {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  padding: 12px 14px; border-radius: var(--radius-sm);
  color: var(--white); font-size: .92rem;
  background: transparent; transition: var(--transition);
}
.chat-list__item:hover { background: rgba(255,255,255,.06); }
.chat-list__item.active { background: rgba(255,153,0,.16); }
.chat-list__text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.chat-list__name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-list__meta {
  font-size: .72rem; font-weight: 500; color: rgba(255,255,255,.45);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-list__item.active .chat-list__meta { color: rgba(255,255,255,.65); }
.chat-list__badge {
  flex-shrink: 0; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 999px; background: var(--orange); color: var(--navy-950);
  font-size: .68rem; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
}
.chat-list__badge[hidden] { display: none; }
.chat-list__mute-icon { flex-shrink: 0; display: flex; color: rgba(255,255,255,.4); font-size: .8rem; }
.chat-list__mute-icon[hidden] { display: none; }
.chat-list__item.muted .chat-list__name { opacity: .6; }
.chat-sidebar__account {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 12px 14px;
  border-top: 1px solid rgba(255,255,255,.1);
}
.chat-sidebar__username-btn {
  display: flex; align-items: center; gap: 6px; width: 100%; min-width: 0;
  color: var(--white); padding: 4px; border-radius: var(--radius-sm);
}
.chat-sidebar__username-btn:hover { background: rgba(255,255,255,.08); }
.chat-sidebar__username-btn svg { flex-shrink: 0; opacity: .6; }
/* Без flex:1: раньше имя растягивалось на всю кнопку, и стрелка уезжала к
   правому краю сайдбара — далеко от имени. Так стрелка стоит сразу рядом. */
.chat-sidebar__username { min-width: 0; max-width: 100%; color: var(--white); font-size: .85rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: left; }
.chat-sidebar__footer {
  padding: 12px;
  padding-bottom: max(12px, calc(env(safe-area-inset-bottom) - 26px));
  border-top: 1px solid rgba(255,255,255,.1);
  display: flex; flex-wrap: wrap; gap: 8px;
}
.chat-sidebar__footer .btn { flex: 1 1 45%; }

.chat-container {
  position: relative;
  flex: 1; min-width: 0; min-height: 0;
  display: flex; flex-direction: column;
  background: var(--white);
}
.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 20px;
  padding-top: max(16px, env(safe-area-inset-top));
  background: var(--navy-950); color: var(--white);
}
.chat-header__title-group { display: flex; align-items: center; gap: 4px; min-width: 0; flex: 1; }
.chat-header__back {
  display: none; /* включается на мобильной ширине, см. медиа-запрос ниже */
  color: var(--white); font-size: 1.4rem; line-height: 1; padding: 4px 8px;
}
.chat-header__title {
  min-width: 0; font-size: 1rem; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-header__title--btn {
  display: flex; align-items: center; gap: 5px; min-width: 0;
  color: inherit; padding: 6px 8px; border-radius: var(--radius-sm);
}
.chat-header__title--btn:hover { background: rgba(255,255,255,.08); }
.chat-header__title--btn svg { flex-shrink: 0; opacity: .6; }
#currentChatNameText { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat-header__user { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.mute-control { position: relative; }
#muteChatBtn.is-muted { color: var(--orange); background: rgba(255,153,0,.16); }
.mute-menu {
  position: absolute; top: calc(100% + 8px); right: 0; min-width: 200px;
  background: var(--white); border: 1px solid #e2e6ee; border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg); padding: 6px;
  display: flex; flex-direction: column; gap: 2px;
  /* Выше, чем .pinned-banner (z-index:5) — они оказываются на одной высоте
     экрана (оба сразу под шапкой), и без этого плашка закрепа рисуется поверх
     верхних пунктов меню, обрезая их. */
  z-index: 8;
}
.mute-menu[hidden] { display: none; }
.mute-menu__item { width: 100%; text-align: left; padding: 8px 10px; border-radius: var(--radius-sm); font-size: .85rem; font-weight: 600; color: var(--text-dark); }
.mute-menu__item:hover { background: var(--gray-100); }
.mute-menu__item--unmute { color: var(--orange); }

.messages-wrap { position: relative; flex: 1; min-height: 0; display: flex; }
.messages {
  flex: 1; min-height: 0; overflow-y: auto; padding: 20px 24px;
  background: var(--gray-100);
  display: flex; flex-direction: column; gap: 12px;
  transition: padding-top var(--transition);
}
.messages.has-pinned { padding-top: 84px; }

.pinned-banner {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  position: absolute; top: 12px; left: 24px; right: 24px; z-index: 5;
  border-radius: var(--radius-md); background: #fff3e0;
  box-shadow: var(--shadow-lg); cursor: pointer;
}
.pinned-banner[hidden] { display: none; }
.pinned-banner__icon { font-size: 1rem; line-height: 1; flex-shrink: 0; }
.pinned-banner__body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.pinned-banner__label { font-size: .76rem; font-weight: 700; color: #b35f00; }
.pinned-banner__text {
  font-size: .8rem; color: var(--text-dark);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.pinned-banner__unpin { flex-shrink: 0; width: 24px; height: 24px; border-radius: 50%; color: var(--gray-500); font-size: .85rem; }
.pinned-banner__unpin:hover { background: rgba(255,153,0,.18); }

.composer-banner {
  display: flex; align-items: center; gap: 10px; padding: 8px 24px;
  border-top: 1px solid var(--gray-300); background: var(--gray-100);
}
.composer-banner[hidden] { display: none; }
.composer-banner[data-mode="edit"] { border-left: 3px solid var(--navy-700); }
.composer-banner[data-mode="reply"] { border-left: 3px solid var(--orange); }
.composer-banner__icon { font-size: 1rem; line-height: 1; flex-shrink: 0; }
.composer-banner__body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.composer-banner__label { font-size: .78rem; font-weight: 700; color: var(--navy-700); }
.composer-banner__text {
  font-size: .8rem; color: var(--gray-500);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.composer-banner__close { flex-shrink: 0; width: 24px; height: 24px; border-radius: 50%; color: var(--gray-500); font-size: .85rem; }
.composer-banner__close:hover { background: #e9edf5; }

.scroll-to-bottom-btn {
  position: absolute; right: 24px; bottom: 92px;
  width: 44px; height: 44px; border: none; border-radius: 50%;
  background: var(--navy-950); color: var(--white);
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow-lg); z-index: 6;
  transition: background .15s ease, transform .15s ease;
}
.scroll-to-bottom-btn:hover { background: var(--navy-800); transform: translateY(-2px); }
.scroll-to-bottom-btn[hidden] { display: none; }

.message {
  max-width: 70%; background: var(--white); border: 1px solid #eceff4;
  padding: 10px 14px; border-radius: var(--radius-md); font-size: 14px;
  align-self: flex-start; box-shadow: var(--shadow-md);
}
/* Только для сенсорных экранов (не трогаем выделение текста мышью на десктопе) —
   иначе долгое нажатие на iOS Safari открывает системное меню выделения текста
   вместо реакции приложения (открытия меню и т.п.). Сначала стояло только на
   .message, но тот же баг воспроизвёлся и на кнопке с именем в сайдбаре —
   ставим на все кнопки и кликабельные строки списков разом, а не по одной. */
@media (hover: none) {
  .message, button, .user-row, .task-item, .search-result-item {
    -webkit-touch-callout: none; -webkit-user-select: none; user-select: none;
  }
}
.message.own {
  background: linear-gradient(135deg, rgba(255,153,0,.14), rgba(255,179,71,.14));
  border-color: rgba(255,153,0,.3);
  align-self: flex-end;
}
.message.system {
  align-self: center; background: none; border: none; box-shadow: none;
  color: var(--gray-500); font-size: 12px; font-style: italic;
}
.message.mentions-me { box-shadow: inset 0 0 0 2px var(--orange); }
.message--flash { animation: message-flash 1.2s ease; }
@keyframes message-flash {
  0%, 100% { box-shadow: var(--shadow-md); }
  25% { box-shadow: inset 0 0 0 2px var(--orange), var(--shadow-md); }
}
.message-forwarded-label { font-size: 11px; font-style: italic; color: var(--gray-500); margin-bottom: 2px; }
.message-reply-quote {
  display: block; margin-bottom: 6px; padding: 4px 8px;
  border-left: 3px solid var(--orange); border-radius: 4px;
  background: rgba(6,11,21,.04); cursor: pointer;
}
.message-reply-quote:hover { background: rgba(6,11,21,.08); }
.message-reply-quote__author { display: block; font-size: 11px; font-weight: 700; color: var(--navy-700); }
.message-reply-quote__text {
  display: block; font-size: 12px; color: var(--gray-500);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.message-edited-tag { font-size: 10px; font-style: italic; color: var(--gray-500); }
.message-author { display: block; font-weight: 700; font-size: 12px; color: var(--navy-700); margin-bottom: 2px; }
.message-meta { display: flex; align-items: center; justify-content: flex-end; gap: 4px; margin-top: 4px; }
.message-reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.message-reactions:empty { display: none; margin-top: 0; }
.reaction-pill {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 999px; border: 1px solid #e2e6ee;
  background: var(--gray-100); font-size: .78rem; font-weight: 700; color: var(--text-dark);
}
.reaction-pill:hover { background: #e9edf5; }
.reaction-pill--mine { background: rgba(255,153,0,.16); border-color: rgba(255,153,0,.55); color: #b35f00; }
.message-time { font-size: 10px; color: var(--gray-500); }
.message-status { display: inline-flex; align-items: center; color: var(--gray-500); }
.message-status--read { color: var(--orange); }
.message-status--failed { color: #e5484d; cursor: pointer; }
.message-status--sending { color: var(--gray-300); }
.status-spinner {
  display: inline-block; width: 9px; height: 9px;
  border: 1.5px solid rgba(16,25,43,.15); border-top-color: var(--gray-500);
  border-radius: 50%; animation: status-spin .8s linear infinite;
}
@keyframes status-spin { to { transform: rotate(360deg); } }

.upload-error { padding: 0 24px; }
.upload-error:empty { display: none; }

.message-form {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 20px;
  padding-bottom: max(18px, env(safe-area-inset-bottom));
  background: var(--white); border-top: 1px solid var(--gray-300);
}
.message-form input {
  flex: 1; padding: 11px 14px; border: 1px solid var(--gray-300);
  border-radius: 999px; font-size: 16px; /* держим от 16px — иначе iOS зумит при фокусе */
  color: var(--text-dark);
}
.message-form input:focus { outline: 2px solid var(--orange-light); outline-offset: 1px; }
.message-form__send {
  flex-shrink: 0; width: 42px; height: 42px; border-radius: 50%;
  background: linear-gradient(135deg, var(--orange), var(--orange-light));
  color: var(--white); font-size: 1.1rem;
}

.mention-wrapper { position: relative; flex: 1; min-width: 0; }
.mention-dropdown {
  position: absolute; bottom: calc(100% + 8px); left: 0; right: 0;
  max-height: 240px; overflow-y: auto;
  background: var(--white); border: 1px solid #e2e6ee; border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg); padding: 6px;
  display: flex; flex-direction: column; gap: 2px;
}
.mention-dropdown[hidden] { display: none; }
.mention-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 7px 8px; border-radius: var(--radius-sm);
  text-align: left; font-size: .88rem; font-weight: 600; color: var(--text-dark);
}
.mention-item:hover, .mention-item.active { background: var(--gray-100); }
.mention-item__name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.avatar {
  flex-shrink: 0; width: 26px; height: 26px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: .75rem; font-weight: 800;
}
.mention-tag { background: rgba(255,153,0,.15); color: var(--navy-700); font-weight: 700; border-radius: 4px; padding: 1px 4px; }
.mention-tag--me { background: var(--orange); color: var(--navy-950); }

.emoji-picker {
  position: fixed; width: 260px; max-height: 220px; overflow-y: auto;
  background: var(--white); border: 1px solid #e2e6ee; border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg); padding: 8px;
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px;
  z-index: 50;
}
.emoji-picker[hidden] { display: none; }
.emoji-picker__item { font-size: 1.15rem; line-height: 1; padding: 6px 0; border-radius: 6px; text-align: center; }
.emoji-picker__item:hover { background: var(--gray-100); }

.message-context-menu {
  position: fixed; min-width: 210px;
  background: var(--white); border: 1px solid #e2e6ee; border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg); padding: 6px;
  display: flex; flex-direction: column; gap: 2px;
  z-index: 60;
}
.message-context-menu[hidden] { display: none; }
.message-context-menu__item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 10px; border-radius: var(--radius-sm);
  text-align: left; font-size: .88rem; font-weight: 600; color: var(--text-dark);
}
.message-context-menu__item:hover { background: var(--gray-100); }
.message-context-menu__icon { font-size: 1.1rem; line-height: 1; }
.message-context-menu__item--danger { color: #c53030; }
.message-context-menu__item--danger:hover { background: rgba(229,72,77,.1); }

/* ==========================================================================
   Диалоги (поиск, пересылка)
   ========================================================================== */
.chat-dialog {
  border: none; border-radius: var(--radius-lg); padding: 0;
  box-shadow: var(--shadow-lg); width: 100%; max-width: 380px;
  max-height: calc(100svh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 32px);
  overflow-y: auto;
  /* Явное центрирование вместо дефолтного UA-центрирования <dialog> — оно
     ломается под viewport-fit=cover в iOS Safari (пин к верху под вырезом). */
  position: fixed; top: 50%; left: 50%; margin: 0; transform: translate(-50%, -50%);
}
.chat-dialog::backdrop { background: rgba(6,11,21,.55); backdrop-filter: blur(2px); }
.chat-dialog form, .chat-dialog .dialog-body { padding: 26px; display: flex; flex-direction: column; gap: 16px; }
.chat-dialog h3 { font-size: 1.1rem; color: var(--text-dark); }
.chat-dialog input[type="text"] {
  padding: 11px 14px; border: 1px solid var(--gray-300); border-radius: var(--radius-sm);
  font-size: 16px; color: var(--text-dark);
}

.member-picker {
  max-height: 220px; overflow-y: auto;
  border: 1.5px solid #e2e6ee; border-radius: var(--radius-sm); padding: 6px;
  display: flex; flex-direction: column; gap: 2px;
}
.member-picker label { display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: 6px; font-size: .88rem; cursor: pointer; }
.member-picker label:hover { background: var(--gray-100); }
.member-picker input { accent-color: var(--orange); flex: 0 0 auto; width: 16px; height: 16px; margin: 0; }
.member-picker__empty { padding: 8px; font-size: .85rem; color: var(--gray-500); }

.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

.users-list { max-height: 340px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.user-row { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: var(--radius-sm); cursor: pointer; }
.user-row:hover { background: var(--gray-100); }
.user-row--self { cursor: default; }
.user-row--self:hover { background: none; }
.user-row__dot { width: 8px; height: 8px; flex-shrink: 0; border-radius: 50%; background: var(--gray-300); }
.user-row__dot.online { background: #22c55e; box-shadow: 0 0 0 3px rgba(34,197,94,.15); }
.user-row__name-wrap { flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; }
.user-row__name { min-width: 0; font-size: .9rem; font-weight: 600; color: var(--text-dark); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-row__status { flex-shrink: 0; font-size: .78rem; color: var(--gray-500); }

.role-badge {
  display: inline-block; padding: 1px 6px; border-radius: 999px;
  font-size: .62rem; font-weight: 800; text-transform: uppercase; letter-spacing: .02em;
}
.role-badge--super { background: rgba(255,153,0,.16); color: #b35f00; }
.role-badge--blocked { background: rgba(229,72,77,.14); color: #c53030; }
.btn--danger { color: #c53030; }
.btn--danger:hover { background: rgba(229,72,77,.12); }

.search-results { max-height: 320px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.search-result-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px; border-radius: var(--radius-sm); cursor: pointer; }
.search-result-item:hover { background: var(--gray-100); }
.search-result-item__body { flex: 1; min-width: 0; }
.search-result-item__text { font-size: .85rem; font-weight: 600; color: var(--text-dark); }
.search-result__chat { font-weight: 400; color: var(--gray-500); }
.search-result-item__meta {
  font-size: .8rem; color: var(--gray-500);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ==========================================================================
   Вложения, лайтбокс, галерея, превью ссылок
   ========================================================================== */
.chat-dialog--wide { max-width: 560px; }

.admin-tabs { display: flex; gap: 4px; border-bottom: 1.5px solid #e2e6ee; }
.admin-tab { padding: 8px 14px; font-size: .85rem; font-weight: 700; color: var(--gray-500); border-bottom: 2px solid transparent; margin-bottom: -1.5px; }
.admin-tab:hover { color: var(--text-dark); }
.admin-tab.active { color: var(--navy-950); border-bottom-color: var(--orange); }
.admin-tab[hidden] { display: none; }

.admin-panel { max-height: 380px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.admin-panel[hidden] { display: none; }

.admin-create-user { display: flex; gap: 8px; }
.admin-create-user input { flex: 1; min-width: 0; padding: 8px 10px; border: 1.5px solid #e2e6ee; border-radius: var(--radius-sm); font-size: .85rem; }
.admin-create-user input:focus { outline: none; border-color: var(--orange); box-shadow: 0 0 0 3px rgba(255,153,0,.12); }

.admin-row { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; padding: 8px; border: 1px solid #eceff4; border-radius: var(--radius-sm); }
.admin-row__info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.admin-row__name { font-size: .88rem; font-weight: 700; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.admin-row__meta { font-size: .75rem; color: var(--gray-500); }
.admin-row__actions { display: flex; gap: 6px; flex-wrap: wrap; flex-shrink: 0; max-width: 100%; }

.admin-version { font-size: .78rem; font-weight: 500; color: var(--gray-500); margin-top: -8px; }

.admin-chat-detail__section { margin-top: 12px; }
.admin-section-label { font-size: .72rem; font-weight: 800; color: var(--gray-500); text-transform: uppercase; letter-spacing: .03em; margin-bottom: 6px; }
/* Явная граница между формой добавления сотрудника и списком существующих. */
.admin-users-list-label { margin-top: 4px; padding-top: 16px; border-top: 1px solid #e2e6ee; }

.admin-member-list, .admin-message-list { display: flex; flex-direction: column; gap: 4px; max-height: 160px; overflow-y: auto; }
.admin-member-row, .admin-message-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; padding: 6px; border-radius: var(--radius-sm); }
.admin-member-row:hover, .admin-message-row:hover { background: var(--gray-100); }
.admin-member-row__name { flex: 1; min-width: 0; font-size: .85rem; font-weight: 600; }
.admin-message-row__body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.admin-message-row__author { font-size: .76rem; font-weight: 700; color: var(--navy-700); }
.admin-message-row__text { font-size: .82rem; color: var(--text-dark); overflow-wrap: break-word; }
.admin-message-row__time { font-size: .66rem; color: var(--gray-500); }

.admin-logs-date { width: 100%; margin-bottom: 8px; padding: 8px 10px; border: 1.5px solid #e2e6ee; border-radius: var(--radius-sm); font-size: .85rem; background: var(--white); }
/* align-items:flex-start (не center) + min-width:0 на тексте — без этого
   длинная JSON-строка журнала (без пробелов внутри токенов) не сжималась
   внутри flex-строки и вылезала за край диалога вбок вместо переноса. */
.admin-log-row { display: flex; align-items: flex-start; gap: 10px; padding: 6px 8px; border-radius: var(--radius-sm); font-size: .82rem; }
.admin-log-row:hover { background: var(--gray-100); }
.admin-log-row__time { font-size: .7rem; font-weight: 700; color: var(--gray-500); flex-shrink: 0; }
.admin-log-row__text { min-width: 0; color: var(--text-dark); overflow-wrap: break-word; font-family: monospace; }

.admin-disk-stat { display: flex; flex-direction: column; gap: 8px; padding: 4px 2px; }
.admin-disk-stat__bar { width: 100%; height: 10px; border-radius: 999px; background: var(--gray-100); overflow: hidden; }
.admin-disk-stat__bar-fill { height: 100%; background: var(--orange); border-radius: inherit; }
.admin-disk-stat__summary { font-size: .9rem; font-weight: 700; color: var(--text-dark); }
.admin-disk-stat__detail { font-size: .8rem; color: var(--gray-500); }

.section-tabs { display: flex; gap: 8px; padding: 4px; background: var(--gray-100); border-radius: 999px; }
.section-tab { flex: 1; padding: 8px 14px; border-radius: 999px; font-size: .85rem; font-weight: 700; color: var(--gray-500); }
.section-tab:hover { color: var(--text-dark); }
.section-tab.active { background: var(--white); color: var(--navy-950); box-shadow: var(--shadow-md); }
.section-panel[hidden] { display: none; }

.about-text { font-size: .88rem; line-height: 1.5; color: var(--text-dark); margin: 0; }
.about-footnote { font-size: .74rem; color: var(--gray-500); margin: 0; }
.about-footnote a { color: var(--gray-500); }

.create-task-form { flex-direction: row !important; flex-wrap: wrap; gap: 8px !important; padding: 0 !important; }
.create-task-form select {
  flex: 0 0 auto; max-width: 160px; padding: 10px 8px;
  border: 1.5px solid #e2e6ee; border-radius: var(--radius-sm); font-size: .85rem; background: var(--white);
}
.create-task-form input { flex: 1; min-width: 160px; padding: 10px 12px; border: 1.5px solid #e2e6ee; border-radius: var(--radius-sm); font-size: .88rem; }
.create-task-form input:focus, .create-task-form select:focus { outline: none; border-color: var(--orange); }

.task-tabs { display: flex; gap: 4px; border-bottom: 1.5px solid #e2e6ee; }
.task-tab { padding: 8px 14px; font-size: .85rem; font-weight: 700; color: var(--gray-500); border-bottom: 2px solid transparent; margin-bottom: -1.5px; }
.task-tab:hover { color: var(--text-dark); }
.task-tab.active { color: var(--navy-950); border-bottom-color: var(--orange); }

.task-list { max-height: 360px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.task-list[hidden] { display: none; }
.task-list__empty { padding: 12px 4px; font-size: .85rem; color: var(--gray-500); }
.task-item { display: flex; align-items: flex-start; gap: 10px; padding: 10px 12px; border: 1px solid #eceff4; border-radius: var(--radius-sm); background: var(--gray-100); }
.task-item.done { opacity: .55; }
.task-item.done .task-item__text { text-decoration: line-through; }
.task-item__check { flex-shrink: 0; width: 20px; height: 20px; margin-top: 1px; accent-color: var(--orange); cursor: pointer; }
.task-item__body { flex: 1; min-width: 0; }
.task-item__text { font-size: .9rem; color: var(--text-dark); overflow-wrap: break-word; }
.task-item__meta { margin-top: 3px; font-size: .74rem; color: var(--gray-500); }
.task-item__delete { flex-shrink: 0; padding: 2px 4px; font-size: 14px; color: var(--gray-500); }
.task-item__delete:hover { color: #c53030; }

.message-image { display: block; max-width: 260px; max-height: 260px; border-radius: var(--radius-sm); object-fit: cover; cursor: pointer; }

.video-preview {
  position: relative; display: block; width: 220px; max-width: 100%;
  aspect-ratio: 16 / 9; border-radius: var(--radius-sm); overflow: hidden;
  cursor: pointer; background: var(--navy-950);
}
.video-preview__el { display: block; width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.video-preview__play {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: #fff; background: rgba(0,0,0,.18); pointer-events: none;
}
.video-preview__play svg { filter: drop-shadow(0 1px 4px rgba(0,0,0,.5)); }

/* У <dialog> в UA-стилях по умолчанию width/height: fit-content — сам по себе
   inset:0 их не растягивает (stretch по инсетам работает только при width/
   height:auto). Явно перебиваем на auto + max-*, none — без единиц vw/vh
   (ненадёжны на iOS Safari standalone, см. памятку "iOS Safari PWA quirks"),
   только через инсеты. Фон — сразу на самом диалоге, а не только через
   ::backdrop: возможен случай, когда ::backdrop не отрисовался (лайтбокс
   открылся поверх уже открытого диалога галереи) и фон оставался прозрачным. */
.media-lightbox {
  position: fixed; inset: 0; width: auto; height: auto;
  max-width: none; max-height: none; margin: 0; padding: 0; border: none;
  background: rgba(6, 11, 21, .97);
}
.media-lightbox[open] { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.media-lightbox::backdrop { background: rgba(6, 11, 21, .94); }

.media-lightbox__toolbar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 28px max(16px, env(safe-area-inset-left));
  background: linear-gradient(to bottom, rgba(0,0,0,.6), transparent);
  z-index: 2;
}
.media-lightbox__label { font-size: .78rem; font-weight: 800; color: #fff; text-transform: uppercase; letter-spacing: .04em; text-shadow: 0 1px 3px rgba(0,0,0,.5); }
.media-lightbox__actions { display: flex; gap: 10px; flex-shrink: 0; }
.media-lightbox__btn {
  display: flex; align-items: center; justify-content: center;
  width: 42px; height: 42px; border-radius: 999px;
  background: rgba(20, 25, 38, .75); border: 1.5px solid rgba(255,255,255,.3);
  box-shadow: 0 2px 10px rgba(0,0,0,.4); color: #fff; font-size: 18px; text-decoration: none;
}
.media-lightbox__btn:hover { background: rgba(45, 52, 70, .85); }

/* width/height:100% вместо vw/vh — размер считается от реального бокса
   диалога (flex), а не от вьюпорта, поэтому не подвержено багу vh на iOS
   Safari. Паддинг сверху — чтобы контент не прятался под плашкой тулбара. */
.media-lightbox__content {
  width: 100%; height: 100%; padding: 76px 16px 16px; box-sizing: border-box;
  display: flex; align-items: center; justify-content: center;
}
.media-lightbox__content img, .media-lightbox__content video {
  max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px;
}

.file-card { display: flex; align-items: center; gap: 12px; padding: 6px 4px; min-width: 200px; color: var(--text-dark); }
.file-card__icon {
  flex-shrink: 0; width: 38px; height: 38px; display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm); background: var(--gray-100); color: var(--navy-700);
}
.file-card__icon--pdf { color: #c53030; }
.file-card__info { display: flex; flex-direction: column; min-width: 0; }
.file-card__name { font-size: .88rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-card__size { font-size: .76rem; color: var(--gray-500); }

.pdf-preview {
  display: flex; flex-direction: column; overflow: hidden;
  border: 1px solid #eceff4; border-radius: var(--radius-sm); background: var(--gray-100);
  width: 280px; max-width: 100%;
}
.pdf-preview__frame { width: 100%; height: 220px; border: none; background: var(--white); }
.pdf-preview__footer { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 10px; }
.pdf-preview__name { font-size: .8rem; font-weight: 600; color: var(--text-dark); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.pdf-preview__download {
  flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 999px; background: var(--white); color: var(--navy-700); box-shadow: var(--shadow-md);
}

.gallery-content { max-height: 420px; overflow-y: auto; margin-top: 10px; }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 6px; }
.gallery-grid__item { position: relative; display: block; width: 100%; padding: 0; aspect-ratio: 1; border-radius: var(--radius-sm); overflow: hidden; background: var(--gray-100); }
.gallery-grid__item img, .gallery-grid__item video { width: 100%; height: 100%; object-fit: cover; display: block; }
.gallery-grid__play { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #fff; font-size: 20px; text-shadow: 0 1px 4px rgba(0,0,0,.6); pointer-events: none; }
.gallery-file-list { display: flex; flex-direction: column; gap: 6px; }
.gallery-file-list .file-card__icon { background: var(--white); }

.link-preview { margin-top: 6px; }
.link-preview__card {
  display: flex; flex-direction: column; overflow: hidden;
  border: 1px solid #eceff4; border-radius: var(--radius-sm); background: var(--gray-100);
  color: inherit; text-decoration: none; max-width: 320px;
}
.link-preview__image { display: block; width: 100%; max-height: 160px; object-fit: cover; }
.link-preview__body { display: flex; flex-direction: column; gap: 2px; padding: 8px 10px; min-width: 0; }
.link-preview__site { font-size: .72rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: .02em; }
.link-preview__title {
  font-size: .86rem; font-weight: 600; color: var(--text-dark);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.link-preview__desc {
  font-size: .78rem; color: var(--gray-500);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* ==========================================================================
   Мобильная раскладка (&lt;=720px / &lt;=480px)
   ========================================================================== */
@media (max-width: 720px) {
  .chat-shell { height: 100vh; height: 100svh; }
  .chat-sidebar { width: 200px; }
}

@media (max-width: 480px) {
  .auth-card { padding: 28px 22px; }
  .chat-shell { flex-direction: column; height: 100vh; height: 100svh; }

  /* Список чатов и открытый чат не помещаются вместе на узком экране —
     показываем одну панель за раз. По умолчанию виден список; после выбора
     чата JS добавляет класс chat-shell--chat-open — список прячется, чат
     встаёт на весь экран, кнопка "назад" в шапке возвращает к списку. */
  .chat-sidebar { width: 100%; height: 100%; max-height: none; }
  .chat-container { display: none; }
  .chat-shell--chat-open .chat-sidebar { display: none; }
  .chat-shell--chat-open .chat-container { display: flex; }
  .chat-header__back { display: inline-flex; }

  .chat-header { padding: 12px 12px; padding-top: max(12px, env(safe-area-inset-top)); }

  .messages { padding: 12px; gap: 8px; }
  .message { padding: 8px 12px; font-size: 13.5px; max-width: 84%; }
  .message-author { font-size: 11px; margin-bottom: 1px; }

  .message-form { padding: 12px 14px; padding-bottom: max(12px, env(safe-area-inset-bottom)); gap: 6px; }
  .scroll-to-bottom-btn { right: 14px; bottom: 74px; width: 40px; height: 40px; }

  /* На узком экране .admin-row (flex-wrap:wrap в одну строку) сжимал название/мета
     чата до пары пикселей вместо переноса кнопок на отдельную строку — кнопки
     наезжали на текст. Полная колонка решает: сначала инфо на всю ширину, потом
     кнопки на всю ширину под ним. */
  .admin-row { flex-direction: column; align-items: stretch; }
  .admin-row__actions { width: 100%; }

  /* Вторая, независимая маскировочная подложка — специально под полем ввода,
     где симптом (зазор снизу после работы с клавиатурой) исторически и
     наблюдался. Декоративный элемент, ничего не перекрывает (pointer-events:
     none) и безопасен, даже если сам чуть промахнётся мимо истинного края. */
  .message-form::after {
    content: '';
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 160px;
    background: var(--white);
    z-index: -1;
    pointer-events: none;
  }
}
