/* ================================================================
   Sama Suuf — Responsive SIG V5.8
   Portable / tablette / mobile
   Chargé après tous les styles SIG précédents.
   ================================================================ */

/* Sécurité générale : aucun composant SIG ne doit créer un scroll horizontal. */
.sb-shell.sb-result-mode,
.sb-shell.sb-result-mode .sb-result-grid-v25,
.sb-shell.sb-result-mode .sb-map-immersive-card,
#resultDrawer,
#mapControlDrawer,
#referenceLayerPanel,
#mapExplorerDrawer,
#mapLegend {
  min-width: 0 !important;
  box-sizing: border-box !important;
}

#resultDrawer *,
#mapControlDrawer *,
#referenceLayerPanel *,
#mapExplorerDrawer *,
#mapLegend * {
  box-sizing: border-box !important;
  min-width: 0;
  word-break: normal !important;
  overflow-wrap: break-word !important;
  hyphens: none !important;
}

/* Les sections du panneau Couches restent toujours verticales. */
#referenceLayerPanel {
  display: block !important;
}
#referenceLayerPanel .sb-map-layer-group {
  display: block !important;
  width: 100% !important;
  margin: 0 !important;
  padding: 14px 0 !important;
  border-top: 1px solid #e1eae6 !important;
}
#referenceLayerPanel .sb-map-layer-group:first-of-type {
  border-top: 0 !important;
  padding-top: 6px !important;
}
#referenceLayerPanel .sb-map-layer-group-head {
  display: block !important;
  width: 100% !important;
  margin: 0 0 10px !important;
}
#referenceLayerPanel .sb-map-layer-group-head strong,
#referenceLayerPanel .sb-map-layer-group-head small {
  display: block !important;
  width: 100% !important;
}

/* Un toggle est une ligne compacte, pas une grande carte vide. */
#referenceLayerPanel .sb-reference-layer-toggles label {
  min-height: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  background: transparent !important;
  border-radius: 0 !important;
}
#referenceLayerPanel .sb-reference-layer-toggles label > span {
  width: 100% !important;
  min-height: 46px !important;
  padding: 9px 11px 9px 34px !important;
  display: flex !important;
  align-items: center !important;
  justify-content: flex-start !important;
  border: 1px solid #d9e5e0 !important;
  border-radius: 13px !important;
  background: #fff !important;
  color: #29483f !important;
  line-height: 1.3 !important;
}
#referenceLayerPanel .sb-reference-layer-toggles input:checked + span {
  border-color: #a9d0c2 !important;
  background: #f0f8f5 !important;
  color: #0b624d !important;
}

/* En-tête cohérent dans tous les drawers. */
#resultDrawer .sb-map-drawer-head,
#mapControlDrawer .sb-map-mini-drawer-head,
#referenceLayerPanel .sb-map-mini-drawer-head,
#mapExplorerDrawer .sb-map-mini-drawer-head {
  display: flex !important;
  flex-direction: row !important;
  align-items: flex-start !important;
  justify-content: space-between !important;
  gap: 12px !important;
  width: 100% !important;
}
#resultDrawer .sb-map-drawer-head > div,
#mapControlDrawer .sb-map-mini-drawer-head > div,
#referenceLayerPanel .sb-map-mini-drawer-head > div,
#mapExplorerDrawer .sb-map-mini-drawer-head > div {
  flex: 1 1 auto !important;
  width: auto !important;
}
#resultDrawer .sb-icon-button,
#mapControlDrawer .sb-icon-button,
#referenceLayerPanel .sb-icon-button,
#mapExplorerDrawer .sb-icon-button {
  flex: 0 0 48px !important;
  width: 48px !important;
  height: 48px !important;
  padding: 0 !important;
}

/* ==================================================================
   PETIT PORTABLE / TABLETTE PAYSAGE : 901–1280 px
   ================================================================== */
