/* === Chat.css (v3 - compatible con widget en Ionic) === */

/* Solo cuando Chat.html se usa como página completa */
html.giachat-page,
body.giachat-page {
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    /* background: #f8f9fa; */
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

/* Raíz del componente (widget o página) */
.giachat {
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    --giachat-bubble-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
    /* Variables de tema (defaults) */
    --giachat-primary: #FFFFFF;
    --giachat-secondary: #007bff;
    --giachat-on-primary: #FFFFFF;
    --giachat-font: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    --giachat-panel-radius: 16px;
    --giachat-bg-image: none;
    /* Burbujas */
    --giachat-chatBackgroundColor: #D9FBD2;
    --giachat-userBubbleBgColor: #007BFFE6;
    --giachat-userBubbleTextColor: #FFF;
    --giachat-assistantBubbleBgColor: #E9ECEFE6; /*IA fondo por defecto*/
    --giachat-assistantBubbleTextColor: #000; /*IA texto por defecto*/
}

    .giachat,
    .giachat * {
        color-scheme: light;
    }

/* En modo página completa, usamos viewport */
body.giachat-page .giachat {
    height: 100vh;
    /* fallback */
    height: 100dvh;
    /* móviles modernos */
}

/* === Chat.css === */

.d-none {
    display: none !important;
}

/* Contenedor principal del chat */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    background-size: cover;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center center;
    background-image: var(--giachat-bg-image);

}

/* Título */
.chat-title {
    margin: 0;
    padding: 0.5rem;
    background: var(--giachat-primary);
    border-bottom: 1px solid #ddd;
    font-size: 1rem;
    font-weight: bold;
    color: var(--giachat-on-primary);
    align-items: center;
    display: flex; 
    gap: 10px;
}

    .chat-title .btn-back {
        position: static; /* o relative */
        left: auto;
        right: auto;
        top: auto;
        bottom: auto;
    }

/* Grupo izquierdo: back + avatar + título */
.chat-title-left {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1 1 auto;
    min-width: 0; /* permite ellipsis */
}

#chatTitleText {
    /*text-align: center;*/
    flex: 1 1 auto; /* ocupa el espacio disponible */
    text-align: left;
    min-width: 0; /* permite ellipsis en flex */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    vertical-align: middle;
}

/* Avatar barra de titulo*/
.chat-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    flex: 0 0 auto;
}

#chatWidgetClose {
    margin-left: auto;
}

#chatWidgetClose.btn-back {
    left: auto !important;
    right: 8px; /* o el margen que uses */
    position: absolute; /* si tu header está en relative */
}

/* Historial */
.chat-history {
    flex-grow: 1;
    min-height: 0;
    /* CRUCIAL para scroll en flex items anidados */
    overflow-y: auto;
    padding: 0.75rem;
    display: flex;
    flex-direction: column;
    /* justify-content: flex-end; <-- Eliminado para evitar scroll clipping arriba */
    gap: 0.5rem;
    /*background: #f8f9fa;*/
}

/* Fix para alinear mensajes abajo pero permitiendo scroll arriba */
.chat-history> :first-child {
    margin-top: auto;
}

/* Burbuja del usuario */
.user-message {
    align-self: flex-end;
    max-width: 75%;
    background: var(--giachat-userBubbleBgColor);
    color: var(--giachat-userBubbleTextColor);
    padding: 0.5rem 0.75rem;
    border-radius: 15px;
    border-bottom-right-radius: 0;
    box-shadow: var(--giachat-bubble-shadow);
    word-wrap: break-word;
    white-space: pre-wrap;
    font-size: 0.95rem;
}

/* Burbuja de la IA */
.ai-message {
    align-self: flex-start;
    max-width: 75%;
    /*background: #e9ecef;*/
    background: var(--giachat-assistantBubbleBgColor);
    color: var(--giachat-assistantBubbleTextColor);
    padding: 0.5rem 0.75rem;
    border-radius: 15px;
    border-bottom-left-radius: 0;
    box-shadow: var(--giachat-bubble-shadow);
    word-wrap: break-word;
    /*white-space:  pre-wrap;*/
    font-size: 0.95rem;
    position: relative;
    padding-right: 2rem;
}

.ai-tts-btn {
    position: absolute;
    right: 0.35rem;
    bottom: 0.35rem;
    width: 1.4rem;
    height: 1.4rem;
    border: none;
    border-radius: 999px;
    cursor: pointer;
    font-size: 0.85rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 14%, #ffffff);
    color: var(--giachat-assistantBubbleTextColor);
}

.ai-tts-btn.is-speaking {
    background: var(--giachat-secondary);
    color: var(--giachat-on-primary);
}

