/*
 * CRC Instagram — front end.
 *
 * Two things this stylesheet is careful about.
 *
 * 1. It assumes the theme is hostile. Divi, and most themes, set list styles,
 *    image rules, button padding and box-sizing globally. Every element this
 *    plugin emits therefore states its own values rather than inheriting and
 *    hoping. The wrapper class is on every selector so nothing leaks out.
 *
 * 2. Not one layout rule is keyed to the viewport. A feed sitting in a 600px
 *    Divi column on a 1600px monitor has a wide window and a narrow box, and a
 *    viewport media query reads that exactly backwards. Column counts come from
 *    auto-fill measured against the container; the one place a real breakpoint
 *    is needed uses a container query.
 *
 * Colours are custom properties at the top so the palette can be matched to the
 * rest of the site in a handful of lines.
 */

.crc-ig {
	/* Palette. */
	--crc-ig-fg: #1f2328;
	--crc-ig-muted: #57606a;
	--crc-ig-line: #d8dee4;
	--crc-ig-bg: transparent;
	--crc-ig-tile-bg: #eef1f4;
	--crc-ig-accent: #1d4ed8;
	--crc-ig-accent-fg: #ffffff;
	--crc-ig-overlay: rgba(0, 0, 0, 0.55);
	--crc-ig-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);

	/* Geometry. Overridden inline from the settings. */
	--crc-ig-cols: 3;
	--crc-ig-gap: 10px;
	--crc-ig-min: 190px;
	--crc-ig-radius: 6px;

	box-sizing: border-box;
	container-type: inline-size;
	container-name: crcig;
	color: var(--crc-ig-fg);
	background: var(--crc-ig-bg);
	max-width: 100%;
}

.crc-ig *,
.crc-ig *::before,
.crc-ig *::after {
	box-sizing: border-box;
}

/* ---------------------------------------------------------------- heading */

/*
 * Deliberately empty of typography. The theme dresses this h2 exactly as it
 * dresses every other h2 on the page, so it matches automatically instead of
 * being matched by hand and drifting later. Only the gap below is set — room
 * above a heading belongs to the theme too.
 */
.crc-ig .crc-ig-heading {
	margin-bottom: 0.6em;
}

/* ----------------------------------------------------------------- header */

.crc-ig .crc-ig-header {
	display: flex;
	align-items: center;
	gap: 12px;
	margin: 0 0 calc(var(--crc-ig-gap) + 4px);
	padding: 0 0 12px;
	border-bottom: 1px solid var(--crc-ig-line);
	flex-wrap: wrap;
}

.crc-ig .crc-ig-avatar {
	display: block;
	flex: 0 0 auto;
	width: 56px;
	height: 56px;
	border-radius: 50%;
	overflow: hidden;
	background: var(--crc-ig-tile-bg);
	box-shadow: none;
	border: 0;
	text-decoration: none;
}

.crc-ig .crc-ig-avatar img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
	border-radius: 50%;
	margin: 0;
	padding: 0;
	max-width: none;
	box-shadow: none;
	border: 0;
}

.crc-ig .crc-ig-ident {
	display: flex;
	flex-direction: column;
	gap: 2px;
	min-width: 0;
	margin-right: auto;
}

.crc-ig .crc-ig-user {
	font-weight: 700;
	text-decoration: none;
	color: inherit;
	overflow-wrap: anywhere;
}

.crc-ig .crc-ig-user:hover,
.crc-ig .crc-ig-user:focus-visible {
	text-decoration: underline;
}

.crc-ig .crc-ig-meta {
	font-size: 0.85em;
	color: var(--crc-ig-muted);
}

.crc-ig .crc-ig-follow {
	display: inline-block;
	flex: 0 0 auto;
	padding: 8px 16px;
	border-radius: 999px;
	background: var(--crc-ig-accent);
	color: var(--crc-ig-accent-fg);
	font-size: 0.9em;
	font-weight: 600;
	line-height: 1.3;
	text-decoration: none;
	border: 0;
	box-shadow: var(--crc-ig-shadow);
}

