/* ============= RESET & BASE ============= */
/* Reset global : supprime les marges par défaut et utilise border-box pour un sizing cohérent */
* { margin: 0; padding: 0; box-sizing: border-box; }

/* Variables globales de design : rayons, largeur sidebar et durées de transition */
:root {
    --radius:          8px;
    --radius-lg:       12px;
    --radius-xl:       16px;
    --sidebar-width:   272px;
    --transition-fast: 150ms ease;
    --transition-base: 220ms ease;
    --transition-slow: 350ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── Palette de couleurs — Thème sombre ── */
[data-theme="dark"] {
    --bg-primary:      #0f0f0f;
    --bg-secondary:    #171717;
    --bg-sidebar:      #0a0a0a;
    --bg-input:        #1e1e1e;
    --bg-message-user: rgba(255, 255, 255, 0.04);
    --bg-message-ai:   transparent;
    --bg-hover:        rgba(255, 255, 255, 0.06);
    --bg-card:         #1a1a1a;
    --text-primary:    #e5e5e5;
    --text-secondary:  #a0a0a0;
    --text-muted:      #525252;
    --accent:          #3b82f6;
    --accent-hover:    #2563eb;
    --accent-subtle:   rgba(59, 130, 246, 0.10);
    --accent-glow:     rgba(59, 130, 246, 0.15);
    --danger:          #f87171;
    --danger-subtle:   rgba(248, 113, 113, 0.10);
    --success:         #34d399;
    --success-subtle:  rgba(52, 211, 153, 0.10);
    --warning:         #fbbf24;
    --border:          rgba(255, 255, 255, 0.08);
    --border-hover:    rgba(255, 255, 255, 0.14);
    --shadow-sm:       0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md:       0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg:       0 8px 24px rgba(0, 0, 0, 0.5);
    --scrollbar-thumb: rgba(255, 255, 255, 0.10);
    --scrollbar-hover: rgba(255, 255, 255, 0.18);
    --code-bg:         rgba(255, 255, 255, 0.06);
    --code-text:       #93c5fd;
    --code-block-bg:   #1a1a1a;
    --strong-color:    #f0f0f0;
    color-scheme: dark;
}

/* ── Palette de couleurs — Thème clair ── */
[data-theme="light"] {
    --bg-primary:      #ffffff;
    --bg-secondary:    #f9fafb;
    --bg-sidebar:      #f3f4f6;
    --bg-input:        #ffffff;
    --bg-message-user: rgba(0, 0, 0, 0.04);
    --bg-message-ai:   transparent;
    --bg-hover:        rgba(0, 0, 0, 0.04);
    --bg-card:         #ffffff;
    --text-primary:    #171717;
    --text-secondary:  #6b7280;
    --text-muted:      #9ca3af;
    --accent:          #3b82f6;
    --accent-hover:    #2563eb;
    --accent-subtle:   rgba(59, 130, 246, 0.08);
    --accent-glow:     rgba(59, 130, 246, 0.12);
    --danger:          #ef4444;
    --danger-subtle:   rgba(239, 68, 68, 0.08);
    --success:         #10b981;
    --success-subtle:  rgba(16, 185, 129, 0.08);
    --warning:         #f59e0b;
    --border:          rgba(0, 0, 0, 0.08);
    --border-hover:    rgba(0, 0, 0, 0.15);
    --shadow-sm:       0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md:       0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg:       0 8px 24px rgba(0, 0, 0, 0.12);
    --scrollbar-thumb: rgba(0, 0, 0, 0.12);
    --scrollbar-hover: rgba(0, 0, 0, 0.20);
    --code-bg:         rgba(0, 0, 0, 0.04);
    --code-text:       #d946ef;
    --code-block-bg:   #f3f4f6;
    --strong-color:    #111827;
    color-scheme: light;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Inter', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;        /* fallback navigateurs sans dvh */
    height: 100dvh;       /* viewport dynamique : tient compte de la barre URL mobile */
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.hidden { display: none !important; }

.view {
    width: 100%;
    height: 100vh;
    height: 100dvh;
}

/* ============= WAKE BANNER ============= */
/* Bannière fixe en haut de l'écran affichée quand le PC de calcul est en cours de réveil (WoL) */
.wake-banner {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background: linear-gradient(90deg, #b45309, #d97706);
    color: #fffbeb;
    padding: 10px 20px;
    text-align: center;
    font-weight: 600;
    font-size: 13px;
    letter-spacing: 0.2px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    box-shadow: 0 2px 12px rgba(217, 119, 6, 0.4);
}

.wake-dot {
    width: 8px;
    height: 8px;
    background: #fffbeb;
    border-radius: 50%;
    animation: pulse 1.5s ease-in-out infinite;
    flex-shrink: 0;
}

@keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50%       { opacity: 0.4; transform: scale(0.75); }
}

/* ============= LOGIN ============= */
/* Page d'authentification (connexion / inscription) — centrée verticalement et horizontalement */
#view-login {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-primary);
}

.login-container {
    background: var(--bg-card);
    padding: 44px 40px 40px;
    border-radius: var(--radius-xl);
    width: 100%;
    max-width: 400px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    position: relative;
    z-index: 1;
    animation: loginAppear 0.4s cubic-bezier(0.16, 1, 0.3, 1) both;
}

@keyframes loginAppear {
    from { opacity: 0; transform: translateY(16px) scale(0.98); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

.login-container h1 {
    text-align: center;
    margin-bottom: 6px;
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--text-primary);
}

.login-subtitle {
    text-align: center;
    color: var(--text-secondary);
    margin-bottom: 28px;
    font-size: 14px;
    letter-spacing: 0.1px;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.auth-form input {
    padding: 11px 16px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
    transition: border-color var(--transition-base), background var(--transition-base), box-shadow var(--transition-base);
}

.auth-form input::placeholder {
    color: var(--text-muted);
}

.auth-form input:hover {
    border-color: var(--border-hover);
}

.auth-form input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

.auth-switch {
    text-align: center;
    margin-top: 18px;
    color: var(--text-secondary);
    font-size: 13px;
}

.auth-switch a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
    transition: color var(--transition-fast);
}

.auth-switch a:hover {
    color: var(--accent-hover);
    text-decoration: underline;
    text-underline-offset: 3px;
}

.error-msg {
    background: var(--danger-subtle);
    border: 1px solid var(--danger);
    color: var(--danger);
    padding: 10px 14px;
    border-radius: var(--radius);
    font-size: 13px;
    margin-bottom: 12px;
    line-height: 1.5;
}

.success-msg {
    background: var(--success-subtle);
    border: 1px solid var(--success);
    color: var(--success);
    padding: 10px 14px;
    border-radius: var(--radius);
    font-size: 13px;
    margin-bottom: 12px;
    line-height: 1.5;
}

/* ============= BUTTONS ============= */
.btn-primary {
    padding: 11px 20px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.2px;
    transition: all var(--transition-base);
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-primary:active {
    opacity: 0.9;
}

.btn-secondary {
    padding: 8px 16px;
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
    transition: all var(--transition-base);
}

.btn-secondary:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--border-hover);
}

.btn-secondary:active {
    background: var(--bg-hover);
}

.btn-icon {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 18px;
    padding: 6px;
    border-radius: var(--radius);
    transition: color var(--transition-fast), background var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-icon:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.btn-sidebar {
    width: 100%;
    padding: 8px 12px;
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
    transition: all var(--transition-base);
    text-align: left;
}

.btn-sidebar:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--border-hover);
}

