/* ═══════════════════════════════════════════════════════════
   P84 Chat System — chat.css
   Telegram-like UI, inherits CSS variables from base.css
   ═══════════════════════════════════════════════════════════ */

/* ── Module container ─────────────────────────────────────── */

/* ── Layout ───────────────────────────────────────────────── */
.chat-root {
  display: flex;
  width: 100%;
  height: 100%;
  background: var(--bg);
  overflow: hidden;
  border-radius: 0;
}

/* ── Left sidebar ─────────────────────────────────────────── */
.chat-sidebar {
  width: 300px;
  min-width: 240px;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  flex-shrink: 0;
  transition: transform 0.25s ease;
}

.chat-sidebar-header {
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-sidebar-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
}

.chat-sidebar-btn {
  width: 32px; height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text2);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 15px;
  transition: all 0.15s;
}
.chat-sidebar-btn:hover { background: var(--accent-bg); color: var(--accent); }

.chat-search-wrap {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.chat-search-input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 7px 14px;
  font-size: 13px;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
.chat-search-input::placeholder { color: var(--text3); }
.chat-search-input:focus { border-color: var(--accent); }

/* ── Conversation list ─────────────────────────────────────── */
.chat-conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}
.chat-conv-list::-webkit-scrollbar { width: 4px; }
.chat-conv-list::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 2px; }

.chat-conv-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  cursor: pointer;
  border-radius: 0;
  transition: background 0.12s;
  position: relative;
}
.chat-conv-item:hover { background: var(--surface2); }
.chat-conv-item.active { background: var(--accent-bg); }
.chat-conv-item.active .chat-conv-name { color: var(--accent); }

.chat-conv-avatar {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--accent-bg);
  color: var(--accent);
  font-size: 16px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  position: relative;
}
.chat-conv-avatar.group { background: var(--gold-bg); color: var(--gold); }
.chat-conv-avatar .online-dot {
  position: absolute; bottom: 1px; right: 1px;
  width: 10px; height: 10px;
  background: #22c55e;
  border-radius: 50%;
  border: 2px solid var(--surface);
}

.chat-conv-body { flex: 1; min-width: 0; }
.chat-conv-name {
  font-size: 14px; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-conv-preview {
  font-family: 'Vazirmatn', 'Vazir', sans-serif;
  font-size: 12px; color: var(--text3);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 2px;
}

.chat-conv-meta {
  display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0;
}
.chat-conv-time { font-size: 11px; color: var(--text3); font-family: 'Vazirmatn', 'Vazir', sans-serif; }
.chat-conv-badge {
  background: var(--accent);
  color: #fff;
  font-size: 11px; font-weight: 700;
  min-width: 18px; height: 18px;
  border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 5px;
}

/* ── Admin actions footer in sidebar ──────────────────────── */
.chat-admin-actions {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  display: flex; gap: 8px;
}
.chat-admin-btn {
  flex: 1;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  font-size: 12px; color: var(--text2);
  cursor: pointer; text-align: center;
  transition: all 0.15s;
}
.chat-admin-btn:hover { background: var(--accent-bg); color: var(--accent); border-color: var(--accent); }

/* ── Main chat area ───────────────────────────────────────── */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg2);
  position: relative;
}

.chat-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text3);
  gap: 12px;
}
.chat-placeholder-icon { font-size: 48px; opacity: 0.5; }
.chat-placeholder-text { font-size: 15px; }

/* ── Chat header ──────────────────────────────────────────── */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 56px;
}
.chat-header-avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--accent-bg);
  color: var(--accent);
  font-size: 14px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.chat-header-avatar.group { background: var(--gold-bg); color: var(--gold); }
