:root {
    --bg-sidebar: #202123;
    --bg-main: #343541;
    --bg-user-bubble: #444654;
    --bg-assistant-bubble: #343541;
    --text-light: #ececf1;
    --text-muted: #8e8ea0;
    --accent: #10a37f;
    --accent-hover: #0e8f6f;
    --border: #4d4d4f;
    --danger: #e05252;
}

* {
    box-sizing: border-box;
}

html, body {
    height: 100%;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--text-light);
    background: var(--bg-main);
    /* 100dvh suit la barre d'adresse mobile qui apparaît/disparaît ; 100vh en repli. */
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

.app {
    display: flex;
    height: 100%;
    width: 100%;
}

/* --- Sidebar --- */

.sidebar {
    width: 260px;
    flex-shrink: 0;
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    padding: 10px;
    padding-top: max(10px, env(safe-area-inset-top, 0px));
    overflow-y: auto;
}

.sidebar-close-btn {
    display: none;
    margin-left: auto;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 18px;
    cursor: pointer;
    padding: 6px 8px;
    border-radius: 6px;
}

.sidebar-close-btn:hover {
    background: #2b2c2f;
    color: var(--text-light);
}

.sidebar-backdrop {
    display: none;
}

.menu-toggle {
    display: none;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-light);
    border-radius: 6px;
    font-size: 16px;
    line-height: 1;
    padding: 8px 10px;
    cursor: pointer;
}

.hal-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 6px 16px;
}

.hal-brand-name {
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.08em;
    color: var(--text-muted);
}

.hal-eye {
    --eye-size: 18px;
    width: var(--eye-size);
    height: var(--eye-size);
    border-radius: 50%;
    flex-shrink: 0;
    background: radial-gradient(circle at 35% 32%, #ff9a9a 0%, #ff1a1a 35%, #8b0000 75%, #300000 100%);
    box-shadow: 0 0 6px 1px rgba(255, 20, 20, 0.55), 0 0 14px 3px rgba(255, 0, 0, 0.25);
    animation: hal-pulse 3.2s ease-in-out infinite;
    cursor: default;
}

.hal-eye.thinking {
    animation: hal-pulse 0.7s ease-in-out infinite;
    box-shadow: 0 0 10px 3px rgba(255, 20, 20, 0.85), 0 0 22px 6px rgba(255, 0, 0, 0.45);
}

@keyframes hal-pulse {
    0%, 100% { opacity: 0.75; transform: scale(1); }
    50%      { opacity: 1;    transform: scale(1.12); }
}

.new-chat-btn {
    background: transparent;
    color: var(--text-light);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    cursor: pointer;
    font-size: 14px;
    text-align: left;
    margin-bottom: 10px;
}

.new-chat-btn:hover {
    background: #2b2c2f;
}

.conversation-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
    overflow-y: auto;
}

.conversation-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-light);
    gap: 8px;
}

.conversation-item:hover {
    background: #2b2c2f;
}

.conversation-item.active {
    background: #343541;
}

.conversation-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}

.conversation-delete {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 14px;
    padding: 2px 6px;
    border-radius: 4px;
    flex-shrink: 0;
}

.conversation-delete:hover {
    color: var(--danger);
    background: rgba(224, 82, 82, 0.15);
}

/* --- Main --- */

.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.topbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    padding-top: max(10px, env(safe-area-inset-top, 0px));
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.model-label {
    font-size: 14px;
    color: var(--text-muted);
}

#model-select {
    background: var(--bg-main);
    color: var(--text-light);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;
    font-size: 14px;
}

.connection-status {
    margin-left: auto;
    font-size: 13px;
    color: var(--danger);
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 0;
}

.empty-state {
    color: var(--text-muted);
    text-align: center;
    margin-top: 60px;
    font-size: 15px;
}

.message-row {
    padding: 20px 15%;
}

.message-row.user {
    background: var(--bg-user-bubble);
}

.message-row.assistant {
    background: var(--bg-assistant-bubble);
}

.message-role {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.message-content {
    white-space: pre-wrap;
    word-wrap: break-word;
    line-height: 1.6;
    font-size: 15px;
}

.message-content.error {
    color: var(--danger);
}

.typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--text-muted);
    font-size: 13px;
}

.typing-indicator .hal-eye {
    --eye-size: 14px;
}

/* --- Input form --- */

.chat-form {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    padding: 15px 15%;
    padding-bottom: max(15px, env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.message-input {
    flex: 1;
    resize: none;
    background: var(--bg-user-bubble);
    color: var(--text-light);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px 14px;
    /* 16px minimum : évite le zoom automatique de Safari iOS au focus du champ. */
    font-size: 16px;
    font-family: inherit;
    max-height: 200px;
}

.message-input:focus {
    outline: none;
    border-color: var(--accent);
}

.send-btn {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 10px;
    padding: 0 20px;
    min-height: 44px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
    background: var(--accent-hover);
}

.send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

@media (max-width: 768px) {
    .message-row {
        padding: 16px 5%;
    }
    .chat-form {
        padding: 12px 5%;
        padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
    }

    .menu-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 40px;
        min-height: 40px;
    }

    .topbar {
        padding: 10px 12px;
        padding-top: max(10px, env(safe-area-inset-top, 0px));
    }

    .model-label {
        display: none; /* "Modèle :" prend trop de place à côté du menu burger sur petit écran */
    }

    /* La sidebar devient un panneau escamotable au-dessus du contenu, plutôt
       qu'une colonne qui grignote l'espace du chat sur un écran étroit. */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 50;
        width: 82%;
        max-width: 300px;
        height: 100%;
        height: 100dvh;
        box-shadow: 2px 0 16px rgba(0, 0, 0, 0.5);
        transform: translateX(-100%);
        transition: transform 0.25s ease;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-close-btn {
        display: inline-block;
    }

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 40;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
    }

    .sidebar-backdrop.visible {
        opacity: 1;
        pointer-events: auto;
    }

    .conversation-item,
    .conversation-delete {
        min-height: 40px;
    }
}
