/* ═══════════════════════════════════════════
   Messages Page — Professional Redesign
   ═══════════════════════════════════════════ */

/* ── Layout ─────────────────────────────── */
.msg-layout {
  display: flex;
  height: calc(100vh - 58px);
  overflow: hidden;
  background: var(--white, #fff);
  flex: 1;
}

/* تأكد إن الـ body والـ wrapper يملأوا الشاشة */
body:has(.msg-layout) {
  background: var(--white, #fff) !important;
}

/* ── Sidebar (conversation list) ─────────── */
.msg-sidebar {
  width: 320px;
  min-width: 320px;
  background: var(--white, #fff);
  border-left: 1px solid var(--border, #e8dcc8);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

.msg-sidebar-head {
  padding: 10px 14px 8px;
  border-bottom: 1px solid var(--border, #e8dcc8);
  background: var(--beige, #faf7f2);
  flex-shrink: 0;
}

.msg-sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.msg-sidebar-title {
  font-family: Amiri, serif;
  font-size: 18px;
  color: var(--green-dark, #1b4332);
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.msg-user-info {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  background: var(--beige, #faf7f2);
  border-radius: 10px;
  margin-bottom: 2px;
}

.msg-user-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dark, #1a1a1a);
}

.msg-role-badge {
  font-size: 10.5px;
  padding: 2px 8px;
  border-radius: 20px;
  font-weight: 600;
}

.msg-search-wrap {
  position: relative;
  margin-bottom: 4px;
}

.msg-search-wrap i {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-mid, #6b7280);
  font-size: 14px;
  pointer-events: none;
}

.tp-search {
  width: 100%;
  padding: 7px 30px 7px 12px;
  border: 1px solid var(--border, #e8dcc8);
  border-radius: 10px;
  font-family: inherit;
  font-size: 13px;
  background: var(--beige, #faf7f2);
  outline: none;
  box-sizing: border-box;
  color: var(--text-dark, #1a1a1a);
  transition: border-color 0.2s;
}

.tp-search:focus {
  border-color: var(--green-mid, #5c3d2e);
  background: #fff;
}

/* ── Compose button ──────────────────────── */
.compose-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  background: var(--green-dark, #1b4332);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  white-space: nowrap;
}

.compose-btn:hover { background: var(--green-mid, #5c3d2e); }
.compose-btn:active { transform: scale(0.97); }

/* ── Conversation list ───────────────────── */
#convList {
  overflow-y: auto;
  flex: 1;
  padding: 4px 0;
  min-height: 0;
}

#convList::-webkit-scrollbar { width: 4px; }
#convList::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.conv-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  cursor: pointer;
  border-bottom: 1px solid rgba(0,0,0,0.04);
  transition: background 0.15s;
  position: relative;
}

.conv-item:hover  { background: var(--beige, #faf7f2); }
.conv-item.active { background: #eef6f1; border-right: 3px solid var(--green-dark, #1b4332); }
.conv-item.unread { background: #f0f7f3; border-right: 3px solid var(--green-dark, #1b4332); }
.conv-item.unread .conv-name { font-weight: 700; color: var(--green-dark, #1b4332); }
.conv-item.unread .conv-preview { color: var(--text-dark, #1a1a1a); font-weight: 600; }
.conv-item.unread .conv-time { color: var(--green-dark, #1b4332); font-weight: 600; }

/* Button Delete المحادثة — يظهر عند hover */
.conv-delete-btn {
  display: none;
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: #fee2e2;
  color: #c0392b;
  font-size: 13px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  z-index: 2;
  flex-shrink: 0;
}
.conv-item:hover .conv-delete-btn { display: flex; }
.conv-delete-btn:hover { background: #fca5a5; }

.conv-meta { flex: 1; min-width: 0; }

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

.conv-bottom-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.conv-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dark, #1a1a1a);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 150px;
}

.conv-preview {
  font-size: 11px;
  color: var(--text-mid, #6b7280);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.conv-time {
  font-size: 10.5px;
  color: var(--text-mid, #6b7280);
  white-space: nowrap;
  flex-shrink: 0;
}

.conv-unread {
  background: var(--green-dark, #1b4332);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  flex-shrink: 0;
}

.conv-role-pill {
  display: inline-block;
  font-size: 10px;
  padding: 1px 7px;
  border-radius: 20px;
  margin-top: 3px;
  font-weight: 600;
}

/* ── Avatar (initials) ───────────────────── */
.conv-avatar {
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-family: 'Noto Naskh Arabic', sans-serif;
  flex-shrink: 0;
  letter-spacing: -0.5px;
}

/* ── Main area ───────────────────────────── */
.msg-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #f0ebe3;
}

.msg-empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-mid, #6b7280);
  gap: 16px;
  text-align: center;
  padding: 40px;
}

.msg-empty-state i {
  font-size: 52px;
  opacity: 0.3;
  color: var(--green-dark, #1b4332);
}

.msg-empty-state h3 {
  font-family: Amiri, serif;
  font-size: 20px;
  color: var(--green-dark, #1b4332);
  margin: 0;
  opacity: 0.7;
}

.msg-empty-state p {
  font-size: 13px;
  margin: 0;
  opacity: 0.6;
}

/* ── Conversation header ─────────────────── */
#msgConv {
  height: 100%;
  flex-direction: column;
}

.msg-conv-head {
  padding: 14px 20px;
  border-bottom: 1px solid rgba(0,0,0,0.08);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--white, #fff);
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  flex-shrink: 0;
}

#convHeaderAvatar { flex-shrink: 0; }

.msg-conv-info { flex: 1; min-width: 0; }
.msg-conv-name { font-size: 15px; font-weight: 700; color: var(--text-dark, #1a1a1a); }
.msg-conv-role { font-size: 12px; font-weight: 600; margin-top: 1px; }

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

.msg-bubbles::-webkit-scrollbar { width: 4px; }
.msg-bubbles::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 4px; }

/* Day divider */
.msg-day-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 12px 0;
  color: var(--text-mid, #6b7280);
  font-size: 11px;
}
.msg-day-divider::before,
.msg-day-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: rgba(0,0,0,0.1);
}
.msg-day-divider span {
  background: rgba(255,255,255,0.8);
  padding: 2px 12px;
  border-radius: 20px;
  border: 1px solid rgba(0,0,0,0.08);
  white-space: nowrap;
}

/* Message row */
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-bottom: 2px;
}
.msg-row.mine    { flex-direction: row-reverse; justify-content: flex-start; }
.msg-row.theirs  { flex-direction: row;         justify-content: flex-start; }

.msg-bubble-wrap {
  display: flex;
  flex-direction: column;
  max-width: 68%;
}

.msg-row.mine .msg-bubble-wrap { align-items: flex-end; }
.msg-row.theirs .msg-bubble-wrap { align-items: flex-start; }

.msg-sender-name {
  font-size: 10.5px;
  color: var(--text-mid, #6b7280);
  margin-bottom: 3px;
  padding: 0 4px;
  font-weight: 600;
}

.msg-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 13.5px;
  line-height: 1.65;
  word-break: break-word;
  position: relative;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.msg-bubble.mine {
  background: var(--green-dark, #1b4332);
  color: #e9f5db;
  border-bottom-right-radius: 4px;
}

.msg-bubble.theirs {
  background: #fff;
  color: var(--text-dark, #1a1a1a);
  border-bottom-left-radius: 4px;
  border: 1px solid rgba(0,0,0,0.06);
}

.msg-text { display: block; }

.msg-time {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 10px;
  margin-top: 5px;
  opacity: 0.6;
  justify-content: flex-end;
}

/* ── Input area ──────────────────────────── */
.msg-input-wrap {
  padding: 12px 16px;
  border-top: 1px solid rgba(0,0,0,0.08);
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--white, #fff);
  flex-shrink: 0;
}

.msg-input {
  flex: 1;
  padding: 11px 16px;
  border: 1.5px solid var(--border, #e8dcc8);
  border-radius: 22px;
  font-family: inherit;
  font-size: 13.5px;
  resize: none;
  outline: none;
  background: var(--beige, #faf7f2);
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.5;
  transition: border-color 0.2s, background 0.2s;
  color: var(--text-dark, #1a1a1a);
}

.msg-input:focus {
  border-color: var(--green-mid, #5c3d2e);
  background: #fff;
}

.msg-send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--green-dark, #1b4332);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 17px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.1s;
}

.msg-send-btn:hover  { background: var(--green-mid, #5c3d2e); }
.msg-send-btn:active { transform: scale(0.93); }

/* ── New Conv Modal ──────────────────────── */
.new-conv-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(2px);
  z-index: 999;
  align-items: center;
  justify-content: center;
}

.new-conv-box {
  background: var(--white, #fff);
  border-radius: 18px;
  padding: 0;
  max-width: 460px;
  width: 92%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  overflow: hidden;
}

.new-conv-head {
  padding: 20px 22px 16px;
  border-bottom: 1px solid var(--border, #e8dcc8);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.new-conv-title {
  font-family: Amiri, serif;
  font-size: 19px;
  color: var(--green-dark, #1b4332);
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.new-conv-close {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--beige, #faf7f2);
  color: var(--text-mid, #6b7280);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: background 0.15s;
}
.new-conv-close:hover { background: #fee2e2; color: #c0392b; }

.new-conv-search-wrap {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border, #e8dcc8);
  position: relative;
}

.new-conv-search-wrap i {
  position: absolute;
  right: 26px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-mid);
  font-size: 14px;
  pointer-events: none;
}

#userResults {
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

#userResults::-webkit-scrollbar { width: 4px; }
#userResults::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.user-group-label {
  padding: 8px 16px 4px;
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--text-mid, #6b7280);
  background: var(--beige, #faf7f2);
  border-top: 1px solid var(--border, #e8dcc8);
}

.user-group-label:first-child { border-top: none; }

.user-result-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  cursor: pointer;
  transition: background 0.15s;
}

.user-result-item:hover { background: var(--beige, #faf7f2); }

.user-result-name { font-size: 13.5px; font-weight: 600; color: var(--text-dark, #1a1a1a); }
.user-result-role { font-size: 11.5px; font-weight: 600; margin-top: 1px; }

/* ── Mobile back button ──────────────────── */
.mob-back-btn {
  display: none;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: var(--green-dark, #1b4332);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  padding: 0;
  font-weight: 600;
  flex-shrink: 0;
}

/* ── Empty state ─────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 40px 20px;
  color: var(--text-mid, #6b7280);
  font-size: 13px;
  text-align: center;
}
.empty-state i { font-size: 32px; opacity: 0.4; }
.empty-state p { margin: 0; }

/* ════════════════════════════════════
   MOBILE
   ════════════════════════════════════ */
@media (max-width: 700px) {
  html {
    height: 100%;
  }

  body {
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .msg-layout {
    height: 100%;
    flex: 1;
    position: relative;
    overflow: hidden;
  }

  .msg-sidebar {
    width: 100%;
    min-width: 0;
    border-left: none;
    position: absolute;
    inset: 0;
    z-index: 2;
    transition: transform 0.25s ease;
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
  }

  .msg-sidebar.mob-hidden {
    transform: translateX(100%);
  }

  .msg-main {
    position: absolute;
    inset: 0;
    z-index: 0;
    display: none;
    transform: translateX(0);
  }

  .msg-main.mob-visible {
    display: flex !important;
    z-index: 3;
    opacity: 1;
    pointer-events: auto;
  }

  .mob-back-btn { display: flex !important; }

  /* Hide الـ empty state on Mobile */
  .msg-empty-state { display: none !important; }

  .msg-conv-head { padding: 12px 14px; }
  .msg-conv-name { font-size: 16px; }

  .msg-bubble { max-width: 85%; font-size: 14.5px; }
  .msg-bubble-wrap { max-width: 82%; }
  .msg-bubbles { padding: 14px 10px; }

  .msg-input { font-size: 15px; }
  .msg-send-btn { width: 46px; height: 46px; }
  .msg-input-wrap { padding: 10px 12px; }

  .conv-item { padding: 13px 14px; }
  .conv-name { font-size: 14.5px; max-width: 170px; }
  .conv-preview { font-size: 12.5px; }
  .msg-sidebar-head { padding: 16px 14px 12px; }
}


/* ── Delete unseen message ──────────────────────────── */
.msg-bubble-outer {
  display: flex;
  align-items: center;
  gap: 6px;
}
.msg-row.mine .msg-bubble-outer {
  flex-direction: row-reverse;
}
.msg-delete-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #bbb;
  font-size: 14px;
  padding: 5px 6px;
  border-radius: 6px;
  opacity: 0;
  transition: opacity .15s, color .15s, background .15s;
  flex-shrink: 0;
  line-height: 1;
}
.msg-row:hover .msg-delete-btn {
  opacity: 1;
}
.msg-delete-btn:hover {
  color: #e74c3c;
  background: #fde8e8;
}
/* مقروءة — رماthisة ولا تتغير عند hover */
.msg-delete-btn.seen {
  color: #ddd;
  cursor: not-allowed;
}
.msg-delete-btn.seen:hover {
  color: #ddd;
  background: none;
}


/* ── Hidden conversation indicator ── */
.conv-avatar-wrap {
  position: relative;
  flex-shrink: 0;
}
.conv-hidden-badge {
  position: absolute;
  bottom: -2px;
  left: -2px;
  background: #888;
  color: #fff;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  border: 2px solid #fff;
}
.hidden-conv {
  opacity: 0.6;
}
.hidden-conv:hover {
  opacity: 1;
}
.hidden-preview {
  color: #aaa !important;
  font-style: italic;
}

/* ── Media Buttons (صوت وImage) ── */
.msg-media-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  background: var(--beige);
  color: var(--green-dark);
  cursor: pointer;
  font-size: 15px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: 0.2s;
}
.msg-media-btn:hover { background: var(--beige2); border-color: var(--green-mid); }
.msg-media-btn.recording {
  background: #fdecea;
  border-color: #c0392b;
  color: #c0392b;
  animation: pulse-rec 1s infinite;
}
@keyframes pulse-rec {
  0%,100% { transform: scale(1); }
  50%      { transform: scale(1.1); }
}
@keyframes blink {
  0%,100% { opacity: 1; }
  50%      { opacity: 0; }
}
@keyframes msg-spin {
  to { transform: rotate(360deg); }
}

/* فقاعة الImage */
.msg-bubble img.msg-img {
  max-width: 220px;
  max-height: 260px;
  border-radius: 12px;
  display: block;
  cursor: pointer;
  object-fit: cover;
}

/* فقاعة الصوت */
.msg-bubble audio {
  max-width: 200px;
  height: 36px;
  display: block;
}


/* ── view-once tag على Messages المُرسِل ── */
.view-once-tag {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 10px; color: var(--gold); opacity: 0.8;
  margin-bottom: 2px;
}

