/* Work order creation/edit form: the page shell and its multi-column layout. What goes
   inside the cards is work-order-cards.css.

   The viewport-height shell is .cp-full-viewport-height-page (app-shell.css). Below it one
   flex chain divides that fixed height: the action bar takes its content height, the card
   grid takes what is left, and .cp-scroll-col reads it back as a definite height for
   overflow-y to work against. */

/* The <form> is in the chain too, and so is the fieldset inside it that holds the action bar
   and the card grid (the one a locked order disables, see app/views/pages/work-order/form.js). */
.cp-wo-page, .cp-wo-page form, .cp-wo-lock {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

/* Content height, not a share of the chain's remaining space. */
.cp-wo-action-bar {
  flex: 0 0 auto;
}

/* Takes the height the action bar leaves; align-items: stretch hands it down to each column.

   Also the query container for the breakpoints below. Queried rather than the viewport
   because the sidebar collapses 220px -> 64px (--cp-sidebar-w, app-shell.css), so the same
   screen can hold four comfortable columns collapsed and four cramped ones expanded. */
.cp-wo-cards {
  container-type: inline-size;
  container-name: cp-wo-cards;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

/* Own grid, not Bootstrap's .row/.col-xl: those size off the viewport, which would
   contradict the container query below at exactly the widths it exists to handle. Their
   stacked fallback was dead anyway, since .cp-wo-cards clips and everything past the first
   column scrolled off into hidden overflow. */
.cp-wo-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1rem;
  height: 100%;
}

/* The four columns are two pairs, each either splitting into two tracks or merging into one:

     4 cols: both split   (main|items|customer|status)
     3 cols: side merges  (main|items|customer+status)
     2 cols: both merge   (main+items|customer+status)

   Split, a pair spans two tracks so all four columns line up as siblings, and its single
   auto row stretches to the pair's full height, making each child's height:100% definite:
   the child scrolls itself and the pair's own overflow-y stays inert.

   Merged, the pair becomes the scroll container, one list of cards under one scrollbar:
   min-content rows size to their cards and overflow the pair, and the children's height:100%
   resolves against those rows so their overflow-y goes inert instead. Moving
   --cp-pair-cols and --cp-pair-rows together is the entire per-breakpoint rule.

   Padding follows the scrolling, hence the third property. On the element that does NOT
   scroll it insets the scrollport rather than its content: the view stops short of the page
   and clips content above a strip of background. Inside the scrolling element the same space
   is breathing room that scrolls away with the content. */
.cp-wo-col-pair {
  --cp-pair-cols: 2;
  --cp-pair-rows: auto;
  --cp-pair-pad: 0;
  --cp-col-pad: 1rem;

  display: grid;
  grid-column: span var(--cp-pair-cols);

  /* minmax(0, 1fr) rather than 1fr: a bare 1fr floors at min-content, which a long
     product description would push past. */
  grid-template-columns: repeat(var(--cp-pair-cols), minmax(0, 1fr));

  /* grid-auto-rows is what has to change. align-items: start looks like the natural switch
     and does nothing: the two rows still split the height evenly, and go on doing so even
     with the children's height forced to auto (measured, not assumed). */
  grid-auto-rows: var(--cp-pair-rows);
  gap: 1rem;
  height: 100%;
  min-height: 0;
  overflow-y: auto;
  padding-top: var(--cp-pair-pad);
  padding-bottom: var(--cp-pair-pad);

  /* stylelint-disable-next-line plugin/no-unsupported-browser-features -- see .cp-scroll-col
     below; same cosmetic scrollbar hiding, same reasoning. */
  scrollbar-width: none;
}

.cp-wo-col-pair::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}

/* Each threshold is one number, sized so a column never drops below roughly 320-350px.
   The narrower query stays second: both apply below 1000px and its
   grid-template-columns wins on source order. */

/* The line-item column runs the densest row on the form (a board picker, a size with
   steppers, a cut, a price and a remove button on one line), which an even split of the main
   pair left every one of them truncated, so it takes a quarter of the width off its pair-mate.
   Art and Measurements can afford it, being mostly paired half-width inputs. Bounded to where
   the pair is actually split; below 1000px it merges and the generic rule above is right. */
@container cp-wo-cards (width >= 1000px) {
  .cp-wo-main { grid-template-columns: minmax(0, 0.75fr) minmax(0, 1.25fr); }
}

@container cp-wo-cards (width < 1400px) {
  .cp-wo-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }

  .cp-wo-side {
    --cp-pair-cols: 1;
    --cp-pair-rows: min-content;
    --cp-pair-pad: 1rem;
    --cp-col-pad: 0;
  }
}

@container cp-wo-cards (width < 1000px) {
  .cp-wo-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  .cp-wo-main {
    --cp-pair-cols: 1;
    --cp-pair-rows: min-content;
    --cp-pair-pad: 1rem;
    --cp-col-pad: 0;
  }
}

.cp-scroll-col {
  height: 100%;
  overflow-y: auto;

  /* Inside the scrollport, so the column reaches the page edges while its content keeps
     its inset. See --cp-col-pad above. */
  padding-top: var(--cp-col-pad);
  padding-bottom: var(--cp-col-pad);

  /* stylelint-disable-next-line plugin/no-unsupported-browser-features -- caniuse marks
     scrollbar-width "partial" on some listed Safari versions. Purely cosmetic: the
     ::-webkit-scrollbar rule below covers Chrome/Safari. */
  scrollbar-width: none;

  /* Without this a card taller than the column's available height is squeezed below its own
     content size instead of the column scrolling: overflow-y only kicks in once shrinking
     bottoms out. */
  & > * { flex-shrink: 0; }

  /* Bootstrap's .card is itself display: flex, nesting a flex column inside this one, which
     miscomputes .card-body's auto/content height (~35% short of its real content on the
     Measurements card, verified with getBoundingClientRect() against a scratch Playwright
     script). flex-shrink: 0 on .card-body does not fix it; only removing the nested flex
     context does. .card's flex layout exists for footer alignment and no card here has a
     footer, so plain block flow is both simpler and correct. */
  & .card { display: block; }
}

.cp-scroll-col::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}
