/* =========================================================================
   Messagerie interne — écran « Messages ».

   ⚠️ TOUT EST PRÉFIXÉ `.msg-`. Cette feuille n'est chargée que par
   `messages.html`, mais le préfixe reste obligatoire : la page hérite de
   `style.css`, et une règle comme `.liste { … }` y entrerait en collision
   silencieusement — le défaut apparaîtrait sur un autre écran.

   ⚠️ DEUX COLONNES, PAS UN TABLEAU.
   Un tableau de messages se lit comme un journal : on voit tout, on ne suit
   aucune conversation. Ici la colonne de gauche liste les CONVERSATIONS et
   celle de droite affiche le FIL complet — c'est la forme qui rend une
   réponse compréhensible sans remonter la page.

   ⚠️ LA COULEUR D'ACCENT EST CELLE DE LA SOCIÉTÉ (`--societe-couleur`).
   L'écran doit ressembler au reste de l'application, y compris quand le
   gestionnaire change la palette depuis les Paramètres.
   ========================================================================= */

.msg-page {
    display: grid;
    /* ⚠️ `minmax(0, …)` SUR LES DEUX COLONNES. Sans le `0`, une longue
       adresse ou un objet sans espaces force la colonne au-delà de sa part,
       et le corps du message déborde de l'écran au lieu de passer à la ligne. */
    grid-template-columns: minmax(0, 340px) minmax(0, 1fr);
    gap: 1rem;
    /* La hauteur est calculée sur la fenêtre, moins la barre du haut et les
       marges : sinon la colonne de gauche s'allonge avec le contenu et la
       page entière défile, ce qui fait perdre la liste de vue. */
    height: calc(100vh - 210px);
    min-height: 420px;
}

/* ==================== COLONNE DE GAUCHE : LA LISTE ==================== */

.msg-panneau {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    overflow: hidden;
}

.msg-entete {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .7rem .8rem .55rem;
    flex: 0 0 auto;
}

.msg-titre {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-color);
}

.msg-titre-pastille {
    display: inline-block;
    min-width: 19px;
    margin-left: .35rem;
    padding: 0 5px;
    border-radius: 10px;
    background: #e05563;
    color: #fff;
    font-size: .66rem;
    font-weight: 700;
    line-height: 19px;
    text-align: center;
    vertical-align: 2px;
}

.msg-titre-pastille[hidden] {
    display: none;
}

.msg-nouveau {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: .3rem;
    padding: .4rem .7rem;
    border: 1px solid var(--primary-color);
    border-radius: 8px;
    background: var(--primary-color);
    color: #fff;
    font-family: inherit;
    font-size: .76rem;
    font-weight: 700;
    cursor: pointer;
    transition: background .15s, transform .1s;
}

.msg-nouveau:hover {
    background: var(--primary-hover);
}

.msg-nouveau:active {
    transform: scale(.97);
}

.msg-nouveau:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.msg-filtres {
    display: flex;
    align-items: center;
    gap: .4rem;
    padding: 0 .8rem .55rem;
    flex: 0 0 auto;
    flex-wrap: wrap;
}

.msg-segments {
    display: flex;
    gap: .12rem;
    padding: .14rem;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: 9px;
}

.msg-segment {
    padding: .3rem .55rem;
    border: none;
    border-radius: 7px;
    background: none;
    color: var(--text-muted);
    font-family: inherit;
    font-size: .72rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s, color .15s;
}

.msg-segment:hover {
    background: var(--bg-hover);
    color: var(--text-color);
}

.msg-segment.msg-actif {
    background: var(--primary-color);
    color: #fff;
}

.msg-segment:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 1px;
}

.msg-recherche {
    flex: 1 1 110px;
    min-width: 90px;
    padding: .33rem .55rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-input);
    color: var(--text-color);
    font-family: inherit;
    font-size: .74rem;
}

.msg-recherche:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: -1px;
}

/* ---------- Liste des conversations ---------- */

