/* ============================================
   CHATBOT — ReyMysterio (panneau de l'accueil + dock flottant)
   ============================================ */
.chat-panel {
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border-radius: var(--radius-xl);
    background: var(--surface-low);
    border: 1px solid rgba(72, 72, 72, 0.6);
    box-shadow: 0 24px 48px rgba(0, 0, 0, 0.45), 0 0 32px rgba(255, 255, 255, 0.08);
}

/* ---------- En-tête ---------- */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md);
    background: rgba(41, 42, 47, 0.9);
    border-bottom: 1px solid rgba(72, 72, 72, 0.5);
}

.chat-header-id {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    min-width: 0;
}

.chat-avatar {
    width: 32px;
    height: 32px;
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.2);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chat-header-text { min-width: 0; }

.chat-title {
    display: flex;
    align-items: center;
    gap: 6px;
    font-family: var(--font-body);
    font-size: var(--text-sm);
    line-height: 20px;
    font-weight: 600;
}

.chat-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0 var(--space-sm);
    font-family: var(--font-label);
    font-size: var(--text-xs);
    line-height: 16px;
    color: var(--outline);
}

.chat-meta > span:first-child {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.chat-meta .dot { background: var(--tertiary); }

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.chat-icon-btn {
    width: 28px;
    height: 28px;
    border-radius: var(--radius);
    color: var(--outline);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s, color 0.2s;
}

.chat-icon-btn .icon { font-size: var(--text-base); }
.chat-icon-btn:hover { background: var(--surface-highest); color: var(--on-surface); }

/* ---------- Messages ---------- */
.chat-messages {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    height: 300px;
    padding: var(--space-md);
    overflow-y: auto;
    overscroll-behavior: contain;
    font-size: var(--text-sm);
    line-height: 20px;
    scrollbar-width: thin;
}

.chat-msg {
    display: flex;
    gap: 10px;
    animation: msg-in 0.25s ease;
}

.chat-msg.bot { align-items: flex-start; }
.chat-msg.user { justify-content: flex-end; }

.chat-msg-avatar {
    width: 28px;
    height: 28px;
    margin-top: 2px;
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.2);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* Logo de ReyMysterio (js/chatbot/avatar.js) dans tous les avatars. */
.chat-avatar,
.chat-msg-avatar,
.chat-launcher-avatar { background: var(--surface-lowest); }

.bot-avatar {
    width: 78%;
    height: 78%;
    display: block;
}

/* Réponses mises en forme : paragraphes, listes, gras. */
.chat-answer {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    line-height: 1.55;
}

.chat-answer ul {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding-left: 1.1em;
}

.chat-answer li::marker { color: var(--secondary); }

.chat-answer strong { color: var(--on-surface); font-weight: 600; }

/* Curseur d'écriture pendant que la réponse s'affiche. */
.chat-caret {
    display: inline-block;
    width: 7px;
    height: 14px;
    margin-left: 2px;
    vertical-align: -2px;
    border-radius: 1px;
    background: var(--secondary);
    animation: chat-caret 0.8s steps(1) infinite;
}

@keyframes chat-caret {
    50% { opacity: 0; }
}

.chat-msg-col {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-width: 88%;
    min-width: 0;
}

.chat-bubble {
    padding: 10px 12px;
    border-radius: 16px;
    overflow-wrap: anywhere;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.chat-msg.bot .chat-bubble {
    border-top-left-radius: 0;
    background: var(--surface-container);
    color: var(--on-surface-variant);
}

.chat-msg.user .chat-bubble {
    max-width: 85%;
    border-top-right-radius: 0;
    background: var(--primary);
    color: var(--on-primary);
}

.chat-msg-meta {
    padding-left: 4px;
    font-family: var(--font-label);
    font-size: var(--text-xs);
    line-height: 14px;
    color: var(--outline);
}

.typing-dots {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

.typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--primary);
    animation: typing 1.2s ease-in-out infinite;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; background: var(--secondary); }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; background: var(--tertiary); }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-4px); opacity: 1; }
}