.chat-header-info { flex: 1; }
.chat-header-name { font-size: 15px; font-weight: 600; color: var(--text); }
.chat-header-status {
  font-size: 12px; color: var(--text3); margin-top: 1px;
  transition: color 0.2s;
}
.chat-header-status.online { color: #22c55e; }
.chat-header-status.typing { color: var(--accent); font-style: italic; }

.chat-header-actions { display: flex; gap: 6px; }
.chat-icon-btn {
  width: 34px; height: 34px;
  border-radius: 8px;
  border: none; background: transparent;
  color: var(--text2); font-size: 17px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: all 0.15s;
}
.chat-icon-btn:hover { background: var(--surface2); color: var(--accent); }

/* ── Pinned message bar ───────────────────────────────────── */
.chat-pinned-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.chat-pinned-bar.visible { display: flex; }
.chat-pinned-icon { color: var(--accent); font-size: 15px; flex-shrink: 0; }
.chat-pinned-text { flex: 1; min-width: 0; }
.chat-pinned-label { font-size: 11px; font-weight: 600; color: var(--accent); text-transform: uppercase; letter-spacing: 0.05em; }
.chat-pinned-content { font-size: 13px; color: var(--text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-pinned-close { color: var(--text3); font-size: 16px; cursor: pointer; flex-shrink: 0; }
.chat-pinned-close:hover { color: var(--text); }

/* ── Now-playing voice bar (under header) ─────────────────── */
.chat-nowplaying-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 7px 14px;
  background: var(--accent-bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-np-toggle {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
}
.chat-np-toggle:hover { background: var(--accent2); }
.chat-np-body { flex: 1; min-width: 0; cursor: pointer; }
.chat-np-label {
  font-size: 12.5px; font-weight: 600; color: var(--accent);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-np-progress {
  height: 6px;
  background: var(--border2);
  border-radius: 3px;
  margin-top: 6px;
  overflow: hidden;
  cursor: pointer;
}
.chat-np-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.15s linear;
}
.chat-np-time {
  font-size: 11px; color: var(--text3);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}
.chat-np-close {
  border: none; background: transparent;
  color: var(--text3); font-size: 15px; cursor: pointer;
  flex-shrink: 0; padding: 2px 4px;
}
.chat-np-close:hover { color: var(--danger); }

/* ── Search overlay ───────────────────────────────────────── */
.chat-search-bar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}
.chat-search-bar.visible { display: flex; }
.chat-search-bar input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 6px 14px;
  font-size: 13px; color: var(--text); outline: none;
}
.chat-search-bar input:focus { border-color: var(--accent); }
.chat-search-results {
  position: absolute; top: 120px; right: 0; left: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  max-height: 250px; overflow-y: auto;
  z-index: 10;
  display: none;
}
.chat-search-results.visible { display: block; }
.chat-search-result-item {
  padding: 10px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.chat-search-result-item:hover { background: var(--surface2); }
.chat-search-result-name { font-size: 12px; font-weight: 600; color: var(--accent); }
.chat-search-result-text { font-size: 13px; color: var(--text); margin-top: 2px; }
.chat-search-result-date { font-size: 11px; color: var(--text3); margin-top: 1px; }

/* ── Messages area ────────────────────────────────────────── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.chat-messages::-webkit-scrollbar { width: 5px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 3px; }

/* date separator — a normal in-flow divider between days. The single
   floating chip (.chat-float-date, driven by JS) replaces the old CSS-sticky
   behaviour, which caused two chips to overlap when one day pushed the next. */
.chat-date-sep {
  text-align: center;
  margin: 12px 0;
  position: relative;
  z-index: 1;
  pointer-events: none;
}
.chat-date-sep span {
  background: var(--bg2);
  color: var(--text3);
  font-size: 11px;
  padding: 4px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  position: relative; z-index: 1;
  font-family: var(--font-fa, 'Vazirmatn', 'Vazir', sans-serif);
  direction: rtl;
  unicode-bidi: embed;
}

/* Single floating date chip — positioned/toggled by chat-module.js
   (updateFloatingDate). It hides once the top edge of the active day's last
   message scrolls above the top line, so it never overlaps the next day's
   inline separator. Only ever ONE chip exists → no overlap bug. */
.chat-float-date {
  position: absolute;
  left: 0;
  right: 0;
  top: 8px;
  text-align: center;
  z-index: 8;
  pointer-events: none;
  opacity: 0;
  transition: opacity .18s ease;
}
.chat-float-date.visible { opacity: 1; }
/* Identical look to the inline .chat-date-sep span — no shadow/halo, so the
   floating chip and the in-flow day separators are visually indistinguishable. */
.chat-float-date span {
  display: inline-block;
  background: var(--bg2);
  color: var(--text3);
  font-size: 11px;
  padding: 4px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  font-family: var(--font-fa, 'Vazirmatn', 'Vazir', sans-serif);
  direction: rtl;
  unicode-bidi: embed;
}

/* ── Link colors inside bubbles ── */
.chat-msg-text a { color: var(--accent); text-decoration: underline; word-break: break-all; }
.chat-msg.admin-msg .chat-msg-text a { color: rgba(255,255,255,0.9); }
.chat-msg-bubble { background-color: var(--surface2); } /* prevent browser default blue */
.chat-msg-text, .chat-msg-text * { user-select: text; -webkit-user-select: text; }

/* ── Msg time + seen font ── */
/* Time is a Latin clock string ("10:00 AM") — force LTR so the hour and the
   AM/PM stay together in the correct order inside RTL bubbles. */
.chat-msg-time {
  direction: ltr;
  unicode-bidi: isolate;
  white-space: nowrap;
}

/* ── Message bubble ───────────────────────────────────────── */
.chat-msg {
  display: flex;
  flex-direction: column;
  max-width: 70%;
  position: relative;
  margin: 1px 0;
  animation: msgIn 0.18s ease;
}
@keyframes msgIn { from { opacity: 0; transform: translateY(6px); } }

.chat-msg.outgoing { align-self: flex-end; align-items: flex-end; }
.chat-msg.incoming { align-self: flex-start; align-items: flex-start; }

/* Admin vs student distinction */
.chat-msg.admin-msg .chat-bubble {
  background: linear-gradient(135deg, #2d5be3 0%, #1a3fa0 100%);
  color: #fff;
  /* Admin bubble arriving at student = incoming from left → bottom-left sharp */
  border-radius: 18px 18px 18px 4px;
  box-shadow: 0 2px 12px rgba(45,91,227,0.25);
}
[data-theme="dark"] .chat-msg.admin-msg .chat-bubble,
.dark .chat-msg.admin-msg .chat-bubble {
  background: linear-gradient(135deg, #6b8ef0 0%, #4a6ed4 100%);
  box-shadow: 0 2px 12px rgba(107,142,240,0.3);
}

/* Admin sending to student = outgoing to right → bottom-right sharp */
.chat-msg.admin-msg.outgoing .chat-bubble {
  border-radius: 18px 18px 4px 18px;
}

/* Admin badge on sender name */
.chat-msg-sender-name {
  font-size: 11px; font-weight: 700;
  color: var(--accent);
  margin-bottom: 3px;
  padding: 0 4px;
}
.chat-msg-sender-name.admin-badge::after {
  content: ' ★';
  color: var(--gold);
}

/* Bubble */
.chat-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  /* incoming = bottom-left sharp */
  border-radius: 18px 18px 18px 4px;
  padding: 8px 12px;
  font-size: 14px;
  line-height: 1.55;
  color: var(--text);
  word-break: break-word;
  position: relative;
  cursor: default;
  transition: opacity 0.15s;
  font-family: 'Vazirmatn', 'Vazir', sans-serif;
}
.chat-msg.outgoing .chat-bubble {
  background: var(--accent-bg);
  border-color: transparent;
  /* outgoing = bottom-right sharp */
  border-radius: 18px 18px 4px 18px;
  color: var(--text);
}

.chat-bubble:hover { opacity: 0.95; }

/* ── Reply preview inside bubble ──────────────────────────── */
.chat-reply-preview {
  background: rgba(0,0,0,0.06);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 5px 9px;
  margin-bottom: 6px;
  font-size: 12px;
  cursor: pointer;
  max-width: 340px;
  width: auto;
  box-sizing: border-box;
  overflow: hidden;
}
.chat-reply-preview .rp-name { font-weight: 600; color: var(--accent); margin-bottom: 1px; }
.chat-reply-preview .rp-text { color: var(--text2); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-msg.admin-msg .chat-reply-preview {
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.6);
}
.chat-msg.admin-msg .chat-reply-preview .rp-name { color: rgba(255,255,255,0.9); }
.chat-msg.admin-msg .chat-reply-preview .rp-text { color: rgba(255,255,255,0.75); }

/* ── Message text ─────────────────────────────────────────── */
.chat-msg-text { white-space: pre-wrap; }
.chat-msg-text > div + div,
.chat-msg-text > p + p { margin-top: 6px; }
.chat-msg.admin-msg .chat-msg-text { color: #fff; }
.chat-msg-edited {
  font-size: 10px;
  color: var(--text3);
  margin-left: 4px;
}
.chat-msg.admin-msg .chat-msg-edited { color: rgba(255,255,255,0.5); }

/* ── Media ────────────────────────────────────────────────── */
.chat-msg-image {
  max-width: 280px; max-height: 240px;
  border-radius: 10px;
  object-fit: cover;
  cursor: zoom-in;
  display: block;
  margin-bottom: 4px;
}
.chat-msg-video {
  max-width: 280px;
  border-radius: 10px;
  display: block;
  margin-bottom: 4px;
}
.chat-msg-audio {
  display: block;
  width: 260px;
  min-width: 200px;
  max-width: 100%;
  margin-bottom: 4px;
  accent-color: var(--accent);
}
/* Ensure audio bubble is never too small */
.chat-msg:has(.chat-msg-audio),
.chat-msg:has(.chat-msg-audio) .chat-bubble {
  min-width: clamp(360px, 55%, 480px) !important;
  width: clamp(360px, 55%, 480px) !important;
  max-width: clamp(360px, 55%, 480px) !important;
  box-sizing: border-box;
}
@media (max-width: 640px) {
  .chat-msg:has(.chat-msg-audio),
  .chat-msg:has(.chat-msg-audio) .chat-bubble {
    min-width: min(85vw, 320px) !important;
    width: min(85vw, 320px) !important;
    max-width: min(85vw, 320px) !important;
  }
}
.chat-msg-file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  margin-bottom: 4px;
}
.chat-msg-file-icon { font-size: 24px; }
.chat-msg-file-info { flex: 1; min-width: 0; }
.chat-msg-file-name { font-size: 13px; font-weight: 500; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-msg-file-size { font-size: 11px; color: var(--text3); }
.chat-msg.admin-msg .chat-msg-file-name { color: #fff; }
.chat-msg.admin-msg .chat-msg-file-size { color: rgba(255,255,255,0.6); }
.chat-msg-file-dl { font-size: 18px; text-decoration: none; color: var(--accent); flex-shrink: 0; }
.chat-msg.admin-msg .chat-msg-file-dl { color: #fff; }

/* ── System messages ──────────────────────────────────────── */
.chat-msg-system {
  align-self: center;
  max-width: 90%;
}
.chat-msg-system .chat-bubble {
  background: var(--gold-bg);
  border-color: transparent;
  color: var(--text2);
  font-size: 13px;
  text-align: center;
  border-radius: 12px;
  padding: 6px 16px;
}

/* ── Timestamp + seen (inside bubble, Telegram-style) ─────── */
.chat-msg-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 2px;
  margin-bottom: -1px;
  padding: 0;
  line-height: 1;
}
.chat-msg-time {
  font-size: 10.5px;
  color: var(--text3);
}
.chat-msg-seen {
  font-size: 13px;
  line-height: 1;
  cursor: help;
  transition: color 0.3s;
  letter-spacing: -3px;   /* pull the two ticks tightly together */
  padding-right: 2px;     /* compensate trailing letter-spacing */
}
/* Delivered (one tick) = muted; Seen (two ticks) = distinct blue */
.chat-msg-seen.unseen { color: var(--text3); letter-spacing: 0; }
.chat-msg-seen.seen   { color: #2f9be0; }
/* Admin (white) bubble: keep meta legible, seen clearly brighter than delivered */
.chat-msg.admin-msg .chat-msg-time { color: rgba(255,255,255,0.7); }
.chat-msg.admin-msg .chat-msg-seen.unseen { color: rgba(255,255,255,0.55); }
.chat-msg.admin-msg .chat-msg-seen.seen   { color: #bfe6ff; }

/* ── Reactions ────────────────────────────────────────────── */
.chat-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
}
.chat-reaction-pill {
  display: flex;
  align-items: center;
  gap: 3px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2px 7px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.12s;
  user-select: none;
}
.chat-reaction-pill:hover { background: var(--accent-bg); border-color: var(--accent); }
.chat-reaction-pill.my-reaction { background: var(--accent-bg); border-color: var(--accent); }
.chat-reaction-count { font-size: 11px; color: var(--text2); font-weight: 500; }

/* ── Context menu ─────────────────────────────────────────── */
.chat-context-menu {
  position: fixed;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-md);
  padding: 4px 0;
  z-index: 1000;
  min-width: 160px;
  animation: ctxIn 0.12s ease;
}
@keyframes ctxIn { from { opacity: 0; transform: scale(0.96); } }

.chat-ctx-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px;
  font-size: 13.5px;
  color: var(--text);
  cursor: pointer;
  transition: background 0.1s;
}
.chat-ctx-item:hover { background: var(--surface2); }
.chat-ctx-item.danger { color: var(--danger); }
.chat-ctx-item span:first-child { font-size: 16px; }

/* ── Emoji picker popup ───────────────────────────────────── */
.chat-emoji-popup {
  position: fixed;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  padding: 10px;
  z-index: 1000;
  width: 300px;
  animation: ctxIn 0.15s ease;
}
.chat-emoji-popup-cats {
  display: flex;
  gap: 2px;
  margin-bottom: 8px;
  overflow-x: auto;
}
.chat-emoji-cat-btn {
  padding: 5px 9px;
  border-radius: 8px;
  border: none; background: transparent;
  font-size: 15px; cursor: pointer;
  flex-shrink: 0;
  transition: background 0.1s;
}
.chat-emoji-cat-btn.active,
.chat-emoji-cat-btn:hover { background: var(--surface2); }
.chat-emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  max-height: 180px;
  overflow-y: auto;
}
.chat-emoji-btn {
  width: 32px; height: 32px;
  border: none; background: transparent;
  font-size: 19px; cursor: pointer;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.1s;
}
.chat-emoji-btn:hover { background: var(--surface2); }

/* Quick-reaction bar (appears on hover, 6 emojis) */
.chat-quick-react {
  position: absolute;
  top: -36px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow-md);
  padding: 4px 6px;
  display: flex;
  gap: 2px;
  z-index: 50;
  opacity: 0; pointer-events: none;
  transition: opacity 0.15s;
}
.chat-msg:hover .chat-quick-react { opacity: 1; pointer-events: all; }
.chat-msg.outgoing .chat-quick-react { right: 0; }
.chat-msg.incoming .chat-quick-react { left: 0; }
.chat-qr-btn {
  width: 28px; height: 28px;
  border: none; background: transparent;
  font-size: 16px; cursor: pointer; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.1s; user-select: none;
}
.chat-qr-btn:hover { background: var(--surface2); }

/* ── Reply active bar ─────────────────────────────────────── */
.chat-reply-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  animation: slideUp 0.15s ease;
}
@keyframes slideUp { from { transform: translateY(4px); opacity:0; } }
.chat-reply-bar.visible { display: flex; }
.chat-reply-bar-line {
  width: 3px; height: 36px;
  background: var(--accent);
  border-radius: 2px; flex-shrink: 0;
}
.chat-reply-bar-content { flex: 1; min-width: 0; }
.chat-reply-bar-name { font-size: 12px; font-weight: 600; color: var(--accent); }
.chat-reply-bar-text {
  font-size: 12px; color: var(--text2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-reply-bar-close {
  color: var(--text3); font-size: 18px; cursor: pointer;
  flex-shrink: 0; transition: color 0.1s;
}
.chat-reply-bar-close:hover { color: var(--text); }

/* ── Input area ───────────────────────────────────────────── */
.chat-input-area {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  flex-shrink: 0;
}
.chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.chat-input-actions { display: flex; gap: 4px; flex-shrink: 0; }
.chat-input-action {
  width: 36px; height: 36px;
  border: none; background: transparent;
  color: var(--text2); font-size: 20px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: all 0.15s; flex-shrink: 0;
}
.chat-input-action:hover { background: var(--surface2); color: var(--accent); }
.chat-input-action.active { color: var(--accent); }

.chat-textarea-wrap {
  flex: 1;
  background: var(--surface2);
  border: 1.5px solid var(--border);
  border-radius: 22px;
  padding: 8px 14px;
  display: flex; align-items: flex-end; gap: 8px;
  transition: border-color 0.15s;
}
.chat-textarea-wrap:focus-within { border-color: var(--accent); }

.chat-textarea {
  flex: 1;
  background: transparent;
  border: none; outline: none;
  font-size: 14px; color: var(--text);
  resize: none;
  max-height: 120px;
  line-height: 1.5;
  font-family: 'Vazirmatn', 'Vazir', sans-serif;
  unicode-bidi: plaintext;
}
.chat-textarea::placeholder { color: var(--text3); }

.chat-send-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--accent);
  border: none; color: #fff; font-size: 17px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0;
  transition: all 0.15s;
  box-shadow: 0 2px 8px rgba(45,91,227,0.3);
}
.chat-send-btn:hover { background: var(--accent2); transform: scale(1.05); }
.chat-send-btn:active { transform: scale(0.97); }

/* ── Voice recorder ───────────────────────────────────────── */
.chat-voice-recorder {
  display: none;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--danger-bg, rgba(239,68,68,0.08));
  border-top: 1px solid rgba(239,68,68,0.2);
  animation: slideUp 0.15s ease;
}
.chat-voice-recorder.visible { display: flex; }
.chat-voice-dot {
  width: 10px; height: 10px;
  background: #ef4444; border-radius: 50%;
  animation: pulseDot 1s ease-in-out infinite;
}
@keyframes pulseDot {
  0%,100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.3); opacity: 0.6; }
}
.chat-voice-timer { font-size: 14px; font-weight: 600; color: var(--text); font-variant-numeric: tabular-nums; }
.chat-voice-cancel { flex: 1; font-size: 13px; color: var(--text3); cursor: pointer; }
.chat-voice-cancel:hover { color: var(--danger); }
.chat-voice-send {
  padding: 7px 16px;
  background: #22c55e; color: #fff;
  border: none; border-radius: 18px;
  font-size: 13px; font-weight: 600;
  cursor: pointer; transition: all 0.15s;
}
.chat-voice-send:hover { background: #16a34a; }

/* ── Lightbox ─────────────────────────────────────────────── */
.chat-lightbox {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.9);
  display: none; align-items: center; justify-content: center;
  z-index: 9000;
}
.chat-lightbox.visible { display: flex; }
.chat-lightbox img { max-width: 92vw; max-height: 92vh; border-radius: 8px; }
.chat-lightbox-close {
  position: absolute; top: 20px; right: 24px;
  color: #fff; font-size: 32px; cursor: pointer; opacity: 0.8;
}
.chat-lightbox-close:hover { opacity: 1; }

