*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', system-ui, sans-serif;
}

:root {
  --bg: #0d1117;
  --bg-card: #161b22;
  --bg-sidebar: #0f1318;
  --accent: #58a6ff;
  --accent-hover: #1f6feb;
  --green: #3fb950;
  --green-hover: #2ea043;
  --red: #f85149;
  --red-hover: #c93025;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --border: #30363d;
  --msg-mine: #1f6feb;
  --msg-theirs: #21262d;
}

/* ── LIGHT THEME ── */
html.light {
  --bg:          #f6f8fa;
  --bg-card:     #ffffff;
  --bg-sidebar:  #f0f2f5;
  --accent:      #0969da;
  --accent-hover:#0550ae;
  --green:       #1a7f37;
  --green-hover: #116329;
  --red:         #d1242f;
  --red-hover:   #a40e26;
  --text:        #1f2328;
  --text-muted:  #656d76;
  --border:      #d0d7de;
  --msg-mine:    #0969da;
  --msg-theirs:  #eaeef2;
}

html, body { overflow-x: hidden; overscroll-behavior-x: none; }
body { background: var(--bg); color: var(--text); height: 100vh; overflow: hidden; }

.hidden { display: none !important; }

/* ── LOGIN ── */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: radial-gradient(ellipse at top, #161b22 0%, #0d1117 70%);
}

.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 44px 40px;
  width: 360px;
  text-align: center;
  box-shadow: 0 16px 48px rgba(0,0,0,0.5);
}

.login-logo { font-size: 3rem; margin-bottom: 10px; }

.login-card h1 {
  font-size: 1.8rem;
  color: var(--accent);
  letter-spacing: 2px;
  margin-bottom: 6px;
}

.subtitle { color: var(--text-muted); font-size: 0.88rem; margin-bottom: 32px; }

.login-card input {
  width: 100%;
  padding: 11px 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 12px;
  outline: none;
  transition: border-color 0.2s;
}

.login-card input:focus { border-color: var(--accent); }

#join-btn {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 6px;
  transition: background 0.2s;
}

#join-btn:hover { background: var(--accent-hover); }
#join-btn:disabled { opacity: 0.6; cursor: default; }

/* ── LAYOUT ── */
#app { display: flex; height: 100vh; }

/* ── SIDEBAR ── */
.sidebar {
  width: 256px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.sidebar-header span { font-size: 0.85rem; color: var(--text-muted); font-weight: 500; }

/* ── ROOMS SECTION ── */
.rooms-section {
  padding: 12px 12px 8px;
  border-bottom: 1px solid var(--border);
}

.section-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.section-row h3, .users-section h3 {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  width: 22px;
  height: 22px;
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover { background: rgba(255,255,255,0.08); color: var(--text); }

.install-btn {
  position: fixed; top: 12px; right: 12px; z-index: 1000;
  width: 42px; height: 42px; border-radius: 50%;
  background: #238636; color: #fff; border: none; cursor: pointer;
  font-size: 18px; line-height: 1;
  box-shadow: 0 4px 14px rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center;
  transition: background .15s ease, transform .15s ease;
}
.install-btn:hover { background: #2ea043; transform: scale(1.06); }
.install-btn:active { transform: scale(.96); }

#rooms-list { list-style: none; display: flex; flex-direction: column; gap: 1px; }

.room-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.88rem;
  transition: background 0.12s, color 0.12s;
  user-select: none;
}

.room-item:hover { background: rgba(255,255,255,0.06); color: var(--text); }

.room-item.active { background: rgba(88,166,255,0.12); color: var(--accent); font-weight: 600; }

.room-hash { font-size: 0.82rem; opacity: 0.6; }
.room-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.room-actions {
  display: none;
  gap: 2px;
  margin-left: auto;
  flex-shrink: 0;
}

.room-item:hover .room-actions { display: flex; }

.room-leave-btn, .room-delete-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 0.75rem;
  padding: 2px 4px;
  border-radius: 4px;
  line-height: 1;
  color: var(--text-muted);
  opacity: 0.7;
  transition: opacity 0.15s, background 0.15s, color 0.15s;
}

.room-leave-btn:hover {
  opacity: 1;
  background: rgba(255,255,255,0.1);
  color: var(--text);
}

.room-delete-btn:hover {
  opacity: 1;
  background: rgba(248,81,73,0.2);
  color: var(--red);
}

.room-unread {
  background: var(--accent);
  color: #fff;
  font-size: 0.67rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.room-item.active .room-unread { display: none; }
html.light .room-unread { background: var(--accent); }

#add-room-wrap { padding-top: 6px; }
#add-member-wrap { padding-top: 6px; display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
#add-member-select { flex: 1; min-width: 0; padding: 5px 8px; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; color: var(--text); font-size: 0.82rem; }
.add-member-error { font-size: 0.78rem; color: var(--red); width: 100%; }

#new-room-input {
  width: 100%;
  padding: 6px 10px;
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.85rem;
  outline: none;
}

/* ── USERS SECTION ── */
.users-section { padding: 12px 12px; flex: 1; overflow-y: auto; }

#users-list { list-style: none; display: flex; flex-direction: column; gap: 2px; }

#users-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  transition: background 0.15s;
  cursor: pointer;
}

