:root {
  --bg: #F2F2F7;
  --surface: #FFFFFF;
  --text: #000000;
  --text-2: #8E8E93;
  --sep: rgba(60, 60, 67, 0.12);
  --accent: #0A84FF;
  --accent-2: #5E5CE6;
  --bubble-in: #E9E9EB;
  --bubble-in-text: #000000;
  --topbar: rgba(249, 249, 249, 0.85);
  --danger: #FF3B30;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1C1C1E;
    --text: #FFFFFF;
    --text-2: #8D8D93;
    --sep: rgba(84, 84, 88, 0.5);
    --bubble-in: #26262A;
    --bubble-in-text: #FFFFFF;
    --topbar: rgba(18, 18, 20, 0.8);
  }
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overscroll-behavior: none;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  position: fixed;
  inset: 0;
  overflow: hidden;
}

.hidden { display: none !important; }

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.screen.slide {
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  transform: translateX(100%);
  z-index: 5;
}
.screen.slide.open { transform: translateX(0); }
.screen.slide.no-anim { transition: none; }
.screen.splash {
  background: linear-gradient(160deg, #0A84FF 0%, #5E5CE6 100%);
  align-items: center;
  justify-content: center;
  z-index: 40;
}
.splash-logo {
  width: 96px;
  height: 96px;
  border-radius: 22px;
  animation: splash-pulse 1.2s ease-in-out infinite;
}
@keyframes splash-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(0.94); opacity: 0.85; }
}
.screen.slide.hidden { display: flex !important; visibility: hidden; }
.screen.slide.open.hidden { visibility: visible; }

