* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Segoe UI', Arial, sans-serif;
    background: linear-gradient(160deg, #eaf4ff 0%, #ffffff 45%, #dcefff 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

/* Léger halo décoratif façon "feuille quadrillée" en fond */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image:
        linear-gradient(rgba(66, 165, 245, 0.06) 1px, transparent 1px),
        linear-gradient(90deg, rgba(66, 165, 245, 0.06) 1px, transparent 1px);
    background-size: 28px 28px;
    z-index: -1;
}

.container {
    background: rgba(255, 255, 255, 0.94);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(33, 150, 243, 0.18);
    width: 100%;
    max-width: 900px;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
    border: 2px solid rgba(66, 165, 245, 0.25);
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
}

/* En-tête */
.header {
    background: linear-gradient(135deg, #4ca6ff 0%, #bbdefb 50%, #73b9ff 100%);
    padding: 20px 30px;
    border-bottom: 3px solid #64b5f6;
    display: flex;
    align-items: center;
    gap: 15px;
    flex-shrink: 0;
    /* La bulle "Connaître la vraie Alex ?" déborde volontairement sous le
       header, par-dessus la zone de chat. On fixe explicitement un
       stacking context avec un z-index plus élevé que .chat-wrapper pour
       garantir qu'elle reste TOUJOURS visible par-dessus (certains
       navigateurs empilent sinon les enfants flex dans l'ordre du DOM,
       ce qui peut faire passer .chat-wrapper au-dessus si son fond est
       opaque, comme en mode Test). */
    position: relative;
    z-index: 5;
}

/* Conteneur externe : englobe le rond de l'avatar (overflow:hidden) ET la
   bulle "Connaître la vraie Alex ?" (qui ne doit surtout pas être coupée
   par l'arrondi de l'avatar). */
.header .avatar-container {
    position: relative;
    flex-shrink: 0;
}

.header .avatar {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    border: 3px solid #ffffff;
    box-shadow: 0 0 25px rgba(33, 150, 243, 0.3);
    background: #bfdfff;
    position: relative;
    cursor: pointer;
    transition: transform 0.2s ease;
}

.header .avatar:hover {
    transform: scale(1.03);
}

/* Bulle "Connaître la vraie Alex ?" affichée au clic sur l'avatar */
.avatar-popover {
    position: absolute;
    top: calc(100% + 12px);
    left: 0;
    min-width: 210px;
    background: rgba(255, 255, 255, 0.82);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(100, 181, 246, 0.55);
    border-radius: 14px;
    padding: 12px 16px;
    box-shadow: 0 10px 28px rgba(33, 150, 243, 0.25);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-6px);
    transition: opacity 0.25s ease, transform 0.25s ease, visibility 0.25s;
    z-index: 30;
}

.avatar-popover.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.avatar-popover::before {
    content: '';
    position: absolute;
    top: -7px;
    left: 24px;
    width: 12px;
    height: 12px;
    background: rgba(255, 255, 255, 0.82);
    border-left: 1px solid rgba(100, 181, 246, 0.55);
    border-top: 1px solid rgba(100, 181, 246, 0.55);
    transform: rotate(45deg);
}

.avatar-popover p {
    margin: 0 0 6px 0;
    font-size: 14px;
    font-weight: 700;
    color: #000040;
}

.avatar-popover a {
    font-size: 12.5px;
    color: #1976d2;
    font-weight: 600;
    text-decoration: underline;
    word-break: break-all;
}

.header .avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.header .avatar .avatar-layer {
    transition: opacity 0.4s ease-in-out;
    position: absolute;
    top: 0;
    left: 0;
}

.header .info {
    flex: 1;
    min-width: 0;
    position: relative; /* ancre pour le typing-indicator positionné en absolu */
}

.header .info h1 {
    color: #000040;
    font-size: 22px;
    margin: 0;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.header .info h1 span {
    color: #1976d2;
}

.header .info .status {
    color: #131304;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.header .info .status .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #fffff1;
    display: inline-block;
    animation: pulseDot 2s ease-in-out infinite;
}

@keyframes pulseDot {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.3; transform: scale(0.8); }
}

.header .badge {
    background: #1976d2;
    color: #bfdfff;
    padding: 4px 14px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border: 1px solid #64b5f6;
    flex-shrink: 0;
}

