/*
 * A.I.C. mobile foundation
 *
 * Global responsive safety layer loaded after feature stylesheets.
 * It intentionally owns only cross-screen mobile behaviour:
 * - viewport/safe-area containment
 * - touch/input ergonomics
 * - compact page context / toolbar behaviour
 * - scoped horizontal scrolling for high-density business tables
 * - modal/sheet sizing
 * - fixed/floating action containment
 *
 * Domain layout remains in each feature stylesheet.
 */

@media (max-width: 1024px) {
  :where(html.aic-layout-refactor),
  :where(html.aic-layout-refactor) body {
    max-width: 100%;
    overflow-x: hidden;
  }

  :where(html.aic-layout-refactor) .main-content,
  :where(html.aic-layout-refactor) .ui-page,
  :where(html.aic-layout-refactor) .ui-page__body,
  :where(html.aic-layout-refactor) .ui-hero__context,
  :where(html.aic-layout-refactor) .page-context,
  :where(html.aic-layout-refactor) .ui-section,
  :where(html.aic-layout-refactor) .ui-card {
    min-width: 0;
    max-width: 100%;
  }

  /* High-density operational tables scroll inside their own region only. */
  :where(html.aic-layout-refactor) :is(
    .table-wrap,
    .table-scroll,
    .table-shell,
    .ui-scroll-region,
    .ui-data-table-wrap
  ) {
    max-width: 100%;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }

  :where(html.aic-layout-refactor) :is(
    .plans-index-page,
    .plans-dispatch-confirm-page,
    .office-transfers-page,
    .support-tickets-page,
    .dispatched-waybills-index-page,
    .unused-dispatch-page,
    .waybills-page,
    .hazard-index-page,
    .place-index-page,
    .report-index-page,
    .waybills-page.admin-users-page,
    .waybills-page.deleted-admin-users-page,
    .waybills-page.admin-groups-page,
    .waybills-page.staff-groups-page
  ) :is(.table-wrap, .table-scroll, .table-shell) {
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x proximity;
    scrollbar-gutter: stable;
  }

  /* Avoid a wide child stretching the application shell. */
  :where(html.aic-layout-refactor) :is(
    .hero-search-row,
    .hero-search-grid,
    .hero-top,
    .filter-row,
    .filter-grid,
    .search-row,
    .bulk-bar,
    .toolbar,
    .page-toolbar
  ) {
    min-width: 0;
    max-width: 100%;
  }
}

