/*
 * Estilos del widget de chat Spark AI Assistant.
 * Interfaz limpia, minimalista y responsiva (Estilo Joinchat).
 * Ruta: spark-ai-assistant/public/css/spark-widget.css
 */

/* ── Variables ─────────────────────────────────────────────────────────────── */
:root {
    --spark-primary:       #E98B17; /* Sobreescrito por JS con el valor del panel */
    --spark-primary-dark:  #c97610;
    
    /* Tema Claro Minimalista */
    --spark-bg-panel:      #ffffff;
    --spark-bg-header:     #ffffff;
    --spark-bg-body:       #f9fafb;
    --spark-bg-bot-msg:    #f3f4f6;
    --spark-text-main:     #111827;
    --spark-text-muted:    #6b7280;
    --spark-border:        #e5e7eb;
    
    /* Dimensiones y Sombras */
    --spark-shadow-btn:    0 4px 14px rgba(0, 0, 0, 0.15);
    --spark-shadow-panel:  0 10px 40px rgba(0, 0, 0, 0.12);
    --spark-radius:        16px;
    --spark-radius-msg:    14px;
    --spark-font:          'Poppins', -apple-system, BlinkMacSystemFont, sans-serif;
    --spark-width:         340px; /* Más pequeño y estilizado */
    --spark-height:        480px;
    --spark-z:             999999;
    --spark-bottom: max(24px, calc(env(safe-area-inset-bottom, 0px) + 16px));  /* ← reemplazada */
    --spark-side:          24px;
}

/* ── Contenedor raíz ───────────────────────────────────────────────────────── */
#spark-root {
    position: fixed;
    bottom: var(--spark-bottom);
    right: var(--spark-side);
    z-index: var(--spark-z);
    font-family: var(--spark-font);
    box-sizing: border-box;
}

#spark-root * {
    box-sizing: border-box;
}

#spark-root.spark--left {
    right: auto;
    left: var(--spark-side);
}

/* ── Launcher (botón flotante estilo Joinchat) ─────────────────────────────── */
.spark-launcher {
    position: fixed;
    bottom: var(--spark-bottom);
    right: var(--spark-side);
    z-index: var(--spark-z);
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--spark-primary);
    border: none !important;     /* Fuerza eliminación de líneas del theme */
    outline: none !important;
    padding: 0 !important;       /* Sin padding para que la imagen ocupe todo */
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--spark-shadow-btn);
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.3s ease;
}

.spark-launcher.spark--left {
    right: auto;
    left: var(--spark-side);
}

.spark-launcher:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.spark-launcher:active {
    transform: scale(0.95);
}

.spark-launcher:focus-visible {
    outline: 2px solid var(--spark-primary) !important;
    outline-offset: 3px;
}

.spark-launcher-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    border: none !important; /* Asegura que la imagen no tenga bordes */
}

/* Invitación discreta junto al botón flotante, independiente del panel. */
.spark-invite {
    position: fixed;
    bottom: calc(var(--spark-bottom) + 72px);
    right: var(--spark-side);
    z-index: var(--spark-z);
    display: flex;
    align-items: flex-start;
    width: min(250px, calc(100vw - 48px));
    background: #fff;
    border: 1px solid var(--spark-border);
    border-radius: 12px;
    box-shadow: 0 6px 22px rgba(17, 24, 39, 0.12);
    animation: spark-invite-in 0.25s ease-out;
}

.spark-invite[hidden] {
    display: none;
}

.spark-invite.spark--left {
    right: auto;
    left: var(--spark-side);
}

.spark-invite::after {
    content: '';
    position: absolute;
    right: 24px;
    bottom: -6px;
    width: 10px;
    height: 10px;
    background: #fff;
    border-right: 1px solid var(--spark-border);
    border-bottom: 1px solid var(--spark-border);
    transform: rotate(45deg);
}

.spark-invite.spark--left::after {
    right: auto;
    left: 24px;
}

.spark-invite__message {
    flex: 1;
    min-width: 0;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 4;
    overflow: hidden;
    padding: 10px 4px 10px 12px !important;
    border: 0 !important;
    background: transparent !important;
    color: var(--spark-text-main) !important;
    font: 500 12px/1.45 var(--spark-font) !important;
    text-align: left;
    white-space: pre-line;
    overflow-wrap: anywhere;
    cursor: pointer;
}

.spark-invite__message:hover {
    color: var(--spark-primary-dark) !important;
}