.msg-liste {
    flex: 1 1 auto;
    /* ⚠️ `min-height: 0` : dans une colonne flex, un enfant refuse de
       rétrécir sous la taille de son contenu. Sans cette ligne, il n'y a pas
       de défilement — la liste pousse le panneau et la page déborde. */
    min-height: 0;
    overflow-y: auto;
    border-top: 1px solid var(--border-color);
}

.msg-item {
    display: grid;
    /* Pastille de non-lu · avatar · contenu · date */
    grid-template-columns: 8px 34px minmax(0, 1fr) auto;
    align-items: center;
    gap: .5rem;
    width: 100%;
    padding: .55rem .8rem;
    border: none;
    border-bottom: 1px solid var(--border-color);
    background: none;
    font-family: inherit;
    text-align: left;
    cursor: pointer;
    transition: background .13s;
}

.msg-item:hover {
    background: var(--bg-hover);
}

.msg-item.msg-selectionne {
    background: rgba(77, 163, 255, .13);
    box-shadow: inset 3px 0 0 var(--primary-color);
}

.msg-item:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: -2px;
}

.msg-point {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: transparent;
}

.msg-item.msg-non-lu .msg-point {
    background: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(77, 163, 255, .18);
}

.msg-avatar {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: rgba(var(--msg-teinte, 77, 163, 255), .22);
    color: var(--text-color);
    font-size: .72rem;
    font-weight: 800;
    letter-spacing: -.02em;
    flex: 0 0 auto;
}

.msg-item-corps {
    min-width: 0;
}

.msg-item-ligne {
    display: flex;
    align-items: baseline;
    gap: .4rem;
}