.btn-danger {
    padding: 4px 10px;
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 12px;
    transition: all var(--transition-base);
}

.btn-danger:hover {
    background: var(--danger-subtle);
    border-color: var(--danger);
}

.btn-small {
    padding: 4px 10px;
    font-size: 12px;
    background: var(--accent-subtle);
    color: var(--accent);
    border: 1px solid var(--accent-glow);
    border-radius: 6px;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.btn-small:hover {
    background: var(--accent);
    color: white;
}

/* ============= MAIN LAYOUT ============= */
/* Layout principal : sidebar fixe à gauche + zone de chat flexible à droite */
#view-main {
    display: flex;
    height: 100vh;
    height: 100dvh;
    overflow-x: hidden;  /* évite que le dropdown modèle fasse déborder la page horizontalement */
}

/* ============= SIDEBAR ============= */
#sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    /* Very subtle inner shadow on the right edge for depth */
    box-shadow: inset -1px 0 0 var(--border);
    transition: width var(--transition-slow), min-width var(--transition-slow);
    overflow: hidden;
}

/* Mode collapsed (desktop uniquement) — rail d'icônes à largeur fixe */
@media (min-width: 769px) {
    #sidebar.collapsed {
        width: 60px;
        min-width: 60px;
        overflow: hidden;
    }

    #sidebar.collapsed .sidebar-header,
    #sidebar.collapsed .sidebar-search,
    #sidebar.collapsed #conversations-list,
    #sidebar.collapsed .sidebar-footer {
        display: none;
    }

    #sidebar.collapsed .sidebar-rail {
        display: flex;
    }
}

/* Rail d'icônes (caché par défaut, affiché en mode collapsed desktop) */
.sidebar-rail {
    display: none;
    flex-direction: column;
    align-items: center;
    padding: 12px 4px 8px;
    height: 100%;
    gap: 4px;
}

.sidebar-rail-spacer {
    flex: 1;
}

.sidebar-rail-avatar {
    cursor: pointer;
    margin-top: 8px;
    margin-bottom: 4px;
}

/* Bouton collapse dans la sidebar header */
.btn-collapse-sidebar {
    opacity: 0.6;
    transition: opacity var(--transition-fast), color var(--transition-fast), background var(--transition-fast);
}

.btn-collapse-sidebar:hover {
    opacity: 1;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    min-height: 53px;
    box-sizing: border-box;
}

.sidebar-header h2 {
    font-size: 15px;
    font-weight: 700;
    letter-spacing: -0.3px;
    color: var(--text-primary);
    padding-left: 4px;
}

.sidebar-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
}

#btn-new-chat {
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius);
}

#btn-new-chat:hover {
    background: var(--accent-subtle);
    color: var(--accent);
}

/* Barre de recherche de conversations */
.sidebar-search {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
}

.sidebar-search input {
    width: 100%;
    padding: 7px 12px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 13px;
    outline: none;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
    font-family: inherit;
}

.sidebar-search input::placeholder {
    color: var(--text-muted);
}

.sidebar-search input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

.conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

/* Section date headings in conv list */
.conv-date-heading {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    font-weight: 600;
    padding: 10px 8px 4px;
}

.conv-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 9px 12px;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background var(--transition-fast), border-color var(--transition-fast);
    margin-bottom: 1px;
    border: 1px solid transparent;
    gap: 4px;
    /* Supprime le délai des 300ms et le comportement "double tap" iOS/Safari
       qui forçait à cliquer deux fois sur certaines conversations. */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* Styles :hover réservés aux devices qui en supportent vraiment un — sinon
   sur mobile le premier tap "active" le hover et le second déclenche le clic,
   ce qui obligeait à double-cliquer certaines conversations. */
@media (hover: hover) {
    .conv-item:hover {
        background: var(--bg-hover);
        border-color: var(--border);
    }
}

.conv-item.active {
    /* Surface émergente sans saturation chromatique — le fond monte d'un niveau
       au lieu d'être teinté, ce qui évite le "glow wash" générique. */
    background: var(--bg-card);
    border-color: var(--border);
}

.conv-item .conv-title {
    flex: 1;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-secondary);
    transition: color var(--transition-fast);
}

.conv-item:hover .conv-title {
    color: var(--text-primary);
}

