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

:root {
  --bg: #f5f7fa;
  --sidebar-bg: #ffffff;
  --header-bg: #ffffff;
  --msg-bg: #eef1f5;
  --mine-bg: #cfe3f8;
  --accent: #3b7dd8;
  --accent-hover: #2f68b8;
  --success: #16a34a;
  --success-hover: #15803d;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --text: #1f2937;
  --muted: #6b7280;
  --input-bg: #f0f2f5;
  --border: #e0e4e9;
  --online: #16a34a;
  --radius: 12px;
  --sidebar-w: 260px;
  /* safe area insets (0 on desktop, non-zero on notch devices) */
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}

/* ── Base ─────────────────────────────────────────── */
html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}
html {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
}

.screen { display: flex; flex-direction: column; }
.hidden { display: none !important; }

/* ── Auth ─────────────────────────────────────────── */
#auth-screen {
  position: fixed;
  inset: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: max(24px, var(--sat)) 16px max(24px, var(--sab));
  overflow-y: auto;
}

#apk-banner {
  display: flex; align-items: center; gap: 12px;
  width: 100%; max-width: 380px;
  background: var(--sidebar-bg); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 16px;
  text-decoration: none; color: var(--text);
  box-shadow: 0 4px 16px rgba(0,0,0,.06);
  transition: border-color .15s, transform .15s;
}
#apk-banner:hover { border-color: var(--accent); transform: translateY(-1px); }
.apk-banner-icon { font-size: 1.5rem; }
.apk-banner-text { flex: 1; display: flex; flex-direction: column; gap: 1px; }
.apk-banner-text b { font-size: 0.9rem; }
.apk-banner-text small { color: var(--muted); font-size: 0.75rem; }
.apk-banner-cta {
  background: var(--accent); color: #fff; border-radius: 8px;
  padding: 8px 12px; font-size: 0.8rem; font-weight: 700; white-space: nowrap;
}
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--sidebar-bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.auth-logo { text-align: center; font-size: 2.8rem; margin-bottom: 8px; }
.auth-card h1 { text-align: center; font-size: 1.6rem; color: var(--text); font-weight: 700; margin-bottom: 4px; }
.auth-sub { text-align: center; font-size: 0.83rem; color: var(--muted); margin-bottom: 24px; line-height: 1.45; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 0.78rem; color: var(--muted); margin-bottom: 5px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.field input { width: 100%; padding: 11px 14px; background: var(--input-bg); border: 1px solid var(--border); border-radius: 10px; color: var(--text); font-size: 0.95rem; transition: border-color .15s; -webkit-appearance: none; }
.field input:focus { outline: none; border-color: var(--accent); }

.btn-primary { width: 100%; padding: 13px; border: none; border-radius: 10px; font-size: 0.97rem; font-weight: 600; cursor: pointer; background: var(--accent); color: #fff; transition: background .15s; margin-top: 6px; -webkit-appearance: none; letter-spacing: .01em; }
.btn-success { width: 100%; padding: 13px; border: none; border-radius: 10px; font-size: 0.97rem; font-weight: 600; cursor: pointer; background: var(--success); color: #fff; transition: background .15s; margin-top: 6px; -webkit-appearance: none; }
.btn-danger  { width: 100%; padding: 13px; border: none; border-radius: 10px; font-size: 0.97rem; font-weight: 600; cursor: pointer; background: var(--danger); color: #fff; transition: background .15s; -webkit-appearance: none; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-success:hover { background: var(--success-hover); }
.btn-danger:hover  { background: var(--danger-hover); }
.btn-primary:disabled { opacity: .6; cursor: default; }

.error   { color: var(--danger);  font-size: 0.83rem; margin-top: 10px; min-height: 18px; }
.success { color: var(--success); font-size: 0.83rem; margin-top: 10px; min-height: 18px; }
.req { color: var(--muted); font-weight: 400; font-size: 0.75rem; }

/* ── App layout ───────────────────────────────────── */
#app-screen {
  position: fixed;
  inset: 0;
  flex-direction: row;
  overflow: hidden;
}

/* ── Sidebar overlay ──────────────────────────────── */
#sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 10; }

/* ── Sidebar ──────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
  transition: width .25s ease, transform .25s ease;
  z-index: 20; overflow: hidden;
  /* top safe area on desktop is 0, fine to always apply */
  padding-top: var(--sat);
}
#sidebar.collapsed { width: 54px; }
#sidebar.collapsed .sidebar-username,
#sidebar.collapsed .room-create,
#sidebar.collapsed .room-label,
#sidebar.collapsed .room-actions { display: none; }
#sidebar.collapsed .sidebar-header { padding: 10px 0; justify-content: center; cursor: default; }
#sidebar.collapsed #room-list li { padding: 13px 0; justify-content: center; }

.sidebar-header { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background .15s; flex-shrink: 0; }
.sidebar-header:hover { background: rgba(0,0,0,.035); }
.sidebar-username { flex: 1; font-weight: 600; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 0.85rem; font-weight: 700; flex-shrink: 0; text-transform: uppercase; }

.room-create { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.room-create input { flex: 1; min-width: 0; background: var(--input-bg); border: 1px solid var(--border); border-radius: 8px; color: var(--text); padding: 7px 10px; font-size: 0.88rem; -webkit-appearance: none; }
.room-create input:focus { outline: none; border-color: var(--accent); }
.room-create button { background: var(--accent); border: none; color: #fff; border-radius: 8px; padding: 7px 13px; cursor: pointer; font-size: 1.1rem; flex-shrink: 0; }

#room-list { list-style: none; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }

#room-list li {
  padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--border);
  transition: background .15s; font-size: 0.9rem; white-space: nowrap;
  overflow: hidden; display: flex; align-items: center; gap: 8px;
}
#room-list li .room-icon { flex-shrink: 0; color: var(--muted); }
#room-list li .room-label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
#room-list li:hover { background: var(--input-bg); }
#room-list li.active { background: var(--accent); color: #fff; }
#room-list li.active .room-icon { color: #fff; }

/* DM divider */
#room-list li.dm-divider {
  padding: 8px 14px 4px; font-size: 0.7rem; color: var(--muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; cursor: default;
  border-top: 1px solid var(--border); margin-top: 2px; background: none !important;
}

/* Unread badge */
.unread-badge {
  background: var(--online); color: #fff;
  border-radius: 10px; padding: 1px 7px;
  font-size: 0.7rem; font-weight: 700;
  flex-shrink: 0; min-width: 18px; text-align: center;
  line-height: 16px;
}

/* Room owner action buttons */
.room-actions { display: none; gap: 2px; flex-shrink: 0; }
#room-list li:hover .room-actions:not(.not-owner) { display: flex; }
.room-action-btn { background: none; border: none; cursor: pointer; padding: 4px 6px; font-size: 0.8rem; border-radius: 6px; color: var(--muted); line-height: 1; -webkit-tap-highlight-color: transparent; }
.room-action-btn:hover, .room-action-btn:active { background: var(--border); color: var(--text); }
.room-action-btn.del:hover, .room-action-btn.del:active { color: var(--danger); }

/* ── Chat area ────────────────────────────────────── */
#chat-area {
  flex: 1; display: flex; flex-direction: column;
  min-width: 0; max-width: 100%; overflow: hidden;
  height: 100%; position: relative;
}

#scroll-fab {
  position: absolute; right: 18px; bottom: 84px;
  width: 42px; height: 42px; border-radius: 50%;
  background: var(--accent); color: #fff; border: none;
  font-size: 1.1rem; cursor: pointer; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 14px rgba(0,0,0,.35);
  transition: background .15s, transform .15s;
}
#scroll-fab:hover { background: var(--accent-hover); transform: scale(1.05); }
#scroll-fab.hidden { display: none !important; }