.crc-ig .crc-ig-follow:hover,
.crc-ig .crc-ig-follow:focus-visible {
	filter: brightness(0.92);
	color: var(--crc-ig-accent-fg);
	text-decoration: none;
}

/* ------------------------------------------------------------------- grid */

.crc-ig .crc-ig-grid {
	display: grid;
	gap: var(--crc-ig-gap);
	margin: 0;
	padding: 0;
	list-style: none;

	/*
	 * The column count without a media query.
	 *
	 * auto-fill fits as many tracks as the CONTAINER has room for. The track
	 * minimum is the larger of a readable floor and one Nth of the container,
	 * so --crc-ig-cols behaves as a ceiling: a wide box lands on exactly N, a
	 * narrow one drops to fewer by itself, and a very narrow one lands on 1.
	 * min(100%, …) stops the floor from forcing overflow in a box narrower
	 * than a single tile.
	 */
	grid-template-columns: repeat(
		auto-fill,
		minmax(
			min(
				100%,
				max(
					var(--crc-ig-min),
					calc((100% - (var(--crc-ig-cols) - 1) * var(--crc-ig-gap)) / var(--crc-ig-cols))
				)
			),
			1fr
		)
	);
}

.crc-ig .crc-ig-grid > .crc-ig-item {
	margin: 0;
	padding: 0;
	list-style: none;
	min-width: 0;
}

.crc-ig .crc-ig-grid > .crc-ig-item::before,
.crc-ig .crc-ig-grid > .crc-ig-item::marker {
	content: none;
}

.crc-ig .crc-ig-item[hidden] {
	display: none;
}

/* ------------------------------------------------------------------- tile */

.crc-ig .crc-ig-tile {
	position: relative;
	display: block;
	overflow: hidden;
	border-radius: var(--crc-ig-radius);
	background: var(--crc-ig-tile-bg);
	aspect-ratio: 1 / 1;
	text-decoration: none;
	border: 0;
	box-shadow: none;
	line-height: 0;
}

.crc-ig .crc-ig-img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
	margin: 0;
	padding: 0;
	max-width: none;
	border: 0;
	border-radius: 0;
	box-shadow: none;
	transition: transform 0.25s ease;
}

.crc-ig .crc-ig-tile:hover .crc-ig-img,
.crc-ig .crc-ig-tile:focus-visible .crc-ig-img {
	transform: scale(1.04);
}

.crc-ig .crc-ig-tile:focus-visible {
	outline: 3px solid var(--crc-ig-accent);
	outline-offset: 2px;
}

/* Badge for videos, reels and albums. */
.crc-ig .crc-ig-badge {
	position: absolute;
	top: 8px;
	right: 8px;
	display: flex;
	align-items: center;
	justify-content: center;
	width: 26px;
	height: 26px;
	border-radius: 6px;
	background: rgba(0, 0, 0, 0.45);
	color: #fff;
	line-height: 0;
	pointer-events: none;
}

.crc-ig .crc-ig-badge .crc-ig-icon {
	width: 15px;
	height: 15px;
}

/* Like and comment counts on hover. */
.crc-ig .crc-ig-hover {
	position: absolute;
	inset: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 18px;
	background: var(--crc-ig-overlay);
	color: #fff;
	font-size: 0.95em;
	font-weight: 600;
	line-height: 1;
	opacity: 0;
	transition: opacity 0.2s ease;
	pointer-events: none;
}

.crc-ig .crc-ig-tile:hover .crc-ig-hover,
.crc-ig .crc-ig-tile:focus-visible .crc-ig-hover {
	opacity: 1;
}

.crc-ig .crc-ig-stat {
	display: inline-flex;
	align-items: center;
	gap: 6px;
}

.crc-ig .crc-ig-stat .crc-ig-icon {
	width: 16px;
	height: 16px;
}

/* Touch devices have no hover; showing it permanently would bury the photo. */
@media (hover: none) {
	.crc-ig .crc-ig-hover {
		display: none;
	}
}

