/* Shared iOS / Android responsive adjustments.
   Loaded after the portal and operations styles so page-specific rules keep priority. */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body { max-width: 100%; overflow-x: hidden; }

button, a, input, select, textarea { touch-action: manipulation; }
button, .ps-btn, .ops-btn, .sheet-add, .sheet-tool, .sheet-batch-del { min-height: 44px; }
input, select, textarea { font-size: 16px; } /* prevents Safari focus zoom */
.ops-mobile-logout { display: none; }

@media (max-width: 767px) {
  /* Public portal shell */
  .ps-outer {
    min-height: 100dvh;
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }
  .ps-shell { min-height: calc(100dvh - 16px); border-radius: 12px; }
  .ps-nav { padding: 14px 14px 12px; gap: 10px; }
  .ps-brand { min-width: 0; }
  .ps-brand span { max-width: 48vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; letter-spacing: .16em; }
  .ps-nav-actions { width: 100%; gap: 8px; }
  .ps-nav-actions > .ps-btn { flex: 1 1 0; padding: 10px 8px; }
  .ps-nav-links {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
    order: 3;
    gap: 4px;
  }
  .ps-nav-links a { min-height: 44px; padding: 10px 8px; text-align: center; }
  .ps-main { padding: 24px 14px 34px; }
  .ps-footer { padding: 14px; flex-direction: column; align-items: flex-start; }
  .ps-footer-links { flex-wrap: wrap; gap: 10px 14px; }
  .ps-card { padding: 18px 16px; }
  .ps-field label { width: auto; min-width: 0; max-width: none; }
  .ps-field input, .ps-field select, .ps-field textarea { width: 100%; min-width: 0; }
  .ps-login-wrap .ps-card { padding: 22px 16px; }
  .ps-price-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .ps-price-table { min-width: 480px; }
  .ps-price-table th, .ps-price-table td { padding: 11px 9px; }

  /* Operations console: turn the fixed desktop sidebar into a compact scrollable bar. */
  .ops-body { overflow: auto; min-height: 100dvh; }
  .ops-outer { height: auto; min-height: 100dvh; padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)); }
  .ops-shell { grid-template-columns: 1fr; height: auto; min-height: calc(100dvh - 16px); overflow: visible; border-radius: 12px; }
  .ops-sidebar { display: flex; flex-direction: row; align-items: center; gap: 8px; position: sticky; top: 0; z-index: 20; border-right: 0; border-bottom: 1px solid var(--ops-line); padding: 6px 10px; overflow: hidden; }
  .ops-brand { display: none; }
  .ops-logo { width: 32px; height: 32px; }
  .ops-nav { display: flex; flex: 1 1 auto; min-width: 0; flex-wrap: nowrap; gap: 6px; overflow-x: auto; overflow-y: hidden; padding: 0 0 2px; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .ops-nav::-webkit-scrollbar { display: none; }
  .ops-nav-group { display: flex; flex: 0 0 auto; align-items: center; gap: 4px; margin: 0; }
  .ops-nav-group-title { display: none; }
  .ops-nav-item { flex: 0 0 auto; min-height: 44px; padding: 10px 12px; margin: 0; white-space: nowrap; }
  .ops-sidebar-foot {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    border: 0;
    margin: 0;
    padding: 0;
  }
  .ops-avatar, .ops-user-meta { display: none; }
  .ops-logout { display: inline-flex; align-items: center; min-height: 44px; padding: 0 8px; white-space: nowrap; }
  .ops-main { height: auto; min-height: calc(100dvh - 130px); overflow: visible; }
  .ops-topbar { flex-direction: column; align-items: stretch; gap: 10px; padding: 16px 14px 8px; }
  .ops-title { font-size: clamp(20px, 6vw, 26px); }
  .ops-desc { font-size: 13px; line-height: 1.5; }
  .ops-topbar-actions { width: 100%; }
  .ops-topbar-actions > * { flex: 1 1 auto; min-height: 44px; }
  .ops-mobile-logout { display: inline-flex; }
  .ops-content { overflow: visible; padding: 8px 12px 28px; }
  .ops-stat-row { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .ops-stat .value { font-size: 23px; }
  .ops-card-grid { grid-template-columns: 1fr; }
  .ops-kanban { display: flex; overflow-x: auto; gap: 10px; padding-bottom: 6px; }
  .ops-kanban-col { flex: 0 0 min(86vw, 320px); min-height: 260px; }
  .ops-tx { grid-template-columns: 1fr auto; gap: 8px; }
  .ops-tx-time { grid-column: 1 / -1; }
  .ops-console-hero h2 { font-size: 26px; }

  /* Page-local dashboards use wide desktop grids; collapse their filters/cards here. */
  .cdb-filters { grid-template-columns: 1fr !important; }
  .cdb-row-top, .cdb-row-mid { grid-template-columns: 1fr !important; }
  .cdb-row-kpi, .cdb-row-bot { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .cdb-table-wrap, .ml-table-wrap, .ba-sub-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .cdb-table { min-width: 620px; }

  /* Data sheets stay usable via an intentional horizontal scroll region. */
  .sheet-page { min-height: calc(100dvh - 120px); margin: 0; }
  .sheet-head { gap: 8px; padding: 10px 10px 0; overflow-x: auto; }
  .sheet-title { font-size: 14px; }
  .sheet-toolbar { padding: 8px; }
  .sheet-search { min-width: min(220px, 58vw); flex: 1 1 140px; }
  .sheet-body { overflow: auto; -webkit-overflow-scrolling: touch; }
  .sheet-table { min-width: 640px; }
  .cmp-modal { width: calc(100vw - 20px); max-height: calc(100dvh - 20px); }

  /* Admin forms and dialogs: comfortable tap targets and edge-to-edge sheets. */
  .ops-input, .ops-select, .ops-textarea,
  .admin-input, .admin-select, .admin-textarea,
  .xb-input, .xb-select, .xb-textarea { min-height: 44px; font-size: 16px !important; }
  .ops-content .overflow-x-auto, .ops-content .table-wrap,
  .ops-content .admin-table-wrap, .od-table-wrap, .ml-table-wrap { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .od-mask, .ldg-mask { align-items: flex-end; padding: 0 0 env(safe-area-inset-bottom); }
  .od-modal, .ldg-modal { width: 100%; max-width: 100%; max-height: 94dvh; border-radius: 16px 16px 0 0; }
  .od-modal-body, .ldg-body { padding: 14px; }
  .od-modal-actions, .ldg-foot { flex-wrap: wrap; }
  .od-modal-actions > *, .ldg-foot > * { min-height: 44px; flex: 1 1 120px; }
  .od-view-grid, .ldg-grid, .ml-role { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}

@media (min-width: 768px) and (max-width: 860px) {
  .ops-mobile-logout { display: inline-flex; }
}


/* Final mobile hardening for iOS Safari and Android browsers. */
html {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  overscroll-behavior-x: none;
}
body {
  min-width: 0;
  width: 100%;
  overscroll-behavior-x: none;
}
img, video, canvas, svg { max-width: 100%; }
button, input, select, textarea { max-width: 100%; }

@media (max-width: 767px) {
  /* Use the small viewport as the stable fallback when mobile browser chrome is visible. */
  .ps-outer, .ps-shell, .ops-outer, .ops-shell { min-height: 100svh; }
  @supports (height: 100dvh) {
    .ps-outer, .ps-shell, .ops-outer, .ops-shell { min-height: 100dvh; }
  }

  .ps-nav, .ps-main, .ps-footer,
  .ops-sidebar, .ops-main, .ops-topbar, .ops-content,
  .sheet-page, .sheet-toolbar, .sheet-body { min-width: 0; }
  .ps-main, .ops-content { overflow-wrap: anywhere; }

  /* Keep wide data tables usable without letting them expand the page itself. */
  .sheet-page { width: 100%; max-width: 100%; overflow: hidden; }
  .sheet-toolbar { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .sheet-toolbar::-webkit-scrollbar { display: none; }
  .sheet-body { width: 100%; max-width: 100%; overflow-x: auto; overscroll-behavior-x: contain; }
  .sheet-table { width: max-content; min-width: 720px; }
  .sheet-table input, .sheet-table select { max-width: none; }

  /* iOS tends to zoom small controls; keep every form control at the safe size. */
  .ps-main input, .ps-main select, .ps-main textarea,
  .ops-content input, .ops-content select, .ops-content textarea,
  .sheet-table input, .sheet-table select { font-size: 16px !important; }

  /* Full-width bottom sheets remain clear of the home indicator and keyboard. */
  .xb-pl-sheet, .xb-cs-modal-panel, .cmp-modal, .od-modal, .ldg-modal, .csr-modal {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
}

@media (max-width: 380px) {
  .ops-content { padding-left: 10px; padding-right: 10px; }
  .ops-topbar { padding-left: 10px; padding-right: 10px; }
  .sheet-table { min-width: 680px; }
}

@supports (-webkit-touch-callout: none) {
  .ps-body, .ops-body { min-height: -webkit-fill-available; }
}


/* Mobile operations navigation: keep the complete vertical sidebar in a drawer. */
.ops-mobile-nav-toggle,
.ops-mobile-nav-backdrop { display: none; }

@media (max-width: 767px) {
  .ops-body.ops-nav-open { overflow: hidden; }
  .ops-shell { display: flex; flex-direction: column; position: relative; }
  .ops-main { width: 100%; min-width: 0; }

  .ops-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(84vw, 280px);
    height: 100svh;
    min-height: 100svh;
    z-index: 110;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    padding: max(12px, env(safe-area-inset-top)) 10px max(12px, env(safe-area-inset-bottom));
    background: #121820;
    border-right: 1px solid var(--ops-line);
    box-shadow: 14px 0 36px rgba(0, 0, 0, .35);
    overflow: hidden;
    transform: translateX(-105%);
    visibility: hidden;
    transition: transform .2s ease, visibility .2s ease;
  }
  .ops-nav-open .ops-sidebar { transform: translateX(0); visibility: visible; }
  .ops-brand { display: flex; flex: 0 0 auto; padding: 6px 10px 16px; }
  .ops-nav {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0 0 4px;
    -webkit-overflow-scrolling: touch;
  }
  .ops-nav-group { display: block; flex: 0 0 auto; margin: 0 0 14px; }
  .ops-nav-group-title { display: block; }
  .ops-nav-item { width: 100%; min-height: 44px; flex: 0 0 auto; }
  .ops-sidebar-foot { display: grid; flex: 0 0 auto; }

  .ops-mobile-nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 100;
    width: 100%;
    height: 100%;
    padding: 0;
    border: 0;
    background: rgba(2, 6, 23, .62);
    opacity: 0;
    visibility: hidden;
    transition: opacity .2s ease, visibility .2s ease;
  }
  .ops-nav-open .ops-mobile-nav-backdrop { opacity: 1; visibility: visible; }

  .ops-mobile-nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    min-height: 44px;
    margin: -2px 0 0 -4px;
    padding: 0;
    border: 1px solid var(--ops-line-strong);
    border-radius: 10px;
    background: var(--ops-elevated);
    color: var(--ops-text);
  }
  .ops-mobile-nav-toggle svg { width: 20px; height: 20px; }
  .ops-topbar > div:first-child { display: flex; align-items: flex-start; gap: 8px; min-width: 0; }
  .ops-topbar-copy { display: flex; flex: 1 1 auto; flex-direction: column; min-width: 0; }
  .ops-topbar > div:first-child .ops-title,
  .ops-topbar > div:first-child .ops-desc { min-width: 0; }
  .ops-topbar-actions { padding-left: 52px; }
}

@supports (height: 100dvh) {
  @media (max-width: 767px) {
    .ops-sidebar { height: 100dvh; min-height: 100dvh; }
  }
}

@media (prefers-reduced-motion: reduce) {
  .ops-sidebar, .ops-mobile-nav-backdrop { transition: none; }
}