.ai-tts-btn.is-disabled {
    opacity: 0.45;
}

.incident-detection-box {
    margin-top: 0.75rem;
    padding: 0.75rem;
    border: 1px solid color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 20%, transparent);
    border-radius: 10px;
    background: color-mix(in srgb, var(--giachat-assistantBubbleBgColor) 70%, #ffffff);
}

.incident-detection-title {
    margin: 0 0 0.5rem 0;
    color: var(--giachat-assistantBubbleTextColor);
}

.incident-detection-subtitle {
    margin: 0 0 0.625rem 0;
    color: color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 72%, #000000);
}

.incident-detection-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.incident-action-btn {
    border: none;
    border-radius: 8px;
    padding: 0.55rem 0.95rem;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.98rem;
    line-height: 1.2;
}

.incident-action-btn--confirm,
.incident-action-btn--manual {
    background: var(--giachat-secondary);
    color: var(--giachat-on-primary);
}

.incident-action-btn--cancel {
    background: color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 35%, #9ca3af);
    color: #ffffff;
}

.incident-action-btn--block {
    width: 100%;
}

.incident-action-btn--spaced {
    margin-top: 0.625rem;
}

.incident-select-control {
    width: 100%;
    padding: 0.5rem;
    margin-bottom: 0.625rem;
    border-radius: 6px;
    border: 1px solid color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 28%, transparent);
    background: #ffffff;
    color: #111827;
}

.incident-select-container {
    display: none;
}

.incident-detection-title--spaced {
    margin-top: 0.625rem;
}

.incident-summary-card {
    border: 1px solid color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 20%, transparent);
    border-radius: 10px;
    padding: 0.75rem;
    background: color-mix(in srgb, var(--giachat-assistantBubbleBgColor) 55%, #ffffff);
    max-width: 320px;
    margin-top: 0.625rem;
}

.incident-summary-title {
    margin: 0 0 0.625rem 0;
    color: var(--giachat-assistantBubbleTextColor);
    font-size: 1rem;
}

.incident-summary-image-wrap {
    text-align: center;
    margin-bottom: 0.625rem;
}

.incident-summary-image {
    max-width: 100%;
    max-height: 150px;
    border-radius: 6px;
    border: 1px solid color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 16%, transparent);
}

.incident-summary-meta {
    font-size: 0.82rem;
    color: color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 80%, #000000);
}

.incident-summary-row {
    margin: 0.25rem 0;
}

.incident-summary-state {
    background: color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 15%, #ffffff);
    padding: 0.12rem 0.4rem;
    border-radius: 999px;
}

.incident-summary-actions {
    margin-top: 0.75rem;
    text-align: center;
    border-top: 1px solid color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 14%, transparent);
    padding-top: 0.625rem;
}

.incident-summary-link {
    display: inline-block;
    background: var(--giachat-secondary);
    color: var(--giachat-on-primary);
    padding: 0.55rem 0.95rem;
    border-radius: 999px;
    text-decoration: none;
    font-size: 0.98rem;
    font-weight: 600;
    border: none;
    cursor: pointer;
    margin: 0.18rem;
}

/* Entrada */
.chat-input {
    display: flex;
    /*align-items: flex-end;*/
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    background: #fff;
    border-top: 1px solid #ddd;
}

.chat-input textarea {
    flex-grow: 1;
    resize: none;
    min-height: 28px;
    max-height: 120px;
    border: 2px solid #ccc;
    border-radius: 10px;
    padding: 0.5rem;
    /*font-size: 0.95rem;*/
    font-size: 16px;
    border-color: var(--giachat-secondary);
    transition: border-color 0.2s, box-shadow 0.2s;
        /* Pera que ionic no lo cambie*/
        background-color: #ffffff !important;
        color: #111827 !important;
        -webkit-text-fill-color: #111827; /* iOS Safari/WKWebView */
        caret-color: #111827;
        -webkit-appearance: none;
        appearance: none;
}

/* Cuando el textarea está enfocado */
.chat-input textarea:focus {
    outline: none;
    border-color: var(--giachat-secondary);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--giachat-secondary) 25%, transparent);
    /* efecto glow */
    /*box-shadow: 0 0 0 4px rgba(0, 123, 255, 0.25); */
}

/* Imágenes dentro de los mensajes del chat */
.chat-img {
    max-width: 100%;
    /* nunca desbordar la burbuja */
    height: auto;
    /* mantiene proporción */
    border-radius: 8px;
    /* esquinas redondeadas */
    margin-top: 4px;
    /* pequeño espacio respecto al texto */
    display: block;
    /* asegura que ocupe su propia línea */
}

