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

[hidden] { display: none !important; }

/* ================================================================
   Telegram-style theme. Default follows the OS colour scheme.
   A manual choice can be stored in localStorage (data-theme=light|dark)
   and always wins when present.
   ================================================================ */
:root {
  --nav: #0e2233;
  --nav-2: #123046;
  --accent: #3a5a8c;
  --accent-dark: #31507f;
  --accent-soft: #e8f0f7;
  --active-chat: #eef3f9;
  --btn-primary: #0044aa;
  --sidebar-bg: #ffffff;
  --sidebar-hover: #f1f5f9;
  --chat-bg-top: #dfe7f0;
  --chat-bg-bottom: #c9d5e6;
  --bubble-in: #ffffff;
  --bubble-out-1: #5f86b3;
  --bubble-out-2: #3a5a8c;
  --bubble-out-text: #ffffff;
  --text: #0f1419;
  --text-secondary: #5f6672;
  --text-muted: #8b91a0;
  --border: #e4e7eb;
  --border-strong: #d4d9e0;
  --danger: #c0392b;
  --danger-soft: #fdeceb;
  --overlay: rgba(14, 34, 51, 0.5);
  --chip-bg: rgba(0, 0, 0, 0.07);
  --chip-text: #0f1419;
  --tick: rgba(255, 255, 255, 0.85);
  --tick-read: #cde8ff;
  --shadow-list: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 30px rgba(14, 34, 51, 0.35);
  --shadow-menu: 0 4px 20px rgba(0, 0, 0, 0.22);
  --input-bg: #f1f3f5;
  --menu-bg: #ffffff;
  --unread-pill: #0a7d4f;
  --scrollbar: rgba(0, 0, 0, 0.18);
  --modal-bg: #ffffff;
  --avatar-border: rgba(0, 0, 0, 0.08);
  --notification-bg: #fff5e0;
  --notification-text: #7a5b12;
  --notification-accent: #c68a08;
  --hover-border: transparent;
}

html[data-theme="light"] {
  --nav: #0e2233;
  --nav-2: #123046;
  --accent: #3a5a8c;
  --accent-dark: #31507f;
  --accent-soft: #e8f0f7;
  --active-chat: #eef3f9;
  --btn-primary: #0044aa;
  --sidebar-bg: #ffffff;
  --sidebar-hover: #f1f5f9;
  --chat-bg-top: #dfe7f0;
  --chat-bg-bottom: #c9d5e6;
  --bubble-in: #ffffff;
  --bubble-out-1: #5f86b3;
  --bubble-out-2: #3a5a8c;
  --bubble-out-text: #ffffff;
  --text: #0f1419;
  --text-secondary: #5f6672;
  --text-muted: #8b91a0;
  --border: #e4e7eb;
  --border-strong: #d4d9e0;
  --danger: #c0392b;
  --danger-soft: #fdeceb;
  --overlay: rgba(14, 34, 51, 0.5);
  --chip-bg: rgba(0, 0, 0, 0.07);
  --chip-text: #0f1419;
  --tick: rgba(255, 255, 255, 0.85);
  --tick-read: #cde8ff;
  --input-bg: #f1f3f5;
  --menu-bg: #ffffff;
  --modal-bg: #ffffff;
  --scrollbar: rgba(0, 0, 0, 0.18);
  --notification-bg: #fff5e0;
  --notification-text: #7a5b12;
  --notification-accent: #c68a08;
  --unread-pill: #0a7d4f;
}

