/* ══════════════════════════════════════════════════════════════
   Galvaframe — mobile + tablet layer (admin, crew and customer portals)
   Loaded in <head> after galvaframe.css. Page styles are inline further
   down the <body>, so overrides inside the media queries use !important.
   Breakpoints: ≤1024 tablet (admin sidebar becomes a drawer),
                ≤768 tablet portrait, ≤560 phone.
   ══════════════════════════════════════════════════════════════ */

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
img, video, canvas { max-width: 100%; }

/* Tables are wrapped in this on small screens (see partials/responsive). */
.gf-tscroll { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
.gf-tscroll > table { min-width: 100%; }

/* Admin drawer controls — hidden on desktop. */
.gf-navtoggle { display: none; }
.gf-nav-backdrop { display: none; }
.gf-nav-close { display: none; }

/* Dialogs size to the VISIBLE screen (dvh), at every width. With plain vh a phone's or tablet's browser
   toolbar covers the bottom of the dialog, so its action buttons can't be scrolled into view. */
.gf-dlg, .cp-dialog, .inv-dialog, .pos-dialog, .dash-dialog, .gfi-dlg, .lo-dlg, .card-max-dialog, .cp-terms-dlg {
  max-height: 90vh !important; max-height: 90dvh !important;
  overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
}
.card-max-dialog, .inv-dialog, .cp-dialog, .gf-dlg, .gfi-dlg, .pos-dialog { overflow-y: auto; }
/* Portal pages scroll as a normal document: never taller than the visible screen unless content needs it. */
body:has(> .cp-nav), body:has(.cp-nav) { min-height: 100vh; min-height: 100dvh; }

/* ── Touch devices ─────────────────────────────────────────── */
@media (pointer: coarse) {
  /* iOS zooms the page when a focused field is under 16px. */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select, textarea { font-size: 16px !important; }
  /* Hover-only affordances are unreachable by touch — show them. */
  .msg-kebab { opacity: .6 !important; }
  .stat:hover, .qa-item:hover, .pos-prod:hover { transform: none !important; }
}

/* ══ Tablet (≤1024px) ═════════════════════════════════════════ */
@media (max-width: 1024px) {
  /* Admin shell: sidebar slides in from the left. */
  body { height: 100vh; height: 100dvh; }
  #sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 1300;
    width: min(280px, 84vw); transform: translateX(-104%);
    /* Closed: fully off-screen, no shadow bleeding onto the page edge, and out of the tab order. */
    visibility: hidden; box-shadow: none;
    transition: transform .25s cubic-bezier(.22,.61,.36,1), visibility 0s linear .25s, box-shadow .25s;
    animation: none !important; opacity: 1 !important;
    padding-top: max(18px, env(safe-area-inset-top));
    padding-bottom: max(18px, env(safe-area-inset-bottom));
    overscroll-behavior: contain;
  }
  body.gf-nav-open #sidebar { transform: none; visibility: visible; box-shadow: 0 0 40px rgba(0,0,0,.28); transition-delay: 0s; }
  .gf-nav-backdrop { display: block; position: fixed; inset: 0; z-index: 1250; background: rgba(0,0,0,.45); opacity: 0; pointer-events: none; transition: opacity .2s; }
  body.gf-nav-open .gf-nav-backdrop { opacity: 1; pointer-events: auto; }
  body.gf-nav-open { overflow: hidden; }
  .gf-nav-close { display: flex; margin-left: auto; width: 34px; height: 34px; border-radius: 9px; border: none; background: var(--surface-2); color: var(--muted); cursor: pointer; align-items: center; justify-content: center; font-size: 18px; }
  .brand { align-items: center; }
  .gf-navtoggle { display: flex; }
  .nav-item { padding: 11px 13px; }

  #topbar { padding: 10px 14px; gap: 8px; }
  #topbar .kbd { display: none; }
  #content { padding: 4px 14px 28px; padding-bottom: max(28px, env(safe-area-inset-bottom)); }

  /* Grid columns may shrink below their content (tables scroll inside instead of widening the page). */
  .dash-main-grid > *, .pos-wrap > *, .grid-2 > *, .dh-cols > *, .dh-hero > *, .an-grid4 > *,
  .chat-layout > *, .gf-chat-layout > *, [style*="display:grid"] > * { min-width: 0; }

  /* Wide inline grids: 5–6 columns → 3. */
  [style*="grid-template-columns:repeat(5"], [style*="grid-template-columns:repeat(6"] { grid-template-columns: repeat(3, minmax(0,1fr)) !important; }

  /* Tab strips scroll sideways instead of wrapping into ragged rows. */
  .rep-tabs { flex-wrap: nowrap !important; overflow-x: auto; scrollbar-width: none; max-width: 100%; }
  .rep-tabs::-webkit-scrollbar { display: none; }
  .rep-tab { white-space: nowrap; flex: 0 0 auto; }

  /* Header rows wrap instead of overflowing. */
  .page-head, .card-head, .cp-card-head, .inv-dialog-head, .dash-dialog-head, .inv-actions, .gf-acts { flex-wrap: wrap; row-gap: 8px; }
  .page-head > div, .card-head > div { flex-wrap: wrap; max-width: 100%; }
  .tbl th { white-space: nowrap; }
  /* Wide tables (6+ columns) keep readable columns and scroll inside their card. */
  .tbl:has(tr > :nth-child(6)), .cp-tbl:has(tr > :nth-child(6)) { min-width: 760px; }
  /* Chat composers: the text field shrinks so the Send button stays on screen. */
  .chat-input, .gf-chat-field { min-width: 0; }
  .chat-input-area > .btn, .gf-chat-input > .cp-btn { flex: 0 0 auto; }

  /* POS: catalogue and cart stack, so keep the catalogue short and let the cart size to its content. */
  .pos-grid { max-height: 46vh !important; }
  .pos-cart { position: static !important; min-height: 0 !important; }
  .pos-cart-items { max-height: 38vh !important; }

  /* Portal nav (crew + customer): right-hand cluster never overflows. */
  .cp-nav { padding: 0 14px; }
  .cp-right { gap: 6px; }
}

