/* Mobile layout overrides. Desktop rules remain unchanged above 700px. */
@media (max-width: 700px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    min-height: 100%;
    overflow-x: hidden;
  }

  body {
    padding: 0 0 calc(74px + env(safe-area-inset-bottom)) !important;
  }

  /* Turn the desktop sidebar into a compact app-style bottom navigation. */
  .sidebar {
    position: fixed !important;
    inset: auto 0 0 !important;
    width: 100% !important;
    height: auto !important;
    min-height: 66px;
    padding: 7px 7px max(7px, env(safe-area-inset-bottom)) !important;
    display: grid !important;
    grid-template-columns: none !important;
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
    align-items: stretch;
    gap: 4px !important;
    border: 1px solid var(--line);
    border-width: 1px 0 0;
    box-shadow: 0 -12px 32px #0008 !important;
    z-index: 1000;
  }

  .sidebar::before,
  .sidebar .brand,
  .sidebar .side-bottom {
    display: none !important;
  }

  .sidebar .nav {
    min-width: 0;
    min-height: 52px;
    margin: 0 !important;
    padding: 6px 2px !important;
    border-radius: 10px;
    display: flex !important;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px !important;
    overflow: hidden;
    font-size: 18px !important;
    line-height: 1;
    text-align: center;
    transform: none !important;
  }

  .sidebar .nav span {
    display: block !important;
    width: 100%;
    overflow: hidden;
    color: inherit;
    font-size: 9px;
    font-weight: 700;
    line-height: 1.15;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  main {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    min-height: calc(100vh - 74px);
    margin: 0 !important;
    padding: 18px 14px 26px !important;
  }

  .page,
  .metric-grid,
  .metric,
  .card,
  .map-card,
  .lower-grid,
  .report-grid,
  .validation-card,
  #parking-cameras,
  #parking-cameras .pc-grid,
  #parking-cameras .pc-card {
    min-width: 0;
    max-width: 100%;
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: start;
    gap: 10px;
    margin: 0 0 18px !important;
  }

  .topbar h1 {
    max-width: 100%;
    margin: 0;
    font-size: clamp(22px, 7vw, 28px) !important;
    line-height: 1.2;
    overflow-wrap: anywhere;
  }

  .eyebrow {
    margin-bottom: 7px;
    font-size: 10px;
    letter-spacing: 1.2px;
  }

  .live {
    padding: 9px 11px;
    font-size: 11px;
    white-space: nowrap;
  }

  .live span {
    display: none;
  }

  /* Summary cards are readable without making the page excessively long. */
  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 10px;
  }

  .metric {
    min-height: 126px;
    padding: 14px !important;
    border-radius: 14px;
  }

  .metric span,
  .metric em {
    font-size: 11px;
    line-height: 1.35;
  }

  .metric strong,
  .metric.accent strong {
    margin: 8px 0 4px;
    font-size: 28px;
  }

  .card,
  .map-card {
    padding: 15px !important;
    border-radius: 14px;
  }

  .usage-card,
  .lower-grid {
    margin-top: 12px;
  }

  .lower-grid,
  .report-grid {
    grid-template-columns: 1fr !important;
    gap: 12px;
  }

  .card-head,
  .section-head {
    width: 100%;
    margin-bottom: 14px;
    display: flex;
    flex-direction: column;
    align-items: stretch !important;
    gap: 12px;
  }

  .card h2,
  .section-head h2 {
    font-size: 18px;
    line-height: 1.3;
  }

  .card p,
  .section-head p {
    font-size: 12px;
    line-height: 1.55;
  }

  .periods {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
    overflow: visible;
    white-space: normal;
  }

  .periods button {
    min-width: 0;
    margin: 0 !important;
    padding: 10px 4px;
    font-size: 12px;
  }

  .chart {
    width: 100%;
    height: 235px !important;
    margin-top: 4px;
    overflow: hidden;
  }

  .chart svg,
  .chart canvas,
  img,
  video {
    max-width: 100%;
  }

  .legend {
    margin: 8px 0 0 !important;
    font-size: 11px;
    line-height: 1.4;
  }

  .donuts {
    height: 145px;
    gap: 12px;
  }

  .donut {
    width: 108px;
    height: 108px;
  }

  .progress {
    grid-template-columns: minmax(92px, auto) minmax(70px, 1fr) 38px;
    gap: 8px;
    margin: 18px 0;
    font-size: 11px;
  }

  /* Parking map: never keep six 70px cells on one 390px screen. */
  #map .section-head > div:first-child h2 {
    display: none;
  }

  .map-summary {
    width: 100%;
    padding: 11px 13px;
    display: flex;
    align-items: baseline;
    justify-content: center;
    gap: 6px;
    font-size: 12px;
  }

  .map-summary span {
    margin: 0 4px;
  }

  .map-card {
    width: 100%;
    min-height: 0 !important;
    gap: 10px !important;
    align-content: start;
    overflow: hidden;
  }

  #parking-map .map-row,
  #parking-map .map-row[style] {
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 10px !important;
  }

  #parking-map .spot {
    width: 100%;
    min-width: 0;
    min-height: 58px !important;
    height: auto;
    padding: 12px 5px;
    border-radius: 10px;
    font-size: 13px !important;
  }

  /* Keep wide validation tables inside their own scroll area. */
  .validation-card {
    padding: 0 !important;
    overflow: hidden;
  }

  .validation-scroll {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
  }

  .validation-card table {
    min-width: 680px;
  }

  .validation-card th,
  .validation-card td {
    padding: 11px 12px;
    font-size: 12px;
    white-space: nowrap;
  }

  .validation-history-title {
    margin-top: 20px;
  }

  .report-card button,
  .parking-report-tools button {
    width: 100%;
    margin: 10px 0 0 !important;
  }

  #parking-cameras .pc-summary,
  #parking-cameras .pc-grid,
  #parking-cameras .pc-fields,
  #parking-cameras .pc-slots {
    grid-template-columns: 1fr !important;
  }

  #parking-cameras input,
  #parking-cameras select,
  #parking-cameras button {
    max-width: 100%;
  }
}

@media (max-width: 370px) {
  main {
    padding-inline: 10px !important;
  }

  .sidebar .nav span {
    font-size: 8px;
  }

  .metric {
    min-height: 118px;
    padding: 12px !important;
  }

  .metric strong,
  .metric.accent strong {
    font-size: 25px;
  }

  #parking-map .map-row,
  #parking-map .map-row[style] {
    gap: 7px !important;
  }
}
