/*
 * Balakhah Latest Posts — self-contained, theme-neutral card grid.
 *
 * The plugin renders inside unknown themes, most of which ship a global
 * `img { width: 100%; height: auto }` rule. That rule outranks anything we set
 * without !important and breaks object-fit (media letterboxes, avatars blow up
 * to full width), so every image here is absolutely positioned inside a fixed
 * ratio frame and its box metrics are forced.
 */

.blkh-grid {
	--blkh-cols: 3;
	--blkh-gap: 18px;
	--blkh-min: 210px; /* smallest sensible card; only used when collapsing */
	--blkh-radius: 16px;
	--blkh-ratio: 4 / 3;

	/* Adaptive by default: a neutral grey wash reads as a card over both light
	   and dark theme backgrounds, and the text simply inherits the theme's own
	   body colour, so contrast is whatever the theme already guarantees. Sites
	   that want a fixed look add .blkh-grid--light / --dark (theme="light|dark").
	   Deliberately NOT keyed to prefers-color-scheme: the visitor's OS says
	   nothing about whether the page around this block is light or dark. */
	--blkh-surface: rgba(128, 128, 128, 0.09);
	--blkh-surface-alt: rgba(128, 128, 128, 0.16);
	--blkh-text: inherit;
	--blkh-muted: inherit;
	--blkh-muted-opacity: 0.68;
	--blkh-border: rgba(128, 128, 128, 0.3);
	--blkh-shadow: none;
	--blkh-shadow-hover: 0 2px 4px rgba(0, 0, 0, 0.08), 0 18px 42px rgba(0, 0, 0, 0.16);
	--blkh-brand: #c81e4f;
	--blkh-brand-2: #6d2b5e;
	--blkh-scrim: rgba(10, 10, 14, 0.55);

	display: grid;
	/* A grid only fills its parent in normal flow. Dropped into a shrink-to-fit
	   context — a Row/Group block (`is-layout-flex`), a float, an inline-grid —
	   it sizes to its content instead, so a couple of short posts render as a
	   narrow strip. width:100% pins it to the container in every context. */
	width: 100%;
	max-width: 100%;
	box-sizing: border-box;
	gap: var(--blkh-gap);
	/* The requested column count, exactly — a "3 columns" setting must give 3
	   columns in a normal ~700px content area, so no minimum track width is
	   applied here. Narrow viewports collapse it in the queries below. */
	grid-template-columns: repeat(var(--blkh-cols), minmax(0, 1fr));
	margin: 0 0 1.5em;
	padding: 0;
	list-style: none;
}

/*
 * Narrow viewports only. --blkh-min acts as a floor here, so a 3-column grid
 * drops to 2 (then 1) as space runs out, while 1- and 2-column grids are
 * untouched — their natural track is already wider than the floor.
 */
@media (max-width: 900px) {
	.blkh-grid {
		grid-template-columns: repeat(
			auto-fill,
			minmax(
				min(100%, max(var(--blkh-min), calc((100% - (var(--blkh-cols) - 1) * var(--blkh-gap)) / var(--blkh-cols)))),
				1fr
			)
		);
	}
}

@media (max-width: 480px) {
	.blkh-grid {
		grid-template-columns: minmax(0, 1fr);
	}
}

/* Explicit schemes (block/shortcode theme="light|dark"). */
.blkh-grid--light {
	--blkh-surface: #ffffff;
	--blkh-surface-alt: #f1f1f5;
	--blkh-text: #15151c;
	--blkh-muted: #5c5c6b;
	--blkh-muted-opacity: 1;
	--blkh-border: rgba(16, 16, 24, 0.12);
	--blkh-shadow: 0 1px 2px rgba(16, 16, 24, 0.06), 0 12px 28px rgba(16, 16, 24, 0.08);
	--blkh-shadow-hover: 0 2px 4px rgba(16, 16, 24, 0.08), 0 18px 42px rgba(16, 16, 24, 0.16);
	color-scheme: light;
}

.blkh-grid--dark {
	--blkh-surface: #17171e;
	--blkh-surface-alt: #262630;
	--blkh-text: #f2f2f6;
	--blkh-muted: #a7a7b8;
	--blkh-muted-opacity: 1;
	--blkh-border: rgba(255, 255, 255, 0.12);
	--blkh-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 28px rgba(0, 0, 0, 0.35);
	--blkh-shadow-hover: 0 2px 4px rgba(0, 0, 0, 0.5), 0 18px 42px rgba(0, 0, 0, 0.5);
	--blkh-brand: #ef4a76;
	--blkh-brand-2: #a05a92;
	color-scheme: dark;
}

