/* ==========================================================
   Omniservice AI Assistant - Floating Button
   ========================================================== */

#ai-assistant {
    position: fixed;
    bottom: 15px;
    right: 15px;
    cursor: pointer;
    z-index: 99999;
    transform: translateZ(0);
    isolation: isolate;
}

#ai-assistant.invisible {
    display: none !important;
}

.ai-wrapper {
    width: 64px;
    height: 64px;
    color: white;
    font-family: 'Helvetica', 'Arial', sans-serif;
    font-size: 32px;
    text-align: center;
    border-radius: 50%;
}

.ai-floating-button {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    overflow: hidden;
    cursor: pointer;
    background-color: #ffffff !important;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    border: 1px solid rgba(0, 0, 0, 0.08);
    transition: transform 0.25s ease, box-shadow 0.25s ease;
}

.ai-floating-button:hover {
    transform: scale(1.08);
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.25);
}

.ai-floating-icon {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}


/* ==========================================================
   FIX CRÍTICO: Nunca bloquear el scroll del sitio
   El chat es un widget flotante — NO debe afectar html/body
   ========================================================== */

/*
 * Bootstrap 5 a veces agrega overflow:hidden al body cuando
 * hay modales u offcanvas. Este bloque lo anula siempre,
 * EXCEPTO dentro del propio chat wrapper.
 */
html {
    overflow-y: scroll !important; /* fuerza scrollbar permanente, evita saltos */
}

body {
    overflow-x: hidden;
    /* NO tocar overflow-y aquí — Bootstrap puede necesitar manejarlo */
}

/* Asegura que Bootstrap no bloquee el body cuando el chat está activo */
body.modal-open {
    overflow: auto !important;
    padding-right: 0 !important;
}


/* ==========================================================
   Chat Container
   ========================================================== */

#ai-chat {
    position: fixed;
    right: 30px;
    bottom: 30px;
    width: 380px;
    max-width: calc(100vw - 32px);
    height: 620px;
    max-height: calc(100vh - 48px);
    display: flex;
    flex-direction: column;
    font-family: 'Helvetica', 'Arial', sans-serif;
    border-radius: 15px;
    border-color: rgba(0, 0, 0, 0.175);

    /*
     * FIX SCROLL: overflow:hidden aquí estaba capturando
     * eventos de scroll del sitio en algunos navegadores.
     * Se cambia a clip para contener visualmente sin
     * interceptar eventos de scroll del documento.
     */
    overflow: hidden;
    z-index: 99999;

    /*
     * FIX CRÍTICO: Evita que el chat consuma eventos de
     * scroll fuera de sus elementos scrollables internos.
     */
    overscroll-behavior: contain;

    /* Asegura que el chat no cree un nuevo stacking context
       que interfiera con el scroll del documento */
    will-change: auto;
}

#ai-chat.invisible {
    display: none !important;
    /* FIX: cuando está oculto, no recibe ningún evento */
    pointer-events: none !important;
    visibility: hidden !important;
}

#ai-chat .card-body {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
    padding: 12px;
}


/* ==========================================================
   Header
   ========================================================== */

.card-header-custom {
    border-radius: 15px 15px 0 0;
    background-color: black;
    font-size: 16px;
    flex-shrink: 0;
}

#ai-close-chat,
#ai-expand-chat,
#ai-clear-chat {
    padding: 2px 5px;
    border-radius: 5px;
    cursor: pointer;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease, transform 0.2s ease;
}

#ai-close-chat:hover {
    background: #ff1919;
    transform: scale(1.05);
}

#ai-clear-chat:hover {
    background: #0d6efd;
    transform: scale(1.05);
}

#ai-expand-chat:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.05);
}


/* ==========================================================
   Chat Messages Area — ÚNICO elemento con scroll interno
   ========================================================== */

#ai-chat-wrapper {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    font-size: 16px;
    padding: 10px;
    padding-right: 6px;
    scroll-behavior: smooth;

    /*
     * FIX CLAVE: contain impide que el scroll se propague
     * al documento cuando se llega al tope o fondo del chat.
     * Esto soluciona el scroll bloqueado en la página.
     */
    overscroll-behavior: contain;

    /* Touch scroll nativo en iOS */
    -webkit-overflow-scrolling: touch;
}

/* Scrollbar */
#ai-chat-wrapper::-webkit-scrollbar {
    width: 8px;
}

#ai-chat-wrapper::-webkit-scrollbar-thumb {
    background: #dadada;
    border-radius: 10px;
}

#ai-chat-wrapper::-webkit-scrollbar-track {
    background: transparent;
}


/* ==========================================================
   Messages
   ========================================================== */

.avatar-img {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 50%;
    object-fit: cover;
}