@media (max-width: 720px) {
  :where(html.aic-layout-refactor) .main-content {
    min-width: 0;
    padding-right: 0;
    padding-left: 0;
  }

  :where(html.aic-layout-refactor) .ui-page,
  :where(html.aic-layout-refactor) .page-context {
    width: 100%;
  }

  /* Inputs never trigger iOS zoom and retain a comfortable touch target. */
  :where(html.aic-layout-refactor) :is(
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="hidden"]),
    select,
    textarea
  ) {
    min-width: 0;
    min-height: 44px;
    font-size: 16px;
    font-size: var(--ui-type-input-size-mobile, 16px);
  }

  :where(html.aic-layout-refactor) :is(
    button,
    input[type="button"],
    input[type="submit"],
    input[type="reset"],
    .btn,
    .ui-button-base,
    .btn-submit,
    .ui-btn
  ) {
    min-height: 44px;
  }

  :where(html.aic-layout-refactor) :is(
    .icon-btn,
    .btn-icon,
    .ui-btn--icon-only,
    .modal-close,
    .close-btn,
    .profile-modal__close,
    .dashboard-sidebar-toggle
  ) {
    width: 44px;
    min-width: 44px;
    height: 44px;
    min-height: 44px;
  }

  /* The page context becomes a compact, horizontally contained control band. */
  :where(html.aic-layout-refactor) .page-context {
    padding-right: 8px;
    padding-left: 8px;
  }

  :where(html.aic-layout-refactor) .page-context :is(.hero-top, .hero-search-row, .hero-search-grid) {
    width: 100%;
  }

  /* Tabs remain one line; the page never grows wider than the viewport. */
  :where(html.aic-layout-refactor) :is(
    .ui-tabs,
    .tabs,
    .group-dispatch-tabs
  ) {
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    flex-wrap: nowrap;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  :where(html.aic-layout-refactor) :is(
    .ui-tabs,
    .tabs,
    .group-dispatch-tabs
  )::-webkit-scrollbar {
    display: none;
  }

  :where(html.aic-layout-refactor) :is(
    .ui-tabs__link,
    .tab,
    .group-dispatch-tab
  ) {
    flex: 0 0 auto;
    min-height: 44px;
    white-space: nowrap;
  }

  /* Filters/searches stack without forcing fixed widths. */
  :where(html.aic-layout-refactor) :is(
    .hero-search-row,
    .filter-row,
    .search-row,
    .filter-grid
  ) {
    gap: 8px;
  }

  :where(html.aic-layout-refactor) :is(
    .hero-search-row,
    .filter-row,
    .search-row
  ) > * {
    min-width: 0;
    max-width: 100%;
  }

  /* Floating/bulk bars stay above the home indicator and inside the viewport. */
  :where(html.aic-layout-refactor) :is(
    .floating-bulk-action,
    .floating-actions,
    .sticky-actions,
    .mobile-sticky-actions
  ) {
    right: max(8px, var(--safe-right, 0px));
    bottom: max(8px, var(--safe-bottom, 0px));
    left: max(8px, var(--safe-left, 0px));
    max-width: calc(100vw - max(16px, var(--safe-left, 0px) + var(--safe-right, 0px)));
  }

  /* Generic dialogs become viewport-safe; feature-specific sizing may remain. */
  :where(html.aic-layout-refactor) :is(
    .ui-modal__dialog,
    .modal-dialog,
    .modal__dialog,
    .dialog,
    [role="dialog"]
  ) {
    max-width: calc(100vw - 16px);
  }

  /* Operational data grids deliberately keep horizontal scrolling, not body overflow. */
  :where(html.aic-layout-refactor) :is(
    .plans-index-page,
    .plans-dispatch-confirm-page,
    .office-transfers-page,
    .support-tickets-page,
    .dispatched-waybills-index-page,
    .unused-dispatch-page,
    .waybills-page,
    .hazard-index-page,
    .place-index-page,
    .report-index-page
  ) :is(.table-wrap, .table-scroll) {
    margin-right: -8px;
    margin-left: -8px;
    padding-right: 8px;
    padding-left: 8px;
  }

  /* Account and group administration: keep dense tables scoped, never page-wide. */
  :where(html.aic-layout-refactor) :is(
    .waybills-page.admin-users-page,
    .waybills-page.deleted-admin-users-page,
    .waybills-page.admin-groups-page,
    .waybills-page.staff-groups-page,
    .staff-group-form-page
  ) :is(.table, .table-wrap) {
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* Plan board is a board/canvas class screen: preserve lanes, contain scrolling. */
  :where(html.aic-layout-refactor) .plan-board-page :is(
    .plan-board-main-scroll,
    .plan-board-pool-scroll,
    .plan-board-lanes,
    .plan-board-panel
  ) {
    min-width: 0;
    max-width: 100%;
  }

  :where(html.aic-layout-refactor) .plan-board-page :is(
    .plan-board-main-scroll,
    .plan-board-pool-scroll
  ) {
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* User-facing cards should not inherit desktop fixed widths. */
  :where(html.aic-layout-refactor) :is(
    .ui-list-card,
    .dispatch-card,
    .hazard-item,
    .place-card,
    .support-card,
    .report-card
  ) {
    min-width: 0;
    max-width: 100%;
  }

  :where(html.aic-layout-refactor) :is(
    .ui-list-card,
    .dispatch-card,
    .hazard-item,
    .place-card,
    .support-card,
    .report-card
  ) :is(p, span, strong, dd, a) {
    overflow-wrap: anywhere;
  }

  /* Standalone/PWA safe bottom breathing room. */
  :where(html.aic-layout-refactor) .ui-page__footer,
  :where(html.aic-layout-refactor) .footer {
    padding-bottom: max(12px, var(--safe-bottom, 0px));
  }
}

@media (max-width: 480px) {
  :where(html.aic-layout-refactor) .main-content {
    padding-right: 0;
    padding-left: 0;
  }

  :where(html.aic-layout-refactor) .ui-hero {
    padding: 11px;
    border-radius: 14px;
  }

  :where(html.aic-layout-refactor) .ui-hero__title {
    font-size: 20px;
    line-height: 1.3;
  }

  :where(html.aic-layout-refactor) .ui-hero__subtitle {
    line-height: 1.5;
  }

  :where(html.aic-layout-refactor) .ui-list-card {
    padding: 12px;
  }

  :where(html.aic-layout-refactor) .ui-action-sheet__panel {
    right: 6px;
    bottom: max(6px, var(--safe-bottom, 0px));
    left: 6px;
    border-radius: var(--ui-radius-lg);
  }
}

@media (prefers-reduced-motion: reduce) {
  :where(html.aic-layout-refactor) :is(
    .table-wrap,
    .table-scroll,
    .ui-tabs,
    .ui-page-context-tabs,
    .ui-page-context-filter-strip
  ) {
    scroll-behavior: auto !important;
  }
}
