/*
 * Image + Long Text (cis/image-long-text). Loaded with the block, front end and editor canvas.
 *
 * A tall image beside a long column of text. The block is WIDER than the article's text column:
 *
 *   inline-size: max( 100%, min( 127%, 100vw - 2 gutters - 1rem ) )
 *
 * 127% of the column (about the width in the design), but never more than the room the page leaves: the article sits in a section
 * with `padding-inline: var(--gutter)` and `overflow-x: hidden`, so the block is limited to the viewport minus a gutter on each side.
 * The 1rem is room for a classic scrollbar (100vw includes it, the section does not). On a phone, and in any window narrower than the
 * wide size, the limit works out at the column width or below, so the max() keeps it at 100% of the column and it never leaves the
 * gutters. Centred with a negative start margin of half the extra width (see the rule below).
 *
 * Mobile first: below 30rem of block width the image is on top (4:5) and the text under it. From 30rem they sit side by side, the
 * image about 53% and the text about 43% of the width, the image as tall as the text (and never shorter than about 9:8 portrait).
 * Auto side: the first block has the image on the left, the next on the right (nth-child among these blocks, no JS).
 *
 * The block is its own container (its parts query it) and has an explicit inline-size, which also avoids a height bug of size-contained
 * flex items. Plain stylesheet, not compiled by Etch, so lengths are in rem and ACSS variables directly (no to-rem()).
 *
 * Measured from the design screenshots (inferred, not given): columns 53 / 43 with a 4% gap, image 16px radius, orange bar 50px by
 * 3px, label about 11px red capitals, schedule text about 12px.
 */

.bw-image-long-text {
	/* Default (odd): image on the left. */
	--bw-cols: minmax(0, 53fr) minmax(0, 43fr);
	--bw-media-col: 1;
	--bw-text-col: 2;
	/* The width (percentages are of the article column) and the margin that centres it. A negative start margin shifts a block wider than its
	 * column left by half the extra, so it overflows both sides equally. This is used instead of align-self: center, which did not centre a
	 * wide item in the article's flex column. */
	--bw-wide: max(100%, min(127%, calc(100vw - 2 * var(--gutter) - 1rem)));
	container-type: inline-size;
	align-self: flex-start;
	inline-size: var(--bw-wide);
	margin-inline-start: calc((100% - var(--bw-wide)) / 2);
	margin-block: var(--space-l);
}

/* The layout lives on the inner element: a container cannot query itself. */
.bw-image-long-text__inner {
	display: flex;
	flex-direction: column;
	gap: var(--space-m);

	@container (min-width: 30rem) {
		display: grid;
		grid-template-columns: var(--bw-cols);
		column-gap: 4cqi;
		align-items: start;
	}
}

/* Even blocks flip. :where() keeps this rule at one class of specificity: nth-child(... of S) would otherwise add the specificity of S
 * and beat the explicit-side rules below. */
/* Even blocks flip. Counts these blocks among their siblings, so paragraphs between them are ignored. */
.bw-image-long-text:where(:nth-child(even of .bw-image-long-text)) {
	--bw-cols: minmax(0, 43fr) minmax(0, 53fr);
	--bw-media-col: 2;
	--bw-text-col: 1;
}

/* Explicit side. Two classes: same specificity as the even rule and declared after it, so they win either way. */
.bw-image-long-text.bw-image-long-text--image-left {
	--bw-cols: minmax(0, 53fr) minmax(0, 43fr);
	--bw-media-col: 1;
	--bw-text-col: 2;
}

.bw-image-long-text.bw-image-long-text--image-right {
	--bw-cols: minmax(0, 43fr) minmax(0, 53fr);
	--bw-media-col: 2;
	--bw-text-col: 1;
}

.bw-image-long-text__media {
	position: relative;
	overflow: hidden;
	margin: 0;
	border-radius: var(--radius-m);
	/* Stacked: a portrait crop. */
	aspect-ratio: 4 / 5;

	@container (min-width: 30rem) {
		grid-column: var(--bw-media-col);
		grid-row: 1;
		/* Beside the text: as tall as the text column, but never shorter than about 9:8 portrait. The image column is
		 * 53/96 of the width left after the 4cqi gap, so 9/8 of that is the minimum height. */
		aspect-ratio: auto;
		align-self: stretch;
		min-block-size: calc((100cqi - 4cqi) * 53 / 96 * 9 / 8);
	}

	/* The image fills the box, so it is taken out of flow. core/image only (articles); !important beats the aspect-ratio /
	 * object-fit style the image block can carry inline. */
	& figure {
		position: absolute;
		inset: 0;
		margin: 0;
		border-radius: inherit;
	}

	& img {
		display: block;
		inline-size: 100%;
		block-size: 100% !important;
		aspect-ratio: auto !important;
		object-fit: cover;
		border-radius: inherit;
	}

	/* A caption would have nowhere to sit inside this layout. */
	& figcaption {
		display: none;
	}
}

.bw-image-long-text__text {
	@container (min-width: 30rem) {
		grid-column: var(--bw-text-col);
		grid-row: 1;
	}

	/* The short orange bar above the first line. Decorative, so it is a pseudo-element. */
	&::before {
		content: "";
		display: block;
		inline-size: 3.125rem;
		block-size: 0.1875rem;
		margin-block-end: var(--space-s);
		background-color: var(--secondary);
	}

	/* Text size: var(--text-m) for the paragraphs, lists and label headings. */
	& :is(p, li) {
		font-size: var(--text-m);
		line-height: 1.5;
	}

	/* Spacing between the blocks, whatever the page around it does. */
	& > * {
		margin-block: 0;
	}

	& > * + * {
		margin-block-start: var(--space-xs);
	}

	/* The page's body text is weight 300, so <strong> only reaches 400 and Georgia has no face between 400 and 700. Ask for the real bold. */
	& :is(strong, b) {
		font-weight: 700;
	}

	/* Headings are labels. The site lowercases every heading with a rule of two classes' specificity (bmda-components.css),
	 * so the uppercase needs !important to win. h2: the red label ("Friday, October 31"); h3 and below: the same, in the body colour. */
	& :is(h2, h3, h4, h5, h6) {
		font-family: "Bricolage Grotesque", sans-serif;
		font-size: var(--text-m);
		font-weight: 700;
		line-height: 1.3;
		letter-spacing: 0.1em;
		text-transform: uppercase !important;
		color: var(--base);
	}

	& h2 {
		color: var(--primary);
	}

	& > :is(h2, h3, h4, h5, h6):not(:first-child) {
		margin-block-start: var(--space-m);
	}

	& > :is(h2, h3, h4, h5, h6) + * {
		margin-block-start: var(--space-s);
	}

	/* Editor only: keep the add button below the last block instead of floating over it. */
	& > .block-list-appender {
		position: static;
		inline-size: auto;
		block-size: auto;
		align-self: flex-start;
	}
}

/* No image: the text is the whole block. */
.bw-image-long-text.bw-image-long-text--no-image .bw-image-long-text__inner {
	display: block;

	& > .bw-image-long-text__text {
		grid-column: auto;
	}
}

/* Editor canvas: the editor column has no gutter section around it, so the block stays at the column width there. */
.block-editor-block-list__block.bw-image-long-text {
	inline-size: 100%;
	margin-inline-start: 0;
}
