/**
 * REI — Widget Assistente AI (chat 24/7)
 * Bolla flottante bottom-right su tutte le pagine; pannello full-screen su mobile.
 *
 * @since 1.8.0
 */

/* Posizione bottom-right verificata: gli altri floating del tema stanno
   altrove (toggle accessibilità bottom-left 24px, badge cookie bottom-left
   90px, promo CTA top-left) → nessuna collisione, nessuno stacking extra. */
.rei-ai-chat {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9998;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
    color: #157155;
}
.rei-ai-chat *, .rei-ai-chat *::before, .rei-ai-chat *::after { box-sizing: border-box; max-width: 100%; }

/* ===== FAB (bolla chiusa) ===== */
.rei-ai-chat__fab {
    position: relative;
    width: 44px; height: 44px;
    border-radius: 50%; border: 0;
    background: linear-gradient(135deg, #20A47B 0%, #20A47B 100%);
    color: #fff; cursor: pointer;
    box-shadow: 0 12px 28px rgba(32,164,123,0.45), 0 4px 10px rgba(0,0,0,0.18);
    display: flex; align-items: center; justify-content: center;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.rei-ai-chat__fab:hover { transform: translateY(-3px) scale(1.04); box-shadow: 0 16px 36px rgba(32,164,123,0.55), 0 6px 14px rgba(0,0,0,0.22); }
.rei-ai-chat__fab:focus-visible { outline: 3px solid #D1E000; outline-offset: 3px; }
.rei-ai-chat__fab::before {
    content: "";
    position: absolute; inset: -6px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(32,164,123,0.35) 0%, rgba(32,164,123,0) 70%);
    z-index: -1;
    animation: rei-ai-pulse 2.4s ease-in-out infinite;
}
@keyframes rei-ai-pulse {
    0%, 100% { transform: scale(1); opacity: 0.6; }
    50%      { transform: scale(1.15); opacity: 0.2; }
}
.rei-ai-chat__fab-icon { width: 22px; height: 22px; }
.rei-ai-chat__fab-dot {
    position: absolute; top: 4px; right: 4px;
    width: 10px; height: 10px; border-radius: 50%;
    background: #D1E000; border: 2px solid #fff;
    box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}

/* Stato aperto: nascondi FAB */
.rei-ai-chat.is-open .rei-ai-chat__fab { display: none; }

/* ===== PANNELLO ===== */
.rei-ai-chat__panel {
    width: 380px; height: 580px; max-height: calc(100vh - 48px);
    background: #F4F8F6;
    border-radius: 18px; overflow: hidden;
    box-shadow: 0 24px 60px rgba(0,0,0,0.28), 0 8px 20px rgba(0,0,0,0.14);
    display: flex; flex-direction: column;
    border: 1px solid rgba(0,0,0,0.06);
    transform-origin: bottom right;
    animation: rei-ai-pop 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes rei-ai-pop {
    from { transform: translateY(20px) scale(0.92); opacity: 0; }
    to   { transform: translateY(0) scale(1); opacity: 1; }
}
.rei-ai-chat__panel[hidden] { display: none; }

/* Header */
.rei-ai-chat__header {
    background: linear-gradient(135deg, #20A47B 0%, #20A47B 100%);
    color: #fff;
    padding: 14px 16px;
    display: flex; align-items: center; gap: 12px;
}
.rei-ai-chat__avatar {
    width: 38px; height: 38px; border-radius: 50%;
    background: rgba(255,255,255,0.18);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.rei-ai-chat__avatar svg { width: 20px; height: 20px; }
.rei-ai-chat__title { flex: 1; min-width: 0; }
.rei-ai-chat__title-main { font-weight: 700; font-size: 14.5px; line-height: 1.2; }
.rei-ai-chat__title-sub { font-size: 11px; opacity: 0.92; display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.rei-ai-chat__title-sub::before {
    content: ""; width: 7px; height: 7px; border-radius: 50%;
    background: #29D8A1; box-shadow: 0 0 6px #29D8A1;
}
.rei-ai-chat__close {
    width: 32px; height: 32px; border-radius: 50%;
    background: rgba(255,255,255,0.15); border: 0; color: #fff;
    font-size: 18px; line-height: 1; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.15s;
}
.rei-ai-chat__close:hover { background: rgba(255,255,255,0.28); }
.rei-ai-chat__close:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }

/* Body (lista messaggi) */
.rei-ai-chat__body {
    flex: 1; overflow-y: auto;
    padding: 16px;
    display: flex; flex-direction: column; gap: 10px;
    background: #F4F8F6;
    scrollbar-width: thin;
    scrollbar-color: rgba(0,0,0,0.2) transparent;
}
.rei-ai-chat__body::-webkit-scrollbar { width: 6px; }
.rei-ai-chat__body::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.2); border-radius: 3px; }

.rei-ai-chat__msg {
    max-width: 82%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 13.8px; line-height: 1.5;
    word-wrap: break-word;
    animation: rei-ai-msg-in 0.2s ease-out;
}
@keyframes rei-ai-msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}
.rei-ai-chat__msg--assistant {
    align-self: flex-start;
    background: #fff; color: #157155;
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.rei-ai-chat__msg--user {
    align-self: flex-end;
    background: linear-gradient(135deg, #20A47B, #20A47B);
    color: #fff;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 8px rgba(32,164,123,0.25);
}
.rei-ai-chat__msg strong { font-weight: 700; }
.rei-ai-chat__msg--assistant strong { color: #20A47B; }
.rei-ai-chat__msg a { color: inherit; text-decoration: underline; }
.rei-ai-chat__msg--error {
    background: #fff5f5; color: #D63638;
    border: 1px solid #f5c2c2; box-shadow: none;
}

/* Indicatore "sta scrivendo…" */
.rei-ai-chat__typing {
    align-self: flex-start;
    background: #fff;
    padding: 12px 16px; border-radius: 16px; border-bottom-left-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    display: flex; gap: 4px;
}
.rei-ai-chat__typing span {
    width: 7px; height: 7px; border-radius: 50%;
    background: #20A47B;
    animation: rei-ai-typing 1.3s ease-in-out infinite;
}
.rei-ai-chat__typing span:nth-child(2) { animation-delay: 0.18s; }
.rei-ai-chat__typing span:nth-child(3) { animation-delay: 0.36s; }
@keyframes rei-ai-typing {
    0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
    30%           { opacity: 1;   transform: translateY(-4px); }
}

/* Chips suggerimenti */
.rei-ai-chat__chips {
    display: flex; flex-wrap: wrap; gap: 6px;
    padding: 0 16px 8px;
}
.rei-ai-chat__chips.is-hidden { display: none; }
.rei-ai-chat__chip {
    font-size: 12px; padding: 6px 12px; border-radius: 14px;
    background: #fff; border: 1px solid #D2F0E6; color: #20A47B;
    cursor: pointer; font-family: inherit;
    transition: background 0.15s, transform 0.1s;
}
.rei-ai-chat__chip:hover { background: #E4F6F0; transform: translateY(-1px); }

/* Footer (input) */
.rei-ai-chat__footer {
    padding: 10px 14px 8px;
    background: #F4F8F6;
    border-top: 1px solid rgba(0,0,0,0.06);
}
.rei-ai-chat__form { margin: 0; }
.rei-ai-chat__input-wrap {
    display: flex; gap: 8px; align-items: center;
    background: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 22px;
    padding: 6px 6px 6px 14px;
    transition: border-color 0.15s;
}
.rei-ai-chat__input-wrap:focus-within { border-color: #20A47B; }
.rei-ai-chat__input {
    flex: 1; border: 0; outline: 0;
    font-size: 13.8px; padding: 8px 0;
    background: transparent; color: #157155;
    font-family: inherit;
}
.rei-ai-chat__send {
    width: 36px; height: 36px; border-radius: 50%;
    background: linear-gradient(135deg, #20A47B, #20A47B);
    border: 0; color: #fff; cursor: pointer; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    transition: transform 0.1s, box-shadow 0.15s;
}
.rei-ai-chat__send:hover { transform: scale(1.06); box-shadow: 0 4px 12px rgba(32,164,123,0.4); }
.rei-ai-chat__send:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }
.rei-ai-chat__send svg { width: 16px; height: 16px; }
.rei-ai-chat__hp { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }
.rei-ai-chat__disclaimer {
    font-size: 10.5px; color: #888;
    text-align: center; margin-top: 6px;
}

/* ===== MOBILE: pannello full-screen ===== */
@media (max-width: 600px) {
    .rei-ai-chat { bottom: 16px; right: 16px; }
    /* v1.8.6: uso 100% invece di 100vw (100vw su iOS Safari include la scrollbar e
       sballa con la tastiera virtuale). overflow-x: hidden a tutti i livelli
       impedisce ai messaggi lunghi di trascinare fuori il pannello. */
    .rei-ai-chat.is-open {
        top: 0 !important; bottom: 0 !important;
        left: 0 !important; right: 0 !important;
        width: 100%;
        max-width: 100vw;
        height: 100vh; height: 100dvh;
        overflow: hidden;
    }
    .rei-ai-chat.is-open .rei-ai-chat__panel {
        width: 100%;
        max-width: 100%;
        height: 100%; max-height: 100%;
        border-radius: 0; border: 0;
        overflow: hidden; /* nessun overflow orizzontale possibile */
        animation: rei-ai-pop-mob 0.22s ease-out;
    }
    @keyframes rei-ai-pop-mob {
        from { opacity: 0; }
        to   { opacity: 1; }
    }
    /* body messaggi: scroll solo verticale, mai orizzontale */
    .rei-ai-chat__body {
        padding: 14px; gap: 8px;
        overflow-x: hidden;
        overflow-y: auto;
        width: 100%;
        max-width: 100%;
    }
    /* v1.8.6: messaggi vincolati alla larghezza del body, spezzano sempre */
    .rei-ai-chat__msg {
        max-width: 85%;
        width: auto;
        word-break: break-word;
        overflow-wrap: anywhere;
        font-size: 14.5px;
        box-sizing: border-box;
    }
    .rei-ai-chat__chips { padding: 0 14px 8px; max-width: 100%; }
    /* footer: blocca espansione, input non deve mai sforare */
    .rei-ai-chat__footer {
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        width: 100%;
        box-sizing: border-box;
        overflow: hidden;
    }
    .rei-ai-chat__input-wrap { max-width: 100%; min-width: 0; }
    /* v1.8.7: 16px è il MINIMO per impedire ad iOS Safari di zoomare automaticamente
       sull'input quando l'utente lo tocca (lo zoom rendeva la chat fuori schermo) */
    .rei-ai-chat__input { min-width: 0; font-size: 16px; }
    .rei-ai-chat__header { padding-top: max(14px, env(safe-area-inset-top)); }
}

@media (prefers-reduced-motion: reduce) {
    .rei-ai-chat__fab::before { animation: none; }
    .rei-ai-chat__panel, .rei-ai-chat__msg, .rei-ai-chat__typing span { animation: none; }
}
