/* ═══════════════════════════════════════════════
   RESPONSIVE — Tablettes (≤ 1024px)
   ═══════════════════════════════════════════════ */

@media (max-width: 1024px) {
  #header {
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 8px;
  }

  #header-left {
    flex-wrap: wrap;
    gap: 8px;
    overflow: visible;
  }

  #header h1 {
    font-size: 14px;
    white-space: normal;
    flex-basis: 100%;
  }

  #header-left .sep:first-of-type {
    display: none;
  }

  #dep-select,
  #metro-select {
    min-width: 160px;
    font-size: 12px;
  }

  #legend {
    min-width: 160px;
    padding: 10px 12px;
  }
}

/* ═══════════════════════════════════════════════
   RESPONSIVE — Mobiles (≤ 768px)
   ═══════════════════════════════════════════════ */

@media (max-width: 768px) {
  body {
    overflow-y: auto;
    height: auto;
    min-height: 100vh;
  }

  #header {
    flex-direction: column;
    align-items: stretch;
    padding: 6px 10px;
    gap: 5px;
    min-height: auto;
  }

  #header-left {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }

  #header h1 {
    font-size: 12px;
    text-align: center;
    white-space: normal;
    line-height: 1.3;
  }

  #header-left .sep {
    display: none;
  }

  /* Labels masqués : les selects ont aria-label */
  .filter-label {
    display: none;
  }

  /* Les deux selects côte à côte */
  #header-left {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 5px;
  }

  /* Le titre repasse en colonne seul */
  #header h1 {
    flex-basis: 100%;
  }

  #dep-select,
  #metro-select {
    flex: 1 1 0;
    min-width: 0;
    font-size: 12px;
    padding: 6px 8px;
  }

  /* Compteur + logos sur une ligne */
  #header-right {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    flex-wrap: nowrap;
    gap: 8px;
  }

  #count-info {
    text-align: left;
    flex-basis: auto;
    font-size: 11px;
  }

  #header-logos {
    border-left: 1px solid var(--color-border-sep);
    padding-left: 8px;
    justify-content: flex-end;
  }

  #header-logos img {
    height: 24px;
  }

  /* Carte : hauteur calculée sur le header compacté */
  #main-content {
    flex: none;
    height: calc(100vh - 110px);
    min-height: 350px;
  }

  /* Widget histogramme : centré en bas, pleine largeur */
  #histo-panel {
    left: 8px;
    right: 8px;
    bottom: 10px;
    width: auto;
    max-height: 45vh;
  }

  #histo-title {
    font-size: 10px;
  }

  #histo-subtitle {
    font-size: 9px;
  }

  /* Légende : coin bas-droit, plus compacte */
  #legend {
    bottom: 14px;
    right: 8px;
    padding: 10px 10px;
    min-width: 150px;
    max-width: 180px;
    font-size: 10px;
  }

  /* Quand le histo-panel est visible, remonter la légende en haut à droite
     pour éviter le chevauchement (sélecteur frère CSS, sans JS) */
  #histo-panel.visible ~ #legend {
    top: 8px;
    bottom: auto;
  }

  .legend-section-title { font-size: 9px; }
  .legend-section-sub   { font-size: 9px; }
  .legend-size-label    { font-size: 9px; }
  .legend-ratio-label   { font-size: 10px; }
  .legend-ratio-circle  { width: 12px; height: 12px; }

  /* Modal : bords à bords */
  #modal-box {
    max-width: 98vw;
    max-height: 96vh;
    border-radius: var(--radius-lg);
  }

  #modal-header { padding: 10px 12px 8px; }
  #modal-title  { font-size: 12px; }
  #modal-subtitle { font-size: 10px; }
  #btn-modal-close { padding: 5px 8px; font-size: 11px; }
  #modal-body { padding: 10px; }

  /* Tooltip plus étroit */
  .qpv-tooltip {
    min-width: 150px;
    padding: 8px 10px;
    font-size: 11px;
  }

  .qpv-tooltip .tt-name {
    font-size: 11px;
    max-width: 170px;
  }
}