.spark-invite__close {
    flex: none;
    width: 36px;
    height: 36px;
    padding: 0 !important;
    border: 0 !important;
    background: transparent !important;
    color: var(--spark-text-muted) !important;
    font: 22px/36px var(--spark-font);
    text-align: center;
    cursor: pointer;
}

.spark-invite__message:focus-visible,
.spark-invite__close:focus-visible {
    outline: 2px solid var(--spark-primary);
    outline-offset: -3px;
    border-radius: 10px;
}

@keyframes spark-invite-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ── Panel del chat ─────────────────────────────────────────────────────────── */
.spark-panel {
    position: fixed;
    bottom: calc( var(--spark-bottom) + 75px );
    right: var(--spark-side);
    z-index: var(--spark-z);
    width: var(--spark-width);
    height: var(--spark-height);
    background: var(--spark-bg-panel);
    border-radius: var(--spark-radius);
    border: 1px solid var(--spark-border);
    box-shadow: var(--spark-shadow-panel);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Animación de entrada fluida */
    opacity: 0;
    transform: translateY(20px) scale(0.98);
    transition: opacity 0.3s ease, transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    pointer-events: none;
}

.spark-panel[hidden] {
    display: none;
}

.spark-panel.spark--left {
    right: auto;
    left: var(--spark-side);
}

.spark-panel--visible {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

/* ── Cabecera ──────────────────────────────────────────────────────────────── */
.spark-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    background: var(--spark-bg-header);
    border-bottom: 1px solid var(--spark-border);
    flex-shrink: 0;
}

.spark-header__identity {
    display: flex;
    align-items: center;
    gap: 12px;
}

.spark-header__avatar {
    position: relative;
    width: 42px;
    height: 42px;
    flex-shrink: 0;
}

.spark-avatar-img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    border: none; /* Eliminado el borde de color para más limpieza */
}

.spark-avatar-fallback {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: var(--spark-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 18px;
}

.spark-online-dot {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 12px;
    height: 12px;
    background: #10b981;
    border-radius: 50%;
    border: 2px solid var(--spark-bg-header);
}

.spark-header__name {
    display: block;
    font-size: 15px;
    font-weight: 600;
    color: var(--spark-text-main);
    line-height: 1.2;
}

.spark-header__sub {
    display: block;
    font-size: 12px;
    color: var(--spark-text-muted);
}

.spark-close-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--spark-text-muted);
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, color 0.2s;
}

.spark-close-btn:hover {
    color: var(--spark-text-main);
    background: #f3f4f6;
}

/* ── Área de mensajes ──────────────────────────────────────────────────────── */
.spark-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: var(--spark-bg-body);
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

.spark-messages::-webkit-scrollbar {
    width: 6px;
}
.spark-messages::-webkit-scrollbar-track {
    background: transparent;
}
.spark-messages::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 10px;
}

/* ── Filas de mensaje ──────────────────────────────────────────────────────── */
.spark-msg-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 90%;
}
.spark-msg-row--bot  { align-self: flex-start; }
.spark-msg-row--user { align-self: flex-end; flex-direction: row-reverse; }

/* ── Avatar en los mensajes ────────────────────────────────────────────────── */
.spark-msg-avatar-wrap {
    flex-shrink: 0;
}

.spark-msg-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
}