.conv-item.active .conv-title {
    /* Le bleu uniquement sur le titre actif : une seule ligne de couleur,
       chirurgicale, sans halo ni fond teinté. */
    color: var(--accent);
    font-weight: 500;
}

.conv-item .conv-delete {
    opacity: 0;
    color: var(--text-muted);
    font-size: 13px;
    padding: 3px 6px;
    border: none;
    background: none;
    cursor: pointer;
    transition: all var(--transition-fast);
    border-radius: 4px;
    flex-shrink: 0;
}

.conv-item:hover .conv-delete { opacity: 1; }

.conv-item .conv-delete:hover {
    color: var(--danger);
    background: var(--danger-subtle);
}

.sidebar-footer {
    padding: 10px 12px;
    border-top: 1px solid var(--border);
    background: transparent;
}

.sidebar-footer-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-width: 0;
}

.sidebar-footer-user {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    min-width: 0;
}

.user-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    flex-shrink: 0;
}

.user-info {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
    font-weight: 500;
}

.sidebar-footer-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

/* ============= CHAT AREA ============= */
/* Zone principale de chat : header avec sélecteur de modèle, flux de messages, zone de saisie */
#chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    min-width: 0;
    min-height: 0;  /* permet à .messages de scroller correctement dans le flex column */
    background: var(--bg-primary);
}

#chat-header {
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 12px;
    background: var(--bg-primary);
    flex-shrink: 0;
    min-height: 53px;
    box-sizing: border-box;
}

.chat-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.chat-header-center {
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-header-right {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
}

#chat-title-area {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

#chat-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    letter-spacing: -0.1px;
}

#ctx-bar-wrapper {
    display: flex;
    align-items: center;
    gap: 6px;
}

#ctx-bar-track {
    width: 72px;
    height: 3px;
    border-radius: 2px;
    background: var(--border);
    overflow: hidden;
    flex-shrink: 0;
}

#ctx-bar-fill {
    height: 100%;
    border-radius: 2px;
    background: var(--accent);
    transition: width 0.4s ease;
}

#ctx-bar-fill.ctx-warn {
    background: #f59e0b;
}

#ctx-bar-fill.ctx-danger {
    background: #ef4444;
}

#ctx-bar-label {
    font-size: 10px;
    color: var(--text-muted);
    white-space: nowrap;
}

#ctx-mobile-badge {
    display: none;
}

.ctx-warning-banner {
    margin: 8px 16px 0;
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 13px;
    line-height: 1.4;
    background: rgba(245, 158, 11, 0.12);
    border: 1px solid rgba(245, 158, 11, 0.45);
    color: var(--text-primary, #e5e7eb);
}

.ctx-warning-banner.ctx-banner-danger {
    background: rgba(239, 68, 68, 0.15);
    border-color: rgba(239, 68, 68, 0.55);
}

/* Sélecteur de modèle dans le header du chat */
/* ── Custom model dropdown ── */
.model-dropdown {
    position: relative;
    flex-shrink: 1;
    min-width: 0;
}

.model-dropdown-trigger {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 12px 5px 8px;
    background: transparent;
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: border-color var(--transition-fast), background var(--transition-fast);
    max-width: 280px;
    white-space: nowrap;
}

.model-dropdown-trigger:hover {
    border-color: var(--border-hover);
    background: var(--bg-hover);
}

.model-dropdown-trigger.locked {
    cursor: not-allowed;
    opacity: 0.6;
}

.model-dropdown-trigger.locked:hover {
    border-color: var(--border);
    background: transparent;
}

.model-dropdown-trigger.locked .model-trigger-chevron {
    display: none;
}

.model-dropdown.open .model-dropdown-trigger {
    border-color: var(--accent);
    background: var(--bg-hover);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

.model-trigger-logo {
    width: 22px;
    height: 22px;
    border-radius: 5px;
    overflow: hidden;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
    font-size: 11px;
    color: var(--text-secondary);
}

.model-trigger-logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.model-trigger-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.model-trigger-chevron {
    flex-shrink: 0;
    opacity: 0.5;
    transition: transform 0.2s;
}

.model-dropdown.open .model-trigger-chevron {
    transform: rotate(180deg);
}

.model-dropdown-menu {
    position: absolute;
    top: calc(100% + 6px);
    left: 50%;
    transform: translateX(-50%);
    min-width: 300px;
    max-width: min(380px, calc(100vw - 24px));  /* ne jamais déborder horizontalement */
    max-height: min(60vh, 360px);
    overflow-y: auto;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: 0 8px 30px rgba(0,0,0,0.25);
    padding: 6px;
    z-index: 200;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s, transform 0.15s;
    transform: translateX(-50%) translateY(-4px);
}

.model-dropdown.open .model-dropdown-menu {
    opacity: 1;
    pointer-events: auto;
    transform: translateX(-50%) translateY(0);
}

.model-dropdown-menu.hidden {
    display: none;
}

.model-dropdown-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.12s;
}

.model-dropdown-item:hover {
    background: var(--bg-hover);
}

.model-dropdown-item.selected {
    background: var(--accent-glow);
}

.model-item-logo {
    width: 28px;
    height: 28px;
    border-radius: 6px;
    overflow: hidden;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
    font-size: 13px;
    color: var(--text-secondary);
}

.model-item-logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.model-item-info {
    flex: 1;
    min-width: 0;
}

.model-item-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.model-item-desc {
    font-size: 11px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
}

.model-item-badges {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

.model-badge {
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 8px;
    font-weight: 500;
    white-space: nowrap;
}

.model-badge-vision {
    background: rgba(59, 130, 246, 0.15);
    color: #60a5fa;
}

.model-badge-thinking {
    background: rgba(168, 85, 247, 0.15);
    color: #c084fc;
}


/* Bouton conversation temporaire */
.btn-temp-chat {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    color: var(--text-muted);
    transition: color var(--transition-fast), background var(--transition-fast);
}

.btn-temp-chat:hover {
    color: var(--accent);
    background: var(--accent-subtle);
}

.btn-temp-chat.active {
    color: var(--accent);
    background: var(--accent-subtle);
}

/* Sur mobile/touch, le :focus persiste après un touch. On neutralise ce
   style tant que l'état visuel n'est pas explicitement "active". */
.btn-temp-chat:focus:not(:focus-visible) {
    color: var(--text-muted);
    background: transparent;
}

.btn-temp-chat.active:focus:not(:focus-visible) {
    color: var(--accent);
    background: var(--accent-subtle);
}

/* Sur écrans tactiles : désactiver le :hover qui persiste après un touch */
@media (hover: none) {
    .btn-temp-chat:hover {
        color: var(--text-muted);
        background: transparent;
    }
    .btn-temp-chat.active:hover {
        color: var(--accent);
        background: var(--accent-subtle);
    }
}

.btn-temp-chat.hidden-temp {
    display: none !important;
}

.btn-temp-chat.temp-locked {
    cursor: default;
    pointer-events: none;
}

/* Wrapper pour le bouton options (position relative pour le dropdown) */
.conv-options-wrapper {
    position: relative;
}

/* Bouton ⋮ options de conversation */
.btn-conv-options {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    color: var(--text-muted);
    transition: color var(--transition-fast), background var(--transition-fast);
}

.btn-conv-options:hover {
    color: var(--accent);
    background: var(--accent-subtle);
}

.btn-conv-options.hidden {
    display: none !important;
}

/* Menu dropdown options */
.conv-options-menu {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    min-width: 175px;
    z-index: 200;
    padding: 4px;
    overflow: hidden;
}

.conv-options-menu.hidden {
    display: none;
}

.conv-option-item {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 8px 10px;
    width: 100%;
    background: none;
    border: none;
    cursor: pointer;
    border-radius: 7px;
    color: var(--text-primary);
    font-size: 13.5px;
    text-align: left;
    transition: background var(--transition-fast);
}

.conv-option-item:hover:not(:disabled) {
    background: var(--bg-hover);
}

.conv-option-item:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.conv-option-danger {
    color: var(--danger);
}

.conv-option-badge {
    margin-left: auto;
    font-size: 10px;
    opacity: 0.55;
    font-weight: 400;
}

.conv-option-separator {
    border: none;
    border-top: 1px solid var(--border);
    margin: 4px 6px;
}

/* Champ de saisie inline pour renommer le titre */
.chat-title-input {
    background: transparent;
    border: none;
    border-bottom: 1.5px solid var(--accent);
    color: inherit;
    font-size: inherit;
    font-family: inherit;
    font-weight: inherit;
    outline: none;
    max-width: 280px;
    padding: 0 2px;
}

.sidebar-toggle {
    display: none;
}

.messages {
    flex: 1;
    min-height: 0;  /* permet au scroll interne de fonctionner dans un flex column */
    overflow-y: auto;
    padding: 24px max(20px, calc((100% - 780px) / 2));
    display: flex;
    flex-direction: column;
    gap: 6px;
    /* Small gap — messages have their own spacing via padding */
}

/* ── Welcome screen ── */
.welcome-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: auto;
    text-align: center;
    padding: 40px 24px;
    max-width: 480px;
}

.welcome-icon {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--accent-subtle);
    border: 1px solid var(--accent-glow);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
    color: var(--accent);
    overflow: hidden;
}

