/* ============================================================
   iSeeMore Chat Module v2.2 — CSS
   Matches portal design system (CSS variables from portal.css)
   v2.2: search bar, new DM modal, channel avatars, unread badges
   ============================================================ */

/* Layout */
.chat-wrap {
  display: flex;
  height: calc(100vh - 56px);
  height: calc(100dvh - 56px); /* dynamic viewport height — accounts for mobile keyboard */
  margin: -20px -24px;
  background: var(--bg);
  overflow: hidden;
}

.chat-sidebar {
  width: 320px;
  min-width: 320px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

/* Sidebar header */
.chat-sidebar-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.chat-sidebar-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.chat-sidebar-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

/* New DM button in sidebar header */
.chat-newdm-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s;
}

.chat-newdm-btn:hover {
  background: var(--primary, #4f46e5);
  color: #fff;
  border-color: var(--primary, #4f46e5);
}

/* Search bar */
.chat-search-wrap {
  position: relative;
}

.chat-search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text3);
  pointer-events: none;
}

.chat-search-input {
  width: 100%;
  padding: 8px 12px 8px 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
  box-sizing: border-box;
}

.chat-search-input:focus {
  border-color: var(--primary, #4f46e5);
}

.chat-search-input::placeholder {
  color: var(--text3);
}

/* Room list */
.chat-room-list {
  flex: 1;
  overflow-y: auto;
}

.chat-room-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.15s;
  border-bottom: 1px solid var(--border-light, var(--border));
}

.chat-room-item:hover {
  background: var(--hover, rgba(0,0,0,0.04));
}

.chat-room-item.active {
  background: var(--primary-light, rgba(79, 70, 229, 0.08));
}

.chat-room-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary, #4f46e5);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  flex-shrink: 0;
}

.chat-room-avatar-channel {
  background: var(--info, #3b82f6);
  border-radius: 12px;
  font-size: 18px;
}

.chat-room-info {
  flex: 1;
  min-width: 0;
}

.chat-room-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.chat-room-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-room-time {
  font-size: 11px;
  color: var(--text3);
  flex-shrink: 0;
}

.chat-room-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}