@keyframes msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ---------- Questions rapides ---------- */
.chat-presets {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 0 var(--space-md) var(--space-sm);
}

.chat-presets-label {
    font-family: var(--font-label);
    font-size: var(--text-xs);
    line-height: 14px;
    color: var(--outline);
}

.chat-presets-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.chat-preset {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border-radius: 999px;
    background: var(--surface-high);
    font-family: var(--font-label);
    font-size: var(--text-xs);
    line-height: 16px;
    transition: background-color 0.2s, color 0.2s;
}

.chat-preset:not([class*="text-"]) { color: var(--on-surface-variant); }
.chat-preset:hover { background: var(--surface-highest); }

/* ---------- Saisie ---------- */
.chat-form {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    padding: var(--space-sm);
    background: var(--surface-low);
    border-top: 1px solid rgba(72, 72, 72, 0.4);
}

.chat-input-wrap {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: 6px 6px 6px 12px;
    border-radius: var(--radius-md);
    background: var(--surface-lowest);
    border: 1px solid rgba(72, 72, 72, 0.6);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.chat-input-wrap:focus-within {
    border-color: rgba(255, 255, 255, 0.6);
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.12);
}

#chatbot-input {
    flex: 1;
    min-width: 0;
    padding: 4px 0;
    background: transparent;
    border: 0;
    outline: none;
    font-size: var(--text-sm);
    color: var(--on-surface);
}

#chatbot-input::placeholder { color: var(--outline); }

.chat-send {
    width: 32px;
    height: 32px;
    border-radius: var(--radius);
    background: var(--primary-container);
    color: var(--on-primary-container);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: opacity 0.2s, transform 0.15s, box-shadow 0.2s;
}

.chat-send:hover { box-shadow: 0 0 14px rgba(255, 255, 255, 0.5); }
.chat-send:active { transform: scale(0.94); }

.chat-form-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-sm);
    padding: 0 4px;
    font-family: var(--font-label);
    font-size: var(--text-xs);
    line-height: 14px;
    color: var(--outline);
}

.chat-form-meta .text-tertiary {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    text-align: right;
}

/* ============================================
   ACCÈS DEPUIS TOUTE LA PAGE
   Lanceur flottant → le même panneau est déplacé dans .chat-dock
   ============================================ */
.chat-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-md);
    padding: var(--space-xl);
    border-radius: var(--radius-xl);
    border: 1px dashed var(--outline-variant);
    background: rgba(26, 27, 33, 0.5);
    color: var(--on-surface-variant);
    text-align: center;
}

.chat-placeholder .chat-avatar { width: 40px; height: 40px; }
.chat-placeholder p { max-width: 22ch; }

/* ---------- Lanceur ---------- */
.chat-launcher {
    position: fixed;
    right: max(var(--space-lg), env(safe-area-inset-right));
    bottom: max(var(--space-lg), env(safe-area-inset-bottom));
    z-index: 55;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 18px 6px 6px;
    border-radius: 999px;
    background: var(--surface-container);
    border: 1px solid rgba(255, 255, 255, 0.35);
    box-shadow: 0 14px 32px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.35);
    font-family: var(--font-body);
    font-size: var(--text-sm);
    line-height: 20px;
    font-weight: 600;
    color: var(--on-surface);
    white-space: nowrap;
    /* Masqué tant que le panneau de l'accueil est à l'écran. */
    opacity: 0;
    visibility: hidden;
    transform: translateY(16px) scale(0.94);
    transition:
        opacity 0.25s ease,
        transform 0.5s cubic-bezier(0.16, 1, 0.3, 1),
        visibility 0s linear 0.5s,
        border-color 0.2s,
        box-shadow 0.2s;
}

.chat-launcher.is-visible {
    opacity: 1;
    visibility: visible;
    transform: none;
    transition-delay: 0s;
}