/* ══ Tablet portrait (≤768px) ═════════════════════════════════ */
@media (max-width: 768px) {
  [style*="grid-template-columns:repeat(4"], [style*="grid-template-columns:repeat(3"] { grid-template-columns: repeat(2, minmax(0,1fr)) !important; }
  [style*="grid-template-columns:1.6fr 1fr"], [style*="grid-template-columns:1.15fr .85fr"] { grid-template-columns: minmax(0,1fr) !important; }
  .qa-grid { grid-template-columns: repeat(3, minmax(0,1fr)) !important; }
  .dh-cols { grid-template-columns: minmax(0,1fr) !important; }
  .po-info { grid-template-columns: repeat(2, minmax(0,1fr)) !important; }

  /* Dialogs use nearly the full screen. */
  .gf-ov, .cp-overlay, .inv-overlay, .pos-overlay, .dash-overlay, .gfi-ov, .lo-ov, .card-max-overlay, #dashExact { padding: 12px !important; }
  .gf-dlg, .cp-dialog, .inv-dialog, .pos-dialog, .dash-dialog, .gfi-dlg, .lo-dlg, .card-max-dialog {
    max-height: calc(100vh - 24px) !important; max-height: calc(100dvh - 24px) !important;
    width: 100% !important; overflow-y: auto;
  }
  /* Long forms: Save / Cancel stay pinned to the bottom of the dialog so they're always reachable. */
  .inv-dialog .inv-actions, .gfi-dlg .gfi-actions {
    position: sticky; bottom: -20px; z-index: 3; margin-bottom: -20px !important;
    padding: 12px 0 20px !important; background: var(--surface); border-top: 1px solid var(--border);
  }
  #dashExact .ex-card { min-width: 0 !important; width: 100%; padding: 28px 20px !important; }
  #dashExact .ex-amt { font-size: 34px !important; }
}

