/* ════════════════════════════════════════════════════════════════════════════
   Phone layout for BildIt IQ.
   Everything here is inside @media (max-width: 760px), so computers and
   tablets in landscape never see any of it — the desktop layout is unchanged.
   ════════════════════════════════════════════════════════════════════════════ */
#m-menu-btn, #m-nav-shade, #m-desktop-switch, #m-phone-switch { display: none; }

@media (max-width: 760px) {
  /* Stop iPhone from zooming in when you tap a box */
  input, select, textarea { font-size: 16px !important; }
  body { -webkit-text-size-adjust: 100%; }

  /* ── Side menu becomes a slide-out drawer ───────────────────────────────── */
  #app { display: block; }
  .sidebar {
    position: fixed !important; top: 0; left: 0; bottom: 0; height: 100% !important;
    width: 82vw !important; max-width: 300px; z-index: 1200;
    transform: translateX(-105%); transition: transform .22s ease;
    box-shadow: 8px 0 30px rgba(0,0,0,.5);
  }
  body.m-nav-open .sidebar { transform: none; }
  body.app-mode .sidebar { display: flex !important; }   /* apps can still open the menu */
  #m-nav-shade { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 1150; }
  body.m-nav-open #m-nav-shade { display: block; }
  .nav-item { padding: 12px 12px; font-size: 15px; }
  #m-desktop-switch { display: flex; }

  /* ── Top bar ────────────────────────────────────────────────────────────── */
  .main { width: 100%; min-width: 0; overflow: visible; }
  .topbar { flex-wrap: wrap; gap: 8px; padding: 10px 12px; justify-content: flex-start; }
  #m-menu-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 42px; height: 40px; flex-shrink: 0; font-size: 20px; padding: 0;
    background: var(--surf2); color: var(--text); border: 1px solid var(--border); border-radius: var(--r-sm);
  }
  /* Show which page you're on (the desktop hides this title) */
  .page-title { display: block !important; font-size: 21px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  body.app-mode .page-title { display: none !important; }
  #btn-app-back { margin-right: 0; }
  body.app-mode #app-mode-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #btn-refresh-page { padding: 0 11px !important; margin-left: 0 !important; height: 40px; }
  .m-hide { display: none !important; }
  .topbar > .btn { margin-left: 0 !important; height: 40px; }
  #topbar-actions { order: 10; width: 100%; flex-wrap: wrap !important; gap: 6px !important; }
  #topbar-actions:empty { display: none; }
  #topbar-actions::-webkit-scrollbar { display: none; }
  #topbar-actions > * { flex-shrink: 0; }

  /* ── Page area ──────────────────────────────────────────────────────────── */
  .content { padding: 12px; }
  .card-header { padding: 12px 14px; flex-wrap: wrap; gap: 8px; }
  .card-body { padding: 14px; }
  .card { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* Bigger, easier-to-tap buttons */
  .btn { min-height: 38px; }
  .btn-sm { min-height: 34px; padding: 6px 12px; font-size: 12px; }

  /* Side-by-side layouts stack on top of each other */
  .grid-2, .grid-3, .ai-page, .dp-picker, .sx-split, .sx-g3, .qx-parties, .qx-top { grid-template-columns: 1fr !important; }
  .kpi-grid { grid-template-columns: 1fr 1fr !important; gap: 10px; }
  .kpi { padding: 14px; }
  .kpi-val { font-size: 26px; }
  .ai-page { height: auto !important; }
  /* (each written both ways: typed in the page, and as the browser rewrites it) */
  [style*="grid-template-columns:1fr 1fr"], [style*="grid-template-columns: 1fr 1fr"],
  [style*="grid-template-columns:1.6fr 1fr"], [style*="grid-template-columns: 1.6fr 1fr"],
  [style*="grid-template-columns:260px 1fr"], [style*="grid-template-columns: 260px 1fr"],
  [style*="grid-template-columns:220px 1fr"], [style*="grid-template-columns: 220px 1fr"],
  [style*="grid-template-columns:200px 1fr"], [style*="grid-template-columns: 200px 1fr"],
  [style*="grid-template-columns:1fr 80px 80px 1fr"], [style*="grid-template-columns: 1fr 80px 80px 1fr"],
  [style*="grid-template-columns:minmax(360px"], [style*="grid-template-columns: minmax(360px"],
  [style*="grid-template-columns:minmax(280px"], [style*="grid-template-columns: minmax(280px"] {
    grid-template-columns: 1fr !important;
  }
  /* Rows of 3+ number boxes become 2 per row */
  [style*="grid-template-columns:repeat("]:not([style*="auto-"]),
  [style*="grid-template-columns: repeat("]:not([style*="auto-"]) { grid-template-columns: 1fr 1fr !important; }
  /* Let grid columns shrink to the phone instead of being pushed wide by their contents */
  .grid-2 > *, .grid-3 > *, .kpi-grid > *, .sx-split > *, [style*="display:grid"] > *, [style*="display: grid"] > * { min-width: 0; }
  /* Rows of buttons / chips wrap instead of running off the screen */
  .flex, [style*="display:flex"]:not(.topbar), [style*="display: flex"], [style*="inline-flex"] { flex-wrap: wrap; }
  #sku-filter-bar input, #sku-filter-bar select { max-width: 100%; }

  /* Fixed widths shrink to fit the phone */
  [style*="width:"]:not(td):not(th):not(.sidebar) { max-width: 100%; }
  [style*="min-width:"] { min-width: 0 !important; }
  img, canvas, svg { max-width: 100%; }

  /* Tables become a stack of cards: one card per row, each value with its
     column name beside it (mobile.js copies the column names onto the cells) */
  .tbl[data-mlab], .tbl[data-mlab] tbody { display: block; width: 100%; }
  .tbl[data-mlab] thead { display: none; }
  .tbl[data-mlab] tr { display: block; padding: 8px 2px; border-bottom: 1px solid var(--border); }
  .tbl[data-mlab] tr:last-child { border-bottom: 0; }
  .tbl[data-mlab] td {
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    padding: 4px 10px; border: 0 !important; text-align: right; min-width: 0; white-space: normal; word-break: break-word;
  }
  .tbl[data-mlab] td::before {
    content: attr(data-label); flex-shrink: 0; max-width: 42%; text-align: left;
    font-size: 10px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase; color: var(--text3);
  }
  .tbl[data-mlab] td[data-label=""]::before { display: none; }
  .tbl[data-mlab] td[data-label=""] { justify-content: flex-end; }
  .tbl[data-mlab] td[colspan] { display: block; text-align: center; }
  .tbl[data-mlab] td[colspan]::before { display: none; }
  .tbl[data-mlab] td:first-child { font-weight: 600; }
  .tbl[data-mlab] tfoot, .tbl[data-mlab] tfoot tr { display: block; }
  .tbl[data-mlab] td input[type=number], .tbl[data-mlab] td select { max-width: 55%; }
  .table-wrap, .tbl-wrap { overflow-x: auto; }

  /* ── Pop-up windows fill the screen ─────────────────────────────────────── */
  .modal-bg { align-items: stretch; }
  .modal {
    width: 100vw !important; max-width: 100vw !important; height: 100%; max-height: 100% !important;
    border-radius: 0; padding: 18px 14px 28px; border: 0;
  }
  #modal-content { width: 100vw !important; }
  .modal-title { font-size: 20px; padding-right: 36px; }
  .modal-close { top: 10px; right: 10px; font-size: 26px; padding: 6px; }

  /* Apps menu */
  #apps-menu .apps-panel { top: 10px; right: 10px; left: 10px; width: auto; max-height: calc(100% - 20px); overflow-y: auto; }
  #apps-menu .apps-grid { grid-template-columns: 1fr 1fr; }

  /* Shop floor timer */
  .timer-big { font-size: 56px; letter-spacing: 2px; }

  /* Quote builder lines */
  .quote-line { grid-template-columns: 1fr auto !important; }

  /* Login */
  .login-box { padding: 32px 22px; }

  /* Toasts sit above the bottom edge */
  #toast, .toast { left: 12px !important; right: 12px !important; bottom: 12px !important; max-width: none !important; }

  /* ── Steel Order & Hardware apps ────────────────────────────────────────── */
  .sx-head { padding: 12px 14px; }
  .sx-tabs { grid-auto-flow: row !important; grid-template-columns: 1fr 1fr !important; }
  .sx-tab { border-bottom: 1px solid var(--border); padding: 11px 4px; white-space: normal; }
  .sx-list { max-height: 46vh; }
  .sx-panel { padding: 14px; }
  .sx-g2 { grid-template-columns: 1fr 1fr; }
  .sx-stats { grid-template-columns: 1fr 1fr; }
  .sx-stat-v { font-size: 20px; }
  .kr-1 { grid-template-columns: 58px 1fr 34px !important; }
  .kr-1 .kit-sup { grid-column: 2 / 3; }
  .kr-2 { grid-template-columns: 1fr 1fr !important; }
  .kr-2 > span:first-child { display: none; }
  .so-line { grid-template-columns: 1fr 1fr !important; }
  .so-line > div:first-child { grid-column: 1 / -1; }
  .ssr { grid-template-columns: 1fr 1fr 34px !important; }
  .ssr > div:first-child { grid-column: 1 / -1; }
  .qx-charge { grid-template-columns: 1fr 100px 34px; }
  .qx-row2 { grid-template-columns: 1fr; }
}

/* "Phone view" button, shown only when someone on a phone switched to desktop view */
#m-phone-switch {
  position: fixed; right: 12px; bottom: 12px; z-index: 2000;
  background: var(--accent, #f97316); color: #fff; border: 0; border-radius: 30px;
  padding: 12px 18px; font: 600 15px 'DM Sans', sans-serif; box-shadow: 0 6px 20px rgba(0,0,0,.4);
}
/* The page is shown zoomed out to computer width, so the button is drawn big */
html.m-force-desktop #m-phone-switch { display: block; font-size: 38px; padding: 26px 40px; border-radius: 60px; right: 30px; bottom: 30px; }

/* ── Tablets / computers: the side menu can be hidden (« in the menu, ☰ to bring it back).
   Remembered per device. Phones keep the slide-out drawer above. ─────────────── */
#m-side-hide, #m-side-show { display: none; }
@media (min-width: 761px) {
  #m-side-hide {
    display: block; position: absolute; top: 10px; right: 10px;
    width: 30px; height: 30px; padding: 0; font-size: 16px; line-height: 1; cursor: pointer;
    background: var(--surf2); color: var(--text2); border: 1px solid var(--border); border-radius: var(--r-sm);
  }
  .sidebar .logo-area { position: relative; }
  body.side-collapsed .sidebar { display: none; }
  body.side-collapsed:not(.app-mode) #m-side-show {
    display: flex; align-items: center; justify-content: center;
    position: fixed; top: 12px; left: 10px; z-index: 150;
    width: 36px; height: 36px; padding: 0; font-size: 18px; cursor: pointer;
    background: var(--surf2); color: var(--text); border: 1px solid var(--border); border-radius: var(--r-sm);
  }
  body.side-collapsed:not(.app-mode) .topbar { padding-left: 58px; }
}