.welcome-icon.has-logo {
    background: var(--bg-secondary);
    border-color: var(--border);
    padding: 0;
}

.welcome-icon.has-logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: inherit;
}

.welcome-model-desc {
    font-size: 13px;
    color: var(--text-muted);
    line-height: 1.5;
    max-width: 320px;
    margin: -4px auto 12px;
    text-align: center;
}

.welcome-message h2 {
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--text-primary);
    margin-bottom: 10px;
}

.welcome-message p {
    color: var(--text-secondary);
    font-size: 15px;
    line-height: 1.6;
}

/* ── Messages ── */
.message {
    display: flex;
    gap: 12px;
    padding: 16px 18px;
    border-radius: var(--radius-lg);
    position: relative;
    transition: background var(--transition-fast);
    /* Entry animation */
    animation: msgAppear 0.2s ease-out both;
}

@keyframes msgAppear {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.message-user {
    background: var(--bg-message-user);
    border: 1px solid transparent;
    align-self: flex-end;
    max-width: 75%;
}

.message-assistant {
    background: var(--bg-message-ai);
    border: 1px solid transparent;
    align-self: flex-start;
    max-width: 85%;
}

.message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    flex-shrink: 0;
    letter-spacing: 0;
    margin-top: 2px;
}

.avatar-user {
    background: var(--accent);
    color: white;
}

.avatar-ai {
    background: var(--bg-secondary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

.avatar-ai.avatar-ai-logo {
    overflow: hidden;
    padding: 0;
}

.avatar-ai.avatar-ai-logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.message-body {
    flex: 1;
    min-width: 0;
}

.message-content {
    font-size: 14px;
    line-height: 1.75;
    word-wrap: break-word;
    overflow-wrap: break-word;
    color: var(--text-primary);
}

.message-content p { margin-bottom: 10px; }
.message-content p:last-child { margin-bottom: 0; }

.message-content ul, .message-content ol {
    margin: 8px 0 8px 20px;
}

.message-content li { margin-bottom: 5px; }

.message-content h1, .message-content h2, .message-content h3 {
    margin: 18px 0 10px;
    letter-spacing: -0.2px;
}

.message-content strong { color: var(--strong-color); }

.message-content a {
    color: var(--accent);
    text-underline-offset: 3px;
}

/* Inline code */
.message-content code {
    background: var(--code-bg);
    padding: 2px 7px;
    border-radius: 5px;
    font-family: 'Fira Code', 'Consolas', 'SF Mono', monospace;
    font-size: 13px;
    border: 1px solid var(--border);
    color: var(--code-text);
}

/* Code blocks */
.message-content pre {
    background: var(--code-block-bg);
    padding: 16px 18px;
    border-radius: var(--radius);
    overflow-x: auto;
    margin: 12px 0;
    position: relative;
    border: 1px solid var(--border);
}

.message-content pre code {
    background: none;
    padding: 0;
    border: none;
    font-size: 13px;
    color: var(--text-primary);
    line-height: 1.65;
}

.message-content blockquote {
    border-left: 3px solid var(--accent);
    padding: 8px 0 8px 16px;
    margin: 10px 0;
    color: var(--text-secondary);
    font-style: italic;
}

.message-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 12px 0;
    font-size: 13px;
}

.message-content th,
.message-content td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    text-align: left;
}

