/* Shared page geometry: the operator guide and primary panels share their edges.
   Navigation, dialogs and printable reports keep their own layout. */
@media screen {
  :root {
    --portal-content-width: 1100px;
    --portal-page-gutter: clamp(12px, 2.5vw, 28px);
    --portal-panel-radius: 16px;
  }

  body :is(.container, .container-fluid, .operator-help-guide, .help-context, .main-card):not(header *, footer *, .modal *, .offcanvas *) {
    box-sizing: border-box;
    width: calc(100% - 2 * var(--portal-page-gutter)) !important;
    max-width: var(--portal-content-width) !important;
    min-width: 0;
    margin-inline: auto !important;
  }

  body :is(.container, .container-fluid):not(header *, footer *, .modal *, .offcanvas *) {
    padding-inline: 0 !important;
  }

  /* Nested wrappers must not add a second pair of margins. */
  body :is(.container, .container-fluid, .main-card) :is(.container, .container-fluid, .operator-help-guide, .help-context, .main-card):not(.modal *, .offcanvas *) {
    width: 100% !important;
    max-width: 100% !important;
  }

  body :is(.container, .container-fluid) :is(.col-12, .card-glass):not(.modal *, .offcanvas *) {
    max-width: 100% !important;
    min-width: 0 !important;
  }

  body :is(.operator-help-guide, .help-context) {
    border-radius: var(--portal-panel-radius) !important;
    box-shadow: 0 6px 20px rgba(0, 0, 0, .12) !important;
  }

  body :is(.card, .card-glass, .main-card, .section-card):not(.modal *, .offcanvas *) {
    border-radius: var(--portal-panel-radius);
  }

  body .table-responsive { min-width: 0; }

  /* Home carousel is a primary panel too. Images retain their aspect ratio. */
  #mainCarousel { width: 100%; max-width: 100% !important; }
}

/* Compact navigation keeps the original branding and dark palette. */
@media screen and (max-width: 991.98px) {
  body header .navbar > .container-fluid { flex-wrap: wrap; }
  body header .navbar .navbar-brand {
    flex: 1 1 0;
    min-width: 0;
    margin-right: .5rem;
    flex-wrap: nowrap;
  }
  body header .header-logo-wrap { width: 80px; flex-basis: 80px; height: 30px; }
  body header .header-logo { height: 30px; transform: none; }
  body header .header-brand-main { font-size: 1rem; }
  body header .header-page-title { font-size: .85rem; }
  body header .header-page-subtitle { font-size: .75rem; }
  body header .navbar-toggler { flex: 0 0 auto; min-width: 44px; min-height: 44px; }
  body header #navbarNav {
    flex-basis: 100%;
    min-width: 0;
    max-height: calc(100vh - var(--portal-header-height, 90px) - 12px);
    max-height: calc(100dvh - var(--portal-header-height, 90px) - 12px);
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  body header .navbar .navbar-nav .dropdown-menu {
    position: static !important;
    min-width: 0;
    width: 100%;
    border-radius: 10px;
    margin-block: .25rem .5rem;
  }
  body header .navbar :is(.nav-link, .dropdown-item) {
    white-space: normal;
    overflow-wrap: anywhere;
    min-height: 44px;
    padding: .65rem .75rem;
  }
  body header .navbar :focus-visible { outline: 2px solid #38bdf8; outline-offset: -2px; }
}

@media screen and (max-width: 767.98px) {
  html { scroll-padding-top: calc(var(--portal-header-height, 90px) + 12px); }
  body :is(.card-body, .tab-pane, .section-card) { overflow: visible; min-width: 0; }
  body :is(.table-responsive, .table-scroll, .scroll-wrapper) {
    max-width: 100%;
    min-width: 0;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
  }
  body .nav-tabs { scrollbar-width: thin; }
  body .nav-tabs::-webkit-scrollbar { display: block; height: 4px; }
  body .nav-tabs .nav-link { min-height: 44px; white-space: nowrap; }
  body :is(.card, .card-glass, .main-card, .help-context, .operator-help-guide) { overflow-wrap: anywhere; }
  body :is(.form-control, .form-select) { font-size: 16px; min-height: 44px; min-width: 0; }
  body .btn { min-height: 44px; white-space: normal; }
  body .btn-group { flex-wrap: wrap; }
  body .date-input { min-width: 0; margin-right: 0; }
  body .glass-section { padding: 12px; }
  body .table { font-size: .875rem; }
  body .table :is(td, th) { padding: .5rem; }
  body .modal { z-index: 1200; }
  body .modal-backdrop { z-index: 1190; }
  body .modal-dialog { margin: .5rem auto; width: calc(100% - 1rem); }
  body .modal-content { max-height: calc(100dvh - 1rem); }
  body .modal-body { overflow-y: auto; }
}

/* Preserve rows and columns, reducing typography and spacing to fit the panel. */
@media screen {
  body table.table {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100%;
    table-layout: fixed;
  }
  body table.table :is(th, td) {
    min-width: 0 !important;
    max-width: 100%;
    white-space: normal !important;
    overflow-wrap: anywhere;
  }
  body table.table :is(.btn, .badge, a, span, code, pre, .station-actions) {
    min-width: 0 !important;
    max-width: 100%;
    white-space: normal !important;
    overflow-wrap: anywhere;
  }
  body table.table :is(input, select, textarea, img) { min-width: 0; max-width: 100%; }
  body table.table .d-flex { flex-wrap: wrap; }
  body table.portal-table-fit {
    font-size: var(--table-fit-font) !important;
  }
  body table.portal-table-fit :is(th, td) {
    padding: var(--table-fit-padding) !important;
    font-size: inherit !important;
    line-height: 1.25;
  }
  body table.portal-table-fit :is(.btn, .badge, input, select, textarea, span, a, code, pre) {
    font-size: inherit !important;
  }
  body table.portal-table-fit :is(.btn, input, select, textarea) {
    padding: var(--table-fit-padding);
    min-height: 0;
  }
}
