/**
 * Estilos para el chat en modo Modal - Braves Chat iA
 * Version: 1.0.0
 * @package BravesChat
 * CORREGIDO: Display del chat window
 */

/* ============================================
   ISOLATION LAYER - THEME PROTECTION
   ============================================ */
/* Comprehensive reset for BravesChat container to prevent theme CSS bleeding */
body #braveslab-chat-container,
body #braveslab-chat-container *,
body #braveslab-chat-container *::before,
body #braveslab-chat-container *::after {
    font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
    line-height: normal;
    letter-spacing: normal;
    text-transform: none;
    font-style: normal;
    font-variant: normal;
}

/* Reset all text elements */
body #braveslab-chat-container h1,
body #braveslab-chat-container h2,
body #braveslab-chat-container h3,
body #braveslab-chat-container h4,
body #braveslab-chat-container h5,
body #braveslab-chat-container h6,
body #braveslab-chat-container p,
body #braveslab-chat-container span,
body #braveslab-chat-container div {
    font-family: inherit;
    margin: 0;
    padding: 0;
    font-weight: normal;
    line-height: inherit;
}

/* Complete button reset to override ALL theme styles */
body #braveslab-chat-container button,
body #braveslab-chat-container input[type="button"],
body #braveslab-chat-container input[type="submit"] {
    all: unset;
    box-sizing: border-box;
    font-family: inherit;
    cursor: pointer;
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Complete input/textarea reset */
body #braveslab-chat-container input,
body #braveslab-chat-container textarea {
    all: unset;
    box-sizing: border-box;
    font-family: inherit;
    display: block;
    width: auto;
}

/* Reset container y contenedor principal */
.braveslab-chat-widget-container {
    position: relative;
    width: 100%;
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Posiciones del chat */
.braveslab-chat-widget-container.position-bottom-right #braveslab-chat-container {
    position: fixed;
    bottom: 30px;
    right: 30px;
}

.braveslab-chat-widget-container.position-bottom-left #braveslab-chat-container {
    position: fixed;
    bottom: 30px;
    left: 30px;
}

.braveslab-chat-widget-container.position-center #braveslab-chat-container {
    position: fixed;
    bottom: 30px;
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    margin: 0;
}

/* Fix para centrar la ventana del chat cuando la burbuja está en el centro */
.braveslab-chat-widget-container.position-center #braveslab-chat-container #chat-window {
    left: 50%;
    right: auto;
    margin-left: -190px;
    /* Mitad de 380px */
}

/* Fix para centrar la ventana expandida */
.braveslab-chat-widget-container.position-center #braveslab-chat-container.braves-expanded #chat-window {
    margin-left: -300px;
    /* Mitad de 600px */
}

/* Contenedor principal del chat */
body #braveslab-chat-container {
    z-index: 990; /* Woocommerce utiliza 999 */
    font-family: 'Montserrat', 'Segoe UI', Roboto, sans-serif;
}

/* ============================================
   BOTÓN DE TOGGLE (BURBUJA)
   ============================================ */
/* NOTA: Estos estilos NO se aplican al skin Braves, que tiene su propio diseño */
body #braveslab-chat-container:not(.braves-skin-braves) #chat-toggle {
    position: relative;
    width: 60px;
    height: 60px;
    min-width: 60px;
    min-height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #01B7AF 0%, #5DD5C7 100%);
    background-color: #01B7AF;
    border: none;
    border-width: 0;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(1, 183, 175, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    color: white;
    font-size: 24px;
    font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-weight: normal;
    outline: none;
    pointer-events: auto;
    z-index: 99999;
    padding: 0;
    margin: 0;
    overflow: hidden;
    line-height: 1;
    text-decoration: none;
    text-transform: none;
    letter-spacing: normal;
}

body #braveslab-chat-container:not(.braves-skin-braves) #chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 25px rgba(1, 183, 175, 0.6);
}

/* ============================================
   LOTTIE ANIMATION - ESTADO CERRADO
   ============================================ */
