/* WIDGET FLOTANTE MINIMALISTA DE ASISTENTE IA */
.ai-fab-btn {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 999;
    width: 48px;
    height: 48px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
    color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 8px 24px -4px rgba(59, 130, 246, 0.5), 0 0 16px rgba(139, 92, 246, 0.35);
    transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.ai-fab-btn:hover {
    transform: translateY(-3px) scale(1.08);
    box-shadow: 0 12px 30px -4px rgba(59, 130, 246, 0.65), 0 0 24px rgba(139, 92, 246, 0.55);
    border-color: rgba(255, 255, 255, 0.5);
}

.ai-fab-btn:active {
    transform: scale(0.95);
}

.ai-fab-btn svg {
    filter: drop-shadow(0 0 5px rgba(255, 255, 255, 0.5));
    transition: transform 0.25s ease;
}

.ai-fab-btn:hover svg {
    transform: scale(1.1);
}

.ai-chat-window {
    position: fixed;
    bottom: 82px;
    right: 24px;
    width: 390px;
    max-width: calc(100vw - 32px);
    height: 540px;
    max-height: calc(100vh - 110px);
    background: rgba(15, 23, 42, 0.94);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 18px;
    box-shadow: 0 25px 60px -10px rgba(0, 0, 0, 0.75), 0 0 30px rgba(59, 130, 246, 0.15);
    display: none;
    flex-direction: column;
    overflow: hidden;
    z-index: 1000;
    contain: layout;
    transform: translateZ(0);
    animation: modalFadeUp 0.25s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.ai-chat-window.open {
    display: flex;
}

@keyframes modalFadeUp {
    from { opacity: 0; transform: translateY(14px) scale(0.97); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

.ai-chat-header {
    position: relative;
    z-index: 1;
    padding: 14px 16px;
    background: linear-gradient(135deg, rgba(30, 41, 69, 0.85) 0%, rgba(15, 23, 42, 0.95) 100%);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

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

.ai-avatar {
    width: 32px;
    height: 32px;
    border-radius: 10px;
    background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    box-shadow: 0 0 12px rgba(139, 92, 246, 0.4);
    flex-shrink: 0;
}

.ai-title-text h4 {
    font-size: 13.5px;
    font-weight: 700;
    color: #f8fafc;
    margin: 0;
    line-height: 1.2;
}

.ai-title-text p {
    font-size: 11px;
    color: #94a3b8;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 5px;
}

.ai-header-actions {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 4px;
}

.ai-header-btn {
    background: transparent;
    border: none;
    color: #94a3b8;
    font-size: 14px;
    cursor: pointer;
    padding: 5px 7px;
    border-radius: 6px;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ai-header-btn:hover {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.08);
}

.ai-close-btn {
    background: transparent;
    border: none;
    color: #94a3b8;
    font-size: 18px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    transition: all 0.15s ease;
}

.ai-close-btn:hover {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.08);
}

.ai-header-btn,
.ai-close-btn { position: relative; z-index: 2; touch-action: manipulation; pointer-events: auto; }

.ai-messages-area {
    flex: 1 1 0;
    min-height: 0;
    padding: 14px 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scrollbar-width: thin;
    contain: content;
}

.ai-msg {
    max-width: 90%;
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 13px;
    line-height: 1.55;
    word-break: break-word;
    letter-spacing: 0.01em;
}

.ai-msg.user {
    align-self: flex-end;
    background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
    color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 16px;
    box-shadow: 0 4px 14px rgba(37, 99, 235, 0.35);
}

.ai-msg.bot {
    align-self: flex-start;
    background: linear-gradient(160deg, rgba(30, 41, 69, 0.82) 0%, rgba(15, 23, 42, 0.92) 100%);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #e2e8f0;
    border-radius: 16px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

.ai-msg.bot strong {
    color: #ffffff;
    font-weight: 700;
}

/* CHIPS / PILLS DE SALAS MODERNAS Y CÓDIGO EN EL CHAT */
.ai-msg code, .ai-room-pill {
    display: inline-flex;
    align-items: center;
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.18) 0%, rgba(139, 92, 246, 0.15) 100%);
    border: 1px solid rgba(147, 197, 253, 0.38);
    color: #93c5fd;
    font-family: inherit;
    font-weight: 700;
    font-size: 12px;
    letter-spacing: 0.02em;
    padding: 2px 7px;
    border-radius: 6px;
    margin: 0 2px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25), inset 0 1px 0 rgba(255, 255, 255, 0.15);
    transition: all 0.18s ease;
    vertical-align: baseline;
}

.ai-msg code:hover, .ai-room-pill:hover {
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.32) 0%, rgba(139, 92, 246, 0.28) 100%);
    border-color: rgba(147, 197, 253, 0.7);
    color: #ffffff;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
    transform: translateY(-1px);
}