#chat-header {
  padding: 12px 16px;
  /* push content below the notch/status bar */
  padding-top: calc(12px + var(--sat));
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
  font-weight: 600; flex-shrink: 0;
}
#room-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#menu-btn { display: none; font-size: 1.3rem; }

/* ── Connection banner ────────────────────────────── */
#connection-banner {
  background: #7c3a00;
  color: #fbbf24;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 6px 16px;
  flex-shrink: 0;
  letter-spacing: .02em;
}

.online-badge { background: rgba(74,222,128,.15); border: 1px solid var(--online); color: var(--online); border-radius: 20px; padding: 3px 10px; font-size: 0.75rem; font-weight: 600; cursor: pointer; white-space: nowrap; flex-shrink: 0; transition: background .15s; }
.online-badge:hover { background: rgba(74,222,128,.25); }

#online-panel { position: fixed; top: 54px; right: 12px; background: var(--sidebar-bg); border: 1px solid var(--border); border-radius: 12px; min-width: 180px; box-shadow: 0 8px 24px rgba(0,0,0,.4); z-index: 200; overflow: hidden; }
.online-panel-title { padding: 10px 14px 6px; font-size: 0.75rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
#online-list { list-style: none; padding-bottom: 8px; }
#online-list li { display: flex; align-items: center; gap: 8px; padding: 8px 14px; font-size: 0.88rem; cursor: pointer; transition: background .12s; }
#online-list li:hover { background: var(--input-bg); }
#online-list li::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: var(--online); flex-shrink: 0; }

/* Messages */
#messages {
  flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden;
  padding: 12px 16px 8px;
  display: flex; flex-direction: column; gap: 6px;
  -webkit-overflow-scrolling: touch;
}

