/* ==================================================================
   ERP Connect - MOBILE-ONLY layer  (loaded AFTER site.css)
   Every rule is inside a max-width media query -> the DESKTOP view
   (>768px) is byte-identical.  Selectors are scoped under
   .main-content / .modal-body / ids and use !important only where
   they must beat a page-level <style> block or an inline width.
   ================================================================== */

/* ---- A. iOS tap-zoom guard: Safari zooms on focus when a field's
        font < 16px.  Force 16px on every form control on phones. ---- */
@media (max-width: 768px) {
  input, select, textarea,
  .form-control, .form-select,
  .form-control-sm, .form-select-sm,
  .input-group-text,
  .sidebar-search input { font-size: 16px !important; }
}

/* ---- B. No input may force page overflow: drop inline min-width,
        cap to its container (kills the min-width:210/340px culprits) - */
@media (max-width: 768px) {
  .main-content main input, .main-content main select, .main-content main textarea,
  .main-content main .form-control, .main-content main .form-select,
  .modal-body input, .modal-body select, .modal-body textarea,
  .modal-body .form-control, .modal-body .form-select {
    min-width: 0 !important;
    max-width: 100% !important;
  }
}

/* ---- C. Topbar de-crowd (the dominant global overflow source):
        Share/Suggest -> icon-only, hide version badge, tighten. ------ */
@media (max-width: 480px) {
  .top-bar { gap: .4rem; padding: 0 .55rem; }
  .topbar-version-badge { display: none !important; }
  /* font-size:0 collapses the bare "Share"/"Suggest" text node; the
     icon inherits 0 so its size is restored below. */
  #btnShareReport, #btnSuggest, #btnWaSend {
    font-size: 0 !important;
    padding-left: .55rem !important; padding-right: .55rem !important;
    margin-right: .35rem !important;
  }
  #btnShareReport > i, #btnSuggest > i, #btnWaSend > i { font-size: .95rem !important; margin: 0 !important; }
  #erpBell { margin-right: .15rem !important; }
  .topbar-session-chip { margin-right: .3rem !important; padding: .25rem .45rem; }
}

/* ---- D. Wide tables: contained horizontal scroll, never widen page - */
@media (max-width: 768px) {
  /* existing scroll wrappers -> guarantee they scroll (and fix the one
     that was overflow:hidden) */
  .main-content .table-responsive,
  .main-content .gst-table-wrap,
  .main-content .reg-table-wrap,
  .main-content .sim-wrap,
  .main-content .sm-scroll,
  .main-content .sm-scroll-mini,
  .main-content .pwb-wrap,
  .main-content .bt-wrap {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
  /* bare, UN-wrapped house tables -> become their own scroll box */
  .main-content main > table.reg-table,
  .main-content main .card > .card-body > table.reg-table,
  .main-content main table.reg-table {
    display: block; width: 100%; max-width: 100%;
    min-width: 0 !important;
    overflow-x: auto; -webkit-overflow-scrolling: touch;
  }
}

/* ---- E. Tab strips: one scrollable row instead of a ragged wrap ---- */
@media (max-width: 768px) {
  .main-content .nav-tabs, .main-content .nav-pills {
    flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .main-content .nav-tabs::-webkit-scrollbar,
  .main-content .nav-pills::-webkit-scrollbar { display: none; }
  .main-content .nav-tabs .nav-link, .main-content .nav-pills .nav-link { white-space: nowrap; }
}

/* ---- F. Filter bars: wrap + full-width controls (tappable) --------- */
@media (max-width: 768px) {
  .main-content .bs-filter-bar, .main-content .pwb-ctl {
    flex-wrap: wrap !important; overflow-x: visible !important;
    gap: .5rem !important; margin-left: 0 !important; width: 100% !important;
  }
  .main-content .bs-filter-bar > .form-control,
  .main-content .bs-filter-bar > .form-select,
  .main-content .bs-filter-bar > input,
  .main-content .bs-filter-bar > select,
  .main-content .bs-filter-bar > .input-group,
  .main-content .pwb-ctl .fgrp,
  .main-content .pwb-ctl .form-select {
    flex: 1 1 100% !important; width: 100% !important;
  }
  .main-content .bs-filter-bar > .btn { flex: 1 1 auto; }
}

/* ---- G. Modals fit the phone; action rows wrap -------------------- */
@media (max-width: 576px) {
  .modal-dialog { margin: .5rem !important; max-width: calc(100vw - 1rem) !important; }
  .modal-body .d-flex.justify-content-between { flex-wrap: wrap; gap: .35rem; }
}

/* ---- H. ChatBot FAB must not sit over an open modal on phones ------ */
@media (max-width: 640px) { body.modal-open #erpCbBtn { display: none; } }

/* ---- I. Reclaim side gutter on small phones ----------------------- */
@media (max-width: 480px) {
  .main-content > main.p-3 { padding-left: .6rem !important; padding-right: .6rem !important; }
}

/* ---- J. Flex KPI tile rows -> clean 2-up (bootstrap-col tiles already
        stack; this only helps flex-based tile rows like .sim-kpi) ---- */
@media (max-width: 480px) {
  .main-content .sim-kpi { flex: 1 1 calc(50% - .5rem) !important; min-width: 0 !important; }
}

/* ---- K. Auth pages (standalone: NO .main-content wrapper) ---------- */
@media (max-width: 768px) {
  .login-card .form-control, .login-card .form-select,
  .login-input-wrap input.form-control { font-size: 16px !important; }
  .login-pane-brand { min-height: auto !important; }
  .login-headline h1 { font-size: 1.5rem !important; line-height: 1.2 !important; }
}

/* ---- L. Stock Report matrix: FREEZE the first (Product) column on phones
        so it stays visible while the 40+ location columns scroll sideways.
        `.sr-tbl` exists only on Pages/Sales/StockReport.aspx, so these rules
        touch no other page.  thead is already sticky-top; we add sticky-left
        and the corner cells.  Opaque backgrounds: the special rows
        (.sr-cat / .sr-grand) already carry !important backgrounds, so only the
        normal rows need an explicit white behind the frozen cell. ---------- */
@media (max-width: 768px) {
  /* frozen body column */
  .sr-tbl tbody td:first-child {
    position: sticky; left: 0; z-index: 4;
    background: #fff;                     /* normal rows opaque; sr-cat/sr-grand keep their own !important bg */
    box-shadow: 2px 0 0 #E2E8F0;          /* thin divider so it reads as a frozen edge */
    min-width: 118px !important; max-width: 150px !important;
    white-space: normal;                  /* let a long product name wrap in the narrow frozen cell */
  }
  /* frozen header cells (both header rows) + the top-left corner above everything */
  .sr-tbl thead tr:first-child  th:first-child { position: sticky; left: 0; z-index: 9; min-width: 118px !important; max-width: 150px !important; }
  .sr-tbl thead tr:nth-child(2) th:first-child { position: sticky; left: 0; z-index: 8; }
}

/* ---- M. Charts & code blocks never widen the page on phones -------- */
@media (max-width: 768px) {
  .main-content main canvas { max-width: 100% !important; height: auto !important; }
  .main-content main pre, .main-content main code { max-width: 100%; overflow-x: auto; }
}