body #braveslab-chat-container.chat-closed:not(.braves-skin-braves) #chat-toggle #chat-lottie {
    display: block;
    width: 40px;
    height: 40px;
    pointer-events: none;
    transition: opacity 0.2s ease;
}

body #braveslab-chat-container.chat-closed:not(.braves-skin-braves) #chat-toggle #chat-lottie svg {
    width: 270%;
    height: 100%;
    transform: translate3d(-10px, 0px, 0px);
}

/* ============================================
   ICONO X - ESTADO ABIERTO
   ============================================ */
body #braveslab-chat-container.chat-closed:not(.braves-skin-braves) #chat-toggle #close-icon {
    display: none !important;
}

body #braveslab-chat-container.chat-open:not(.braves-skin-braves) #chat-toggle #chat-lottie {
    display: none !important;
}

body #braveslab-chat-container.chat-open:not(.braves-skin-braves) #chat-toggle #close-icon {
    display: block !important;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 28px;
    font-weight: 300;
    line-height: 1;
    color: white;
}

/* ============================================
   VENTANA DE CHAT - CORREGIDO ⭐
   ============================================ */
body #braveslab-chat-container #chat-window {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: 380px;
    height: 600px;
    background: white;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(10, 10, 10, 0.15);
    display: flex;
    /* CAMBIADO: Era display: none */
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    /* NUEVO: Usamos opacity en vez de display */
    visibility: hidden;
    /* NUEVO: Ocultamos con visibility */
    transform: translateY(20px);
    /* NUEVO: Animación inicial */
    transition: width 0.3s ease, height 0.3s ease, opacity 0.3s ease, visibility 0.3s ease, transform 0.3s ease;
    /* ACTUALIZADO: Añadida transición de width y height para contracción suave */
    pointer-events: none;
    /* NUEVO: Evita clicks cuando está oculto */
}

/* Fix posición bottom-left */
.braveslab-chat-widget-container.position-bottom-left #braveslab-chat-container #chat-window {
    right: auto;
    left: 0;
}

/* Estado ABIERTO - NUEVO ⭐ */
body #braveslab-chat-container.chat-open #chat-window {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
    pointer-events: auto;
}

/* Estado EXPANDIDO - NUEVO ⭐ */
body #braveslab-chat-container.braves-expanded #chat-window {
    width: 600px;
    height: 776px;
    transition: width 0.3s ease, height 0.3s ease, opacity 0.3s ease, visibility 0.3s ease, transform 0.3s ease;
}

/* Estado CERRADO - NUEVO ⭐ */
body #braveslab-chat-container.chat-closed #chat-window {
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px);
    pointer-events: none;
}

/* Animación de entrada (mantener por compatibilidad) */
@keyframes slideInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Header del chat */
body #braveslab-chat-container #chat-header {
    background: #141414;
    color: white;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

body #braveslab-chat-container #chat-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: white;
}

body #braveslab-chat-container #chat-header p {
    margin: 5px 0 0 0;
    font-size: 13px;
    opacity: 0.9;
    color: white;
}

body #braveslab-chat-container #close-chat {
    background: none;
    background-color: transparent;
    border: none;
    border-width: 0;
    color: white;
    font-size: 28px;
    cursor: pointer;
    width: 30px;
    height: 30px;
    min-width: 30px;
    min-height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s ease;
    line-height: 1;
    padding: 0;
    margin: 0;
    font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-weight: 300;
    text-decoration: none;
    text-transform: none;
}

body #braveslab-chat-container #close-chat:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Área de mensajes */
body #braveslab-chat-container #chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #F8FCFC;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

body #braveslab-chat-container #chat-messages::-webkit-scrollbar {
    width: 6px;
}

body #braveslab-chat-container #chat-messages::-webkit-scrollbar-thumb {
    background: #CEF2EF;
    border-radius: 3px;
}

body #braveslab-chat-container #chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

/* Mensajes */
body #braveslab-chat-container .message {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    animation: messageIn 0.3s ease;
}

@keyframes messageIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

body #braveslab-chat-container .message.user {
    align-self: flex-end;
    align-items: flex-end;
}