@media (min-width: 901px) and (max-width: 1280px) {
  .sb-shell.sb-result-mode .sb-sig-topbar {
    height: 68px !important;
    grid-template-columns: 168px minmax(220px, 1fr) auto !important;
    gap: 10px !important;
    padding: 0 12px !important;
  }
  .sb-shell.sb-result-mode .sb-result-grid-v25 {
    height: calc(100dvh - 68px) !important;
  }
  .sb-sig-brand img {
    max-width: 155px !important;
    height: 46px !important;
  }
  .sb-sig-search {
    height: 44px !important;
  }
  .sb-sig-nav-action {
    min-width: 44px !important;
    padding: 0 9px !important;
  }
  .sb-sig-nav-action:not(.is-primary) {
    font-size: 0 !important;
    gap: 0 !important;
  }
  .sb-sig-nav-action:not(.is-primary) > span {
    font-size: 1.05rem !important;
  }

  /* Rail légèrement réduit, fixe. */
  .sb-map-floating-actions,
  .sb-map-immersive-card.has-side-drawer .sb-map-floating-actions,
  .sb-map-immersive-card.has-side-drawer[data-active-panel="resultDrawer"] .sb-map-floating-actions {
    right: 16px !important;
    top: 16px !important;
    gap: 7px !important;
  }
  .sb-map-fab {
    width: 74px !important;
    min-width: 74px !important;
    height: 68px !important;
    min-height: 68px !important;
    border-radius: 15px !important;
  }
  .sb-map-fab span {
    width: 28px !important;
    height: 28px !important;
  }
  .sb-map-fab strong {
    font-size: .61rem !important;
  }

  /* Les drawers s'ouvrent à gauche du rail avec marge sûre. */
  #resultDrawer,
  #mapControlDrawer,
  #referenceLayerPanel,
  #mapExplorerDrawer {
    right: 104px !important;
    top: 12px !important;
    bottom: auto !important;
    width: min(430px, calc(100vw - 220px)) !important;
    max-width: min(430px, calc(100vw - 220px)) !important;
    max-height: calc(100dvh - 96px) !important;
    padding: 16px !important;
    border-radius: 18px !important;
  }
  #mapExplorerDrawer { width: min(455px, calc(100vw - 220px)) !important; max-width: min(455px, calc(100vw - 220px)) !important; }

  #referenceLayerPanel .sb-reference-layer-toggles {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }
  #mapControlDrawer #parcelTechnicalHost .sb-technical-grid {
    grid-template-columns: repeat(2, minmax(0,1fr)) !important;
  }
  #resultDrawer .sb-map-result-metrics {
    grid-template-columns: repeat(2, minmax(0,1fr)) !important;
  }
}

/* ==================================================================
   TABLETTE portrait / petit écran tactile : 641–900 px
   ================================================================== */