.msg-item-nom {
    flex: 1;
    min-width: 0;
    font-size: .78rem;
    font-weight: 600;
    color: var(--text-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-item.msg-non-lu .msg-item-nom {
    font-weight: 800;
}

.msg-item-heure {
    flex: 0 0 auto;
    font-size: .66rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.msg-item-objet {
    margin-top: .1rem;
    font-size: .74rem;
    color: var(--text-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-item.msg-non-lu .msg-item-objet {
    font-weight: 700;
}

.msg-item-extrait {
    margin-top: .1rem;
    font-size: .7rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-item-droite {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: .2rem;
}

.msg-compte {
    min-width: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: var(--bg-hover);
    color: var(--text-muted);
    font-size: .64rem;
    font-weight: 700;
    line-height: 18px;
    text-align: center;
}

.msg-compte.msg-compte-nu {
    background: var(--primary-color);
    color: #fff;
}

/* Bandeau de priorité : une urgence doit se voir sans ouvrir la conversation. */
.msg-urgence {
    display: inline-block;
    padding: 0 .35rem;
    border-radius: 5px;
    background: rgba(224, 85, 99, .2);
    color: #ff8a96;
    font-size: .6rem;
    font-weight: 800;
    letter-spacing: .04em;
    text-transform: uppercase;
}

/* ==================== COLONNE DE DROITE : LA CONVERSATION ==================== */

.msg-lecture {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    overflow: hidden;
}

.msg-lecture-vide {
    display: grid;
    place-content: center;
    justify-items: center;
    gap: .5rem;
    flex: 1 1 auto;
    padding: 2rem 1.5rem;
    text-align: center;
    color: var(--text-muted);
}

.msg-lecture-vide[hidden] {
    display: none;
}

.msg-lecture-vide-titre {
    font-size: 2.1rem;
    line-height: 1;
    opacity: .5;
}

.msg-lecture-vide-texte {
    font-size: .82rem;
    max-width: 300px;
}

.msg-fil {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
}

.msg-fil[hidden] {
    display: none;
}

.msg-fil-tete {
    flex: 0 0 auto;
    padding: .75rem .9rem .6rem;
    border-bottom: 1px solid var(--border-color);
    background:
        linear-gradient(0deg, rgba(var(--societe-rgb, 77, 163, 255), .07),
                               rgba(var(--societe-rgb, 77, 163, 255), .07)),
        var(--bg-card);
}

.msg-fil-objet {
    margin: 0 0 .25rem;
    font-size: .95rem;
    font-weight: 700;
    color: var(--text-color);
    overflow-wrap: anywhere;
}

.msg-fil-meta {
    display: flex;
    align-items: center;
    gap: .5rem;
    flex-wrap: wrap;
    font-size: .7rem;
    color: var(--text-muted);
}

.msg-fil-actions {
    display: flex;
    gap: .25rem;
    margin-left: auto;
}

.msg-icone-action {
    display: inline-flex;
    align-items: center;
    gap: .25rem;
    padding: .24rem .5rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: none;
    color: var(--text-muted);
    font-family: inherit;
    font-size: .7rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s, color .15s;
}

.msg-icone-action:hover {
    background: var(--bg-hover);
    color: var(--text-color);
}

.msg-icone-action.msg-danger:hover {
    background: rgba(224, 85, 99, .18);
    color: #ff8a96;
    border-color: rgba(224, 85, 99, .4);
}

.msg-icone-action:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 1px;
}

/* ---------- Les messages du fil ---------- */

.msg-fil-corps {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: .85rem .9rem;
    display: flex;
    flex-direction: column;
    gap: .7rem;
}

.msg-bulle {
    max-width: min(560px, 88%);
    padding: .55rem .7rem;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    background: var(--bg-input);
}

/* ⚠️ MES MESSAGES À DROITE, CEUX DE L'AUTRE À GAUCHE.
   L'alignement n'est pas décoratif : il évite de confondre ce qu'on a écrit
   avec ce qu'on a reçu au moment de relire une conversation longue. */
.msg-bulle.msg-sortant {
    align-self: flex-end;
    background:
        linear-gradient(0deg, rgba(77, 163, 255, .14), rgba(77, 163, 255, .14)),
        var(--bg-input);
    border-color: rgba(77, 163, 255, .35);
}

.msg-bulle-tete {
    display: flex;
    align-items: baseline;
    gap: .45rem;
    margin-bottom: .2rem;
    font-size: .68rem;
    color: var(--text-muted);
}

.msg-bulle-auteur {
    font-weight: 700;
    color: var(--text-color);
}

.msg-bulle-date {
    margin-left: auto;
    font-variant-numeric: tabular-nums;
}

.msg-bulle-texte {
    font-size: .81rem;
    line-height: 1.55;
    color: var(--text-color);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.msg-bulle-etat {
    margin-top: .3rem;
    font-size: .66rem;
    color: var(--text-muted);
    text-align: right;
}

.msg-bulle-etat.msg-lu {
    color: var(--success-color);
}

/* ---------- Zone de réponse ---------- */

.msg-reponse {
    flex: 0 0 auto;
    display: grid;
    gap: .4rem;
    padding: .6rem .9rem .8rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-card);
}

.msg-reponse[hidden] {
    display: none;
}

.msg-reponse-champ {
    width: 100%;
    min-height: 64px;
    max-height: 30vh;
    padding: .5rem .6rem;
    border: 1px solid var(--border-color);
    border-radius: 9px;
    background: var(--bg-input);
    color: var(--text-color);
    font-family: inherit;
    font-size: .8rem;
    line-height: 1.5;
    resize: vertical;
}

.msg-reponse-champ:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: -1px;
}

.msg-reponse-pied {
    display: flex;
    align-items: center;
    gap: .45rem;
}

.msg-aide {
    font-size: .68rem;
    color: var(--text-muted);
    margin-right: auto;
}

.msg-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .3rem;
    padding: .42rem .85rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-input);
    color: var(--text-color);
    font-family: inherit;
    font-size: .78rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s, transform .1s;
}

.msg-btn:hover {
    background: var(--bg-hover);
}

.msg-btn:active {
    transform: scale(.97);
}

.msg-btn:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.msg-btn.msg-principal {
    background: var(--primary-color);
    border-color: var(--primary-color);
    color: #fff;
}

.msg-btn.msg-principal:hover {
    background: var(--primary-hover);
}

.msg-btn[disabled] {
    opacity: .5;
    cursor: not-allowed;
}

/* ==================== MODALE DE COMPOSITION ==================== */

.msg-modale {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: grid;
    place-items: center;
    padding: 1rem;
    background: rgba(0, 0, 0, .55);
}

.msg-modale[hidden] {
    display: none;
}

.msg-modale-boite {
    display: flex;
    flex-direction: column;
    width: min(560px, 100%);
    max-height: calc(100vh - 2rem);
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    box-shadow: 0 24px 60px rgba(0, 0, 0, .55);
    overflow: hidden;
    animation: msgApparait .16s ease-out;
}

@keyframes msgApparait {
    from { opacity: 0; transform: translateY(-8px) scale(.99); }
    to { opacity: 1; transform: none; }
}

.msg-modale-tete {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .7rem .9rem;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-input);
}

.msg-modale-titre {
    flex: 1;
    font-size: .9rem;
    font-weight: 700;
    color: var(--text-color);
}

.msg-fermer {
    display: grid;
    place-items: center;
    width: 26px;
    height: 26px;
    padding: 0;
    border: 1px solid transparent;
    border-radius: 6px;
    background: none;
    color: var(--text-muted);
    font-family: inherit;
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
}

.msg-fermer:hover {
    background: rgba(224, 85, 99, .2);
    color: #ff8a96;
    border-color: rgba(224, 85, 99, .45);
}

.msg-modale-corps {
    display: grid;
    gap: .6rem;
    padding: .8rem .9rem;
    overflow-y: auto;
    min-height: 0;
}

.msg-champ {
    display: grid;
    gap: .22rem;
}

.msg-etiquette {
    font-size: .68rem;
    font-weight: 700;
    letter-spacing: .04em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.msg-saisie,
.msg-liste-saisie {
    width: 100%;
    padding: .42rem .6rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-input);
    color: var(--text-color);
    font-family: inherit;
    font-size: .8rem;
}

.msg-saisie:focus,
.msg-liste-saisie:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: -1px;
}