/* ---------- Шапка ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px 10px;
  background: var(--topbar);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-bottom: 0.5px solid var(--sep);
  z-index: 2;
  flex-shrink: 0;
}
.topbar h1 { font-size: 17px; font-weight: 600; }
.icon-btn {
  width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  border: none; background: none; color: var(--accent);
  border-radius: 50%; cursor: pointer;
}
.icon-btn:active { background: var(--sep); }
button svg { pointer-events: none; }

/* ---------- Экран входа ---------- */
#screen-auth {
  background: linear-gradient(160deg, #0A84FF 0%, #5E5CE6 100%);
  justify-content: center;
  align-items: center;
  padding: 24px;
}
.auth-wrap {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border-radius: 24px;
  padding: 32px 24px calc(24px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
.auth-logo { width: 76px; height: 76px; border-radius: 18px; margin-bottom: 14px; }
.auth-title { font-size: 28px; font-weight: 700; letter-spacing: -0.5px; }
.auth-sub { color: var(--text-2); font-size: 15px; margin: 4px 0 24px; }
.auth-form { width: 100%; display: flex; flex-direction: column; gap: 10px; }
.auth-form .field { display: flex; flex-direction: column; gap: 4px; }
.auth-form .field.hidden { display: none; }
.field-hint { font-size: 13px; padding-left: 6px; }
.field-hint.ok { color: #30D158; }
.field-hint.bad { color: var(--danger); }
.code-input { text-align: center; font-size: 24px !important; letter-spacing: 10px; font-weight: 600; }
.auth-links { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.btn-link.muted { color: var(--text-2); font-size: 14px; }
.auth-form input {
  width: 100%;
  padding: 14px 16px;
  font-size: 16px;
  border: 1px solid var(--sep);
  border-radius: 14px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.auth-form input:focus { border-color: var(--accent); }
.auth-error {
  color: var(--danger);
  font-size: 14px;
  text-align: center;
}
.btn-primary {
  padding: 14px;
  font-size: 17px;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: none;
  border-radius: 14px;
  cursor: pointer;
}
.btn-primary:active { opacity: 0.85; transform: scale(0.985); }
.btn-primary.small { padding: 10px 22px; font-size: 15px; }
.btn-link {
  margin-top: 16px;
  border: none; background: none;
  color: var(--accent);
  font-size: 15px;
  cursor: pointer;
}

/* ---------- Баннер уведомлений ---------- */
.push-banner {
  margin: 10px 14px 0;
  padding: 12px 14px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--sep);
  font-size: 14px;
  line-height: 1.45;
  flex-shrink: 0;
}
.push-banner b { font-weight: 600; }
.push-banner .btn-primary {
  margin-top: 10px;
  width: 100%;
  padding: 11px;
  font-size: 15px;
}

/* ---------- Список чатов ---------- */
.chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.chat-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  cursor: pointer;
}
.chat-row:active { background: var(--surface); }
.avatar {
  width: 52px; height: 52px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; font-size: 20px;
  position: relative;
  background-size: cover !important;
  background-position: center !important;
}
.avatar.small { width: 36px; height: 36px; font-size: 15px; }
.avatar .dot {
  position: absolute;
  right: 0; bottom: 1px;
  width: 13px; height: 13px;
  border-radius: 50%;
  background: #30D158;
  border: 2.5px solid var(--bg);
}
.chat-row-body {
  flex: 1;
  min-width: 0;
  border-bottom: 0.5px solid var(--sep);
  padding: 8px 0;
}
.chat-row:last-child .chat-row-body { border-bottom: none; }
.chat-row-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-name { font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { font-size: 13px; color: var(--text-2); flex-shrink: 0; }
.chat-row-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.chat-last {
  font-size: 15px; color: var(--text-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-username { font-size: 14px; color: var(--text-2); }
.badge {
  min-width: 21px; height: 21px;
  padding: 0 6px;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 13px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-2);
  font-size: 16px;
  padding: 24px;
  text-align: center;
}
.empty-icon { font-size: 44px; }

/* ---------- Диалог ---------- */
.chat-topbar { justify-content: flex-start; }
.back-btn { margin-left: -6px; }
.chat-peer-info { min-width: 0; }
.chat-peer-name { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-peer-status { font-size: 12.5px; color: var(--text-2); }
.chat-peer-status.online { color: #30D158; }
.chat-peer-status.typing { color: var(--accent); }

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 12px 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.day-sep {
  align-self: center;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-2);
  margin: 12px 0 6px;
  padding: 4px 12px;
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
.msg {
  max-width: 76%;
  padding: 8px 13px;
  font-size: 16px;
  line-height: 1.35;
  border-radius: 18px;
  word-wrap: break-word;
  position: relative;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  animation: msg-in 0.18s ease;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}
.msg-text { white-space: pre-wrap; }
.msg.mine {
  align-self: flex-end;
  background: linear-gradient(135deg, #0A84FF, #0B6CDF);
  color: #fff;
  box-shadow: 0 1px 3px rgba(10, 60, 160, 0.18);
}
.msg.mine.tail { border-bottom-right-radius: 5px; }
.msg.theirs {
  align-self: flex-start;
  background: var(--bubble-in);
  color: var(--bubble-in-text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}
.msg .ticks {
  display: inline-block;
  margin-left: 4px;
  font-size: 11px;
  letter-spacing: -1.5px;
  opacity: 0.65;
}
.msg .ticks.read { opacity: 1; color: #8AF7B0; }
.msg.theirs.tail { border-bottom-left-radius: 5px; }
.msg .meta {
  display: block;
  font-size: 11px;
  margin-top: 2px;
  text-align: right;
  opacity: 0.7;
}
.typing-bubble {
  align-self: flex-start;
  background: var(--bubble-in);
  border-radius: 18px;
  padding: 12px 16px;
  display: flex;
  gap: 4px;
}
.typing-bubble i {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-2);
  animation: blink 1.2s infinite;
}
.typing-bubble i:nth-child(2) { animation-delay: 0.2s; }
.typing-bubble i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.3; } 30% { opacity: 1; } }

/* ---------- Поле ввода ---------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px));
  background: var(--topbar);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--sep);
  flex-shrink: 0;
}
#composer-input {
  flex: 1;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.35;
  padding: 9px 14px;
  border: 1px solid var(--sep);
  border-radius: 19px;
  background: var(--surface);
  color: var(--text);
  resize: none;
  outline: none;
  max-height: 120px;
}
.tab svg { transition: transform 0.15s; }
.tab.active svg { transform: scale(1.08); }
.send-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.15s, transform 0.15s;
}
.send-btn:disabled { opacity: 0.35; transform: scale(0.92); }

.attach-btn { color: var(--text-2); flex-shrink: 0; margin-bottom: -1px; }
.send-btn.mic { background: var(--text-2); }

/* ---------- Запись голосового ---------- */
.rec-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  background: var(--topbar);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--sep);
  flex-shrink: 0;
}
.rec-bar.hidden { display: none; }
.rec-cancel { color: var(--danger); }
.rec-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--danger);
  animation: blink 1.2s infinite;
}
#rec-time { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 16px; }
.rec-hint { flex: 1; color: var(--text-2); font-size: 14px; }

/* ---------- Медиа-сообщения ---------- */
.msg.media {
  padding: 3px;
  background: none;
}
.msg.media .msg-img {
  display: block;
  max-width: min(66vw, 300px);
  max-height: 340px;
  border-radius: 16px;
  cursor: pointer;
}
.msg.media .meta {
  position: absolute;
  right: 10px; bottom: 8px;
  color: #fff;
  text-shadow: 0 0 4px rgba(0,0,0,0.8);
  margin: 0;
}
.voice {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 170px;
  padding: 2px 0;
}
.vp-btn {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.25);
  color: inherit;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.msg.theirs .vp-btn { background: rgba(127,127,127,0.22); }
.vp-bar {
  flex: 1;
  height: 4px;
  border-radius: 2px;
  background: rgba(127,127,127,0.35);
  overflow: hidden;
}
.msg.mine .vp-bar { background: rgba(255,255,255,0.35); }
.vp-bar i {
  display: block;
  height: 100%;
  width: 0;
  background: currentColor;
  border-radius: 2px;
}
.vp-time { font-size: 12.5px; font-variant-numeric: tabular-nums; opacity: 0.85; }

/* ---------- Просмотр фото ---------- */
.img-viewer {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.img-viewer img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}

/* ---------- Вкладки ---------- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 4; /* ниже слайд-экранов (чат/поиск), чтобы они его накрывали */
  display: flex;
  background: var(--topbar);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--sep);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tabbar { padding-bottom: max(0px, calc(env(safe-area-inset-bottom, 0px) - 8px)); }
.tab {
  flex: 1;
  border: none;
  background: none;
  color: var(--text-2);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 4px 0 3px;
  font-size: 9.5px;
  font-family: inherit;
  cursor: pointer;
  position: relative;
}
.tab svg { width: 22px; height: 22px; }
.tab.active { color: var(--accent); }
.tab-badge {
  position: absolute;
  top: 4px;
  left: calc(50% + 8px);
  min-width: 17px; height: 17px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--danger);
  color: #fff;
  font-size: 11px; font-weight: 600; font-style: normal;
  display: flex; align-items: center; justify-content: center;
}
.screen.with-tabbar .chat-list,
.screen.with-tabbar .profile-body {
  padding-bottom: calc(48px + env(safe-area-inset-bottom, 0px));
}

/* ---------- Профиль ---------- */
.profile-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px 16px;
}
.profile-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin-bottom: 22px;
}
.profile-avatar {
  width: 96px; height: 96px;
  font-size: 36px;
  cursor: pointer;
}
.profile-name { font-size: 22px; font-weight: 700; }
.profile-username { font-size: 15px; color: var(--text-2); }
.settings-group {
  background: var(--surface);
  border-radius: 14px;
  overflow: hidden;
  margin-bottom: 18px;
}
.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 14px 16px;
  border: none;
  background: none;
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  border-bottom: 0.5px solid var(--sep);
}
.settings-row:last-child { border-bottom: none; }
.settings-row:active { background: var(--sep); }
.settings-row .row-value {
  color: var(--text-2);
  font-size: 15px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 55%;
}
.settings-row.danger { color: var(--danger); justify-content: center; }
.profile-footer { text-align: center; color: var(--text-2); font-size: 13px; }