/* ══ Phone (≤560px) ═══════════════════════════════════════════ */
@media (max-width: 560px) {
  body { font-size: 14px; }

  /* Every inline multi-column grid collapses, except calendars / auto-fill grids. */
  [style*="grid-template-columns:1fr 1fr"],
  [style*="grid-template-columns: 1fr 1fr"] { grid-template-columns: minmax(0,1fr) !important; }
  .inv-grid, .at-grid2, .pos-field-row, .cp-grid2, .set-grid, .book-cols { grid-template-columns: minmax(0,1fr) !important; }
  #schModal .inv-grid { grid-template-columns: minmax(0,1fr) !important; }
  .inv-grid > * { grid-column: auto !important; }
  .det-kpis, .est-result-grid, .an-grid4, .kpi-row { grid-template-columns: repeat(2, minmax(0,1fr)) !important; }
  .po-info { grid-template-columns: minmax(0,1fr) !important; }
  .qa-grid { grid-template-columns: repeat(2, minmax(0,1fr)) !important; }

  /* Admin shell */
  #topbar .search { display: none; }
  #content { padding: 2px 12px 24px; }
  .page-head { margin-bottom: 12px; }
  .page-title { font-size: 17px; }
  .stats { grid-template-columns: repeat(2, minmax(0,1fr)) !important; gap: 10px; }
  .stat { padding: 13px 14px; }
  /* An odd last card spans the row instead of leaving a gap beside it. */
  .stats > .stat:last-child:nth-child(odd), .cp-kpis > .cp-kpi:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .stat-value { font-size: 21px; margin-top: 8px; }
  .card { padding: 14px; border-radius: 14px; }
  .card + .card { margin-top: 12px; }
  .tbl td { padding: 10px 8px; }
  /* Data tables scroll sideways rather than crushing columns into tall, wrapped cells. */
  .tbl td, .cp-tbl td { white-space: nowrap; }

  /* Dashboard period filter: the two dates share one row. */
  .dash-dates { margin-left: 0 !important; width: 100%; flex-wrap: nowrap !important; }
  .dash-date { flex: 1 1 0; min-width: 0; }
  /* Reports toolbar: presets wrap, the date range fills its own row. */
  .rep-toolbar > * { min-width: 0; max-width: 100%; }
  .rep-range { width: 100%; flex-wrap: wrap; }
  .rep-date { flex: 1 1 120px; min-width: 0; }

  /* In-page search boxes take the full row. */
  #content .search { min-width: 0 !important; flex: 1 1 100% !important; margin-left: 0 !important; max-width: none; }
  select[style*="margin-left:auto"] { margin-left: 0 !important; }

  /* Buttons are comfortable to tap. */
  .btn { min-height: 38px; }
  .btn-sm { min-height: 34px; }

  /* Dialogs: edge-to-edge sheet with smaller padding. */
  .gf-ov, .cp-overlay, .inv-overlay, .pos-overlay, .dash-overlay, .gfi-ov, .lo-ov, .card-max-overlay { padding: 8px !important; }
  .gf-dlg, .cp-dialog, .inv-dialog, .pos-dialog, .dash-dialog, .gfi-dlg, .lo-dlg, .card-max-dialog {
    padding: 16px !important; border-radius: 14px !important;
    max-height: calc(100vh - 16px) !important; max-height: calc(100dvh - 16px) !important;
  }
  .inv-dialog .inv-actions, .gfi-dlg .gfi-actions { bottom: -16px; margin-bottom: -16px !important; padding-bottom: 16px !important; }
  .inv-actions, .gf-acts { justify-content: stretch !important; }
  .inv-actions > .btn, .gf-acts > .gf-btn { flex: 1 1 auto; justify-content: center; }

  /* Admin scheduling month calendar: compact cells; columns may shrink below a booking label's width. */
  .cal-grid { gap: 3px !important; grid-template-columns: repeat(7, minmax(0, 1fr)) !important; }
  .cal-cell { min-width: 0; }
  .cal-cell { min-height: 54px !important; padding: 3px !important; border-radius: 7px !important; }
  .cal-day { font-size: 11px !important; margin-bottom: 2px !important; }
  .cal-ev { font-size: 9px !important; padding: 1px 3px !important; border-radius: 4px !important; }

  /* Portals */
  .cp-main { padding: 16px 12px 40px !important; padding-bottom: max(40px, env(safe-area-inset-bottom)) !important; }
  .cp-brand .bname, .cp-brand .bsub { display: none; }
  .cp-nav { padding: 0 10px !important; }
  .cp-nav-inner { gap: 8px !important; }
  .cp-iconbtn, .cp-navtoggle { width: 36px !important; height: 36px !important; }
  .cp-avatar { width: 34px; height: 34px; }
  .cp-card { padding: 14px; }
  .cp-welcome h1 { font-size: 20px; }
  .cp-kpis { gap: 10px; margin: 16px 0; }
  .cp-kpi { padding: 13px; }
  .cp-kpi .val { font-size: 21px !important; }
  .cp-tbl th, .cp-tbl td { padding: 9px 8px; }
  .cp-toast { left: 12px; right: 12px; transform: none; text-align: center; }
  .cp-menu { position: fixed !important; top: 60px !important; right: 10px !important; left: auto; max-width: calc(100vw - 20px); }
  .cp-cal .day { padding: 8px 0 11px; }
  .cp-thumb { width: 64px; height: 64px; }
}

