/* Uniquement les IMAGES de fond. Pas de filtre sur .gm-style / .leaflet-pane :
   ils contiennent aussi des marqueurs, tracés, couches météo et fenêtres.
   Le changement de classe du thème ne déclenche aucun chargement de carte,
   déplacement, modification de zoom ni changement de fond enregistré. */
:root.dark .eb-map-theme-google[data-eb-map-theme-type='plan'] .gm-style img:is(
    [src*='googleapis.com/maps/vt'], [src*='google.com/maps/vt'], [src*='google.com/vt']
),
:root.dark .leaflet-tile[src*='tile.openstreetmap.org/'],
:root.dark :is(.gm-style img, .leaflet-tile)[src*='geopf.fr/'][src*='GEOGRAPHICALGRIDSYSTEMS.' i] {
    filter: invert(1) hue-rotate(180deg) brightness(.85) saturate(.7);
}

:root.dark .eb-map-theme-google[data-eb-map-theme-type='photo'] .gm-style img:is(
    [src*='googleapis.com/maps/vt'], [src*='google.com/maps/vt'], [src*='google.com/vt'],
    [src*='googleapis.com/kh'], [src*='google.com/kh'], [src*='gstatic.com/kh']
),
:root.dark :is(.gm-style img, .leaflet-tile)[src*='geopf.fr/'][src*='ORTHOIMAGERY.ORTHOPHOTOS' i] {
    filter: brightness(.65);
}