/* ---------- v4: группы, ответы, реакции, файлы ---------- */
.topbar-action { font-size: 16px; font-weight: 600; padding: 0 4px; }
.chat-header-main {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
  cursor: pointer;
}
.new-group-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 13px 16px;
  border: none;
  background: none;
  color: var(--accent);
  font-size: 16.5px;
  font-weight: 600;
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  border-bottom: 0.5px solid var(--sep);
  flex-shrink: 0;
}
.ngr-icon { font-size: 20px; }
.gnew-selected {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 14px 8px;
  flex-shrink: 0;
}
.gnew-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 13.5px;
  cursor: pointer;
}
.chat-row.selected { background: var(--surface); }
.chat-row .check {
  margin-left: auto;
  color: var(--accent);
  font-size: 20px;
  flex-shrink: 0;
}

/* ответ / редактирование над полем ввода */
.composer-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--topbar);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--sep);
  flex-shrink: 0;
}
.cb-body {
  flex: 1;
  min-width: 0;
  border-left: 2.5px solid var(--accent);
  padding-left: 9px;
}
.cb-title { font-size: 13.5px; font-weight: 600; color: var(--accent); }
.cb-text {
  font-size: 14px;
  color: var(--text-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* цитата в пузыре */
.msg-quote {
  display: block;
  border-left: 2.5px solid currentColor;
  padding: 2px 8px;
  margin-bottom: 4px;
  border-radius: 4px;
  background: rgba(127, 127, 127, 0.14);
  font-size: 13.5px;
  opacity: 0.9;
}
.msg.mine .msg-quote { background: rgba(255, 255, 255, 0.16); }
.msg-quote b { display: block; font-size: 12.5px; }

/* имя отправителя в группе + пересылка */
.msg-sender {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 1px;
}
.msg-fwd {
  display: block;
  font-size: 13px;
  font-style: italic;
  opacity: 0.8;
  margin-bottom: 2px;
}

/* реакции */
.msg-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
}
.rx {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 8px;
  border-radius: 12px;
  background: rgba(127, 127, 127, 0.18);
  font-size: 13px;
  cursor: pointer;
}
.msg.mine .rx { background: rgba(255, 255, 255, 0.22); }
.rx.mine-rx { background: var(--accent); color: #fff; }

/* видео и файлы */
.msg-video {
  display: block;
  max-width: min(66vw, 300px);
  max-height: 340px;
  border-radius: 16px;
  background: #000;
}
.msg-file {
  display: flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  min-width: 180px;
  padding: 2px 0;
}
.msg-file .mf-icon {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}
.msg.theirs .msg-file .mf-icon { background: rgba(127, 127, 127, 0.22); }
.mf-info { min-width: 0; }
.mf-name {
  font-size: 14.5px;
  font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 180px;
}
.mf-size { font-size: 12.5px; opacity: 0.8; }

/* эмодзи в шторке */
.sheet-emojis {
  display: flex;
  justify-content: space-between;
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--surface);
}
.sheet-emojis button {
  border: none;
  background: none;
  font-size: 26px;
  cursor: pointer;
  padding: 2px;
  border-radius: 10px;
  transition: transform 0.12s;
}
.sheet-emojis button:active { transform: scale(1.25); }
.sheet-emojis button.mine-rx { background: rgba(10, 132, 255, 0.25); }
.sheet-actions {
  display: flex;
  flex-direction: column;
  border-radius: 14px;
  overflow: hidden;
  background: var(--surface);
}
.sheet-actions .sheet-btn {
  border-radius: 0;
  border-bottom: 0.5px solid var(--sep);
}
.sheet-actions .sheet-btn:last-child { border-bottom: none; }

/* пересылка: список чатов */
.fwd-list {
  max-height: 45vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.fwd-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 4px;
  border: none;
  background: none;
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  border-bottom: 0.5px solid var(--sep);
}
.fwd-row:last-child { border-bottom: none; }
.fwd-row .avatar { width: 38px; height: 38px; font-size: 15px; }

/* свайп для ответа */
.msg { transition: transform 0.15s ease; touch-action: pan-y; }
.msg.swiping { transition: none; }

/* пузырь-заглушка при загрузке вложения */
.msg.pending { opacity: 0.9; }
.msg.pending .msg-img { filter: brightness(0.75); }
.up-bar {
  display: block;
  height: 3.5px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.3);
  margin-top: 6px;
  overflow: hidden;
}
.msg.pending.media .up-bar {
  position: absolute;
  left: 10px; right: 10px; bottom: 10px;
  margin: 0;
}
.up-bar i {
  display: block;
  height: 100%;
  width: 0;
  background: #fff;
  border-radius: 2px;
  transition: width 0.15s;
}
.up-fail {
  display: block;
  margin-top: 5px;
  font-size: 12.5px;
  color: #FFD1CF;
}

