/* Mobile layout refinement: compact header, dedicated map controls, and dashboard readability. */
@media (max-width: 980px) {
  html[data-view-options="2"] { --header-h: 86px !important; }

  html[data-view-options="2"] body .app {
    grid-template-rows: var(--header-h) minmax(0, 1fr) !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] {
    height: 86px !important;
    min-height: 86px !important;
    max-height: 86px !important;
    display: grid !important;
    grid-template-columns: minmax(138px, 42vw) minmax(0, 1fr) !important;
    grid-template-rows: 1fr !important;
    grid-template-areas: "brand search" !important;
    align-items: center !important;
    align-content: center !important;
    gap: 10px !important;
    padding: 6px 10px !important;
    overflow: visible !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-brand-stack {
    display: contents !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .brand {
    grid-area: brand !important;
    width: min(152px, 100%) !important;
    height: 64px !important;
    align-self: center !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .brand img {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain !important;
    object-position: left center !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .search {
    grid-area: search !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    height: 44px !important;
    justify-self: stretch !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .search input {
    height: 44px !important;
    border-radius: 15px !important;
    padding-left: 39px !important;
    padding-right: 28px !important;
    font-size: 12px !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .search svg {
    left: 12px !important;
    top: 11px !important;
    width: 21px !important;
    height: 21px !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell] .map-instruction,
  html[data-view-options="2"] body .header[data-grid-shell] .sites-toggle {
    display: none !important;
  }

  /* Grid status becomes a compact map control instead of occupying header height. */
  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-control,
  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-control.is-enabled {
    position: absolute !important;
    left: 10px !important;
    right: auto !important;
    top: calc(100% + 10px) !important;
    width: 158px !important;
    min-width: 158px !important;
    max-width: 158px !important;
    height: 36px !important;
    margin: 0 !important;
    z-index: 95 !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-toggle-column,
  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-control .grid-condition-toggle-row {
    width: 158px !important;
    min-width: 158px !important;
    max-width: 158px !important;
    height: 36px !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-control .grid-condition-toggle-row {
    display: grid !important;
    grid-template-columns: 24px minmax(0, 126px) !important;
    grid-template-rows: 36px !important;
    column-gap: 8px !important;
    align-items: center !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-toggle-row > .grid-condition-about {
    grid-column: 1 !important;
    grid-row: 1 !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-toggle-row > .grid-condition-toggle {
    grid-column: 2 !important;
    grid-row: 1 !important;
    width: 126px !important;
    min-width: 126px !important;
    max-width: 126px !important;
    height: 34px !important;
    padding: 0 7px !important;
    gap: 5px !important;
    justify-content: space-between !important;
    font-size: 10px !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-toggle-label {
    font-size: 10px !important;
    white-space: nowrap !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-options {
    position: absolute !important;
    left: 32px !important;
    top: 40px !important;
    width: 126px !important;
    min-width: 126px !important;
    max-width: 126px !important;
    z-index: 96 !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-view-wrap,
  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-view {
    width: 126px !important;
    min-width: 126px !important;
    max-width: 126px !important;
  }

  /* Outage control occupies the other half of the first mobile map-control row. */
  .map-stage[data-grid-shell] .outage-overlay-control {
    top: 10px !important;
    left: auto !important;
    right: 10px !important;
    align-items: flex-end !important;
    z-index: 94 !important;
  }

  .map-stage[data-grid-shell] .outage-overlay-toggle {
    width: 158px !important;
    min-height: 34px !important;
    padding: 0 7px 0 9px !important;
    justify-content: space-between !important;
    gap: 5px !important;
    font-size: 10px !important;
  }

  .map-stage[data-grid-shell] .outage-toggle-label {
    max-width: 112px !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  /* Display Options sits on a clean second row instead of competing with the toggles. */
  html[data-view-options="2"] body .map-stage[data-grid-shell] .grid-condition-float,
  html[data-view-options="2"][data-final-display-revisions="1"] body .map-stage[data-grid-shell] .grid-condition-float {
    top: 54px !important;
    right: 8px !important;
    width: min(304px, calc(100% - 16px)) !important;
    max-width: calc(100% - 16px) !important;
  }

  html[data-view-options="2"] .view-options {
    width: 100% !important;
  }

  html[data-view-options="2"] .view-options:not([open]) {
    width: 154px !important;
    margin-left: auto !important;
  }

  html[data-view-options="2"] body .map-stage[data-grid-shell] #map {
    top: 0 !important;
  }

  /* Slightly improve dashboard typography at the narrowest point in Network Overview. */
  html.clh-mobile-v3 #sidebar #energyHeartbeat[data-value-dashboard-round2] .cv-capacity-reference-battery-stats {
    column-gap: 8px !important;
  }

  html.clh-mobile-v3 #sidebar #energyHeartbeat[data-value-dashboard-round2] .cv-capacity-reference-battery .cv-capacity-reference-number,
  html.clh-mobile-v3 #sidebar #energyHeartbeat[data-value-dashboard-round2] .cv-battery-stat .cv-capacity-number {
    font-size: 15px !important;
    letter-spacing: -.015em !important;
  }

  html.clh-mobile-v3 #sidebar #energyHeartbeat[data-value-dashboard-round2] .cv-capacity-reference-desc,
  html.clh-mobile-v3 #sidebar #energyHeartbeat[data-value-dashboard-round2] .cv-battery-stat .cv-capacity-desc {
    font-size: 6px !important;
    line-height: 1.1 !important;
  }

  /* Give Energy & Savings a clean two-line header on mobile. */
  html body #sidebar #energyHeartbeat[data-value-dashboard-round2][data-energy-savings-layout] .cv-flow[data-row2-latest="1"] > .cv-heading {
    min-height: 48px !important;
    height: auto !important;
    display: grid !important;
    grid-template-columns: 1fr auto !important;
    grid-template-rows: auto auto !important;
    align-items: center !important;
    row-gap: 5px !important;
    padding-top: 2px !important;
  }

  html body #sidebar #energyHeartbeat[data-value-dashboard-round2][data-energy-savings-layout] .cv-flow[data-row2-latest="1"] > .cv-heading h2 {
    grid-column: 1 / -1 !important;
    grid-row: 1 !important;
    justify-self: start !important;
    font-size: 14px !important;
  }

  html body #sidebar .pd-period-anchor {
    position: relative !important;
    left: auto !important;
    top: auto !important;
    width: 148px !important;
    height: 32px !important;
    margin: 0 0 6px auto !important;
    z-index: 24 !important;
  }

  html body #sidebar .pd-period-control select {
    height: 32px !important;
    font-size: 11px !important;
  }
}

@media (max-width: 380px) {
  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] {
    grid-template-columns: 132px minmax(0, 1fr) !important;
    gap: 8px !important;
    padding-left: 8px !important;
    padding-right: 8px !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .brand {
    width: 132px !important;
  }

  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-control,
  html[data-view-options="2"] body .header[data-grid-shell][data-compact-grid-controls] .grid-condition-control.is-enabled {
    left: 8px !important;
    width: 154px !important;
    min-width: 154px !important;
    max-width: 154px !important;
  }

  .map-stage[data-grid-shell] .outage-overlay-control { right: 8px !important; }
  .map-stage[data-grid-shell] .outage-overlay-toggle { width: 154px !important; }
}
