/* ==========================================================
   INGENIUS ACADEMY - MENSAJES CSS (VERSIÓN 5.4 OPTIMIZADA)
   ========================================================== */

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
}

:root {
  --bg-main: #0f172a;
  --bg-panel: #020617;
  --bg-chat: #020617;
  --bg-hover: #1e293b;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --gold: #f3e302;
  --accent: #22c55e;
  --msg-sent: #22c55e;
  --msg-received: #1e293b;
  --border-color: rgba(255, 255, 255, 0.08);
}

body {
  background: var(--bg-main);
  color: var(--text-main);
  padding-top: 65px;
  -webkit-font-smoothing: antialiased;
}

/* ==========================================================
   HEADER FIJO
   ========================================================== */
.header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 65px;
  background: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 25px;
  border-bottom: 1px solid #e5e7eb;
  z-index: 1000;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.header-left img {
  height: 38px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

/* ==========================================================
   LAYOUT DEL CHAT
   ========================================================== */
.app-layout {
  display: flex;
  height: calc(100vh - 65px);
}

.chat-container {
  margin-left: 230px;
  width: calc(100% - 230px);
  height: calc(100vh - 65px);
  display: flex;
  background: #111827;
}

/* Lista de Chats (Sidebar Izquierdo) */
/* Lista de Chats (Sidebar Izquierdo) */
.chat-list {
  width: 400px;         /* Antes era 320px, ahora más ancho y cómodo */
  min-width: 400px;     /* Asegura que no se contraiga */
  background: #111827;
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.chat-list h3 {
  padding: 16px 16px 8px 16px;
  font-size: 1.1rem;
  color: #fff;
}

.chat-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.2s ease;
  position: relative;
  background: transparent;
  border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}

.chat-item:hover {
  background: var(--bg-hover);
}

.chat-item.active {
  background: rgba(245, 158, 11, 0.1);
  border-left: 4px solid var(--gold);
}

.chat-avatar {
  width: 46px;
  height: 46px;
  min-width: 46px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--border-color);
}

.chat-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
}

.chat-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-top strong {
  color: #fff;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.last-time {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.last-msg {
  color: var(--text-muted);
  font-size: 0.82rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Insignias de Roles y Premium */
.role-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  margin-left: 6px;
  vertical-align: middle;
}
.role-badge.premium {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #000;
}
.role-badge.admin {
  background: #ef4444;
  color: #fff;
}
.role-badge.docente, .role-badge.profesor {
  background: #38bdf8;
  color: #000;
}
.role-badge.estudiante {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-muted);
}

/* Estados (En línea / Inactivo) */
.estado {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.estado::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #94a3b8;
}
.estado.online { color: #10b981; }
.estado.online::before { background: #10b981; }
.estado.offline { color: #94a3b8; }
.estado.offline::before { background: #94a3b8; }

/* Ventana Principal del Chat */
.chat-window {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #0f172a;
  position: relative;
}

.chat-header {
  background: #111827;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 14px;
}

#chatNombre {
  color: #fff;
  font-size: 1.05rem;
  font-weight: 600;
}

.back-btn {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  display: none;
  padding: 4px 8px;
}

.chat-messages {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: #090d16;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Burbujas de Mensajes */
.msg {
  max-width: 72%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 0.9rem;
  line-height: 1.4;
  word-break: break-word;
  position: relative;
}

.msg-sent {
  margin-left: auto;
  background: var(--gold);
  color: #000;
  border-bottom-right-radius: 4px;
  font-weight: 500;
}

.msg-received {
  background: #1f2937;
  color: #fff;
  border-bottom-left-radius: 4px;
}

.msg-text a {
  color: inherit;
  text-decoration: underline;
  font-weight: 600;
}

/* Adjuntos (Imágenes y Documentos) */
.msg-attachment {
  margin-bottom: 6px;
  border-radius: 8px;
  overflow: hidden;
  max-width: 240px;
}
.msg-attachment img {
  width: 100%;
  height: auto;
  border-radius: 8px;
  display: block;
  object-fit: cover;
}
.msg-file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
  color: inherit;
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
}
.msg-file-card i {
  font-size: 1.3rem;
  color: var(--gold);
}

.msg-meta {
  font-size: 10px;
  margin-top: 4px;
  text-align: right;
  color: rgba(0, 0, 0, 0.6);
}
.msg-received .msg-meta { color: var(--text-muted); }
.msg-meta.visto { color: #0284c7 !important; font-weight: 600; }

/* Formulario de Envío de Mensajes */
#chatForm {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: #111827;
  border-top: 1px solid var(--border-color);
  position: relative;
}

.chat-upload-status {
  margin: 0 16px 10px;
  padding: 12px 14px;
  border: 1px solid rgba(212, 175, 55, 0.28);
  border-radius: 14px;
  color: #e5e7eb;
  background: linear-gradient(135deg, rgba(212, 175, 55, 0.1), rgba(31, 41, 55, 0.92));
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  font-size: 0.82rem;
  line-height: 1.45;
}

.chat-upload-status[hidden] {
  display: none;
}

.chat-upload-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.chat-upload-message {
  min-width: 0;
  overflow-wrap: anywhere;
  font-weight: 650;
}

.chat-upload-percent {
  flex: 0 0 auto;
  color: #f5d76e;
  font-size: 0.9rem;
  font-weight: 800;
}

.chat-upload-track {
  height: 9px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.38);
}

.chat-upload-track[hidden] {
  display: none;
}

.chat-upload-progress {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #d4af37, #ffe58a);
  box-shadow: 0 0 12px rgba(212, 175, 55, 0.55);
  transition: width 180ms ease;
}

.chat-upload-help {
  margin: 7px 0 0;
  color: #aeb8c7;
  font-size: 0.72rem;
}

.chat-upload-status[data-state="success"] {
  border-color: rgba(74, 222, 128, 0.35);
  background: linear-gradient(135deg, rgba(22, 101, 52, 0.3), rgba(31, 41, 55, 0.92));
}

.chat-upload-status[data-state="error"] {
  border-color: rgba(248, 113, 113, 0.4);
  background: linear-gradient(135deg, rgba(127, 29, 29, 0.3), rgba(31, 41, 55, 0.92));
}

#chatForm input[type="text"] {
  flex: 1;
  padding: 10px 16px;
  border-radius: 20px;
  border: 1px solid var(--border-color);
  background: #1f2937;
  color: #fff;
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s;
}
#chatForm input[type="text"]:focus {
  border-color: var(--gold);
}

