/* ==========================================================================
   ZONE DE CHAT
   ========================================================================== */
.chat-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    position: relative;
}

.chat-panel.avec-panneau {
    flex: 1;
    min-width: 400px;
}

.tool-panel {
    flex: 0 0 45%;
    background: var(--fond-secondaire);
    border-left: 1px solid var(--bordure);
    display: none;
    flex-direction: column;
    min-width: 350px;
    max-width: 70%;
    position: relative;
}

.panel-resizer {
    width: 4px;
    background: var(--bordure);
    cursor: col-resize;
    position: absolute;
    left: -2px;
    top: 0;
    bottom: 0;
    z-index: 10;
    transition: background-color 0.2s;
}

.panel-resizer:hover {
    background: var(--accent);
}

.panel-resizer.dragging {
    background: var(--accent);
}

.content-layout.resizing {
    cursor: col-resize;
    user-select: none;
}

.tool-panel-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--bordure);
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--fond-principal);
}

.tool-panel-tabs {
    display: flex;
    gap: 4px;
}

.tool-tab {
    padding: 8px 12px;
    border: 1px solid var(--bordure);
    border-radius: var(--rayon);
    background: var(--fond-principal);
    color: var(--texte-secondaire);
    font-size: 12px;
    cursor: pointer;
    transition: var(--transition);
}

.tool-tab:hover {
    background: var(--fond-tertiaire);
}

.tool-tab.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.tool-panel-title {
    flex: 1;
    font-weight: 500;
    font-size: 14px;
}

.tool-panel-actions {
    display: flex;
    gap: 8px;
}

.btn-close-panel {
    width: 24px;
    height: 24px;
    border-radius: 6px;
    border: 1px solid var(--bordure);
    background: var(--fond-principal);
    color: var(--texte-secondaire);
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
}

.btn-close-panel:hover {
    background: var(--fond-tertiaire);
    color: var(--accent);
    border-color: var(--accent);
}

.tool-panel-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.chat-zone {
    /* ===== NOUVEAU: CSS Grid 3 lignes fixes ===== */
    display: grid;
    grid-template-rows: auto 1fr auto;  /* header fixe / messages scroll / input fixe */
    grid-template-columns: 1fr;
    height: 100%;
    position: relative;
    background: var(--fond-principal);
    border: 1px solid var(--bordure);
    border-radius: var(--rayon);
    min-height: 0;
    /* overflow: hidden pour border-radius uniquement */
    overflow: hidden;
}

.chat-zone::before, .chat-zone::after {
    content: '';
    position: absolute;
    left: 0; right: 0; top: 0; bottom: 0;
    background: var(--fond-principal);
    border: 1px solid var(--bordure);
    border-radius: var(--rayon);
    transition: var(--transition);
}
.chat-zone::before { z-index: -1; transform: rotate(-1.5deg); }
.chat-zone::after { z-index: -2; transform: rotate(1deg); }

.chat-header {
    /* ===== Grid row 1: Header TOUJOURS visible ===== */
    grid-row: 1;
    padding: 6px 10px;
    border-bottom: 1px solid var(--bordure);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--fond-principal);
    z-index: 10;
    /* Plus besoin de position: sticky avec Grid */
}
.chat-title-container {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    max-width: 60%;
    margin-left: 0;
}
.chat-title {
    font-size: 16px;
    font-weight: 500;
    background: transparent;
    border: none;
    color: var(--texte-principal);
    outline: none;
    cursor: text;
    padding: 4px 8px;
    border-radius: var(--rayon);
    transition: var(--transition);
    width: 100%;
    min-width: 200px;
}
.chat-title:hover, .chat-title:focus {
    background: var(--fond-tertiaire);
    box-shadow: inset 0 0 0 1px var(--bordure);
}
.chat-title::placeholder {
    color: var(--texte-tertiaire);
    font-style: italic;
}
.chat-title:focus::placeholder {
    opacity: 0;
}
.chat-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    position: relative;
    flex-shrink: 0;
}