html[data-theme="dark"] {
  --nav: #0a1a28;
  --nav-2: #0e2233;
  --accent: #5b80ab;
  --accent-dark: #6c92bd;
  --accent-soft: #1c3a54;
  --active-chat: #202b36;
  --btn-primary: #3f7ecd;
  --sidebar-bg: #17212b;
  --sidebar-hover: #202b36;
  --chat-bg-top: #122232;
  --chat-bg-bottom: #0e1621;
  --bubble-in: #182533;
  --bubble-out-1: #5b80ab;
  --bubble-out-2: #3a5a8c;
  --bubble-out-text: #ffffff;
  --text: #f5f5f5;
  --text-secondary: #8696a0;
  --text-muted: #70828e;
  --border: #0f1720;
  --border-strong: #263442;
  --danger: #ff5c4d;
  --danger-soft: #3a221f;
  --overlay: rgba(0, 0, 0, 0.55);
  --chip-bg: rgba(0, 0, 0, 0.25);
  --chip-text: #e4e9ee;
  --tick: rgba(255, 255, 255, 0.85);
  --tick-read: #7fb8f2;
  --input-bg: #202b36;
  --menu-bg: #1d2733;
  --modal-bg: #1d2733;
  --scrollbar: rgba(255, 255, 255, 0.25);
  --notification-bg: #332b12;
  --notification-text: #e8d9a8;
  --notification-accent: #d7a92e;
  --avatar-border: rgba(255, 255, 255, 0.08);
  --shadow-lg: 0 6px 24px rgba(0, 0, 0, 0.55);
  --shadow-menu: 0 4px 24px rgba(0, 0, 0, 0.6);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --nav: #0a1a28;
    --nav-2: #0e2233;
    --accent: #5b80ab;
    --accent-dark: #6c92bd;
    --accent-soft: #1c3a54;
    --active-chat: #202b36;
    --btn-primary: #3f7ecd;
    --sidebar-bg: #17212b;
    --sidebar-hover: #202b36;
    --chat-bg-top: #122232;
    --chat-bg-bottom: #0e1621;
    --bubble-in: #182533;
    --bubble-out-1: #5b80ab;
    --bubble-out-2: #3a5a8c;
    --bubble-out-text: #ffffff;
    --text: #f5f5f5;
    --text-secondary: #8696a0;
    --text-muted: #70828e;
    --border: #0f1720;
    --border-strong: #263442;
    --danger: #ff5c4d;
    --danger-soft: #3a221f;
    --overlay: rgba(0, 0, 0, 0.55);
    --chip-bg: rgba(0, 0, 0, 0.25);
    --chip-text: #e4e9ee;
    --tick: rgba(255, 255, 255, 0.85);
    --tick-read: #7fb8f2;
    --input-bg: #202b36;
    --menu-bg: #1d2733;
    --modal-bg: #1d2733;
    --scrollbar: rgba(255, 255, 255, 0.25);
    --notification-bg: #332b12;
    --notification-text: #e8d9a8;
    --notification-accent: #d7a92e;
    --avatar-border: rgba(255, 255, 255, 0.08);
    --shadow-lg: 0 6px 24px rgba(0, 0, 0, 0.55);
    --shadow-menu: 0 4px 24px rgba(0, 0, 0, 0.6);
  }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: var(--nav);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* WTT Capture-style top bar */
#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; background: var(--nav); }

