/* OrbisERP V96 · authenticated/public PWA responsive hardening
   Final mobile layer. Loaded after module CSS so legacy desktop geometry cannot
   leak into standalone/mobile views. POS keeps its dedicated responsive shell. */

@media (max-width:1024px){
  html,body{max-width:100%;}
  html.orbis-authenticated body.orbis-app:not(.pos-page),
  body.superadmin-app,
  body.orbis-public{
    overflow-x:hidden!important;
    -webkit-text-size-adjust:100%;
    text-size-adjust:100%;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(*, *::before, *::after),
  body.superadmin-app :where(*, *::before, *::after),
  body.orbis-public :where(*, *::before, *::after){box-sizing:border-box;}

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(img,video,canvas,svg),
  body.superadmin-app :where(img,video,canvas,svg),
  body.orbis-public :where(img,video,canvas,svg){max-width:100%;}

  /* Never allow an old desktop rail/min-width to widen the document. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) > main,
  html.orbis-authenticated body.orbis-app:not(.pos-page) > main[class],
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-page,.bo-page,.clients-page,.suppliers-page,.staff-page,
    .stock-page,.kardex-page,.inventory-unified,.inventory-unified-page,
    .purchases-page,.purchase-page,.purchase-fast-page,.purchase-create-v5-page,
    .sales-page,.monthly-page,.reports-center,.reports-shell,.company-settings-page,
    .user-activity-page,.data-center-page,.ops-page,.fiscal-page,.transfer-page,
    .transfer-order-page,.scanner-workstation,.main-content,.sales-hub,.crm-page
  ){
    min-width:0!important;
    max-width:100%!important;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) > main :where(
    section,article,aside,header,footer,form,fieldset,div,label
  ){
    min-width:0;
  }

  /* Flex rows must wrap instead of pushing controls outside the screen. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-header-actions,.workspace-actions,.workspace-card-head,.workspace-filter,
    .header-actions,.simple-header-actions,.workstation-actions,.reports-actions,
    .hub-header__actions,.inventory-unified__actions,.stock-actions,.row-actions,
    .profile-actions,.bo-header-actions,.bo-inline,.actions-bar,.panel-header,
    .purchase-actions,.order-actions,.sales-actions,.staff-actions,.supplier-actions,
    .cash-branch-switcher,.data-hero,.ops-hero,.transfer-header
  ){
    min-width:0;
    flex-wrap:wrap;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    input:not([type=checkbox]):not([type=radio]):not([type=range]),select,textarea
  ){
    min-width:0!important;
    max-width:100%!important;
  }

  /* Scrollable tables: keep the page itself fixed-width. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-table-wrap,.table-wrap,.table-responsive,.table-container,.table-card,
    .purchases-table-wrap,.lines-table-wrap,.return-items-table,.audit-table,
    .history-table-wrap,.monthly-table-wrap,.stock-table-wrap,.sales-table-wrap,
    .transfer-table-wrap,.bo-table-wrap,.gov-table-wrap,.data-table-wrap
  ){
    width:100%!important;
    max-width:100%!important;
    overflow-x:auto!important;
    overflow-y:hidden;
    -webkit-overflow-scrolling:touch;
    overscroll-behavior-x:contain;
  }

  /* A few legacy screens place tables directly inside cards. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-card,.bo-card,.acc-card,.content-card,.panel,.audit-card,.warehouse-panel
  ) > table{
    display:block!important;
    width:100%!important;
    max-width:100%!important;
    overflow-x:auto!important;
    -webkit-overflow-scrolling:touch;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) table{max-width:100%;}

  /* Dialogs/sheets must fit the visual viewport and the on-screen keyboard. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    dialog,.simple-dialog,.workspace-dialog-card,.seat-dialog-card,.sa-modal,
    .wa-modal,.chat-modal,.filters-popover,.orbis-feedback-dialog,.orbis-confirm-dialog
  ),
  body.superadmin-app :where(dialog,.sa-modal,.sa-reset-modal,.sa-create-company-modal){
    max-width:calc(100vw - 20px)!important;
    max-height:calc(100dvh - 20px)!important;
  }
}

@media (max-width:900px){
  /* Consistent page gutters. Do not touch dedicated chat/WA full-height shells. */
  html.orbis-authenticated body.orbis-app:not(.pos-page):not(.chat-page):not(.wa-page):not(.pda-body):not(.scanner-mode-page):not(.cashier-home) > main{
    padding-left:max(14px,env(safe-area-inset-left))!important;
    padding-right:max(14px,env(safe-area-inset-right))!important;
    padding-bottom:calc(24px + env(safe-area-inset-bottom))!important;
  }

  /* Statistics stay as compact 2xN cards on PWA instead of long single columns. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-grid,.bo-kpis,.staff-summary,.metric-grid,.stats-grid,.kpi-strip,
    .activity-stats,.retail-page-banner__stats,.purchase-metrics,.order-overview,
    .summary-grid,.report-summary,.dashboard-stats,.sa-kpis
  ),
  body.superadmin-app :where(.sa-kpis){
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
    gap:8px!important;
  }

  /* Main two/three-column workspaces collapse before controls get cramped. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-columns,.workspace-columns.equal,.settings-workspace,.currency-workspace-columns,
    .data-flow,.return-sale-result,.order-bottom,.purchase-workspace,.inventory-board-shell,
    .crm-workspace,.ops-layout,.report-layout,.detail-layout
  ){
    grid-template-columns:minmax(0,1fr)!important;
  }

  /* Forms use two columns on small tablets, then one on phones. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-form,.settings-grid,.bo-form-grid,.edit-grid,.form-grid,.data-fields,
    .sa-client-modal-grid,.assignment-grid,.warehouse-columns,.purchase-form-grid
  ){
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
  }

  /* Directory/list cards that used desktop row grids. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .client-directory,.supplier-directory,.staff-collection.is-grid,.products-grid,
    .warehouses-grid,.inventory-grid,.companies-grid,.category-workspace-grid,.plan-grid
  ){
    max-width:100%!important;
  }

  /* Purchase/orders: desktop tables and add-line grids are common overflow sources. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) .add-product-form{
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) .add-product-form :where(.product-picker,.tax-picker){
    grid-column:1/-1!important;
  }

  /* Accounting had a hard 280px memo input. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) .acc-actions input[name="memo"]{
    min-width:0!important;
    width:100%!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) .acc-grid{
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
  }

  /* Superadmin: sidebar becomes a native drawer and KPI cards remain 2-up. */
  body.superadmin-app .sa-app{margin-left:0!important;max-width:100%!important;}
  body.superadmin-app .sa-content{
    width:100%!important;
    max-width:100%!important;
    padding:12px max(12px,env(safe-area-inset-right)) calc(22px + env(safe-area-inset-bottom)) max(12px,env(safe-area-inset-left))!important;
  }
  body.superadmin-app :where(.sa-grid-3,.sa-grid-2,.sa-wa-platform-grid){grid-template-columns:1fr!important;}
  body.superadmin-app .sa-panel{min-width:0!important;}
  body.superadmin-app :where(.clients-table,.sa-table){max-width:100%;}

  /* PWA standalone: no horizontal bounce from legacy widths. */
  @media (display-mode:standalone){
    html.orbis-authenticated body.orbis-app:not(.pos-page),body.superadmin-app{overscroll-behavior-x:none;}
  }
}

@media (max-width:760px){
  /* Headers become a predictable vertical block. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-header,.bo-header,.page-header,.workstation-header,.stock-header,
    .clients-header,.suppliers-header,.purchases-header,.sales-header,.staff-header,
    .reports-header,.history-header,.monthly-header,.kardex-header,.company-settings-header,
    .profile-header,.hub-header,.cash-header,.data-hero,.ops-hero,.order-header,
    .purchase-setup-header,.transfer-header,.inventory-unified__header
  ),
  body.superadmin-app .sa-page-head{
    align-items:stretch!important;
    flex-direction:column!important;
    min-height:0!important;
    gap:10px!important;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-header-actions,.header-actions,.simple-header-actions,.bo-header-actions,
    .reports-actions,.hub-header__actions,.inventory-unified__actions,.stock-actions
  ),
  body.superadmin-app .sa-page-actions{
    width:100%!important;
    max-width:100%!important;
    justify-content:flex-start!important;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-form,.workspace-form.compact,.settings-grid,.bo-form-grid,.edit-grid,
    .form-grid,.form-row,.data-fields,.assignment-grid,.warehouse-columns,
    .purchase-form-grid,.sa-client-modal-grid
  ),
  body.superadmin-app :where(.sa-client-modal-grid,.sa-create-grid,.sa-wa-fields){
    grid-template-columns:minmax(0,1fr)!important;
  }

  /* Do not let legacy span classes leave empty columns after collapsing. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(.span-2,.span-3,.full),
  body.superadmin-app :where(.span-2,.span-3){grid-column:auto!important;}

  /* Mobile filters/search use the whole row. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .clients-toolbar,.supplier-toolbar,.staff-toolbar,.filters-panel,.filter-bar,
    .filter-controls,.stock-filters,.date-fields,.cash-branch-switcher
  ){
    width:100%!important;
    max-width:100%!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .clients-toolbar,.supplier-toolbar,.staff-toolbar,.filter-bar,.date-fields
  ){
    grid-template-columns:minmax(0,1fr)!important;
  }

  /* Customer/supplier/staff rows become touch-friendly cards. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(.client-directory,.supplier-directory){
    display:grid!important;
    grid-template-columns:1fr!important;
    gap:9px!important;
    border:0!important;
    background:transparent!important;
    overflow:visible!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(.directory-head,.supplier-head,.staff-list-head){display:none!important;}
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(.client-record,.supplier-record){
    width:100%!important;
    max-width:100%!important;
  }

  /* Transfer screens: the warehouse rail cannot consume half a phone screen. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) .logistics-grid{
    grid-template-columns:1fr!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) .warehouse-sidebar{
    position:static!important;
    width:100%!important;
    max-width:none!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) .tabs-nav{
    width:100%!important;
    overflow-x:auto!important;
    -webkit-overflow-scrolling:touch;
  }

  /* Cards/modals use the viewport instead of desktop minimums. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-card,.bo-card,.audit-card,.ops-card,.report-card,.content-card
  ){
    max-width:100%!important;
  }

  /* Purchase line editor. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) .add-product-form{
    grid-template-columns:1fr!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) .add-product-form > *{
    grid-column:auto!important;
  }

  /* Accounting actions stack cleanly without shrinking date/memo controls. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) .acc-actions{
    align-items:stretch!important;
    flex-direction:column!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) .acc-actions > *{max-width:100%!important;}

  /* Superadmin touch targets + 2-column metrics, not a long 1-column wall. */
  body.superadmin-app .sa-kpis{grid-template-columns:repeat(2,minmax(0,1fr))!important;}
  body.superadmin-app .sa-toolbar{align-items:stretch!important;flex-direction:column!important;}
  body.superadmin-app :where(.sa-search,.sa-search.mini){width:100%!important;}
  body.superadmin-app .sa-filter-pills{max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;}

  /* Public/login/onboarding PWA. */
  body.orbis-public main{width:100%;max-width:100%;padding-left:max(14px,env(safe-area-inset-left));padding-right:max(14px,env(safe-area-inset-right));}
  body.orbis-public :where(.auth-shell,.onboarding-shell,.plan-shell-v45){width:100%!important;max-width:100%!important;}
  body.orbis-public .auth-shell{grid-template-columns:1fr!important;}
  body.orbis-public .auth-story{display:none!important;}
  body.orbis-public :where(.auth-panel,.onboarding-panel,.plan-panel-v45){width:100%!important;max-width:560px!important;margin-inline:auto!important;}
  body.orbis-public .plan-grid-v45{grid-template-columns:1fr!important;}
}

@media (max-width:620px){
  :root{--orbis-pwa-card-pad:13px;}

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    h1,.workspace-header h1,.bo-header h1,.page-header h1,.page-header h2,
    .clients-header h1,.suppliers-header h1,.staff-header h1,.reports-header h1,
    .purchases-header h1,.stock-header h1,.monthly-header h1
  ){
    max-width:100%;
    overflow-wrap:anywhere;
  }

  /* Keep statistics 2-up, but make their contents compact. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-grid,.bo-kpis,.staff-summary,.metric-grid,.stats-grid,.kpi-strip,
    .activity-stats,.retail-page-banner__stats,.purchase-metrics,.order-overview,
    .summary-grid,.report-summary,.dashboard-stats
  ),body.superadmin-app .sa-kpis{
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
    gap:7px!important;
  }
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-metric,.metric-card,.stat-card,.kpi-card,.bo-kpis article,.staff-summary>div
  ),body.superadmin-app .sa-kpis article{
    min-width:0!important;
    min-height:92px!important;
    padding:12px!important;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-card,.bo-card,.ops-card,.audit-card,.report-card,.content-card
  ){
    padding:var(--orbis-pwa-card-pad)!important;
    border-radius:12px!important;
  }

  /* Primary actions fill available width only when a header is stacked. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-header-actions,.simple-header-actions,.header-actions,.bo-header-actions,
    .reports-actions,.hub-header__actions,.inventory-unified__actions
  ) > :where(a,button){
    flex:1 1 auto;
    min-height:42px;
    justify-content:center;
  }

  /* Small-screen dialogs behave like bottom sheets without escaping the viewport. */
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .simple-dialog,.workspace-dialog-card,.seat-dialog-card,.wa-small-modal,.filters-popover
  ),body.superadmin-app :where(.sa-modal,.sa-reset-modal,.sa-create-company-modal){
    width:calc(100vw - 16px)!important;
    max-width:calc(100vw - 16px)!important;
    margin-inline:auto!important;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .simple-dialog-actions,.workspace-dialog-actions,.actions-bar
  ),body.superadmin-app :where(.sa-modal footer,.sa-reset-form footer,.sa-create-company-form>footer,.sa-wa-platform-form footer){
    width:100%!important;
  }

  /* Existing WhatsApp/chat shells already own their mobile geometry; only ensure
     the connection/setup modals cannot become wider than the phone. */
  body.wa-page :where(.wa-account-setup-modal,.wa-modal){max-width:calc(100vw - 12px)!important;}
  body.chat-page :where(.chat-modal,.chat-modal-small){max-width:calc(100vw - 12px)!important;}
}