.chat-room-preview {
  font-size: 12px;
  color: var(--text2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-room-unread {
  background: var(--primary, #4f46e5);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
  flex-shrink: 0;
}

/* Chat header */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.chat-back-btn {
  display: none;
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
  color: var(--text2);
}

.chat-back-btn svg {
  width: 20px;
  height: 20px;
}

.chat-header-info { flex: 1; min-width: 0; }
.chat-header-name { font-size: 15px; font-weight: 600; color: var(--text); }
.chat-header-topic { font-size: 12px; color: var(--text3); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Messages area */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chat-date-sep {
  text-align: center;
  font-size: 11px;
  color: var(--text3);
  padding: 12px 0 4px;
  font-weight: 500;
}

.chat-msg {
  display: flex;
  flex-direction: column;
  max-width: 70%;
}

.chat-msg-other {
  align-self: flex-start;
}

.chat-msg-me {
  align-self: flex-end;
}

.chat-msg-sender {
  font-size: 11px;
  font-weight: 600;
  color: var(--primary, #4f46e5);
  margin-bottom: 2px;
  padding-left: 12px;
}

.chat-msg-bubble {
  padding: 8px 12px;
  border-radius: 16px;
  position: relative;
  line-height: 1.4;
}

.chat-msg-other .chat-msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.chat-msg-me .chat-msg-bubble {
  background: var(--primary, #4f46e5);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-msg-text {
  font-size: 14px;
  word-break: break-word;
}

.chat-msg-time {
  font-size: 10px;
  opacity: 0.6;
  float: right;
  margin-left: 8px;
  margin-top: 4px;
}

.chat-msg-img {
  max-width: 280px;
  max-height: 200px;
  border-radius: 8px;
  cursor: pointer;
}

.chat-msg-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: inherit;
  text-decoration: underline;
}

.chat-msg-me .chat-msg-file { color: #fff; }

.chat-msg-img-wrap {
  min-width: 80px;
  min-height: 40px;
}

.chat-msg-img-loading {
  font-size: 11px;
  color: var(--text3);
  padding: 8px 0;
}

/* Input area */
.chat-input-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.chat-attach-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--text3);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.15s;
}

.chat-attach-btn:hover {
  color: var(--primary, #4f46e5);
}

.chat-input {
  flex: 1;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 24px;
  font-size: 14px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

.chat-input:focus {
  border-color: var(--primary, #4f46e5);
}

.chat-send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--primary, #4f46e5);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.15s;
}

.chat-send-btn:hover { opacity: 0.85; }
.chat-send-btn svg { width: 18px; height: 18px; }

/* Placeholder */
.chat-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text3);
}

.chat-placeholder-icon svg {
  width: 48px;
  height: 48px;
  opacity: 0.3;
}

.chat-placeholder-text {
  font-size: 14px;
}

/* Drag-over feedback */
.chat-wrap.chat-dragover {
  outline: 2px dashed var(--primary, #4f46e5);
  outline-offset: -4px;
  background: var(--primary-light, rgba(79, 70, 229, 0.04));
}

/* Loading & Empty */
.chat-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: var(--text3);
  font-size: 13px;
}

.chat-error { color: var(--danger, #ef4444); }

.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text3);
  font-size: 13px;
}

/* ─── New DM Modal ────────────────────────────────────────────────────────── */
.chat-newdm-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9998;
}

.chat-newdm-modal.open {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 10vh;
}

.chat-newdm-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.chat-newdm-panel {
  position: relative;
  width: 420px;
  max-width: 95vw;
  max-height: 70vh;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.chat-newdm-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

.chat-newdm-close {
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
  color: var(--text3);
  transition: color 0.15s;
}

.chat-newdm-close:hover {
  color: var(--text);
}

.chat-newdm-search-wrap {
  padding: 12px 20px;
}

.chat-newdm-search {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
  box-sizing: border-box;
}

.chat-newdm-search:focus {
  border-color: var(--primary, #4f46e5);
}

.chat-newdm-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 4px 8px;
}

.chat-newdm-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  cursor: pointer;
  border-radius: 10px;
  transition: background 0.15s;
}

.chat-newdm-item:hover {
  background: var(--hover, rgba(0,0,0,0.04));
}

.chat-newdm-info {
  flex: 1;
  min-width: 0;
}

.chat-newdm-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.chat-newdm-role {
  font-size: 12px;
  color: var(--text3);
  margin-top: 1px;
}

/* ─── Topbar tab unread badge ─────────────────────────────────────────────── */
.mtab-unread {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--danger, #ef4444);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  margin-left: 4px;
  vertical-align: middle;
}

/* ─── Bottom nav unread badge ─────────────────────────────────────────────── */
.bnav-badge {
  position: absolute;
  top: 2px;
  right: 50%;
  transform: translateX(calc(50% + 10px));
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--danger, #ef4444);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

/* ─── Image Modal ─────────────────────────────────────────────────────────── */
.chat-img-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
}

.chat-img-modal.open {
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-img-modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.85);
}

.chat-img-modal-content {
  position: relative;
  max-width: 90vw;
  max-height: 90vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-img-modal-img {
  max-width: 90vw;
  max-height: 85vh;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.chat-img-modal-loading {
  color: #fff;
  font-size: 14px;
}

.chat-img-modal-close {
  position: fixed;
  top: 16px;
  right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  z-index: 1;
}

.chat-img-modal-close:hover {
  background: rgba(255, 255, 255, 0.3);
}

.chat-img-modal-close svg {
  width: 20px;
  height: 20px;
}

.chat-img-modal-download {
  position: fixed;
  top: 16px;
  right: 68px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  text-decoration: none;
  z-index: 1;
}

.chat-img-modal-download:hover {
  background: rgba(255, 255, 255, 0.3);
}

.chat-img-modal-download svg {
  width: 20px;
  height: 20px;
}

/* ─── Tablet & Mobile (≤900px) — matches portal bottomnav breakpoint ────── */
@media (max-width: 900px) {
  .chat-wrap {
    /* Fixed positioning eliminates all scroll/keyboard/padding issues */
    position: fixed;
    top: 48px; /* topbar height = var(--header-h) */
    left: 0;
    right: 0;
    bottom: 60px; /* bottomnav height */
    height: auto;
    margin: 0;
    z-index: 20;
    overflow: hidden;
  }

  .chat-sidebar {
    width: 100%;
    min-width: 0;
  }

  .chat-main {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    background: var(--bg);
    height: 100%;
  }

  .chat-wrap.chat-room-open .chat-sidebar {
    display: none;
  }

  .chat-wrap.chat-room-open .chat-main {
    display: flex;
  }

  .chat-back-btn {
    display: flex;
  }

  .chat-msg {
    max-width: 85%;
  }

  .chat-input-wrap {
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }

  /* New DM modal — full width on mobile */
  .chat-newdm-modal.open {
    padding-top: 0;
  }

  .chat-newdm-panel {
    width: 100%;
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 0;
    height: 100%;
  }
}
/* ─── Invite Room Cards ──────────────────────────────────────── */
.chat-room-invite {
  border-left: 3px solid var(--primary);
  background: rgba(37, 99, 235, 0.04);
}
.chat-room-invite .chat-room-info {
  padding-bottom: 4px;
}
.chat-invite-actions {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}
.chat-invite-btn {
  padding: 4px 14px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: opacity 0.15s;
}
.chat-invite-btn:hover { opacity: 0.85; }
.chat-invite-accept {
  background: var(--primary);
  color: #fff;
}
.chat-invite-decline {
  background: var(--surface, #f3f4f6);
  color: var(--text2, #6b7280);
  border: 1px solid var(--border, #e5e7eb);
}

/* ─── Reply Bar (above input) ────────────────────────────────── */
.chat-reply-bar {
  padding: 8px 16px 0;
  background: var(--bg, #fff);
}
.chat-reply-bar-content {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--surface, #f3f4f6);
  border-radius: 8px;
  min-height: 36px;
}
.chat-reply-bar-line {
  width: 3px;
  min-height: 28px;
  border-radius: 2px;
  background: var(--primary);
  flex-shrink: 0;
}
.chat-reply-bar-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
}
.chat-reply-bar-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-reply-bar-body {
  font-size: 12px;
  color: var(--text2, #6b7280);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}
.chat-reply-bar-close {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  color: var(--text3, #9ca3af);
  flex-shrink: 0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.chat-reply-bar-close:hover {
  color: var(--text, #1f2937);
  background: rgba(0,0,0,0.05);
}

/* ─── Reply Quote in Message Bubble ──────────────────────────── */
.chat-msg-reply {
  padding: 6px 10px;
  margin-bottom: 4px;
  background: rgba(0,0,0,0.04);
  border-left: 3px solid var(--primary);
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s;
}
.chat-msg-reply:hover {
  background: rgba(0,0,0,0.07);
}
.chat-msg-reply-name {
  font-size: 11px;
  font-weight: 600;
  color: var(--primary);
}
.chat-msg-reply-text {
  font-size: 11px;
  color: var(--text2, #6b7280);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 250px;
  margin-top: 1px;
}

/* ─── Reply Button on Message ────────────────────────────────── */
.chat-msg-bubble-wrap {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  max-width: 80%;
}
.chat-msg-me .chat-msg-bubble-wrap {
  flex-direction: row-reverse;
}
.chat-msg-reply-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  color: var(--text3, #9ca3af);
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.chat-msg:hover .chat-msg-reply-btn {
  opacity: 1;
}
.chat-msg-reply-btn:hover {
  color: var(--primary);
  background: rgba(37, 99, 235, 0.08);
}

/* ─── Read Receipts ──────────────────────────────────────────── */
.chat-msg-receipt {
  font-size: 11px;
  margin-left: 4px;
  color: var(--text3, #9ca3af);
  user-select: none;
}
.chat-msg-receipt-read {
  color: var(--primary, #2563eb);
}

/* ─── Message Highlight (when clicking reply quote) ──────────── */
@keyframes chat-msg-flash {
  0%, 100% { background: transparent; }
  30% { background: rgba(37, 99, 235, 0.1); }
}
.chat-msg-highlight {
  animation: chat-msg-flash 2s ease;
  border-radius: 8px;
}

/* ─── Input Area wrapper (for reply bar positioning) ─────────── */
.chat-input-area {
  flex-shrink: 0;
}