.msg-zone {
    width: 100%;
    min-height: 130px;
    padding: .5rem .6rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-input);
    color: var(--text-color);
    font-family: inherit;
    font-size: .8rem;
    line-height: 1.55;
    resize: vertical;
}

.msg-zone:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: -1px;
}

.msg-modale-pied {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .7rem .9rem .8rem;
    border-top: 1px solid var(--border-color);
}

.msg-modale-pied .msg-aide {
    margin-right: auto;
}

/* ==================== ÉTATS & NOTIFICATIONS ==================== */

.msg-vide {
    padding: 1.8rem 1rem;
    text-align: center;
    font-size: .78rem;
    color: var(--text-muted);
}

.msg-vide[hidden] {
    display: none;
}

/* Bandeau de retour, en haut de la liste : confirmation d'envoi ou erreur.
   ⚠️ IL N'EST PAS UNE ALERTE FLOTTANTE QUI DISPARAÎT : un envoi échoué doit
   rester lisible le temps qu'on comprenne pourquoi. */
.msg-retour {
    margin: 0 .8rem .5rem;
    padding: .4rem .6rem;
    border-radius: 8px;
    font-size: .74rem;
    font-weight: 600;
}

.msg-retour[hidden] {
    display: none;
}

.msg-retour.msg-ok {
    background: rgba(60, 179, 113, .16);
    color: var(--success-color);
    border: 1px solid rgba(60, 179, 113, .4);
}

.msg-retour.msg-erreur {
    background: rgba(224, 85, 99, .16);
    color: #ff8a96;
    border: 1px solid rgba(224, 85, 99, .4);
}

/* ==================== RESPONSIVE ==================== */

@media (max-width: 900px) {
    /* ⚠️ EN DESSOUS DE 900 px, LA COLONNE DE LECTURE PASSE SOUS LA LISTE.
       Deux colonnes de 340 px ne tiennent pas : la liste serait réduite à
       une bande de 200 px où aucun objet ne se lit. On empile, et la page
       reprend un défilement normal — d'où la hauteur automatique. */
    .msg-page {
        grid-template-columns: minmax(0, 1fr);
        height: auto;
    }

    .msg-liste {
        max-height: 46vh;
    }

    .msg-lecture {
        min-height: 320px;
    }
}