.blkh-grid *,
.blkh-grid *::before,
.blkh-grid *::after {
	box-sizing: border-box;
}

/* ---------------------------------------------------------------- card ---- */

/* `.blkh-grid a.blkh-card` outranks the theme's `.entry-content a` colour and
   underline, which a bare `.blkh-card` would lose to on specificity. */
.blkh-grid a.blkh-card {
	position: relative;
	display: flex;
	flex-direction: column;
	overflow: hidden;
	border-radius: var(--blkh-radius);
	border: 1px solid var(--blkh-border);
	background: var(--blkh-surface);
	color: var(--blkh-text) !important;
	text-decoration: none !important;
	box-shadow: var(--blkh-shadow);
	transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
}

.blkh-grid a.blkh-card:hover,
.blkh-grid a.blkh-card:focus-visible {
	transform: translateY(-3px);
	box-shadow: var(--blkh-shadow-hover);
	border-color: color-mix(in srgb, var(--blkh-brand) 45%, var(--blkh-border));
	text-decoration: none !important;
}

.blkh-card:focus-visible {
	outline: 2px solid var(--blkh-brand);
	outline-offset: 2px;
}

/* --------------------------------------------------------------- media ---- */

.blkh-card__media {
	position: relative;
	display: block;
	width: 100%;
	aspect-ratio: var(--blkh-ratio);
	overflow: hidden;
	background: linear-gradient(135deg, var(--blkh-surface-alt), var(--blkh-surface));
}

/* Ratio fallback for engines without aspect-ratio (matches 4 / 3). */
@supports not (aspect-ratio: 4 / 3) {
	.blkh-card__media::before {
		content: "";
		display: block;
		padding-top: 75%;
	}
}

/* Forced metrics: this is what defeats the theme's `height: auto` and makes
   object-fit actually crop wide video stills instead of letterboxing them.
   The long selector is deliberate — themes ship `.entry-content img { height:
   auto !important }`, which ties on specificity and would win on source order. */
.blkh-grid .blkh-card .blkh-card__media img {
	position: absolute !important;
	top: 0 !important;
	left: 0 !important;
	right: 0 !important;
	bottom: 0 !important;
	width: 100% !important;
	height: 100% !important;
	max-width: none !important;
	max-height: none !important;
	min-width: 0 !important;
	margin: 0 !important;
	padding: 0 !important;
	border: 0 !important;
	border-radius: 0 !important;
	display: block !important;
	object-fit: cover;
	object-position: center;
	transition: transform 0.35s ease;
}

.blkh-grid .blkh-card:hover .blkh-card__media img {
	transform: scale(1.04);
}

.blkh-card__play {
	position: absolute;
	inset: 0;
	margin: auto;
	width: 54px;
	height: 54px;
	border-radius: 50%;
	background: var(--blkh-scrim);
	backdrop-filter: blur(2px);
	box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
	transition: transform 0.2s ease, background 0.2s ease;
	z-index: 1;
}

.blkh-card:hover .blkh-card__play {
	transform: scale(1.08);
	background: linear-gradient(135deg, var(--blkh-brand), var(--blkh-brand-2));
}

.blkh-card__play::after {
	content: "";
	position: absolute;
	top: 50%;
	left: 54%;
	transform: translate(-50%, -50%);
	border-style: solid;
	border-width: 9px 0 9px 15px;
	border-color: transparent transparent transparent #fff;
}

/* Corner chip: item count for galleries. */
.blkh-card__count {
	position: absolute;
	top: 10px;
	inset-inline-end: 10px;
	z-index: 1;
	display: inline-flex;
	align-items: center;
	gap: 4px;
	padding: 3px 8px;
	border-radius: 999px;
	background: var(--blkh-scrim);
	backdrop-filter: blur(2px);
	color: #fff;
	font-size: 12px;
	font-weight: 600;
	line-height: 1.4;
}

/* ---------------------------------------------------------------- body ---- */

.blkh-card__body {
	display: flex;
	flex: 1 1 auto;
	flex-direction: column;
	gap: 12px;
	padding: 14px 16px 13px;
	text-align: start;
}