/* FÓRMULAS MATEMÁTICAS KATEX */
.ai-msg .katex {
    font-size: 1.08em;
    color: #f1f5f9;
}
.ai-msg .katex-display {
    margin: 10px 0;
    padding: 10px 14px;
    background: rgba(15, 23, 42, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: thin;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
}
.ai-msg .katex-display > .katex {
    text-align: center;
    display: block;
}

/* ESTRUCTURA Y ELEMENTOS DE MENSAJES */
.ai-section-title {
    font-size: 13.5px;
    font-weight: 700;
    color: #93c5fd;
    margin: 10px 0 6px 0;
    display: flex;
    align-items: center;
    gap: 6px;
    letter-spacing: 0.01em;
}
.ai-section-title:first-child { margin-top: 2px; }

.ai-list-item {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    margin: 6px 0;
    line-height: 1.5;
}

.ai-list-item .status-dot {
    margin-top: 6px;
    flex-shrink: 0;
}

/* TARJETAS REDISEÑADAS DE CLASES / RAMOS EN EL CHAT IA */
.ai-class-card {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.09);
    border-radius: 12px;
    padding: 9px 12px;
    margin: 6px 0;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    display: flex;
    align-items: flex-start;
    gap: 9px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.ai-class-card:hover {
    background: rgba(255, 255, 255, 0.07);
    border-color: rgba(96, 165, 250, 0.45);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.ai-class-card .status-dot {
    margin-top: 5px;
    flex-shrink: 0;
}

.ai-class-body {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.ai-course-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.ai-course-title {
    font-size: 13px;
    font-weight: 700;
    color: #ffffff;
    line-height: 1.35;
    letter-spacing: 0.01em;
    word-break: break-word;
}

.ai-sec-badge {
    display: inline-flex;
    align-items: center;
    background: rgba(148, 163, 184, 0.16);
    color: #cbd5e1;
    border: 1px solid rgba(148, 163, 184, 0.25);
    padding: 1px 6.5px;
    border-radius: 6px;
    font-size: 10.5px;
    font-weight: 600;
    white-space: nowrap;
    flex-shrink: 0;
}

.ai-class-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.ai-time-pill {
    display: inline-flex;
    align-items: center;
    gap: 4.5px;
    background: rgba(56, 189, 248, 0.12);
    color: #38bdf8;
    border: 1px solid rgba(56, 189, 248, 0.28);
    padding: 2px 7.5px;
    border-radius: 6px;
    font-size: 11.5px;
    font-weight: 600;
    white-space: nowrap;
}

/* BOTONES DE ACCIÓN RÁPIDA / SELECCIÓN DE DÍA */
.ai-quick-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 10px;
    padding-top: 6px;
}

.ai-quick-btn {
    display: inline-flex;
    align-items: center;
    gap: 6.5px;
    background: linear-gradient(135deg, rgba(37, 99, 235, 0.22) 0%, rgba(59, 130, 246, 0.14) 100%);
    border: 1px solid rgba(96, 165, 250, 0.4);
    color: #93c5fd;
    padding: 7px 13px;
    border-radius: 20px;
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.18s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.22);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    max-width: 100%;
}

.ai-quick-btn:hover {
    background: linear-gradient(135deg, rgba(37, 99, 235, 0.48) 0%, rgba(59, 130, 246, 0.38) 100%);
    border-color: rgba(147, 197, 253, 0.75);
    color: #ffffff;
    box-shadow: 0 4px 14px rgba(37, 99, 235, 0.4);
    transform: translateY(-1.5px);
}

.ai-quick-btn:active {
    transform: scale(0.96);
    background: rgba(37, 99, 235, 0.55);
    border-color: #60a5fa;
    color: #ffffff;
}

.ai-quick-btn .status-dot {
    flex-shrink: 0;
}

.ai-divider {
    border: none;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.15), transparent);
    margin: 12px 0;
}