@media (prefers-reduced-motion: reduce) {
	.crc-ig .crc-ig-img,
	.crc-ig .crc-ig-hover {
		transition: none;
	}
	.crc-ig .crc-ig-tile:hover .crc-ig-img,
	.crc-ig .crc-ig-tile:focus-visible .crc-ig-img {
		transform: none;
	}
}

/* ---------------------------------------------------------------- caption */

.crc-ig .crc-ig-caption {
	margin: 8px 0 0;
	font-size: 0.88em;
	line-height: 1.45;
	color: var(--crc-ig-muted);
	display: -webkit-box;
	-webkit-line-clamp: 3;
	line-clamp: 3;
	-webkit-box-orient: vertical;
	overflow: hidden;
	overflow-wrap: anywhere;
}

/* Visible to screen readers, not on screen. */
.crc-ig .crc-ig-sr,
.crc-ig .crc-ig-live:empty {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0 0 0 0);
	clip-path: inset(50%);
	white-space: nowrap;
	border: 0;
}

.crc-ig .crc-ig-live {
	margin: 10px 0 0;
	font-size: 0.85em;
	color: var(--crc-ig-muted);
	text-align: center;
}

/* --------------------------------------------------------------- masonry */

/*
 * Tiles keep their own proportions instead of being cropped square.
 *
 * Without JavaScript this is still a perfectly good grid — every tile shows at
 * its true aspect ratio, rows are as tall as their tallest tile. The script
 * then assigns row spans so the tiles pack tightly, and adds .is-packed. The
 * layout is never broken while waiting for that, and never depends on it.
 */
.crc-ig--masonry .crc-ig-tile {
	aspect-ratio: var(--crc-ig-ar, 1);
}

/*
 * align-items: start belongs to the whole masonry layout, not just the packed
 * state, and it is load-bearing twice over.
 *
 * A grid item stretches to its row height by default. That means an unpacked
 * tile reports its ROW's height rather than its own — so the script, which
 * measures with packing switched off, read the same height for every tile in a
 * row and handed them all the same span. The packing silently collapsed back
 * into a plain row grid, which is exactly what it was there to avoid.
 *
 * It is also simply correct for the no-JavaScript fallback: tiles should sit at
 * their own height rather than being padded out to match their tallest
 * neighbour.
 */
.crc-ig--masonry .crc-ig-grid {
	align-items: start;
}

.crc-ig--masonry.is-packed .crc-ig-grid {
	grid-auto-rows: 2px;

	/*
	 * dense is doing the actual packing, and it is not optional.
	 *
	 * Default (sparse) auto-placement walks a cursor that may never move
	 * backwards. Once a tall tile has been placed, every later tile is pushed
	 * past it even when there is obvious room beside it, and the result is a
	 * plain row grid with ragged holes — which is what this layout looked like
	 * before the browser harness was pointed at it.
	 *
	 * dense restarts the search for each tile, so every tile lands in the
	 * shortest column that will take it. For single-column tiles like these
	 * that is exactly masonry, and it keeps reading order.
	 */
	grid-auto-flow: row dense;
}

.crc-ig--masonry.is-packed .crc-ig-item {
	/* Set inline by the script. */
	grid-row-end: span var(--crc-ig-span, 100);
}

/* ------------------------------------------------------------- highlight */

/*
 * Every Nth tile is twice the size. dense packing lets the smaller tiles fill
 * the hole a feature tile leaves behind, so there are no gaps.
 *
 * This layout needs a known column count — auto-fill and a 2-column span do not
 * co-operate — so it declares one and steps down with a CONTAINER query, not a
 * viewport one.
 */
.crc-ig--highlight .crc-ig-grid {
	grid-template-columns: repeat(var(--crc-ig-cols), minmax(0, 1fr));
	grid-auto-flow: dense;
}

.crc-ig--highlight .crc-ig-item--feature {
	grid-column: span 2;
	grid-row: span 2;
}

@container crcig (max-width: 700px) {
	.crc-ig--highlight .crc-ig-grid {
		grid-template-columns: repeat(min(var(--crc-ig-cols), 3), minmax(0, 1fr));
	}
}

