/* ============================================================================
   Ruminax ERP — mobile responsiveness layer
   Loaded globally after the theme + my.css so it wins on the phone breakpoints.
   Goal: nothing overflows the viewport, the cash numbers stay readable, and the
   Cash In / Cash Out actions become one-thumb targets. Desktop is untouched.
   ============================================================================ */

/* ---- Global safety net: never let a stray wide child scroll the page ------ */
html, body { max-width: 100%; }
body { overflow-x: hidden; }

/* ---- Bootstrap-4 custom-file height fix (all widths) ----------------------
   BS4's `.custom-file-label` is absolutely positioned; if the `.custom-file`
   wrapper is shorter than the label (a theme override left it ~27px while the
   label is ~41px), the label overlaps whatever follows — so the attachment
   help text ("Attach one or more images or PDF files…") rendered BEHIND the
   file input. Force the wrapper + input + label to one consistent height so
   following content clears it. */
.custom-file,
.custom-file-input,
.custom-file-label {
  height: calc(1.5em + 0.75rem + 2px) !important;
}
.custom-file-label {
  display: flex;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.custom-file-label::after { height: calc(1.5em + 0.75rem) !important; display: flex; align-items: center; }

/* ---- Mobile shell guard --------------------------------------------------
   The CORK theme already turns the sidebar into an off-canvas drawer at
   ≤991px (opened by the hamburger, which adds .sbar-open). These rules just
   harden that default so the drawer is reliably hidden until opened and the
   content stays full-width — belt-and-suspenders against any init race.
   The hamburger + overlay (app.js) still open/close it exactly as before. */
@media (max-width: 991.98px) {
  .main-container:not(.sbar-open) .sidebar-wrapper {
    width: 0 !important;
    left: -60px !important;
    overflow: hidden;
  }
  .main-container.sbar-open .sidebar-wrapper {
    width: 255px !important;
    left: 0 !important;
    overflow: visible;
  }
  #content, .main-container.sidebar-closed #content { margin-left: 0 !important; }
}

/* Long money figures (PKR 1,000,000.00) must shrink before they clip or push
   their card wider than the screen. Applies to the custom KPI/metric values. */
@media (max-width: 575.98px) {
  .metric-val,
  .kpi-value,
  .stat-value,
  .org-title {
    font-size: clamp(1rem, 6.2vw, 1.3rem) !important;
    word-break: break-word;
    overflow-wrap: anywhere;
  }
}

/* ===========================================================================
   PHONES (≤ 767px) — the main mobile breakpoint
   =========================================================================== */
@media (max-width: 767.98px) {

  /* --- Content gutters: tighten so cards get usable width ------------------ */
  .main-content .layout-px-spacing,
  .main-content .layout-spacing { padding-left: 12px !important; padding-right: 12px !important; }

  .main-content { padding-top: 0 !important; }

  /* --- Hero / banners: smaller padding + type on a phone ------------------- */
  .dashboard-hero-banner { padding: 18px 16px !important; }
  .dashboard-hero-banner::after { width: 110px; height: 110px; }
  .org-title { font-size: 1.25rem !important; }

  /* --- Any "space-between" toolbar must wrap instead of crushing children -- */
  .d-flex.justify-content-between:not(.flex-nowrap) { flex-wrap: wrap; row-gap: 10px; }

  /* --- Card bodies: a little less padding so content breathes -------------- */
  .widget-content, .card-body, .filter-card, .metric-card, .cashbook-bar-card {
    padding-left: 14px !important;
    padding-right: 14px !important;
  }

  /* --- Buttons: comfortable touch height; action rows go full-width -------- */
  .btn { min-height: 42px; }

  /* Cash In / Cash Out / primary actions become full-width, stacked buttons.
     These two bars in the cashbook + dashboard are the key mobile action. */
  .cashbook-bar-card .d-flex.align-items-center.flex-wrap,
  .cashbook-bar-card .d-flex.align-items-center.flex-wrap > * { width: 100%; }
  .cashbook-bar-card .btn-quick-in,
  .cashbook-bar-card .btn-quick-out,
  .cashbook-bar-card .btn-cashbook-primary {
    width: 100%;
    margin-right: 0 !important;
    margin-bottom: 8px;
    justify-content: center;
  }

  /* --- Filter forms: each control on its own full-width row ---------------- */
  #filterForm .form-row > [class^="col"],
  #filterForm .form-row > [class*=" col"] { flex: 0 0 100%; max-width: 100%; margin-bottom: 10px; }
  #filterForm .btn,
  #filterForm .btn-block { width: 100%; }

  /* --- Tables: keep horizontal scroll inside their own box, add an edge
         fade hint so users know there's more to the right ------------------- */
  .table-responsive {
    -webkit-overflow-scrolling: touch;
    position: relative;
  }
  .table-responsive > table { min-width: 560px; } /* force the scroll, not squish */

  /* Pills / action buttons inside table cells shouldn't wrap mid-word */
  td .btn, td .btn-receipt-pill { white-space: nowrap; }

  /* --- In-dashboard branch switcher: full width, easy to tap --------------- */
  .dashboard-branch-box { padding: 10px 12px !important; }
  .branch-select-btn { width: 100%; }
  .dashboard-branch-box .dropdown-menu { min-width: 0 !important; width: 100% !important; }

  /* --- Recent-activity / two-column rows collapse cleanly ------------------ */
  .col-xl-8, .col-xl-4, .col-lg-7, .col-lg-5 { margin-bottom: 16px; }
}

/* ===========================================================================
   SECONDARY NAVBAR (.sub-header-container) on phones
   The sub-header is `navbar-expand-sm`, so below 576px Bootstrap collapses it:
   the hamburger and the branch switcher stop sharing a row, the switcher floats
   off to one side, and the dropdown renders in-flow — leaving a tall empty bar.
   Keep it a single tidy row: hamburger left, compact branch switcher right.
   =========================================================================== */
@media (max-width: 767.98px) {
  /* Force the sub-header to stay one horizontal row, overriding the collapse */
  .sub-header-container .navbar {
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    min-height: 52px !important;
    padding: 6px 4px !important;
  }

  /* The empty breadcrumb list shouldn't consume the row; let the switcher sit
     at the right via its own ml-auto. Hide the (empty) breadcrumb slot. */
  .sub-header-container .navbar > ul.navbar-nav.flex-row:first-of-type { display: none; }

  /* Hamburger: fixed, tappable target on the left */
  .sub-header-container .sidebarCollapse {
    padding: 0 10px 0 12px !important;
    flex: 0 0 auto;
  }

  /* The switcher list stays inline on the right, no wrapping */
  .sub-header-container .navbar-nav.ml-auto {
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    margin-left: auto !important;
  }
  .branch-switcher-dropdown { position: static; }

  /* Compact the branch button so it + the hamburger fit a 360px screen */
  .branch-btn { padding: 4px 10px !important; max-width: 68vw; }
  .branch-icon-pill { width: 26px; height: 26px; margin-right: 8px !important; }
  .branch-sublabel { font-size: 0.56rem !important; }
  .branch-active-name { max-width: 42vw !important; font-size: 0.8rem !important; }

  /* The dropdown MUST be absolutely positioned so it overlays rather than
     stretching the bar; anchor it to the right edge under the button. */
  .branch-dropdown-menu {
    position: absolute !important;
    right: 8px !important;
    left: auto !important;
    top: 100% !important;
    width: calc(100vw - 24px) !important;
    max-width: 320px !important;
    min-width: 0 !important;
  }
}

/* ===========================================================================
   MODALS → always fit the viewport, BODY scrolls (laptop + desktop + mobile)
   The data-entry modals (cash entry, journal voucher) have many fields and run
   taller than the screen, so a centered dialog spilled off the top and bottom
   and the footer (Save/Cancel) ended up below the fold — AND the inner body
   would not scroll, because a plain `.modal-dialog-centered` (without Bootstrap's
   `.modal-dialog-scrollable`) is designed to grow with its content, not scroll.
   Bootstrap also injects a `::before` spacer on centered dialogs that forces
   full viewport height and defeats an internal cap.

   Fix: give EVERY modal dialog the scrollable+centered behavior Bootstrap
   normally reserves for `.modal-dialog-scrollable.modal-dialog-centered` —
   cap the dialog to the viewport, make it a flex column, drop the spacer, and
   let the body scroll between a pinned header and footer. Runs at all widths.
   =========================================================================== */
.modal-dialog {
  display: flex;
  flex-direction: column;
  justify-content: center;
  /* cap the whole dialog to the viewport (vh, so it resolves regardless of
     whether an ancestor has an explicit height) minus the 1.75rem top+bottom
     margin Bootstrap gives the dialog. */
  max-height: calc(100vh - 3.5rem);
}
/* kill the centered-dialog spacer pseudo-element that forces full height */
.modal-dialog-centered::before { content: none !important; display: none !important; }
.modal-dialog-centered { min-height: auto; }

.modal-content {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 3.5rem);
  overflow: hidden; /* clip so the inner body scrolls, not the whole dialog */
}
/* Some modals wrap their body + footer in a <form> inside .modal-content, so
   the body is NOT a direct child. Make that <form> a flex column too and have
   it fill the content height, so the body below can scroll. */
