/* ============================================================
   Chat IA - Widget flotante (burbuja + ventana no-modal)
   Colores de marca Kleon: #924AEF -> #E328AF
   z-index: 1085 (por encima de sidebar 1000 y header 1020/1030)
   ============================================================ */

.chat-ia-bubble {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 1085;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #924AEF 0%, #E328AF 100%);
    color: #fff;
    font-size: 24px;
    box-shadow: 0 6px 20px rgba(146, 74, 239, 0.45);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.chat-ia-bubble:hover {
    transform: scale(1.08);
    box-shadow: 0 8px 26px rgba(146, 74, 239, 0.6);
}

.chat-ia-bubble .chat-ia-bubble-close {
    display: none;
    font-size: 20px;
}

.chat-ia-bubble.abierto .chat-ia-bubble-icon {
    display: none;
}

.chat-ia-bubble.abierto .chat-ia-bubble-close {
    display: inline;
}

/* ---------- Ventana flotante ---------- */

.chat-ia-window {
    position: fixed;
    bottom: 94px;
    right: 24px;
    z-index: 1085;
    width: 380px;
    max-width: calc(100vw - 32px);
    height: 560px;
    max-height: calc(100vh - 130px);
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.22);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: chatIaSlideUp 0.25s ease;
}

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

/* ---------- Header ---------- */

.chat-ia-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    background: linear-gradient(135deg, #924AEF 0%, #E328AF 100%);
    color: #fff;
    flex-shrink: 0;
}

.chat-ia-header-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chat-ia-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 19px;
}

.chat-ia-header-text {
    display: flex;
    flex-direction: column;
    line-height: 1.2;
}

.chat-ia-header-text strong {
    font-size: 15px;
}

.chat-ia-header-text small {
    font-size: 11px;
    opacity: 0.85;
}

.chat-ia-header-actions {
    display: flex;
    gap: 6px;
}

.chat-ia-btn-icon {
    width: 30px;
    height: 30px;
    border: none;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease;
}

.chat-ia-btn-icon:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* ---------- Selector de conversaciones ---------- */

.chat-ia-conv-select {
    background: rgba(255, 255, 255, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    color: #fff;
    font-size: 11.5px;
    padding: 4px 25px 4px 8px;
    max-width: 160px;
    cursor: pointer;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='white'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
}

.chat-ia-conv-select option {
    background: #fff;
    color: #2b2f3a;
    font-size: 12px;
}

.chat-ia-conv-select:hover,
.chat-ia-conv-select:focus {
    background-color: rgba(255, 255, 255, 0.22);
}

/* ---------- Área de mensajes ---------- */

.chat-ia-messages {
    flex: 1;
    overflow-y: auto;
    padding: 14px;
    background: #f6f7fb;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-ia-messages::-webkit-scrollbar {
    width: 5px;
}

.chat-ia-messages::-webkit-scrollbar-thumb {
    background: #cfd4e0;
    border-radius: 4px;
}

.chat-ia-msg {
    max-width: 85%;
    padding: 9px 12px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.45;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.chat-ia-msg-time {
    display: block;
    font-size: 10px;
    opacity: 0.6;
    margin-top: 4px;
}

.chat-ia-msg-user {
    align-self: flex-end;
    background: linear-gradient(135deg, #924AEF 0%, #a55ef3 100%);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-ia-msg-bot {
    align-self: flex-start;
    background: #fff;
    color: #2b2f3a;
    border: 1px solid #e6e8f0;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
    white-space: normal; /* el contenido ya va estructurado en HTML */
}

.chat-ia-msg-bot strong {
    color: #924AEF;
}

/* ---------- Contenido markdown de las respuestas ---------- */

.chat-ia-p {
    margin: 3px 0;
}

.chat-ia-h2,
.chat-ia-h3 {
    font-weight: 600;
    color: #924AEF;
    margin: 8px 0 3px;
    line-height: 1.3;
}

.chat-ia-h2 {
    font-size: 14px;
}

.chat-ia-h3 {
    font-size: 13.5px;
}

.chat-ia-ul {
    margin: 4px 0 6px;
    padding-left: 18px;
}

.chat-ia-ul li {
    margin: 2px 0;
}

.chat-ia-ol {
    list-style-type: decimal;
}

.chat-ia-tabla {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    margin: 6px 0 8px;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 0 0 1px #e6e8f0;
}

.chat-ia-tabla th {
    background: linear-gradient(135deg, #924AEF 0%, #a55ef3 100%);
    color: #fff;
    font-weight: 600;
    padding: 6px 8px;
    text-align: left;
    font-size: 11.5px;
}

.chat-ia-tabla td {
    padding: 5px 8px;
    border-bottom: 1px solid #eef0f6;
    background: #fff;
}

.chat-ia-tabla tr:nth-child(even) td {
    background: #f8f5fe;
}

.chat-ia-tabla tr:last-child td {
    border-bottom: none;
}

/* ---------- Burbuja de descarga de Excel ---------- */

.chat-ia-msg-download {
    min-width: 260px;
}

.chat-ia-download-info {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
}

.chat-ia-download-icon {
    font-size: 30px;
    color: #1d6f42;
}

.chat-ia-download-texto {
    display: flex;
    flex-direction: column;
    line-height: 1.25;
    min-width: 0;
}

.chat-ia-download-texto strong {
    font-size: 13px;
    color: #1d6f42;
}

.chat-ia-download-texto small {
    font-size: 10.5px;
    color: #8a8fa3;
    word-break: break-all;
}

.chat-ia-download-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    width: 100%;
    padding: 8px 12px;
    background: #1d6f42;
    color: #fff !important;
    border-radius: 9px;
    font-size: 13px;
    font-weight: 600;
    text-decoration: none !important;
    transition: background 0.15s ease, transform 0.15s ease;
}

.chat-ia-download-btn:hover {
    background: #155231;
    transform: translateY(-1px);
}

/* Boton regenerar DENTRO de la burbuja de descarga (SIEMPRE visible) */
.chat-ia-download-regenerar {
    display: block;
    width: 100%;
    margin-top: 6px;
    padding: 6px 10px;
    border: 1px solid #924AEF;
    border-radius: 8px;
    background: #fff;
    color: #924AEF;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
}

.chat-ia-download-regenerar:hover {
    background: #924AEF;
    color: #fff;
}

/* ---------- Botón "Regenerar" independiente (fuera de burbujas) ---------- */

.chat-ia-btn-regenerar-wrapper {
    align-self: flex-end;
    margin-top: -6px;
    margin-bottom: 6px;
}

.chat-ia-btn-regenerar {
    padding: 7px 14px;
    border-radius: 8px;
    border: none;
    background: #924AEF;
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    letter-spacing: 0.3px;
    transition: transform 0.15s, box-shadow 0.15s;
}

.chat-ia-btn-regenerar:hover {
    transform: scale(1.04);
    background: #7c35d6;
    box-shadow: 0 3px 12px rgba(146, 74, 239, 0.4);
}

/* ---------- Cursor de streaming (texto generandose en vivo) ---------- */

.chat-ia-stream-cursor {
    display: inline-block;
    width: 12px;
    height: 2px;
    background: #924AEF;
    vertical-align: text-bottom;
    margin-left: 1px;
    border-radius: 1px;
    animation: chatIaCursorBlink 0.7s infinite;
}

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

.chat-ia-msg-error {
    align-self: flex-start;
    background: #fdecea;
    color: #b3261e;
    border: 1px solid #f5c6c2;
    border-bottom-left-radius: 4px;
    font-size: 13px;
}

/* Indicador "escribiendo..." */

.chat-ia-typing {
    align-self: flex-start;
    background: #fff;
    border: 1px solid #e6e8f0;
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    padding: 12px 16px;
    display: flex;
    gap: 5px;
}

.chat-ia-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #b9a3e8;
    animation: chatIaBlink 1.2s infinite;
}

.chat-ia-typing span:nth-child(2) {
    animation-delay: 0.2s;
}

.chat-ia-typing span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes chatIaBlink {
    0%, 60%, 100% {
        opacity: 0.3;
        transform: translateY(0);
    }
    30% {
        opacity: 1;
        transform: translateY(-3px);
    }
}

/* ---------- Chips de sugerencias ---------- */

.chat-ia-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 12px;
    background: #f6f7fb;
    border-top: 1px solid #e9ebf2;
    flex-shrink: 0;
}

.chat-ia-suggestions.oculto {
    display: none;
}

.chat-ia-chip {
    border: 1px solid #d9c9f8;
    background: #fff;
    color: #7a3fe0;
    font-size: 11.5px;
    padding: 5px 10px;
    border-radius: 20px;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
}

.chat-ia-chip:hover {
    background: #924AEF;
    color: #fff;
    border-color: #924AEF;
}

/* ---------- Input ---------- */

.chat-ia-input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
    background: #fff;
    border-top: 1px solid #e9ebf2;
    flex-shrink: 0;
}

