/*
 * villas-more/vm-card-grid — front-end styles (Task 018).
 *
 * RULE (TASK-018 §6.1): no colour, font, font size or spacing value is
 * written here. Appearance comes from theme tokens (`--wp--preset--*`).
 * The literals that remain are structural and carry no appearance:
 * grid/flex declarations, aspect-ratio, scroll-snap, and the two
 * minimum track widths (16rem desktop, 13rem tablet) that decide when a
 * column stops fitting. Those are layout thresholds, not design values.
 *
 * Loaded only on pages where the block appears (block.json `style`, §6.2).
 *
 * Card counts are read in CSS with :has(), not in PHP, because the block
 * saves statically (§6.3) and therefore cannot count its own children at
 * render time.
 */

.vmc-grid {
	/* One gap, from the theme scale, everywhere. Not a setting (§5). */
	--vmc-gap: var(--wp--preset--spacing--30);

	display: grid;
	gap: var(--vmc-gap);
	/*
	 * Auto: fill the row by width, never more than five per row. The
	 * max() floor is what caps it at five — a track can never be
	 * narrower than a fifth of the row.
	 */
	grid-template-columns: repeat(
		auto-fit,
		minmax( max( 16rem, calc( ( 100% - 4 * var(--vmc-gap) ) / 5 ) ), 1fr )
	);
}

/* --------------------------------------------------------------
   1. Columns — desktop. The editor chooses only this one (§2.2).
   -------------------------------------------------------------- */

.vmc-grid--cols-2 { grid-template-columns: repeat( 2, minmax( 0, 1fr ) ); }
.vmc-grid--cols-3 { grid-template-columns: repeat( 3, minmax( 0, 1fr ) ); }
.vmc-grid--cols-4 { grid-template-columns: repeat( 4, minmax( 0, 1fr ) ); }
.vmc-grid--cols-5 { grid-template-columns: repeat( 5, minmax( 0, 1fr ) ); }

/* --------------------------------------------------------------
   2. Columns — tablet and phone. Derived in code, never chosen (§4).
   Breakpoints are the theme's own viewport tokens: 781px / 600px.
   -------------------------------------------------------------- */

@media (max-width: 781px) {
	.vmc-grid {
		grid-template-columns: repeat(
			auto-fit,
			minmax( max( 13rem, calc( ( 100% - 2 * var(--vmc-gap) ) / 3 ) ), 1fr )
		);
	}

	.vmc-grid--cols-5 { grid-template-columns: repeat( 3, minmax( 0, 1fr ) ); }

	.vmc-grid--cols-2,
	.vmc-grid--cols-3,
	.vmc-grid--cols-4 { grid-template-columns: repeat( 2, minmax( 0, 1fr ) ); }
}

@media (max-width: 600px) {
	.vmc-grid,
	.vmc-grid--cols-2,
	.vmc-grid--cols-3,
	.vmc-grid--cols-4,
	.vmc-grid--cols-5 { grid-template-columns: minmax( 0, 1fr ); }

	/* The one exception in §4: Compact keeps two per row on a phone. */
	.vmc-grid--cardstyle-compact { grid-template-columns: repeat( 2, minmax( 0, 1fr ) ); }
}

/* --------------------------------------------------------------
   3. The grid's card style, passed down as a default.
   A card that sets its own style carries `vmc-card--<style>` and no
   `vmc-card--inherit`, so none of these selectors can reach it. That
   is the whole inheritance mechanism — no JavaScript, no PHP.
   Each set is complete, never a delta.
   -------------------------------------------------------------- */