body #braveslab-chat-container .message.bot {
    align-self: flex-start;
    align-items: flex-start;
}

body #braveslab-chat-container .message-bubble {
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.4;
    word-wrap: break-word;
}

body #braveslab-chat-container .message.user .message-bubble {
    background: linear-gradient(135deg, #01B7AF 0%, #5DD5C7 100%);
    color: white;
}

body #braveslab-chat-container .message.bot .message-bubble {
    background: white;
    color: #242424;
    border: 1px solid rgba(1, 183, 175, 0.1);
    box-shadow: 0 2px 8px rgba(1, 183, 175, 0.05);
}

body #braveslab-chat-container .message-time {
    font-size: 11px;
    color: #242424;
    opacity: 0.7;
    margin-top: 5px;
}

/* ============================================
   INPUT CONTAINER
   ============================================ */
body #braveslab-chat-container #chat-input-container {
    padding: 15px;
    background: white;
    border-top: 1px solid #CEF2EF;
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

body #braveslab-chat-container #chat-input {
    flex: 1;
    border: 1px solid #CEF2EF;
    border-radius: 25px;
    padding: 12px 16px;
    font-size: 14px;
    outline: none;
    transition: height 0.15s ease-out;
    color: #242424;
    background: white;
    box-shadow: none;
    font-family: 'Montserrat', 'Segoe UI', Roboto, sans-serif;
    line-height: 1.5;
    resize: none;
    overflow-y: auto;
    min-height: 44px;
    max-height: 150px;
    vertical-align: middle;
    box-sizing: border-box;
}

body #braveslab-chat-container #chat-input::placeholder {
    color: #9ca3af;
    opacity: 1;
}

body #braveslab-chat-container #chat-input:focus {
    border-color: #01B7AF;
    box-shadow: 0 0 0 3px rgba(1, 183, 175, 0.1);
    outline: none;
}

/* ============================================
   BOTÓN DE ENVIAR
   ============================================ */
body #braveslab-chat-container #send-button {
    width: 45px;
    height: 45px;
    min-width: 45px;
    min-height: 45px;
    max-width: 45px;
    max-height: 45px;
    flex-shrink: 0;
    border: none;
    border-width: 0;
    background: linear-gradient(135deg, #01B7AF 0%, #5DD5C7 100%);
    background-color: #01B7AF;
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    outline: none;
    padding: 0;
    margin: 0;
    font-size: 18px;
    font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-weight: normal;
    line-height: 1;
    text-decoration: none;
    text-transform: none;
    letter-spacing: normal;
}

body #braveslab-chat-container #send-button span {
    display: flex;
    align-items: center;
    justify-content: center;
    transform: translateX(1px);
    line-height: 1;
}

body #braveslab-chat-container #send-button:hover:not(:disabled) {
    transform: scale(1.05);
    box-shadow: 0 4px 15px rgba(1, 183, 175, 0.3);
}

body #braveslab-chat-container #send-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

body #braveslab-chat-container #send-button:active:not(:disabled) {
    transform: scale(0.95);
}

/* ============================================
   INDICADOR DE ESCRITURA
   ============================================ */
body #braveslab-chat-container .typing-indicator {
    display: none;
    align-items: center;
    gap: 5px;
    color: #242424;
    opacity: 0.8;
    font-style: italic;
    font-size: 13px;
    padding: 0 20px;
}

/* Ocultar indicador en skin Braves (ya tiene cursor de latido) */
body #braveslab-chat-container.braves-skin-braves .typing-indicator {
    display: none !important;
}

body #braveslab-chat-container .typing-dots {
    display: flex;
    gap: 3px;
}

body #braveslab-chat-container .typing-dot {
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: #01B7AF;
    animation: typing 1.4s infinite ease-in-out;
}

body #braveslab-chat-container .typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

body #braveslab-chat-container .typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {

    0%,
    60%,
    100% {
        transform: translateY(0);
    }

    30% {
        transform: translateY(-10px);
    }
}