.chat-action-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}
.chat-action-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--gold);
}

/* Popup Flotante de Emojis */
.emoji-picker-popup {
  position: absolute;
  bottom: 70px;
  left: 16px;
  background: #1e293b;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 14px;
  padding: 10px;
  display: flex;
  gap: 8px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.5);
  z-index: 1000;
}
.emoji-picker-popup.hidden {
  display: none;
}
.emoji-option {
  background: transparent;
  border: none;
  font-size: 22px;
  cursor: pointer;
  padding: 4px;
  transition: transform 0.15s ease;
}
.emoji-option:hover {
  transform: scale(1.25);
}

/* Badges de no leídos */
.unread-badge {
  background: #ef4444 !important;
  color: #fff !important;
  border-radius: 12px !important;
  padding: 2px 7px !important;
  font-size: 11px !important;
  font-weight: 700 !important;
  position: absolute !important;
  right: 16px !important;
  top: 50% !important;
  transform: translateY(-50%) !important;
  box-shadow: 0 0 10px rgba(239, 68, 68, 0.5);
}

.unread-total-badge {
  background: #ef4444 !important;
  color: #fff !important;
  border-radius: 10px !important;
  padding: 1px 6px !important;
  font-size: 10px !important;
  font-weight: 700 !important;
  margin-left: auto !important;
  display: none;
}
.unread-total-badge.show {
  display: inline-flex !important;
  align-items: center;
  justify-content: center;
}

/* ==========================================================
   MODO CLARO ESTRICTO (`body.light`)
   ========================================================== */
body.light {
  background: #f8fafc;
  color: #0f172a;
}

body.light .chat-container {
  background: #ffffff;
}

body.light .chat-list {
  background: #ffffff;
  border-right: 1px solid #cbd5e1;
}

body.light .chat-item:hover {
  background: #f1f5f9;
}

body.light .chat-item.active {
  background: rgba(245, 158, 11, 0.12);
}

body.light .chat-top strong {
  color: #0f172a;
}

body.light .last-msg {
  color: #475569;
}

body.light .chat-window {
  background: #f8fafc;
}

body.light .chat-header {
  background: #ffffff;
  border-bottom: 1px solid #cbd5e1;
}

body.light #chatNombre {
  color: #0f172a;
}

body.light .chat-messages {
  background: #f1f5f9;
}

body.light .msg-received {
  background: #ffffff;
  color: #0f172a;
  border: 1px solid #cbd5e1;
}

body.light #chatForm {
  background: #ffffff;
  border-top: 1px solid #cbd5e1;
}