/* Enveloppe de la zone de chat : permet de positionner le bouton
   "revenir en bas" par-dessus la zone de messages, en bas à droite. */
.chat-wrapper {
    position: relative;
    z-index: 1;
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* Zone des messages */
.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px 25px;
    background: #f4faff;
    scroll-behavior: smooth;
    min-height: 0;
}

/* Bouton "revenir aux derniers messages" (apparaît/disparaît en fondu) */
.scroll-to-bottom-btn {
    position: absolute;
    right: 22px;
    bottom: 18px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid #90caf9;
    background: #ffffff;
    color: #1976d2;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(33, 150, 243, 0.28);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateY(8px);
    transition: opacity 0.25s ease, transform 0.25s ease, visibility 0.25s;
    z-index: 6;
}

.scroll-to-bottom-btn.visible {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateY(0);
}

.scroll-to-bottom-btn:hover {
    background: #eaf4ff;
    border-color: #64b5f6;
}

@media (max-width: 600px) {
    .scroll-to-bottom-btn {
        right: 14px;
        bottom: 14px;
        width: 40px;
        height: 40px;
        font-size: 20px;
    }
}

.chat-area::-webkit-scrollbar {
    width: 6px;
}

.chat-area::-webkit-scrollbar-track {
    background: #eaf4ff;
}

.chat-area::-webkit-scrollbar-thumb {
    background: #90caf9;
    border-radius: 3px;
}

.chat-area::-webkit-scrollbar-thumb:hover {
    background: #64b5f6;
}

