/* ════════════════════════════════════════════════════════════════
   chatbot.css — Widget assistant IA dethie.fr
   Bouton flottant + panneau overlay + bulles de conversation.
   Tokens du design system → dark mode automatique. Mobile-first.
   ════════════════════════════════════════════════════════════════ */

.cbot {
    --cbot-accent: var(--accent, #00D4AA);
    --cbot-accent-deep: var(--accent-deep, #007A63);
    --cbot-surface: var(--bg-surface, #fff);
    --cbot-bubble-bot: var(--bg-secondary, #F4F6F9);
    --cbot-text: var(--text-primary, #0A1628);
    --cbot-text-soft: var(--text-secondary, #475569);
    --cbot-border: var(--border, #E2E8F0);
    position: fixed;
    right: clamp(1rem, 3vw, 1.5rem);
    bottom: clamp(1rem, 3vw, 1.5rem);
    z-index: 9000;
    font-family: var(--font-body, system-ui, sans-serif);
}

/* ── Bouton flottant ─────────────────────────────────────────── */
.cbot__toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 60px;
    height: 60px;
    border: none;
    border-radius: var(--radius-full, 9999px);
    background: linear-gradient(135deg, var(--primary, #0A1628) 0%, var(--primary-soft, #1E3A5F) 100%);
    color: var(--cbot-accent);
    cursor: pointer;
    box-shadow: 0 10px 30px -8px rgba(10, 22, 40, 0.45);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.cbot__toggle:hover { transform: translateY(-2px) scale(1.04); }
.cbot__toggle:focus-visible {
    outline: 3px solid var(--cbot-accent);
    outline-offset: 3px;
}
.cbot__toggle svg { width: 28px; height: 28px; }
.cbot__toggle-close { display: none; }
.cbot.is-open .cbot__toggle-open { display: none; }
.cbot.is-open .cbot__toggle-close { display: block; }

/* ── Panneau ─────────────────────────────────────────────────── */
.cbot__panel {
    position: absolute;
    right: 0;
    bottom: calc(60px + 0.85rem);
    width: min(380px, calc(100vw - 2rem));
    height: min(560px, calc(100vh - 7rem));
    display: flex;
    flex-direction: column;
    background: var(--cbot-surface);
    border: 1px solid var(--cbot-border);
    border-radius: 18px;
    box-shadow: 0 24px 60px -20px rgba(10, 22, 40, 0.4);
    overflow: hidden;
    opacity: 0;
    transform: translateY(12px) scale(0.98);
    transform-origin: bottom right;
    pointer-events: none;
    transition: opacity 0.22s ease, transform 0.22s ease;
}
.cbot.is-open .cbot__panel {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ── Header ──────────────────────────────────────────────────── */
.cbot__header {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    padding: 0.95rem 1.1rem;
    background: linear-gradient(135deg, var(--primary, #0A1628) 0%, var(--primary-soft, #1E3A5F) 100%);
    color: #fff;
}
.cbot__avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: var(--radius-full, 9999px);
    background: rgba(0, 212, 170, 0.2);
    color: var(--cbot-accent);
    flex-shrink: 0;
}
.cbot__avatar svg { width: 22px; height: 22px; }
.cbot__title { font-size: 0.95rem; font-weight: 600; margin: 0; line-height: 1.2; }
.cbot__subtitle { font-size: 0.75rem; margin: 0.1rem 0 0; opacity: 0.8; }

/* Bouton fermer (X) — toujours visible dans le header, cible tactile ≥44px */
.cbot__close {
    margin-left: auto;          /* poussé à droite du header */
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: none;
    border-radius: var(--radius-full, 9999px);
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    cursor: pointer;
    transition: background 0.2s ease;
    -webkit-tap-highlight-color: transparent;
}
.cbot__close:hover { background: rgba(255, 255, 255, 0.24); }
.cbot__close:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
}
.cbot__close svg { width: 22px; height: 22px; }

/* ── Fil de messages ─────────────────────────────────────────── */
.cbot__log {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
    background: var(--bg-primary, #FAFBFC);
}
.cbot__msg {
    max-width: 84%;
    padding: 0.65rem 0.85rem;
    border-radius: 14px;
    font-size: 0.875rem;
    line-height: 1.5;
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}
.cbot__msg a { color: var(--cbot-accent-deep); font-weight: 600; }

/* ── Rendu markdown dans les bulles ──────────────────────────── */
.cbot__msg p { margin: 0 0 0.5rem; }
.cbot__msg > :last-child { margin-bottom: 0; }
.cbot__msg h3 { font-size: 0.95rem; font-weight: 700; margin: 0.5rem 0 0.3rem; }
.cbot__msg h4 { font-size: 0.875rem; font-weight: 700; margin: 0.5rem 0 0.3rem; }
.cbot__msg ul,
.cbot__msg ol { margin: 0.3rem 0; padding-left: 1.2rem; }
.cbot__msg li { margin: 0.15rem 0; }
.cbot__msg strong { font-weight: 700; }
.cbot__msg em { font-style: italic; }
.cbot__msg code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.82em;
    background: rgba(0, 0, 0, 0.06);
    padding: 0.05em 0.35em;
    border-radius: 5px;
}
.cbot__msg--user code { background: rgba(255, 255, 255, 0.22); }
.cbot__table {
    border-collapse: collapse;
    width: 100%;
    margin: 0.45rem 0;
    font-size: 0.8rem;
}
.cbot__table th,
.cbot__table td {
    border: 1px solid var(--cbot-border);
    padding: 0.3rem 0.45rem;
    text-align: left;
    vertical-align: top;
}
.cbot__table th { background: rgba(0, 0, 0, 0.04); font-weight: 700; }

/* Curseur clignotant pendant le streaming (effet de frappe) */
.cbot__msg--streaming::after {
    content: "";
    display: inline-block;
    width: 2px;
    height: 1em;
    margin-left: 2px;
    vertical-align: -0.15em;
    background: var(--cbot-accent-deep);
    animation: cbot-caret 1s steps(2, start) infinite;
}
@keyframes cbot-caret { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }
.cbot__msg--bot {
    align-self: flex-start;
    background: var(--cbot-bubble-bot);
    color: var(--cbot-text);
    border-bottom-left-radius: 4px;
}
.cbot__msg--user {
    align-self: flex-end;
    background: var(--cbot-accent-deep);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.cbot__msg--user a { color: #fff; text-decoration: underline; }

/* Indicateur de saisie */
.cbot__typing {
    align-self: flex-start;
    display: inline-flex;
    gap: 4px;
    padding: 0.75rem 0.9rem;
    background: var(--cbot-bubble-bot);
    border-radius: 14px;
    border-bottom-left-radius: 4px;
}
.cbot__typing span {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--cbot-text-soft);
    animation: cbot-bounce 1.2s infinite ease-in-out;
}
.cbot__typing span:nth-child(2) { animation-delay: 0.15s; }
.cbot__typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes cbot-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* ── Zone de saisie ──────────────────────────────────────────── */
.cbot__form {
    display: flex;
    gap: 0.5rem;
    padding: 0.7rem;
    border-top: 1px solid var(--cbot-border);
    background: var(--cbot-surface);
}
.cbot__input {
    flex: 1;
    resize: none;
    border: 1px solid var(--cbot-border);
    border-radius: 12px;
    padding: 0.6rem 0.75rem;
    font: inherit;
    font-size: 0.875rem;
    line-height: 1.4;
    max-height: 90px;
    color: var(--cbot-text);
    background: var(--bg-primary, #FAFBFC);
}
.cbot__input:focus-visible {
    outline: none;
    border-color: var(--cbot-accent);
    box-shadow: 0 0 0 3px rgba(0, 212, 170, 0.18);
}
.cbot__send {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    flex-shrink: 0;
    border: none;
    border-radius: 12px;
    background: var(--cbot-accent-deep);
    color: #fff;
    cursor: pointer;
    transition: background 0.2s ease, opacity 0.2s ease;
}
.cbot__send:hover { background: var(--cbot-accent); }
.cbot__send:disabled { opacity: 0.45; cursor: not-allowed; }
.cbot__send svg { width: 20px; height: 20px; }

/* Saisie désactivée (limite atteinte) — grisée */
.cbot__input:disabled {
    opacity: 0.55;
    cursor: not-allowed;
    background: var(--cbot-bubble-bot, rgba(0, 0, 0, 0.04));
}

/* Compteur discret de messages restants */
.cbot__counter {
    padding: 0.4rem 0.7rem 0;
    margin: 0;
    font-size: 0.6875rem;
    text-align: center;
    color: var(--cbot-text-soft);
    background: var(--cbot-surface);
}
.cbot__counter[hidden] { display: none; }

/* Bloc + CTA affichés quand la limite est atteinte */
.cbot__msg--limit { font-weight: 500; }
.cbot__cta {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    margin-top: 0.6rem;
    padding: 0.5rem 0.9rem;
    border-radius: 10px;
    background: var(--cbot-accent-deep);
    color: #fff !important;
    font-size: 0.8125rem;
    font-weight: 600;
    text-decoration: none;
    transition: background 0.2s ease, transform 0.15s ease;
}
.cbot__cta:hover { background: var(--cbot-accent); transform: translateY(-1px); }
.cbot__cta svg { width: 16px; height: 16px; }

.cbot__footnote {
    padding: 0.4rem 0.7rem 0.55rem;
    margin: 0;
    font-size: 0.6875rem;
    text-align: center;
    color: var(--cbot-text-soft);
    background: var(--cbot-surface);
}
.cbot__footnote--limit { color: var(--warning-text, #B45309); font-weight: 600; }

/* ── Mobile ──────────────────────────────────────────────────── */
/* ── Mobile : cohabitation avec la barre CTA ─────────────────── */
@media (max-width: 767px) {
    /* Bouton flottant remonté au-dessus de la barre CTA (anti-superposition,
       même cote que .back-to-top / .audio-controls). */
    .cbot { bottom: 5rem; }
    /* Quand la barre CTA mobile est visible et le chat fermé, le bouton
       flottant s'efface : c'est le bouton "Chat" de la barre qui prend le
       relais. Dès que le chat est ouvert (.is-open), il réapparaît (croix de
       fermeture). Hors barre (haut de page, footer, page contact) le bouton
       reste disponible. */
    body.mobile-cta-visible .cbot:not(.is-open) { display: none; }
}

@media (max-width: 480px) {
    .cbot__panel {
        position: fixed;
        right: 0; left: 0; bottom: 0; top: 0;
        width: 100vw;
        height: 100dvh;
        border-radius: 0;
        border: none;
    }
    /* Header + zone de saisie fixes ; seul le fil de messages scrolle.
       Empêche tout débordement hors écran en plein écran. */
    .cbot__header,
    .cbot__form,
    .cbot__counter,
    .cbot__footnote { flex-shrink: 0; }
    /* Plein écran : le X du header gère la fermeture. Le bouton flottant
       (qui flotterait par-dessus le fil) est masqué quand le chat est ouvert. */
    .cbot.is-open .cbot__toggle { display: none; }
}

/* ── Reduced motion ──────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    .cbot__toggle,
    .cbot__panel { transition: none; }
    .cbot__toggle:hover { transform: none; }
    .cbot__typing span { animation: none; }
}