.topbar {
  position: relative;
  z-index: 30;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: #ffffff;
  color: #16222e;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.12);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand .brand-logo { height: 32px; width: auto; display: block; }
.brand-name { font-weight: 700; font-size: 15px; line-height: 1.1; white-space: nowrap; }
.brand-name small { display: block; font-weight: 500; font-size: 11px; color: #6b7683; }
.user { display: flex; align-items: center; gap: 10px; }
.user-name { font-size: 13px; color: #16222e; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.btn-link { background: none; border: none; color: #0044aa; font-size: 13px; font-weight: 700; padding: 6px; cursor: pointer; }
.btn-link:active { opacity: 0.7; }

/* ============ shared bits ============ */
.flex { display: flex; align-items: center; }
.spacer { flex: 1; }

.btn {
  padding: 12px 22px;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
  transition: background 0.15s, opacity 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { opacity: 0.8; }
.btn-primary { background: var(--btn-primary); color: #fff; }
.btn-primary:hover { background: var(--accent); }
.btn-primary:disabled { background: var(--text-muted); cursor: not-allowed; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(0.92); }
.btn-ghost { background: var(--input-bg); color: var(--text); }
.btn-ghost:hover { background: var(--border-strong); }
.btn-block { width: 100%; }
.btn-sm { padding: 7px 14px; font-size: 13px; }

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  color: var(--text-secondary);
  border-radius: 50%;
  -webkit-tap-highlight-color: transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.btn-icon:hover { background: var(--sidebar-hover); }

.form-label { display: block; font-size: 13px; font-weight: 600; color: var(--text-secondary); margin-bottom: 6px; }
.form-input {
  width: 100%; padding: 10px 14px; border: 1px solid var(--border-strong); border-radius: 8px;
  font-size: 15px; margin-bottom: 12px; outline: none; background: var(--modal-bg); color: var(--text);
  transition: border-color 0.15s;
}
.form-input:focus { border-color: var(--accent); }
.error-msg { color: var(--danger); font-size: 13px; margin: 8px 0; padding: 8px 12px; background: var(--danger-soft); border-radius: 8px; }

/* ============ auth ============ */
#auth-screen {
  display: flex; align-items: center; justify-content: center;
  height: 100vh; height: 100dvh;
  background: linear-gradient(160deg, #3a5a8c 0%, #5b80ab 100%);
  padding: 16px;
}
.auth-card {
  background: var(--modal-bg); color: var(--text);
  border-radius: 18px; padding: 28px; width: 100%; max-width: 400px;
  box-shadow: var(--shadow-lg);
}
.auth-logo { text-align: center; margin-bottom: 24px; }
.auth-logo .auth-logo-img { max-width: 120px; max-height: 110px; object-fit: contain; display: block; margin: 0 auto 10px; }
.auth-logo h2 { font-size: 22px; margin-bottom: 4px; }
.auth-logo p { color: var(--text-muted); font-size: 14px; }
#auth-tabs { display: flex; gap: 4px; margin-bottom: 20px; background: var(--input-bg); border-radius: 8px; padding: 4px; }
.auth-tab {
  flex: 1; padding: 10px; border: none; background: none; border-radius: 6px; cursor: pointer;
  font-size: 15px; font-weight: 500; color: var(--text-secondary); transition: all 0.15s;
}
.auth-tab.active { background: var(--menu-bg); color: var(--accent); box-shadow: var(--shadow-list); }
#login-form input, #register-form input {
  width: 100%; padding: 12px 14px; border: 1px solid var(--border-strong); border-radius: 8px; font-size: 16px;
  margin-bottom: 10px; outline: none; background: var(--modal-bg); color: var(--text); -webkit-appearance: none;
  transition: border-color 0.15s;
}
#login-form input:focus, #register-form input:focus { border-color: var(--accent); }

/* ============ layout ============ */
.tg { flex: 1; display: flex; min-height: 0; position: relative; }

.sidebar {
  width: 100%; background: var(--sidebar-bg); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; z-index: 5;
}
.sidebar.hidden { display: none; }

.sb-top { padding: 10px 12px 6px; position: relative; }
.sb-actions { display: flex; align-items: center; gap: 4px; padding: 4px 4px 8px; }
.sb-brand { font-weight: 700; font-size: 18px; letter-spacing: 0.5px; color: var(--accent); margin-left: 2px; }
.sb-flex { flex: 1; }
.sb-more { position: relative; display: flex; }
.sb-dropdown {
  position: absolute; top: calc(100% + 6px); right: 0; background: var(--menu-bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-menu);
  padding: 6px; z-index: 80; min-width: 200px;
}
.sb-opt {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px 12px;
  border: none; background: none; cursor: pointer; font-size: 14px; color: var(--text);
  border-radius: 8px; text-align: left; font-weight: 500;
}
.sb-opt:hover { background: var(--sidebar-hover); }
.sb-opt svg { flex-shrink: 0; color: var(--text-secondary); }

.search-pill {
  display: flex; align-items: center; gap: 8px; background: var(--input-bg);
  border-radius: 22px; padding: 7px 14px; margin-bottom: 8px; color: var(--text-muted);
}
.search-pill input {
  flex: 1; border: none; background: none; outline: none; font-size: 14px; color: var(--text); min-width: 0;
}
.search-pill input::placeholder { color: var(--text-muted); }

.filter-tabs { display: flex; gap: 2px; padding: 2px 0 4px; }
.ftab {
  flex: 1; padding: 6px 4px; border: none; background: none; cursor: pointer; font-size: 13px; font-weight: 500;
  color: var(--text-secondary); border-radius: 6px; position: relative; transition: color 0.15s;
}
.ftab:hover { background: var(--sidebar-hover); }
.ftab.active { color: var(--accent); }
.ftab.active::after {
  content: ''; position: absolute; left: 50%; transform: translateX(-50%); bottom: -4px;
  width: 18px; height: 3px; border-radius: 2px; background: var(--accent);
}

.conv-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 6px 10px; scrollbar-width: thin; scrollbar-color: var(--scrollbar) transparent; }
.conv-list::-webkit-scrollbar { width: 6px; }
.conv-list::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 3px; }

.list-section-label {
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.4px;
  color: var(--text-muted); padding: 10px 12px 4px;
}

.conv-item {
  display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 10px; cursor: pointer;
  transition: background 0.12s; -webkit-tap-highlight-color: transparent; position: relative;
}
.conv-item:hover { background: var(--sidebar-hover); }
.conv-item.active { background: var(--active-chat); }

.ci-avatar { position: relative; flex-shrink: 0; }
.ci-avatar .online-dot {
  position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--sidebar-bg); background: #ccc;
}
.ci-avatar .online-dot.on { background: #4cae4f; }

.ci-main { flex: 1; min-width: 0; }
.ci-row1 { display: flex; align-items: center; gap: 6px; }
.ci-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.ci-time { font-size: 11px; color: var(--text-muted); flex-shrink: 0; }
.ci-row2 { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.ci-preview {
  flex: 1; font-size: 13px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ci-preview .ci-typing { color: var(--accent); }
.ci-sender { color: var(--accent); font-weight: 500; }
.ci-badges { display: flex; align-items: center; gap: 5px; flex-shrink: 0; }
.ci-pin, .ci-mute, .ci-ch-silent { font-size: 12px; color: var(--text-muted); }
.unread-badge {
  background: var(--unread-pill); color: #fff; font-size: 11px; font-weight: 700; padding: 1px 7px;
  border-radius: 12px; min-width: 20px; text-align: center; line-height: 1.6;
}

.empty-state-hint { padding: 30px 16px; text-align: center; color: var(--text-muted); font-size: 14px; }

/* ============ chat area ============ */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative;
  background: linear-gradient(180deg, var(--chat-bg-top), var(--chat-bg-bottom)); }

.chat-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-muted); gap: 10px; text-align: center; padding: 30px; }
.chat-empty h2 { color: var(--text-secondary); font-size: 26px; }
.tb-wallpaper { position: relative; width: 140px; height: 100px; opacity: 0.5; }
.tb-bubble { position: absolute; width: 60px; height: 34px; background: var(--bubble-in); border-radius: 12px; box-shadow: var(--shadow-list); }
.tb-b2 { left: 46%; top: 18px; width: 70px; }
.tb-b3 { left: 22%; top: 42px; width: 52px; }

.chat-open { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.chat-header {
  padding: 8px 12px; background: var(--sidebar-bg); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px; min-height: 58px; z-index: 3;
}
.ch-header-main { flex: 1; min-width: 0; cursor: pointer; display: flex; align-items: center; gap: 10px; }
.ch-back { display: none; }
.ch-text { min-width: 0; }
.ch-text .ch-title, #chat-title .ch-title { font-weight: 700; font-size: 15px; }
.ch-subtitle { font-size: 12.5px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch-subtitle { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }

.messages {
  flex: 1; overflow-y: auto; padding: 12px 16px 4px; display: flex; flex-direction: column; gap: 2px;
  -webkit-overflow-scrolling: touch; scrollbar-width: thin; scrollbar-color: var(--scrollbar) transparent;
}
.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 3px; }

.day-chip { align-self: center; background: var(--chip-bg); color: var(--chip-text); font-size: 12px; font-weight: 500; padding: 4px 12px; border-radius: 14px; margin: 8px 0 6px; }

.typing-bar { padding: 2px 18px 4px; font-size: 13px; color: var(--accent); font-style: italic; min-height: 22px; }

.composer { background: var(--sidebar-bg); border-top: 1px solid var(--border); padding-bottom: 6px; position: relative; }
.composer-row { display: flex; align-items: center; gap: 4px; padding: 6px 10px 8px; }
.composer input[type="text"] {
  flex: 1; padding: 11px 16px; border: none; border-radius: 22px; background: var(--input-bg);
  outline: none; font-size: 15px; color: var(--text); min-width: 0;
}
.composer input[type="text"]::placeholder { color: var(--text-muted); }
.c-btn { color: var(--text-muted); padding: 6px; }
.send-btn { color: var(--btn-primary); }

.reply-preview {
  display: flex; align-items: center; gap: 8px; padding: 8px 14px 2px; font-size: 13px;
  border-top: 2px solid var(--accent);
}
.reply-preview .rp-label { color: var(--accent); font-weight: 600; white-space: nowrap; }
.reply-preview .rp-text { color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.reply-preview .rp-close { border: none; background: none; cursor: pointer; color: var(--text-muted); font-size: 18px; padding: 2px 6px; }

/* edit mode pill */
.edit-pill { padding: 8px 14px 2px; font-size: 12px; color: var(--accent); font-weight: 600; }

/* ============ message bubbles ============ */
.msg-wrap { display: flex; margin: 1px 0; }
.msg-wrap.mine { justify-content: flex-end; }
.msg-wrap.other { justify-content: flex-start; }

.bubble {
  max-width: min(84%, 480px); padding: 6px 9px 5px; border-radius: 13px; font-size: 15px;
  word-wrap: break-word; line-height: 1.4; position: relative;
}
.mine .bubble {
  background: linear-gradient(145deg, var(--bubble-out-1), var(--bubble-out-2));
  color: var(--bubble-out-text); border-top-right-radius: 3px;
}
.other .bubble { background: var(--bubble-in); color: var(--text); border-top-left-radius: 3px; box-shadow: var(--shadow-list); }

.msg-sender { font-size: 13px; font-weight: 600; margin-bottom: 1px; }
.msg-sender.mine-s .msg-sender { color: #cfe0f5; }
.forwarded { font-size: 13px; font-weight: 600; color: var(--accent); margin-bottom: 1px; }

.reply-quote { display: block; border-left: 3px solid var(--accent); padding: 3px 8px; border-radius: 4px; margin: 2px 0 4px; background: rgba(0, 0, 0, 0.07); cursor: pointer; min-width: 0; }
.other .reply-quote { background: rgba(0, 0, 0, 0.05); }
.mine .reply-quote { background: rgba(255, 255, 255, 0.12); }
.rq-name { font-weight: 600; font-size: 12px; color: var(--accent); }
.mine .rq-name { color: #e9f2ff; }
.rq-text { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px; }

.msg-body { font-size: 15px; word-break: break-word; white-space: pre-wrap; }
.msg-body a { color: #42a5f5; text-decoration: underline; }
.mine .msg-body a { color: #ffffff; }

.msg-media img { max-width: 260px; max-height: 260px; border-radius: 8px; cursor: pointer; display: block; }
.msg-media .file-card {
  display: flex; align-items: center; gap: 10px; padding: 8px; background: rgba(0,0,0,0.07); border-radius: 8px; min-width: 200px; cursor: pointer;
}
.mine .msg-media .file-card { background: rgba(255,255,255,0.14); }
.msg-media .file-card .fc-icon { font-size: 26px; }
.msg-media .file-card .fc-name { font-size: 13px; font-weight: 600; word-break: break-all; }

.msg-footer { display: flex; align-items: center; justify-content: flex-end; gap: 3px; margin-top: 1px; }
.msg-time { font-size: 11px; }
.mine .msg-time { color: var(--tick); }
.other .msg-time { color: var(--text-muted); }
.edited { font-size: 10px; font-style: italic; opacity: 0.75; }

.ticks { display: inline-flex; align-items: center; margin-left: 2px; }
.tick { width: 14px; height: 10px; }
.tick.t1 { margin-left: -7px; opacity: 0.9; }
.tick.read { color: var(--tick-read); }
.mine .tick { color: var(--tick); }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
.reaction {
  display: inline-flex; align-items: center; gap: 4px; font-size: 13px; padding: 2px 8px; border-radius: 12px;
  background: var(--bubble-in); border: 1px solid var(--border-strong); cursor: pointer; color: var(--text);
  box-shadow: var(--shadow-list); user-select: none;
}
.mine .reaction { background: rgba(255,255,255,0.92); }
.reaction.me { border-color: var(--accent); }
.reaction .r-count { font-size: 12px; color: var(--text-muted); }
.reaction.add { border-style: dashed; color: var(--text-muted); background: none; }

.notification-wrap { display: flex; justify-content: center; margin: 4px 0; }
.notification-box {
  max-width: 96%; background: var(--notification-bg); color: var(--notification-text);
  border: 1px solid var(--notification-accent); border-left: 4px solid var(--notification-accent);
  border-radius: 10px; padding: 8px 12px; font-size: 13px; line-height: 1.45;
}
.notification-box .nb-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; color: var(--notification-accent); margin-bottom: 4px; }
.notification-box .nb-body { white-space: pre-wrap; }

/* ============ emoji popover ============ */
#emoji-pop {
  position: absolute; right: 12px; bottom: 64px; background: var(--menu-bg); color: var(--text);
  border-radius: 14px; box-shadow: var(--shadow-menu); border: 1px solid var(--border);
  padding: 10px; display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; z-index: 60;
  max-width: 280px;
}
.emoji-cell { font-size: 22px; padding: 4px; border: none; background: none; cursor: pointer; border-radius: 8px; transition: background 0.1s; }
.emoji-cell:hover { background: var(--sidebar-hover); }

#reaction-picker {
  position: absolute; right: 12px; bottom: 64px; background: var(--menu-bg); color: var(--text);
  border-radius: 14px; box-shadow: var(--shadow-menu); border: 1px solid var(--border);
  padding: 8px; display: flex; flex-wrap: wrap; gap: 2px; z-index: 60; max-width: 280px;
}
.re-opt { font-size: 22px; padding: 5px; border: none; background: none; cursor: pointer; border-radius: 8px; }
.re-opt:hover { background: var(--sidebar-hover); }

/* ============ context menu ============ */
#ctx-menu {
  position: fixed; background: var(--menu-bg); color: var(--text); border-radius: 12px;
  box-shadow: var(--shadow-menu); border: 1px solid var(--border); padding: 6px; z-index: 3000; min-width: 190px;
}
.ctx-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 12px; border: none; background: none; cursor: pointer; font-size: 14px; color: var(--text); border-radius: 8px; text-align: left; }
.ctx-item:hover { background: var(--sidebar-hover); }
.ctx-item.danger { color: var(--danger); }
.ctx-item svg { flex-shrink: 0; }
.ctx-sep { height: 1px; background: var(--border-strong); margin: 5px 8px; }
.ctx-reactions { display: flex; gap: 2px; padding: 4px 6px; }
.ctx-emoji { font-size: 20px; padding: 6px; border: none; background: none; cursor: pointer; border-radius: 8px; }
.ctx-emoji:hover { background: var(--sidebar-hover); }

/* ============ modal ============ */
#modal-overlay {
  position: fixed; inset: 0; background: var(--overlay); display: flex; align-items: flex-end; justify-content: center; z-index: 1000;
}
.modal {
  background: var(--modal-bg); color: var(--text); border-radius: 18px 18px 0 0; width: 100%; max-width: 720px;
  max-height: 94vh; overflow-y: auto; -webkit-overflow-scrolling: touch; animation: slideUp 0.2s ease; scrollbar-width: thin;
  padding: 0 4px;
}
.modal.modal-wide { max-width: 940px; }
.modal-wide .modal-body { padding: 32px 40px; }
.modal-wide .usr-row { padding: 14px; }
.modal-wide .nst-results { max-height: 420px; }
@keyframes slideUp { from { transform: translateY(60px); opacity: 0.6; } to { transform: translateY(0); opacity: 1; } }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 20px 28px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--modal-bg); z-index: 1; }
.modal-header h3 { font-size: 20px; }
.modal-close { background: none; border: none; font-size: 26px; cursor: pointer; color: var(--text-muted); padding: 4px; }
.modal-body { padding: 28px; }

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