/* ── Typing bar — bottom center above input ───────── */
#typing-bar {
  flex-shrink: 0;
  padding: 4px 16px 2px;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  font-size: 0.78rem; color: var(--success); font-weight: 500;
  min-height: 22px;
}
.typing-dots { display: flex; gap: 3px; align-items: center; }
.typing-dots span { width: 4px; height: 4px; border-radius: 50%; background: var(--success); animation: tdot 1.2s infinite ease-in-out; }
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes tdot { 0%,60%,100% { transform: translateY(0); opacity:.35; } 30% { transform: translateY(-3px); opacity:1; } }

/* ── Edit banner ──────────────────────────────────── */
#edit-banner { display: flex; align-items: center; gap: 8px; padding: 6px 14px; background: rgba(82,136,193,.12); border-top: 1px solid var(--accent); font-size: 0.82rem; color: var(--accent); flex-shrink: 0; }
#edit-banner button { margin-left: auto; background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1rem; padding: 2px 6px; }

/* ── Reply bar ────────────────────────────────────── */
#reply-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px;
  background: rgba(82,136,193,.08);
  border-top: 1px solid var(--accent);
  flex-shrink: 0;
}
.reply-bar-icon { color: var(--accent); font-size: 1rem; flex-shrink: 0; }
.reply-bar-body { flex: 1; min-width: 0; }
#reply-bar-user { display: block; font-size: 0.72rem; font-weight: 700; color: var(--accent); margin-bottom: 1px; }
#reply-bar-text { display: block; font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#reply-bar button { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1rem; padding: 4px 6px; }

/* ── Reply quote inside bubble ────────────────────── */
.reply-quote {
  display: flex; flex-direction: column; gap: 1px;
  border-left: 3px solid var(--accent);
  padding: 5px 8px; border-radius: 4px;
  background: rgba(0,0,0,.18); margin-bottom: 6px;
  cursor: pointer;
}
.reply-quote:hover { background: rgba(0,0,0,.28); }
.reply-quote-user { font-size: 0.7rem; font-weight: 700; color: var(--accent); }
.reply-quote-text { font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }

.msg-highlight .msg-bubble {
  animation: msg-highlight-flash 1.5s ease;
}
@keyframes msg-highlight-flash {
  0% { box-shadow: 0 0 0 2px var(--accent); }
  70% { box-shadow: 0 0 0 2px var(--accent); }
  100% { box-shadow: none; }
}

/* ── Input bar ────────────────────────────────────── */
#input-bar {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px;
  padding-bottom: max(10px, calc(var(--sab) + 8px));
  background: var(--header-bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
#msg-input { flex: 1; min-width: 0; background: var(--input-bg); border: 1px solid var(--border); border-radius: 20px; padding: 10px 16px; color: var(--text); font-size: 0.95rem; outline: none; -webkit-appearance: none; }
#msg-input:focus { border-color: var(--accent); }

.send-btn { background: var(--accent); border: none; color: #fff; border-radius: 50%; width: 42px; height: 42px; cursor: pointer; font-size: 1rem; display: flex; align-items: center; justify-content: center; flex-shrink: 0; -webkit-appearance: none; }
.icon-btn { background: none; border: none; color: var(--text); font-size: 1.2rem; cursor: pointer; padding: 6px; border-radius: 8px; transition: background .15s; display: flex; align-items: center; justify-content: center; flex-shrink: 0; -webkit-tap-highlight-color: transparent; -webkit-appearance: none; }
.icon-btn:hover { background: var(--input-bg); }

#record-btn { position: relative; }

/* ── Recording bar ────────────────────────────────── */
#recording-bar, #preview-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, calc(var(--sab) + 8px));
  background: var(--header-bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* Pulsing red dot */
.rec-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--danger); flex-shrink: 0;
  animation: pulse 1s infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .3; } }

