/* Story header: date + title */

.story-header {
	max-width: 880px;
	margin: 0 auto;
	box-sizing: border-box;
	padding: 56px 20px 48px;
	text-align: center;
	display: flex;
	flex-direction: column;
	gap: 16px;
	align-items: center;
}

@media ( min-width: 640px ) {
	.story-header {
		padding: 56px 40px 48px;
	}
}

.story-header__date {
	font-size: 13px;
	color: var( --wp--preset--color--accent );
	letter-spacing: 0.04em;
}

.story-header__title {
	margin: 0;
	font-family: var( --wp--preset--font-family--serif );
	font-weight: 300;
	font-size: 34px;
	line-height: 1.1;
	letter-spacing: -0.01em;
	text-wrap: balance;
}

@media ( min-width: 640px ) {
	.story-header__title {
		font-size: 54px;
	}
}

/* Cover image */

.cover-image {
	max-width: 1360px;
	margin: 0 auto;
	box-sizing: border-box;
	padding: 0 20px;
	min-height: 200px;
	background: var( --wp--preset--color--surface );
	/* Paint the no-image fallback only inside the gutters, not under them. */
	background-clip: content-box;
}

@media ( min-width: 640px ) {
	.cover-image {
		padding: 0 40px;
	}
}

.cover-image > .cover-image__img {
	width: 100%;
	height: auto;
	max-height: 88vh;
	object-fit: cover;
	display: block;
	background: var( --wp--preset--color--surface );
}

/* Story content: blocks */

.story-content {
	display: flex;
	flex-direction: column;
	gap: 40px;
	padding: 72px 0 0;
	/* One column for text and images alike, as in the block editor.
	   Client request; the design had 660px text and 1160px photos. */
	--lop-story-width: 860px;
}

/* Blocks centre themselves with margin: auto inside max-width, and auto
   margins switch off the flex stretch, so without an explicit width each
   block shrank to its content: a short paragraph sat in the middle of
   the page, and in Firefox/Safari a gallery of not-yet-loaded lazy images
   was 0px wide - its images never entered the viewport and never loaded. */
.story-content > * {
	width: 100%;
	box-sizing: border-box;
}

.story-content .wp-block-paragraph {
	max-width: var( --lop-story-width );
	margin: 0 auto;
	box-sizing: border-box;
	padding: 0 20px;
	font-family: var( --wp--preset--font-family--serif );
	font-size: 20px;
	line-height: 1.65;
	color: #d6d5d0;
	text-wrap: pretty;
}

@media ( min-width: 640px ) {
	.story-content .wp-block-paragraph {
		padding: 0 40px;
	}
}

.story-content .wp-block-paragraph.is-style-note {
	font-family: ui-monospace, Menlo, monospace;
	font-size: 13px;
	line-height: 1.6;
	color: #6d6c68;
}

.story-content :is( h2, h3, h4, h5, h6 ) {
	max-width: var( --lop-story-width );
	margin: 24px auto -24px;
	box-sizing: border-box;
	padding: 0 20px;
	font-size: 15px;
	font-weight: 500;
	letter-spacing: 0.02em;
	color: var( --wp--preset--color--foreground );
}

@media ( min-width: 640px ) {
	.story-content :is( h2, h3, h4, h5, h6 ) {
		padding: 0 40px;
	}
}

/* Single image block: orientation read from the file, see inc/images.php */

.story-content > .wp-block-image {
	max-width: var( --lop-story-width );
	margin: 16px auto;
	box-sizing: border-box;
	padding: 0 20px;
}

@media ( min-width: 640px ) {
	.story-content > .wp-block-image {
		padding: 0 40px;
	}
}

/* Full width, unless that would make the image taller than 90% of the
   screen (square-ish photos on a wide, short window): then it narrows
   to fit and centres. --lop-ratio is width / height, from inc/images.php. */
.story-content > .wp-block-image.is-landscape img {
	width: 100%;
	max-width: calc( 90vh * var( --lop-ratio, 10 ) );
	margin-inline: auto;
	height: auto;
	display: block;
	background: var( --wp--preset--color--surface );
}

.story-content > .wp-block-image.is-portrait {
	display: flex;
	justify-content: center;
}

.story-content > .wp-block-image.is-portrait img {
	width: auto;
	height: auto;
	max-width: 100%;
	max-height: 90vh;
	display: block;
	background: var( --wp--preset--color--surface );
}

/* Gallery block: 3+ images get the lightbox tile rows, exactly 2 images
   get the "para zdjęć" side-by-side treatment at full aspect ratio, and
   a single image is shown whole. */

.story-content .wp-block-gallery {
	margin: 16px auto !important;
	box-sizing: border-box;
	padding: 0 20px;
	max-width: var( --lop-story-width );
	display: grid !important;
	grid-template-columns: repeat( 2, minmax( 0, 1fr ) ) !important;
	gap: 12px !important;
}

@media ( min-width: 640px ) {
	.story-content .wp-block-gallery {
		padding: 0 40px;
		grid-template-columns: repeat( 3, minmax( 0, 1fr ) ) !important;
	}
}

.story-content .wp-block-gallery .wp-block-image {
	margin: 0 !important;
	padding: 0;
	max-width: none !important;
	width: 100% !important;
	display: block !important;
}

.story-content .wp-block-gallery .wp-block-image img {
	width: 100%;
	height: auto;
	display: block;
	background: var( --wp--preset--color--surface );
}

/* 3+ images: tiles with the lightbox zoom affordance, in rows of 2 (phone)
   or 3 (from 640px). Full rows are squares; a short last row keeps the
   same height and its images grow to fill the width, as in the block
   editor, instead of leaving an empty cell (client request). The tile
   height comes from the gallery's own width (container query units).
   Pairs and single images are left out rather than reset later:
   resetting the size would also drop the ratio the browser takes from
   the width/height attributes (see the pair rule below). */
