:root {
  --bg: #0f1115;
  --card: #141820;
  --muted: #a8b0bd;
  --text: #e8eaed;
  --border: #252830;
  --primary: #4f8cff;
  --primary2: #6aa4ff;
  --font-text: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-emoji: "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-text), var(--font-emoji);
  line-height: 1.4;
}

html,
body {
  height: 100%;
  overflow: hidden; /* страница целиком не должна скроллиться */
}

.screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow: auto; /* логин/регистрация при маленьком экране */
}

#app-screen.app-screen {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.app-screen .layout {
  flex: 1;
}

.hidden {
  display: none;
}

.card {
  width: 100%;
  max-width: 420px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
}

.title {
  margin: 0 0 14px 0;
  font-size: 24px;
}

.tabs {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.tab {
  flex: 1;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
}

.tab.active {
  border-color: rgba(79, 140, 255, 0.8);
  color: var(--text);
}

.panel {
  display: none;
}

.panel.active {
  display: block;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.field span {
  color: var(--muted);
  font-size: 13px;
}

.input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #0c0e12;
  color: var(--text);
  outline: none;
  font-family: var(--font-text), var(--font-emoji);
}

.primary {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 0;
  background: linear-gradient(90deg, var(--primary), var(--primary2));
  color: #06101f;
  font-weight: 700;
  cursor: pointer;
}

.ghost {
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.small {
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.error {
  min-height: 18px;
  margin-top: 12px;
  color: #ff7a7a;
  font-size: 13px;
}

.topbar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: #141820;
  position: sticky;
  top: 0;
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.me-block {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

.me-text {
  min-width: 0;
  flex: 1;
}

#me-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
}

.me-status-line {
  font-size: 12px;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.me-avatar {
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.profile-avatar-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 12px;
}

.profile-avatar-preview {
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border);
}

.profile-form label.small.primary {
  width: auto;
  padding: 9px 14px;
}

.brand {
  font-weight: 800;
}

.muted {
  color: var(--muted);
}

.mobile-only {
  display: none;
}

.menu-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 25;
}

.layout {
  width: 100%;
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 14px;
  padding: 14px;
  flex: 1;
  min-height: 0;
}

.sidebar {
  background: #141820;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  overflow: auto;
  min-height: 0;
}

.block {
  margin-bottom: 16px;
}

.block h2 {
  margin: 0 0 10px 0;
  font-size: 14px;
  color: var(--muted);
  font-weight: 600;
}

.inline {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.list-item {
  background: #0c0e12;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  color: var(--text);
  text-align: left;
  font: inherit;
  display: flex;
  align-items: center;
  gap: 10px;
}

.list-item.active {
  border-color: rgba(79, 140, 255, 0.9);
}

.direct-chat-item .chat-list-text {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.chat-list-entry .chat-list-label,
.chat-list-entry .chat-list-text {
  flex: 1;
  min-width: 0;
  text-align: left;
}

.chat-unread-badge {
  flex-shrink: 0;
  min-width: 22px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(79, 140, 255, 0.95);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}

.chat-list-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border);
  object-fit: cover;
  flex-shrink: 0;
}

.main {
  background: #141820;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

.chat-header {
  flex: 0 0 auto;
}

.chat-header {
  padding: 6px 6px 10px 6px;
  border-bottom: 1px solid var(--border);
}

.chat-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-avatar {
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-title {
  font-weight: 800;
  min-width: 0;
}

.chat-subrow {
  display: flex;
  align-items: center;
  gap: 10px;
}

.manage-section {
  margin-top: 14px;
}

.manage-section-title {
  margin: 0 0 10px 0;
  font-size: 14px;
  color: var(--muted);
  font-weight: 600;
}

.manage-add {
  display: flex;
  gap: 10px;
  align-items: center;
}

.manage-add select {
  flex: 1;
}

.manage-add button {
  flex: 0 0 auto;
}

.manage-add .primary {
  width: auto; /* не растягивать "Добавить" на всю строку */
}

.messages {
  flex: 1;
  overflow-y: auto; /* скролл только тут */
  overflow-x: hidden;
  padding: 12px 6px;
  /* Иначе при загрузке img браузер двигает scrollTop и срывает наш pin */
  overflow-anchor: none;
}

/* Растёт с контентом — ResizeObserver видит смену высоты при подгрузке img/video */
.messages-inner {
  display: block;
  min-width: 0;
  overflow-anchor: none;
}

.message img,
.message video {
  overflow-anchor: none;
}

.message-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-bottom: 10px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

.message-row.me {
  flex-direction: row-reverse;
}

.message-avatar-wrap {
  width: 40px;
  flex: 0 0 40px;
  align-self: flex-end;
}

.message-avatar-wrap.message-avatar-spacer {
  visibility: hidden;
  pointer-events: none;
}

.message-avatar-img {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border);
  display: block;
}

.message-bubble-col {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
  max-width: min(68%, calc(100% - 48px));
}

.message-row.me .message-bubble-col {
  align-items: flex-end;
}

.message-readmarks {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
  padding: 0 2px;
  align-self: flex-end;
}

.message-readmarks.clickable {
  cursor: pointer;
  color: #7ab0ff;
}

.message-row.me .message-readmarks {
  align-self: flex-end;
}

.message.message-bubble {
  margin-bottom: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0c0e12;
  max-width: 100%;
  color: var(--text);
  width: fit-content;
  font-family: var(--font-text), var(--font-emoji);
  line-height: 1.45;
  word-break: break-word;
}

.message.message-bubble.me {
  margin-left: 0;
  border-color: rgba(79, 140, 255, 0.7);
}

.message.message-bubble.selected {
  border-color: #6aa4ff;
  box-shadow: 0 0 0 1px rgba(106, 164, 255, 0.45);
}

.message.message-bubble:has(.voice-message) {
  width: min(100%, 400px);
  min-width: min(280px, 100%);
  max-width: 100%;
}

/* Голосовые: без нативных controls — на Android Chrome они часто сворачиваются в «⋯» */
.voice-message audio.voice-audio-hidden,
.voice-message audio.audio-inline {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
}

.voice-message {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0;
  width: 100%;
  align-self: stretch;
}

.voice-player-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-width: 0;
}

.voice-player-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.voice-progress-outer {
  width: 100%;
  padding: 4px 0;
}

/* Нативный range — нормальная работа мыши на ПК без pointer/click-конфликтов */
.voice-progress-range {
  display: block;
  width: 100%;
  height: 10px;
  margin: 0;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  accent-color: #4f8cff;
}

.voice-progress-range:focus {
  outline: none;
}

.voice-progress-range:focus-visible {
  outline: 2px solid rgba(79, 140, 255, 0.7);
  outline-offset: 2px;
  border-radius: 4px;
}

.voice-progress-range::-webkit-slider-runnable-track {
  height: 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.1);
}

.voice-progress-range::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  margin-top: -3px;
  border-radius: 50%;
  background: #4f8cff;
  border: 1px solid rgba(255, 255, 255, 0.35);
  cursor: grab;
}