/* пометка изменено */
.meta .edited { opacity: 0.8; margin-right: 4px; font-style: italic; }

/* ---------- Шторка действий ---------- */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 65;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: flex-end;
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom, 0px));
}
.sheet-card {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  animation: sheet-up 0.22s cubic-bezier(0.32, 0.72, 0, 1);
}
@keyframes sheet-up {
  from { transform: translateY(40px); opacity: 0.5; }
  to { transform: none; opacity: 1; }
}
.sheet-btn {
  padding: 16px;
  border: none;
  border-radius: 14px;
  background: var(--surface);
  color: var(--accent);
  font-size: 18px;
  font-family: inherit;
  cursor: pointer;
}
.sheet-btn:active { opacity: 0.8; }
.sheet-btn.danger { color: var(--danger); font-weight: 600; }

#mini-toast {
  position: fixed;
  left: 50%;
  bottom: 88px;
  transform: translateX(-50%) translateY(12px);
  background: rgba(0, 0, 0, 0.82);
  color: #fff;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 15px;
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
#mini-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Кнопка «вниз» (умный скролл) ---------- */
.jump-bottom {
  position: absolute;
  right: 14px;
  bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  width: 44px; height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 2px 10px rgba(0,0,0,0.28);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  z-index: 6;
  transition: opacity 0.18s, transform 0.18s;
}
.jump-bottom.hidden { display: none; }
.jump-bottom:active { transform: scale(0.92); }
.jump-badge {
  position: absolute;
  top: -4px; right: -2px;
  min-width: 18px; height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-size: 11px; font-weight: 700;
  line-height: 18px; text-align: center;
}
.jump-badge.hidden { display: none; }