body.light .chat-upload-status {
  color: #1f2937;
  background: linear-gradient(135deg, #fff8df, #f8fafc);
}

body.light .chat-upload-status[data-state="success"] {
  background: linear-gradient(135deg, #ecfdf5, #f8fafc);
}

body.light .chat-upload-status[data-state="error"] {
  background: linear-gradient(135deg, #fef2f2, #f8fafc);
}

body.light .chat-upload-help {
  color: #64748b;
}

body.light #chatForm input[type="text"] {
  background: #f1f5f9;
  color: #0f172a;
  border: 1px solid #cbd5e1;
}

/* ==========================================================
   RESPONSIVIDAD MÓVIL (MESSENGER STYLE)
   ========================================================== */
@media (max-width: 760px) {
  .menu_side {
    display: none;
  }
  .chat-container {
    margin-left: 0;
    width: 100%;
  }
  .chat-list {
    width: 100%;
    display: block;
  }
  .chat-list.hidden {
    display: none !important;
  }
  .chat-window {
    position: fixed;
    top: 65px;
    left: 0;
    width: 100%;
    height: calc(100vh - 65px);
    display: none;
    flex-direction: column;
    z-index: 3000;
  }
  .chat-window.active {
    display: flex;
  }
  .back-btn {
    display: inline-flex;
  }
}


/* --- CORRECCIONES DE CONTRASTE Y UI (VERSIÓN 5.4) --- */

/* 🔍 Buscador en la lista de chats (Contraste Modo Claro y Oscuro) */
.chat-search-container input,
.chat-list input {
  width: 100%;
  padding: 10px 14px 10px 36px !important;
  border-radius: 12px !important;
  border: 1px solid var(--border-color, rgba(255, 255, 255, 0.15)) !important;
  background: var(--panel, #1f2937) !important;
  color: #fff !important;
  font-size: 0.9rem !important;
  outline: none;
}

body.light .chat-search-container input,
body.light .chat-list input {
  background: #ffffff !important;
  color: #0f172a !important;
  border-color: #cbd5e1 !important;
}
body.light .chat-search-container input::placeholder,
body.light .chat-list input::placeholder {
  color: #64748b !important;
}

/* 🏷️ Normalización de Insignias de Rol */
.role-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  margin-left: 6px;
  vertical-align: middle;
}
.role-badge.premium {
  background: linear-gradient(135deg, #f59e0b, #e0d178);
  color: #000;
}
.role-badge.admin {
  background: #ef4444;
  color: #fff;
}
.role-badge.docente, .role-badge.profesor {
  background: #0284c7;
  color: #fff;
}
.role-badge.estudiante {
  background: rgba(148, 163, 184, 0.2);
  color: #94a3b8;
}
body.light .role-badge.estudiante {
  background: #e2e8f0;
  color: #475569;
}

/* 🗑️ Botón para eliminar mensaje propio (dentro del tiempo límite) */
.delete-msg-btn {
  background: transparent;
  border: none;
  color: inherit;
  opacity: 0.6;
  font-size: 11px;
  cursor: pointer;
  margin-left: 8px;
  transition: opacity 0.2s;
}
.delete-msg-btn:hover {
  opacity: 1;
  color: #ef4444;
}

/* 🖼️ Modal o visor de imagen ampliada */
.image-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99999;
  padding: 20px;
  backdrop-filter: blur(5px);
}
.image-modal-content {
  position: relative;
  max-width: 90vw;
  max-height: 90vh;
}
.image-modal-content img {
  width: 100%;
  height: auto;
  max-height: 85vh;
  object-fit: contain;
  border-radius: 12px;
  box-shadow: 0 25px 50px rgba(0,0,0,0.8);
}
.image-modal-close {
  position: absolute;
  top: -15px;
  right: -15px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #ef4444;
  color: #fff;
  border: none;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.4);
}


/* --- FILTROS DE CHAT Y SCROLL PROFESIONAL (VERSIÓN 5.4) --- */

/* Ocultar barra de desplazamiento manteniendo el scroll fluido */
.chat-list {
  scrollbar-width: none !important; /* Firefox */
  -ms-overflow-style: none !important; /* Internet Explorer 10+ */
}
.chat-list::-webkit-scrollbar {
  display: none !important; /* Chrome, Safari, Opera */
}

/* Contenedor de los botones de filtro rápido */
.chat-filters-bar {
  display: flex;
  gap: 6px;
  padding: 8px 12px 12px 12px;
  overflow-x: auto;
  scrollbar-width: none;
  border-bottom: 1px solid var(--border-color, rgba(255,255,255,0.08));
}
.chat-filters-bar::-webkit-scrollbar {
  display: none;
}

.filter-chip {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-color, rgba(255,255,255,0.1));
  color: var(--text-muted, #94a3b8);
  padding: 5px 10px;
  border-radius: 16px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s ease;
}
.filter-chip:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}
.filter-chip.active {
  background: var(--gold, #f59e0b) !important;
  color: #000 !important;
  border-color: var(--gold, #f59e0b) !important;
}

/* Modo claro para los chips de filtro */
body.light .filter-chip {
  background: #f1f5f9;
  border-color: #cbd5e1;
  color: #475569;
}
body.light .filter-chip:hover {
  background: #e2e8f0;
  color: #0f172a;
}
body.light .filter-chip.active {
  background: var(--gold, #f59e0b) !important;
  color: #000 !important;
}

/* --- CORRECCIÓN DE POSICIONAMIENTO FIJO PARA BUSCADOR Y FILTROS --- */

.chat-list {
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden !important; /* Evita desbordamientos generales en la lista */
}

/* Fijar el título, buscador y filtros arriba para que no se muevan al hacer scroll */
.chat-list h3,
.chat-search-container,
.chat-filters-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: #111827; /* Fondo sólido para que no se transparenten los chats */
}

body.light .chat-list h3,
body.light .chat-search-container,
body.light .chat-filters-bar {
  background: #ffffff !important;
}

/* Ajustes de orden individual para los elementos fijos superiores */
.chat-list h3 {
  padding: 14px 16px 4px 16px;
}
.chat-search-container {
  padding: 8px 16px !important;
}
.chat-filters-bar {
  padding: 6px 16px 12px 16px !important;
  border-bottom: 1px solid var(--border-color, rgba(255,255,255,0.08));
}

/* Contenedor exclusivo para los chats que sí tendrá scroll fluido */
#listaChats {
  flex: 1;
  overflow-y: auto !important;
  scrollbar-width: none !important;
  -ms-overflow-style: none !important;
}
#listaChats::-webkit-scrollbar {
  display: none !important;
}

