/* mobile.css — phone-first overrides for every dashboard module
 *
 * Loaded LAST in the cascade (after page-specific <style>) so phone rules win.
 * Touch targets: 44px (iOS HIG). Inputs ≥16px (kills iOS auto-zoom).
 * Tap delay killed via touch-action: manipulation.
 * Safe areas via env(safe-area-inset-*).
 *
 * Page-specific overrides should still go in the page's own <style>; only
 * patterns shared by ≥3 pages live here.
 */

/* ── iOS notch / home-indicator support (all sizes) ─────────────────── */
@supports (padding: env(safe-area-inset-bottom)) {
  body { padding-bottom: env(safe-area-inset-bottom); }
  header { padding-top: max(14px, env(safe-area-inset-top)); }
  .modal-card { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
}

/* ── PHONE: ≤ 600px ─────────────────────────────────────────────────── */
@media (max-width: 600px) {

  /* Inputs ≥16px = no iOS zoom on focus. Buttons same size for consistency. */
  input, select, textarea, button {
    font-size: 16px !important;
  }
  /* Prevent textarea horizontal overflow */
  textarea { resize: vertical; max-width: 100%; }

  /* 44px min touch target */
  button, .btn, a.btn-link,
  input[type="submit"], input[type="button"], input[type="checkbox"], input[type="radio"],
  .row-act button, .copy-btn, .modal-actions button {
    min-height: 44px;
    touch-action: manipulation;
  }
  /* Compact "icon" buttons (like reveal-toggle for PIN) need to stay small */
  button[data-compact], .compact-btn { min-height: 32px !important; padding: 4px 10px !important; }

  /* Buttons get more breathing room */
  button, .btn { padding: 10px 16px; }
  .modal-card .modal-actions button { min-width: 100px; }
  /* Larger checkbox/radio for easier touch */
  input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; }

  /* ── Layout ──────────────────────────────────────────────────────── */
  main { padding: 14px 12px 80px; }     /* extra bottom for nav bar */
  body { font-size: 14px; }

  /* Header — sticky + minimal
   * Strategy: on phones we keep ONLY essential items in the header bar:
   *   1. Nav cluster (← back + 🏠 hub)
   *   2. Page title (truncated if long)
   *   3. Sign-out (icon-only)
   * Everything else (user name, role badge, page-specific selectors) is
   * hidden — they show up in the side drawer / context toolbar / settings.
   * This keeps header at ~48px tall single-row on every page. */
  header {
    position: sticky !important;
    top: 0 !important;
    z-index: 50 !important;
    padding: 6px 10px !important;
    gap: 6px !important;
    flex-wrap: nowrap !important;
    min-height: 44px !important;
    overflow: hidden;
  }
  header > * { min-width: 0; flex-shrink: 1; }
  header h1 {
    font-size: 14px !important;
    flex: 1 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin: 0 !important;
  }
  /* Hide title icons + extras to save space — emoji prefixes still render */
  header img.logo { height: 22px !important; }

  /* Hide non-essential header content on phones — keeps the bar single-row.
   * Strategy: hide ALL direct children of <header> EXCEPT the nav cluster
   * (← back / 🏠 hub) and the page title (h1).
   * Page-specific controls (date pickers, branch selectors, etc.) get
   * hidden in the header — they belong in the filter bar / drawer / context
   * toolbar below, not crammed into a 44px bar. The .who block is also
   * trimmed to just the sign-out button. */
  header > div:not(.navCluster):not(.who):not([data-keep-on-mobile]) {
    display: none !important;
  }
  /* .who block — hide all spans/labels, keep ONLY the sign-out button */
  header .who > span,
  header .who > #userLabel, header #userLabel,
  header #userName,
  header .who > label,
  header .badge, header #roleLabel {
    display: none !important;
  }
  /* Sign-out button stays visible inside .who */
  header .who { gap: 0 !important; }
  header .who > button { display: inline-flex !important; }
  /* Sign-out button — keep, but icon-only on phone */
  header #signOut, header button[id*="signOut"] {
    padding: 6px 8px !important;
    font-size: 11px !important;
    min-height: 32px !important;
    flex-shrink: 0;
  }
  /* If a page has its own dropdown / select / input in header,
     truncate harshly so it doesn't blow out the bar */
  header select, header input { max-width: 90px; padding: 4px 6px !important; font-size: 12px !important; }

  /* ── Sticky toolbar / filter bar — ALWAYS reachable ─────────────── */
  .toolbar, .filter-bar, .filter-row, [data-toolbar],
  .card-head[data-sticky] {
    position: sticky;
    top: 0;
    background: var(--card, #fff);
    z-index: 20;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .06);
  }
  .toolbar select, .filter-bar select {
    flex: 1;
    min-width: 0;          /* allow shrinking inside flex */
  }

  /* ── Tables ───────────────────────────────────────────────────────── */
  /* Force tables to fill the available width on mobile (was min-width:100%
   * which still let them shrink to content when columns were narrow). */
  table { width: 100% !important; min-width: 100%; }
  /* Name column (first non-hidden cell) gets the slack when other columns
   * are hidden by .mobile-hide */
  table th:not(.mobile-hide):first-of-type,
  table td:not(.mobile-hide):first-child {
    width: auto;
  }
  /* Force horizontal scroll on table containers */
  .table-wrap, [data-table-wrap], main > .card > div:has(> table),
  main > div:has(> table) {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    max-width: 100vw;
  }
  th, td { padding: 6px 8px; font-size: 12px; }
  /* Mobile-hide convention — author-marked columns disappear on phone */
  .mobile-hide, [data-mobile="hide"] { display: none !important; }
  /* Counterpart: .mobile-only is HIDDEN by default (in page CSS), shown on
   * phone. Pages put condensed info (e.g. inline SKU + branch under name)
   * inside .mobile-only divs so the user gets context without all columns. */
  .mobile-only, [data-mobile="only"] { display: block !important; }
  /* Numeric columns get consistent right-align even when squished */
  td.num, th.num, td[align="right"], th[align="right"] { text-align: right; }
  /* Filter bar — wrap and compact controls on phone */
  .filter-bar, .toolbar {
    padding: 10px 10px !important;
    gap: 6px !important;
  }
  .filter-bar input, .filter-bar select,
  .toolbar input, .toolbar select {
    font-size: 14px !important;
    padding: 6px 8px !important;
    min-width: 0 !important;
    flex: 1 1 100px;
  }
  .filter-bar input[type="text"] { max-width: none !important; }   /* override stock_balance.html cap */
  .filter-bar label, .toolbar label {
    font-size: 11px !important;
    flex-wrap: nowrap;
  }
  .chk-pill { padding: 4px 10px !important; font-size: 11px !important; }
  /* Card head — actions wrap below title on tight screens */
  .card-head {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .card-head .actions { flex-wrap: wrap; gap: 4px !important; }
  /* Card itself — let inner content scroll horizontally if needed */
  .card { overflow: visible !important; }
  .card > div[style*="overflow"], .card > .table-wrap {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* ── KPI grid — 2 across on phone, 1 on very small ──────────────── */
  .kpis, .kpi-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .kpi, .kpi-card {
    padding: 10px 12px !important;
  }
  .kpi-value, .kpi .v, .check .v {
    font-size: 18px !important;
  }

  /* ── Modal — slide up from bottom, near-full-screen ─────────────── */
  .modal {
    align-items: flex-end !important;
    padding: 0 !important;
  }
  .modal-card {
    max-height: 92vh !important;
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 14px 14px 0 0 !important;
    margin: auto 0 0 !important;
    padding: 18px 16px max(18px, env(safe-area-inset-bottom)) !important;
    box-shadow: 0 -4px 18px rgba(0,0,0,.18);
  }
  .modal-card h3 { font-size: 16px; margin-bottom: 12px; }
  /* Sticky modal actions at bottom of modal card */
  .modal-card .modal-actions {
    position: sticky;
    bottom: 0;
    background: linear-gradient(to top, #fff 80%, rgba(255,255,255,0));
    padding: 10px 0 0;
    margin-top: 14px;
  }

  /* ── Drawer (right-side panel) → bottom sheet on phone ─────────── */
  .drawer, [data-drawer] {
    width: 100vw !important;
    max-width: 100% !important;
    right: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    top: auto !important;
    height: 92vh !important;
    border-radius: 14px 14px 0 0 !important;
    transform: translateY(100%);
  }
  .drawer.open, [data-drawer].open { transform: translateY(0) !important; }

  /* ── Toast — top center, full width ─────────────────────────────── */
  .toast {
    top: max(14px, env(safe-area-inset-top)) !important;
    left: 14px !important;
    right: 14px !important;
    text-align: center;
  }

  /* ── Form rows — labels on top, full-width inputs ──────────────── */
  .form-row { display: block; margin-bottom: 12px; }
  .form-row label { display: block; margin-bottom: 4px; font-weight: 600; }
  .grid2, .grid3 {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }
  /* ── Tabs — SLIDE-OUT LEFT DRAWER on phone ──────────────────────────
   * Default: drawer is OFF-SCREEN (translateX -100%). Content gets full
   * viewport width — no cramping.
   * Tap the ☰ menu button (auto-injected by hub-back.js) → drawer slides
   * in from the left + dimming backdrop covers content.
   * Tap a tab → drawer auto-closes + content switches.
   * Tap backdrop → drawer closes.
   */
  .tabs, .pill-tabs, .view-tabs, [role="tablist"],
  div:has(> button[data-tab]),
  div:has(> button[data-utab]),
  div:has(> button[data-mtab]) {
    position: fixed !important;
    left: 0 !important;
    top: 0 !important;
    height: 100vh !important;
    /* Reserve safe-area top + bottom inside the drawer */
    padding-top: max(60px, env(safe-area-inset-top, 0px)) !important;
    padding-bottom: max(76px, env(safe-area-inset-bottom, 0px)) !important;
    width: min(70vw, 240px) !important;
    display: flex !important;
    flex-direction: column !important;
    flex-wrap: nowrap !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 2px !important;
    margin: 0 !important;
    background: #fff !important;
    border-right: 1px solid var(--border, #e5e7eb) !important;
    border-bottom: 0 !important;
    box-shadow: 4px 0 18px rgba(0,0,0,0.16);
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.22s ease-out;
  }
  /* Open state — added by JS (hub-back.js _toggleTabDrawer) */
  body.tab-drawer-open .tabs,
  body.tab-drawer-open .pill-tabs,
  body.tab-drawer-open .view-tabs,
  body.tab-drawer-open [role="tablist"],
  body.tab-drawer-open div:has(> button[data-tab]),
  body.tab-drawer-open div:has(> button[data-utab]),
  body.tab-drawer-open div:has(> button[data-mtab]) {
    transform: translateX(0) !important;
  }
  .tabs::-webkit-scrollbar,
  .pill-tabs::-webkit-scrollbar,
  .view-tabs::-webkit-scrollbar,
  [role="tablist"]::-webkit-scrollbar { display: none; }

  /* Backdrop overlay — dims the content when drawer is open. Auto-injected. */
  #_tabDrawerBackdrop {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 99;
    display: none;
    -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px);
  }
  body.tab-drawer-open #_tabDrawerBackdrop { display: block; }

  /* Drawer trigger lives in the bottom nav as #_mobTabs — see hub-back.js */

  /* Tab button styling (inside the drawer) */
  .tab-btn, .tab, .u-tab, .sub-tab-btn, .master-tab, .view-tab,
  [role="tab"],
  .tabs button, .pill-tabs button, .view-tabs button,
  div:has(> button[data-tab]) > button[data-tab],
  div:has(> button[data-utab]) > button[data-utab],
  div:has(> button[data-mtab]) > button[data-mtab] {
    flex: 0 0 auto !important;
    width: 100% !important;
    min-height: 48px !important;
    padding: 12px 14px 12px 17px !important;     /* room for 3px left border */
    font-size: 14px !important;
    font-weight: 500 !important;
    line-height: 1.3 !important;
    text-align: left !important;
    white-space: normal !important;
    border: 0 !important;
    border-left: 3px solid transparent !important;
    border-radius: 0 !important;
    background: transparent !important;
    color: var(--ink, #1f2937) !important;
    box-shadow: none !important;
    margin: 0 !important;
    transition: background 0.12s, border-color 0.12s, color 0.12s;
    word-break: break-word;
  }
  .tab-btn:active, .tab:active, .u-tab:active, .sub-tab-btn:active,
  .master-tab:active, .view-tab:active, [role="tab"]:active {
    background: #f3f4f6 !important;
  }
  /* Active state — left border + brand text + light tint */
  .tab-btn.active, .tab.active, .u-tab.active, .sub-tab-btn.active,
  .master-tab.active, .view-tab.active, [role="tab"][aria-selected="true"] {
    color: var(--brand, #9b2335) !important;
    border-left-color: var(--brand, #9b2335) !important;
    background: rgba(155, 35, 53, 0.06) !important;
    font-weight: 700 !important;
  }
  .tab-btn.indent { padding-left: 17px !important; }

  /* ── Content area gets FULL viewport width — no margin reservation ─ */
  /* Drawer floats on top with backdrop, doesn't push content. */

  /* ── Cards — tighter ─────────────────────────────────────────────── */
  .card { padding: 12px 14px !important; margin-bottom: 12px !important; }
  .card-head { padding: 10px 12px !important; font-size: 14px !important; }

  /* ── Section headers ─────────────────────────────────────────────── */
  .section h2, h2 { font-size: 14px; }
  .section .grid { gap: 8px; }

  /* ── Dropdowns / selects — more padding so the chevron isn't cramped */
  select { padding-right: 28px !important; background-position: right 8px center !important; }

  /* ── Hide non-critical chrome ───────────────────────────────────── */
  .meta, .hint, .muted-small { font-size: 11px; }
  /* Generic "desktop only" marker — pages can opt rows out */
  .desktop-only { display: none !important; }
  /* Long codes in compact form */
  code { word-break: break-all; }

  /* ── Fixed-positioned bottom nav (added by hub-back.js auto-injection
   *     OR by a page-specific include). Reserve room. */
  body.has-bottom-nav { padding-bottom: calc(60px + env(safe-area-inset-bottom)); }
}

/* ── TABLET: 601-1024px ─────────────────────────────────────────────── */
@media (min-width: 601px) and (max-width: 1024px) {
  .kpis, .kpi-grid {
    grid-template-columns: repeat(3, 1fr) !important;
  }
  button, .btn { min-height: 40px; }
  /* Tables stay wide; cards loosen but don't go phone-thin */
  .card { padding: 14px 16px; }
}

/* ── EXTRA-SMALL PHONES: ≤ 360px (iPhone SE, low-end Android) ──────── */
@media (max-width: 360px) {
  .kpis, .kpi-grid {
    grid-template-columns: 1fr !important;     /* one across */
  }
  header h1 { font-size: 14px; }
  main { padding: 10px 8px 80px; }
  .card { padding: 10px 12px !important; }
}
