/* Mobile viewport contract. Load after all other lab stylesheets.
   Keep only the engine scene and eight-instrument summary pinned. The detailed
   monitor, notes and every control stay in the document's single scroll flow.
   These overrides intentionally replace the older whole-card/nested scrollers. */
@media (max-width: 800px), (max-width: 1100px) and (max-height: 600px) and (orientation: landscape) {
  :root {
    --mobile-safe-top: env(safe-area-inset-top, 0px);
    --mobile-pin-budget: min(48vh, 340px);
    --mobile-live-height: calc(var(--mobile-pin-budget) - var(--mobile-safe-top));
    --mobile-scene-height: calc(var(--mobile-live-height) * .58);
    --mobile-gauge-height: calc(var(--mobile-live-height) * .42);
    scroll-padding-top: calc(var(--mobile-pin-budget) + 12px);
  }

  @supports (height: 100svh) {
    :root { --mobile-pin-budget: min(48svh, 340px); }
  }

  body { padding-bottom: env(safe-area-inset-bottom, 0px); }

  .workspace {
    display: block;
    height: auto;
    min-height: 0;
    max-height: none;
    overflow: visible;
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
  }

  /* Removing this wrapper's layout box lets both sticky siblings use the
     entire workspace (including its controls) as their containing block. */
  .stand-display,
  body[data-lab-mode] .stand-display {
    display: contents;
    position: static;
    height: auto;
    min-height: 0;
    max-height: none;
    overflow: visible;
    overscroll-behavior: auto;
  }

  .stand-head {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 7px 7px 0 0;
    padding: 7px 10px;
    gap: 8px;
  }
  .stand-head > div { min-width: 0; flex-wrap: wrap; gap: 2px 8px; }
  .stand-head > span { flex-shrink: 0; }

  .stand-display > .scene {
    position: sticky;
    top: var(--mobile-safe-top);
    z-index: 5;
    display: flex;
    flex-direction: column;
    height: var(--mobile-scene-height);
    min-height: 0;
    max-height: var(--mobile-scene-height);
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    background: #edf1f4;
  }
  .scene-view-switch { flex: 0 0 auto; padding: 2px 6px; gap: 4px; }
  .scene-view-switch button { min-height: 32px; padding: 4px 8px; font-size: 10px; }
  .stand-display .engine-stage {
    flex: 1 1 0;
    width: 100%;
    height: auto;
    min-height: 0;
    max-height: none;
    aspect-ratio: auto;
  }
  .stand-display .scene .engine-stage svg { height: 100%; max-height: none; }
  .flow-legend { flex: 0 0 auto; padding: 2px 7px; gap: 4px 7px; font-size: 7px; line-height: 1.2; }
  .flow-legend small { display: none; }
  .flow-legend span:before { width: 10px; margin-right: 3px; }
  .scene-bottom { flex: 0 0 auto; padding: 2px 7px 4px; gap: 8px; font-size: 8px; line-height: 1.2; }
  .scene-bottom > span { min-width: 0; }

  .stand-display > .gauges {
    position: sticky;
    top: calc(var(--mobile-safe-top) + var(--mobile-scene-height));
    z-index: 5;
    height: var(--mobile-gauge-height);
    min-height: 0;
    max-height: var(--mobile-gauge-height);
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
    gap: 4px 6px;
    padding: 5px 7px;
    border: 1px solid var(--border);
    background: #dde3e8;
    box-shadow: 0 5px 9px #183c4b1c;
  }
  .gauges .gauge {
    display: grid;
    grid-template-columns: minmax(0, 30px) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    column-gap: 3px;
    align-items: center;
    min-height: 0;
  }
  .gauges .gauge svg {
    grid-column: 1;
    grid-row: 1;
    width: 100%;
    height: auto;
    max-height: 100%;
    filter: none;
  }
  .gauges .mobile-value {
    display: block;
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
    margin: 0;
    text-align: center;
    font-size: clamp(11px, 3.2vw, 15px);
    line-height: 1.15;
    letter-spacing: -.4px;
    white-space: nowrap;
  }
  .gauges .gauge-label {
    grid-column: 1 / -1;
    grid-row: 2;
    min-height: 2.2em;
    margin: 1px 0 0;
    font-size: 8px;
    line-height: 1.1;
    letter-spacing: 0;
    overflow-wrap: anywhere;
  }
  /* Keep units readable without relying on the miniature dial-face type. */
  .gauges [data-instrument="manifoldInHg"] .gauge-label::after { content: " · inHg"; }
  .gauges [data-instrument="torque"] .gauge-label::after { content: " · lb·ft"; }
  .gauges [data-instrument="hp"] .gauge-label::after { content: " · hp"; }
  .gauges [data-instrument="thrust"] .gauge-label::after { content: " · lbf"; }
  .gauges [data-instrument="fuel"] .gauge-label::after { content: " · gal/hr"; }
  .gauges [data-instrument="oilPressure"] .gauge-label::after { content: " · PSI"; }
  .gauges [data-instrument="oil"] .gauge-label::after { content: " · °F"; }

  /* These are deliberately outside the pinned height budget, and never clipped. */
  #instrumentBandNote { margin: 0; padding: 7px 10px; font-size: 9px; }
  #temperatureMonitor { min-width: 0; }
  .oil-summary { flex-wrap: wrap; }
  .stand-footer { background: #fff; padding: 9px 10px; min-height: 0; }
  .observation-strip { flex-wrap: wrap; }
  .roughness-track { min-width: 50px; }

  .console {
    height: auto;
    min-height: 0;
    max-height: none;
    overflow: visible;
    overscroll-behavior: auto;
    margin-top: 14px;
    padding: 15px;
  }
  .console .power-buttons { position: static; top: auto; padding: 0; }
  .power-buttons { flex-wrap: wrap; }
  .power-buttons .primary { min-width: 130px; }
  .operation-buttons { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .operation-buttons button { min-width: 0; min-height: 46px; }
  .control-panel { min-height: 0; }
  .console select { width: 100%; max-width: 100%; font-size: 16px; }
  .slider-control input { min-height: 30px; }
  .mag-switch button, .primer-panel > button:first-child { min-height: 44px; }
  .exercise-debrief ol { max-height: none; overflow: visible; }

  .mode-bar, .audio-status { padding-left: max(12px, env(safe-area-inset-left, 0px)); padding-right: max(12px, env(safe-area-inset-right, 0px)); }
  .mode-bar { flex-wrap: wrap; }
  .sound-tools { flex-wrap: wrap; }
  .sound-tools label { min-height: 40px; }
  .sound-tools input { max-width: 100%; }
  .audio-status p { overflow-wrap: anywhere; }

  dialog { max-height: calc(100vh - 24px); }
  @supports (height: 100svh) {
    dialog { max-height: calc(100svh - 24px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)); }
  }
}

/* Very short screens, landscape browser chrome and the on-screen keyboard get
   a completely ordinary page. Pinning must never consume the control area. */
@media (max-width: 1100px) and (max-height: 520px) {
  :root { scroll-padding-top: 12px; }
  .workspace { display: block; height: auto; min-height: 0; max-height: none; overflow: visible; }
  .stand-display,
  body[data-lab-mode] .stand-display { position: static; height: auto; max-height: none; overflow: visible; overscroll-behavior: auto; }
  .stand-display > .scene { position: relative; top: auto; height: 210px; max-height: none; }
  .stand-display > .gauges { position: relative; top: auto; height: 128px; max-height: none; }
  .console { height: auto; max-height: none; overflow: visible; overscroll-behavior: auto; margin-top: 14px; }
  .console .power-buttons { position: static; top: auto; }
}

@media (min-width: 600px) and (max-width: 1100px) and (max-height: 520px) and (orientation: landscape) {
  .stand-display > .gauges { grid-template-columns: repeat(8, minmax(0, 1fr)); grid-template-rows: minmax(0, 1fr); height: 72px; }
}