:root.dark .leaflet-container { background-color: var(--eb-palette-map-night, #20252c); }

/* Habillage commun des cartes Google / Leaflet et de leurs contrôles injectés
   après chargement (SPA comprise). Les !important ciblés remplacent les styles
   inline de Google et les resets de boutons, sans filtrer photos ou marqueurs.
   En clair, les styles d'origine reprennent automatiquement la main. */
:root.dark {
    --eb-map-surface: var(--eb-surface, #1c1c1c);
    --eb-map-hover: var(--eb-surface-hover, #252525);
    --eb-map-text: var(--eb-text, #e2e8f0);
    --eb-map-muted: var(--eb-text-secondary, #94a3b8);
    --eb-map-border: var(--eb-border, #333);
    --eb-map-link: #93c5fd;
}

/* Volet Couches, intégré à la SITAC ou flottant sur les autres cartes. */
:root.dark :is(.eb-lyr-panel, .eb-img-bar, .eb-map-bg-control,
    .eb-map-annotation-control, .eb-map-drawing-actions) {
    background: var(--eb-map-surface);
    color: var(--eb-map-text);
    border-color: var(--eb-map-border);
}
:root.dark .eb-lyr-embed .eb-lyr-panel { background: transparent; }
:root.dark :is(.eb-lyr-head, .eb-lyr-sec, .eb-lyr-foot, .eb-lyr-unavail) {
    border-color: var(--eb-map-border);
}
:root.dark :is(.eb-lyr-foot, .eb-lyr-detail) {
    background: var(--eb-map-hover);
    border-color: var(--eb-map-border);
}
:root.dark :is(.eb-lyr-sec-t, .eb-lyr-name, .eb-map-bg-control label) { color: var(--eb-map-text); }
:root.dark :is(.eb-lyr-head strong, .eb-lyr-sec-t em, .eb-lyr-meta,
    .eb-lyr-op span, .eb-lyr-detail, .eb-lyr-live, .eb-lyr-unavail,
    .eb-lyr-unavail-sum, .eb-img-empty, .eb-img-hint) { color: var(--eb-map-muted); }
:root.dark :is(.eb-lyr-btn, .eb-lyr-ghost, .eb-lyr-refresh,
    .eb-lyr-bg button, .eb-map-bg-option) {
    background: var(--eb-map-surface) !important;
    color: var(--eb-map-text) !important;
    border-color: var(--eb-map-border) !important;
}
:root.dark :is(.eb-lyr-btn, .eb-lyr-ghost, .eb-lyr-refresh,
    .eb-lyr-bg button, .eb-map-bg-option):is(:hover, :focus-visible) {
    background: var(--eb-map-hover) !important;
    border-color: var(--eb-map-link) !important;
}
:root.dark :is(.eb-lyr-bg button, .eb-map-bg-option).active,
:root.dark #tools-maps .eb-map-bg-option.active {
    background: var(--eb-palette-map-blue, #3159d5) !important;
    border-color: var(--eb-palette-blue-300, #93c5fd) !important;
    color: var(--eb-palette-white, #ffffff) !important;
}
:root.dark :is(.eb-lyr-btn, .eb-lyr-ghost, .eb-lyr-refresh,
    .eb-lyr-bg button, .eb-map-bg-option):focus-visible {
    outline: 2px solid var(--eb-map-link);
    outline-offset: 2px;
}
:root.dark :is(.eb-lyr-count, .eb-lyr-badge) {
    background: var(--eb-palette-navy-800, #1e3a5f) !important;
    color: var(--eb-palette-blue-200, #bfdbfe) !important;
}
:root.dark :is(.eb-lyr-count.zero, .eb-lyr-badge.off) {
    background: var(--eb-map-hover) !important;
    color: var(--eb-map-muted) !important;
}
:root.dark .eb-lyr-count.err { background: var(--eb-palette-red-night, #4c2025) !important; color: var(--eb-palette-red-300, #fca5a5) !important; }
:root.dark :is(.eb-lyr-state.err, .eb-img-actions .eb-lyr-ghost.danger:hover) { color: var(--eb-palette-red-300, #fca5a5) !important; }
:root.dark :is(.eb-lyr-state.warn, .eb-lyr-warn) { color: var(--eb-palette-amber-300, #fcd34d); }
:root.dark .eb-map-annotation-separator { background: var(--eb-map-border); }

/* Contrôles natifs : conserver les dimensions et le comportement de Google.
   Seuls les pictogrammes monochromes des commandes sont éclaircis. */
:root.dark .gm-style :is(.gm-control-active, .gm-svpc, .gm-style-mtc button,
    .gm-style-mtc ul, .gm-style-mtc li, .gm-style-iw-c, .gm-style-iw-d) {
    background-color: var(--eb-map-surface) !important;
    color: var(--eb-map-text) !important;
}
:root.dark .gm-style :is(.gm-control-active, .gm-svpc, .gm-style-mtc button):hover {
    background-color: var(--eb-map-hover) !important;
}
:root.dark .gm-style .gm-control-active > img { filter: brightness(0) invert(.9); }
:root.dark .gm-style .gm-style-iw-tc::after { background: var(--eb-map-surface); }
:root.dark .gm-style .gm-style-iw-t::after { background: var(--eb-map-surface); }
:root.dark .gm-style .gm-style-iw-c button.gm-ui-hover-effect {
    background: var(--eb-map-hover) !important;
    opacity: 1;
}
:root.dark .gm-style .gm-style-iw-c button.gm-ui-hover-effect span {
    background-color: var(--eb-map-text) !important;
}
:root.dark .gm-style .gm-style-iw-c button.gm-ui-hover-effect img { filter: brightness(0) invert(.9); }

/* Fiches personne, activité, intervention, repères et couches de risques.
   Les en-têtes et badges métier conservent leurs couleurs sémantiques. */
:root.dark :is(.eb-perpop, .eb-evpop, .eb-ivpop, .eb-hz-pop, .eb-map-point-bulle) {
    background: var(--eb-map-surface);
    color: var(--eb-map-text);
}
:root.dark :is(.eb-perpop-head, .eb-ivpop-head) {
    background: color-mix(in srgb, var(--acc, #3159d5) 35%, var(--eb-palette-ink-950, #151515));
}
:root.dark :is(.eb-evpop-head, .eb-evpop-cta) {
    background: color-mix(in srgb, var(--ev, #3159d5) 35%, var(--eb-palette-ink-950, #151515));
}
:root.dark :is(.eb-perpop-row, .eb-evpop-row, .eb-ivpop-row,
    .eb-evpop-meta, .eb-hz-pop dt, .eb-hz-pop .t) { color: var(--eb-map-muted); }
:root.dark :is(.eb-perpop-row b, .eb-ivpop-row b) { color: var(--eb-map-text); }
:root.dark :is(.eb-perpop-actions, .eb-ivpop-actions, .eb-hz-pop .t) { border-color: var(--eb-map-border); }
:root.dark .eb-evpop-pill.is-open { background: var(--eb-palette-green-night, #123d2b); color: var(--eb-palette-green-300, #86efac); }
:root.dark .eb-evpop-pill.is-closed { background: var(--eb-palette-red-night, #4c2025); color: var(--eb-palette-red-300, #fca5a5); }
:root.dark .eb-ivpop-chip { background: var(--eb-palette-amber-night, #422f12); color: var(--eb-palette-amber-300, #fcd34d); }
:root.dark :is(.gm-style-iw-d, .leaflet-popup-content) a:not(.btn):not([class*='cta']) {
    color: var(--eb-map-link);
}
:root.dark :is(.gm-style-iw-d, .leaflet-popup-content, .eb-img-bar) :is(textarea, input[type='text'], select) {
    background-color: var(--eb-map-surface) !important;
    color: var(--eb-map-text) !important;
    border-color: var(--eb-map-border) !important;
}

/* Leaflet : cartes de fiche, maraudes, sélecteurs d'adresse et mode souverain. */
:root.dark :is(.leaflet-popup-content-wrapper, .leaflet-popup-tip, .leaflet-tooltip,
    .leaflet-control-layers, .leaflet-bar a, .leaflet-bar button) {
    background-color: var(--eb-map-surface);
    color: var(--eb-map-text);
    border-color: var(--eb-map-border);
}
:root.dark .leaflet-bar :is(a, button):hover { background-color: var(--eb-map-hover); }
:root.dark .leaflet-bar a.leaflet-disabled { color: var(--eb-map-muted); }
:root.dark .leaflet-container a.leaflet-popup-close-button { color: var(--eb-map-text); }
:root.dark .leaflet-tooltip-top::before { border-top-color: var(--eb-map-surface); }
:root.dark .leaflet-tooltip-bottom::before { border-bottom-color: var(--eb-map-surface); }
:root.dark .leaflet-tooltip-left::before { border-left-color: var(--eb-map-surface); }
:root.dark .leaflet-tooltip-right::before { border-right-color: var(--eb-map-surface); }
:root.dark .leaflet-control-attribution,
:root.dark .eb-ign-attrib {
    background: var(--eb-map-surface) !important;
    color: var(--eb-map-muted) !important;
}
:root.dark .leaflet-control-attribution a { color: var(--eb-map-link); }
:root.dark .leaflet-control.map-mode-toggle,
:root.dark .leaflet-control.map-mode-toggle button,
:root.dark .leaflet-control.route-info > div {
    background: var(--eb-map-surface) !important;
    color: var(--eb-map-text) !important;
}
:root.dark .leaflet-control.map-mode-toggle button[aria-pressed='true'] {
    background: var(--eb-palette-map-blue, #3159d5) !important;
    color: var(--eb-palette-white, #ffffff) !important;
}
:root.dark .leaflet-control.map-mode-toggle button:focus-visible {
    outline: 2px solid var(--eb-map-link);
    outline-offset: -2px;
}

/* Téléphones et tablettes : mêmes cibles au doigt en clair et en sombre.
   La hauteur bornée garde la croix visible ; seul le contenu des fiches défile.
   Les sélecteurs incluent la racine pour primer sur les styles des pages. */
@media (max-width: 1200px), (pointer: coarse) {
    :root :is(.eb-lyr-btn, .eb-lyr-ghost, .eb-lyr-refresh, .eb-lyr-bg button,
        .eb-map-bg-option, .leaflet-control.map-mode-toggle button) {
        min-height: 44px;
        min-width: 44px;
    }
    :root #tools-maps :is(.sitac-couches-btn, .eb-map-bg-option) {
        height: 44px !important;
        min-height: 44px;
    }
    :root :is(.eb-lyr-name, .eb-lyr-bg button, .eb-map-bg-option) { font-size: var(--eb-fs-md, 14px) !important; }
    :root .eb-lyr-main { min-height: 48px; }
    :root :is(.eb-lyr-name, .eb-lyr-meta) { white-space: normal; }
    :root .eb-lyr-meta { font-size: var(--eb-fs-sm, 12px); }
    :root .eb-lyr-op input[type='range'] { height: 44px; }
    :root .eb-lyr-panel { max-height: min(70dvh, calc(100dvh - 100px)); }
    :root .eb-lyr-body { min-height: 0; }
    :root .gm-style .gm-style-iw-c {
        max-width: min(340px, calc(100vw - 32px)) !important;
    }
    :root .gm-style .gm-style-iw-d {
        max-height: min(35dvh, var(--eb-map-popup-height, 220px)) !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }
    :root .gm-style .gm-style-iw-c button.gm-ui-hover-effect {
        width: 44px !important;
        height: 44px !important;
        top: 0 !important;
        right: 0 !important;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    :root .gm-style .gm-style-iw-c button.gm-ui-hover-effect span { margin: 0 !important; }
    :root :is(.eb-perpop, .eb-evpop, .eb-ivpop, .eb-map-point-bulle) {
        max-width: calc(100vw - 64px);
        overflow-wrap: anywhere;
    }
    :root :is(.eb-perpop-row, .eb-evpop-row, .eb-ivpop-row) { font-size: var(--eb-fs-md, 14px); }
    :root :is(.eb-perpop-actions, .eb-ivpop-actions, .eb-map-point-actions) :is(.btn-action, .btn),
    :root .eb-evpop-cta {
        min-height: 44px;
        min-width: 44px;
    }
    :root .leaflet-container a.leaflet-popup-close-button { width: 44px; height: 44px; line-height: 44px; }
    :root .leaflet-popup-content { margin-right: 44px; max-height: 35dvh; overflow-y: auto; overscroll-behavior: contain; }
    /* Les panneaux voisins sont au-dessus du calque des fenêtres Google.
       Les effacer le temps de la lecture évite de masquer la fiche, sans
       changer le cadrage ni la géométrie de la carte. */
    :root #gmaps-main-container:has(.gm-style-iw-c) #eventDiv,
    :root #gmaps-main-container:has(.gm-style-iw-c) #mobile-filter,
    :root body:has(#gps-main-container):has(#map_canvas .gm-style-iw-c) #eventDiv,
    :root body:has(#gps-main-container):has(#map_canvas .gm-style-iw-c) #myGps1,
    :root .eb-map-theme-google:has(.gm-style-iw-c) .eb-map-annotation-control {
        visibility: hidden;
        pointer-events: none;
    }
    :root body.eb-lyr-sheet-open #iaWidgetBtn,
    :root body:has(#sitac-couches.is-open) #iaWidgetBtn { display: none !important; }
    /* cibles écrites une à une (id visible) plutôt que dans :is() : scripts/test_has_ancetre_coche.php */
    :root #sitac-main-row:has(#sitac-couches.is-open) #equipesdiv,
    :root #sitac-main-row:has(#sitac-couches.is-open) #interventionsdiv,
    :root #sitac-main-row:has(#sitac-couches.is-open) #sitac-dock {
        visibility: hidden;
        pointer-events: none;
    }
    :root #tools-maps #sitac-couches {
        flex-wrap: nowrap;
        overflow-y: auto;
        overflow-x: hidden;
        overscroll-behavior: contain;
    }
    :root #tools-maps #sitac-couches > * { flex-shrink: 0; }
    :root #sitac-couches .eb-lyr-body { max-height: none; overflow: visible; }
}
@media (min-width: 768px) and (max-width: 1199px) {
    :root #sitac-main-row:has(#interventionsdiv, #equipesdiv) #tools-maps {
        left: 50% !important;
        top: 12px !important;
        transform: translateX(-50%) !important;
        width: max-content;
        min-width: 0;
        max-width: calc(100% - 648px);
    }
}
:root.dark :is(.gm-style-iw-d, .leaflet-popup-content, .eb-lyr-body) {
    scrollbar-color: #64748b var(--eb-map-surface);
}
:root.dark :is(.gm-style-iw-d, .leaflet-popup-content, .eb-lyr-body)::-webkit-scrollbar-track {
    background: var(--eb-map-surface);
}
:root.dark :is(.gm-style-iw-d, .leaflet-popup-content, .eb-lyr-body)::-webkit-scrollbar-thumb {
    background: var(--eb-palette-slate-500, #64748b);
}
@media (max-width: 992px) {
    :root #gmaps-main-container .gps.gps2.hideState { display: none !important; }
    :root #gmaps-main-container:has(.gm-style-iw-c) .gps.gps2 {
        visibility: hidden;
        pointer-events: none;
    }
}