.voice-progress-range::-moz-range-track {
  height: 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.1);
}

.voice-progress-range::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #4f8cff;
  border: 1px solid rgba(255, 255, 255, 0.35);
  cursor: grab;
}

.voice-play-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: rgba(79, 140, 255, 0.18);
  color: var(--text);
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.voice-play-btn:active {
  background: rgba(79, 140, 255, 0.32);
}

.voice-play-btn--playing {
  background: rgba(79, 140, 255, 0.28);
}

.voice-play-ico {
  display: block;
}

.voice-time {
  font-size: 12px;
  line-height: 1.3;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  user-select: none;
}

.voice-time-sep {
  opacity: 0.75;
  padding: 0 2px;
}

@media (min-width: 501px) {
  .voice-message {
    min-width: 280px;
    max-width: min(100%, 400px);
  }
}

@media (max-width: 500px) {
  .message.message-bubble:has(.voice-message) {
    width: 100%;
    max-width: 100%;
    min-width: min(100%, 320px);
    box-sizing: border-box;
  }

  .voice-message {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .voice-time {
    font-size: 13px;
  }
}

.read-details-body {
  max-height: 50vh;
  overflow-y: auto;
  margin-top: 8px;
}

.read-details-line {
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

.message-meta {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.reply-quote {
  margin-bottom: 8px;
  padding: 7px 9px;
  border-left: 3px solid #4f8cff;
  background: rgba(79, 140, 255, 0.08);
  border-radius: 6px;
}

.forward-quote {
  margin-bottom: 8px;
  padding: 7px 9px;
  border-left: 3px solid #87a8ff;
  background: rgba(135, 168, 255, 0.08);
  border-radius: 6px;
}

.composer-section {
  position: relative;
  flex: 0 0 auto;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.composer {
  display: flex;
  gap: 10px;
  align-items: center;
}

.emoji-panel {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  margin-bottom: 6px;
  max-height: min(220px, 40vh);
  overflow-y: auto;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 4px;
  background: #0c0e12;
  border: 1px solid var(--border);
  border-radius: 10px;
  z-index: 55;
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.35);
}

.emoji-panel.hidden {
  display: none !important;
}

.emoji-cell {
  font-family: var(--font-text), var(--font-emoji);
  font-size: 22px;
  line-height: 1.2;
  padding: 6px 4px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.emoji-cell:hover {
  background: rgba(79, 140, 255, 0.15);
}

@media (max-width: 500px) {
  .emoji-panel {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
}

.message-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 6px 0 8px;
  align-items: center;
}

.message-actions.hidden {
  display: none !important;
}

#reply-preview {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #0c0e12;
}

#reply-preview.hidden {
  display: none !important;
}

#pinned-preview {
  margin-top: 4px;
}

.composer .input {
  flex: 1 1 auto;
  min-width: 0;
}

.composer .primary {
  width: auto;
  flex: 0 0 auto;
  padding-left: 14px;
  padding-right: 14px;
}

.attach-btn {
  width: 40px;
  flex: 0 0 auto;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.attach-btn.recording {
  border-color: #ff7a7a;
  color: #ff7a7a;
}

.message img {
  margin-top: 8px;
  max-width: 220px;
  max-height: 220px;
  width: auto;
  height: auto;
  object-fit: cover;
  border-radius: 10px;
  display: block;
  cursor: pointer;
}

.message video {
  margin-top: 8px;
  max-width: 320px;
  max-height: 220px;
  width: auto;
  height: auto;
  border-radius: 10px;
  background: #000;
  cursor: pointer;
}

.message .video-note {
  width: 180px;
  height: 180px;
  border-radius: 50%;
  object-fit: cover;
  max-width: none;
  max-height: none;
}

.video-note-preview {
  position: fixed;
  right: 16px;
  bottom: 96px;
  width: 140px;
  height: 180px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: rgba(12, 14, 18, 0.95);
  z-index: 88;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.video-note-preview.hidden {
  display: none !important;
}

.video-note-preview video {
  width: 100%;
  height: 146px;
  object-fit: cover;
  transform: scaleX(-1);
  background: #000;
}

.video-note-preview-label {
  font-size: 12px;
  color: var(--muted);
  padding: 8px;
  text-align: center;
}

.media-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
}

.media-modal-content {
  width: 100%;
  max-width: 980px;
  max-height: 88vh;
  background: #0c0e12;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: auto;
  position: relative;
  padding: 12px;
}

.media-modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
}

.media-modal-body {
  display: flex;
  justify-content: center;
  align-items: center;
}

.media-modal-body img {
  max-width: 100%;
  max-height: 80vh;
  object-fit: contain;
  border-radius: 10px;
}

.media-modal-body img.media-modal-avatar-full {
  max-width: min(90vw, 560px) !important;
  max-height: min(90vh, 560px) !important;
}

.media-modal-body video {
  width: 100%;
  max-height: 80vh;
  border-radius: 10px;
  background: #000;
}

/* `hidden` должен побеждать `display:flex` у модалки */
.media-modal.hidden {
  display: none !important;
}

/* Group modal */
.group-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 85;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
}