/* ---------- История звонков ---------- */
.call-row.missed .chat-name { color: var(--danger); }
.call-dir { font-weight: 700; opacity: 0.65; margin-right: 2px; }
.call-row.missed .call-dir { color: var(--danger); opacity: 1; }

/* ---------- Модалка ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.modal-card {
  width: 100%;
  max-width: 320px;
  background: var(--surface);
  border-radius: 18px;
  padding: 20px 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.modal-title { font-size: 17px; font-weight: 600; text-align: center; }
.modal-card input {
  padding: 12px 14px;
  font-size: 16px;
  border: 1px solid var(--sep);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.modal-card input:focus { border-color: var(--accent); }
.modal-btns { display: flex; gap: 8px; margin-top: 4px; }
.modal-btn {
  flex: 1;
  padding: 11px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-family: inherit;
  background: var(--bg);
  color: var(--accent);
  cursor: pointer;
}
.modal-btn.primary { background: var(--accent); color: #fff; font-weight: 600; }

/* ---------- Звонки ---------- */
.chat-call-btns { margin-left: auto; display: flex; gap: 2px; }

.call-screen {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: #101014;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  color: #fff;
}
.call-screen.hidden { display: none; }
#remote-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #101014;
}
#local-video.mirror, #remote-video.mirror { transform: scaleX(-1); }
#local-video {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 14px);
  right: 14px;
  width: 104px;
  height: 148px;
  object-fit: cover;
  border-radius: 14px;
  border: 1.5px solid rgba(255,255,255,0.35);
  z-index: 2;
  background: #000;
}
.call-info {
  position: relative;
  z-index: 2;
  margin-top: calc(env(safe-area-inset-top, 0px) + 64px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  text-shadow: 0 1px 8px rgba(0,0,0,0.6);
}
.call-avatar {
  width: 112px; height: 112px;
  font-size: 42px;
}
.call-name { font-size: 26px; font-weight: 700; margin-top: 4px; }
/* входящий звонок — пульсирующие волны вокруг аватара */
.call-info.ringing .call-avatar {
  animation: callPulse 1.8s ease-out infinite;
}
@keyframes callPulse {
  0%   { box-shadow: 0 0 0 0 rgba(48,209,88,0.55), 0 0 0 0 rgba(48,209,88,0.35); }
  70%  { box-shadow: 0 0 0 22px rgba(48,209,88,0), 0 0 0 40px rgba(48,209,88,0); }
  100% { box-shadow: 0 0 0 0 rgba(48,209,88,0), 0 0 0 0 rgba(48,209,88,0); }
}
/* бейдж «групповой звонок» над именем */
.call-badge {
  display: none;
  align-items: center;
  gap: 6px;
  padding: 5px 14px;
  margin-bottom: 6px;
  border-radius: 20px;
  background: rgba(255,255,255,0.14);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.3px;
}
.call-info.show-badge .call-badge { display: inline-flex; }
.call-info.compact .call-badge { display: none; }
.call-info.compact { margin-top: calc(env(safe-area-inset-top, 0px) + 14px); flex-direction: row; gap: 8px; align-items: baseline; }
.call-info.compact .call-avatar { display: none; }
.call-info.compact .call-name { font-size: 17px; }
.call-status { font-size: 15px; opacity: 0.85; }
.call-actions {
  position: relative;
  z-index: 2;
  display: flex;
  gap: 26px;
  margin-bottom: calc(env(safe-area-inset-bottom, 0px) + 40px);
}
.call-btn {
  width: 62px; height: 62px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.22);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.call-btn:active { opacity: 0.8; }
.call-btn.red { background: #FF3B30; }
.call-btn.green { background: #30D158; }
.call-btn.off { background: rgba(255,255,255,0.85); color: #101014; }

/* ---------- Поиск ---------- */
.search-box { padding: 10px 14px; flex-shrink: 0; }
.search-box input {
  width: 100%;
  padding: 11px 16px;
  font-size: 16px;
  border: none;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

/* зелёная кнопка «Войти» в шапке группы, пока идёт звонок */
.call-join-btn {
  border: none;
  background: #30D158;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 15px;
  cursor: pointer;
}
.call-join-btn:active { opacity: 0.8; }

/* разовый экран запроса разрешений */
.perm-overlay {
  position: fixed; inset: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(6px);
}
.perm-card {
  width: 100%; max-width: 340px;
  background: var(--bg);
  color: var(--text);
  border-radius: 22px;
  padding: 28px 24px 20px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
}
.perm-emoji { font-size: 40px; margin-bottom: 10px; }
.perm-card h2 { font-size: 21px; margin: 0 0 8px; }
.perm-card p { font-size: 15px; line-height: 1.45; opacity: 0.75; margin: 0 0 20px; }
.perm-card .btn-primary { width: 100%; }
.perm-card .btn-link { margin-top: 10px; display: inline-block; }

/* плашка о звонке в ленте — пузырь по стороне отправителя */
.call-msg {
  max-width: 82%;
  margin: 3px 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px;
  border-radius: 18px;
  font-size: 14px;
}
.call-msg.mine {
  align-self: flex-end;
  background: linear-gradient(135deg, #0A84FF, #0B6CDF);
  color: #fff;
  border-bottom-right-radius: 5px;
}
.call-msg.theirs {
  align-self: flex-start;
  background: var(--bubble-in);
  color: var(--bubble-in-text);
  border-bottom-left-radius: 5px;
}
.call-msg-ic { font-size: 18px; flex-shrink: 0; }
.call-msg-tx { display: flex; flex-direction: column; line-height: 1.2; }
.call-msg-time { font-size: 11px; opacity: 0.6; margin-top: 2px; }
.call-msg-join {
  border: none;
  background: #30D158;
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 13px;
  border-radius: 14px;
  cursor: pointer;
  flex-shrink: 0;
}
.call-msg-join:active { opacity: 0.8; }

/* ---------- Групповой звонок: сетка участников ---------- */
.gcall-grid {
  position: absolute; inset: 0;
  display: grid;
  grid-auto-rows: 1fr;
  gap: 8px;
  padding: 76px 8px 140px;
  z-index: 1;
}
.gcall-tile {
  position: relative;
  border-radius: 16px;
  overflow: hidden;
  background: #1c1c22;
  min-height: 0;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
/* увеличенная плитка — на весь экран звонка, тап возвращает обратно */
.gcall-tile.max {
  position: absolute; inset: 0;
  z-index: 3;
  border-radius: 0;
}
.gcall-tile.max video { object-fit: contain; background: #000; }
.gcall-tile video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
}
.gcall-tile .avatar {
  width: 64px; height: 64px;
  font-size: 24px;
  background-size: cover !important;
}
.gcall-tile-name {
  position: absolute; left: 10px; bottom: 8px;
  color: #fff; font-size: 13px; font-weight: 600;
  text-shadow: 0 1px 3px rgba(0,0,0,0.7);
  z-index: 2;
  max-width: calc(100% - 20px);
  display: flex; align-items: center; gap: 5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.gcall-tile-mic { font-size: 13px; flex-shrink: 0; }
/* пинг — маленький бейдж в правом верхнем углу плитки */
.gcall-tile-ping {
  position: absolute; top: 7px; right: 8px;
  z-index: 2;
  padding: 2px 7px;
  border-radius: 10px;
  background: rgba(0,0,0,0.5);
  color: #fff; font-size: 11px; font-weight: 600;
  text-shadow: none;
}
.gcall-tile-ping:empty { display: none; }
/* переподключение участника: затемняем плитку и показываем плашку по центру */
.gcall-tile.reconnecting { opacity: 0.55; }
.gcall-tile-reconn {
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  z-index: 3;
  padding: 6px 12px;
  border-radius: 14px;
  background: rgba(0,0,0,0.62);
  color: #fff; font-size: 12px; font-weight: 600;
  white-space: nowrap;
}