.chat-launcher:hover {
    border-color: rgba(255, 255, 255, 0.75);
    box-shadow: 0 14px 32px rgba(0, 0, 0, 0.5), 0 6px 22px rgba(255, 255, 255, 0.25);
}

.chat-launcher:active { transform: scale(0.97); }
.chat-launcher:focus-visible { border-radius: 999px; outline-offset: 3px; }

.chat-launcher-avatar {
    position: relative;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--primary-container);
    color: var(--on-primary-container);
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-launcher-avatar { border: 1px solid rgba(242, 242, 242, 0.35); }

/* Pastille « en ligne », reprise de l'en-tête du panneau. */
.chat-launcher-avatar::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--tertiary);
    box-shadow: 0 0 0 2px var(--surface-container);
}

.chat-launcher-verb { color: var(--on-surface-variant); font-weight: 500; }

/* ---------- Fenêtre flottante ---------- */
.chat-dock {
    position: fixed;
    right: max(var(--space-lg), env(safe-area-inset-right));
    bottom: max(var(--space-lg), env(safe-area-inset-bottom));
    z-index: 60;
    display: flex;
    flex-direction: column;
    width: min(400px, calc(100vw - 2 * var(--space-md)));
    height: min(640px, calc(100dvh - var(--nav-height) - 2 * var(--space-lg)));
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px) scale(0.96);
    transform-origin: bottom right;
    transition:
        opacity 0.2s ease,
        transform 0.45s cubic-bezier(0.16, 1, 0.3, 1),
        visibility 0s linear 0.45s;
}

.chat-dock.is-open {
    opacity: 1;
    visibility: visible;
    transform: none;
    transition-delay: 0s;
}

.chat-dock .chat-panel {
    flex: 1;
    min-height: 0;
    border-color: rgba(255, 255, 255, 0.3);
    box-shadow: 0 28px 64px rgba(0, 0, 0, 0.6), 0 6px 16px rgba(0, 0, 0, 0.35);
}

.chat-dock .chat-messages {
    flex: 1;
    height: auto;
    min-height: 0;
}

.chat-dock .chat-panel:focus { outline: none; }

/* La fenêtre est plus étroite que l'accueil : le badge décoratif cède sa place au bouton Fermer. */

/* Le voile n'existe que sur mobile, où la fenêtre devient un panneau plein écran. */
.chat-scrim { display: none; }

@media (max-width: 639px) {
    .chat-launcher {
        right: max(var(--space-md), env(safe-area-inset-right));
        bottom: max(var(--space-md), env(safe-area-inset-bottom));
        padding-right: 16px;
    }

    .chat-launcher-verb { display: none; }

    .chat-dock {
        left: 0;
        right: 0;
        bottom: 0;
        width: auto;
        height: min(88dvh, calc(100dvh - var(--nav-height)));
        transform: translateY(100%);
        transform-origin: bottom center;
        transition:
            opacity 0.2s ease,
            transform 0.45s cubic-bezier(0.16, 1, 0.3, 1),
            visibility 0s linear 0.45s;
    }

    .chat-dock .chat-panel {
        border-radius: var(--radius-xl) var(--radius-xl) 0 0;
        border-bottom: 0;
        padding-bottom: env(safe-area-inset-bottom);
    }

    .chat-scrim {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 58;
        background: rgba(10, 10, 10, 0.7);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0s linear 0.3s;
    }

    .chat-scrim.is-open {
        opacity: 1;
        visibility: visible;
        transition-delay: 0s;
    }

    /* La page ne défile plus derrière le panneau ouvert. */
    html.chat-dock-open { overflow: hidden; }

    /* Le lanceur ne doit pas masquer la dernière ligne du pied de page. */
    #footer { padding-bottom: calc(var(--space-xl) + 56px); }
}

@media (prefers-reduced-motion: reduce) {
    .chat-launcher, .chat-dock, .chat-scrim { transition-duration: 0s; transition-delay: 0s; }
    .chat-launcher, .chat-dock { transform: none; }
}