.group-modal.hidden {
  display: none !important;
}

.group-modal-content {
  width: 100%;
  max-width: 520px;
  max-height: 88vh;
  background: #0c0e12;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: auto;
  position: relative;
  padding: 12px 14px 14px 14px;
}

.group-modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
}

.group-modal-title {
  margin: 18px 0 12px 0;
  font-size: 16px;
}

.group-form .field {
  margin-bottom: 10px;
}

.message .file-link {
  display: inline-block;
  margin-top: 8px;
  color: var(--primary2);
  text-decoration: none;
  word-break: break-all;
}

.ws-status {
  margin-top: 8px;
  font-size: 13px;
}

#voice-status {
  margin-top: 6px;
  font-size: 13px;
}

#app-error {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 78px; /* над композером */
  z-index: 60;
  pointer-events: none;
}

@media (max-width: 500px) {
  #app-error {
    bottom: 94px;
  }
}

@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
    padding: 8px;
    gap: 8px;
    height: calc(100dvh - 62px);
  }

  .mobile-only {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    top: 56px;
    left: 10px;
    right: 10px;
    bottom: 10px;
    z-index: 30;
    max-height: calc(100dvh - 66px);
    transform: translateY(-110%);
    transition: transform 180ms ease;
    pointer-events: none;
  }

  .app-screen.mobile-open .sidebar {
    transform: translateY(0);
    pointer-events: auto;
  }

  .main {
    min-height: 0;
    padding: 8px;
  }
}

