/* =====================================================================
   STOCK & ROLL ERP — responsive layer
   Loaded after app.css. Everything that changes layout is inside a
   max-width query, so the desktop UI is byte-for-byte what it was.
   Only the two blocks marked ALL WIDTHS apply everywhere: one adds a
   class app.css never defined, the other stops long strings pushing the
   page sideways — neither alters desktop spacing or sizing.
   ===================================================================== */

/* ---------------------------------------------------------------------
   ALL WIDTHS — summary rows in the costing tables
   --------------------------------------------------------------------- */
table.data tr.rowsum td {
  background: var(--maroon-050);
  border-top: 1px solid var(--maroon-200);
}
table.data tr.rowsum td strong { color: var(--maroon-800); }
table.data tr.rowsum small.hint { font-weight: 400; }
[data-theme="dark"] table.data tr.rowsum td { background: rgba(249, 115, 22, .12); }
[data-theme="dark"] table.data tr.rowsum td strong { color: var(--maroon-400); }

/* ---------------------------------------------------------------------
   ALL WIDTHS — nothing may push the document sideways
   Grid and flex children default to min-width:auto, so one long word or a
   wide table stretches the whole shell. These reset that without moving
   anything that already fits.
   --------------------------------------------------------------------- */
.shell, .main, .page { min-width: 0; max-width: 100%; }
.card2, .card2__body, .card2__head { min-width: 0; }
.grid2 > *, .grid3 > *, .trail > *, .calcbar > *, .kpi-grid > * { min-width: 0; }
.table-wrap { max-width: 100%; }
.field .hint, .drop__name { overflow-wrap: anywhere; }
img, canvas, svg, video { max-width: 100%; }

/* DataTables builds its own wrapper around the table; without this the
   search box and pagination can overflow on a narrow screen. */
.dataTables_wrapper { max-width: 100%; }
.dataTables_wrapper .dataTables_scrollBody,
.dataTables_wrapper > .row { max-width: 100%; }

/* =====================================================================
   TABLET AND PHONE  (below the 992px desktop breakpoint)
   ===================================================================== */