/* Botones */
.btn-icon {
    background: #e9ecef;
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1rem;
}

.btn-icon.primary {
    background: var(--giachat-secondary);
    color: #fff;
    /*  background: #fff; 
        border: 2px solid #007bff;  
        color: #007bff; */
}

.btn-icon:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-icon.is-listening {
    animation: giachat-mic-pulse 1.1s infinite;
}

@keyframes giachat-mic-pulse {
    0% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.45);
    }

    70% {
        transform: scale(1.04);
        box-shadow: 0 0 0 10px rgba(220, 53, 69, 0);
    }

    100% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(220, 53, 69, 0);
    }
}

/* Tokens */
.chat-tokens {
    font-size: 0.8rem;
    padding: 0.5rem;
    background: #fff;
    border-top: 1px solid #ddd;
    display: flex;
    justify-content: space-between;
}


/* Burbuja especial para animación escribiendo */
.ai-message.typing {
    display: inline-flex;
    /* burbuja como flex */
    align-items: center;
    /* centra verticalmente */
    justify-content: center;
    background: #e9ecef;
    color: #000;
    border-radius: 15px;
    border-bottom-left-radius: 0;
    max-width: fit-content;
    padding: 6px 10px;
    /* algo de aire */
    min-height: 32px;
    /* evita recortes */
    line-height: 0;
}

/* Wrapper para el lottie; así controlamos la altura real visible */
.ai-message.typing .typing-dots {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 24px;
    /* altura “visible” del loader */
}


/* El propio lottie: tamaño consistente y sin márgenes raros */
.ai-message.typing lottie-player {
    width: 44px;
    height: 24px;
    display: block;
    margin: 0;
}

.typing-row {
    display: flex;
    align-items: center;
}

.typing-custom-text {
    margin-left: 0.625rem;
    font-size: 0.9em;
    color: color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 70%, #000000);
}

.quick-actions {
    display: flex;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
}

.quick-actions-nav {
    border-bottom: 1px solid color-mix(in srgb, var(--giachat-assistantBubbleTextColor) 14%, transparent);
    background: color-mix(in srgb, var(--giachat-assistantBubbleBgColor) 55%, #ffffff);
}

.quick-actions-chip {
    background: var(--giachat-secondary);
    color: var(--giachat-on-primary);
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 999px;
    font-weight: 600;
    font-size: 0.875rem;
    cursor: pointer;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.18);
    transition: transform 0.2s, box-shadow 0.2s, filter 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    white-space: nowrap;
    flex: 0 0 auto;
}

.quick-actions-chip:hover {
    transform: translateY(-1px);
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.22);
    filter: brightness(1.04);
}

#imageButton {
    display: none;
}

#imageButton:not(.d-none) {
    display: flex;
    /* o inline-flex */
}


.chat-title {
    display: flex;
    align-items: center;
    justify-content: center;
    /* 👈 centra el contenido */
    gap: 0.5rem;
    position: relative;
    /* necesario para el botón back */
}

.btn-back {
    position: absolute;
    left: 0.5rem;
    background: none;
    border: none;
    color: var(--giachat-on-primary);    
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0;
}

.btn-back:focus {
    outline: none;
}

/* ===============================
   Widget shell (FAB + overlay)
   =============================== */

.giachat-widget {
    --giachat-fab-offset: 20px;
    --giachat-panel-offset: 20px;
}


.giachat-widget .chat-fab {
    position: fixed;
    bottom: var(--giachat-fab-offset, 20px);
    right: var(--giachat-fab-offset, 20px);
    left: auto;
    top: auto;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    /*box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);*/
    z-index: 999999 !important;
    transition: transform 0.2s ease-in-out;
    border: none;
    touch-action: none;
    user-select: none;
    cursor: grab;
    background: transparent;
    padding: 0;
}

.giachat-widget .chat-fab:active {
    cursor: grabbing;
    transform: scale(1.05);
}

.giachat-widget .chat-fab.dragging {
    opacity: 0.8;
    transition: none;
}

.giachat-widget .chat-fab:hover {
    transform: scale(1.1);
}

.giachat-widget .chat-fab-icon {
    max-width: 110px;
    border-radius: 50px;
    z-index: 1000000 !important;
    position: relative !important;
}

.giachat-widget .chat-widget-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: none;
    z-index: 999998;
}

.giachat-widget .chat-widget-overlay.open {
    display: block;
}

.giachat-widget .chat-widget-container {
    position: fixed;
    bottom: calc(var(--giachat-panel-offset, 20px) + env(safe-area-inset-bottom, 0px));
    right: calc(var(--giachat-panel-offset, 20px) + env(safe-area-inset-right, 0px));
    width: 400px;
    height: 600px;
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
    display: none;
    flex-direction: column;
    overflow: hidden;
    z-index: 999999;
    transition: all 0.3s ease;
    transform: translateY(20px);
    opacity: 0;
}