@media (max-width: 500px) {
  .composer {
    flex-direction: row;
    align-items: center;
    gap: 6px;
  }
  .composer .input {
    flex: 1 1 auto;
    min-width: 0;
  }
  .composer .attach-btn {
    width: 34px;
    min-width: 34px;
    height: 34px;
    font-size: 14px;
  }
  .composer .primary {
    width: auto;
    flex: 0 0 auto;
    padding-left: 10px;
    padding-right: 10px;
  }

  .chat-subrow {
    flex-direction: column;
    align-items: flex-start;
  }

  .message-bubble-col {
    max-width: min(88%, calc(100% - 44px));
  }
}

.pinned-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 6px 0 4px;
  min-height: 0;
}

.pinned-bar.hidden {
  display: none !important;
}

.pinned-bar-arrow {
  flex-shrink: 0;
  width: 28px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #0c0e12;
  color: var(--text);
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
  padding: 0;
}

.pinned-bar-arrow:hover {
  background: #141820;
}

.pinned-bar-viewport {
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}

.pinned-bar-track {
  display: flex;
  gap: 8px;
  padding: 4px 0;
}

.pinned-chip {
  flex: 0 0 auto;
  max-width: min(100%, 340px);
  text-align: left;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #0c0e12;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  line-height: 1.35;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  white-space: normal;
}

.pinned-chip:hover {
  border-color: rgba(79, 140, 255, 0.9);
}

.composer-section.composer-recording .attach-btn {
  width: 48px !important;
  min-width: 48px !important;
  height: 48px !important;
  font-size: 20px !important;
}

.composer-section.composer-recording #btn-voice,
.composer-section.composer-recording #btn-video-note {
  animation: recording-pulse 1.2s ease-in-out infinite;
}

.btn-record-cancel {
  color: #ff8a8a;
  border-color: rgba(255, 140, 140, 0.5);
}

@keyframes recording-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(255, 100, 100, 0.35);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(255, 100, 100, 0.1);
  }
}

.chat-list-avatar,
.message-avatar-img,
.chat-avatar,
.me-avatar {
  cursor: pointer;
}

.group-member-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border);
  flex-shrink: 0;
  cursor: pointer;
}