/* ============================================
   RESPONSIVE
   ============================================ */
@media (max-width: 480px) {

    body #braveslab-chat-container #chat-window,
    body #braveslab-chat-container.braves-expanded #chat-window {
        width: calc(100vw - 40px);
        right: -10px;
        height: calc(100vh - 140px);
    }

    .braveslab-chat-widget-container.position-bottom-right #braveslab-chat-container,
    .braveslab-chat-widget-container.position-bottom-left #braveslab-chat-container {
        bottom: 20px;
        right: 20px;
        left: auto;
    }

    /* Burbuja más pequeña en móvil (skin default) */
    body #braveslab-chat-container:not(.braves-skin-braves) #chat-toggle {
        width: 48px;
        height: 48px;
        min-width: 48px;
        min-height: 48px;
    }

    body #braveslab-chat-container.chat-closed:not(.braves-skin-braves) #chat-toggle #chat-lottie {
        width: 32px;
        height: 32px;
    }
}

/* ============================================
   RESET DE ESTILOS
   ============================================ */

/* Previene ajustes automáticos de color en iOS Safari dark mode */
body #braveslab-chat-container {
    color-scheme: light;
}

body #braveslab-chat-container * {
    box-sizing: border-box;
}

body #braveslab-chat-container button {
    appearance: none;
    -webkit-appearance: none;
}

body #braveslab-chat-container input {
    appearance: none;
    -webkit-appearance: none;
}

/* Ocultar elementos de burbuja Braves cuando el chat está abierto */
body #braveslab-chat-container.chat-open #chat-toggle .braves-bubble-wrapper {
    display: none !important;
}

/* =============================================
   MOBILE FULLSCREEN MODE
   ============================================= */

/* Oculto por defecto en todos los breakpoints */
#braves-mobile-header {
    display: none;
}

@media (max-width: 480px) {
    /* Bloquear scroll del body cuando el modo móvil está activo */
    body.braves-mobile-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
    }

    /* Chat window: flex column a pantalla completa */
    body.braves-mobile-open #braveslab-chat-container.chat-open #chat-window {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        height: 100vh !important;   /* fallback */
        height: 100dvh !important;  /* ajusta con el teclado virtual */
        border-radius: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        max-height: none !important;
        z-index: 999999 !important;
    }

    /* Header móvil visible en modo fullscreen */
    body.braves-mobile-open #braves-mobile-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 14px 16px;
        padding-top: max(14px, env(safe-area-inset-top));
        background: var(--braves-primary-color, #000000);
        color: #ffffff;
        flex-shrink: 0;
        min-height: 56px;
    }

    body.braves-mobile-open #braves-mobile-header button {
        all: unset;
        color: #ffffff;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        flex-shrink: 0;
    }

    body.braves-mobile-open #braves-mobile-title {
        font-size: 16px;
        font-weight: 600;
        color: #ffffff;
        flex: 1;
        text-align: center;
    }

    /* Ocultar el header original (reemplazado por el header móvil) */
    body.braves-mobile-open #braveslab-chat-container #chat-header {
        display: none !important;
    }

    /* Mensajes: ocupa todo el espacio restante, anclados al fondo */
    body.braves-mobile-open #braveslab-chat-container #chat-messages {
        flex: 1 !important;
        height: auto !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        justify-content: flex-end;
    }

    /* Typing indicator: flex item normal */
    body.braves-mobile-open #braveslab-chat-container .typing-indicator {
        flex-shrink: 0;
    }

    /* Input wrapper: en el flujo normal (no absolutamente posicionado) */
    body.braves-mobile-open #braveslab-chat-container #chat-input-wrapper {
        position: static !important;
        flex-shrink: 0 !important;
        padding-bottom: max(12px, env(safe-area-inset-bottom)) !important;
    }

    /* font-size 16px en el textarea para prevenir zoom automático en iOS */
    body.braves-mobile-open #braveslab-chat-container #chat-input {
        font-size: 16px !important;
    }

    /* Ocultar botón minimize en mobile fullscreen (ocupa espacio como flex item) */
    body.braves-mobile-open #braveslab-chat-container #minimize-chat {
        display: none !important;
    }

    /* Textarea: una sola línea en mobile */
    body.braves-mobile-open #braveslab-chat-container #chat-input {
        min-height: 30px !important;
        max-height: 30px !important;
    }

    /* Botón enviar: sin círculo, icono en color primary */
    body.braves-mobile-open #braveslab-chat-container #send-button,
    body.braves-mobile-open #braveslab-chat-container #send-button:not(:disabled) {
        background: transparent !important;
        background-color: transparent !important;
        box-shadow: none !important;
        border-radius: 0 !important;
        width: auto !important;
        height: auto !important;
        min-width: unset !important;
        min-height: unset !important;
        max-width: unset !important;
        max-height: unset !important;
        padding: 4px !important;
    }

    body.braves-mobile-open #braveslab-chat-container #send-button svg path {
        fill: var(--braves-primary, #01B7AF) !important;
        fill-opacity: 1 !important;
    }

    body.braves-mobile-open #braveslab-chat-container #send-button:disabled svg path {
        fill: #cccccc !important;
    }
}