@media (max-width: 991.98px) {

  /* ---- CRITICAL: hide action sidebar on mobile ----
     The right-side action panel should not appear on mobile. It breaks
     the single-column layout and forces horizontal scroll. */
  .action-sidebar, .action-panel, .quick-actions {
    display: none !important;
  }

  /* ---- top bar ----
     The real cause of the sideways scroll. .topbar is a flex row and
     .search sits in it with flex:1 but no min-width, so it cannot shrink
     below the text input's intrinsic width (~170px). Add the title and
     three round buttons and the row is wider than the screen, which drags
     the whole page across. Flex children need min-width:0 to be allowed
     to shrink. */
  .topbar { padding: 0 .7rem; gap: .5rem; }
  .topbar > * { min-width: 0; }
  .topbar__title {
    min-width: 0; font-size: .98rem;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .search { flex: 1 1 auto; min-width: 0; max-width: none; }
  .search input { min-width: 0; }
  .search__results { max-width: 100vw; }

  /* ---- page frame ---- */
  .page { padding-left: .85rem; padding-right: .85rem; }
  /* Full-bleed: stretch the card past the page's own side padding all
     the way to the true screen edge, so there's no gap left on either
     side for any residual line/sliver to sit in. */
  .card2--bleed {
    margin-left: -.85rem; margin-right: -.85rem;
    border-left: 0; border-right: 0; border-radius: 0;
  }
  .card2--bleed .card2__body { padding-left: .85rem; padding-right: .85rem; }
  .page__head { gap: .7rem; }
  .page__head > div { min-width: 0; flex: 1 1 100%; }
  .page__head h1 { font-size: 1.35rem; overflow-wrap: anywhere; }
  .page__head .d-flex { width: 100%; }
  .page__head .btn2 { flex: 1 1 auto; }

  /* ---- cards ---- */
  .card2__head { flex-wrap: wrap; row-gap: .35rem; }
  .card2__head h3 { flex: 1 1 auto; min-width: 0; }
  .card2__body { padding: .85rem; }

  /* ---- forms ---- */
  .grid2, .grid3 { gap: 0 .7rem; }
  .field { margin-bottom: .8rem; }

  /* Steppers keep a 44px touch target and never squeeze the number box. */
  .stepper button { flex: 0 0 44px; width: 44px; min-width: 44px; }
  .stepper .input { min-width: 0; }

  /* Read-only calculated boxes read as output, not as something to fill. */
  .input--calc { font-family: var(--font-mono); }

  /* ---- tables — improved for mobile readability ---- */
  table.data { 
    font-size: .85rem; 
    border-collapse: collapse;
    width: 100%;
  }
  table.data th, table.data td { 
    padding: .65rem .6rem; 
  }
  table.data th {
    white-space: nowrap !important;
    word-break: normal !important;
    overflow-wrap: normal !important;
    font-weight: 600;
    font-size: .8rem;
  }
  table.data td {
    vertical-align: middle;
    overflow-wrap: break-word;
  }
  /* A wide table scrolls inside its own card rather than moving the page. */
  .table-wrap { 
    overflow-x: auto !important; 
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    display: block;
  }
  /* Safety net for any data table that is not transformed into cards */
  table.data:not(.cards) {
    min-width: max-content;
  }
  .card2__body > table.data:not(.cards),
  .card2 > table.data:not(.cards) { 
    display: table; 
    width: 100%;
    min-width: max-content;
  }

  /* ---- read-only stage panels and calc bars ---- */
  .trail { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .calcbar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .calcbar__v { font-size: 1.05rem; overflow-wrap: anywhere; }

  /* ---- dropdowns stay on screen ---- */
  .dropdown-menu {
    max-width: calc(100vw - 1.6rem);
    max-height: 70vh; overflow-y: auto;
  }
  .dropdown-menu .dropdown-item { white-space: normal; }

  /* ---- action bar: sits below the last field, never floats ---- */
  /* It used to be position:sticky, so Save & Next hovered over the form and
     covered whatever field was underneath it. On a phone it now sits in the
     normal flow at the end of the form, where you reach it by scrolling to
     the bottom like any other control. */
  .actionbar {
    position: static;
    bottom: auto;
    box-shadow: none;
    margin: 1.1rem 0 0;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    flex-wrap: wrap; row-gap: .5rem;
    padding: .8rem .85rem;
  }
  .actionbar .btn2 { flex: 1 1 auto; min-width: 8.5rem; }
  .actionbar .btn2--icon { flex: 0 0 46px; min-width: 46px; }
  .actionbar .autosave { flex: 1 1 100%; order: -1; }
  /* Nothing sticky at the bottom any more, so the page no longer needs to
     reserve a strip of empty space for it. */
  body.has-actionbar .page { padding-bottom: 1.5rem; }
  .page { padding-bottom: calc(var(--tabbar-h) + 1.5rem + env(safe-area-inset-bottom)); }

  /* ---- floating button ---- */
  /* On any screen carrying an action bar the floating button was a second,
     competing primary action sitting on top of the form. It is hidden there
     and stays available everywhere else. */
  body.has-actionbar .fab { display: none; }
  .fab { width: 52px; height: 52px; right: .85rem; }
  .fab svg { width: 23px; height: 23px; }
  .fab:hover { transform: none; }

  /* ---- agreement / print preview ---- */
  .deed { padding: 1.3rem 1.1rem; margin: .8rem 0; font-size: 13px; }

  /* ---- charts ---- */
  .chart-box, .chart-box canvas { height: 210px; max-height: 210px; }
}

/* =====================================================================
   NARROW PHONES (below 480px)
   ===================================================================== */
@media (max-width: 479.98px) {
  .page { padding-left: .7rem; padding-right: .7rem; }
  .card2__body { padding: .75rem; }
  .card2--bleed { margin-left: -.7rem; margin-right: -.7rem; }
  .card2--bleed .card2__body { padding-left: .7rem; padding-right: .7rem; }

  .actionbar { padding-left: .7rem; padding-right: .7rem; gap: .4rem; }
  /* Two per row, evenly, rather than three cramped ones. */
  .actionbar .btn2 { flex: 1 1 calc(50% - .5rem); min-width: 0; padding: .6rem .7rem; }
  .actionbar .btn2--icon { flex: 0 0 46px; }

  .trail { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .4rem; }
  .calcbar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .calcbar__v { font-size: .98rem; }

  /* ---- improved table spacing for narrow phones ---- */
  table.data { 
    font-size: .8rem;
  }
  table.data th { 
    font-size: .75rem;
    padding: .5rem .4rem;
  }
  table.data td { 
    padding: .55rem .4rem;
  }

  .fab { width: 48px; height: 48px; }
  .fab svg { width: 21px; height: 21px; }

  .chip { font-size: .68rem; }
  .page__head h1 { font-size: 1.2rem; }

  /* Narrowest phones: the search keeps shrinking rather than the row
     breaking, and the title gives up its space first. */
  .topbar { gap: .4rem; padding: 0 .55rem; }
  .topbar__title { font-size: .9rem; flex: 0 1 auto; }
  .search { flex: 1 1 90px; }
  .search input { font-size: .82rem; padding-left: 2rem; }
}

/* =====================================================================
   SHORT LANDSCAPE — the sticky rail leaves no room for the form
   ===================================================================== */
@media (max-width: 991.98px) and (max-height: 480px) {
  .rail--sticky { position: static; }
}

/* =====================================================================
   MOBILE CARD ROWS — 40 / 60 FLEX LAYOUT
   Loaded after app.css so these rules win without !important.
   Each td is a flex row: ::before (label) gets 40 %, the value content
   gets 60 %. min-width:0 on both sides prevents a long phone number,
   currency amount, or chip from expanding past the card boundary.
   ===================================================================== */
@media (max-width: 767.98px) {
  /* Push the label to the left edge and the value to the right edge.
     Without this, td (display:flex, default justify-content:flex-start)
     leaves bare text values — Name, Company, City, etc. with no wrapping
     element to target — sitting flush left right after the label, since
     nothing else in this file or app.css sets justify-content on the td. */
  table.cards td {
    justify-content: space-between;
  }

  /* The label pseudo-element: fixed 40 %, never wider, never narrower */
  table.cards td::before {
    flex: 0 0 40%;
    min-width: 0;
    overflow-wrap: anywhere;
  }

  /* Direct children of the td act as the value column (chips, links, spans,
     strong, small, etc.) — up to 60 % basis, min-width:0 so it can shrink
     below that when the content is wide. flex-grow is 0 (not 1): with
     justify-content:space-between above, the item only needs to size to
     its own content and space-between carries it to the right edge. A
     grow of 1 was stretching short content — like the OPEN button — to
     fill the entire 60 % column instead of sitting right-aligned at its
     natural size. */
  table.cards td > * {
    flex: 0 1 60%;
    min-width: 0;
    text-align: right;
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /* Buttons and chips don't stretch to fill the 60% value column the way
     plain text does, so they were sitting short of the right edge that
     numeric/text values reach. Pin them to the true right edge instead
     and let them size to their own content. */
  table.cards td > button,
  table.cards td > .btn2,
  table.cards td > .chip {
    flex: 0 0 auto;
    margin-left: auto;
  }

  /* Chips and other inline-flex children must also respect min-width:0 */
  table.cards td > .chip,
  table.cards td > a,
  table.cards td > strong,
  table.cards td > small,
  table.cards td > span {
    max-width: 100%;
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /* The title row is a single-column header — let it span the full width */
  table.cards td.cards__title {
    flex-wrap: wrap;
  }
  table.cards td.cards__title::before {
    flex: 0 0 auto;
  }

  /* ---- Card list width fix — Cards span 100% full width on mobile ---- */
  .table-wrap:has(table.cards) {
    padding-right: 0 !important;
    overflow: visible !important;
  }
}

/* =====================================================================
   CARD BOX-SHADOW / BORDER CLIPPING FIX
   ===================================================================== */
@media (max-width: 767.98px) {
  .table-wrap:has(table.cards) {
    overflow: visible !important;
    padding-right: 0 !important;
  }
}

/* =====================================================================
   NOTHING OVERFLOWS SIDEWAYS
   Belt and braces for the whole ERP on a phone: any element that reports a
   fixed width, a wide minimum, or an unbreakable string gets reined in.
   Scoped to mobile so desktop table layouts are untouched.
   ===================================================================== */
@media (max-width: 991.98px) {
  /* Long unbroken strings — docket numbers, emails, share URLs, file names */
  td:not(.cards__title), p, li, dd, .chip, .trail__v, .search__item, .dropdown-item {
    overflow-wrap: break-word;
  }
  th {
    white-space: nowrap !important;
    word-break: normal !important;
    overflow-wrap: normal !important;
  }
  /* Inline widths pasted into markup are the usual culprit */
  [style*="min-width"]:not(th):not(td) { min-width: 0 !important; }
  .modal-dialog { margin: .5rem; max-width: calc(100vw - 1rem); }
  .modal-body { overflow-x: auto; }
  /* DataTables controls stack instead of squeezing side by side */
  .dataTables_wrapper .dataTables_filter,
  .dataTables_wrapper .dataTables_length,
  .dataTables_wrapper .dataTables_info,
  .dataTables_wrapper .dataTables_paginate {
    float: none; text-align: left; width: 100%; margin-bottom: .4rem;
  }
  .dataTables_wrapper .dataTables_filter input { width: 100%; margin-left: 0; }
  .dataTables_wrapper .dataTables_paginate .paginate_button { padding: .3rem .55rem; }
  /* Anything genuinely too wide scrolls inside its own box, not the page */
  .card2__body > table, .card2__body > .dataTables_wrapper { max-width: 100%; }
  /* The stage rail scrolls horizontally by design — keep it contained */
  .rail { max-width: 100%; }
}

/* ---------------------------------------------------------------------
   ALL WIDTHS — phone value should read as plain text, not a shouting
   maroon link. Placed last in the last-loaded stylesheet with !important
   so it wins regardless of load order or any other rule touching `a`.
   --------------------------------------------------------------------- */
table td[data-label="Phone"] a,
table.data td[data-label="Phone"] a,
table.cards td[data-label="Phone"] a {
  color: var(--ink) !important;
  font-weight: 400 !important;
  text-decoration: none !important;
}
table td[data-label="Phone"] a:hover,
table td[data-label="Phone"] a:focus-visible {
  color: var(--maroon-600) !important;
  text-decoration: underline !important;
}

/* =====================================================================
   MOBILE POPUPS — footer buttons, read-only fields, matrix tables
   Booking popup, product form and every other modal added since the
   blocks above: buttons in a modal footer are a flex row with no wrap,
   so two full-width buttons ("Cancel" + "Book in") can still overflow
   past each other on very narrow phones. Stack them instead, each still
   a full 46px touch target, and keep any matrix table inside the popup
   scrolling in its own box rather than widening the dialog.
   ===================================================================== */
@media (max-width: 991.98px) {
  .modal-footer {
    flex-wrap: wrap;
    gap: .5rem;
    row-gap: .5rem;
  }
  .modal-footer .btn2 {
    flex: 1 1 auto;
    min-width: 45%;
  }
  .modal-body { padding: .9rem; }
  .modal-body .grid2, .modal-body .grid3 { gap: 0 .6rem; }
  .modal-body .field { margin-bottom: .7rem; }

  /* Read-only auto-filled boxes (Manufacturing Cost, Colour/Size display)
     wrap their content instead of clipping it. */
  .modal-body .input[readonly],
  .modal-body div.input {
    overflow-wrap: anywhere;
    white-space: normal;
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  /* The marketplace-split matrix (psf-matrix) scrolls inside its own box
     instead of forcing the modal wider than the screen. */
  .psf-scroll {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .psf-matrix { min-width: 340px; }
  .psf-matrix th, .psf-matrix td { padding: .5rem .5rem; }
  .psf-matrix input.input { min-width: 72px; }

  /* Every interactive control in a popup keeps a real touch target,
     including selects and plain text/number inputs that app.css leaves
     at their intrinsic size. */
  .modal-body .input,
  .modal-body select,
  .modal-body input[type="text"],
  .modal-body input[type="number"],
  .modal-body input[type="date"],
  .modal-body input[type="email"] {
    min-height: 44px;
  }
}

/* =====================================================================
   SEARCHABLE SELECT — Sales "Item sold" / Returns line combobox
   Vanilla-JS searchable dropdown (see app.js searchableSelect()). The
   panel is positioned by JS to sit under its input, so it only needs to
   never run past the viewport edge or get clipped by a card's overflow.
   ===================================================================== */
.psf-search-combo { position: relative; min-width: 0; }
.psf-search-combo input.input { width: 100%; }
.psf-search-combo__panel {
  position: absolute; z-index: 1080;
  left: 0; right: 0; top: calc(100% + 4px);
  max-height: 280px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-1);
  -webkit-overflow-scrolling: touch;
}
.psf-search-combo__opt {
  padding: .55rem .75rem; cursor: pointer; font-size: .88rem;
  min-height: 40px; display: flex; align-items: center;
  overflow-wrap: anywhere;
}
.psf-search-combo__opt:hover,
.psf-search-combo__opt.is-active { background: var(--maroon-050); }
.psf-search-combo__empty { padding: .6rem .75rem; color: var(--muted); font-size: .85rem; }
@media (max-width: 767.98px) {
  .psf-search-combo__panel { max-height: 55vh; left: 0; right: 0; }
}