/* ── Typing indicator ─────────────────────────────────────── */
.chat-typing-bubble {
  align-self: flex-start;
  display: none;
}
.chat-typing-bubble.visible { display: flex; }
.chat-typing-dots {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px 18px 18px 4px;
  padding: 10px 14px;
  display: flex; gap: 4px; align-items: center;
}
.chat-typing-dots span {
  width: 7px; height: 7px;
  background: var(--text3); border-radius: 50%;
  animation: dotBounce 1.2s ease infinite;
}
.chat-typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes dotBounce {
  0%,80%,100% { transform: translateY(0); }
  40% { transform: translateY(-6px); }
}

/* ── Admin: broadcast modal ───────────────────────────────── */
.chat-modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: none; align-items: center; justify-content: center;
  z-index: 5000;
}
.chat-modal-overlay.visible { display: flex; }
.chat-modal {
  background: var(--surface);
  border-radius: 16px;
  padding: 24px;
  width: 440px; max-width: 95vw;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.2s ease;
}
@keyframes modalIn { from { opacity:0; transform: scale(0.95); } }
.chat-modal-title { font-size: 17px; font-weight: 600; margin-bottom: 16px; }
.chat-modal-label { font-size: 13px; color: var(--text2); margin-bottom: 6px; display: block; }
.chat-modal-textarea {
  width: 100%;
  background: var(--surface2);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px; color: var(--text);
  resize: vertical; min-height: 80px; outline: none;
  font-family: inherit;
}
.chat-modal-textarea:focus { border-color: var(--accent); }
.chat-modal-select {
  width: 100%;
  background: var(--surface2);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 14px; color: var(--text); outline: none;
  margin-bottom: 14px;
}
.chat-modal-footer { display: flex; gap: 10px; margin-top: 16px; justify-content: flex-end; }
.chat-modal-btn {
  padding: 8px 20px; border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 14px; cursor: pointer; transition: all 0.15s;
}
.chat-modal-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.chat-modal-btn.primary:hover { background: var(--accent2); }
.chat-modal-btn:not(.primary) { background: var(--surface2); color: var(--text2); }
.chat-modal-btn:not(.primary):hover { background: var(--surface3); }

