/* css/chat.css */
:root {
    --chat-border: #e2e8f0;
    --chat-bg-side: #f8fafc;
    --chat-bg-main: #ffffff;
}

body.dark-mode {
    --chat-border: #1e293b;
    --chat-bg-side: #0f172a;
    --chat-bg-main: #020617;
}

#chat-ventana {
    --border-color: var(--chat-border);
    --bg-body: var(--chat-bg-side);
    --bg-header: var(--sidebar-bg);
    --card-bg: var(--chat-bg-main);
    --text-main: var(--text-main);
    --text-muted: var(--text-muted);
}

/* BOTÓN FLOTANTE (Oculto, el disparador ahora está en el sidebar) */
#chat-flotante {
    display: none !important;
}

/* VENTANA DEL CHAT */
#chat-ventana {
    position: fixed;
    bottom: 25px;
    left: 280px;
    /* Offset de la barra lateral (260px + 20px) */
    width: 700px;
    height: 600px;
    max-height: calc(100vh - 50px);
    max-width: calc(100vw - 320px);
    background: var(--surface);
    border-radius: 16px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
    display: none;
    z-index: 9998;
    border: 1px solid var(--border);
    overflow: hidden;
    animation: chatSlideIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes chatSlideIn {
    from {
        transform: translateX(-20px);
        opacity: 0;
    }

    to {
        transform: translateX(0);
        opacity: 1;
    }
}

#chat-ventana.abierto {
    display: flex;
}

#chat-ventana.abierto {
    display: flex;
}

/* COLUMNA IZQUIERDA */
.chat-izq {
    width: 300px;
    border-right: 2px solid var(--border-color);
    display: flex;
    flex-direction: column;
    background: var(--bg-body);
}

.chat-cabecera {
    padding: 15px;
    font-weight: bold;
    font-size: 1.1em;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.chat-cabecera-izq {
    background: var(--bg-header);
    color: white;
    padding: 20px 15px;
}

.lista-contactos {
    flex: 1;
    overflow-y: auto;
}

.contacto-item {
    padding: 15px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-main);
}

.contacto-item:hover {
    background: var(--card-bg);
}

.contacto-item.activo {
    background: rgba(52, 152, 219, 0.1);
    border-left: 4px solid var(--primary);
}

.contacto-nombre {
    font-weight: 600;
    font-size: 0.95em;
}

.contacto-rol {
    font-size: 0.75em;
    color: var(--text-muted);
    text-transform: uppercase;
}

/* COLUMNA DERECHA */
.chat-der {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--card-bg);
    border-left: 1px solid var(--border-color);
}

.chat-cabecera-der {
    background: var(--bg-body);
    color: var(--text-main);
    border-bottom: 2px solid var(--border-color);
    padding: 20px 15px;
}

.chat-header-title {
    cursor: pointer;
    font-size: 0.9em;
    font-weight: 700;
}

.mensajes-area {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background-color: var(--bg-body);
}

.mensaje {
    max-width: 75%;
    padding: 10px 15px;
    border-radius: 12px;
    font-size: 0.95em;
    position: relative;
    color: var(--text-main);
}

.mensaje.mio {
    align-self: flex-end;
    background: #dcf8c6;
    border-bottom-right-radius: 2px;
}

.mensaje.suyo {
    align-self: flex-start;
    background: white;
    border-bottom-left-radius: 2px;
    border: 1px solid var(--border-color);
}

.mensaje-hora {
    font-size: 0.7em;
    color: rgba(0, 0, 0, 0.5);
    text-align: right;
    margin-top: 5px;
    display: block;
}

.estado-leido {
    margin-left: 3px;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.4);
    font-size: 1.1em;
}

.estado-leido.leido {
    color: #34b7f1; /* Color azul estilo WhatsApp */
}

/* UTILIDADES Y BOTONES REFACORIZADOS DE INLINE A CLASES */
.btn-cerrar {
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
}

.btn-cerrar-izq {
    color: white;
    font-size: 1.2em;
    padding: 0;
    display: none;
}

.btn-cerrar-der {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
    width: 30px;
    height: 30px;
    border-radius: 50%;
    font-size: 0.9em;
    transition: 0.2s;
}

.btn-cerrar-der:hover {
    background: var(--danger);
    color: white;
}

.chat-empty-msg {
    text-align: center;
    color: var(--text-muted);
    padding: 20px;
}