/* Messages */
.message {
    display: flex;
    margin-bottom: 18px;
    animation: fadeIn 0.4s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(12px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.message.user {
    justify-content: flex-end;
}

.message.bot {
    justify-content: flex-start;
}

.message .bubble {
    max-width: 75%;
    padding: 13px 19px;
    border-radius: 18px;
    font-size: 17px;
    line-height: 1.65;
    word-wrap: break-word;
    white-space: pre-wrap;
    position: relative;
}

.message.user .bubble {
    background: linear-gradient(135deg, #42a5f5, #1e88e5);
    color: #ffffff;
    border-bottom-right-radius: 4px;
    border: 1px solid #1976d2;
}

.message.bot .bubble {
    background: #ffffff;
    color: #000040;
    border-bottom-left-radius: 4px;
    border: 1px solid #90caf9;
    box-shadow: 0 2px 8px rgba(33, 150, 243, 0.12);
    white-space: normal;
}

.message .timestamp {
    font-size: 11px;
    color: #002040;
    margin-top: 4px;
    padding: 0 4px;
}

.message.user .timestamp {
    text-align: right;
    color: #64b5f6;
}

/* Indicateur de frappe : ancré au bord bas de .info via bottom:0 + transform
   (et non un "top: 100%" en pourcentage). Certains navigateurs mobiles
   recalculent la hauteur d'un parent "auto" quand un enfant absolu utilise
   un top en %, ce qui décalait statusText à l'apparition/disparition des
   points. bottom:0 + translateY ne dépend jamais de la hauteur du parent :
   la position ne peut donc plus bouger. Peut déborder sur le chat-area,
   c'est voulu. */
.typing-indicator {
    display: none;
    position: absolute;
    bottom: 0;
    left: 0;
    transform: translateY(calc(100% + 8px));
    z-index: 20;
}

.typing-indicator .bubble {
    background: #ffffff;
    border: 1px solid #90caf9;
    border-radius: 18px;
    padding: 10px 18px;
    display: inline-block;
    border-bottom-left-radius: 4px;
    box-shadow: 0 4px 14px rgba(33, 150, 243, 0.25);
}

.typing-indicator .dots {
    display: flex;
    gap: 8px;
    align-items: center;
}

.typing-indicator .dots span {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #42a5f5;
    display: inline-block;
    animation: typingBounce 1.4s ease-in-out infinite;
}

.typing-indicator .dots span:nth-child(1) { animation-delay: 0s; }
.typing-indicator .dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator .dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 60%, 100% {
        transform: translateY(0) scale(0.8);
        opacity: 0.4;
    }
    30% {
        transform: translateY(-10px) scale(1.2);
        opacity: 1;
        background: #90caf9;
    }
}

/* Variante mode Test : même rebond, mais sans repasser au bleu au sommet
   (utilisée à la place de typingBounce quand body.theme-hot est actif). */
@keyframes typingBounceHot {
    0%, 60%, 100% {
        transform: translateY(0) scale(0.8);
        opacity: 0.4;
    }
    30% {
        transform: translateY(-10px) scale(1.2);
        opacity: 1;
        background: #ff5252;
    }
}

/* Zone de saisie */
.input-area {
    padding: 15px 20px 20px;
    background: linear-gradient(135deg, #73b9ff 0%, #bbdefb 50%, #90caf9 100%);
    border-top: 2px solid #90caf9;
    display: flex;
    gap: 12px;
    align-items: flex-end;
    flex-shrink: 0;
}

.input-area textarea {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid #90caf9;
    border-radius: 12px;
    background: #ffffff;
    color: #000040;
    font-size: 16px;
    font-family: inherit;
    line-height: 1.5;
    /* Hauteur fixe = exactement 2 lignes visibles (12px de padding haut/bas
       + 2 × 24px de ligne). Pas de poignée de redimensionnement : le
       texte plus long défile verticalement DANS le champ (overflow-y). */
    height: 76px;
    min-height: 76px;
    max-height: 76px;
    resize: none;
    overflow-y: auto;
    transition: border-color 0.3s, box-shadow 0.3s;
}

.input-area textarea:focus {
    outline: none;
    border-color: #42a5f5;
    box-shadow: 0 0 20px rgba(66, 165, 245, 0.2);
}

.input-area textarea::placeholder {
    color: #686859;
}

.input-area button {
    background: linear-gradient(135deg, #42a5f5, #1e88e5);
    color: #ffffff;
    border: 2px solid #64b5f6;
    padding: 12px 24px;
    border-radius: 12px;
    font-size: 18px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
    min-height: 50px;
    min-width: 70px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.input-area button:hover:not(:disabled) {
    transform: scale(1.05);
    background: linear-gradient(135deg, #64b5f6, #1e88e5);
    box-shadow: 0 4px 25px rgba(66, 165, 245, 0.4);
    border-color: #90caf9;
}

.input-area button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

/* Bouton reset */
.reset-btn {
    background: #ffffff;
    color: #131304;
    font-family: 'Segoe UI', Arial, sans-serif;
    border: 1px solid #90caf9;
    padding: 6px 14px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.3s;
    margin-left: 10px;
}

.reset-btn:hover {
    border-color: #42a5f5;
    color: #ffffff;
    background: #4ca6ff;
}

/* Message de bienvenue */
.welcome-msg {
    text-align: center;
    color: #464637;
    padding: 40px 20px;
    font-size: 15px;
    line-height: 1.8;
}

.welcome-msg .alex-icon {
    font-size: 48px;
    display: block;
    margin-bottom: 10px;
}

.welcome-msg .sub {
    font-size: 13px;
    color: #3783ba;
}

/* Sélecteur de modèle IA (discret) */
.model-switcher {
    position: fixed;
    bottom: 16px;
    right: 16px;
    z-index: 10;
}

.model-switcher-btn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: 1px solid #90caf9;
    background: #ffffff;
    color: #131304;
    font-size: 16px;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(33, 150, 243, 0.2);
}

.model-switcher-menu {
    display: none;
    position: absolute;
    bottom: 46px;
    right: 0;
    background: #ffffff;
    border: 1px solid #90caf9;
    border-radius: 10px;
    padding: 10px;
    width: 230px;
    box-shadow: 0 6px 24px rgba(33, 150, 243, 0.2);
}

.model-switcher-menu.open {
    display: block;
}

.model-switcher-title {
    font-size: 12px;
    font-weight: bold;
    color: #000040;
    margin-bottom: 8px;
}

.model-option {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: #131304;
    padding: 4px 0;
    cursor: pointer;
}

.model-option input {
    accent-color: #42a5f5;
    cursor: pointer;
}

/* Responsive : sur mobile, l'app prend TOUTE la fenêtre, comme une vraie appli de chat */
@media (max-width: 600px) {
    html, body {
        height: 100%;
        overflow: hidden;
        position: fixed;
        inset: 0;
        width: 100%;
        overscroll-behavior: none;
    }

    body {
        margin: 0;
        padding: 0;
        display: block;
        background: #eaf4ff;
    }

    /* Pas de quadrillage visible en fond sur mobile */
    body::before {
        display: none;
    }

    .container {
        max-width: 100%;
        margin: 0;
        border-radius: 0;
        border: none;
        box-shadow: none;
        width: 100%;
        height: 100vh;
        height: 100dvh; /* tient compte des barres d'adresse mobiles */
        max-height: 100vh;
        max-height: 100dvh;
    }

    .header {
        padding: 12px 16px;
        flex-wrap: nowrap;
        flex-shrink: 0;
    }

    .header .info h1 {
        font-size: 17px;
    }

    .header .avatar {
        width: 100px;
        height: 100px;
    }

    /* Bouton "Nouvelle discussion" retiré sur mobile pour gagner de la place */
    .reset-btn {
        display: none;
    }

    .chat-area {
        flex: 1;
        height: auto;
        min-height: 0;
        padding: 14px;
        overscroll-behavior-y: contain;
    }

    .message .bubble {
        max-width: 85%;
        font-size: 15.5px;
        padding: 10px 14px;
    }

    .input-area {
        flex-shrink: 0;
        padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
        margin: 0;
    }

    .input-area textarea {
        font-size: 16px; /* évite le zoom auto de Safari iOS sur un champ < 16px */
    }

    /* Le sélecteur de modèle IA reste discret : caché sur mobile */
    .model-switcher {
        display: none;
    }
}

/* Ajustements visuels pour le contenu rendu (markdown + KaTeX) dans les bulles */
.message.bot .bubble a {
    color: #131304;
    font-weight: 600;
    text-decoration: underline;
    word-break: break-word;
}
.message.bot .bubble p {
    margin: 0 0 8px 0;
}
.message.bot .bubble p:last-child {
    margin-bottom: 0;
}
.message.bot .bubble ul,
.message.bot .bubble ol {
    margin: 6px 0 6px 22px;
}
.message.bot .bubble code {
    background: #bfdfff;
    color: #000040;
    padding: 1px 5px;
    border-radius: 5px;
    font-size: 0.92em;
}
.message.bot .bubble .katex-display {
    margin: 10px 0;
    overflow-x: auto;
    overflow-y: hidden;
}
.message.bot .bubble .katex {
    font-size: 1.05em;
}

/* ============================================================
   MODE TEST — ambiance rouge + noir ("hot hot"), activée en JS via
   document.body.classList.toggle('theme-hot', ...) dans app.js.
   Toutes les règles ci-dessous surchargent uniquement les couleurs ;
   la mise en page reste identique au mode normal.
   ============================================================ */
body.theme-hot {
    background: linear-gradient(160deg, #1a0000 0%, #050000 45%, #200000 100%);
}

body.theme-hot::before {
    background-image:
        linear-gradient(rgba(255, 23, 23, 0.08) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 23, 23, 0.08) 1px, transparent 1px);
}

body.theme-hot .container {
    background: rgba(15, 0, 0, 0.94);
    box-shadow: 0 20px 60px rgba(255, 23, 23, 0.28);
    border: 2px solid rgba(255, 23, 23, 0.35);
}

body.theme-hot .header {
    background: linear-gradient(135deg, #3b0000 0%, #8e0000 50%, #1a0000 100%);
    border-bottom: 3px solid #b71c1c;
}

body.theme-hot .header .avatar {
    border: 3px solid #2b0000;
    box-shadow: 0 0 25px rgba(255, 23, 23, 0.45);
    background: #2b0000;
}

body.theme-hot .avatar-popover {
    background: rgba(20, 0, 0, 0.88);
    border: 1px solid rgba(255, 23, 23, 0.55);
    box-shadow: 0 10px 28px rgba(255, 23, 23, 0.3);
}

body.theme-hot .avatar-popover::before {
    background: rgba(20, 0, 0, 0.88);
    border-left: 1px solid rgba(255, 23, 23, 0.55);
    border-top: 1px solid rgba(255, 23, 23, 0.55);
}

body.theme-hot .avatar-popover p {
    color: #ffdada;
}

body.theme-hot .avatar-popover a {
    color: #ff5252;
}

body.theme-hot .header .info h1 {
    color: #ffe5e5;
}

body.theme-hot .header .info h1 span {
    color: #ff5252;
}

body.theme-hot .header .info .status {
    color: #ffcccc;
}

body.theme-hot .header .info .status .dot {
    background: #ff1744;
}

body.theme-hot .header .badge {
    background: #8e0000;
    color: #ffcccc;
    border: 1px solid #b71c1c;
}

body.theme-hot .chat-area {
    background: #0d0000;
}

body.theme-hot .chat-area::-webkit-scrollbar-track {
    background: #1a0000;
}

body.theme-hot .chat-area::-webkit-scrollbar-thumb {
    background: #8e0000;
}

body.theme-hot .chat-area::-webkit-scrollbar-thumb:hover {
    background: #b71c1c;
}

body.theme-hot .scroll-to-bottom-btn {
    background: #1a0000;
    color: #ff5252;
    border: 1px solid #8e0000;
    box-shadow: 0 4px 16px rgba(255, 23, 23, 0.3);
}

body.theme-hot .scroll-to-bottom-btn:hover {
    background: #2b0000;
    border-color: #b71c1c;
}

body.theme-hot .message.user .bubble {
    background: linear-gradient(135deg, #b71c1c, #4a0000);
    color: #ffffff;
    border: 1px solid #8e0000;
}

body.theme-hot .message.bot .bubble {
    background: #170202;
    color: #ffdada;
    border: 1px solid #8e0000;
    box-shadow: 0 2px 8px rgba(255, 23, 23, 0.18);
}

body.theme-hot .message.bot .bubble a {
    color: #ff8a80;
}

body.theme-hot .message.bot .bubble code {
    background: #3b0000;
    color: #ffdada;
}

body.theme-hot .message .timestamp {
    color: #ff8a80;
}

body.theme-hot .message.user .timestamp {
    color: #ff5252;
}

body.theme-hot .typing-indicator .bubble {
    background: #170202;
    border: 1px solid #8e0000;
    box-shadow: 0 4px 14px rgba(255, 23, 23, 0.3);
}

body.theme-hot .typing-indicator .dots span {
    background: #b71c1c;
    animation-name: typingBounceHot;
}

body.theme-hot .typing-indicator .dots span:nth-child(2) {
    background: #ff1744;
}

body.theme-hot .input-area {
    background: linear-gradient(135deg, #3b0000 0%, #8e0000 50%, #1a0000 100%);
    border-top: 2px solid #8e0000;
}

body.theme-hot .input-area textarea {
    background: #170202;
    color: #ffdada;
    border: 2px solid #8e0000;
}

body.theme-hot .input-area textarea:focus {
    border-color: #ff1744;
    box-shadow: 0 0 20px rgba(255, 23, 23, 0.35);
}

body.theme-hot .input-area textarea::placeholder {
    color: #a05050;
}

body.theme-hot .input-area button {
    background: linear-gradient(135deg, #b71c1c, #4a0000);
    border: 2px solid #8e0000;
    color: #ffffff;
}

body.theme-hot .input-area button:hover:not(:disabled) {
    background: linear-gradient(135deg, #ff1744, #8e0000);
    box-shadow: 0 4px 25px rgba(255, 23, 23, 0.5);
    border-color: #b71c1c;
}

body.theme-hot .reset-btn {
    background: #170202;
    color: #ffdada;
    border: 1px solid #8e0000;
}

body.theme-hot .reset-btn:hover {
    border-color: #ff1744;
    color: #ffffff;
    background: #8e0000;
}

body.theme-hot .welcome-msg {
    color: #ffcccc;
}

body.theme-hot .welcome-msg .sub {
    color: #ff8a80;
}

body.theme-hot .model-switcher-btn {
    background: #170202;
    color: #ffdada;
    border: 1px solid #8e0000;
}

body.theme-hot .model-switcher-menu {
    background: #170202;
    border: 1px solid #8e0000;
}

body.theme-hot .model-switcher-title {
    color: #ffdada;
}

body.theme-hot .model-option {
    color: #ffcccc;
}

body.theme-hot .model-option input {
    accent-color: #ff1744;
}

@media (max-width: 600px) {
    body.theme-hot {
        background: #0d0000;
    }
}