.message-content th {
    background: var(--bg-hover);
    font-weight: 600;
    color: var(--strong-color);
}

.message-content tr:nth-child(even) {
    background: var(--bg-hover);
}

.message-actions {
    display: flex;
    gap: 6px;
    margin-top: 10px;
    opacity: 0;
    transition: opacity var(--transition-base);
}

.message:hover .message-actions,
.message:focus-within .message-actions { opacity: 1; }

.message-actions button {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 3px 8px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 11px;
    transition: all var(--transition-fast);
}

.message-actions button:hover {
    color: var(--text-primary);
    border-color: var(--border-hover);
    background: var(--bg-hover);
}

.message-meta {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 8px;
}


.message-edited {
    font-style: italic;
    color: var(--text-muted);
    font-size: 11px;
}

/* ── Thinking block ── */
/* Bloc repliable affichant le raisonnement interne des modèles thinking (DeepSeek-R1, QwQ) */
.thinking-block {
    background: var(--bg-hover);
    border-left: 2px solid var(--accent);
    padding: 10px 14px;
    margin: 10px 0;
    border-radius: 0 var(--radius) var(--radius) 0;
    font-size: 13px;
    color: var(--text-secondary);
    font-style: italic;
}

.thinking-toggle {
    cursor: pointer;
    color: var(--accent);
    font-size: 12px;
    margin-bottom: 6px;
    font-style: normal;
    font-weight: 500;
}

.thinking-content {
    white-space: pre-wrap; /* Préserve les sauts de ligne du raisonnement */
    margin-top: 6px;
}

.thinking-content.hidden {
    display: none;
}

/* ── Thinking badge (input area) ── */
.thinking-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    font-size: 11px;
    font-weight: 500;
    color: #c084fc;
    background: rgba(168, 85, 247, 0.12);
    border-radius: 12px;
    white-space: nowrap;
    transition: opacity 0.2s;
    flex-shrink: 0;
}

.thinking-badge.hidden {
    display: none;
}

.thinking-badge svg {
    color: #c084fc;
}

/* ── Streaming cursor ── */
.streaming-cursor::after {
    content: '▊';
    animation: blink 0.9s ease-in-out infinite;
    color: var(--accent);
    font-size: 0.85em;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0; }
}

#preparing-indicator .message-content {
    min-height: 24px;
}

.retry-generation-btn {
    display: inline-block;
    margin-top: 8px;
    padding: 6px 12px;
    background: var(--bg-hover);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 6px;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s;
}
.retry-generation-btn:hover {
    background: var(--border);
}

/* ── Copy button on code blocks ── */
.code-copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    background: var(--bg-hover);
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 3px 8px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 11px;
    opacity: 0;
    transition: opacity var(--transition-fast), background var(--transition-fast), color var(--transition-fast);
}

.message-content pre:hover .code-copy-btn { opacity: 1; }

.code-copy-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--border-hover);
}

/* ============= INPUT AREA ============= */
/* Zone de saisie en bas du chat : textarea auto-extensible, bouton upload, bouton envoi */
.input-area {
    padding: 12px 20px 20px;
    background: var(--bg-primary);
    flex-shrink: 0;
}

.input-wrapper {
    max-width: 780px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 10px 12px 10px 18px;
    transition: border-color var(--transition-base), box-shadow var(--transition-base);
}

.input-top-row {
    display: flex;
    align-items: center;
    min-height: 0;
}

.input-top-row:empty,
.input-top-row:has(> .hidden:only-child) {
    display: none;
}

.input-bottom-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.input-wrapper:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

#message-input {
    flex: 1;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.6;
    resize: none;
    outline: none;
    max-height: 200px;
    font-family: inherit;
    padding: 2px 0;
}

#message-input::placeholder {
    color: var(--text-muted);
}

.upload-btn {
    cursor: pointer;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: var(--text-muted);
    transition: color var(--transition-fast), background var(--transition-fast);
}

.upload-btn:hover {
    color: var(--text-secondary);
    background: var(--accent-subtle);
}

.btn-send {
    background: var(--accent);
    border: none;
    color: white;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition-base), transform var(--transition-fast);
    flex-shrink: 0;
    margin-bottom: 1px;
}

.btn-send:hover {
    background: var(--accent-hover);
    transform: scale(1.06);
}

.btn-send:active {
    transform: scale(0.95);
}

.btn-send:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* Bouton Arrêter la génération — même calibrage que .btn-send mais couleur
   d'alerte. Affiché à la place du bouton Envoyer pendant le streaming. */
#btn-stop {
    background: var(--danger, #ef4444);
}
#btn-stop:hover {
    background: var(--danger-hover, #dc2626);
    transform: scale(1.06);
}

.file-preview {
    max-width: 780px;
    margin: 8px auto 0;
    padding: 7px 12px;
    background: var(--bg-input);
    border-radius: var(--radius);
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 8px;
    border: 1px solid var(--border);
    color: var(--text-secondary);
}

.file-preview .remove-file {
    cursor: pointer;
    color: var(--danger);
    font-size: 15px;
    margin-left: auto;
    opacity: 0.7;
    transition: opacity var(--transition-fast);
}

