/* ============================================
   CHATBOT WIDGET ESTILOS
   ============================================ */

/* Aislar el chatbot del sitio - no afectar otros elementos.
   Paleta institucional UdeC (azul corporativo) */
#chatbot-widget-container {
    --udec-azul-oscuro: #002B5C;
    --udec-azul: #0066A1;
    --udec-azul-claro: #0D84C7;
    --udec-azul-mensaje: #005A8C;
    --udec-azul-halo: rgba(0, 60, 113, 0.35);
    --chat-text: #1e293b;
    --chat-text-muted: #64748b;
    --chat-surface: #f1f5f9;
    --chat-bubble-bot: #ffffff;
    --chat-bubble-border: #e2e8f0;
    --chat-msg-size: 13px;
    --chat-msg-lh: 1.58;
    position: fixed !important;
    bottom: 20px !important;
    right: 20px !important;
    z-index: 999999 !important;
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif !important;
    font-size: var(--chat-msg-size) !important;
    /* Aislar completamente del DOM del sitio */
    isolation: isolate;
    /* Asegurar que no afecte el fondo del sitio */
    background: transparent !important;
    /* Mantener display block */
    display: block !important;
    /* No permitir que el sitio afecte estos estilos */
    margin: 0 !important;
    padding: 0 !important;
}

/* Contenedor del chat */
.chat-container {
    width: 360px;
    height: 550px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    display: none; /* Oculto por defecto */
    flex-direction: column;
    overflow: hidden;
    transform: translateY(0);
    transition: transform 0.3s ease, opacity 0.3s ease;
    opacity: 1;
    /* Aislar el fondo del chatbot */
    isolation: isolate;
    /* Por debajo del FAB cuando está cerrado; al abrir, .open sube y cubre el FAB */
    position: relative;
    z-index: 0;
}

.chat-container.open {
    display: flex !important;
    /* Por encima del botón flotante (hermano .chat-toggle); evita que tape input/enviar */
    z-index: 2;
}

.chat-container.minimized {
    transform: translateY(calc(100% - 60px));
    height: 60px;
}

.chat-container.hidden {
    opacity: 0;
    pointer-events: none;
    transform: translateY(20px);
}

/* Header del chat */
.chat-header {
    background: linear-gradient(135deg, var(--udec-azul-oscuro) 0%, var(--udec-azul) 100%);
    color: white;
    padding: 12px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: move;
}

.chat-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 15px;
}

.chat-title i {
    font-size: 18px;
}

.chat-controls {
    display: flex;
    gap: 10px;
}

.minimize-btn,
.close-btn,
.new-conversation-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    font-size: 13px;
}

