/* =====================================================================
   SÉLECTEUR DE MATÉRIEL — js/material-picker.js
   ---------------------------------------------------------------------
   Deux morceaux :
     .mp-champ        le bouton qui remplace un <select> de matériel
                      (n'importe où dans le CRM, hors ou dans une modale) ;
     #modal-mat-picker la fenêtre de choix : type → marque → recherche.

   La fenêtre parle le vocabulaire .mm-* de css/modal-machine.css (feuille
   chargée avant celle-ci) : en-tête, cartes de verre, pastilles, pied.
   Aucune couleur en dur : jetons --t3-* du thème, avec valeurs de secours.
   ===================================================================== */

/* ── Le champ qui remplace un <select> ──────────────────────────────── */
/* Le <select> reste dans le formulaire (valeur, required, scripts de la
   page) mais ne se voit plus ; son éventuel habillage Chosen non plus. */
select.mp-natif {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    border: 0 !important;
    opacity: 0 !important;
    pointer-events: none !important;
}
select.mp-natif ~ .chosen-container { display: none !important; }

.mp-champ {
    position: relative;
    display: flex;
    align-items: stretch;
    width: 100%;
    min-width: 0;
}

.mp-champ-btn {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    gap: 9px;
    min-width: 0;
    min-height: 36px;
    padding: 6px 10px 6px 11px;
    text-align: left;
    font-size: 13.5px;
    line-height: 1.3;
    color: var(--t3-text, #131a2e);
    background: var(--t3-glass-hi, rgba(255, 255, 255, 0.78));
    border: 1px solid var(--t3-border-sub, rgba(19, 26, 46, 0.14));
    border-radius: var(--t3-radius-sm, 8px);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5);
    backdrop-filter: blur(10px) saturate(150%);
    -webkit-backdrop-filter: blur(10px) saturate(150%);
    cursor: pointer;
    transition: border-color .2s, box-shadow .2s, transform .2s;
}
.mp-champ-btn:hover { border-color: color-mix(in srgb, var(--t3-accent, #2563eb) 45%, transparent); }
.mp-champ-btn:focus-visible {
    outline: none;
    border-color: var(--t3-accent, #2563eb);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--t3-accent, #2563eb) 20%, transparent);
}
.mp-champ-btn > .ti-package { flex: 0 0 auto; font-size: 16px; color: var(--t3-accent, #2563eb); }
.mp-champ-btn > .ti-chevron-down { flex: 0 0 auto; margin-left: auto; color: var(--t3-text-3, #5f6883); }
.mp-champ-txt { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.mp-champ.is-vide .mp-champ-txt { font-weight: 400; color: var(--t3-text-3, #5f6883); }
.mp-champ.is-bloque .mp-champ-btn { opacity: .6; cursor: not-allowed; }

.mp-champ-vider {
    flex: 0 0 auto;
    margin-left: 6px;
    width: 34px;
    border: 1px solid var(--t3-border-sub, rgba(19, 26, 46, 0.14));
    border-radius: var(--t3-radius-sm, 8px);
    background: transparent;
    color: var(--t3-text-3, #5f6883);
    cursor: pointer;
}
.mp-champ-vider:hover { color: #dc2626; border-color: color-mix(in srgb, #dc2626 40%, transparent); }
.mp-champ.is-vide .mp-champ-vider { display: none; }

body.v2-dark .mp-champ-btn { box-shadow: none; }

/* Dans l'en-tête d'une grille (filtre « matériel ») : compact, la croix de la
   grille efface déjà le filtre. */
.jdgridtb thead .mp-champ-btn { min-height: 30px; padding: 3px 7px; font-size: 12px; gap: 5px; backdrop-filter: none; -webkit-backdrop-filter: none; }
.jdgridtb thead .mp-champ-btn > .ti-package { font-size: 14px; }
.jdgridtb thead .mp-champ-vider { display: none; }
.jdgridtb thead .mp-champ.is-vide .mp-champ-txt { font-size: 0; }
.jdgridtb thead .mp-champ.is-vide .mp-champ-txt::after { content: "Tous"; font-size: 12px; }


/* ── La fenêtre ─────────────────────────────────────────────────────── */
/* Au-dessus des autres modales (.modal est à 10001 dans main.css) : le
   sélecteur s'ouvre presque toujours depuis une fenêtre déjà ouverte. */
#modal-mat-picker { z-index: 10060; }
.modal-backdrop.mp-voile { z-index: 10055; }

#modal-mat-picker.mm-modal .modal-dialog { max-width: 820px; }

#modal-mat-picker .mp-sous-titre { margin: 2px 0 0; font-size: 12.5px; color: var(--mm-texte-3); }

#modal-mat-picker .mp-etape {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    font-size: 11px;
    font-weight: 700;
    color: var(--mm-brand-on);
    background: var(--mm-accent);
}

#modal-mat-picker .mm-pastille { padding: 6px 12px 6px 10px; font-size: 12.5px; }
#modal-mat-picker .mm-pastille small { font-weight: 500; opacity: .65; }
#modal-mat-picker .mm-pastille.is-vide { opacity: .45; }

/* Marques : deux rangées, le reste derrière « Toutes les marques » */
#modal-mat-picker .mp-marques { max-height: 70px; overflow: hidden; transition: max-height .35s var(--mm-courbe); }
#modal-mat-picker .mp-marques.is-ouvert { max-height: 260px; overflow-y: auto; }
#modal-mat-picker .mp-lien {
    align-self: flex-start;
    padding: 0;
    border: 0;
    background: none;
    font-size: 12px;
    font-weight: 600;
    color: var(--mm-accent);
    cursor: pointer;
}
#modal-mat-picker .mp-lien:hover { text-decoration: underline; }

#modal-mat-picker .mp-recherche { position: relative; }
#modal-mat-picker .mp-recherche > .ti-search {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--mm-texte-3);
    pointer-events: none;
}
#modal-mat-picker .mp-recherche .form-control { padding-left: 36px; height: 40px; font-size: 14px; }

#modal-mat-picker .mp-compte { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--mm-texte-3); }
#modal-mat-picker .mp-compte strong { color: var(--mm-texte); }
#modal-mat-picker .mp-effacer { margin-left: auto; }

#modal-mat-picker .mp-resultats {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 44vh;
    min-height: 120px;
    overflow-y: auto;
    padding: 2px 4px 2px 2px;
}
#modal-mat-picker .mp-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 8px 12px;
    text-align: left;
    color: var(--mm-texte);
    background: var(--mm-verre-hi);
    border: 1px solid var(--mm-trait);
    border-radius: 12px;
    cursor: pointer;
    transition: transform .2s var(--mm-courbe), border-color .2s var(--mm-courbe), box-shadow .2s var(--mm-courbe), background .2s var(--mm-courbe);
}
#modal-mat-picker .mp-item:hover,
#modal-mat-picker .mp-item:focus-visible {
    outline: none;
    transform: translateY(-1px);
    border-color: color-mix(in srgb, var(--mm-accent) 45%, transparent);
    background: color-mix(in srgb, var(--mm-accent) 8%, var(--mm-verre-hi));
    box-shadow: 0 6px 18px color-mix(in srgb, var(--mm-accent) 14%, transparent);
}
#modal-mat-picker .mp-item.is-choisi { border-color: var(--mm-ok); }
#modal-mat-picker .mp-vignette {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    overflow: hidden;
    font-size: 18px;
    color: var(--mp-couleur, var(--mm-accent));
    background: color-mix(in srgb, var(--mp-couleur, var(--mm-accent)) 13%, transparent);
}
#modal-mat-picker .mp-vignette img { width: 100%; height: 100%; object-fit: cover; }
#modal-mat-picker .mp-item-txt { flex: 1 1 auto; min-width: 0; }
#modal-mat-picker .mp-item-nom { display: block; font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#modal-mat-picker .mp-item-infos { display: block; font-size: 12px; color: var(--mm-texte-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#modal-mat-picker .mp-item-type {
    flex: 0 0 auto;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    color: var(--mp-couleur, var(--mm-texte-2));
    background: color-mix(in srgb, var(--mp-couleur, var(--mm-texte-2)) 12%, transparent);
}
#modal-mat-picker .mp-item > .ti-chevron-right { flex: 0 0 auto; color: var(--mm-texte-3); }

#modal-mat-picker .mp-rien {
    padding: 26px 12px;
    text-align: center;
    color: var(--mm-texte-3);
}
#modal-mat-picker .mp-rien i { display: block; font-size: 30px; margin-bottom: 6px; opacity: .6; }

#modal-mat-picker .mp-plus { align-self: center; }

#modal-mat-picker .mm-pied { justify-content: space-between; }

@media (max-width: 560px) {
    #modal-mat-picker .mp-item-type { display: none; }
    #modal-mat-picker .mp-resultats { max-height: 38vh; }
}