.file-preview .remove-file:hover { opacity: 1; }

.file-loading {
    color: var(--text-secondary);
    animation: pulse-loading 1.2s ease-in-out infinite;
}
@keyframes pulse-loading {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 1; }
}

/* Spinner de chargement de conversation */
.conv-loading {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    opacity: 0.4;
}
.conv-loading-spinner {
    width: 28px;
    height: 28px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: conv-spin 0.7s linear infinite;
}
@keyframes conv-spin {
    to { transform: rotate(360deg); }
}

/* Blocs fichier repliables dans les messages utilisateur */
.file-attachment-block {
    margin-top: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    font-size: 0.85em;
    text-align: left;
}

.file-attachment-block summary {
    padding: 6px 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-input);
    color: var(--text-secondary);
    user-select: none;
    list-style: none;
}

.file-attachment-block summary::-webkit-details-marker { display: none; }

.file-attachment-block summary svg {
    flex-shrink: 0;
    opacity: 0.7;
}

.file-attachment-block[open] summary {
    border-bottom: 1px solid var(--border);
}

.file-attachment-content {
    max-height: 300px;
    overflow: auto;
    padding: 10px 12px;
    margin: 0;
    font-size: 0.9em;
    white-space: pre-wrap;
    word-break: break-all;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-family: var(--font-mono, monospace);
}

.message-image {
    max-width: 300px;
    max-height: 300px;
    border-radius: var(--radius);
    margin-bottom: 8px;
    cursor: pointer;
    transition: transform 0.2s ease;
}
.message-image:hover {
    transform: scale(1.02);
}

/* ============= ADMIN ============= */
/* Panel d'administration : gestion des utilisateurs, prompts système, config modèles, statistiques */
.admin-container {
    max-width: 1000px;
    margin: 0 auto;
    padding: 32px 24px;
    height: 100vh;
    height: 100dvh;
    overflow-y: auto;
}

.admin-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 28px;
}

.admin-header h2 {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.3px;
    color: var(--text-primary);
}

.admin-tabs {
    display: flex;
    gap: 2px;
    margin-bottom: 24px;
    border-bottom: 1px solid var(--border);
}

.tab {
    padding: 9px 20px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    transition: color var(--transition-base), border-color var(--transition-base);
    border-radius: var(--radius) var(--radius) 0 0;
}

.tab:hover { color: var(--text-secondary); }

.tab.active {
    color: var(--text-primary);
    border-bottom-color: var(--accent);
}

.tab-content { display: none; }
.tab-content.active { display: block; }

/* Users table */
.admin-table {
    width: 100%;
    border-collapse: collapse;
    border-radius: var(--radius-lg);
    overflow: hidden;
}

.admin-table thead {
    background: var(--bg-hover);
}

.admin-table th, .admin-table td {
    padding: 12px 14px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}

.admin-table th {
    color: var(--text-muted);
    text-transform: uppercase;
    font-size: 11px;
    letter-spacing: 0.7px;
    font-weight: 600;
}

.admin-table tbody tr {
    transition: background var(--transition-fast);
}

.admin-table tbody tr:hover {
    background: var(--bg-hover);
}

.admin-table select {
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 4px 8px;
    font-size: 12px;
    outline: none;
    cursor: pointer;
}

/* Modèle par défaut (panel admin) */
.admin-default-model {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px 20px;
    margin-bottom: 20px;
}

.admin-default-model > label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 10px;
}

.default-model-row {
    display: flex;
    gap: 10px;
    align-items: center;
}

.default-model-row select {
    flex: 1;
    padding: 8px 10px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.9rem;
}

.default-model-hint {
    margin-top: 8px;
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* Prompts editor */
.prompt-editor {
    background: var(--bg-secondary);
    padding: 20px 24px;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    margin-bottom: 24px;
}

.prompt-editor label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.prompt-editor select {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 14px;
    font-size: 13px;
    outline: none;
    transition: border-color var(--transition-fast);
}

.prompt-editor select:focus { border-color: var(--border-hover); }

.prompt-editor textarea {
    width: 100%;
    padding: 12px 14px;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    font-family: 'Fira Code', 'Consolas', monospace;
    resize: vertical;
    outline: none;
    line-height: 1.65;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.prompt-editor textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

/* ── Model config section (admin) ── */
.model-config-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin: 14px 0;
    padding: 16px;
    background: var(--bg-hover);
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.config-row {
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}

.config-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-width: 160px;
}

.config-field-wide {
    flex: 2;
}

.config-field label {
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 500;
}

.config-field input {
    padding: 8px 12px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 13px;
    transition: border-color 0.2s;
}

.config-field input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

.config-checkbox {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-secondary);
    padding: 6px 12px;
    border-radius: var(--radius);
    transition: background 0.15s;
}

.config-checkbox:hover {
    background: var(--bg-secondary);
}

.config-checkbox input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--accent);
    cursor: pointer;
}

.config-logo-preview {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 32px;
}

.config-logo-preview img {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    object-fit: contain;
    background: var(--bg-primary);
    padding: 2px;
}

.prompt-actions {
    display: flex;
    gap: 8px;
    margin-top: 14px;
}

/* Prompts list */
.prompts-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* Stats */
.stats-summary {
    display: flex;
    gap: 14px;
    margin-bottom: 24px;
    flex-wrap: wrap;
}

.stat-card {
    background: var(--bg-secondary);
    padding: 20px 24px;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    flex: 1;
    min-width: 150px;
    text-align: center;
    transition: border-color var(--transition-base), box-shadow var(--transition-base);
}

.stat-card:hover {
    border-color: var(--border-hover);
    box-shadow: var(--shadow-md);
}

.stat-card .stat-value {
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: -1px;
    color: var(--accent);
    line-height: 1;
    margin-bottom: 4px;
}

.stat-card .stat-label {
    font-size: 12px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 500;
}