.tabs-row { display: flex; gap: 6px; margin-bottom: 16px; }
.tab-pill {
  flex: 1; padding: 8px; border: 1px solid var(--border-strong); background: none; border-radius: 8px; cursor: pointer;
  font-size: 13px; font-weight: 600; color: var(--text-secondary); transition: all 0.15s;
}
.tab-pill.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.user-list { max-height: 320px; overflow-y: auto; border: 1px solid var(--border-strong); border-radius: 10px; margin-bottom: 12px; }
.user-row { display: flex; align-items: center; gap: 10px; padding: 10px; cursor: pointer; border-bottom: 1px solid var(--border); }
.user-row:last-child { border-bottom: none; }
.user-row:hover { background: var(--sidebar-hover); }
.user-row.sel { background: var(--accent-soft); }
.user-row .uinfo { flex: 1; min-width: 0; }
.user-row .uname { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-row .uname-sub { font-size: 13px; color: var(--text-muted); }
.user-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

.member-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.member-row:last-child { border-bottom: none; }
.member-row .uinfo { flex: 1; min-width: 0; }
.member-row .uname { font-weight: 600; font-size: 14px; }
.member-row .role-badge { font-size: 11px; font-weight: 600; color: var(--accent); background: var(--accent-soft); padding: 2px 8px; border-radius: 10px; margin-left: 6px; }

.info-section { margin-bottom: 18px; }
.info-section > strong { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.5px; }

.toggle-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); cursor: pointer; }
.toggle-row:last-child { border-bottom: none; }
.toggle-row .tl { flex: 1; font-size: 15px; }
.toggle-row .tsub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.switch { position: relative; width: 44px; height: 26px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; background: var(--border-strong); border-radius: 26px; transition: background 0.15s; cursor: pointer; }
.slider::before { content: ''; position: absolute; width: 20px; height: 20px; border-radius: 50%; background: #fff; left: 3px; top: 3px; transition: transform 0.15s; box-shadow: 0 1px 3px rgba(0,0,0,0.3); }
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(18px); }