.blkh-card__caption {
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 3;
	line-clamp: 3;
	overflow: hidden;
	margin: 0;
	color: var(--blkh-text) !important;
	font-size: 15px;
	line-height: 1.55;
	font-weight: 500;
	overflow-wrap: anywhere;
}

/* ------------------------------------------------------- text-only card ---- */

.blkh-card--text .blkh-card__body {
	position: relative;
	min-height: 190px;
	justify-content: space-between;
	padding: 22px 20px 16px;
	/* Only a brand tint — the card underneath already supplies the surface. */
	background-image: radial-gradient(
		120% 90% at 100% 0%,
		color-mix(in srgb, var(--blkh-brand) 12%, transparent),
		transparent 60%
	);
}

/* margin-top:auto here plus the footer's own auto margin centres the quote in
   whatever height the row ends up at. */
.blkh-card--text .blkh-card__caption {
	-webkit-line-clamp: 6;
	line-clamp: 6;
	margin-top: auto;
	font-size: 17px;
	font-weight: 600;
	line-height: 1.6;
}

/* --------------------------------------------------------------- footer ---- */

.blkh-card__foot {
	display: flex;
	align-items: center;
	gap: 8px;
	margin-top: auto;
	padding-top: 11px;
	border-top: 1px solid var(--blkh-border);
	color: var(--blkh-muted) !important;
	font-size: 13px;
	line-height: 1.3;
	min-width: 0;
}

/* Caption-less card: the footer still sits at the bottom, but with nothing
   above it the rule and its padding would look like a stray line. */
.blkh-card__body > .blkh-card__foot:first-child {
	padding-top: 0;
	border-top: 0;
}

.blkh-card__author {
	display: inline-flex;
	align-items: center;
	gap: 8px;
	min-width: 0;
}

.blkh-card__handle {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	font-weight: 600;
	color: var(--blkh-text) !important;
	direction: ltr;
	unicode-bidi: isolate;
}

.blkh-card__date {
	margin-inline-start: auto;
	flex: 0 0 auto;
	white-space: nowrap;
	font-size: 12px;
	color: var(--blkh-muted) !important;
	/* In adaptive mode --blkh-muted is `inherit`, so the de-emphasis comes from
	   opacity instead of a hard-coded grey that could vanish on some themes. */
	opacity: var(--blkh-muted-opacity);
}

/* Avatar: fixed 26px frame, image forced to fill it. */
.blkh-card__avatar {
	position: relative;
	display: block;
	flex: 0 0 26px;
	width: 26px;
	height: 26px;
	border-radius: 50%;
	overflow: hidden;
	background: linear-gradient(135deg, var(--blkh-brand), var(--blkh-brand-2));
	box-shadow: 0 0 0 1px var(--blkh-border);
}

.blkh-grid .blkh-card .blkh-card__avatar img {
	position: absolute !important;
	top: 0 !important;
	left: 0 !important;
	width: 100% !important;
	height: 100% !important;
	max-width: none !important;
	max-height: none !important;
	min-width: 0 !important;
	margin: 0 !important;
	padding: 0 !important;
	border: 0 !important;
	border-radius: 50% !important;
	display: block !important;
	object-fit: cover;
}

/* Initial-letter fallback when the author has no avatar. */
.blkh-card__avatar--initial {
	display: flex;
	align-items: center;
	justify-content: center;
	color: #fff;
	font-size: 12px;
	font-weight: 700;
	line-height: 1;
	text-transform: uppercase;
}

/* -------------------------------------------------------------- notices ---- */

.blkh-notice {
	padding: 14px 16px;
	border: 1px dashed rgba(0, 0, 0, 0.2);
	border-radius: 12px;
	font-size: 14px;
	opacity: 0.75;
}

.blkh-editor-preview {
	padding: 18px;
	border: 1px dashed rgba(0, 0, 0, 0.25);
	border-radius: 12px;
	text-align: center;
}

.blkh-editor-preview p {
	margin: 6px 0 0;
	opacity: 0.7;
	font-size: 13px;
}

@media (prefers-color-scheme: dark) {
	.blkh-notice,
	.blkh-editor-preview {
		border-color: rgba(255, 255, 255, 0.2);
	}
}

@media (prefers-reduced-motion: reduce) {
	.blkh-card,
	.blkh-card__media img,
	.blkh-card__play {
		transition: none;
	}

	.blkh-grid a.blkh-card:hover {
		transform: none;
	}

	.blkh-grid .blkh-card:hover .blkh-card__media img {
		transform: none;
	}
}