#stats-chart {
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    width: 100%;
    display: block;
}

.chart-title {
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 600;
    margin: 20px 0 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.charts-row {
    display: flex;
    gap: 20px;
    margin-top: 10px;
}
.chart-container {
    flex: 1;
    min-width: 0;
}
.chart-container canvas {
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    width: 100%;
    display: block;
}

@media (max-width: 1024px) {
    .charts-row {
        flex-direction: column;
    }
}

.stat-card-online .stat-value {
    color: var(--success);
}
.stat-detail {
    font-size: 11px;
    color: var(--text-secondary);
    margin-top: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
}

/* ============= SCROLLBAR ============= */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-hover); }

/* ============= OVERLAY LOADER MODÈLES ============= */
@keyframes models-spin {
    to { transform: rotate(360deg); }
}

@keyframes models-fade-out {
    to { opacity: 0; pointer-events: none; }
}

.models-loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-primary, #0f0f13);
    transition: opacity 0.4s ease;
}

.btn-overlay-logout {
    position: absolute;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%);
    background: transparent;
    border: 1px solid rgba(255,255,255,0.15);
    color: rgba(255,255,255,0.45);
    padding: 0.5rem 1.25rem;
    border-radius: 6px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: color 0.2s, border-color 0.2s;
    white-space: nowrap;
}
.btn-overlay-logout:hover {
    color: rgba(255,255,255,0.85);
    border-color: rgba(255,255,255,0.35);
}

.models-loading-overlay.hidden {
    animation: models-fade-out 0.4s ease forwards;
    pointer-events: none;
}

.models-loading-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

.models-loading-spinner {
    width: 48px;
    height: 48px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: models-spin 0.8s linear infinite;
}

.models-loading-text {
    font-size: 14px;
    color: var(--text-secondary);
    margin: 0;
    letter-spacing: 0.02em;
}

/* ============= MOBILE OVERLAY ============= */
/* Dimmed backdrop when sidebar is open on mobile */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 99;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    animation: fadeIn 0.25s ease both;
}

.sidebar-overlay.active { display: block; }

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* ============= RESPONSIVE ============= */
/* Adaptations pour les différentes tailles d'écran (tablette, mobile, petit smartphone, paysage) */

/* ── Tablette ── */
@media (max-width: 1024px) {
    :root {
        --sidebar-width: 220px;
    }

    .messages {
        padding: 18px 16px;
    }

    .admin-container {
        padding: 24px 18px;
    }
}

/* ── Mobile ── */
/* La sidebar passe en mode tiroir (off-canvas), le header se compacte */
@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        left: calc(var(--sidebar-width) * -1);
        top: 0;
        z-index: 100;
        transition: left var(--transition-slow), box-shadow var(--transition-slow);
        box-shadow: none;
    }

    #sidebar.open {
        left: 0;
        box-shadow: 4px 0 32px rgba(0, 0, 0, 0.6);
    }

    .btn-collapse-sidebar,
    .btn-expand-sidebar {
        display: none !important;
    }

    .sidebar-toggle {
        display: flex !important;
    }

    #chat-header {
        grid-template-columns: auto 1fr auto;
        grid-template-rows: auto auto;
        padding: 6px 12px 0;
        gap: 0;
    }

    .chat-header-left {
        grid-column: 1 / 2;
        grid-row: 1;
        padding-bottom: 6px;
    }

    .chat-header-right {
        grid-column: 3 / 4;
        grid-row: 1;
        padding-bottom: 6px;
    }

    .chat-header-center {
        grid-column: 1 / -1;
        grid-row: 2;
        justify-content: center;
        border-top: 1px solid var(--border);
        padding: 5px 0 6px;
    }

    /* Masque la barre linéaire de contexte sur mobile — remplacée par le badge rond */
    #ctx-bar-wrapper { display: none !important; }
    #ctx-bar-label { display: none; }

    /* Badge rond % de contexte — visible uniquement sur mobile.
       30×30px : calibré pour tenir dans la hauteur du header (~44px) tout en
       restant lisible avec font-size 9px (nécessaire pour afficher "100%"). */
    #ctx-mobile-badge {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 30px;
        height: 30px;
        border-radius: 50%;
        font-size: 9px;
        font-weight: 700;
        letter-spacing: -0.3px;
        flex-shrink: 0;
        background: var(--border);
        color: var(--text-muted);
        transition: background 0.3s ease, color 0.3s ease;
    }
    #ctx-mobile-badge.hidden { display: none !important; }
    #ctx-mobile-badge.ctx-warn {
        background: rgba(245, 158, 11, 0.20);
        color: #f59e0b;
    }
    #ctx-mobile-badge.ctx-danger {
        background: rgba(239, 68, 68, 0.20);
        color: #ef4444;
    }

    .model-dropdown-trigger {
        max-width: 220px;
        font-size: 12px;
        padding: 4px 8px 4px 6px;
    }

    .model-item-desc {
        /* Descriptions affichées sur mobile mais plus compactes : on autorise
           le wrap sur 2 lignes maximum pour garder le menu lisible. */
        white-space: normal;
        font-size: 10.5px;
        line-height: 1.35;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .model-dropdown-menu {
        min-width: min(320px, calc(100vw - 24px));
        max-width: calc(100vw - 24px);
        max-height: min(70vh, 420px);
        left: 50%;
        right: auto;
        transform: translateX(-50%);
    }

    .model-dropdown.open .model-dropdown-menu {
        transform: translateX(-50%) translateY(0);
    }

    .messages {
        padding: 12px;
        gap: 4px;
    }

    .message {
        padding: 12px 14px;
        border-radius: var(--radius);
    }

    .input-area {
        padding: 10px 12px 14px;
    }

    .admin-container {
        padding: 16px;
    }

    .stats-summary {
        flex-direction: column;
    }

    .btn-send {
        width: 38px;
        height: 38px;
    }

    .conv-item {
        padding: 11px 12px;
    }

    /* Empêche le zoom iOS au focus sur les inputs (font-size < 16px déclenche le zoom) */
    .auth-form input,
    #message-input,
    .sidebar-search input,
    .prompt-editor textarea {
        font-size: 16px;
    }

    /* Login responsive */
    .login-container {
        padding: 28px 24px;
        margin: 0 16px;
    }

    /* Admin table scrollable */
    .tab-content {
        overflow-x: auto;
    }

    .admin-table {
        min-width: 600px;
    }

    /* Colonnes secondaires masquees */
    .admin-table th:nth-child(4),
    .admin-table td:nth-child(4),
    .admin-table th:nth-child(5),
    .admin-table td:nth-child(5) {
        display: none;
    }

    /* Tabs scrollable */
    .admin-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .admin-tabs::-webkit-scrollbar {
        display: none;
    }

    .tab {
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Image responsive */
    .message-image {
        max-width: 100%;
    }
}

/* ── Petit smartphone ── */
@media (max-width: 480px) {
    .login-container {
        padding: 24px 18px;
        max-width: 100%;
        margin: 0 12px;
    }

    .login-container h1 {
        font-size: 1.6rem;
    }

    .messages {
        padding: 10px 8px;
    }

    .message {
        padding: 10px 12px;
        gap: 8px;
    }

    .ctx-warning-banner {
        margin: 8px 8px 0;
        padding: 8px 10px;
        font-size: 12px;
    }

    .message-avatar {
        width: 26px;
        height: 26px;
        font-size: 11px;
    }

    .input-area {
        padding: 8px 8px 12px;
    }

    .input-wrapper {
        padding: 6px 8px 6px 12px;
    }

    .stat-card {
        min-width: 120px;
        padding: 14px 16px;
    }

    .stat-card .stat-value {
        font-size: 1.5rem;
    }

    .welcome-message {
        padding: 24px 16px;
    }

    .welcome-message h2 {
        font-size: 1.2rem;
    }

    .model-dropdown-trigger {
        max-width: 140px;
    }

    .model-trigger-name {
        font-size: 11px;
    }

    /* Sur très petit écran, on ancre le menu au viewport (fixed) pour
       garantir qu'il n'est jamais coupé par le header ou la largeur du parent. */
    .model-dropdown-menu {
        position: fixed;
        top: 56px;
        left: 12px;
        right: 12px;
        bottom: auto;
        min-width: 0;
        max-width: none;
        width: auto;
        max-height: min(72vh, 480px);
        transform: none !important;
    }

    .model-dropdown.open .model-dropdown-menu {
        transform: none !important;
    }

    .model-item-badges {
        font-size: 9px;
        gap: 3px;
    }

    .model-badge {
        font-size: 9px;
        padding: 1px 5px;
    }

    .config-row {
        flex-direction: column;
    }

    .admin-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }

    .admin-table {
        min-width: 450px;
    }

    .admin-table th:nth-child(4),
    .admin-table td:nth-child(4),
    .admin-table th:nth-child(5),
    .admin-table td:nth-child(5),
    .admin-table th:nth-child(6),
    .admin-table td:nth-child(6) {
        display: none;
    }
}