.rec-timer { font-size: 0.88rem; color: var(--danger); font-weight: 600; font-variant-numeric: tabular-nums; min-width: 34px; }
.rec-stop-btn { color: var(--danger); }
.rec-delete-btn { color: var(--muted); }

/* Live waveform during recording */
.rec-waveform {
  flex: 1; display: flex; align-items: center; gap: 2px;
  height: 32px; overflow: hidden;
}
.rec-waveform .bar {
  width: 3px; border-radius: 2px; background: var(--danger);
  flex-shrink: 0; opacity: 0.7;
  transition: height 0.08s ease;
}

/* ── Voice player (sent messages) ────────────────── */
.voice-player {
  display: flex; align-items: center; gap: 8px;
  width: 220px; max-width: 100%; min-width: 0;
  padding: 4px 2px; box-sizing: border-box;
}
.voice-play-btn {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--accent); border: none; color: #fff;
  font-size: 0.9rem; cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
}
.voice-play-btn:hover { background: var(--accent-hover); }
.voice-play-btn.loading::after {
  content: ''; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Upload / download progress ───────────────────── */
.msg-uploading .msg-bubble { opacity: 0.75; }
.upload-overlay {
  position: absolute; left: 0; right: 0; bottom: 0; height: 4px;
  background: rgba(255,255,255,0.25); border-radius: 0 0 var(--radius) var(--radius); overflow: hidden;
}
.upload-progress-bar { height: 100%; background: var(--accent); transition: width .15s linear; }
.download-spinner {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 26px; height: 26px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.35); border-top-color: #fff;
  animation: spin 0.7s linear infinite; pointer-events: none;
}
.msg-upload-failed .msg-bubble { opacity: 0.85; }
.upload-retry {
  margin-top: 6px; font-size: 0.78rem; color: #f87171; cursor: pointer;
  text-decoration: underline; text-underline-offset: 2px;
}

.voice-waveform {
  flex: 1; position: relative; height: 32px;
  cursor: pointer; min-width: 0; overflow: hidden;
}
.voice-bars {
  display: flex; align-items: center; gap: 1px;
  height: 100%; position: relative; z-index: 1;
  overflow: hidden; width: 100%;
}
.voice-bars .bar {
  flex: 1 1 auto; width: 2px; max-width: 3px; border-radius: 2px;
  background: rgba(31,41,55,0.3); min-width: 1px;
}
.mine .voice-bars .bar { background: rgba(59,125,216,0.3); }
.theirs .voice-bars .bar { background: rgba(31,41,55,0.25); }
.voice-bars .bar.played { background: var(--accent); }
.mine .voice-bars .bar.played { background: var(--accent-hover); }

.voice-progress {
  position: absolute; top: 0; left: 0; height: 100%;
  width: 0%; pointer-events: none; z-index: 0;
}