/* INDICADOR DE ESCRITURA IA FLUIDO Y CONTENIDO */
.ai-typing-indicator {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 12px 18px !important;
    min-height: 38px;
    border-radius: 16px !important;
    width: fit-content;
    background: rgba(30, 41, 69, 0.8) !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.ai-input-row {
    padding: 10px 12px;
    background: #0f172a;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    display: flex;
    align-items: center;
    gap: 8px;
    contain: layout paint;
    transform: translateZ(0);
    isolation: isolate;
}

.ai-input-row input {
    flex: 1;
    background: #1e293b;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 10px;
    color: #f8fafc;
    padding: 9px 12px;
    font-size: 13px;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
    contain: layout paint style;
    transform: translateZ(0);
    -webkit-appearance: none;
}

.ai-input-row input:focus {
    border-color: #3b82f6;
    box-shadow: 0 0 10px rgba(59, 130, 246, 0.25);
    background: #1e2942;
}

.ai-send-btn {
    background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
    border: none;
    color: #ffffff;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.ai-send-btn:hover {
    transform: scale(1.06);
    box-shadow: 0 0 12px rgba(59, 130, 246, 0.5);
}

.ai-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* PREVIEW DE IMAGEN ADJUNTA (COMPACTO Y MINIMALISTA) */
.ai-attachment-preview {
    padding: 6px 12px;
    background: rgba(15, 23, 42, 0.95);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    display: flex;
    align-items: center;
    gap: 8px;
    animation: modalFadeUp 0.18s ease forwards;
}

.ai-preview-thumb-container {
    position: relative;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid rgba(59, 130, 246, 0.35);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
    flex-shrink: 0;
    background: #090e1a;
}

.ai-preview-thumb-container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.ai-preview-remove {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: rgba(15, 23, 42, 0.85);
    color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.2);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    line-height: 1;
    padding: 0;
    transition: all 0.15s ease;
}

.ai-preview-remove:hover {
    background: #ef4444;
    border-color: #ef4444;
}

.ai-preview-meta {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
    flex: 1;
}

.ai-preview-filename {
    font-size: 11.5px;
    color: #e2e8f0;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 220px;
}

.ai-preview-badge {
    font-size: 10px;
    color: #38bdf8;
    font-weight: 500;
}

/* BOTÓN DE ADJUNTAR IMAGEN */
.ai-attach-btn {
    background: rgba(30, 41, 69, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #94a3b8;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.ai-attach-btn:hover {
    color: #60a5fa;
    border-color: rgba(96, 165, 250, 0.45);
    background: rgba(30, 41, 69, 0.95);
    transform: scale(1.05);
}

.ai-attach-btn.has-image {
    color: #38bdf8;
    border-color: #38bdf8;
    background: rgba(56, 189, 248, 0.18);
    box-shadow: 0 0 10px rgba(56, 189, 248, 0.35);
}

/* LIGHTBOX PARA VER FOTOS EN TAMAÑO COMPLETO */
.ai-image-lightbox {
    position: fixed;
    inset: 0;
    background: rgba(3, 7, 18, 0.88);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: 3000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 16px;
    animation: modalFadeUp 0.15s ease forwards;
}

.ai-image-lightbox.open {
    display: flex;
}

.ai-lightbox-content {
    position: relative;
    max-width: 92vw;
    max-height: 88vh;
    border-radius: 14px;
    overflow: hidden;
    box-shadow: 0 25px 60px rgba(0, 0, 0, 0.75);
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: #0b1120;
}

.ai-lightbox-content img {
    max-width: 92vw;
    max-height: 88vh;
    object-fit: contain;
    display: block;
}

.ai-lightbox-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(15, 23, 42, 0.85);
    color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.2);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.15s ease;
    z-index: 10;
}

.ai-lightbox-close:hover {
    background: #ef4444;
    border-color: #ef4444;
    transform: scale(1.08);
}


/* Asistente unificado: conversación amplia y composición tipo chat moderno */
.ai-chat-window {
    width: 440px;
    height: min(650px, calc(100vh - 110px));
    background: rgba(10, 17, 32, 0.96);
}

.ai-chat-header {
    flex-shrink: 0;
}

.ai-title-text p {
    margin-top: 3px;
}

.ai-messages-area {
    padding: 18px;
    gap: 18px;
}

.ai-msg {
    font-size: 13.5px;
    line-height: 1.6;
}