.spark-msg-avatar--fallback {
    background: var(--spark-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
}

/* ── Burbujas de mensaje ───────────────────────────────────────────────────── */
.spark-bubble {
    padding: 12px 16px;
    border-radius: var(--spark-radius-msg);
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.spark-bubble--bot {
    background: var(--spark-bg-bot-msg);
    color: var(--spark-text-main);
    border-bottom-left-radius: 4px;
}

.spark-bubble--user {
    background: var(--spark-primary);
    color: #ffffff;
    border-bottom-right-radius: 4px;
    font-weight: 500;
}

/* ── Tablas dentro de las burbujas del bot ──────────────────────────────── */
.spark-bubble--bot table {
    border-collapse: separate;
    border-spacing: 0;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    min-width: 200px;
    margin: 10px 0;
    font-size: 12.5px;
    line-height: 1.45;
    border-radius: 10px;
    border: 1px solid var(--spark-border);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.spark-bubble--bot table:first-child { margin-top: 0; }
.spark-bubble--bot table:last-child  { margin-bottom: 0; }

.spark-bubble--bot th,
.spark-bubble--bot td {
    padding: 8px 10px;
    text-align: left;
    vertical-align: top;
    border-top: 1px solid var(--spark-border);
    border-left: 1px solid var(--spark-border);
    white-space: nowrap;
}

.spark-bubble--bot tr:first-child th,
.spark-bubble--bot tr:first-child td { border-top: none; }

.spark-bubble--bot th:first-child,
.spark-bubble--bot td:first-child { border-left: none; }

.spark-bubble--bot th {
    background: var(--spark-primary);
    color: #ffffff;
    font-weight: 600;
    font-size: 12.5px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.spark-bubble--bot tbody tr:nth-child(even) { background: #fcfcfd; }
.spark-bubble--bot tbody tr:hover           { background: #f3f4f6; }

.spark-bubble--bot caption {
    caption-side: top;
    padding: 0 0 6px;
    font-size: 12px;
    font-weight: 600;
    color: var(--spark-text-muted);
    text-align: left;
}

/* Celdas de números: alineadas a la derecha y con tabulación de moneda */
.spark-bubble--bot td[align="right"],
.spark-bubble--bot th[align="right"] { text-align: right; }

/* Encabezados/títulos cortos dentro de la burbuja */
.spark-bubble--bot h1, .spark-bubble--bot h2,
.spark-bubble--bot h3, .spark-bubble--bot h4 {
    margin: 10px 0 6px;
    line-height: 1.3;
    color: var(--spark-text-main);
}
.spark-bubble--bot h1 { font-size: 17px; }
.spark-bubble--bot h2 { font-size: 16px; }
.spark-bubble--bot h3 { font-size: 15px; }
.spark-bubble--bot h4 { font-size: 14px; }
.spark-bubble--bot h1:first-child, .spark-bubble--bot h2:first-child,
.spark-bubble--bot h3:first-child, .spark-bubble--bot h4:first-child { margin-top: 0; }

.spark-bubble--bot blockquote {
    margin: 8px 0;
    padding: 8px 12px;
    border-left: 3px solid var(--spark-primary);
    background: rgba(0, 0, 0, 0.03);
    border-radius: 0 8px 8px 0;
    color: var(--spark-text-muted);
}
.spark-bubble--bot ul, .spark-bubble--bot ol {
    margin: 6px 0;
    padding-left: 20px;
}
.spark-bubble--bot li { margin: 2px 0; }
.spark-bubble--bot a { color: var(--spark-primary-dark); text-decoration: underline; }

/* ── Indicador "escribiendo..." ────────────────────────────────────────────── */
.spark-bubble--typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 14px 18px;
    min-width: 60px;
}

.spark-bubble--typing span {
    width: 6px;
    height: 6px;
    background: var(--spark-text-muted);
    border-radius: 50%;
    display: inline-block;
    animation: spark-typing 1.4s infinite ease-in-out both;
}

.spark-bubble--typing span:nth-child(1) { animation-delay: -0.32s; }
.spark-bubble--typing span:nth-child(2) { animation-delay: -0.16s; }

@keyframes spark-typing {
    0%, 80%, 100% { transform: scale(0); opacity: 0.5; }
    40%           { transform: scale(1); opacity: 1; }
}

/* ── Quick replies ──────────────────────────────────────────────────────────── */
.spark-quick-replies {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 4px 0 8px;
    max-width: 100%;
    align-self: flex-start;
    justify-content: flex-start;
}

.spark-quick-reply-btn {
    background: #f9fafb;
    border: 1px solid var(--spark-border);
    border-radius: 16px;
    padding: 6px 12px;
    font-size: 12px;
    font-family: var(--spark-font);
    color: var(--spark-text-main);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.1s;
    line-height: 1.3;
    white-space: nowrap;
    text-align: center;
    min-width: 0;
    flex: 0 1 auto;
}

.spark-quick-reply-btn:hover {
    background: var(--spark-primary);
    border-color: var(--spark-primary);
    color: #ffffff;
    transform: translateY(-1px);
}

.spark-quick-reply-btn:focus-visible {
    outline: 2px solid var(--spark-primary);
    outline-offset: 2px;
}

.spark-quick-reply-btn:active {
    transform: scale(0.96);
}

/* Mobile: más compactos, dos por fila si caben */
@media (max-width: 480px) {
    .spark-quick-reply-btn {
        padding: 5px 10px;
        font-size: 11px;
        border-radius: 14px;
    }
    .spark-quick-replies {
        gap: 5px;
    }
}

/* ── Formulario de input ───────────────────────────────────────────────────── */
.spark-form {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 16px;
    background: var(--spark-bg-panel);
    border-top: 1px solid var(--spark-border);
    flex-shrink: 0;
}

.spark-input {
    flex: 1;
    background: #f9fafb;
    border: 1px solid var(--spark-border);
    border-radius: 24px;
    padding: 12px 16px;
    font-size: 14px;
    font-family: var(--spark-font);
    color: var(--spark-text-main);
    outline: none;
    transition: border-color 0.2s ease, background 0.2s ease;
}

.spark-input::placeholder {
    color: #9ca3af;
}

.spark-input:focus {
    border-color: var(--spark-primary);
    background: #ffffff;
}

.spark-input:focus-visible {
    outline: 2px solid var(--spark-primary);
    outline-offset: 1px;
}

.spark-input:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.spark-send-btn {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: var(--spark-primary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    flex-shrink: 0;
    transition: transform 0.2s ease, background 0.2s ease;
}

.spark-send-btn:hover {
    background: var(--spark-primary-dark);
    transform: scale(1.05);
}

.spark-send-btn:active {
    transform: scale(0.95);
}

.spark-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* ── Footer del panel ──────────────────────────────────────────────────────── */
.spark-footer {
    text-align: center;
    padding: 6px 0 8px;
    border-top: 1px solid #f0f0f0;
}

.spark-footer__brand {
    font-size: 10px;
    color: #aaa;
    letter-spacing: 0.3px;
}

/* ── Responsive: móvil ─────────────────────────────────────────────────────── */
@media (max-width: 480px) {

    .spark-invite {
        right: 16px;
        width: min(245px, calc(100vw - 32px));
        bottom: calc(var(--spark-bottom) + 66px);
    }

    .spark-invite__message {
        font-size: 12.5px !important;
    }

    .spark-invite.spark--left {
        left: 16px;
    }
    
    /* Panel full-screen modal (estilo WhatsApp/Messenger) */
    .spark-panel {
        position: fixed;
        inset: 0;
        width: 100vw;
        height: var(--spark-visible-height, 100dvh);
        max-height: none;
        border-radius: 0;
        bottom: auto;
        right: auto;
        left: 0;
        top: var(--spark-visible-top, 0px);
        z-index: calc(var(--spark-z) + 100);
        display: flex;
        flex-direction: column;
        box-shadow: none;
        border: none;
        /* Animación nativa slide-up + fade */
        opacity: 0;
        transform: translateY(20px);
        transition: opacity 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94),
                    transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    }

    .spark-panel.spark-panel--visible {
        opacity: 1;
        transform: translateY(0);
    }

    .spark-panel.spark--left,
    .spark-panel.spark--right {
        left: 0;
        right: 0;
    }

    /* Header fijo arriba con safe-area */
    .spark-header {
        position: sticky;
        top: 0;
        z-index: 10;
        background: var(--spark-bg-header);
        border-bottom: 1px solid var(--spark-border);
        padding-top: calc(16px + env(safe-area-inset-top, 0px));
        /* Shadow suave al hacer scroll */
        transition: box-shadow 0.2s ease;
    }

    .spark-header.scrolled {
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    }
    
    .spark-header.spark-header--scrolled {
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    }

    /* Área de mensajes: scroll interno, ocupa el espacio medio */
    .spark-messages {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    }

    /* El layout flex mantiene el formulario al fondo sin reposicionarlo al abrir el teclado. */
    .spark-form {
        background: var(--spark-bg-panel);
        border-top: 1px solid var(--spark-border);
        padding: 12px 16px;
    }

    .spark-footer {
        flex-shrink: 0;
        padding: 4px 0 calc(4px + env(safe-area-inset-bottom, 0px));
    }

    .spark-footer__brand {
        font-size: 9px;
    }

    /* Launcher: animación de salida al abrir panel */
    .spark-launcher {
        right: 16px;
        width: 56px;
        height: 56px;
        transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s ease;
    }

    .spark-launcher.launcher-hidden {
        opacity: 0;
        transform: scale(0.8);
        visibility: hidden;
        pointer-events: none;
    }

    .spark-launcher.spark--left {
        right: auto;
        left: 16px;
    }
}

/* ── Accesibilidad: reducir movimiento ─────────────────────────────────────── */
@media ( prefers-reduced-motion: reduce ) {
    .spark-panel,
    .spark-launcher,
    .spark-invite,
    .spark-bubble--typing span {
        animation: none !important;
        transition: none !important;
    }
}
