/* Reglas compartidas al final de la cascada: una sola barra por vista. */
.module-bar-links { display: contents; }
body.has-module-bar .mobile-page-topbar,
body.has-section-bar .mobile-page-topbar { display: none !important; }

/* Incluye tablets grandes con pantalla táctil y teléfonos en horizontal. */
@media (max-width: 1100px), (hover: none) and (pointer: coarse) {
  body {
    --header-row: 66px;
    --header-height: calc(var(--header-row) + env(safe-area-inset-top, 0px));
    --panel-gap: 12px;
    min-height: 0;
    height: 100dvh;
  }

  body.has-module-bar {
    --header-height: calc(var(--header-row) + 56px + env(safe-area-inset-top, 0px));
  }

  .nav-toggle {
    top: calc(env(safe-area-inset-top, 0px) + (var(--header-row) - 44px) / 2);
    left: calc(env(safe-area-inset-left, 0px) + 12px);
  }

  .mobile-page-topbar,
  .section-bar {
    height: var(--header-height);
    padding: env(safe-area-inset-top, 0px) calc(env(safe-area-inset-right, 0px) + 12px) 0 calc(env(safe-area-inset-left, 0px) + 72px);
  }

  .mobile-page-topbar {
    position: fixed;
    inset: 0 0 auto;
    z-index: 18;
    display: flex;
    align-items: center;
    background: rgba(245, 243, 234, .96);
    border-bottom: 1px solid var(--line);
    pointer-events: none;
  }

  .mobile-page-topbar-title {
    min-width: 0;
    overflow: hidden;
    font-size: 20px;
    font-weight: 850;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .module-bar.is-visible:not([hidden]) {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: var(--header-row) 56px;
    gap: 0;
    height: var(--header-height);
    padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) 0 env(safe-area-inset-left, 0px);
  }

  .module-bar-title {
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 0 12px 0 72px;
    font-size: 20px;
  }

  .module-bar-links {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
    height: 56px;
    padding: 4px 12px;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
    scroll-padding-inline: 12px;
  }

  .module-bar-links .module-link {
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
  }

  /* La altura de paneles depende del espacio restante, no de otro 100vh. */
  html body.modules-page > .page-shell {
    align-items: stretch;
    align-content: stretch;
    grid-template-rows: minmax(0, 1fr);
    width: 100%;
    height: 100dvh;
    min-height: 0;
    max-height: 100dvh;
    padding: calc(var(--header-height) + var(--panel-gap)) calc(env(safe-area-inset-right, 0px) + 12px) calc(env(safe-area-inset-bottom, 0px) + 12px) calc(env(safe-area-inset-left, 0px) + 12px) !important;
    overflow: hidden;
  }

  body .page-shell > :not([hidden]) {
    width: 100%;
    min-width: 0;
    height: 100% !important;
    min-height: 0 !important;
    max-height: 100% !important;
    margin: 0;
    overflow: auto;
    overscroll-behavior: contain;
  }

  .module-grid {
    height: auto;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: none;
    grid-auto-rows: minmax(118px, auto);
  }

  .module-card { min-width: 0; min-height: 118px; }
  .module-grid .module-card[href^="#modulos-"] { grid-column: auto; grid-row: auto; }
  .module-card-copy { width: 100%; }
  .module-card-copy small { white-space: normal; overflow-wrap: anywhere; }
  .module-card-loader { position: absolute; top: 30px; right: 14px; bottom: auto; }

  body.has-tunnels-view,
  body.has-cameras-map-view { --panel-gap: 0px; }

  html body.has-tunnels-view > .page-shell,
  html body.has-cameras-map-view > .page-shell {
    padding-right: env(safe-area-inset-right, 0px) !important;
    padding-bottom: 0 !important;
    padding-left: env(safe-area-inset-left, 0px) !important;
  }

  body .page-shell > .tunnels-map-view,
  body .page-shell > .cameras-map-view {
    overflow: hidden;
    padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px)) !important;
  }

  body.has-tunnels-view .tunnel-action-dock,
  body.has-cameras-map-view .camera-action-dock {
    bottom: calc(8px + env(safe-area-inset-bottom, 0px)) !important;
    max-width: calc(100vw - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px) - 20px) !important;
  }

  body.has-cameras-map-view .camera-action-dock {
    right: calc(10px + env(safe-area-inset-right, 0px)) !important;
    left: calc(10px + env(safe-area-inset-left, 0px)) !important;
    width: auto !important;
    transform: none !important;
  }

  .sidebar {
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    overflow-y: auto;
  }

  /* El botón de cierre del drawer se mantiene sobre su fondo en móvil. */
  body:has(.sidebar.is-open) .nav-toggle { z-index: 2147483001; }
}

@media (min-width: 701px) and (max-width: 1100px), (min-width: 701px) and (hover: none) and (pointer: coarse) {
  .module-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (orientation: landscape) and (max-height: 700px) {
  body { --header-row: 52px; }
}