@media (min-width: 641px) and (max-width: 900px) {
  body.sb-result-mode {
    overflow: hidden !important;
  }
  .sb-shell.sb-result-mode .sb-sig-topbar {
    height: 64px !important;
    grid-template-columns: 128px minmax(0,1fr) 48px !important;
    gap: 8px !important;
    padding: 0 10px !important;
  }
  .sb-shell.sb-result-mode .sb-result-grid-v25 {
    height: calc(100dvh - 64px) !important;
  }
  .sb-sig-brand img {
    max-width: 120px !important;
    height: 42px !important;
  }
  .sb-sig-search {
    height: 42px !important;
    padding: 0 11px !important;
  }
  .sb-sig-search input { font-size: .72rem !important; }
  .sb-sig-nav > .sb-sig-nav-action { display: none !important; }

  /* Outils gauche plus compacts. */
  .sb-sig-map-tools {
    left: 10px !important;
    top: 10px !important;
    gap: 6px !important;
  }
  .sb-sig-map-tools button {
    width: 42px !important;
    height: 42px !important;
    border-radius: 12px !important;
  }

  /* Rail vertical d'icônes, fixe et sans texte. */
  .sb-map-floating-actions,
  .sb-map-immersive-card.has-side-drawer .sb-map-floating-actions,
  .sb-map-immersive-card.has-side-drawer[data-active-panel="resultDrawer"] .sb-map-floating-actions {
    top: 10px !important;
    right: 10px !important;
    left: auto !important;
    transform: none !important;
    flex-direction: column !important;
    gap: 7px !important;
  }
  .sb-map-fab {
    width: 48px !important;
    min-width: 48px !important;
    height: 48px !important;
    min-height: 48px !important;
    padding: 0 !important;
    border-radius: 14px !important;
  }
  .sb-map-fab strong { display: none !important; }
  .sb-map-fab span {
    width: 27px !important;
    height: 27px !important;
    background: transparent !important;
  }

  /* Bottom sheet centré : aucune colonne globale dans le drawer. */
  #resultDrawer,
  #mapControlDrawer,
  #referenceLayerPanel,
  #mapExplorerDrawer {
    position: fixed !important;
    z-index: 90 !important;
    display: block;
    left: 10px !important;
    right: 10px !important;
    top: auto !important;
    bottom: max(7px, env(safe-area-inset-bottom)) !important;
    width: auto !important;
    max-width: none !important;
    height: auto !important;
    max-height: min(76dvh, 720px) !important;
    padding: 14px 16px calc(18px + env(safe-area-inset-bottom)) !important;
    border-radius: 24px 24px 18px 18px !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    overscroll-behavior: contain !important;
  }
  #resultDrawer[hidden],
  #mapControlDrawer[hidden],
  #referenceLayerPanel[hidden],
  #mapExplorerDrawer[hidden] { display: none !important; }

  /* La poignée reste, mais sans créer de grand espace. */
  #resultDrawer::before,
  #mapControlDrawer::before,
  #referenceLayerPanel::before,
  #mapExplorerDrawer::before {
    content: "" !important;
    display: block !important;
    width: 54px !important;
    height: 5px !important;
    margin: -3px auto 10px !important;
    border-radius: 999px !important;
    background: #cdd9d4 !important;
  }

  /* Couches : groupes empilés, 2 toggles par ligne seulement. */
  #referenceLayerPanel .sb-reference-layer-toggles {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0,1fr)) !important;
    gap: 8px !important;
    width: 100% !important;
  }
  #referenceLayerPanel .sb-map-layer-group {
    width: 100% !important;
  }

  /* Analyse : 2 métriques par ligne, détails en une colonne. */
  #resultDrawer .sb-map-result-metrics {
    grid-template-columns: repeat(2, minmax(0,1fr)) !important;
    gap: 9px !important;
  }
  #resultAdvancedHost .sb-check-controls,
  #resultAdvancedHost .sb-detail-grid-compact {
    grid-template-columns: 1fr !important;
  }
  #resultAdvancedHost .sb-key-value {
    grid-template-columns: minmax(0,1fr) minmax(120px, 38%) !important;
  }

  /* Parcelle : les informations pro peuvent rester en 2 colonnes, sans couper les mots. */
  #mapControlDrawer #parcelTechnicalHost .sb-technical-grid {
    grid-template-columns: repeat(2, minmax(0,1fr)) !important;
  }

  /* Explorer : formulaire vertical, cartes de service sur une colonne. */
  #mapExplorerDrawer .sb-map-explorer-tabs--two {
    grid-template-columns: repeat(2,minmax(0,1fr)) !important;
  }
  #mapExplorerDrawer .sb-nearby-categories {
    grid-template-columns: repeat(2,minmax(0,1fr)) !important;
  }
  #mapExplorerDrawer .sb-nearby-item-actions {
    grid-template-columns: repeat(2,minmax(0,1fr)) !important;
    padding-left: 52px !important;
  }
  #mapExplorerDrawer #routePlannerDetails .sb-route-form {
    grid-template-columns: 1fr !important;
  }

  /* Légende : panneau compact au-dessus de la carte, pas plus large que l'écran. */
  #mapLegend.sb-sig-legend-panel {
    position: fixed !important;
    left: 10px !important;
    right: 72px !important;
    top: 76px !important;
    width: auto !important;
    max-width: none !important;
    max-height: 56dvh !important;
    border-radius: 18px !important;
  }
  .sb-sig-legend-toggle {
    left: 62px !important;
    bottom: 12px !important;
  }
}

/* ==================================================================
   MOBILE : <=640 px
   ================================================================== */