/* Harmonisation de la taille des boutons dans les actions du chat */
.chat-actions .btn-icon {
    width: 28px;
    height: 28px;
    border-radius: 6px;
    font-size: 12px;
}
.messages-container {
    /* ===== Grid row 2: Messages SEULE zone scrollable ===== */
    grid-row: 2;
    overflow-y: auto;
    overflow-x: hidden;
    /* IMPORTANT: display block pour que scrollHeight reflète contenu enfant */
    display: block;
    min-height: 0;  /* Crucial pour Grid shrinking */
    position: relative;
}
.messages-wrapper {
    /* IMPORTANT: display block au lieu de flex pour que container voie débordement */
    display: block;
    padding: 12px 24px 24px 24px;
    max-width: 100%;
    overflow-x: hidden;
}

/* Espace en bas pour scroller dernier message en haut */
.messages-wrapper::after {
    content: '';
    display: block;
    height: 80vh;
    flex-shrink: 0;
}
.messages-wrapper.centrer {
    justify-content: center;
}
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 400px;
    text-align: center;
    color: var(--texte-tertiaire);
    padding: 60px 20px;
}
.empty-state i {
    font-size: 64px;
    margin-bottom: 24px;
    color: var(--accent);
    opacity: 0.3;
}
.empty-state h3 {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 12px;
    color: var(--texte-secondaire);
}
.empty-state p {
    font-size: 15px;
    line-height: 1.6;
    max-width: 300px;
}
.message-date-separateur {
    margin-bottom: 8px;
    margin-top: 8px;
    text-align: center;
    font-size: 11px;
    color: var(--texte-tertiaire);
}

.message-date-separateur:first-child {
    margin-top: 0;
}

/* Bouton flottant pour tool panel - caché par défaut sur desktop */
.btn-show-tool-panel {
    display: none;
}

/* ============================================
   RESPONSIVE MOBILE
   ============================================ */
@media (max-width: 768px) {
    .messages-container {
        padding: 0 !important;
    }

    .messages-wrapper {
        padding: 4px 0 16px 0;
    }

    .chat-header {
        padding: 4px 8px 4px 52px; /* 52px à gauche pour éviter le bouton toggle */
    }

    .chat-zone {
        border-radius: 0;
        border: none;
    }

    .chat-zone::before,
    .chat-zone::after {
        display: none;
    }

    .empty-state {
        padding: 40px 16px;
        min-height: 300px;
    }

    .empty-state i {
        font-size: 48px;
        margin-bottom: 16px;
    }

    .empty-state h3 {
        font-size: 18px;
    }

    /* Tool panel mobile: overlay slide-in depuis la droite */
    .tool-panel {
        position: fixed !important;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        z-index: 1000;
        flex: none !important;
        transform: translateX(100%);
        transition: transform 0.3s ease;
        border-left: none;
    }

    .tool-panel[style*="display: flex"],
    .tool-panel[style*="display:flex"],
    .tool-panel.visible {
        transform: translateX(0) !important;
    }

    .tool-panel-header {
        padding: 10px 12px;
        gap: 8px;
    }

    .tool-panel-tabs {
        flex-wrap: nowrap;
        overflow-x: auto;
        max-width: 60%;
    }

    .tool-tab {
        padding: 6px 8px;
        font-size: 11px;
        white-space: nowrap;
    }

    .tool-panel-title {
        display: none;
    }

    .btn-close-panel {
        width: 32px;
        height: 32px;
        font-size: 16px;
    }

    .tool-panel-content {
        padding: 8px;
    }

    /* Bouton pour afficher le tool panel sur mobile */
    .btn-show-tool-panel {
        position: fixed;
        bottom: 100px;
        right: 16px;
        z-index: 900;
        width: 48px;
        height: 48px;
        border-radius: 50%;
        background: var(--accent);
        color: white;
        border: none;
        box-shadow: var(--ombre-moyenne);
        cursor: pointer;
        display: none;
        align-items: center;
        justify-content: center;
        font-size: 18px;
    }

    .btn-show-tool-panel.has-content {
        display: flex;
        animation: pulse 2s infinite;
    }
}