.ai-msg.bot {
    max-width: 100%;
    padding: 2px 2px 2px 0;
    background: transparent;
    border: 0;
    border-radius: 0;
    box-shadow: none;
}

.ai-msg.user {
    max-width: 86%;
    padding: 10px 14px;
    background: rgba(51, 65, 85, 0.9);
    border-color: rgba(255, 255, 255, 0.1);
    border-radius: 16px 16px 4px 16px;
    box-shadow: none;
}

.ai-user-turn {
    align-self: stretch;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 8px;
}

.ai-user-image {
    display: block;
    max-width: 78%;
    border-radius: 14px;
    overflow: hidden;
    cursor: zoom-in;
    background: #060b16;
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.32);
}

.ai-user-image .ai-msg-img {
    display: block;
    width: auto;
    max-width: 100%;
    max-height: 260px;
    object-fit: contain;
}

.ai-list-marker {
    flex: 0 0 auto;
    color: #94a3b8;
    font-size: 14px;
    line-height: 1.45;
}

.ai-class-card {
    padding-left: 12px;
}

.ai-composer {
    flex-shrink: 0;
    padding: 10px 12px 9px;
    background: rgba(8, 15, 29, 0.98);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.ai-input-row {
    min-height: 50px;
    padding: 6px 7px;
    align-items: flex-end;
    gap: 6px;
    background: rgba(30, 41, 59, 0.72);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 16px;
}

.ai-input-row:focus-within {
    border-color: rgba(96, 165, 250, 0.48);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.08);
}

.ai-input-row textarea {
    flex: 1;
    min-width: 0;
    min-height: 36px;
    max-height: 120px;
    resize: none;
    overflow-y: auto;
    padding: 8px 5px;
    border: 0;
    outline: 0;
    background: transparent;
    color: #f8fafc;
    font: inherit;
    font-size: 13.5px;
    line-height: 1.45;
}

.ai-input-row textarea::placeholder {
    color: #64748b;
}

.ai-attach-btn,
.ai-send-btn {
    width: 36px;
    height: 36px;
    border-radius: 11px;
}

.ai-attach-btn {
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(148, 163, 184, 0.18);
    background: rgba(148, 163, 184, 0.08);
    color: #94a3b8;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.ai-attach-btn:hover {
    border-color: rgba(96, 165, 250, 0.34);
    background: rgba(59, 130, 246, 0.12);
    color: #bfdbfe;
}

.ai-send-btn {
    background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
    color: #ffffff;
    box-shadow: 0 3px 10px rgba(59, 130, 246, 0.28);
}

.ai-send-btn:hover {
    background: linear-gradient(135deg, #60a5fa 0%, #a78bfa 100%);
    box-shadow: 0 4px 13px rgba(99, 102, 241, 0.4);
}

.ai-composer-hint {
    padding-top: 7px;
    text-align: center;
    color: #64748b;
    font-size: 9.5px;
}

.ai-attachment-preview {
    width: fit-content;
    max-width: 100%;
    margin: 0 0 8px 4px;
    padding: 0;
    gap: 8px;
    background: transparent;
    border: 0;
}

.ai-preview-thumb-container {
    width: 64px;
    height: 64px;
    border-radius: 12px;
}

.ai-preview-filename {
    max-width: 260px;
    align-self: center;
    color: #94a3b8;
    font-weight: 500;
}

.ai-typing-indicator {
    align-self: flex-start;
    display: flex;
    align-items: center;
    justify-content: center;
    width: fit-content;
    min-width: 68px;
    min-height: 42px;
    padding: 10px 16px !important;
    background: linear-gradient(160deg, rgba(30, 41, 69, 0.82) 0%, rgba(15, 23, 42, 0.92) 100%) !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    border-radius: 16px !important;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35) !important;
}

.ai-thinking-dots {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    height: 12px;
}

.ai-thinking-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #bfdbfe;
    animation: aiThinkingPulse 1.1s ease-in-out infinite;
}

.ai-thinking-dot:nth-child(2) { animation-delay: 0.14s; }
.ai-thinking-dot:nth-child(3) { animation-delay: 0.28s; }

@keyframes aiThinkingPulse {
    0%, 60%, 100% { opacity: 0.38; transform: translateY(0) scale(0.82); }
    30% { opacity: 1; transform: translateY(-3px) scale(1); }
}