/* ── Restrict modal ───────────────────────────────────────── */
.chat-restrict-modal .chat-modal-textarea { min-height: 60px; }

/* ── Unread nav badge ─────────────────────────────────────── */
.nav-link[data-module="chat"] .chat-nav-badge {
  display: inline-flex;
  align-items: center; justify-content: center;
  background: #ef4444; color: #fff;
  font-size: 10px; font-weight: 700;
  min-width: 16px; height: 16px;
  border-radius: 8px; padding: 0 4px;
  margin-left: 5px;
  vertical-align: middle;
}

/* ── Suspended notice ─────────────────────────────────────── */
.chat-suspended-bar {
  background: rgba(239,68,68,0.1);
  border: 1px solid rgba(239,68,68,0.3);
  border-radius: 10px;
  padding: 10px 16px;
  margin: 12px;
  font-size: 13px;
  color: var(--danger);
  text-align: center;
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 640px) {
  /* ── chat-root MUST be a positioned containing block for the abs sidebar ── */
  .chat-root { position: relative; overflow: hidden; }

  /* ── Sidebar slide-over with overlay ── */
  .chat-sidebar {
    position: absolute; left: 0; top: 0; bottom: 0;
    z-index: 20;
    transform: translateX(-100%);
    width: 85%;
    max-width: 320px;
  }
  .chat-sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg); }

  /* Dark overlay behind sidebar when open — tap to close */
  .chat-sidebar.open::after {
    content: '';
    position: fixed;
    inset: 0;
    left: min(85%, 320px);
    z-index: 19;
    cursor: pointer;
  }

  /* ── Message max-width ── */
  .chat-msg { max-width: 88%; }

  /* ── Prevent iOS font zoom on input focus (needs ≥16px) ── */
  .chat-textarea,
  .chat-search-input,
  .chat-modal-textarea,
  .chat-modal-select { font-size: 16px; }

  /* ── Larger touch targets for action buttons ── */
  .chat-input-action { width: 44px; height: 44px; font-size: 22px; }
  .chat-send-btn     { width: 44px; height: 44px; font-size: 19px; }
  .chat-sidebar-btn  { width: 40px; height: 40px; }

  /* ── Back button — always visible on mobile ── */
  .mobile-back-btn { display: flex !important; }

  /* ── Conversation list rows easier to tap ── */
  .chat-conv-item { padding: 12px 14px; }

  /* ── Input area — account for iOS safe-area (home bar) ── */
  .chat-input-area {
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }

  /* ── Context menu full-width at bottom on mobile ── */
  .chat-ctx-menu {
    position: fixed !important;
    left: 8px !important; right: 8px !important;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px)) !important;
    top: auto !important;
    width: auto !important;
    border-radius: 14px;
    padding: 4px;
  }
  .chat-ctx-item { padding: 13px 16px; font-size: 15px; border-radius: 10px; }

  /* ── Emoji picker — full width on mobile ── */
  .chat-emoji-picker {
    position: fixed !important;
    left: 8px !important; right: 8px !important;
    bottom: calc(64px + env(safe-area-inset-bottom, 0px)) !important;
    top: auto !important;
    width: auto !important;
  }
  .chat-emoji-grid { grid-template-columns: repeat(9, 1fr); }

  /* ── Quick reactions — bigger tap area ── */
  .chat-qr-btn { width: 34px; height: 34px; font-size: 18px; }

  /* ── Header actions ── */
  .chat-header-btn { width: 40px; height: 40px; }

  /* ── Multi-delete bar ── */
  #_multi-del-bar {
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* ── Bubble text — comfortable reading size ── */
  .chat-msg-text { font-size: 15px; line-height: 1.55; }
  .chat-msg-time { font-size: 11px; }
  .chat-msg-audio { min-width: 80% !important; width: 100%; }

  /* ── Hide quick-react on touch (use long-press context menu instead) ── */
  .chat-quick-react { display: none !important; }

  /* ── Reply bar ── */
  .chat-reply-bar { padding: 10px 12px; }

  /* ── Lightbox close button bigger ── */
  .chat-lightbox-close { font-size: 38px; top: 16px; right: 16px; }

  /* ── Scroll-to-bottom button ── */
  .chat-scroll-bottom-btn { width: 42px; height: 42px; font-size: 20px; }

  /* ── Module container height: use dvh so the view shrinks when
        the virtual keyboard pops up, instead of being hidden behind it ── */
  #module-chat.active {
    height: calc(100dvh - 160px);
  }
}

