/* One visible viewport and safe area for every mobile route, including documents. */
:root {
  --app-safe-top: env(safe-area-inset-top, 0px);
  --app-safe-right: env(safe-area-inset-right, 0px);
  --app-safe-bottom: env(safe-area-inset-bottom, 0px);
  --app-safe-left: env(safe-area-inset-left, 0px);
}

.delivery-field-navigation { display: none; }

@media screen and (max-width: 1080px) {
  html, body { height: 100%; overflow: hidden; }
  .v2-root {
    position: fixed;
    top: var(--app-viewport-top, 0px);
    left: 0;
    width: 100%;
    height: var(--app-viewport-height, 100dvh);
    min-height: 0;
    overflow: hidden;
  }
  .shell { height: 100%; min-height: 0; overflow: hidden; }
  .shell > .workspace {
    height: 100%; min-height: 0;
    grid-template-rows: auto minmax(0, 1fr);
  }
  .workspace > .topbar {
    position: relative; top: auto; min-height: 61px;
    padding: calc(8px + var(--app-safe-top)) max(12px, var(--app-safe-right)) 8px max(12px, var(--app-safe-left));
    background: #fff;
  }
  .workspace > .module-host {
    min-height: 0; overflow: auto; overscroll-behavior: contain;
    padding-left: max(12px, var(--app-safe-left));
    padding-right: max(12px, var(--app-safe-right));
    padding-bottom: max(18px, var(--app-safe-bottom));
    scroll-padding-top: 76px;
  }
  .workspace > .module-host:has(.reports-v2) { overflow: hidden; }
  .mobile-phase-tabs { top: 0; }
  /* Stable command geometry while focus opens/closes the keyboard controls. */
  .module-host .delivery-v2 { padding-bottom: calc(68px + var(--app-safe-bottom)); }
  .delivery-field-navigation:not([hidden]) {
    display: flex; align-items: center; gap: 8px; position: fixed; z-index: 27;
    top: calc(var(--app-viewport-top, 0px) + var(--app-viewport-height, 100dvh));
    transform: translateY(-100%); left: 0; right: 0;
    padding: 6px max(12px, var(--app-safe-right)) max(6px, var(--app-safe-bottom)) max(12px, var(--app-safe-left));
    border-top: 1px solid var(--line); background: #fff;
  }
  .delivery-field-navigation .btn { display: inline-flex; align-items: center; justify-content: center; flex: 0 0 44px; width: 44px; height: 44px; padding: 8px; }
  .delivery-field-navigation [data-field-previous] img { transform: rotate(180deg); }
  .delivery-field-navigation > span { flex: 1; min-width: 0; font-size: 13px; font-weight: 650; overflow-wrap: anywhere; }
  html[data-compact-viewport] .module-host :is(.delivery-form-footer, .collection-v3-savebar, .challan-bottom-bar, .mobile-phase-actions) {
    position: static;
  }
  /* Challan bars own grid rows; only the active form/records pane scrolls. */
  .workspace > .module-host:has(.challan-v2) { overflow: hidden; padding: 0; }
  .module-host > .challan-v2 {
    display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
    height: 100%; min-height: 0; gap: 0; padding: 0; overflow: hidden;
  }
  .challan-v2 > .mobile-phase-tabs {
    position: static; margin: 0;
    padding: 8px max(12px, var(--app-safe-right)) 8px max(12px, var(--app-safe-left));
    border-bottom: 1px solid var(--line);
  }
  .module-host .challan-v2 > :is(.challan-header-card, .challan-workbench) {
    height: 100%; min-height: 0; overflow: auto; overscroll-behavior: contain;
    padding: 0 max(12px, var(--app-safe-right)) 12px max(12px, var(--app-safe-left));
    scroll-padding-block: 12px; align-content: start;
  }
  .module-host .challan-v2 > .challan-header-card { padding-top: 12px; }
  .challan-v2 > :is(.challan-bottom-bar, .mobile-phase-actions:not(:empty)) {
    position: static; margin: 0; width: auto; border-radius: 0;
    padding: 8px max(12px, var(--app-safe-right)) max(8px, var(--app-safe-bottom)) max(12px, var(--app-safe-left));
  }
  .shell .side-rail {
    top: var(--app-viewport-top, 0px); bottom: auto;
    height: var(--app-viewport-height, 100dvh);
    padding: calc(14px + var(--app-safe-top)) max(14px, var(--app-safe-right)) max(16px, var(--app-safe-bottom)) max(14px, var(--app-safe-left));
  }
  .mobile-menu-backdrop, .app-modal-backdrop, .delivery-confirm-backdrop {
    top: var(--app-viewport-top, 0px); bottom: auto;
    height: var(--app-viewport-height, 100dvh);
    padding: calc(12px + var(--app-safe-top)) max(12px, var(--app-safe-right)) calc(12px + var(--app-safe-bottom)) max(12px, var(--app-safe-left));
  }
  .app-modal, .delivery-confirm-dialog {
    max-height: 100%; min-height: 0; overflow: auto;
  }
  body:has(.v2-root) dialog[open] {
    top: calc(var(--app-viewport-top, 0px) + var(--app-safe-top) + 12px);
    bottom: calc(100% - var(--app-viewport-top, 0px) - var(--app-viewport-height, 100dvh) + var(--app-safe-bottom) + 12px);
    left: var(--app-safe-left); right: var(--app-safe-right);
    margin: auto;
    max-width: calc(100% - var(--app-safe-left) - var(--app-safe-right) - 24px);
    max-height: calc(var(--app-viewport-height, 100dvh) - var(--app-safe-top) - var(--app-safe-bottom) - 24px);
  }
  .v2-root > .report-viewer { height: 100%; min-height: 0; padding-bottom: var(--app-safe-bottom); }
  .report-viewer .report-viewer-toolbar {
    flex-shrink: 0;
    padding: calc(10px + var(--app-safe-top)) max(16px, var(--app-safe-right)) 10px max(16px, var(--app-safe-left));
  }
  .v2-root > .auth-screen {
    min-height: 0; height: 100%; overflow: auto;
    padding: calc(24px + var(--app-safe-top)) max(24px, var(--app-safe-right)) calc(24px + var(--app-safe-bottom)) max(24px, var(--app-safe-left));
  }
}