#users-list li:hover { background: rgba(255,255,255,0.08); }
#users-list li:hover .user-name { color: var(--accent); }

#users-list li:not(.is-online) { opacity: 0.45; }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
}

.status-dot.online {
  background: var(--green);
  box-shadow: 0 0 0 2px rgba(63, 185, 80, 0.25);
}

.user-avatar {
  width: 28px;
  height: 28px;
  background: var(--accent-hover);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.78rem;
  flex-shrink: 0;
}

.user-name { flex: 1; font-size: 0.86rem; }

.call-btn {
  background: var(--green);
  border: none;
  border-radius: 6px;
  width: 24px;
  height: 24px;
  cursor: pointer;
  font-size: 0.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.1s;
  flex-shrink: 0;
}

.call-btn:hover { background: var(--green-hover); transform: scale(1.1); }
.audio-call-btn { background: var(--accent) !important; }
.audio-call-btn:hover { background: #3b82f6 !important; }

/* ── CHAT ── */
.chat-container { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.chat-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-header h2 { font-size: 0.95rem; }

#messages {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#messages::-webkit-scrollbar { width: 5px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.message {
  max-width: 68%;
  padding: 9px 14px;
  border-radius: 12px;
  word-break: break-word;
  animation: fadeIn 0.15s ease;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.message.mine {
  align-self: flex-end;
  background: var(--msg-mine);
  border-bottom-right-radius: 3px;
}

.message.theirs {
  align-self: flex-start;
  background: var(--msg-theirs);
  border: 1px solid var(--border);
  border-bottom-left-radius: 3px;
}

.message-header {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-bottom: 3px;
}

.msg-username { font-size: 0.73rem; font-weight: 600; color: rgba(255,255,255,0.55); }
.msg-time     { font-size: 0.68rem; color: rgba(255,255,255,0.4); }
.msg-edited-mark { font-size: 0.62rem; color: rgba(255,255,255,0.3); font-style: italic; }
.message-body { font-size: 0.93rem; line-height: 1.5; overflow-wrap: anywhere; }
.message-body a { color: #58a6ff; text-decoration: underline; word-break: break-all; }
html.light .message.mine .message-body a { color: #cfe2ff; }
html.light .message.theirs .message-body a { color: #1a56db; }

/* Push actions to the right */
.msg-actions {
  margin-left: auto;
  display: none;
  gap: 2px;
  align-items: center;
}

.message:hover .msg-actions,
.message.touch-active .msg-actions { display: flex; }

.msg-edit-btn, .msg-delete-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 0.72rem;
  padding: 2px 4px;
  border-radius: 4px;
  opacity: 0.6;
  transition: opacity 0.15s, background 0.15s;
  line-height: 1;
}

.msg-edit-btn:hover   { opacity: 1; background: rgba(255,255,255,0.1); }
.msg-delete-btn:hover { opacity: 1; background: rgba(248,81,73,0.2); }

.msg-edit-input {
  width: 100%;
  background: rgba(0,0,0,0.2);
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.93rem;
  padding: 3px 8px;
  outline: none;
}

.message.deleted { opacity: 0.5; }
.msg-deleted-text { font-style: italic; color: rgba(255,255,255,0.4); font-size: 0.85rem; }

/* ── Аудиозвонок ── */
#video-overlay.audio-mode .remote-video-bg,
#video-overlay.audio-mode #local-video {
  visibility: hidden; /* место под аудиопанель, видео не нужно */
}
.audio-call-ui {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 1;
}
.audio-call-avatar {
  width: 110px; height: 110px;
  border-radius: 50%;
  background: linear-gradient(145deg, #1f6feb, #0d1117);
  color: #fff;
  font-size: 3rem;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 0 10px rgba(31,111,235,0.15), 0 10px 40px rgba(0,0,0,0.5);
  animation: audioPulse 2.4s ease-in-out infinite;
}
.audio-call-name   { font-size: 1.25rem; font-weight: 700; color: #fff; margin: 0; }
.audio-call-timer  { font-size: 0.95rem; color: rgba(255,255,255,0.65); margin: 0; font-variant-numeric: tabular-nums; }
@keyframes audioPulse {
  0%, 100% { box-shadow: 0 0 0 10px rgba(31,111,235,0.15), 0 10px 40px rgba(0,0,0,0.5); }
  50%      { box-shadow: 0 0 0 22px rgba(31,111,235,0.07), 0 10px 40px rgba(0,0,0,0.5); }
}

/* ── Пустое состояние (нет выбранного чата) ── */
.no-room-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  padding: 24px;
}
.no-room-icon    { font-size: 2.6rem; margin-bottom: 10px; }
.no-room-title   { font-weight: 600; color: var(--text); margin-bottom: 6px; }
.no-room-hint    { font-size: 0.85rem; line-height: 1.5; }

.chat-input-area.no-room { opacity: .45; pointer-events: none; }

/* ── Группы: бейджи ролей и панель ── */
.room-role { font-size: 0.75rem; flex-shrink: 0; }
.chat-header-group { font-size: 1.1rem; }
.typing-indicator { font-size: 0.75rem; color: var(--text-muted); flex-shrink: 0; white-space: nowrap; animation: fadeIn 0.2s ease; }
#room-online-dot { flex-shrink: 0; width: 10px; height: 10px; }
.chat-header { display: flex; align-items: center; gap: 8px; }
.chat-header h2 { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.group-add-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 16px; font-size: 0.9rem; border-bottom: 1px solid var(--border);
}
.group-add-row.muted { color: var(--text-muted); justify-content: center; }
.group-add-row button {
  background: var(--accent); color: #fff; border: none; border-radius: 8px;
  width: 28px; height: 28px; cursor: pointer; font-size: 1rem; line-height: 1;
}
#group-add-results { max-height: 160px; overflow-y: auto; }
.grp-actions { display: flex; gap: 6px; justify-content: flex-end; }
.grp-actions button {
  background: transparent; border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; width: 30px; height: 30px; cursor: pointer; font-size: 0.85rem;
}
.grp-actions button:hover { border-color: var(--accent); color: var(--accent); }

/* ── Поиск по нику в сайдбаре ── */
.user-search-wrap { padding: 0 12px 8px; }
.user-search-wrap input {
  width: 100%;
  box-sizing: border-box;
  padding: 8px 12px;
  font-size: 0.88rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
}
.user-search-wrap input:focus { border-color: var(--accent); }
.user-search-empty {
  color: var(--text-muted);
  font-size: 0.82rem;
  padding: 10px 12px;
  list-style: none;
  text-align: center;
}
#users-list .user-name { cursor: pointer; }

.system-message {
  align-self: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 3px 12px;
  background: rgba(255,255,255,0.04);
  border-radius: 20px;
  animation: fadeIn 0.15s ease;
}

/* Reply bar */
.reply-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 14px;
  background: var(--bg-card);
  border-top: 1px solid var(--accent);
  gap: 8px;
}
.reply-bar-content { display: flex; align-items: center; gap: 8px; min-width: 0; }
.reply-bar-label { color: var(--accent); font-size: 1rem; flex-shrink: 0; }
.reply-bar-info { display: flex; flex-direction: column; min-width: 0; }
.reply-bar-name { font-size: 0.75rem; font-weight: 600; color: var(--accent); }
.reply-bar-text { font-size: 0.78rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reply-bar-close { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 1rem; padding: 2px 6px; flex-shrink: 0; }
.reply-bar-close:hover { color: var(--text); }

/* Quote inside message */
.msg-reply {
  display: flex;
  flex-direction: column;
  border-left: 3px solid var(--accent);
  padding: 3px 8px;
  margin-bottom: 4px;
  border-radius: 2px;
  background: rgba(88,166,255,0.08);
  cursor: pointer;
}
.msg-reply:hover { background: rgba(88,166,255,0.15); }
.msg-reply-name { font-size: 0.72rem; font-weight: 600; color: var(--accent); }
.msg-reply-text { font-size: 0.78rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
html.light .msg-reply { background: rgba(26,86,219,0.1); border-color: #1a56db; }
html.light .msg-reply-name { color: #1a56db; }
html.light .msg-reply-text { color: #374151; }
html.light .message.mine .msg-reply { background: rgba(255,255,255,0.2); border-color: rgba(255,255,255,0.8); }
html.light .message.mine .msg-reply-name { color: rgba(255,255,255,0.9); }
html.light .message.mine .msg-reply-text { color: rgba(255,255,255,0.75); }

/* Highlight jump target */
@keyframes msgHighlight {
  0%   { background: rgba(88,166,255,0.25); }
  100% { background: transparent; }
}
.msg-highlight { animation: msgHighlight 1.5s ease; }

/* Reply button in message actions */
.msg-reply-btn { font-size: 1rem; }

/* Read status checkmarks */
.msg-status {
  font-size: 0.72rem;
  color: rgba(255,255,255,0.4);
  margin-left: 2px;
  flex-shrink: 0;
  letter-spacing: -1px;
}
.msg-status.read { color: #58a6ff; }
html.light .message.mine .msg-status { color: rgba(255,255,255,0.5); }
html.light .message.mine .msg-status.read { color: #cfe2ff; }
html.light .message.theirs .msg-status { color: rgba(0,0,0,0.35); }
html.light .message.theirs .msg-status.read { color: var(--accent); }

.chat-input-area {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-card);
  display: flex;
  gap: 10px;
  align-items: center;
}

.chat-input-area input {
  flex: 1;
  padding: 10px 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.93rem;
  outline: none;
  transition: border-color 0.2s;
}

.chat-input-area input:focus { border-color: var(--accent); }

.attach-btn {
  padding: 10px 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.15s, border-color 0.15s;
}

.attach-btn:hover  { color: var(--accent); border-color: var(--accent); }
.attach-btn:disabled { opacity: 0.5; cursor: default; }

#send-btn {
  padding: 10px 18px;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s;
}

#send-btn:hover { background: var(--accent-hover); }

/* ── VIDEO OVERLAY ── */
#video-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-wrapper {
  position: relative;
  width: 100%;
  max-width: 900px;
  height: 100%;
  max-height: 580px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.remote-video-bg {
  width: 100%;
  height: 100%;
  background: #111;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

#remote-placeholder {
  color: var(--text-muted);
  font-size: 1rem;
  position: absolute;
}

#remote-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

#local-video {
  position: absolute;
  bottom: 80px;
  right: 16px;
  width: 176px;
  height: 118px;
  object-fit: cover;
  border-radius: 10px;
  border: 2px solid rgba(255,255,255,0.2);
  background: #222;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}

.call-controls {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 16px;
  align-items: center;
}

.ctrl-btn {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.14);
  color: white;
  font-size: 1.25rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.1s;
  backdrop-filter: blur(8px);
}

.ctrl-btn:hover { background: rgba(255,255,255,0.26); transform: scale(1.08); }
.ctrl-btn.active { background: rgba(248,81,73,0.35); }

.ctrl-btn.end {
  background: var(--red);
  width: 60px;
  height: 60px;
  font-size: 1.4rem;
}

.ctrl-btn.end:hover { background: var(--red-hover); }

/* ── INCOMING CALL ── */
#incoming-call {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.72);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(6px);
}

.incoming-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 44px 40px;
  text-align: center;
  min-width: 300px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}

.call-icon {
  display: block;
  font-size: 3rem;
  margin-bottom: 14px;
  animation: ring 0.7s infinite alternate;
}

@keyframes ring {
  from { transform: rotate(-12deg); }
  to   { transform: rotate(12deg); }
}

.incoming-card p { font-size: 1.05rem; margin-bottom: 28px; }

.call-actions { display: flex; gap: 14px; justify-content: center; }

.call-actions button {
  padding: 11px 26px;
  border: none;
  border-radius: 8px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}

.call-actions button:hover { opacity: 0.85; transform: scale(1.04); }

#accept-call { background: var(--green); color: #fff; }
#reject-call { background: var(--red); color: #fff; }

/* ── SAVED SESSION ── */
.saved-avatar {
  width: 64px;
  height: 64px;
  background: var(--accent-hover);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 700;
  margin: 0 auto 10px;
  color: #fff;
}

.saved-name {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 20px;
}

.link-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.82rem;
  cursor: pointer;
  margin-top: 4px;
  padding: 4px 0;
  text-decoration: underline;
  transition: color 0.15s;
}

.link-btn:hover { color: var(--text); }

/* ── AUTH TABS ── */
.auth-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  background: var(--bg);
  padding: 4px;
  border-radius: 8px;
}

.auth-tab {
  flex: 1;
  width: auto;
  padding: 8px;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 0;
  transition: background 0.15s, color 0.15s;
}

.auth-tab.active {
  background: var(--bg-card);
  color: var(--text);
}

.auth-tab:hover:not(.active) { color: var(--text); }

/* ── AUTH ERROR ── */
.auth-error {
  background: rgba(248, 81, 73, 0.12);
  border: 1px solid var(--red);
  color: var(--red);
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 0.85rem;
  margin-bottom: 12px;
  text-align: left;
}

/* ── LOGOUT BUTTON ── */
.logout-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  cursor: pointer;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s;
}

.logout-btn:hover { color: var(--red); border-color: var(--red); }

.sidebar-header-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

/* ── ADMIN PANEL ── */
.admin-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
}

.admin-modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 760px; max-width: 95vw;
  max-height: 85vh;
  display: flex; flex-direction: column;
  box-shadow: 0 24px 64px rgba(0,0,0,0.6);
}