.voice-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 1px; flex-shrink: 0; }
.voice-duration { font-size: 0.72rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.voice-speed-btn {
  font-size: 0.65rem; font-weight: 700; color: var(--accent);
  background: rgba(82,136,193,.15); border: 1px solid var(--accent);
  border-radius: 4px; padding: 1px 4px; cursor: pointer; line-height: 1.4;
  white-space: nowrap;
}
.voice-speed-btn:hover { background: rgba(82,136,193,.3); }

/* Preview bar waveform */
#preview-waveform .bar { background: var(--accent); opacity: 0.5; }
#preview-waveform .bar.played { opacity: 1; }

/* ── Message bubbles ──────────────────────────────── */
.msg-wrapper { display: flex; flex-direction: column; max-width: 72%; min-width: 0; }
.msg-wrapper.mine { align-self: flex-end; align-items: flex-end; }
.msg-wrapper.theirs { align-self: flex-start; align-items: flex-start; }
.msg-sender {
  font-size: 0.74rem; color: var(--accent); margin-bottom: 3px; padding: 0 4px;
  display: inline-flex; align-items: center; gap: 5px; font-weight: 600;
}
.msg-sender-avatar {
  display: inline-flex; align-items: center; justify-content: center;
  width: 18px; height: 18px; border-radius: 50%; font-size: 0.72rem; line-height: 1;
  background: rgba(59,125,216,0.16); flex-shrink: 0;
}
.msg-sender.clickable {
  cursor: pointer;
  padding: 3px 9px 3px 4px; border-radius: 20px; margin-left: 4px;
  background: rgba(59,125,216,0.1);
  transition: background .15s, transform .1s;
}
.msg-sender.clickable:hover { background: rgba(59,125,216,0.2); }
.msg-sender.clickable:active { transform: scale(0.96); background: rgba(59,125,216,0.28); }
.msg-bubble { position: relative; padding: 8px 12px; border-radius: var(--radius); font-size: 0.92rem; line-height: 1.45; word-break: break-word; overflow-wrap: break-word; max-width: 100%; }
.mine .msg-bubble { background: var(--mine-bg); border-bottom-right-radius: 4px; }
.theirs .msg-bubble { background: var(--msg-bg); border-bottom-left-radius: 4px; }
.msg-bubble img { max-width: 200px; max-height: 180px; border-radius: 8px; display: block; cursor: pointer; }
.msg-bubble audio { display: block; max-width: 220px; width: 100%; }
.msg-video { display: block; max-width: 260px; max-height: 220px; border-radius: 8px; background: #000; }
.music-player { display: flex; flex-direction: column; gap: 6px; min-width: 220px; max-width: 100%; }
.music-label { font-size: 0.82rem; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.music-audio { display: block; width: 100%; max-width: 240px; }
.file-link { display: flex; align-items: center; gap: 8px; color: #93c5fd; text-decoration: none; font-size: 0.88rem; }
.file-link:hover { text-decoration: underline; }
.edited-tag { font-size: 0.65rem; color: var(--muted); margin-left: 4px; }

/* ── Message footer ───────────────────────────────── */
.msg-footer { display: flex; align-items: center; gap: 4px; margin-top: 3px; padding: 0 4px; }
.msg-time { font-size: 0.68rem; color: var(--muted); flex-shrink: 0; }
.msg-status { display: inline-flex; align-items: center; color: var(--muted); flex-shrink: 0; }
.msg-status .ticks { width: 15px; height: 10px; }
.msg-status.seen { color: #4fc3f7; }
.msg-action-btn { background: none; border: none; font-size: 0.75rem; cursor: pointer; padding: 2px 5px; border-radius: 5px; color: var(--muted); line-height: 1; opacity: 0.45; transition: opacity .15s, background .15s; flex-shrink: 0; }
.msg-action-btn:hover, .msg-wrapper:hover .msg-action-btn { opacity: 1; }
.msg-action-btn.delete:hover { color: var(--danger); }
.react-btn { background: none; border: none; font-size: 0.75rem; cursor: pointer; padding: 2px 5px; border-radius: 5px; color: var(--muted); line-height: 1; opacity: 0.45; transition: opacity .15s; }
.msg-wrapper:hover .react-btn { opacity: 1; }

.reactions-row { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction-chip { background: var(--input-bg); border: 1px solid var(--border); border-radius: 20px; padding: 2px 8px; font-size: 0.82rem; cursor: pointer; display: flex; align-items: center; gap: 3px; transition: background .15s; user-select: none; }
.reaction-chip:hover { background: var(--border); }
.reaction-chip.mine { border-color: var(--accent); background: rgba(82,136,193,.15); }
.reaction-chip .count { font-size: 0.75rem; color: var(--muted); }

/* ── Context menu ─────────────────────────────────── */
#ctx-menu { position: fixed; background: var(--sidebar-bg); border: 1px solid var(--border); border-radius: 10px; z-index: 200; box-shadow: 0 6px 20px rgba(0,0,0,.5); min-width: 160px; overflow: hidden; }
#ctx-menu button { display: flex; align-items: center; gap: 10px; width: 100%; padding: 13px 16px; background: none; border: none; color: var(--text); font-size: 0.9rem; cursor: pointer; text-align: left; transition: background .12s; }
#ctx-menu button:hover { background: var(--input-bg); }
#ctx-menu button.danger { color: var(--danger); }
#ctx-menu hr { border: none; border-top: 1px solid var(--border); margin: 0; }

/* ── Emoji picker ─────────────────────────────────── */
#emoji-picker { position: fixed; background: var(--sidebar-bg); border: 1px solid var(--border); border-radius: 12px; padding: 10px; z-index: 300; box-shadow: 0 8px 24px rgba(0,0,0,.4); width: 240px; }
#emoji-list { display: flex; flex-wrap: wrap; gap: 4px; }
#emoji-list span { font-size: 1.4rem; cursor: pointer; padding: 4px; border-radius: 6px; line-height: 1; transition: background .1s; }
#emoji-list span:hover { background: var(--input-bg); }

/* ── Profile modal ────────────────────────────────── */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 500; display: flex; align-items: flex-start; justify-content: center; padding: 16px; padding-bottom: max(16px, var(--sab)); overflow-y: auto; }
.modal-card { background: var(--sidebar-bg); border: 1px solid var(--border); border-radius: 20px; width: 100%; max-width: 400px; box-shadow: 0 20px 60px rgba(0,0,0,.5); overflow: hidden; margin: auto; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.modal-header h2 { font-size: 1.05rem; font-weight: 700; }
.modal-section { padding: 20px; border-top: 1px solid var(--border); }
.profile-avatar-big { width: 72px; height: 72px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 1.8rem; font-weight: 700; text-transform: uppercase; margin: 20px auto 8px; }
.profile-name { text-align: center; font-weight: 600; font-size: 1rem; margin-bottom: 4px; }

/* ── Lightbox ────────────────────────────────────── */
#lightbox {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.92);
  display: flex; align-items: center; justify-content: center;
  z-index: 2000; cursor: zoom-out;
}
#lightbox.hidden { display: none !important; }
#lightbox img {
  max-width: 92vw; max-height: 92vh;
  object-fit: contain; border-radius: 6px;
  cursor: default; box-shadow: 0 8px 40px rgba(0,0,0,.6);
  transition: transform .05s linear; touch-action: none;
}
.lightbox-close, .lightbox-download {
  position: fixed; top: 16px; right: 16px;
  background: rgba(255,255,255,.15); border: none; color: #fff;
  font-size: 18px; border-radius: 50%; width: 38px; height: 38px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background .15s; z-index: 2001;
}
.lightbox-download { right: 64px; }
.lightbox-close:hover, .lightbox-download:hover { background: rgba(255,255,255,.25); }

/* ── My Rooms in profile ─────────────────────────── */
.my-rooms-header {
  font-size: 0.72rem; color: var(--muted); font-weight: 700;
  text-transform: uppercase; letter-spacing: .05em; margin-bottom: 10px;
}

/* ── App update section ──────────────────────────── */
.update-hint { font-size: 0.82rem; color: var(--muted); margin: 0 0 10px; }
.update-btn { display: block; text-align: center; text-decoration: none; box-sizing: border-box; }
#my-rooms-list { list-style: none; padding: 0; margin: 0; }
.my-room-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 0; border-bottom: 1px solid var(--border);
}
.my-room-item:last-child { border-bottom: none; }
.my-room-name { flex: 1; color: var(--text); font-size: 0.9rem; }
.my-room-empty { color: var(--muted); font-size: 0.85rem; padding: 6px 0; }

/* ── System messages ──────────────────────────────── */
.system-msg { text-align: center; font-size: 0.75rem; color: var(--muted); padding: 4px; }

/* ── Scrollbar ────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }

/* ── Mobile ───────────────────────────────────────── */
@media (max-width: 640px) {
  #menu-btn { display: flex; }

  #sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: var(--sidebar-w) !important;
    transform: translateX(-100%);
    padding-top: var(--sat);
  }
  #sidebar.open { transform: translateX(0); box-shadow: 4px 0 20px rgba(0,0,0,.5); }
  #sidebar.collapsed { transform: translateX(-100%); }
  #sidebar-overlay.open { display: block; }

  /* Room owner buttons: always visible on mobile (hover doesn't work on touch) */
  .room-actions:not(.not-owner) { display: flex !important; }

  .msg-wrapper { max-width: 88%; }
  .msg-bubble img { max-width: 160px; }
  .msg-bubble audio { max-width: 100%; }
  #emoji-picker { width: 210px; }

  .msg-action-btn, .react-btn { opacity: 0.7; }

  .auth-card { padding: 28px 18px; }
}

/* Landscape mobile: prevent horizontal scroll */
@media (max-height: 500px) and (max-width: 900px) {
  .msg-bubble audio { max-width: 160px; }
}

/* ── Emoji avatar picker ─────────────────────────── */
#avatar-emoji-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.avatar-emoji-cell {
  width: 42px; height: 42px; border-radius: 10px;
  background: var(--input-bg); border: 2px solid transparent;
  font-size: 1.35rem; cursor: pointer; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  transition: border-color .15s, background .15s;
}
.avatar-emoji-cell:hover { background: rgba(59,125,216,0.1); }
.avatar-emoji-cell.active { border-color: var(--accent); background: rgba(59,125,216,0.12); }
.remove-avatar {
  display: block; margin: 12px auto 0; background: none; border: none;
  color: var(--danger); font-size: 0.82rem; font-weight: 600; cursor: pointer;
}
.emoji-avatar { font-size: 1.3rem !important; }
.profile-avatar-big.emoji-avatar { font-size: 2.6rem !important; }


/* ── Search results / forward / invites / links ───── */
#search-results { border-bottom: 1px solid var(--border); max-height: 240px; overflow-y: auto; background: var(--sidebar-bg); }
.search-result { display: flex; align-items: center; gap: 8px; padding: 9px 14px; cursor: pointer; font-size: 0.88rem; border-bottom: 1px solid var(--border); }
.search-result:hover { background: var(--input-bg); }
.search-result b { flex: 1; font-weight: 600; }
.search-result em { color: var(--accent); font-style: normal; font-size: 0.75rem; font-weight: 700; }
.search-empty { color: var(--muted); font-size: 0.85rem; padding: 12px; text-align: center; }

.forward-list { list-style: none; max-height: 320px; overflow-y: auto; padding: 8px 0; }
.forward-list li { padding: 11px 20px; cursor: pointer; font-size: 0.92rem; }
.forward-list li:hover { background: var(--input-bg); }

.forwarded-label { font-size: 0.72rem; color: var(--muted); font-style: italic; margin-bottom: 4px; }

.msg-link { color: var(--accent); text-decoration: underline; word-break: break-all; }

.invite-card { display: flex; flex-direction: column; gap: 6px; min-width: 200px; }
.invite-title { font-weight: 700; font-size: 0.88rem; }
.invite-text { color: var(--muted); font-size: 0.84rem; }
.invite-join-btn { background: var(--accent); color: #fff; border: none; border-radius: 8px; padding: 8px; font-weight: 700; cursor: pointer; margin-top: 4px; }
.invite-join-btn:hover { background: var(--accent-hover); }

.privacy-check { display: flex; gap: 10px; align-items: flex-start; margin: 6px 0 14px; cursor: pointer; font-size: 0.88rem; }
.privacy-check small { color: var(--muted); }

.avatar-emoji-cell.more-toggle { color: var(--muted); font-weight: 700; }

#room-link-btn { font-size: 0.95rem; }


/* ── Room dashboard ──────────────────────────────── */
.room-meta-row { display: flex; justify-content: space-between; padding: 7px 0; font-size: 0.9rem; border-bottom: 1px solid var(--border); }
.room-meta-row:last-child { border-bottom: none; }
.room-meta-label { color: var(--muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: .04em; }
.room-link-row { display: flex; align-items: center; gap: 8px; }
.room-link-row code { flex: 1; background: var(--input-bg); border-radius: 8px; padding: 9px 10px; font-size: 0.8rem; color: var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-members-list { list-style: none; max-height: 200px; overflow-y: auto; }
.room-members-list li { padding: 8px 4px; font-size: 0.9rem; border-bottom: 1px solid var(--border); }
.room-members-list li:last-child { border-bottom: none; }
.room-invite-row { display: flex; gap: 8px; }
.room-invite-row input { flex: 1; min-width: 0; padding: 10px 12px; background: var(--input-bg); border: 1px solid var(--border); border-radius: 10px; color: var(--text); font-size: 0.9rem; }
.room-invite-row .invite-send-btn { width: auto; margin-top: 0; padding: 10px 18px; }