.giachat-widget .chat-widget-container.open {
    display: flex;
    transform: translateY(0);
    opacity: 1;
}

.giachat-widget .chat-widget-container.fullscreen {
    width: 100% !important;
    height: 100% !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    border-radius: 0 !important;
}

/* El chat debe ocupar el panel completo */
.giachat-widget .giachat-slot {
    height: 100%;
    flex: 1 1 auto;
    min-height: 0;
}

/* Responsive */
@media (max-width: 768px) {
    .giachat-widget .chat-widget-container:not(.fullscreen) {
        width: calc(100% - 40px);
        height: 70vh;
        bottom: calc(20px + env(safe-area-inset-bottom, 0px));
        right: calc(20px + env(safe-area-inset-right, 0px));
        left: 20px;
    }

    .giachat-widget .chat-fab {
        bottom: 15px;
        right: 15px;
    }
}

@media (max-width: 480px) {
    .giachat-widget .chat-widget-container:not(.fullscreen) {
        width: calc(100% - 20px);
        right: 10px;
        left: 10px;
    }
}

/* Estado base del widget */
.giachat-widget #chatWidget {
    display: none;
    /* Empezamos oculto */
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    z-index: 1000001;
}

/* Estado abierto */
.giachat-widget #chatWidget.open {
    display: flex;
    /* O block, según tu diseño */
    flex-direction: column;
    opacity: 1;
    transform: translateY(0);
}

/* ===============================
   Botón cerrar integrado en el header del chat
   =============================== */

/* Asegura alineación vertical (page y widget) */
.giachat #strTituloChat {
    display: flex;
    align-items: center;
}

/* En widget, queremos que el cierre quede a la derecha del todo */
.giachat #chatTitleText {
    flex: 1 1 auto;
}

/* El botón cerrar va dentro del header (no absolute) */
.giachat #strTituloChat .giachat #strTituloChat .chat-widget-close:hover {
    background-color: rgba(0, 0, 0, 0.1);
}

/* ===============================
   Intro modal (primera vez)
   =============================== */

.giachat-intro-overlay {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.45);
    z-index: 1000001;
    padding: 16px;
}

.giachat-intro-overlay.open {
    display: flex;
}

.giachat-intro-modal {
    width: min(560px, calc(100vw - 32px));
    background: #fff;
    border-radius: 14px;
    box-shadow: 0 14px 40px rgba(0, 0, 0, 0.25);
    padding: 18px 18px 16px 18px;
    position: relative;
    transform: translateY(10px);
    opacity: 0;
    transition: transform 220ms ease, opacity 220ms ease;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

.giachat-intro-overlay.open .giachat-intro-modal {
    transform: translateY(0);
    opacity: 1;
}

.giachat-intro-close {
    position: absolute;
    right: 10px;
    top: 10px;
    width: 34px;
    height: 34px;
    border-radius: 999px;
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 22px;
    line-height: 1;
    color: #667085;
    display: flex;
    align-items: center;
    justify-content: center;
}

.giachat-intro-close:hover {
    background: rgba(0, 0, 0, 0.06);
}

.giachat-intro-icon-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 6px;
    margin-bottom: 10px;
}

.giachat-intro-icon {
    width: 110px;
    height: 110px;
    border-radius: 999px;
    object-fit: cover;
    /*box-shadow: 0 10px 22px rgba(0, 0, 0, 0.18);*/
}

.giachat-intro-text {
    color: #101828;
    font-size: 15px;
    line-height: 1.45;
    margin: 0 0 14px 0;
    text-align: center;
    white-space: pre-wrap;
}

.giachat-intro-actions {
    display: flex;
    justify-content: center;
}

.giachat-intro-btn {
    border: 0;
    background: var(--ion-color-primary,#007bff);
    color: var(--ion-color-primary-contrast,#ffffff);
    border-radius: 10px;
    cursor: pointer;
    font-size: 14px;
    padding: 10px 16px; /* da “cuerpo” al botón */
    line-height: 1.2; /* evita que el texto lo aplaste */
    min-height: 44px; /* recomendado para touch (iOS/Android) */
}

.giachat-intro-btn:hover {
    opacity: 0.92;
}

/* Evita flashes de tema (icono/colores) mientras carga */
.giachat.is-loading {
    opacity: 0;
    pointer-events: none;
}

.giachat.is-ready {
    opacity: 1;
    pointer-events: auto;
    transition: opacity 120ms ease;
}