.admin-modal-sm { width: 420px; }

.admin-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}

.admin-header h2 { font-size: 1rem; color: var(--text); }

.admin-close {
  background: transparent; border: none;
  color: var(--text-muted); font-size: 1.1rem;
  cursor: pointer; padding: 4px 8px; border-radius: 6px;
  transition: color 0.15s, background 0.15s;
}
.admin-close:hover { color: var(--red); background: rgba(248,81,73,.1); }

.admin-tabs {
  display: flex; gap: 4px; padding: 12px 16px 0;
  border-bottom: 1px solid var(--border);
}

.admin-tab {
  background: transparent; border: none; border-bottom: 2px solid transparent;
  color: var(--text-muted); font-size: 0.88rem; font-weight: 600;
  padding: 8px 14px 10px; cursor: pointer; margin-bottom: -1px;
  transition: color 0.15s, border-color 0.15s;
}
.admin-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.admin-tab:hover:not(.active) { color: var(--text); }

.admin-tab-content { flex: 1; overflow: hidden; display: flex; flex-direction: column; }

.admin-add-form {
  display: flex; flex-wrap: wrap; gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.admin-add-form input,
.admin-add-form select {
  flex: 1; min-width: 130px;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 6px; color: var(--text);
  font-size: 0.83rem; padding: 7px 10px;
  outline: none;
}
.admin-add-form input:focus,
.admin-add-form select:focus { border-color: var(--accent); }
.admin-add-form select { cursor: pointer; }

.adm-checkbox {
  display: flex; align-items: center; gap: 6px;
  font-size: 0.83rem; color: var(--text-muted); cursor: pointer;
  align-self: center;
}

.admin-add-form button {
  background: var(--accent-hover); border: none; border-radius: 6px;
  color: #fff; font-size: 0.83rem; font-weight: 600;
  padding: 7px 14px; cursor: pointer; white-space: nowrap;
  transition: opacity 0.15s;
}
.admin-add-form button:hover { opacity: 0.85; }

.admin-error {
  margin: 8px 16px 0; padding: 8px 12px;
  background: rgba(248,81,73,.12); border: 1px solid rgba(248,81,73,.3);
  border-radius: 6px; color: var(--red); font-size: 0.83rem;
}

.admin-table-wrap { flex: 1; overflow-y: auto; padding: 12px 16px; }

.admin-table { width: 100%; border-collapse: collapse; font-size: 0.84rem; }
.admin-table th {
  text-align: left; color: var(--text-muted); font-weight: 600;
  padding: 6px 10px; border-bottom: 1px solid var(--border);
}
.admin-table td { padding: 8px 10px; border-bottom: 1px solid rgba(48,54,61,.5); vertical-align: middle; }
.admin-table tr:last-child td { border-bottom: none; }
.admin-table tr:hover td { background: rgba(255,255,255,.03); }

.adm-badge {
  font-size: 0.72rem; font-weight: 700; padding: 2px 7px;
  border-radius: 10px; display: inline-block;
}
.adm-badge.admin { background: rgba(88,166,255,.2); color: var(--accent); }
.adm-badge.user  { background: rgba(139,148,158,.15); color: var(--text-muted); }

.adm-actions { display: flex; gap: 6px; justify-content: flex-end; }

.adm-btn {
  background: transparent; border: 1px solid var(--border);
  border-radius: 5px; color: var(--text-muted); font-size: 0.78rem;
  padding: 3px 9px; cursor: pointer; transition: all 0.15s; white-space: nowrap;
}
.adm-btn:hover { color: var(--text); border-color: var(--accent); }
.adm-btn.danger:hover { color: var(--red); border-color: var(--red); }

/* ── EMOJI BUTTON ── */
.emoji-btn {
  padding: 10px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.15s, border-color 0.15s;
}
.emoji-btn:hover { color: var(--accent); border-color: var(--accent); }

/* ── EMOJI PICKER ── */
.emoji-picker {
  position: fixed;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  width: 288px;
  max-height: 210px;
  overflow-y: auto;
  z-index: 2000;
  box-shadow: 0 8px 32px rgba(0,0,0,0.45);
}
.emoji-picker.hidden { display: none; }
.ep-btn {
  font-size: 1.25rem;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  line-height: 1.3;
  transition: background 0.1s;
}
.ep-btn:hover { background: rgba(255,255,255,0.1); }

/* ── REACTION PICKER ── */
.reaction-picker {
  position: fixed;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 5px 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  max-width: 250px;
  z-index: 2000;
  box-shadow: 0 4px 20px rgba(0,0,0,0.45);
}
.reaction-picker.hidden { display: none; }
.rp-btn {
  font-size: 1.2rem;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
  line-height: 1.3;
  transition: background 0.1s, transform 0.12s;
}
.rp-btn:hover { background: rgba(255,255,255,0.1); transform: scale(1.3); }

/* ── REACTIONS ── */
.message { position: relative; }
.msg-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 5px;
}
.reaction-chip {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  background: rgba(255,255,255,0.07);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2px 8px 2px 5px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
  user-select: none;
  line-height: 1.4;
}
.reaction-chip:hover { background: rgba(255,255,255,0.15); }
.reaction-chip.mine  { border-color: var(--accent); background: rgba(88,166,255,0.15); }
.reaction-emoji { font-size: 0.95rem; }
.reaction-count { color: var(--text-muted); font-size: 0.72rem; font-weight: 600; }