@media (max-width: 640px) {
  body.sb-result-mode {
    overflow: hidden !important;
  }
  .sb-shell.sb-result-mode .sb-sig-topbar {
    height: 58px !important;
    grid-template-columns: 86px minmax(0,1fr) 42px !important;
    gap: 6px !important;
    padding: 0 7px !important;
  }
  .sb-shell.sb-result-mode .sb-result-grid-v25 {
    height: calc(100dvh - 58px) !important;
  }
  .sb-sig-brand img {
    max-width: 82px !important;
    height: 38px !important;
  }
  .sb-sig-search {
    height: 38px !important;
    padding: 0 8px !important;
    border-radius: 11px !important;
  }
  .sb-sig-search > span { font-size: 1rem !important; }
  .sb-sig-search input {
    font-size: .66rem !important;
  }
  .sb-sig-search input::placeholder {
    color: transparent !important;
  }
  .sb-sig-account summary {
    width: 40px !important;
    height: 40px !important;
  }

  /* Outils de carte : seulement ceux utiles, superposés à gauche. */
  .sb-sig-map-tools {
    left: 7px !important;
    top: 8px !important;
    gap: 5px !important;
  }
  .sb-sig-map-tools button {
    width: 38px !important;
    height: 38px !important;
    border-radius: 11px !important;
    font-size: 1rem !important;
  }
  .sb-sig-map-tools button:nth-child(n+4) { display: none !important; }

  /* Le rail devient une petite barre horizontale centrée sous la topbar. */
  .sb-map-floating-actions,
  .sb-map-immersive-card.has-side-drawer .sb-map-floating-actions,
  .sb-map-immersive-card.has-side-drawer[data-active-panel="resultDrawer"] .sb-map-floating-actions {
    top: 7px !important;
    right: 7px !important;
    left: auto !important;
    transform: none !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 5px !important;
  }
  .sb-map-fab {
    width: 42px !important;
    min-width: 42px !important;
    height: 42px !important;
    min-height: 42px !important;
    padding: 0 !important;
    border-radius: 13px !important;
  }
  .sb-map-fab strong { display: none !important; }
  .sb-map-fab span {
    width: 24px !important;
    height: 24px !important;
    font-size: .92rem !important;
    background: transparent !important;
  }

  /* Bottom sheets mobiles. */
  #resultDrawer,
  #mapControlDrawer,
  #referenceLayerPanel,
  #mapExplorerDrawer {
    position: fixed !important;
    z-index: 90 !important;
    left: 4px !important;
    right: 4px !important;
    top: auto !important;
    bottom: max(4px, env(safe-area-inset-bottom)) !important;
    width: auto !important;
    max-width: none !important;
    height: auto !important;
    max-height: min(82dvh, calc(100dvh - 70px)) !important;
    padding: 11px 11px calc(15px + env(safe-area-inset-bottom)) !important;
    border-radius: 22px 22px 14px 14px !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    overscroll-behavior: contain !important;
  }
  #resultDrawer[hidden],
  #mapControlDrawer[hidden],
  #referenceLayerPanel[hidden],
  #mapExplorerDrawer[hidden] { display: none !important; }

  #resultDrawer::before,
  #mapControlDrawer::before,
  #referenceLayerPanel::before,
  #mapExplorerDrawer::before {
    content: "" !important;
    display: block !important;
    width: 48px !important;
    height: 4px !important;
    margin: -2px auto 8px !important;
    border-radius: 999px !important;
    background: #ccd8d3 !important;
  }

  /* En-têtes plus petits. */
  #resultDrawer .sb-map-drawer-head,
  #mapControlDrawer .sb-map-mini-drawer-head,
  #referenceLayerPanel .sb-map-mini-drawer-head,
  #mapExplorerDrawer .sb-map-mini-drawer-head {
    gap: 8px !important;
    margin-bottom: 10px !important;
  }
  #resultDrawer .sb-map-drawer-head h3,
  #mapControlDrawer .sb-map-mini-drawer-head strong,
  #referenceLayerPanel .sb-map-mini-drawer-head strong,
  #mapExplorerDrawer .sb-map-mini-drawer-head strong {
    font-size: .94rem !important;
  }
  #mapControlDrawer .sb-map-mini-drawer-head small,
  #referenceLayerPanel .sb-map-mini-drawer-head small,
  #mapExplorerDrawer .sb-map-mini-drawer-head small {
    font-size: .63rem !important;
    line-height: 1.35 !important;
  }
  #resultDrawer .sb-icon-button,
  #mapControlDrawer .sb-icon-button,
  #referenceLayerPanel .sb-icon-button,
  #mapExplorerDrawer .sb-icon-button {
    flex-basis: 42px !important;
    width: 42px !important;
    height: 42px !important;
    border-radius: 13px !important;
  }

  /* Couches : tout en une colonne, zéro espace vide. */
  #referenceLayerPanel .sb-reference-layer-toggles {
    grid-template-columns: 1fr !important;
    gap: 7px !important;
  }
  #referenceLayerPanel .sb-map-layer-group {
    padding: 11px 0 !important;
  }
  #referenceLayerPanel .sb-map-layer-group-head {
    margin-bottom: 8px !important;
  }
  #referenceLayerPanel .sb-reference-layer-toggles label > span {
    min-height: 44px !important;
    font-size: .7rem !important;
  }

  /* Analyse : 1 colonne pour toutes les cartes. */
  #resultDrawer .sb-map-result-metrics {
    grid-template-columns: 1fr !important;
    gap: 7px !important;
  }
  #resultDrawer .sb-map-result-metric {
    min-height: 76px !important;
    padding: 10px 11px !important;
  }
  #resultDrawer .sb-map-result-geometry {
    grid-template-columns: 1fr !important;
  }
  #resultDrawer .sb-map-narrative-head {
    display: grid !important;
    gap: 5px !important;
  }
  #resultDrawer .sb-map-narrative-head button {
    justify-self: start !important;
  }
  #resultAdvancedHost .sb-check-controls,
  #resultAdvancedHost .sb-detail-grid-compact,
  #resultAdvancedHost .sb-key-value {
    grid-template-columns: 1fr !important;
  }
  #resultAdvancedHost .sb-key-value strong {
    text-align: left !important;
  }

  /* Parcelle : une seule colonne. */
  .sb-sig-info-grid,
  #mapControlDrawer #parcelTechnicalHost .sb-technical-grid {
    grid-template-columns: 1fr !important;
  }
  .sb-sig-point-row {
    grid-template-columns: 42px 1fr !important;
  }
  .sb-sig-point-row span:last-child {
    grid-column: 2 !important;
  }
  .sb-parcel-tabs {
    gap: 5px !important;
  }
  .sb-parcel-tabs button {
    min-height: 40px !important;
    padding: 0 10px !important;
    font-size: .62rem !important;
  }

  /* Explorer : une seule colonne partout. */
  #mapExplorerDrawer .sb-map-explorer-tabs--two {
    grid-template-columns: repeat(2,minmax(0,1fr)) !important;
    padding: 5px !important;
  }
  #mapExplorerDrawer .sb-map-explorer-tabs button {
    min-height: 42px !important;
    padding: 6px !important;
    font-size: .68rem !important;
  }
  #mapExplorerDrawer .sb-nearby-categories,
  #mapExplorerDrawer .sb-nearby-item-actions,
  #mapExplorerDrawer #routePlannerDetails .sb-route-form {
    grid-template-columns: 1fr !important;
  }
  #mapExplorerDrawer .sb-nearby-item-actions {
    padding-left: 0 !important;
  }
  #mapExplorerDrawer .sb-nearby-item {
    grid-template-columns: 36px minmax(0,1fr) !important;
    padding: 10px !important;
  }
  #mapExplorerDrawer #routePlannerDetails .sb-route-form input,
  #mapExplorerDrawer #routePlannerDetails .sb-route-form select,
  #mapExplorerDrawer #calculateRouteButton {
    min-height: 46px !important;
    height: 46px !important;
    width: 100% !important;
  }

  /* Légende mobile : fenêtre compacte, pas de collision avec le rail. */
  #mapLegend.sb-sig-legend-panel {
    position: fixed !important;
    left: 5px !important;
    right: 54px !important;
    top: 66px !important;
    width: auto !important;
    max-width: none !important;
    max-height: 58dvh !important;
    padding: 12px !important;
    border-radius: 16px !important;
  }
  .sb-sig-legend-toggle {
    left: 50px !important;
    bottom: 8px !important;
    min-height: 38px !important;
    padding: 0 12px !important;
  }
}

/* Très petits téléphones : 360 px et moins. */
@media (max-width: 380px) {
  .sb-shell.sb-result-mode .sb-sig-topbar {
    grid-template-columns: 70px minmax(0,1fr) 38px !important;
    padding: 0 5px !important;
  }
  .sb-sig-brand img { max-width: 67px !important; }
  .sb-sig-search { padding: 0 6px !important; }
  .sb-map-fab {
    width: 39px !important;
    min-width: 39px !important;
    height: 39px !important;
    min-height: 39px !important;
  }
  #resultDrawer,
  #mapControlDrawer,
  #referenceLayerPanel,
  #mapExplorerDrawer {
    left: 2px !important;
    right: 2px !important;
    padding-left: 9px !important;
    padding-right: 9px !important;
  }
}

/* Orientation paysage sur mobile/tablette : utiliser moins de hauteur. */
@media (max-width: 900px) and (orientation: landscape) {
  #resultDrawer,
  #mapControlDrawer,
  #referenceLayerPanel,
  #mapExplorerDrawer {
    max-height: 88dvh !important;
    left: 8% !important;
    right: 8% !important;
  }
  .sb-map-floating-actions {
    gap: 4px !important;
  }
}