@container crcig (max-width: 480px) {
	.crc-ig--highlight .crc-ig-grid {
		grid-template-columns: repeat(2, minmax(0, 1fr));
	}
	/* Below this a double-width tile is the whole row, which is not a highlight. */
	.crc-ig--highlight .crc-ig-item--feature {
		grid-column: span 2;
		grid-row: span 2;
	}
}

@container crcig (max-width: 320px) {
	.crc-ig--highlight .crc-ig-grid {
		grid-template-columns: minmax(0, 1fr);
	}
	.crc-ig--highlight .crc-ig-item--feature {
		grid-column: span 1;
		grid-row: span 1;
	}
}

/*
 * Browsers without container query support fall back to the plain grid rather
 * than a broken one.
 */
@supports not (container-type: inline-size) {
	.crc-ig--highlight .crc-ig-grid {
		grid-template-columns: repeat(
			auto-fill,
			minmax(min(100%, max(var(--crc-ig-min), calc((100% - (var(--crc-ig-cols) - 1) * var(--crc-ig-gap)) / var(--crc-ig-cols)))), 1fr)
		);
	}
	.crc-ig--highlight .crc-ig-item--feature {
		grid-column: auto;
		grid-row: auto;
	}
}

/* --------------------------------------------------------------- actions */

.crc-ig .crc-ig-actions {
	display: flex;
	justify-content: center;
	margin-top: calc(var(--crc-ig-gap) + 6px);
}

.crc-ig .crc-ig-more {
	appearance: none;
	-webkit-appearance: none;
	display: inline-block;
	padding: 10px 22px;
	border: 1px solid var(--crc-ig-line);
	border-radius: 999px;
	background: transparent;
	color: inherit;
	font: inherit;
	font-size: 0.92em;
	font-weight: 600;
	line-height: 1.3;
	cursor: pointer;
	text-transform: none;
	letter-spacing: normal;
	box-shadow: none;
	width: auto;
	height: auto;
	min-height: 0;
}

.crc-ig .crc-ig-more:hover {
	border-color: var(--crc-ig-accent);
	color: var(--crc-ig-accent);
	background: transparent;
}

.crc-ig .crc-ig-more:focus-visible {
	outline: 3px solid var(--crc-ig-accent);
	outline-offset: 2px;
}

.crc-ig .crc-ig-more[hidden] {
	display: none;
}

/* --------------------------------------------------------------- notice */

.crc-ig-notice {
	padding: 14px 16px;
	border: 1px solid #f0c36d;
	border-left-width: 4px;
	border-radius: 4px;
	background: #fff8e5;
	color: #4a3b12;
	font-size: 0.95em;
}

.crc-ig-notice p {
	margin: 0;
}

.crc-ig-notice .crc-ig-notice-sub {
	margin-top: 6px;
	font-size: 0.88em;
	opacity: 0.8;
}

/* ------------------------------------------------------------- lightbox */

.crc-ig-lb {
	position: fixed;
	inset: 0;
	z-index: 999999;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 24px;
	background: rgba(0, 0, 0, 0.86);
	box-sizing: border-box;
}

.crc-ig-lb *,
.crc-ig-lb *::before,
.crc-ig-lb *::after {
	box-sizing: border-box;
}

.crc-ig-lb[hidden] {
	display: none;
}

.crc-ig-lb-panel {
	position: relative;
	display: flex;
	flex-direction: row;
	gap: 0;
	max-width: 1100px;
	max-height: 100%;
	width: 100%;
	background: #fff;
	color: #1f2328;
	border-radius: 8px;
	overflow: hidden;
}

.crc-ig-lb-stage {
	position: relative;
	flex: 1 1 auto;
	min-width: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	background: #000;
	max-height: 86vh;
}

.crc-ig-lb-media {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 100%;
	max-height: 86vh;
}

.crc-ig-lb-stage img,
.crc-ig-lb-stage video {
	display: block;
	max-width: 100%;
	max-height: 86vh;
	width: auto;
	height: auto;
	margin: 0;
	object-fit: contain;
	background: #000;
}