.vmc-grid--cardstyle-panel > .vmc-card--inherit {
	--vmc-ratio: 3 / 2;
	--vmc-columns: minmax( 0, 1fr );
	--vmc-align: stretch;
	--vmc-title-size: var(--wp--preset--font-size--intro);
	--vmc-text-size: var(--wp--preset--font-size--small);
	--vmc-inner-gap: var(--wp--preset--spacing--30);
	--vmc-pad: var(--wp--preset--spacing--20) calc( var(--wp--preset--spacing--20) + var(--wp--preset--spacing--10) / 2 ) calc( var(--wp--preset--spacing--20) + var(--wp--preset--spacing--10) / 2 );
	--vmc-bg: var(--wp--preset--color--white);
	--vmc-border-width: 1px;
	--vmc-border-color: var(--wp--preset--color--border);
	--vmc-radius: 2px;
	--vmc-rule-width: 2px;
}

.vmc-grid--cardstyle-compact > .vmc-card--inherit {
	--vmc-ratio: 1 / 1;
	--vmc-columns: minmax( 0, 1fr );
	--vmc-align: stretch;
	--vmc-title-size: var(--wp--preset--font-size--small);
	--vmc-text-size: var(--wp--preset--font-size--small);
	--vmc-inner-gap: var(--wp--preset--spacing--30);
	--vmc-pad: 0px;
	--vmc-bg: transparent;
	--vmc-border-width: 0px;
	--vmc-border-color: transparent;
	--vmc-radius: 0px;
	--vmc-rule-width: 0px;
}

.vmc-grid--cardstyle-editorial > .vmc-card--inherit {
	--vmc-ratio: 4 / 5;
	--vmc-columns: minmax( 0, 1fr );
	--vmc-align: stretch;
	--vmc-title-size: var(--wp--preset--font-size--large);
	--vmc-text-size: var(--wp--preset--font-size--medium);
	--vmc-inner-gap: var(--wp--preset--spacing--30);
	--vmc-pad: 0px;
	--vmc-bg: transparent;
	--vmc-border-width: 0px;
	--vmc-border-color: transparent;
	--vmc-radius: 0px;
	--vmc-rule-width: 0px;
}

/* --------------------------------------------------------------
   3a. The photograph's shape, when the grid chooses one (Task 018-2).
   Written after section 3 so it beats an inherited style at equal
   specificity, and it reaches a card that set its own style too. No
   class is written for "inherit", so nothing changes unless chosen.
   Both featured layouts outrank it (§5) and keep their own shapes.
   -------------------------------------------------------------- */

.vmc-grid--ratio-3-2 > .vmc-card { --vmc-ratio: 3 / 2; }
.vmc-grid--ratio-4-5 > .vmc-card { --vmc-ratio: 4 / 5; }
.vmc-grid--ratio-5-4 > .vmc-card { --vmc-ratio: 5 / 4; }
.vmc-grid--ratio-1-1 > .vmc-card { --vmc-ratio: 1 / 1; }

/* --------------------------------------------------------------
   4. Slider (§2.3). Pure CSS scroll-snap. No front-end JavaScript.
   It engages only at three or more cards; with two they stack, which
   is what the :has() guard is for.
   -------------------------------------------------------------- */

.vmc-grid--display-slider:has( > .vmc-card:nth-child(3) ) {
	display: flex;
	overflow-x: auto;
	scroll-snap-type: x mandatory;
	scroll-padding-inline-start: 0;
}

.vmc-grid--display-slider:has( > .vmc-card:nth-child(3) ) > .vmc-card {
	flex: 0 0 calc( ( 100% - var(--vmc-gap) ) / 1.5 );
	scroll-snap-align: start;
}

@media (max-width: 600px) {
	.vmc-grid--display-mobile-slider:has( > .vmc-card:nth-child(3) ) {
		display: flex;
		overflow-x: auto;
		scroll-snap-type: x mandatory;
		scroll-padding-inline-start: 0;
	}

	.vmc-grid--display-mobile-slider:has( > .vmc-card:nth-child(3) ) > .vmc-card {
		flex: 0 0 calc( ( 100% - var(--vmc-gap) ) / 1.5 );
		scroll-snap-align: start;
	}
}

/*
 * Keyboard operation (§2.3). The scroller carries tabindex="0" whenever
 * the display setting can produce a slider, so arrow keys scroll it; the
 * focus ring has to be visible or the stop is a trap in practice.
 */