/* ═══════════════════════════════════════════════
   RESPONSIVE — Mobile paysage (hauteur ≤ 500px)
   Header compact en ligne, max de place pour la carte
   ═══════════════════════════════════════════════ */

@media (max-height: 600px) and (orientation: landscape) {
  body {
    overflow: hidden;
    height: 100vh;
  }

  /* Header en une seule ligne compacte */
  #header {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    padding: 4px 10px;
    gap: 8px;
    min-height: auto;
  }

  /* Cacher le titre pour gagner de la place */
  #header h1 {
    display: none;
  }

  #header-left {
    flex-direction: row;
    align-items: center;
    flex-wrap: nowrap;
    gap: 6px;
    overflow: visible;
    flex: 1 1 auto;
  }

  #header-left .sep {
    display: none;
  }

  .filter-label {
    display: none;
  }

  #dep-select,
  #metro-select {
    width: auto;
    min-width: 130px;
    max-width: 180px;
    font-size: 12px;
    padding: 4px 8px;
  }

  #header-right {
    flex-wrap: nowrap;
    gap: 6px;
    flex-shrink: 0;
  }

  #count-info {
    font-size: 11px;
    white-space: nowrap;
    flex-basis: auto;
  }

  #header-logos {
    border-left: 1px solid var(--color-border-sep);
    padding-left: 8px;
    justify-content: center;
  }

  #header-logos img {
    height: 22px;
  }

  /* Carte : prend toute la hauteur restante */
  #main-content {
    flex: 1 1 auto;
    height: auto;
    min-height: 0;
  }

  /* Histo-panel : coin bas-gauche, hauteur limitée */
  #histo-panel {
    left: 8px;
    right: auto;
    bottom: 8px;
    width: clamp(220px, 35vw, 320px);
    max-height: 70vh;
  }

  /* Légende : ancrée en haut à droite — croissance vers le bas, limitée + scroll */
  #legend {
    top: 8px;
    bottom: auto;
    right: 8px;
    max-width: 160px;
    max-height: calc(100% - 16px);
    overflow-y: auto;
    padding: 7px 9px;
    font-size: 9px;
  }

  /* Quand le histo-panel est visible, la légende reste en haut (déjà le cas) */
  #histo-panel.visible ~ #legend {
    top: 8px;
    bottom: auto;
    max-height: calc(100% - 16px);
  }

  .legend-section-title { font-size: 8px; }
  .legend-section-sub   { font-size: 8px; }
  .legend-size-label    { font-size: 8px; }
  .legend-ratio-label   { font-size: 9px; }
  .legend-ratio-circle  { width: 10px; height: 10px; }
}

/* ═══════════════════════════════════════════════
   RESPONSIVE — Petits mobiles (≤ 480px)
   ═══════════════════════════════════════════════ */

@media (max-width: 480px) {
  #header h1 {
    font-size: 11px;
  }

  #dep-select,
  #metro-select {
    font-size: 11px;
    padding: 5px 6px;
  }

  #header-logos img {
    height: 20px;
  }

  #main-content {
    height: calc(100vh - 100px);
    min-height: 280px;
  }

  #histo-panel {
    max-height: 40vh;
  }

  #legend {
    max-width: 155px;
    padding: 8px 8px;
    bottom: 10px;
    right: 6px;
  }

  /* Quand le histo-panel est visible, remonter la légende en haut à droite */
  #histo-panel.visible ~ #legend {
    top: 6px;
    bottom: auto;
  }

  .legend-section-title { font-size: 8px; }
  .legend-section-sub   { font-size: 8px; }
  .legend-size-label    { font-size: 8px; }
  .legend-ratio-label   { font-size: 9px; }
  .legend-ratio-circle  { width: 10px; height: 10px; }
  #volume-legend-container { gap: 8px; }
}