.crc-ig-lb-side {
	flex: 0 0 320px;
	max-width: 320px;
	padding: 20px;
	overflow-y: auto;
	max-height: 86vh;
	font-size: 0.92em;
	line-height: 1.5;
}

/* Keep the first line clear of the close button sitting in that corner. */
.crc-ig-lb-side > :first-child {
	padding-right: 44px;
}

.crc-ig-lb-user {
	font-weight: 700;
	margin: 0 0 4px;
}

.crc-ig-lb-date {
	margin: 0 0 14px;
	color: #57606a;
	font-size: 0.9em;
}

.crc-ig-lb-caption {
	margin: 0 0 16px;
	white-space: pre-wrap;
	overflow-wrap: anywhere;
}

.crc-ig-lb-stats {
	display: flex;
	gap: 18px;
	margin: 0 0 16px;
	color: #57606a;
	font-weight: 600;
}

.crc-ig-lb-stats span {
	display: inline-flex;
	align-items: center;
	gap: 6px;
}

.crc-ig-lb-link {
	display: inline-block;
	padding: 8px 16px;
	border-radius: 999px;
	background: #1d4ed8;
	color: #fff;
	text-decoration: none;
	font-weight: 600;
	font-size: 0.92em;
}

.crc-ig-lb-link:hover,
.crc-ig-lb-link:focus-visible {
	color: #fff;
	filter: brightness(0.92);
}

.crc-ig-lb-btn {
	position: absolute;
	appearance: none;
	-webkit-appearance: none;
	display: flex;
	align-items: center;
	justify-content: center;
	width: 44px;
	height: 44px;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: rgba(0, 0, 0, 0.55);
	color: #fff;
	cursor: pointer;
	line-height: 0;
	z-index: 2;

	/*
	 * A ring, because these buttons sit on top of whatever the photo happens to
	 * be. A dark disc disappears against a dark image and a light one
	 * disappears against a light image; a dark disc with a light edge is
	 * legible on both.
	 */
	box-shadow: 0 0 0 1.5px rgba(255, 255, 255, 0.55);
}

.crc-ig-lb-btn:hover {
	background: rgba(0, 0, 0, 0.8);
	box-shadow: 0 0 0 1.5px rgba(255, 255, 255, 0.9);
}

.crc-ig-lb-btn:focus-visible {
	outline: 3px solid #fff;
	outline-offset: 2px;
}

.crc-ig-lb-close {
	top: 10px;
	right: 10px;
}

.crc-ig-lb-prev {
	left: 10px;
	top: 50%;
	transform: translateY(-50%);
}

.crc-ig-lb-next {
	right: 10px;
	top: 50%;
	transform: translateY(-50%);
}

.crc-ig-lb-btn[hidden] {
	display: none;
}

.crc-ig-lb-dots {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 10px;
	display: flex;
	justify-content: center;
	gap: 6px;
	z-index: 2;
}

.crc-ig-lb-dot {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: rgba(255, 255, 255, 0.45);
}

.crc-ig-lb-dot.is-on {
	background: #fff;
}

.crc-ig-lb-fallback {
	padding: 24px;
	color: #fff;
	text-align: center;
	max-width: 32em;
}

/*
 * The panel goes vertical in a narrow WINDOW, which is the correct trigger
 * here — the lightbox is fixed to the viewport, so the viewport really is its
 * container.
 */
@media (max-width: 860px) {
	.crc-ig-lb {
		padding: 0;
	}
	.crc-ig-lb-panel {
		flex-direction: column;
		max-height: 100%;
		border-radius: 0;
		height: 100%;
	}
	.crc-ig-lb-stage {
		max-height: 60vh;
	}
	.crc-ig-lb-stage img,
	.crc-ig-lb-stage video {
		max-height: 60vh;
	}
	.crc-ig-lb-side {
		flex: 1 1 auto;
		max-width: none;
		max-height: none;
	}
}

html.crc-ig-lb-open,
body.crc-ig-lb-open {
	overflow: hidden;
}
