/* ============================================================================
   COUNT-AWARE ROWS
   ============================================================================
   A repeated-content section is laid out from HOW MANY things are in it, not
   from a column count typed in once and then outlived by the content. Four
   related links used to render as 3 + 1, leaving one card stranded at the left
   of its own row; five tiles rendered 3 + 2 with the pair jammed left.

   FLEX, NOT GRID, AND THAT IS THE WHOLE TRICK. A grid can give equal columns
   but it cannot centre an incomplete final row: the orphans sit in tracks 1..n
   and the empty tracks hold the space open on the right. Flex items with a
   fixed basis and `justify-content:center` give identical widths and identical
   gaps AND centre whatever is left over, with no empty cells to leave holes.

   HOW TO USE IT
     <div class="s-row" data-count="4"> … </div>
   `data-count` is written by sfv-grid.js from the number of visible children,
   so adding, removing, hiding or reordering items in Admin re-balances the
   section with no CSS change and no hand-maintained number.

   The balance for each count is the owner's, 2 through 10:
     2→2   3→3   4→2×2   5→3+2   6→3×2   7→4+3   8→4×2   9→3×3   10→5×2
   Anything above ten falls back to four per row, still centred.
   ========================================================================= */

/* SPECIFICITY, DELIBERATELY. Several of these sections were already laid out
   by a display:grid in a page's own inline <style> block, or in a stylesheet
   that loads after this one. An inline block always wins on order, so the
   shared system doubles its class and marks the two properties that decide the
   layout. Everything else stays normal weight so a page can still adjust
   padding, colour or type without fighting this file. */
.s-row.s-row{
  --row-gap: clamp(16px, 2vw, 28px);
  display:flex!important;
  flex-wrap:wrap;
  justify-content:center;
  align-items:stretch;              /* equal card heights within a row */
  gap:var(--row-gap);
}
/* A row of one is not a row. It keeps the card's natural width rather than
   stretching one card across the whole measure. */
.s-row.s-row[data-count="1"]{justify-content:flex-start}
.s-row.s-row[data-count="1"] > *{flex:0 1 auto;max-width:min(100%,520px)}

.s-row.s-row > *{
  min-width:0;                      /* long words wrap instead of overflowing */
  display:flex;
  flex-direction:column;
  /* EQUAL HEIGHTS ARE THE ROW'S JOB, NOT THE CARD'S. Several cards carry
     `height:100%`, which resolves against a parent of auto height and so
     resolves to auto — the card then sizes to its own content and sits short
     beside a taller neighbour. Stretch does it properly, and the explicit
     height is cleared so it cannot interfere. */
  align-self:stretch!important;
  height:auto!important;
}
/* the child's own content fills the card, so equal heights are real and not
   a card with a short body floating above a tall neighbour */
.s-row.s-row > * > *:last-child{margin-bottom:0}

/* ── desktop: the owner's balance ─────────────────────────────────────── */
@media (min-width:1000px){
  .s-row.s-row > *{flex:0 0 calc((100% - 3 * var(--row-gap)) / 4)!important}   /* 11+ */
  .s-row.s-row[data-count="2"] > *,
  .s-row.s-row[data-count="4"] > *{flex-basis:calc((100% - 1 * var(--row-gap)) / 2)!important}
  .s-row.s-row[data-count="3"] > *,
  .s-row.s-row[data-count="5"] > *,
  .s-row.s-row[data-count="6"] > *,
  .s-row.s-row[data-count="9"] > *{flex-basis:calc((100% - 2 * var(--row-gap)) / 3)!important}
  .s-row.s-row[data-count="7"] > *{flex-basis:calc((100% - 3 * var(--row-gap)) / 4)!important}
  .s-row.s-row[data-count="10"] > *{flex-basis:calc((100% - 4 * var(--row-gap)) / 5)!important}
  /* 8 is 4×2 and 7 is 4+3, so both take the four-up basis, which is also the
     fallback above and is repeated here only for the reader. */
  .s-row.s-row[data-count="8"] > *{flex-basis:calc((100% - 3 * var(--row-gap)) / 4)!important}
}

/* ── tablet: balanced two-column rows, odd item centred ───────────────── */
@media (min-width:620px) and (max-width:999.98px){
  .s-row.s-row > *{flex:0 0 calc((100% - 1 * var(--row-gap)) / 2)!important}
  /* three across still reads at this width when the cards are small */
  .s-row.s-row[data-count="3"] > *,
  .s-row.s-row[data-count="6"] > *,
  .s-row.s-row[data-count="9"] > *{flex-basis:calc((100% - 2 * var(--row-gap)) / 3)!important}
}

/* ── mobile: one full-width card per row ──────────────────────────────── */
@media (max-width:619.98px){
  .s-row.s-row{justify-content:stretch}
  .s-row.s-row > *{flex:0 0 100%!important}
  .s-row.s-row[data-count="1"] > *{max-width:100%}
}