.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 3 ) ) {
	--lop-cols: 2;
	container-type: inline-size;
	display: flex !important;
	flex-wrap: wrap !important;
}

@media ( min-width: 640px ) {
	.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 3 ) ) {
		--lop-cols: 3;
	}
}

/* The 1px keeps rounding from pushing the last tile of a row onto the next. */
.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 3 ) ) > .wp-block-image {
	flex: 1 1 calc( ( 100% - ( var( --lop-cols ) - 1 ) * 12px ) / var( --lop-cols ) - 1px ) !important;
	width: auto !important;
	min-width: 0;
}

.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 3 ) ) .wp-block-image img {
	width: 100% !important;
	height: calc( ( 100cqw - ( var( --lop-cols ) - 1 ) * 12px ) / var( --lop-cols ) ) !important;
	object-fit: cover !important;
	cursor: zoom-in;
	transition: opacity 0.6s ease, transform 1.4s cubic-bezier( 0.2, 0.7, 0.2, 1 );
}

.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 3 ) ) .wp-block-image img:hover {
	opacity: 0.9;
	transform: scale( 1.025 );
}

/* From 640px a lone image in the last row (4, 7, 10... images) would be
   stretched into a 3:1 strip, so the last four images go two per row. */
@media ( min-width: 640px ) {
	.story-content .wp-block-gallery:has( > .wp-block-image:first-child:nth-last-child( 3n + 4 ) ) > .wp-block-image:nth-last-child( -n + 4 ) {
		flex-basis: calc( ( 100% - 12px ) / 2 - 1px ) !important;
	}
}

/* A gallery with a single image shows it whole, like an image block:
   full column width, but never taller than 90% of the screen. */
.story-content .wp-block-gallery:has( > .wp-block-image:only-child ) {
	grid-template-columns: minmax( 0, 1fr ) !important;
}

.story-content .wp-block-gallery:has( > .wp-block-image:only-child ) > .wp-block-image {
	width: min( 100%, calc( 90vh * var( --lop-ratio, 10 ) ) ) !important;
	margin: 0 auto !important;
}

.story-content .wp-block-gallery:has( > .wp-block-image:only-child ) .wp-block-image img {
	height: auto !important;
	flex: none !important;
	object-fit: initial !important;
}

/* Exactly two images: "para zdjęć" - natural ratio, no zoom affordance */

.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 2 ):last-child ) {
	grid-template-columns: minmax( 0, 1fr ) !important;
	gap: 16px !important;
}

/* From 640px the two images share one row at equal height: each figure
   grows in proportion to its width/height ratio (--lop-ratio, set in
   inc/images.php), so a landscape + portrait pair fills the row without
   cropping and without the hole the design's two equal columns leave
   under the shorter image. Agreed deviation from the design. */
@media ( min-width: 640px ) {
	.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 2 ):last-child ) {
		display: flex !important;
		flex-wrap: nowrap !important;
		align-items: flex-start;
	}

	.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 2 ):last-child ) > .wp-block-image {
		flex: var( --lop-ratio, 1 ) 1 0% !important;
		width: auto !important;
		min-width: 0;
	}
}

/* Pair images keep their own ratio from the width/height attributes.
   Core's cropped-gallery rule sets height: 100% through an #id selector
   (hence !important), and lazy images with sizes="auto" get size
   containment with core's 3000x1500 placeholder, so without that
   attribute ratio a pair image renders 1500px tall even once loaded. */
.story-content .wp-block-gallery:has( > .wp-block-image:nth-child( 2 ):last-child ) .wp-block-image img {
	height: auto !important;
	flex: none !important;
	object-fit: initial !important;
}

/* Prev / next story navigation */

.story-nav {
	max-width: 1160px;
	margin: 120px auto 0;
	box-sizing: border-box;
	padding: 0 20px;
}

@media ( min-width: 640px ) {
	.story-nav {
		padding: 0 40px;
	}
}

.story-nav__grid {
	border-top: 1px solid var( --wp--preset--color--border );
	padding-top: 40px;
	display: grid;
	grid-template-columns: minmax( 0, 1fr );
	gap: 32px;
}

@media ( min-width: 640px ) {
	.story-nav__grid {
		grid-template-columns: repeat( 2, minmax( 0, 1fr ) );
	}
}

.story-nav__next {
	display: flex;
	flex-direction: column;
	align-items: flex-end;
}

.story-nav__link {
	display: flex;
	gap: 20px;
	align-items: center;
	text-decoration: none;
	transition: opacity 0.25s ease;
}

.story-nav__link:hover {
	opacity: 0.85;
}

.story-nav__next .story-nav__link {
	flex-direction: row-reverse;
	text-align: right;
}

.story-nav__link .cover-image__img {
	width: 120px;
	height: auto;
	aspect-ratio: 4 / 3;
	object-fit: cover;
	display: block;
	flex: none;
	background: var( --wp--preset--color--surface );
}

.story-nav__text {
	display: flex;
	flex-direction: column;
	gap: 6px;
}

.story-nav__label {
	font-size: 12px;
	letter-spacing: 0.12em;
	text-transform: uppercase;
	color: #8d8c87;
}

.story-nav__title {
	font-size: 16px;
	line-height: 1.35;
	color: var( --wp--preset--color--foreground );
}

.story-nav__all {
	text-align: center;
	margin-top: 56px;
	font-size: 14px;
}

.story-nav__all a {
	color: #8d8c87;
	transition: color 0.25s ease;
}

.story-nav__all a:hover {
	color: #ffffff;
}