.danger-zone { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 8px; }

.chat-info-head { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; }
.chat-info-head .ci-title { font-size: 17px; font-weight: 700; }
.chat-info-head .ci-sub { font-size: 13px; color: var(--text-muted); }

/* ============ avatar ============ */
.avatar { border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; flex-shrink: 0; user-select: none; border: 1px solid var(--avatar-border); overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.avatar-add {
  width: 96px; height: 96px; border-radius: 50%; background: #ffffff; color: #8a94a6;
  display: inline-flex; align-items: center; justify-content: center; flex-direction: column; gap: 3px;
  cursor: pointer; border: 2px dashed #cbd3df; flex-shrink: 0;
}
.avatar-add span { font-size: 12px; font-weight: 600; }

/* ============ modal content bits ============ */
.ch-info-big { display: flex; justify-content: center; }
.ch-name { font-weight: 700; font-size: 18px; }
.ch-sub { font-size: 13px; color: var(--text-muted); }
.empty-hint { padding: 18px; text-align: center; color: var(--text-muted); font-size: 13px; }
.input {
  width: 100%; padding: 14px 16px; border: 1px solid var(--border-strong); border-radius: 12px;
  background: var(--modal-bg); color: var(--text); font-size: 16px; outline: none; margin-bottom: 12px;
  box-sizing: border-box;
}
.input:focus { border-color: var(--accent); }
.btn.primary { background: var(--btn-primary); color: #fff; }
.btn.primary:hover { background: var(--accent); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.soft { background: var(--danger-soft); color: var(--danger); }
.btn.soft:hover { filter: brightness(0.96); }
.info-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 8px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.info-row:hover { background: var(--sidebar-hover); }
.info-row.danger { color: var(--danger); }
.info-row span { display: flex; align-items: center; gap: 10px; }
.pill { font-size: 11px; font-weight: 700; background: var(--accent-soft); color: var(--accent); padding: 2px 8px; border-radius: 10px; }
.usr-row { display: flex; align-items: center; gap: 12px; padding: 12px; cursor: pointer; border-bottom: 1px solid var(--border); }
.usr-row:last-child { border-bottom: none; }
.usr-row:hover { background: var(--sidebar-hover); }
.usr-row.sel { background: var(--accent-soft); }
.usr-name { flex: 1; min-width: 0; font-weight: 600; font-size: 16px; }
.usr-sub { font-size: 13px; color: var(--text-muted); font-weight: 400; margin-top: 2px; }
.usr-pick { font-size: 18px; font-weight: 700; color: var(--accent); flex-shrink: 0; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--accent-soft); color: var(--accent); padding: 6px 12px; border-radius: 16px; font-size: 13px; font-weight: 600; cursor: pointer; }
.nst-tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.nst-tab { flex: 1; text-align: center; padding: 12px 8px; border: 1px solid var(--border); border-radius: 10px; font-size: 14px; font-weight: 600; color: var(--text-secondary); cursor: pointer; }
.nst-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.tsearch-wrap { margin-bottom: 8px; }
.nst-results { max-height: 320px; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; margin-bottom: 12px; }
.nst-selected { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }

/* ============ admin: manage users ============ */
.au-create { padding: 14px; border: 1px solid var(--border); border-radius: 12px; margin-bottom: 16px; background: var(--sidebar-bg); }
.au-title { font-weight: 700; font-size: 14px; margin-bottom: 10px; }
.au-list { max-height: 320px; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; }
.au-list-title { font-weight: 700; font-size: 14px; margin: 14px 0 8px; }
.usr-actions { display: flex; gap: 6px; flex-shrink: 0; }
.admin-row { cursor: default; }
.pill.hi { background: var(--accent-soft); color: var(--accent); }
.pill.off { background: var(--danger-soft); color: var(--danger); }

/* ============ toast / loading ============ */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #2b3440; color: #fff;
  padding: 11px 22px; border-radius: 12px; font-size: 14px; z-index: 2000; max-width: 90%; text-align: center;
  box-shadow: var(--shadow-menu); animation: toastIn 0.18s ease;
}
#toast.error { background: var(--danger); }
@keyframes toastIn { from { opacity: 0; transform: translateX(-50%) translateY(16px); } to { opacity: 1; transform: translateX(-50%) translateY(0); } }

#loading-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.25); display: flex; align-items: center; justify-content: center; z-index: 3000; }
.spinner { width: 36px; height: 36px; border: 3px solid rgba(255,255,255,0.4); border-top-color: #fff; border-radius: 50%; animation: spin 0.7s linear infinite; background: rgba(0,0,0,0.25); }
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ responsive ============ */
@media (min-width: 768px) {
  .sidebar { width: 420px; flex-shrink: 0; }
  .modal { border-radius: 20px; align-self: center; animation: none; padding-bottom: 12px; }
  #modal-overlay { align-items: center; }
  #toast { bottom: 24px; }
  .bubble { max-width: 480px; }
  .msg-media img { max-width: 320px; max-height: 320px; }
}
@media (max-width: 767px) {
  .sidebar { width: 100%; }
  .sidebar.hidden-m { display: none; }
  .modal { max-width: none; height: 100dvh; border-radius: 0; }
  .modal-body { padding-bottom: calc(28px + env(safe-area-inset-bottom)); }
  .chat-area { position: absolute; inset: 0; transform: translateX(100%); transition: transform 0.22s ease; z-index: 10; }
  .chat-area.active,
  body.chat-open .chat-area { transform: translateX(0); }
  body.chat-open .sidebar { display: none; }
  .chat-empty { display: none; }
  .msg-media img { max-width: 220px; max-height: 220px; }
  .bubble { max-width: 88%; }
  .brand-name small { display: none; }
  .ch-back { display: inline-flex; }
}
@media (max-width: 360px) {
  .bubble { max-width: 92%; padding: 6px 8px; font-size: 14px; }
}

@supports (padding: env(safe-area-inset-bottom)) {
  .composer { padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
  #toast { bottom: calc(24px + env(safe-area-inset-bottom)); }
}

/* ===================================================================
   Current markup layer.
   js/app.js renders .msg / .msg-meta / .quote / .ci-body / .badge /
   .ctx-menu and friends, while the rules above are written for the
   older .msg-wrap / .msg-footer / .reply-quote / .ci-main names.
   The rules below supply the missing ones using the same variables, so
   nothing above has to change.
   =================================================================== */

/* ---------- message list ---------- */
.msg { display: flex; margin: 1px 0; }
.msg.sent { justify-content: flex-end; }
.msg.received { justify-content: flex-start; }
.msg.channel { justify-content: center; }
.msg.channel .bubble {
  background: var(--bubble-in); color: var(--text); border-radius: 13px;
  border-top-left-radius: 3px; border-top-right-radius: 3px; box-shadow: var(--shadow-list);
  max-width: min(84%, 480px);
}

.msg.sent .bubble {
  background: linear-gradient(145deg, var(--bubble-out-1), var(--bubble-out-2));
  color: var(--bubble-out-text); border-top-right-radius: 3px;
}
.msg.received .bubble {
  background: var(--bubble-in); color: var(--text);
  border-top-left-radius: 3px; box-shadow: var(--shadow-list);
}

/* the meta row carries sender + time + ticks, and must sit inside the bubble */
.msg-meta {
  display: flex; align-items: center; justify-content: flex-end;
  gap: 3px; margin-top: 1px; float: right; max-width: 100%;
}
.msg-meta .msg-sender { margin: 0 6px 0 0; }
.msg-time { font-size: 11px; white-space: nowrap; }
.msg.sent .msg-time { color: var(--tick); }
.msg.received .msg-time { color: var(--text-muted); }
.meta-ticks { display: inline-flex; align-items: center; margin-left: 2px; }
.msg.sent .meta-ticks svg { color: var(--tick); }

.msg-photo { max-width: 260px; max-height: 260px; border-radius: 8px; display: block; cursor: pointer; }
.msg-caption { margin-top: 4px; font-size: 15px; word-break: break-word; white-space: pre-wrap; }

.fwd-hd { font-size: 12px; color: var(--text-muted); margin-bottom: 2px; }
.msg.sent .fwd-hd { color: var(--tick); }

/* inline quoted reply */
.quote {
  display: block; border-left: 3px solid var(--accent); padding: 3px 8px;
  border-radius: 4px; margin: 2px 0 4px; background: rgba(0, 0, 0, 0.07);
  cursor: pointer; min-width: 0;
}
.msg.sent .quote { background: rgba(255, 255, 255, 0.12); }
.q-name { font-weight: 600; font-size: 12px; color: var(--accent); }
.msg.sent .q-name { color: #e9f2ff; }
.q-body { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px; }

/* file attachment card */
.file-card {
  display: flex; align-items: center; gap: 10px; padding: 9px 11px;
  border-radius: 10px; background: var(--bubble-in); color: var(--text);
  border: 1px solid var(--border-strong); text-decoration: none; max-width: 300px;
}
.msg.sent .file-card { background: rgba(255, 255, 255, 0.14); color: var(--bubble-out-text); border-color: transparent; }
.file-ic { font-size: 24px; flex-shrink: 0; }
.file-meta { display: flex; flex-direction: column; min-width: 0; }
.file-name { font-size: 13px; font-weight: 600; word-break: break-all; }
.file-size { font-size: 11px; opacity: 0.7; }

/* reaction chips */
.rchip {
  display: inline-flex; align-items: center; gap: 4px; font-size: 13px;
  padding: 2px 8px; border-radius: 12px; background: var(--bubble-in);
  border: 1px solid var(--border-strong); cursor: pointer; color: var(--text);
  box-shadow: var(--shadow-list); user-select: none;
}
.rchip.mine { border-color: var(--accent); }
.msg.sent .rchip { background: rgba(255, 255, 255, 0.92); }
.re-emoji { line-height: 1; }
.re-count { font-size: 12px; color: var(--text-muted); }

/* date separator - .day is the flex item, .day-label the pill inside it */
.day { align-self: center; display: flex; justify-content: center; width: 100%; margin: 8px 0 6px; }
.day-label {
  display: inline-flex; align-items: center;
  background: var(--chip-bg); color: var(--chip-text);
  font-size: 12px; font-weight: 500; padding: 4px 12px;
  border-radius: 14px;
}

/* search hit rows */
.msg-result { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.msg-result:hover { background: var(--sidebar-hover); }

/* ---------- conversation list ---------- */
.ci-body { flex: 1; min-width: 0; }
.ci-top { display: flex; align-items: center; gap: 6px; }
.ci-bottom { display: flex; align-items: center; gap: 5px; margin-top: 3px; }
.ci-bottom .ci-preview { flex: 1; min-width: 0; }
.typing-dots { color: var(--accent); font-style: italic; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  background: var(--unread-pill); color: #fff; font-size: 11px; font-weight: 700;
  padding: 1px 7px; border-radius: 12px; min-width: 20px;
  text-align: center; line-height: 1.6; flex-shrink: 0;
}
.mut-dot, .pin-tag, .chn-tag { font-size: 12px; flex-shrink: 0; }
.chn-tag {
  background: var(--accent-soft); color: var(--accent);
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.4px; padding: 1px 6px; border-radius: 8px;
}

/* ---------- long-press / right-click menu ---------- */
.ctx-menu {
  position: absolute; z-index: 40; min-width: 172px; padding: 5px;
  background: var(--modal-bg); border: 1px solid var(--border-strong);
  border-radius: 12px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
  display: flex; flex-direction: column; gap: 1px;
}
.ctx-menu > button {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 10px; border: none; background: none; cursor: pointer;
  font-size: 14px; color: var(--text); border-radius: 8px; text-align: left;
}
.ctx-menu > button:hover { background: var(--sidebar-hover); }
.ctx-ic { font-size: 15px; width: 18px; text-align: center; flex-shrink: 0; }

/* ---------- composer reply preview ---------- */
.rpl-body {
  font-size: 13px; color: var(--text-secondary); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.rpl-name { font-size: 12px; font-weight: 600; color: var(--accent); }
.rpl-x {
  background: none; border: none; color: var(--text-muted);
  font-size: 16px; cursor: pointer; padding: 0 4px; align-self: flex-start;
}

/* ---------- search / pickers ---------- */
.sbox { padding: 10px 4px 6px; font-size: 13px; color: var(--text-muted); }
.nst-pane[hidden] { display: none; }
.fs-row:hover { background: var(--sidebar-hover); }

@media (max-width: 767px) {
  .msg-photo { max-width: 220px; max-height: 220px; }
  .msg.sent .bubble, .msg.received .bubble, .msg.channel .bubble { max-width: 88%; }
  .q-body { max-width: 190px; }
}
@media (max-width: 360px) {
  .msg.sent .bubble, .msg.received .bubble, .msg.channel .bubble { max-width: 92%; padding: 6px 8px; font-size: 14px; }
}