body #braveslab-chat-container.chat-open #chat-toggle #chat-bubble-image {
    display: none !important;
}

/* ============================================
   HEADER STATUS TEXT & ANIMATION
   ============================================ */
body #braveslab-chat-container .chat-header-badge {
    display: flex;
    align-items: center;
    gap: 12px;
}

body #braveslab-chat-container .chat-header-info {
    display: flex;
    flex-direction: column;
    justify-content: center;
    overflow: hidden;
}

body #braveslab-chat-container .chat-title {
    font-size: 16px;
    font-weight: 600;
    line-height: 1.2;
    color: white;
    margin: 0;
}

body #braveslab-chat-container .chat-status-text {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.9);
    font-weight: 400;
    white-space: nowrap;
    overflow: hidden;
    margin-top: 2px;
    animation: slideOutText 0.8s ease-out forwards;
    transform-origin: left;
    max-width: 0;
    opacity: 0;
}

@keyframes slideOutText {
    0% {
        max-width: 0;
        opacity: 0;
        transform: translateX(-10px);
    }

    100% {
        max-width: 250px;
        opacity: 1;
        transform: translateX(0);
    }
}

/* ============================================
   STREAMING VISUAL - CURSOR PARPADEANTE
   ============================================ */
body #braveslab-chat-container .typing-cursor {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #01B7AF;
    animation: heartbeat 2s ease-in-out infinite;
    /* Efecto latido de corazón */
    margin-left: 4px;
    margin-bottom: 2px;
    vertical-align: middle;
}

/* Animación tipo latido de corazón: "tun-tun" (pausa) "tun-tun" */
@keyframes heartbeat {
    0% {
        opacity: 0.4;
        transform: scale(1);
    }

    /* Primer latido */
    10% {
        opacity: 1;
        transform: scale(1.1);
    }

    20% {
        opacity: 0.4;
        transform: scale(1);
    }

    /* Segundo latido */
    30% {
        opacity: 1;
        transform: scale(1.1);
    }

    40% {
        opacity: 0.4;
        transform: scale(1);
    }

    /* Pausa larga */
    100% {
        opacity: 0.4;
        transform: scale(1);
    }
}


/* Smooth scrolling durante streaming */
body #braveslab-chat-container #chat-messages {
    scroll-behavior: smooth;
}

/* ============================================
   WOOCOMMERCE - CHECKOUT / CART COMPATIBILITY
   El widget queda detrás del sidebar de compra
   ============================================ */
body.woocommerce-checkout #braveslab-chat-container,
body.woocommerce-cart #braveslab-chat-container,
body.woocommerce-checkout #braveslab-chat-container #chat-toggle,
body.woocommerce-cart #braveslab-chat-container #chat-toggle {
    z-index: 800 !important;
}

/* Input deshabilitado durante streaming */
body #braveslab-chat-container #chat-input:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    background-color: #f5f5f5;
}