/* ══ Short screens (phones in landscape) ══════════════════════ */
@media (max-height: 520px) {
  /* Chat panels fill what's left of the screen instead of a fixed 420px minimum. */
  .chat-layout { min-height: 0 !important; height: calc(100vh - 140px) !important; height: calc(100dvh - 140px) !important; }
  .gf-chat-layout { min-height: 0 !important; height: calc(100vh - 92px) !important; height: calc(100dvh - 92px) !important; }
  /* Portal chat pages: drop the intro heading so the whole conversation fits on screen. */
  .cp-main:has(.gf-chat) { padding-top: 12px !important; padding-bottom: 12px !important; }
  .cp-main:has(.gf-chat) > h1, .cp-main:has(.gf-chat) > p, .cp-main:has(.gf-chat) > .cp-crumbs { display: none; }
  .chat-card { margin-top: 0; }
  /* Popups keep a small margin and scroll inside. */
  .gf-ov, .cp-overlay, .inv-overlay, .pos-overlay, .dash-overlay, .gfi-ov, .lo-ov, .card-max-overlay, #dashExact { padding: 6px !important; align-items: flex-start !important; }
  .gf-dlg, .cp-dialog, .inv-dialog, .pos-dialog, .dash-dialog, .gfi-dlg, .lo-dlg, .card-max-dialog {
    max-height: calc(100vh - 12px) !important; max-height: calc(100dvh - 12px) !important; overflow-y: auto !important;
  }
  /* Compact headers leave more room for content. */
  .cp-nav { height: 54px !important; }
  .cp-links { top: 54px !important; max-height: calc(100dvh - 54px); overflow-y: auto; }
  #topbar { padding-top: 6px; padding-bottom: 6px; }
}

/* Pop-up date pickers never run past the screen edge. */
.sch-cal-pop, .pi-cal-pop { max-width: calc(100vw - 32px); }

/* Tables print at full width. */
@media print {
  .gf-tscroll { overflow: visible !important; }
  .gf-navtoggle, .gf-nav-backdrop { display: none !important; }
}