@media (max-width:390px){
  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-grid,.bo-kpis,.staff-summary,.metric-grid,.stats-grid,.kpi-strip,
    .activity-stats,.retail-page-banner__stats,.purchase-metrics,.order-overview
  ),body.superadmin-app .sa-kpis{gap:6px!important;}

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(
    .workspace-metric,.metric-card,.stat-card,.kpi-card,.bo-kpis article,.staff-summary>div
  ),body.superadmin-app .sa-kpis article{
    padding:10px!important;
  }

  html.orbis-authenticated body.orbis-app:not(.pos-page) :where(.workspace-card,.bo-card,.ops-card,.report-card){padding:12px!important;}
  body.orbis-public main{padding-left:max(10px,env(safe-area-inset-left));padding-right:max(10px,env(safe-area-inset-right));}
}

/* iOS/PWA keyboard guard: prevent fixed-height legacy screens from clipping forms. */
@media (max-width:900px) and (max-height:560px){
  html.orbis-authenticated body.orbis-app:not(.pos-page):not(.chat-page):not(.wa-page) :where(dialog,.simple-dialog,.workspace-dialog-card),
  body.superadmin-app :where(dialog,.sa-modal),
  body.orbis-public :where(.auth-panel,.onboarding-panel,.plan-panel-v45){
    max-height:calc(100dvh - 10px)!important;
    overflow-y:auto!important;
  }
}