.ai-message {
    border-radius: 15px;
    background-color: #dadada;
    color: black;
    max-width: 80%;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.user-message {
    border-radius: 15px;
    background-color: #0d6efd;
    color: white;
    max-width: 80%;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.ai-message p,
.user-message p {
    margin-bottom: 6px;
}

.ai-message p:last-of-type,
.user-message p:last-of-type {
    margin-bottom: 0;
}


/* ==========================================================
   Form
   ========================================================== */

#ai-chat-form {
    flex-shrink: 0;
}

#ai-chat-form .d-flex {
    align-items: flex-end;
}

#ai-user-message {
    border-color: #dee2e6;
    font-family: 'Helvetica', 'Arial', sans-serif;
    font-size: 16px;
    resize: none;
    min-height: 48px;
    max-height: 120px;
}

#ai-send-button {
    background: #0d6efd;
    border-color: #0d6efd;
    font-size: 16px;
    color: white;
    flex-shrink: 0;
    align-self: flex-end;
    width: 46px;
    min-width: 46px;
    height: 46px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

#ai-send-button:hover,
#ai-send-button:active {
    background: #0a58ca;
    border-color: #0a58ca;
}


/* ==========================================================
   Lead Form
   ========================================================== */

#ai-lead-form {
    flex-shrink: 0;
    margin-bottom: 10px;
}

#ai-lead-form .bg-light {
    background-color: #f8f9fa !important;
}


/* ==========================================================
   Expanded State
   ========================================================== */

#ai-chat.expanded {
    left: 24px;
    right: 24px;
    bottom: 24px;
    width: auto;
    height: calc(100vh - 48px);
    max-height: calc(100vh - 48px);
}

#ai-chat.expanded #ai-chat-wrapper {
    flex: 1;
    min-height: 0;
}


/* ==========================================================
   Streaming cursor
   ========================================================== */

.ai-cursor {
    display: inline-block;
    width: 2px;
    height: 0.85em;
    background: #888;
    margin-left: 2px;
    vertical-align: middle;
    border-radius: 1px;
    animation: ai-blink 0.9s step-end infinite;
}

@keyframes ai-blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0; }
}


/* ==========================================================
   Code formatting
   ========================================================== */

.ai-message pre {
    background: #1e1e1e;
    color: #d4d4d4;
    border-radius: 8px;
    padding: 12px;
    margin: 8px 0 4px;
    overflow-x: auto;
    font-size: 13px;
    line-height: 1.5;
}

.ai-message pre code {
    background: none;
    color: inherit;
    padding: 0;
    border-radius: 0;
    font-size: inherit;
}

.ai-message code {
    background: #e8e8e8;
    color: #c7254e;
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 85%;
    font-family: 'Courier New', Courier, monospace;
}


/* ==========================================================
   Tablet Responsive
   ========================================================== */

@media (min-width: 577px) and (max-width: 991px) {
    #ai-chat {
        right: 18px;
        bottom: 18px;
        width: 420px;
        max-width: calc(100vw - 36px);
        height: 650px;
        max-height: calc(100vh - 36px);
    }
}


/* ==========================================================
   Mobile Responsive
   ========================================================== */

@media (max-width: 576px) {
    #ai-assistant {
        right: 18px;
        bottom: 18px;
        z-index: 99998;
    }

    .ai-wrapper,
    .ai-floating-button {
        width: 62px;
        height: 62px;
    }

    #ai-chat {
        left: 10px;
        right: 10px;
        bottom: 10px;
        width: calc(100vw - 20px);
        height: calc(100dvh - 20px);
        max-height: calc(100dvh - 20px);
        border-radius: 16px;
    }

    #ai-chat.expanded {
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        height: 100dvh;
        max-height: 100dvh;
        border-radius: 0;
    }

    .card-header-custom {
        padding: 12px !important;
        font-size: 15px;
    }

    #ai-chat .card-body {
        padding: 10px;
    }

    #ai-chat-wrapper {
        padding: 8px;
        padding-right: 2px;
        font-size: 15px;
    }

    #ai-user-message {
        min-height: 46px;
        max-height: 100px;
        font-size: 15px;
    }

    #ai-send-button {
        width: 44px;
        min-width: 44px;
        height: 44px;
    }

    .avatar-img,
    .user-avatar-img,
    .b2b-avatar-icon {
        width: 36px;
        height: 36px;
        min-width: 36px;
    }

    .ai-message,
    .user-message {
        max-width: 78vw;
        font-size: 14px;
    }
}


/* ==========================================================
   Very Small Mobile
   ========================================================== */

@media (max-width: 380px) {
    #ai-chat {
        left: 6px;
        right: 6px;
        bottom: 6px;
        width: calc(100vw - 12px);
        height: calc(100dvh - 12px);
        max-height: calc(100dvh - 12px);
    }

    .ai-message,
    .user-message {
        max-width: 76vw;
    }

    #ai-user-message {
        font-size: 14px;
    }
}