.add-reaction-btn {
  display: none;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 2px 8px;
  cursor: pointer;
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.4;
  transition: border-color 0.12s, color 0.12s;
}
.message:hover .add-reaction-btn { display: inline-flex; }
.add-reaction-btn:hover { border-color: var(--accent); color: var(--accent); }

html.light .ep-btn:hover,
html.light .rp-btn:hover { background: rgba(0,0,0,0.07); }
html.light .reaction-chip { background: rgba(0,0,0,0.05); }
html.light .reaction-chip:hover { background: rgba(0,0,0,0.1); }
html.light .reaction-chip.mine { background: rgba(9,105,218,0.12); }

/* ── LIGHT THEME: сообщения ── */

/* Мои — насыщенный синий, белый текст */
html.light .message.mine {
  background: #1a56db;
  color: #ffffff;
}
html.light .message.mine .message-body { color: #ffffff; }
html.light .message.mine .msg-username { color: rgba(255,255,255,0.7); }
html.light .message.mine .msg-time     { color: rgba(255,255,255,0.55); }
html.light .message.mine .msg-edited-mark { color: rgba(255,255,255,0.45); }

/* Чужие — светло-серые с чётким контрастом */
html.light .message.theirs {
  background: #dde4ef;
  border-color: #b8c8d8;
  color: #1f2328;
}
html.light .message.theirs .message-body { color: #1f2328; }
html.light .message.theirs .msg-username { color: rgba(0,0,0,0.5); }
html.light .message.theirs .msg-time     { color: rgba(0,0,0,0.38); }
html.light .message.theirs .msg-edited-mark { color: rgba(0,0,0,0.28); }
html.light .message.theirs .msg-deleted-text { color: rgba(0,0,0,0.4); }

/* Системные сообщения */
html.light .system-message { background: rgba(0,0,0,0.06); color: #656d76; }

/* ── DESKTOP-ONLY ELEMENTS ── */
.sidebar-toggle { display: none; }
#sidebar-backdrop { display: none; }
.mobile-nav { display: none; }

/* ════════════════════════════════════════
   MOBILE  (≤ 768px)
   ════════════════════════════════════════ */
@media (max-width: 768px) {

  :root {
    --safe-top:    env(safe-area-inset-top,    0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --nav-h:    calc(58px + var(--safe-bottom));
    --header-h: calc(54px + var(--safe-top));
  }

  body { overflow: hidden; }

  /* ── Экран входа ── */
  #login-screen {
    position: fixed; inset: 0;
    overflow-y: auto;
    align-items: flex-start;
    padding: calc(var(--safe-top) + 24px)
             clamp(16px, 5vw, 28px)
             calc(var(--safe-bottom) + 24px);
    background: radial-gradient(ellipse at 30% 20%, #161b22 0%, #0d1117 70%);
  }

  .login-card {
    width: 100%;
    max-width: 440px;
    margin: 0 auto;
    padding: clamp(24px, 6vw, 40px) clamp(18px, 5vw, 36px);
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  }

  .login-logo    { font-size: clamp(2.4rem, 10vw, 3.4rem); }
  .login-card h1 { font-size: clamp(1.4rem, 6vw, 2rem); }
  .subtitle      { font-size: clamp(0.82rem, 3.2vw, 0.92rem); }

  .login-card input {
    padding: clamp(12px, 3.5vw, 14px) 16px;
    /* 16px: iOS не зумит экран при фокусе на поле */
    font-size: 16px;
    border-radius: 12px;
  }
  #join-btn {
    padding: clamp(13px, 3.5vw, 15px);
    font-size: clamp(0.95rem, 3.5vw, 1.05rem);
    border-radius: 12px;
  }

  /* ── Нижняя навигация — скрыта, заменена кнопкой ← ── */
  .mobile-nav { display: none; }

  /* ── Главный контейнер ── */
  #app {
    position: fixed;
    top: 0; left: 0; right: 0;
    bottom: 0;
    overflow: hidden;
  }

  /* ── Боковая панель — заполняет весь #app ── */
  .sidebar {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    width: auto;
    border-right: none;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: none;
    flex-direction: column;
  }
  .sidebar.mob-active { display: flex; }

  /* Шапка сайдбара — учёт notch сверху */
  .sidebar-header {
    flex-shrink: 0;
    padding-top: calc(14px + env(safe-area-inset-top, 0px));
    min-height: calc(54px + env(safe-area-inset-top, 0px));
  }

  /* ── Область чата — заполняет весь #app ── */
  .chat-container {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    display: flex;
    flex-direction: column;
  }
  .chat-container.mob-hidden { display: none; }

  /* ── Кнопка «Назад» в шапке чата ── */
  .sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    border-radius: 8px;
    -webkit-tap-highlight-color: transparent;
    transition: color 0.15s, background 0.15s;
  }
  .sidebar-toggle:active { color: var(--accent); background: rgba(88,166,255,0.1); }

  /* Шапка чата — учёт notch */
  .chat-header {
    flex-shrink: 0;
    padding-top: calc(12px + env(safe-area-inset-top, 0px));
    min-height: calc(54px + env(safe-area-inset-top, 0px));
    padding-bottom: 12px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
  }
  .chat-header h2 {
    font-size: clamp(0.9rem, 4vw, 1.05rem);
    font-weight: 700;
  }

  .mobile-nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px 0;
    -webkit-tap-highlight-color: transparent;
    transition: color 0.2s;
    min-height: 48px;
    position: relative;
  }

  .mobile-nav-btn.active { color: var(--accent); }

  /* Активный индикатор — точка над иконкой */
  .mobile-nav-btn.active::before {
    content: '';
    position: absolute;
    top: 6px;
    width: 20px; height: 3px;
    background: var(--accent);
    border-radius: 2px;
  }

  .nav-icon  { font-size: clamp(1.2rem, 5vw, 1.4rem); line-height: 1; }
  .nav-label { font-size: clamp(0.68rem, 2.6vw, 0.78rem); font-weight: 600; }

  /* ── Сообщения ── */
  #messages {
    min-height: 0;
    -webkit-overflow-scrolling: touch;
    padding: 12px 14px;
    gap: 5px;
  }

  .message {
    max-width: min(82%, calc(100vw - 52px));
    padding: 9px 13px;
    font-size: clamp(0.88rem, 3.5vw, 0.95rem);
    border-radius: 16px;
  }
  .message.mine   { border-bottom-right-radius: 4px; }
  .message.theirs { border-bottom-left-radius:  4px; }

  .message-body { font-size: clamp(0.88rem, 3.5vw, 0.95rem); line-height: 1.55; }
  .msg-username  { font-size: clamp(0.68rem, 2.5vw, 0.75rem); }
  .msg-time      { font-size: clamp(0.62rem, 2.3vw, 0.68rem); }

  /* Кнопки действий с сообщением — всегда видны на мобиле */
  .message:hover .msg-actions { display: flex; }
  .msg-edit-btn, .msg-delete-btn, .msg-reply-btn {
    font-size: 0.85rem;
    padding: 4px 6px;
  }

  /* Картинки/видео */
  .msg-image { max-width: min(240px, calc(100vw - 80px)); max-height: 220px; }
  .msg-video { max-width: min(260px, calc(100vw - 60px)); }

  /* Реакции */
  .add-reaction-btn { display: inline-flex; }
  .reaction-chip { font-size: 0.8rem; padding: 3px 7px; }

  /* ── Поле ввода ── */
  .reply-bar { flex-shrink: 0; padding: 6px 12px; }

  .chat-input-area {
    flex-shrink: 0;
    flex-grow: 0;
    padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    gap: 8px;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
  }

  .chat-input-area input {
    padding: 11px 16px;
    border-radius: 24px;
    /* 16px минимум: iOS не авто-зумит страницу при фокусе → нет горизонтального скролла */
    font-size: 16px;
    background: var(--bg);
  }

  #send-btn {
    width: 42px; height: 42px;
    padding: 0;
    border-radius: 50%;
    flex-shrink: 0;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .attach-btn, .emoji-btn {
    padding: 10px 8px;
    font-size: 1.15rem;
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* ── Списки в сайдбаре ── */
  .room-item { padding: 13px 12px; min-height: 48px; font-size: 0.93rem; }
  .room-actions { display: flex; gap: 4px; }
  .room-leave-btn, .room-delete-btn { font-size: 0.9rem; padding: 4px 7px; }

  #users-list li { padding: 10px 12px; min-height: 48px; gap: 10px; }
  .user-avatar { width: 34px; height: 34px; font-size: 0.85rem; }
  .user-name   { font-size: 0.92rem; }
  .status-dot  { width: 9px; height: 9px; }

  /* Секция чатов и пользователей */
  .section-row h3, .users-section h3 { font-size: 0.72rem; letter-spacing: 1.5px; }

  /* ── Системные сообщения ── */
  .system-message { font-size: clamp(0.7rem, 2.8vw, 0.78rem); }

  /* ── Админ-панель снизу ── */
  .admin-modal {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    width: 100%;
    max-height: 92svh;
    border-radius: 18px 18px 0 0;
  }
  .admin-modal-sm {
    position: relative;
    bottom: auto;
    width: 96vw;
    border-radius: 16px;
    max-height: 88svh;
  }

  /* ── Входящий звонок ── */
  .incoming-card {
    width: min(90vw, 320px);
    border-radius: 20px;
    padding: 28px 24px;
  }

  /* ── Видеозвонок ── */
  #video-overlay { align-items: stretch; }
  .video-wrapper { width: 100%; height: 100%; max-width: none; max-height: none; }
  .remote-video-bg { border-radius: 0; }
  #local-video {
    width: clamp(80px, 22vw, 120px);
    height: clamp(56px, 15vw, 84px);
    bottom: max(84px, calc(80px + var(--safe-bottom)));
    right: 12px;
    border-radius: 12px;
  }
  .call-controls { bottom: max(18px, calc(16px + var(--safe-bottom))); gap: 20px; }
  .ctrl-btn     { width: 50px; height: 50px; font-size: 1.2rem; }
  .ctrl-btn.end { width: 58px; height: 58px; font-size: 1.35rem; }
}

