/* ==========================================================================
   EMTOP — Listes déroulantes avec champ de recherche (contrôleur Stimulus « liste-recherche »)
   Le bouton reprend les classes du <select> d'origine (form-select, is-invalid…) : même apparence
   que les autres champs dans chaque module. Classes préfixées « lr- ».
   ========================================================================== */

/* <select> d'origine : masqué visuellement mais toujours envoyé avec le formulaire */
select.lr-natif {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    min-height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    opacity: 0;
    overflow: hidden;
    clip-path: inset(50%);
    pointer-events: none;
}

.lr { position: relative; width: 100%; min-width: 0; }

.lr-bouton {
    display: flex;
    align-items: center;
    width: 100%;
    text-align: left;
    cursor: pointer;
}

.lr-bouton:disabled { cursor: not-allowed; }
.lr-bouton.is-vide .lr-valeur { color: var(--em-muted); }
.lr.is-ouverte .lr-bouton { border-color: rgba(var(--em-primary-rgb), .6); box-shadow: 0 0 0 3px rgba(var(--em-primary-rgb), .12); }
.lr-valeur { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Panneau : recherche + options */
.lr-panneau {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    z-index: 1080;
    display: flex;
    flex-direction: column;
    width: 100%;
    min-width: 260px;
    max-width: calc(100vw - 24px);
    overflow: hidden;
    border: 1px solid var(--em-border);
    border-radius: 12px;
    background: var(--em-card-bg);
    box-shadow: 0 16px 40px -12px rgba(16, 24, 40, .28);
}

.lr-panneau[hidden] { display: none; }
.lr.is-haut .lr-panneau { top: auto; bottom: calc(100% + 6px); }
.lr.is-droite .lr-panneau { right: 0; left: auto; }

.lr-recherche {
    position: relative;
    padding: 10px;
    border-bottom: 1px solid var(--em-border);
}

.lr-recherche .em-icon {
    position: absolute;
    top: 50%;
    left: 22px;
    width: 17px;
    height: 17px;
    color: var(--em-muted);
    transform: translateY(-50%);
    pointer-events: none;
}

.lr-panneau input.lr-champ {
    min-height: 40px;
    padding-left: 38px;
    border-radius: 8px;
    font-size: .9375rem;
}

.lr-options {
    max-height: 280px;
    margin: 0;
    padding: 6px;
    overflow-y: auto;
    list-style: none;
    overscroll-behavior: contain;
}

.lr-option {
    padding: 9px 12px;
    border-radius: 8px;
    font-size: .9375rem;
    line-height: 1.35;
    color: var(--em-heading-color);
    overflow-wrap: anywhere;
    cursor: pointer;
    transition: background-color .15s ease, color .15s ease;
}

.lr-option.is-choisie { font-weight: 600; color: var(--em-primary); }
.lr-option.is-vide { color: var(--em-muted); }
.lr-option.is-desactivee { opacity: .5; cursor: not-allowed; }
.lr-option mark { padding: 0; border-radius: 2px; background: rgba(var(--em-warning-rgb), .45); color: inherit; }

.lr-groupe {
    padding: 10px 12px 4px;
    font-size: .6875rem;
    font-weight: 700;
    color: var(--em-muted);
    text-transform: uppercase;
    letter-spacing: .05em;
}

.lr-info { margin: 0; padding: 10px 14px 12px; font-size: .8125rem; color: var(--em-muted); text-align: center; }
.lr-info[hidden] { display: none; }

/* --- Survol des options -----------------------------------------------------------------
   Le fond bleu clair suit le curseur de la souris (:hover). Au clavier (↑ ↓, saisie d'une recherche), le
   contrôleur ajoute « is-clavier » sur la liste : le même bleu suit alors l'option active. Une seule option
   est en bleu à la fois ; l'option sélectionnée se repère à son texte bleu en gras. */
:root {
    --lr-survol-fond: rgba(var(--em-primary-rgb), .14); /* bleu clair du thème (#deeefb sur fond blanc) */
    --lr-survol-texte: #105e9b;                         /* bleu foncé : contraste 5,2:1 */
}

html[data-theme="dark"] {
    --lr-survol-fond: rgba(var(--em-primary-rgb), .22);
    --lr-survol-texte: #8cc6f4;
}

.lr-options:not(.is-clavier) .lr-option:not(.is-desactivee):hover,
.lr-options.is-clavier .lr-option.is-active:not(.is-desactivee) {
    background: var(--lr-survol-fond);
    color: var(--lr-survol-texte);
}

/* Selects natifs non transformés (multi-sélection, taille fixe, data-recherche="non") */
select option { cursor: pointer; }

select option:not(:disabled):not(:checked):hover {
    background-color: var(--lr-survol-fond);
    color: var(--lr-survol-texte);
}

@media (max-width: 767.98px) {
    .lr-options { max-height: 45vh; }
    .lr-option { padding: 11px 12px; }
}
/* ==========================================================================
   Listes chargées à la demande (contrôleur Stimulus « liste-distante ») : mêmes classes lr-* que ci-dessus,
   plus le pied « ld- » (chargement, nombre, aucun résultat, Voir plus) et le détail de chaque ligne.
   ========================================================================== */
.ld .lr-options[hidden] { display: none; }
.ld .lr-options { max-height: min(330px, 45vh); } /* un lot de 5 lignes (libellé + contact) visible en entier */
.ld .lr-option { display: flex; flex-direction: column; gap: 1px; }
.ld-libelle { min-width: 0; }
.ld-detail { font-size: .8125rem; color: var(--em-muted); }
.lr-options:not(.is-clavier) .lr-option:hover .ld-detail,
.lr-options.is-clavier .lr-option.is-active .ld-detail { color: inherit; opacity: .8; }

/* Recherche en cours : lignes précédentes grisées et inactives jusqu'à l'arrivée des résultats */
.lr-options.is-obsolete { opacity: .5; pointer-events: none; transition: opacity .15s ease; }

.ld-pied {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 44px;
    padding: 6px 10px 6px 14px;
    border-top: 1px solid var(--em-border);
}

.ld .lr-recherche + .lr-options[hidden] + .ld-pied { border-top: 0; }
.ld-etat { display: flex; align-items: center; gap: 8px; min-width: 0; margin: 0; font-size: .8125rem; color: var(--em-muted); }
.ld-pied.is-vide .ld-etat { flex: 1; justify-content: center; padding: 8px 0; font-size: .875rem; font-weight: 500; }
.ld-pied.is-erreur .ld-etat { color: var(--em-danger); }

.ld-spinner {
    display: none;
    flex-shrink: 0;
    width: 15px;
    height: 15px;
    border: 2px solid rgba(var(--em-primary-rgb), .25);
    border-top-color: var(--em-primary);
    border-radius: 50%;
    animation: ld-tourner .7s linear infinite;
}

.ld-pied.is-chargement .ld-spinner { display: inline-block; }

.ld-plus {
    flex-shrink: 0;
    padding: 5px 12px;
    border: 1px solid rgba(var(--em-primary-rgb), .35);
    border-radius: var(--em-radius-pill);
    background: transparent;
    font-size: .8125rem;
    font-weight: 600;
    color: var(--em-primary);
    cursor: pointer;
    transition: background-color .15s ease;
}

.ld-plus:hover { background: rgba(var(--em-primary-rgb), .1); }
.ld-plus[hidden] { display: none; }

@keyframes ld-tourner { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
    .ld-spinner { animation-duration: 1.6s; }
}