/* ── Paste preview ────────────────────────────────────────── */
.chat-paste-preview {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--surface2);
  border-top: 1px solid var(--border);
}
.chat-paste-preview.visible { display: flex; }
.chat-paste-img { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; }
.chat-paste-info { flex: 1; font-size: 12px; color: var(--text2); }
.chat-paste-cancel { font-size: 18px; cursor: pointer; color: var(--text3); }
.chat-paste-cancel:hover { color: var(--danger); }
/* ── bubble elevation (v2) ─────────────────────────────── */
.chat-msg.incoming .chat-bubble {
  box-shadow: 0 1px 6px rgba(0,0,0,0.09), 0 0.5px 2px rgba(0,0,0,0.06);
}
.chat-msg.outgoing .chat-bubble {
  box-shadow: 0 1px 6px rgba(45,91,227,0.15), 0 0.5px 2px rgba(0,0,0,0.06);
}
.chat-deleted-bubble {
  opacity: 0.55; font-style: italic;
  color: var(--text3) !important; font-size: 13px;
}
.chat-msg.selected .chat-bubble { box-shadow: 0 0 0 2px var(--accent); }
#module-chat.active {
  height: calc(100vh - 160px);
  overflow: hidden;
  padding: 0;
}
/* Admin multi-select */
.chat-msg.selected { background: rgba(79,110,247,.10); border-radius: 8px; user-select: none; }
.chat-msg.selected .chat-bubble { box-shadow: 0 0 0 2px var(--accent); }

/* ── Scroll-to-bottom button ──────────────────────────────── */
.chat-scroll-bottom-btn {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent, #4f6ef7);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  z-index: 20;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: auto;
}
.chat-scroll-bottom-btn:hover {
  transform: translateX(-50%) scale(1.1);
  opacity: 0.9;
}

/* ── Mobile hamburger in placeholder ─────────────────────── */
.chat-mobile-menu-btn {
  display: none;
  position: absolute;
  top: 12px;
  right: 12px;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

@media (max-width: 640px) {
  .chat-mobile-menu-btn { display: flex; }

  /* Placeholder needs relative positioning for the abs button */
  .chat-placeholder { position: relative; }

  /* ── Sidebar: also open when window is wide enough again ── */
  .chat-sidebar { min-width: 0; }

  /* ── conv list: full-height tap area ── */
  .chat-conv-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }

  /* ── Make sure sidebar header is tall enough to tap ── */
  .chat-sidebar-header { min-height: 56px; }

  /* ── search wrap ── */
  .chat-search-wrap { padding: 8px 10px; }
  .chat-search-input { height: 40px; font-size: 16px; }

  /* ── Prevent sidebar from clipping header ── */
  .chat-root { min-height: 0; }
}