.chat-empty-margin {
    margin-top: 50%;
}

.input-area {
    padding: 15px;
    background: var(--card-bg);
    display: flex;
    gap: 10px;
    border-top: 2px solid var(--border-color);
    transition: opacity 0.3s;
}

.input-disabled {
    opacity: 0.5;
    pointer-events: none;
}

.input-area input {
    flex: 1;
    padding: 10px 15px;
    border-radius: 20px;
    border: 1px solid var(--border-color);
    outline: none;
    background: var(--bg-body);
    color: var(--text-main);
}

.input-area input:focus {
    border-color: var(--primary);
}

.btn-enviar {
    background: var(--primary);
    color: white;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.btn-enviar:hover {
    background: var(--primary-hover);
}

/* VISOR DE PREGUNTAS EN CHAT */
.btn-ver-pregunta {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.4);
    color: inherit;
    padding: 6px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    margin: 5px 0;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.modal-visor-chat {
    z-index: 100000;
}

.modal-visor-content {
    max-width: 700px;
    width: 90%;
    border-radius: 20px;
    padding: 0;
    overflow: hidden;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.4);
}

.visor-titulo {
    color: var(--primary);
    font-weight: 800;
}

.visor-enunciado {
    font-size: 1.1em;
    font-weight: 700;
    color: var(--text-main);
    line-height: 1.6;
}

.visor-opciones-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 10px;
}

/* OPCIONES RESPUESTA */
.opcion-chat {
    background: var(--surface);
    color: var(--text-main);
    padding: 15px 20px;
    border-radius: 12px;
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 12px;
    transition: all 0.2s;
    font-size: 0.95em;
}

.opcion-chat:hover {
    background: var(--app-bg);
}

.opcion-chat.correcta {
    background: rgba(16, 185, 129, 0.08); /* Success Alpha */
    color: #065f46;
    border-color: #10b981;
    font-weight: 700;
    box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.1);
}

body.dark-mode .opcion-chat.correcta {
    background: rgba(16, 185, 129, 0.15);
    color: #34d399;
    border-color: #059669;
}

/* TARJETA DE EXAMEN EN CHAT */
.chat-examen-card {
    background: white;
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 15px;
    margin: 10px 0;
    display: flex;
    gap: 15px;
    align-items: center;
    box-shadow: var(--shadow-sm);
    min-width: 250px;
    max-width: 100%;
}

body.dark-mode .chat-examen-card {
    background: var(--surface);
    border-color: var(--border);
}

.chat-examen-icon {
    font-size: 2em;
    background: rgba(37, 99, 235, 0.1);
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
}

.chat-examen-info {
    flex: 1;
}

.chat-examen-title {
    font-weight: 800;
    font-size: 0.9em;
    color: var(--text-main);
    margin-bottom: 8px;
    line-height: 1.3;
}