/* ── Paysage mobile ── */
@media (max-height: 450px) and (orientation: landscape) {
    #chat-header {
        padding: 8px 16px;
    }

    .input-area {
        padding: 6px 12px 8px;
    }

    #message-input {
        max-height: 100px;
    }

    .messages {
        padding: 8px 12px;
    }
}

/* ── Accessibilité clavier ── */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Remove default outline when not using keyboard */
:focus:not(:focus-visible) {
    outline: none;
}


/* ============= KILLSWITCH (ARRÊT D'URGENCE) ============= */

/* ── Panneau admin : boutons killswitch ── */
.admin-killswitch {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px 20px;
    margin-bottom: 20px;
}

.admin-killswitch h3 {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 12px;
}

.killswitch-buttons {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.btn-killswitch {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border: 2px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
    font-weight: 600;
    font-size: 0.85rem;
    transition: all var(--transition-fast);
    background: var(--bg-input);
}

.btn-killswitch svg {
    flex-shrink: 0;
}

.btn-killswitch-soft {
    border-color: #e67e22;
    color: #e67e22;
}

.btn-killswitch-soft:hover {
    background: rgba(230, 126, 34, 0.1);
}

.btn-killswitch-soft.active {
    background: #e67e22;
    color: white;
    border-color: #e67e22;
}

.btn-killswitch-hard {
    border-color: #e74c3c;
    color: #e74c3c;
}

.btn-killswitch-hard:hover {
    background: rgba(231, 76, 60, 0.1);
}

.btn-killswitch-hard.active {
    background: #e74c3c;
    color: white;
    border-color: #e74c3c;
}

.killswitch-status {
    margin-top: 10px;
    font-size: 0.82rem;
    color: var(--text-secondary);
    font-style: italic;
}

/* ── Zone de saisie désactivée (killswitch soft) ── */
#input-area.killswitch-disabled {
    opacity: 0.5;
    pointer-events: none;
}

#input-area.killswitch-disabled #message-input {
    background: var(--bg-secondary);
    cursor: not-allowed;
    color: var(--text-muted);
}

/* ── Page de maintenance (killswitch hard) ── */
.maintenance-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    height: 100dvh;
    text-align: center;
    padding: 20px;
    background: var(--bg-primary);
    color: var(--text-primary);
}

.maintenance-icon {
    color: #e74c3c;
    margin-bottom: 24px;
    animation: pulse-icon 2s ease-in-out infinite;
}

@keyframes pulse-icon {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.maintenance-container h1 {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 12px;
}

.maintenance-container p {
    color: var(--text-secondary);
    font-size: 1rem;
    max-width: 500px;
}

.maintenance-sub {
    color: var(--text-muted) !important;
    font-size: 0.85rem !important;
    margin-top: 8px;
}