@media (max-width: 768px) {
    .ai-chat-window {
        right: 10px;
        bottom: 72px;
        width: calc(100vw - 20px);
        height: min(72vh, 620px);
        max-height: calc(100vh - 88px);
        border-radius: 18px;
    }

    .ai-messages-area {
        padding: 16px 14px;
    }

    .ai-user-image {
        max-width: 88%;
    }
}

/* Adaptación del diseño archivado al asistente actual y sus controles seguros. */
.ai-chat-window:not([hidden]) { display: flex; }
.ai-chat-window[hidden],
.ai-attachment-preview[hidden],
.ai-image-lightbox[hidden] { display: none !important; }
.ai-image-lightbox[aria-hidden="true"] { display: none; }
.ai-msg.error { color: #fecaca; }
.ai-msg.bot.error { color: #fecaca; }
.ai-input-row textarea { box-sizing: border-box; }
.ai-attachment-preview[hidden] { display: none !important; }
.ai-lightbox-close { padding: 0; }

@media (max-width: 768px) {
    .ai-chat-window {
        inset: 0;
        left: 0;
        right: 0;
        width: min(480px, calc(100vw - 28px));
        height: min(88dvh, 720px);
        max-height: calc(100dvh - 48px);
        margin: auto;
        border-radius: 20px;
        animation: none;
    }
    .ai-chat-window.ai-keyboard-open {
        top: var(--ai-keyboard-top, 16px);
        left: 50%;
        right: auto;
        bottom: auto;
        width: min(480px, calc(100vw - 28px));
        height: var(--ai-keyboard-panel-height, 88dvh);
        max-height: var(--ai-keyboard-panel-height, 88dvh);
        margin: 0;
        transform: translateX(-50%);
    }
}
@media (max-width: 560px) {
    .ai-fab-btn { bottom: max(16px, env(safe-area-inset-bottom)); }
    .ai-chat-window {
        width: min(480px, calc(100vw - 28px));
    }
}

/* Respuestas con estructura legible, fórmulas y bloques de código. */
.ai-msg.bot > p { margin: 0 0 0.8em; }
.ai-msg.bot > p:last-child { margin-bottom: 0; }
.ai-msg.bot h1,
.ai-msg.bot h2,
.ai-msg.bot h3,
.ai-msg.bot h4 { margin: 1.05em 0 0.42em; color: #f8fafc; line-height: 1.35; }
.ai-msg.bot h1 { font-size: 1.22em; }
.ai-msg.bot h2 { font-size: 1.14em; }
.ai-msg.bot h3,
.ai-msg.bot h4 { font-size: 1.06em; }
.ai-msg.bot ul,
.ai-msg.bot ol { margin: 0.45em 0 0.9em; padding-left: 1.5em; }
.ai-msg.bot li { margin: 0.28em 0; padding-left: 0.12em; }
.ai-msg.bot blockquote { margin: 0.65em 0; padding: 0.15em 0 0.15em 0.9em; border-left: 3px solid rgba(96,165,250,.58); color: #b9c7d9; }
.ai-msg.bot blockquote p { margin: 0.35em 0; }
.ai-msg.bot a { color: #93c5fd; text-decoration: underline; text-decoration-color: rgba(147,197,253,.45); text-underline-offset: 2px; }
.ai-msg.bot a:hover { color: #dbeafe; }
.ai-msg.bot code:not(.katex *) { padding: 0.12em 0.38em; border: 1px solid rgba(148,163,184,.18); border-radius: 5px; background: rgba(15,23,42,.82); color: #c4b5fd; font: 0.9em/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.ai-code-block { margin: 0.7em 0 0.9em; overflow: hidden; border: 1px solid rgba(148,163,184,.2); border-radius: 11px; background: #0b1220; }
.ai-code-language { padding: 6px 11px; border-bottom: 1px solid rgba(148,163,184,.14); color: #94a3b8; font: 10px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; text-transform: uppercase; letter-spacing: .06em; }
.ai-code-block pre { max-width: 100%; margin: 0; padding: 12px 14px; overflow: auto; color: #e2e8f0; font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; tab-size: 2; }
.ai-msg.bot .ai-code-block code:not(.katex *) { padding: 0; border: 0; border-radius: 0; background: transparent; color: inherit; font: inherit; white-space: pre; box-shadow: none; }
.ai-table-wrap { max-width: 100%; margin: 0.75em 0; overflow-x: auto; border: 1px solid rgba(148,163,184,.2); border-radius: 10px; scrollbar-width: thin; }
.ai-msg.bot table { width: 100%; border-collapse: collapse; font-size: .92em; }
.ai-msg.bot th,
.ai-msg.bot td { min-width: 70px; padding: 7px 10px; border-bottom: 1px solid rgba(148,163,184,.15); text-align: left; }
.ai-msg.bot th { color: #f8fafc; background: rgba(30,41,59,.72); font-weight: 700; }
.ai-msg.bot tr:last-child td { border-bottom: 0; }
.ai-msg.bot hr { height: 1px; margin: 0.85em 0; border: 0; background: rgba(148,163,184,.25); }
.ai-math-inline { display: inline-block; max-width: 100%; color: #f1f5f9; vertical-align: middle; }
.ai-math-block { width: 100%; min-width: 0; max-width: 100%; box-sizing: border-box; margin: 0.85em 0; overflow-x: auto; overflow-y: hidden; color: #f1f5f9; scrollbar-width: thin; }
.ai-math-block .katex-display { width: max-content; min-width: 100%; box-sizing: border-box; margin: 0; padding: 0.55em 0.8em; border: 1px solid rgba(148,163,184,.15); border-radius: 10px; background: rgba(15,23,42,.62); text-align: left; }
.ai-math-render-fallback { padding: 0.08em 0.38em; border-radius: 5px; background: rgba(148,163,184,.12); color: #e2e8f0; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; overflow-wrap: anywhere; }

/* Recupera el panel archivado y la cápsula de respuesta del asistente anterior a V1. */
.ai-chat-window:not([hidden]) { background: rgba(20, 30, 49, 0.98); }
.ai-messages-area {
    background: linear-gradient(135deg, rgba(30, 41, 69, 0.85) 0%, rgba(15, 23, 42, 0.95) 100%);
}
.ai-composer {
    background: linear-gradient(135deg, rgba(30, 41, 69, 0.85) 0%, rgba(15, 23, 42, 0.95) 100%);
}
.ai-msg.bot:not(.ai-typing-indicator) {
    align-self: flex-start;
    max-width: 90%;
    padding: 12px 16px;
    background: linear-gradient(160deg, rgba(30, 41, 69, 0.82) 0%, rgba(15, 23, 42, 0.92) 100%);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

/* Mantiene el mismo degradado del encabezado en todo el panel y el compositor. */
.ai-chat-window:not([hidden]),
.ai-messages-area,
.ai-composer {
    background: linear-gradient(135deg, rgba(30, 41, 69, 0.85) 0%, rgba(15, 23, 42, 0.95) 100%);
}

@media (max-width: 560px) {
    .ai-chat-header { display: grid; grid-template-columns: minmax(0, 1fr) auto; width: 100%; min-width: 0; box-sizing: border-box; gap: 8px; padding: 11px 12px; }
    .ai-header-info { width: 100%; min-width: 0; gap: 8px; }
    .ai-avatar { flex: 0 0 30px; width: 30px; height: 30px; }
    .ai-title-text { min-width: 0; overflow: hidden; }
    .ai-title-text h4,
    .ai-title-text p { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
    .ai-title-text p { display: block; }
    .ai-header-actions { align-self: center; justify-content: flex-end; min-width: 44px; height: 44px; gap: 4px; }
    .ai-header-btn,
    .ai-close-btn { display: inline-flex; align-items: center; justify-content: center; flex: 0 0 44px; width: 44px; height: 44px; box-sizing: border-box; margin: 0; padding: 0; line-height: 1; vertical-align: middle; }
    .ai-messages-area { width: 100%; min-width: 0; min-height: 0; box-sizing: border-box; padding-top: 24px; overflow-x: hidden; }
    .ai-msg.bot:not(.ai-typing-indicator) { width: fit-content; min-width: 0; max-width: 100%; box-sizing: border-box; }
    .ai-math-inline { min-width: 0; max-width: 100%; box-sizing: border-box; overflow-x: auto; overflow-y: hidden; }
    .ai-math-block .katex-display { padding: 0.5em 0.65em; }
    .ai-msg.bot h1 { font-size: 1.15em; }
    .ai-code-block pre { padding: 10px 11px; font-size: 11px; }
    .ai-msg.bot th,
    .ai-msg.bot td { padding: 6px 8px; }
}

@media (prefers-reduced-motion: reduce) {
    .ai-thinking-dot { animation-duration: 2.8s; }
}