.btn-examen-chat {
    background: var(--primary);
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 0.8em;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    width: 100%;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.btn-examen-chat:hover {
    background: #1d4ed8;
    transform: translateY(-1px);
}

.mensaje.mio .chat-examen-card {
    background: rgba(255, 255, 255, 0.7);
}

/* GRUPOS DE CLASES EN CHAT */
.chat-grupo-clase {
    border-bottom: 1px solid var(--border);
}

.chat-grupo-header {
    padding: 12px 15px;
    background: rgba(37, 99, 235, 0.03);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 800;
    font-size: 0.85em;
    color: var(--primary);
    transition: all 0.2s;
}

.chat-grupo-header:hover {
    background: rgba(37, 99, 235, 0.08);
}

.chat-grupo-usuarios {
    display: none;
    background: white;
}

body.dark-mode .chat-grupo-usuarios {
    background: var(--surface);
}

.chat-grupo-clase.abierto .chat-grupo-usuarios {
    display: block;
}

.chat-grupo-clase.abierto .chat-grupo-header {
    border-bottom: 1px solid var(--border);
    background: rgba(37, 99, 235, 0.1);
}

.badge-count {
    background: var(--border);
    color: var(--text-muted);
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 0.8em;
}

.chat-separator {
    padding: 10px 15px;
    font-size: 0.7em;
    font-weight: 800;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    background: var(--app-bg);
}

.chat-broadcast-msg {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 40px 20px;
    height: 100%;
}

.chat-broadcast-msg h3 {
    color: var(--primary);
    font-weight: 800;
}

.btn-difundir-chat {
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 0.9em;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-difundir-chat:hover {
    background: #1d4ed8;
    transform: scale(1.1);
}

/* MODO OSCURO & MÓVIL */
body.dark-mode .mensaje.mio {
    background: #064e3b;
    color: white;
}

body.dark-mode .mensaje.suyo {
    background: #1e293b;
    color: white;
    border-color: #334155;
}

body.dark-mode .mensaje-hora {
    color: rgba(255, 255, 255, 0.6);
}

body.dark-mode .estado-leido:not(.leido) {
    color: rgba(255, 255, 255, 0.4);
}

@media (max-width: 768px) {
    #chat-ventana {
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        max-height: 100vh !important;
        max-width: 100vw !important;
        border-radius: 0 !important;
        flex-direction: column !important;
        z-index: 30000 !important; /* Por encima del sidebar y top-bar */
    }

    /* Vista normal (lista de contactos) */
    .chat-izq {
        width: 100% !important;
        border-right: none !important;
        flex: 1 1 100% !important;
        height: 100% !important;
        display: flex !important;
    }

    .chat-der {
        display: none !important; /* Ocultar chat derecho si no hay contacto seleccionado */
    }

    /* Vista de conversación seleccionada (Colapso total de chat-izq) - Dani */
    #chat-ventana.contacto-seleccionado .chat-izq {
        display: none !important; /* Colapso absoluto de la barra izquierda */
    }

    #chat-ventana.contacto-seleccionado .chat-der {
        display: flex !important; /* Chat derecho toma el 100% */
        width: 100% !important;
        height: 100% !important;
        flex: 1 1 100% !important;
        border-left: none !important;
    }

    /* Botón premium de retroceso (Volver) en el título del chat - Dani */
    #chat-ventana.contacto-seleccionado .chat-header-title {
        display: inline-flex !important;
        align-items: center !important;
        gap: 12px !important;
        background: var(--primary) !important;
        color: white !important;
        padding: 8px 18px !important;
        border-radius: 99px !important;
        font-size: 0.85em !important;
        font-weight: 800 !important;
        box-shadow: 0 4px 15px rgba(28, 117, 188, 0.3) !important;
        cursor: pointer !important;
        transition: all 0.2s ease !important;
        max-width: 85% !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    #chat-ventana.contacto-seleccionado .chat-header-title::before {
        content: '⬅ Volver';
        font-size: 0.85em !important;
        background: rgba(255, 255, 255, 0.25) !important;
        padding: 4px 12px !important;
        border-radius: 99px !important;
        font-weight: 900 !important;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    #chat-ventana.contacto-seleccionado .chat-header-title:active {
        transform: scale(0.95) !important;
        background: var(--primary-hover) !important;
    }

    /* Estilización Premium del botón de cerrar chat de la izquierda */
    .btn-cerrar-izq {
        display: flex !important;
        background: rgba(255, 255, 255, 0.15) !important;
        color: white !important;
        width: 32px !important;
        height: 32px !important;
        border-radius: 50% !important;
        align-items: center !important;
        justify-content: center !important;
        transition: all 0.2s ease !important;
        border: none !important;
        font-size: 0.9em !important;
    }

    .btn-cerrar-izq:hover {
        background: rgba(255, 255, 255, 0.3) !important;
        transform: scale(1.1) !important;
    }

    .btn-cerrar-der {
        display: none !important; /* El botón de retroceso virtual suplanta al botón de cerrar normal */
    }

    /* Ajustar áreas del chat en móviles */
    .mensajes-area {
        padding: 15px !important;
    }

    .mensaje {
        max-width: 85% !important;
        font-size: 0.9em !important;
    }

    .input-area {
        padding: 10px 15px !important;
    }
}

/* SEPARADOR DE FECHAS */
.chat-date-separator {
    display: flex;
    align-items: center;
    text-align: center;
    margin: 20px 0;
    color: var(--text-muted);
    font-size: 0.75em;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.chat-date-separator::before,
.chat-date-separator::after {
    content: '';
    flex: 1;
    border-bottom: 1px solid var(--border-color);
}

.chat-date-separator:not(:empty)::before {
    margin-right: 1.5em;
}

.chat-date-separator:not(:empty)::after {
    margin-left: 1.5em;
}

body.dark-mode .chat-date-separator::before,
body.dark-mode .chat-date-separator::after {
    border-bottom-color: rgba(255, 255, 255, 0.1);
}