.minimize-btn:hover,
.close-btn:hover,
.new-conversation-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Área de mensajes */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 14px 12px 16px;
    background: var(--chat-surface, #f1f5f9);
    display: flex;
    flex-direction: column;
    gap: 14px;
    font-size: var(--chat-msg-size, 13px);
    line-height: var(--chat-msg-lh, 1.58);
    color: var(--chat-text, #1e293b);
}

/* Scrollbar personalizado */
.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Mensajes */
.message {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    animation: fadeIn 0.3s ease;
    max-width: 100%;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.bot-message {
    align-self: flex-start;
}

.message.user-message {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
    margin-top: 2px;
}

.bot-message .message-avatar {
    background: linear-gradient(135deg, var(--udec-azul-oscuro) 0%, var(--udec-azul) 100%);
    color: white;
}

/* Icono del asistente (SVG inline: siempre visible, sin depender de fuentes CDN) */
.chatbot-avatar-svg,
.chatbot-header-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 0;
    color: #ffffff;
}

.chatbot-avatar-svg svg,
.chatbot-header-icon svg {
    width: 20px;
    height: 20px;
    display: block;
}

.chat-title .chatbot-header-icon svg {
    width: 18px;
    height: 18px;
}

.user-message .message-avatar {
    background: linear-gradient(135deg, var(--udec-azul-mensaje) 0%, var(--udec-azul-claro) 100%);
    color: white;
}

.message-content {
    max-width: min(88%, 17.5rem);
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.user-message .message-content {
    align-items: flex-end;
}

.message-text {
    background: var(--chat-bubble-bot, #fff);
    color: var(--chat-text, #1e293b);
    padding: 11px 14px 12px;
    border-radius: 14px;
    border: 1px solid var(--chat-bubble-border, #e2e8f0);
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
    font-size: var(--chat-msg-size, 13px);
    line-height: var(--chat-msg-lh, 1.58);
    letter-spacing: 0.01em;
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    -webkit-hyphens: auto;
}

.message-text .message-p {
    margin: 0 0 0.55em 0;
}

.message-text .message-p:last-child {
    margin-bottom: 0;
}

.message-text .message-p--lead {
    font-weight: 600;
    color: #0f172a;
    font-size: 1.02em;
    line-height: 1.5;
}

.message-text .message-p--contact-title {
    margin-top: 0.75em;
    margin-bottom: 0;
    padding-top: 0.65em;
    border-top: 1px solid var(--chat-bubble-border, #e2e8f0);
    font-size: 0.95em;
    color: #334155;
}

.message-text--user .message-p {
    margin-bottom: 0;
}

.message-text--user .message-p + .message-p {
    margin-top: 0.45em;
}

.message-text small {
    font-size: 0.92em !important;
    line-height: 1.55;
    display: block;
    color: var(--chat-text-muted, #64748b);
}

.bot-message .message-text {
    border-top-left-radius: 6px;
}

.bot-message .message-text a {
    color: var(--udec-azul, #0066A1);
    text-decoration: underline;
    text-underline-offset: 2px;
    word-break: break-word;
}

.bot-message .message-text em,
.bot-message .message-text i {
    color: #475569;
    font-style: italic;
}

.bot-message .message-text ul,
.bot-message .message-text ol {
    margin: 0.35em 0 0.5em 1rem;
    padding-left: 0.25em;
}

.bot-message .message-text li {
    margin: 0.2em 0;
}

.bot-message .message-text p {
    margin: 0.35em 0;
}

.bot-message .message-text p:first-child {
    margin-top: 0;
}

.bot-message .message-text p:last-child {
    margin-bottom: 0;
}

.user-message .message-text {
    background: linear-gradient(145deg, var(--udec-azul-mensaje) 0%, var(--udec-azul-claro) 100%);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-top-right-radius: 6px;
    box-shadow: 0 2px 8px rgba(0, 90, 140, 0.22);
}

.user-message .message-text a {
    color: #fff;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.message-time {
    font-size: 10px;
    line-height: 1.3;
    color: var(--chat-text-muted, #64748b);
    padding: 2px 2px 0 4px;
    letter-spacing: 0.02em;
}

.user-message .message-time {
    text-align: right;
    padding-right: 2px;
}

.message-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 10px;
    line-height: 1.35;
    font-weight: 500;
    margin-top: 2px;
    padding: 0 2px;
}

.message-meta--kb {
    color: var(--udec-azul, #0066A1);
    opacity: 0.88;
}

.message-meta--friendly {
    color: #1b5e20;
    opacity: 0.9;
}

.message-contact-card {
    margin-top: 4px;
    padding: 10px 12px;
    background: #f8fafc;
    border: 1px solid var(--chat-bubble-border, #e2e8f0);
    border-radius: 10px;
    font-size: 12px;
    line-height: 1.45;
}

.message-contact-row {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    margin-bottom: 8px;
}

.message-contact-row:last-child {
    margin-bottom: 0;
}

.message-contact-icon {
    color: var(--udec-azul, #0066A1);
    margin-top: 2px;
    flex-shrink: 0;
}

.message-contact-card a {
    color: var(--udec-azul, #0066A1);
    font-weight: 500;
}

.message-source {
    font-size: 10px;
    color: var(--udec-azul);
    margin-top: 4px;
    font-style: italic;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Contenedor de input */
.chat-input-container {
    padding: 12px;
    background: white;
    border-top: 1px solid #e9ecef;
    display: flex;
    gap: 8px;
    align-items: center;
}

#messageInput {
    flex: 1;
    border: 1px solid #cbd5e1;
    border-radius: 20px;
    padding: 10px 16px;
    font-size: var(--chat-msg-size, 13px);
    line-height: 1.45;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    background: #fafbfc;
}

#messageInput:focus {
    border-color: var(--udec-azul);
    background: #fff;
    box-shadow: 0 0 0 3px rgba(0, 102, 161, 0.12);
}

#messageInput:disabled {
    background: #f8f9fa;
    cursor: not-allowed;
}

#sendBtn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, var(--udec-azul-oscuro) 0%, var(--udec-azul) 100%);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
    font-size: 14px;
}

#sendBtn:hover:not(:disabled) {
    transform: scale(1.1);
    box-shadow: 0 4px 12px var(--udec-azul-halo);
}

#sendBtn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Indicador de escritura: todo bajo #chatbot-widget-container y @keyframes con nombre único
   para que el tema u otros CSS no reemplacen la animación (p. ej. otro @keyframes typing). */
#chatbot-widget-container .typing-indicator {
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 8px;
    background: white;
    border-top: 1px solid #e9ecef;
}

#chatbot-widget-container .typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--udec-azul);
    animation: chatbotGroqTypingDots 1.4s ease-in-out infinite;
}

#chatbot-widget-container .typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

#chatbot-widget-container .typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes chatbotGroqTypingDots {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.7;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

@media (prefers-reduced-motion: reduce) {
    #chatbot-widget-container .typing-dot {
        animation-name: chatbotGroqTypingDotsReduced;
        animation-timing-function: linear;
        transform: none;
    }
}

@keyframes chatbotGroqTypingDotsReduced {
    0%, 100% {
        opacity: 0.35;
    }
    50% {
        opacity: 1;
    }
}

#chatbot-widget-container #typingText {
    font-size: 12px;
    color: var(--chat-text-muted, #64748b);
    font-style: normal;
}

/* Botón para abrir chat (debe quedar por debajo del panel cuando está abierto) */
.chat-toggle,
#chatToggle {
    position: fixed !important;
    bottom: 20px !important;
    right: 20px !important;
    width: 56px !important;
    height: 56px !important;
    border-radius: 50% !important;
    background: linear-gradient(135deg, #002B5C 0%, #0066A1 100%) !important;
    color: white !important;
    border: none !important;
    box-shadow: 0 4px 20px rgba(0, 50, 100, 0.45) !important;
    cursor: pointer !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 20px !important;
    /* Menor que .chat-container.open (z-index: 2) dentro del mismo contenedor */
    z-index: 1 !important;
    transition: transform 0.3s, box-shadow 0.3s !important;
    animation: pulse 2s infinite !important;
    /* Aislar completamente */
    isolation: isolate;
    /* Asegurar visibilidad */
    visibility: visible !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    margin: 0 !important;
    padding: 0 !important;
}

@keyframes pulse {
    0%, 100% {
        box-shadow: 0 4px 20px rgba(0, 50, 100, 0.45);
    }
    50% {
        box-shadow: 0 4px 30px rgba(0, 80, 140, 0.55);
    }
}

.chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 30px rgba(0, 50, 100, 0.5);
}

.chat-toggle.hidden {
    display: none;
}

/* Chat abierto: ocultar FAB por CSS (refuerzo si inline styles del tema compiten con JS) */
#chatbot-widget-container .chat-container.open ~ .chat-toggle,
#chatbot-widget-container .chat-container.open ~ #chatToggle {
    display: none !important;
    visibility: hidden !important;
    pointer-events: none !important;
    opacity: 0 !important;
}

/* Responsive */
@media (max-width: 480px) {
    .chat-container {
        width: calc(100vw - 40px);
        height: calc(100vh - 100px);
        max-width: 380px;
        max-height: 600px;
    }
    
    #chatbot-widget-container {
        bottom: 10px;
        right: 10px;
    }
    
    .chat-toggle {
        bottom: 10px;
        right: 10px;
        width: 55px;
        height: 55px;
    }
}

/* Estados del chatbot */
.chat-container.loading .chat-messages::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    z-index: 1;
}

/* Asegurar que el chatbot no afecte el fondo del sitio */
#chatbot-widget-container * {
    box-sizing: border-box;
}

/* Reset específico para elementos del chatbot - solo los internos */
#chatbot-widget-container {
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif !important;
    background: transparent !important;
}

/* Asegurar que elementos internos no hereden estilos del sitio */
#chatbot-widget-container .chat-container,
#chatbot-widget-container .chat-header,
#chatbot-widget-container .chat-messages,
#chatbot-widget-container .message,
#chatbot-widget-container .chat-input-container,
#chatbot-widget-container .chat-toggle {
    /* No heredar fondos del sitio */
    background-clip: padding-box !important;
}

/* Prevenir que el chatbot afecte el body o html */
body:has(#chatbot-widget-container),
html:has(#chatbot-widget-container) {
    /* El chatbot no debe cambiar nada del body/html */
}