.modal-content > form {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}
.modal-content .modal-header,
.modal-content .modal-footer { flex-shrink: 0; }
.modal-content .modal-body {
  flex: 1 1 auto;
  /* min-height:0 is REQUIRED — a flex item defaults to min-height:auto, which
     refuses to shrink below its content height, so without this the body stays
     full-height and never scrolls (the footer then spills past the viewport). */
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ===========================================================================
   MODALS → bottom-anchored sheets on phones
   The entry / edit / attachment modals are tall; make the body scroll and the
   footer actions wrap so nothing is pushed off-screen.
   =========================================================================== */
@media (max-width: 575.98px) {
  /* Fit the dialog to the viewport height and make it a flex column so the
     body scrolls BETWEEN a pinned header and footer — the footer can never
     overlap the form content (the bug on the cash & journal modals). */
  .modal-dialog:not(.modal-sm) {
    max-width: 100%;
    margin: 0.5rem;
  }
  .modal-dialog.modal-lg,
  .modal-dialog.modal-xl { max-width: 100%; }

  .modal-dialog.modal-dialog-centered,
  .modal-dialog:not(.modal-sm) {
    min-height: calc(100% - 1rem);
    align-items: stretch;
  }

  .modal-content {
    border-radius: 14px;
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 1rem);
    overflow: hidden; /* clip so the flex body scrolls instead of the footer spilling */
  }
  .modal-header, .modal-footer { flex: 0 0 auto; }

  /* Body takes the remaining space and scrolls; header/footer stay put */
  .modal-body {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Footer buttons: stack full-width, primary action on top (DOM order kept) */
  .modal-footer { flex-wrap: wrap; row-gap: 8px; }
  .modal-footer > .btn { flex: 1 1 100%; margin: 0; }
  /* when a footer splits into two groups (e.g. download/open vs close) let the
     groups each take the full row rather than colliding */
  .modal-footer.justify-content-between > * { flex: 1 1 100%; }
  .modal-footer.justify-content-between > div .btn { width: 100%; margin-bottom: 6px; }

  /* Attachment preview tiles: a bit smaller so a row fits the screen */
  .multi-preview-item { width: 76px; height: 76px; }

  /* The inline PDF viewer: shorter on a phone so the modal isn't a tunnel */
  #viewAttachmentModalPdf { height: 60vh !important; }
  #viewAttachmentModalImg { max-height: 60vh !important; }

  /* --- JOURNAL / EDIT VOUCHER MODAL ----------------------------------------
     These modals set `overflow: visible !important` on the dialog/content/body
     and on the journal-lines table, so the account picker's dropdown wouldn't
     be clipped. But the Tom Select dropdown is appended to <body>
     (dropdownParent:'body'), so it never needed that — and on a phone the
     visible-overflow meant the modal body couldn't scroll vertically and the
     wide debit/credit table spilled off-screen with no way to reach it.
     Restore scrolling: body scrolls vertically, the lines table scrolls
     horizontally, so every column (incl. Debit & Credit) is reachable. */
  #journalModal .modal-body,
  #editModal .modal-body {
    overflow-y: auto !important;
    overflow-x: hidden !important;
    flex: 1 1 auto;
  }
  #journalModal .modal-dialog,
  #editModal .modal-dialog { overflow: visible !important; }
  /* The debit/credit grid must scroll sideways so all columns are reachable */
  #journalModal .table-responsive,
  #editModal .table-responsive {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  #journalModal #voucherLinesTable,
  #editModal .table-voucher-grid { min-width: 620px; }

  /* Tell the user the wide debit/credit grid scrolls sideways (hidden on
     desktop where the whole table fits). */
  #journalModal .voucher-scroll-hint { display: block !important; }
}

/* ===========================================================================
   Small-tablet tidy-up (768–991px): 2-up KPI cards already handled by the
   grid; just make sure custom flex toolbars wrap here too.
   =========================================================================== */
@media (min-width: 768px) and (max-width: 991.98px) {
  .d-flex.justify-content-between:not(.flex-nowrap) { flex-wrap: wrap; }
}