.vmc-grid[tabindex]:focus-visible {
	outline: 2px solid var(--wp--preset--color--primary);
	outline-offset: 4px;
}

/* --------------------------------------------------------------
   5. Featured (§2.4). Desktop only — on tablet and phone the first card
   is just the first card. Emphasis by a change of kind, never by size.
   -------------------------------------------------------------- */

@media (min-width: 782px) {

	/* 5a. Half and half ------------------------------------------------ */

	/*
	 * REBUILT in Task 018-1's third pass, from a RULE rather than from a
	 * ratio. The two drawings disagree — the 2026-09-05 board shows a 4:3
	 * feature beside 84px square thumbnails, the 2026-09-06 one shows 4:5
	 * beside wide rows — and neither fills the height by its own
	 * proportions, so copying either reproduces the fault it was meant to
	 * fix. Measured before this: a 868px left column beside 411px of list,
	 * with 457px of empty right-hand side and nothing aligned to anything.
	 *
	 * The rule instead: THE LIST SETS THE HEIGHT AND THE PHOTOGRAPH FILLS
	 * IT. Three things make that true, and all three are load-bearing.
	 *
	 * 1. `minmax( 0, auto )` rows, not `auto` and not a trailing `1fr`
	 *    (018-1's reasoning; Task 018-2 brings the spacer back — below).
	 *    A spanning item contributes its size to any track with an
	 *    intrinsic MINIMUM, which is how the featured card used to push the
	 *    list apart; a `0` minimum takes that contribution away entirely.
	 *    The old trailing `1fr` solved the same problem by parking the
	 *    surplus in a spacer row — which is exactly the 457px hole.
	 * 2. `align-items: stretch`, so the columns align by HEIGHT rather
	 *    than both starting at the top and ending wherever they end.
	 * 3. `min-height: 0` on the featured media. Without it a grid item
	 *    will not shrink below its min-content height, and for a box with
	 *    an aspect-ratio that is the ratio's own height — 711px here — so
	 *    the ratio would go on dictating the row after all. With it, the
	 *    ratio is where the media starts and `object-fit: cover` keeps the
	 *    crop honest as it grows.
	 *
	 * Row gap is zero: the list is separated by hairlines and its own
	 * padding, the way a list is, not by grid gutters. The column gap stays.
	 *
	 * AMENDED in Task 018-2, on Ana's ruling: the list rows pack at the
	 * TOP, as the 2026-09-05 board draws them, rather than stretching to
	 * the photograph's height. Each row is `auto`, and a trailing
	 * `minmax( 0, 1fr )` track takes whatever height the feature has over
	 * the list, so the three rows keep their own height and the hairlines
	 * sit under their own words. If the list is ever the taller side, the
	 * spacer is 0 and the photograph still grows to match (point 3).
	 */
	.vmc-grid--featured-half {
		grid-template-columns: 1.15fr .85fr;
		align-items: stretch;
		gap: 0 var(--vmc-gap);
	}

	.vmc-grid--featured-half:has( > .vmc-card:nth-child(2) ) { grid-template-rows: repeat( 1, auto ) minmax( 0, 1fr ); }
	.vmc-grid--featured-half:has( > .vmc-card:nth-child(3) ) { grid-template-rows: repeat( 2, auto ) minmax( 0, 1fr ); }
	.vmc-grid--featured-half:has( > .vmc-card:nth-child(4) ) { grid-template-rows: repeat( 3, auto ) minmax( 0, 1fr ); }
	.vmc-grid--featured-half:has( > .vmc-card:nth-child(5) ) { grid-template-rows: repeat( 4, auto ) minmax( 0, 1fr ); }
	.vmc-grid--featured-half:has( > .vmc-card:nth-child(6) ) { grid-template-rows: repeat( 5, auto ) minmax( 0, 1fr ); }
	.vmc-grid--featured-half:has( > .vmc-card:nth-child(7) ) { grid-template-rows: repeat( 6, auto ) minmax( 0, 1fr ); }
	.vmc-grid--featured-half:has( > .vmc-card:nth-child(8) ) { grid-template-rows: repeat( 7, auto ) minmax( 0, 1fr ); }
	.vmc-grid--featured-half:has( > .vmc-card:nth-child(9) ) { grid-template-rows: repeat( 8, auto ) minmax( 0, 1fr ); }

	/* Forced Editorial on the featured card — overrides any card choice. */
	.vmc-grid--featured-half > .vmc-card:first-child {
		grid-column: 1;
		grid-row: 1 / -1;
		min-height: 0;
		/*
		 * The media row takes every pixel the list gives it and the text
		 * below keeps its own height. `1fr auto` is what says that; without
		 * it the surplus would be split between the two and the words would
		 * float away from the picture.
		 */
		grid-template-rows: minmax( 0, 1fr ) auto;
		--vmc-ratio: 4 / 3;
		--vmc-columns: minmax( 0, 1fr );
		--vmc-align: stretch;
		--vmc-title-size: var(--wp--preset--font-size--large);
		--vmc-text-size: var(--wp--preset--font-size--medium);
		--vmc-inner-gap: var(--wp--preset--spacing--30);
		--vmc-pad: 0px;
		--vmc-bg: transparent;
		--vmc-border-width: 0px;
		--vmc-border-color: transparent;
		--vmc-radius: 0px;
		--vmc-rule-width: 0px;
	}

	/*
	 * Four declarations, and each one is answering a different failure —
	 * this was arrived at by measuring four candidate rule-sets on the
	 * real page, not by reasoning.
	 *
	 * `height: 100%` fills the 1fr track, so the list's height reaches
	 * the photograph. `min-height: 0` is what lets it: a grid item will
	 * not shrink below its min-content height, and for a box with an
	 * aspect-ratio that IS the ratio's height, so without this the ratio
	 * would go on dictating the row.
	 *
	 * `width: 100%` and `min-width: 0` are the ones that are easy to miss
	 * and cost the most. With only the height pinned, the media keeps
	 * satisfying `aspect-ratio` by growing SIDEWAYS — measured: a taller
	 * list pushed the media from 654px to 854px wide and stole 200px from
	 * the list beside it, because the column's `1.15fr` has an auto
	 * minimum that follows its content. Pinning the width to the column
	 * makes both axes definite, the ratio stops driving anything, and
	 * `object-fit: cover` on the image does the cropping.
	 *
	 * What that leaves: the ratio is the MINIMUM, and above it the list
	 * governs and the photograph grows. Task 018-2 changed the ratio from
	 * 4:5 to the 4:3 of the 2026-09-05 board, so with three short items
	 * the feature is now the taller side (see TASK-018-2.md).
	 */
	.vmc-grid--featured-half > .vmc-card:first-child .vmc-card__media {
		min-height: 0;
		min-width: 0;
		height: 100%;
		width: 100%;
	}

	/* Forced Compact on the list beside it, and it is a list, not a stack
	 * of cards: a hairline between rows, its own vertical padding, and no
	 * grid gutter. First row flush with the top of the photograph, last row
	 * flush with its bottom, so the two columns end together. */
	.vmc-grid--featured-half > .vmc-card:not(:first-child) {
		grid-column: 2;
		padding-block: var(--wp--preset--spacing--20);
		border-bottom: 1px solid var(--wp--preset--color--border);
		--vmc-ratio: 1 / 1;
		--vmc-columns: minmax( 0, 1fr );
		--vmc-align: stretch;
		--vmc-title-size: var(--wp--preset--font-size--small);
		--vmc-text-size: var(--wp--preset--font-size--small);
		--vmc-inner-gap: var(--wp--preset--spacing--20);
		--vmc-pad: 0px;
		--vmc-bg: transparent;
		--vmc-border-width: 0px;
		--vmc-border-color: transparent;
		--vmc-radius: 0px;
		--vmc-rule-width: 0px;
	}

	.vmc-grid--featured-half > .vmc-card:nth-child(2) { padding-top: 0; }

	.vmc-grid--featured-half > .vmc-card:last-child {
		padding-bottom: 0;
		border-bottom: 0;
	}

	/* 5b. Wide row ----------------------------------------------------- */

	/*
	 * BUILT in Task 018-1's third pass, to board 2 section D of
	 * docs/design/components-reference.html. Before this it was two plain
	 * columns of equal width with a 24px gutter, no ground, a 4:3 media
	 * and 143px of void above and below the text — a card that had been
	 * widened rather than a composition.
	 *
	 * What the drawing actually asks for: the whole thing sits ON a
	 * filled ground, the two halves touch, the photograph runs the full
	 * height of the row, and the words are inset and centred against it.
	 * The ground is `surface` — the theme's section band, and the same
	 * value the drawing uses; it needed no new token.
	 */
	.vmc-grid--featured-wide > .vmc-card:first-child {
		grid-column: 1 / -1;
		--vmc-columns: 1.25fr 1fr;
		--vmc-align: stretch;
		--vmc-inner-gap: 0px;
		--vmc-pad: 0px;
		--vmc-bg: var(--wp--preset--color--surface);
		--vmc-border-width: 0px;
		--vmc-border-color: transparent;
		--vmc-radius: 0px;
		--vmc-rule-width: 0px;
	}

	/*
	 * 16:9 and square-cornered. The ratio is the floor: `--vmc-align:
	 * stretch` grows the media to whatever the text side needs, and
	 * `object-fit: cover` on the image keeps the crop from distorting.
	 * No `min-height: 0` here on purpose — unlike Half and half, the
	 * ratio SHOULD hold the row open when the text is short.
	 */
	.vmc-grid--featured-wide > .vmc-card:first-child .vmc-card__media {
		--vmc-ratio: 16 / 9;
		border-radius: 0;
	}

	.vmc-grid--featured-wide > .vmc-card:first-child .vmc-card__body {
		justify-content: center;
		padding: var(--wp--preset--spacing--40);
		--vmc-title-size: var(--wp--preset--font-size--large);
		--vmc-text-size: var(--wp--preset--font-size--medium);
	}

	/*
	 * The drawing puts a bordered button here, not the quiet label, and
	 * it does so whatever the card itself is set to — the same way this
	 * layout already forces the style and the ratio. Identical
	 * declarations to `.vmc-card--action-button`, at a specificity that
	 * beats it.
	 */
	.vmc-grid--featured-wide > .vmc-card:first-child .vmc-card__action {
		display: inline-flex;
		align-items: center;
		padding: var(--wp--preset--spacing--10) var(--wp--preset--spacing--20);
		border: 1px solid currentColor;
		border-radius: 2px;
	}
}

/* --------------------------------------------------------------
   6. The one place a card is a row rather than a stack: the list beside
   a Half-and-half featured card.

   This is the whole horizontal form. Until Task 018-1 the same two
   declarations also reached every Compact card, in this file and in the
   card's own; Ana ruled that the row belongs to this layout and to no
   other, and the other two selectors were removed. A Compact card
   anywhere else is a 1:1 image above its words. DECISIONS.md 125.

   Below 601px this stands down with everything else featured, because
   the featured layouts themselves are desktop-only (§5).
   -------------------------------------------------------------- */

@media (min-width: 601px) {
	/*
	 * The thumbnail is the board's 84px (5.25rem, so it follows the text
	 * size) — a structural size, like the 16rem track minimum above.
	 * It was 42% of the row until Task 018-2, which made three 203px
	 * pictures that read as three more cards.
	 */
	.vmc-grid--featured-half > .vmc-card:not(:first-child) {
		--vmc-columns: 5.25rem minmax( 0, 1fr );
		--vmc-align: center;
	}
}