/* --- BOTÓN DE CARGAR MENSAJES ANTERIORES --- */
.load-more-container {
  text-align: center;
  padding: 10px 0;
  margin-bottom: 12px;
}
.load-more-btn {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--border-color, rgba(255, 255, 255, 0.15));
  color: var(--text-main, #f8fafc);
  padding: 6px 18px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.load-more-btn:hover {
  background: var(--gold, #f59e0b);
  color: #000;
  border-color: var(--gold, #f59e0b);
}
body.light .load-more-btn {
  background: #e2e8f0;
  color: #0f172a;
  border-color: #cbd5e1;
}
body.light .load-more-btn:hover {
  background: var(--gold, #f59e0b);
  color: #000;
}

/* Ajustes de ancho para tablets y pantallas compactas */
@media (min-width: 761px) and (max-width: 1100px) {
  .chat-container {
    min-width: 0;
  }

  .chat-list {
    width: clamp(280px, 34vw, 360px);
    min-width: 280px;
    flex: 0 0 auto;
  }

  .chat-window {
    min-width: 0;
  }

  .chat-messages {
    min-width: 0;
    padding: 16px;
  }
}

@media (max-width: 760px) {
  body.messages-page {
    min-width: 0;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .chat-container {
    position: fixed;
    top: 65px;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    height: auto;
    min-height: 0;
    margin: 0;
  }

  .chat-list {
    display: flex;
    width: 100%;
    min-width: 0;
    min-height: 0;
    flex: 1 1 auto;
  }

  #listaChats {
    min-height: 0;
    overscroll-behavior: contain;
  }

  .chat-window {
    top: 65px;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    height: auto;
    min-width: 0;
    min-height: 0;
  }

  .chat-header {
    min-height: 56px;
    padding: 8px 12px;
  }

  #chatNombre {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .chat-messages {
    min-height: 0;
    padding: 12px;
    overscroll-behavior: contain;
  }

  .msg {
    max-width: min(88%, 520px);
    overflow-wrap: anywhere;
  }

  .msg-attachment {
    max-width: min(240px, 70vw);
  }

  #chatForm {
    flex: 0 0 auto;
    gap: 4px;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  }

  #chatForm input[type="text"] {
    min-width: 0;
    padding: 10px 12px;
    font-size: 16px;
  }

  .chat-action-btn {
    flex: 0 0 38px;
    width: 38px;
    height: 38px;
    padding: 6px;
  }

  .chat-upload-status {
    margin: 0 8px 8px;
    padding: 10px 12px;
  }

  .emoji-picker-popup {
    right: 8px;
    left: 8px;
    flex-wrap: wrap;
    justify-content: space-around;
    gap: 4px;
    bottom: calc(62px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 360px) {
  .chat-messages {
    padding: 10px 8px;
  }

  #chatForm {
    gap: 2px;
    padding-right: 5px;
    padding-left: 5px;
  }

  .chat-action-btn {
    flex-basis: 34px;
    width: 34px;
    height: 38px;
    font-size: 16px;
  }

  #chatForm input[type="text"] {
    padding-right: 8px;
    padding-left: 8px;
  }
}