/* ── Очень маленькие экраны (iPhone SE, 320px) ── */
@media (max-width: 360px) {
  .chat-input-area { padding: 7px 8px; gap: 5px; }
  .chat-input-area input { padding: 9px 12px; }
  #send-btn { width: 38px; height: 38px; }
  .attach-btn, .emoji-btn { padding: 8px 6px; font-size: 1rem; }
  .message-body { font-size: 0.86rem; }
  .room-item { padding: 11px 10px; }
}

/* ── HISTORY ── */
.message.history { opacity: 0.72; }

.history-divider {
  align-self: stretch;
  height: 1px;
  background: var(--border);
  margin: 10px 0;
  flex-shrink: 0;
}

/* ── FILE MESSAGES ── */
.msg-image {
  display: block;
  max-width: 280px;
  max-height: 220px;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
  transition: opacity 0.15s;
}

.msg-image:hover { opacity: 0.88; }

.msg-video {
  display: block;
  max-width: 300px;
  border-radius: 8px;
}

.msg-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: rgba(255,255,255,0.07);
  border-radius: 8px;
  color: inherit;
  text-decoration: none;
  font-size: 0.85rem;
  word-break: break-all;
  transition: background 0.15s;
}

.msg-file:hover { background: rgba(255,255,255,0.13); text-decoration: underline; }