.chat-ia-input {
    flex: 1;
    border: 1px solid #dfe3ee;
    border-radius: 12px;
    padding: 9px 12px;
    font-size: 13.5px;
    font-family: inherit;
    resize: none;
    outline: none;
    max-height: 100px;
    line-height: 1.4;
    transition: border-color 0.15s ease;
}

.chat-ia-input:focus {
    border-color: #924AEF;
    box-shadow: 0 0 0 3px rgba(146, 74, 239, 0.12);
}

.chat-ia-send {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 12px;
    background: linear-gradient(135deg, #924AEF 0%, #E328AF 100%);
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform 0.15s ease, opacity 0.15s ease;
}

.chat-ia-send:hover {
    transform: scale(1.06);
}

.chat-ia-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* ---------- Botón de micrófono (dictado por voz) ---------- */

.chat-ia-mic {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 12px;
    background: #eef0f6;
    color: #6b7286;
    font-size: 17px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s ease, color 0.15s ease;
}

.chat-ia-mic:hover {
    background: #e2e6f2;
    color: #924AEF;
}

.chat-ia-mic.escuchando {
    background: #e3282f;
    color: #fff;
    animation: chatIaMicPulse 1.2s infinite;
}

@keyframes chatIaMicPulse {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(227, 40, 47, 0.45);
    }
    50% {
        box-shadow: 0 0 0 9px rgba(227, 40, 47, 0);
    }
}

/* Textarea mientras se dicta (resultado parcial) */

.chat-ia-input.chat-ia-input-dictando {
    border-color: #e3282f;
    box-shadow: 0 0 0 3px rgba(227, 40, 47, 0.12);
    font-style: italic;
    color: #6b7286;
}

/* ---------- Responsive: móvil casi pantalla completa ---------- */

@media (max-width: 576px) {
    .chat-ia-bubble {
        bottom: 16px;
        right: 16px;
        width: 52px;
        height: 52px;
    }

    .chat-ia-window {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        max-width: 100%;
        height: 82vh;
        max-height: 82vh;
        border-radius: 16px 16px 0 0;
    }
}
