/* ─── Box-sizing reset ──────────────────────────────────────────── */
.cp-gallery,
.cp-gallery *,
.cp-gallery *::before,
.cp-gallery *::after,
.cp-lightbox,
.cp-lightbox *,
.cp-lightbox *::before,
.cp-lightbox *::after,
.cp-gallery-pagination,
.cp-gallery-pagination * {
	box-sizing: border-box;
}

/* ─── Shared gallery vars ───────────────────────────────────────── */
/*
 * --cp-gallery-admin-cols is set by PHP from the saved column setting.
 * CSS owns the responsive column count — PHP's value is applied only
 * at the desktop breakpoint via the layout rules below.
 */
.cp-gallery {
	--cp-gallery-admin-cols: 3;
	--cp-gallery-mobile-cols: 2;
	--cp-gap-desktop: 8px;
	--cp-gap-mobile: 0px;
	--cp-gap: var(--cp-gap-mobile);
	margin: 0 0 2em;
}

/* ─── Post text preceding the gallery ──────────────────────────────── */
.cp-gallery-post-text {
	margin-bottom: 2em;
}

/* ─── Shared item / link / img ──────────────────────────────────── */
.cp-gallery__item {
	margin: 0;
	padding: 0;
	/* Grid/flex children default to min-width:auto, which lets unbreakable
	   content (e.g. a long single-word caption) force the track wider than its
	   fraction and blow out the column layout. Pin to 0 so items always honor
	   their track width. */
	min-width: 0;
}

.cp-gallery__link {
	display: block;
	overflow: hidden;
	cursor: pointer;
	text-decoration: none;
	position: relative;
	-webkit-tap-highlight-color: transparent;
}

/* zoom-in cursor only where hover is available */
@media (hover: hover) and (pointer: fine) {
	.cp-gallery__link {
		cursor: zoom-in;
	}
}

/* ─── Video items ────────────────────────────────────────────────── */
.cp-gallery__item--video .cp-gallery__link {
	cursor: pointer;
}

.cp-gallery__play-btn {
	position: absolute;
	inset: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	pointer-events: none;
}

.cp-gallery__play-btn::before {
	content: '';
	width: clamp(44px, 12vw, 56px);
	height: clamp(44px, 12vw, 56px);
	border-radius: 50%;
	background: rgba(0, 0, 0, 0.55);
	display: block;
	position: absolute;
}

.cp-gallery__play-btn::after {
	content: '';
	width: 0;
	height: 0;
	border-style: solid;
	border-width: 10px 0 10px 18px;
	border-color: transparent transparent transparent #fff;
	position: absolute;
	margin-left: 4px;
}

@media (hover: hover) and (pointer: fine) {
	.cp-gallery__item--video .cp-gallery__link:hover .cp-gallery__play-btn::before {
		background: rgba(0, 0, 0, 0.72);
	}
}

.cp-gallery__video-placeholder {
	width: 100%;
	aspect-ratio: 16 / 9;
	background: #111;
}

.cp-gallery__img {
	display: block;
	width: 100%;
	height: auto;
	transition: transform 0.25s ease;
}

@media (hover: hover) and (pointer: fine) {
	.cp-gallery__link:hover .cp-gallery__img {
		transform: scale(1.04);
	}
}

.cp-gallery__caption {
	font-size: clamp(0.75rem, 2vw, 0.85rem);
	color: #666;
	padding: 10px 2px 0;
	line-height: 1.4;
	/* Break unbreakable strings (long tokens, URLs) so they wrap inside the
	   column instead of forcing it wider. */
	overflow-wrap: anywhere;
}

/* ─── Layout: Masonry ───────────────────────────────────────────── */
/* Mobile default: uses per-gallery mobile column and gap settings.
 * True staggered masonry via CSS Grid: each item gets grid-row-end set to
 * a --cp-row-span value computed by cp-gallery-frontend.js from the item's
 * actual rendered column width + the image's real aspect ratio (PHP can't
 * know the responsive column width at render time). grid-auto-flow stays
 * at its default (row, not dense) so items are always placed strictly in
 * source order — unlike column-count, which fills column-by-column and
 * scrambles curated ordering. --cp-row-span falls back to a placeholder
 * before the JS pass runs on load. */
.cp-gallery--masonry {
	display: grid;
	grid-template-columns: repeat(var(--cp-gallery-mobile-cols), 1fr);
	grid-auto-rows: 1px;
	gap: var(--cp-gap);
}

.cp-gallery--masonry .cp-gallery__item {
	grid-row-end: span var(--cp-row-span, 300);
}

.cp-gallery--masonry .cp-gallery__item--wide {
	grid-column: span 2;
}

/* ─── Layout: Grid ──────────────────────────────────────────────── */
.cp-gallery--grid {
	display: grid;
	grid-template-columns: repeat(var(--cp-gallery-mobile-cols), 1fr);
	gap: var(--cp-gap);
}

.cp-gallery--grid .cp-gallery__item {
	aspect-ratio: var(--cp-grid-aspect, 2/3);
	overflow: hidden;
}

.cp-gallery--grid .cp-gallery__link {
	height: 100%;
}

.cp-gallery--grid .cp-gallery__img {
	width: 100%;
	height: 100%;
	object-fit: cover;
}

/* ─── Layout: Polaroid ──────────────────────────────────────────── */
.cp-gallery--polaroid {
	display: grid;
	grid-template-columns: repeat(var(--cp-gallery-mobile-cols), 1fr);
	gap: var(--cp-gap);
	padding: 16px 8px 8px;
}

.cp-gallery--polaroid .cp-gallery__item {
	background: #fff;
	padding: 10px 10px 44px;
	box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22);
	position: relative;
	transition: transform 0.2s ease, box-shadow 0.2s ease;
}

/* Tilts and hover only on devices that support hover */
@media (hover: hover) and (pointer: fine) {
	.cp-gallery--polaroid .cp-gallery__item:nth-child(4n + 1) { transform: rotate(-2deg); }
	.cp-gallery--polaroid .cp-gallery__item:nth-child(4n + 2) { transform: rotate(1.5deg); }
	.cp-gallery--polaroid .cp-gallery__item:nth-child(4n + 3) { transform: rotate(-0.8deg); }
	.cp-gallery--polaroid .cp-gallery__item:nth-child(4n + 4) { transform: rotate(1.2deg); }

	.cp-gallery--polaroid .cp-gallery__item:hover {
		transform: rotate(0deg) scale(1.05);
		box-shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
		z-index: 2;
	}
}

.cp-gallery--polaroid .cp-gallery__link {
	overflow: visible;
}

.cp-gallery--polaroid .cp-gallery__img {
	aspect-ratio: 1;
	object-fit: cover;
	width: 100%;
	height: auto;
}

.cp-gallery--polaroid .cp-gallery__caption {
	text-align: center;
	position: absolute;
	bottom: 10px;
	left: 10px;
	right: 10px;
	color: #555;
}

/* ─── Layout: Video (all-video post, no grid) ───────────────────── */
/* Handles any number of videos: a centered wrapping flow — one
   full-width tile per row on phones, side-by-side as space allows. */
.cp-gallery--video {
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	gap: clamp(10px, 2.5vw, 16px);
}

.cp-gallery--video .cp-gallery__item--video {
	width: min(100%, 480px);
	margin: 0;
}

/* ─── Video items in mixed galleries (masonry/grid) ────────────── */
/* min() keeps the item inside its column while capping at 480px */
.cp-gallery__item--video {
	width: min(100%, 480px);
	margin-left: auto;
	margin-right: auto;
}

/* ─── Inline video player ────────────────────────────────────────
   aspect-ratio here is only the fallback when the video's real
   dimensions are unknown — the frontend JS sets an inline
   aspect-ratio (and widens landscape players) from the item's
   stored dimensions or the thumbnail's natural size. */
.cp-gallery__video-player {
	position: relative;
	width: 100%;
	max-width: clamp(240px, 60vw, 420px);
	aspect-ratio: 9 / 16;
	margin: 0 auto;
	overflow: hidden;
}

.cp-gallery__video-player iframe,
.cp-gallery__video-player stream {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	border: 0;
}

/* ─── Gallery message ───────────────────────────────────────────── */
.cp-gallery__message {
	margin-bottom: 1.5em;
}

.cp-gallery__message > *:last-child {
	margin-bottom: 0;
}

/* ─── Empty state ───────────────────────────────────────────────── */
.cp-gallery-empty {
	color: #888;
	font-style: italic;
	padding: 1em 0;
}

/* ─── Pagination ────────────────────────────────────────────────── */
.cp-gallery-pagination {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
	justify-content: center;
	margin: 1.25em 0;
}

.cp-gallery-pagination__btn {
	min-height: 44px;
	min-width: 44px;
	padding: 0 clamp(10px, 3vw, 16px);
	border: 1px solid #d0d0d0;
	background: #fff;
	color: #333;
	cursor: pointer;
	border-radius: 3px;
	font-size: clamp(0.82rem, 2vw, 0.88rem);
	line-height: 1;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 0.15s, border-color 0.15s, color 0.15s;
	-webkit-tap-highlight-color: transparent;
}

@media (hover: hover) and (pointer: fine) {
	.cp-gallery-pagination__btn:hover:not(:disabled) {
		background: #f4f4f4;
		border-color: #aaa;
	}
}

.cp-gallery-pagination__btn.is-current {
	background: #222;
	color: #fff;
	border-color: #222;
	cursor: default;
}

.cp-gallery-pagination__btn:disabled {
	opacity: 0.35;
	cursor: default;
}

/* ─── Lightbox overlay ──────────────────────────────────────────── */
.cp-lightbox {
	position: fixed;
	inset: 0;
	z-index: 99999;
	display: flex;
	align-items: center;
	justify-content: center;
	opacity: 0;
	pointer-events: none;
	transition: opacity 0.25s ease;
	touch-action: none;
}

.cp-lightbox--open {
	opacity: 1;
	pointer-events: auto;
}

.cp-lightbox__backdrop {
	position: absolute;
	inset: 0;
}

.cp-lightbox--dark  .cp-lightbox__backdrop { background: rgba(0, 0, 0, 0.93); }
.cp-lightbox--light .cp-lightbox__backdrop { background: rgba(255, 255, 255, 0.96); }

/* ─── Lightbox layout ───────────────────────────────────────────── */
.cp-lightbox__wrap {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	width: 100%;
	height: 100%;
	overflow: hidden;
}

.cp-lightbox__stage {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	max-width: 100%;
	max-height: 100%;
}

.cp-lightbox__img-wrap {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	max-width: 100%;
	overflow: visible;
}

.cp-lightbox__img {
	max-width: 100vw;
	max-height: 100vh;
	width: auto;
	height: auto;
	object-fit: contain;
	display: block;
	opacity: 0;
	transition: opacity 0.2s ease;
	user-select: none;
	-webkit-user-select: none;
}

@media (hover: hover) and (pointer: fine) {
	.cp-lightbox__img {
		cursor: zoom-in;
	}
}

.cp-lightbox__img--in {
	opacity: 1;
}

/* ─── Lightbox video pane ───────────────────────────────────────── */
.cp-lightbox__pane--hidden {
	display: none;
}

.cp-lightbox__video-wrap {
	position: relative;
	/* Definite width, not 100% — .cp-lightbox__stage is an auto-sized flex
	   item (its size normally comes from the <img>'s intrinsic size), so a
	   percentage width here has nothing definite to resolve against and
	   collapses to zero. */
	/* width/aspect-ratio are the portrait fallback for videos with unknown
	   dimensions — frontend JS overrides both inline from the video's real
	   dimensions (landscape videos get a wider box sized off 92vh). */
	width: clamp(280px, 70vw, 520px);
	aspect-ratio: 9 / 16;
	max-height: 92vh;
	margin: 0 auto;
	overflow: hidden;
}

.cp-lightbox__video-wrap iframe,
.cp-lightbox__video-wrap stream {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	border: 0;
}

.cp-lightbox__video-placeholder {
	width: 100%;
	height: 100%;
	background: #111;
}

/* ─── Lightbox caption ──────────────────────────────────────────── */
.cp-lightbox__caption {
	position: absolute;
	bottom: 0;
	left: 0;
	right: 0;
	margin: 0;
	padding: 40px 20px 16px;
	font-size: clamp(0.78rem, 2.5vw, 0.88rem);
	text-align: center;
	line-height: 1.5;
	background: linear-gradient(transparent, rgba(0, 0, 0, 0.65));
	color: #ddd;
	pointer-events: none;
}

.cp-lightbox__caption:empty {
	display: none;
}

.cp-lightbox--dark  .cp-lightbox__caption { color: #ddd; }
.cp-lightbox--light .cp-lightbox__caption { color: #f5f5f5; }

/* ─── Lightbox counter ──────────────────────────────────────────── */
.cp-lightbox__counter {
	position: absolute;
	top: 20px;
	left: 50%;
	transform: translateX(-50%);
	font-size: clamp(0.7rem, 2vw, 0.78rem);
	letter-spacing: 0.04em;
	opacity: 0.65;
	pointer-events: none;
}

.cp-lightbox--dark  .cp-lightbox__counter { color: #fff; }
.cp-lightbox--light .cp-lightbox__counter { color: #222; }

/* ─── Lightbox close ────────────────────────────────────────────── */
.cp-lightbox__close {
	position: absolute;
	top: 0;
	right: 0;
	background: none;
	border: none;
	font-size: clamp(1.8em, 5vw, 2.2em);
	line-height: 1;
	cursor: pointer;
	min-width: 56px;
	min-height: 56px;
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 1;
	transition: opacity 0.15s;
	-webkit-tap-highlight-color: transparent;
}

.cp-lightbox--dark  .cp-lightbox__close { color: #fff; }
.cp-lightbox--light .cp-lightbox__close { color: #222; }

.cp-lightbox__close:hover { opacity: 0.6; }

/* ─── Lightbox arrows ───────────────────────────────────────────── */
.cp-lightbox__prev,
.cp-lightbox__next {
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
	background: none;
	border: none;
	font-size: clamp(1.8em, 5vw, 2.6em);
	line-height: 1;
	cursor: pointer;
	min-width: 56px;
	min-height: 56px;
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 1;
	transition: opacity 0.15s;
	-webkit-user-select: none;
	user-select: none;
	-webkit-tap-highlight-color: transparent;
}

.cp-lightbox--dark  .cp-lightbox__prev,
.cp-lightbox--dark  .cp-lightbox__next { color: #fff; }
.cp-lightbox--light .cp-lightbox__prev,
.cp-lightbox--light .cp-lightbox__next { color: #222; }

.cp-lightbox__prev { left: 0; }
.cp-lightbox__next { right: 0; }

.cp-lightbox__prev:hover,
.cp-lightbox__next:hover { opacity: 0.6; }

.cp-lightbox__prev:disabled,
.cp-lightbox__next:disabled { opacity: 0.18; cursor: default; }

/* Prevent body scroll while lightbox is open */
body.cp-lightbox-open { overflow: hidden; }

/* WP admin bar offset */
.admin-bar .cp-lightbox {
	top: var(--wp-admin--admin-bar--height, 32px);
}

/* ─── Lightbox loading spinner ──────────────────────────────────── */
.cp-lightbox__loader {
	display: none;
	position: absolute;
	width: 44px;
	height: 44px;
	border: 3px solid rgba(255, 255, 255, 0.15);
	border-top-color: rgba(255, 255, 255, 0.75);
	border-radius: 50%;
	animation: cp-spin 0.75s linear infinite;
	pointer-events: none;
	z-index: 1;
}

.cp-lightbox__loader.is-active {
	display: block;
}

.cp-lightbox--light .cp-lightbox__loader {
	border-color: rgba(0, 0, 0, 0.12);
	border-top-color: rgba(0, 0, 0, 0.6);
}

@keyframes cp-spin {
	to { transform: rotate(360deg); }
}

/* ─── Lightbox play/pause ───────────────────────────────────────── */
.cp-lightbox__play {
	position: absolute;
	bottom: 18px;
	left: calc(50% - 28px);
	transform: translateX(-50%);
	background: rgba(0, 0, 0, 0.45);
	border: 1px solid rgba(255, 255, 255, 0.25);
	border-radius: 50%;
	width: 44px;
	height: 44px;
	font-size: 1.1em;
	line-height: 1;
	cursor: pointer;
	z-index: 1;
	transition: background 0.15s, opacity 0.15s;
	display: flex;
	align-items: center;
	justify-content: center;
	color: #fff;
	-webkit-tap-highlight-color: transparent;
}

.cp-lightbox__play:hover { background: rgba(0, 0, 0, 0.7); }

.cp-lightbox__speed {
	position: absolute;
	bottom: 22px;
	left: calc(50% + 30px);
	background: rgba(0, 0, 0, 0.45);
	border: 1px solid rgba(255, 255, 255, 0.25);
	border-radius: 4px;
	color: #fff;
	font-size: 0.75em;
	padding: 0 8px;
	min-height: 44px;
	display: flex;
	align-items: center;
	cursor: pointer;
	z-index: 1;
	appearance: none;
	-webkit-appearance: none;
	-webkit-tap-highlight-color: transparent;
}

/* ─── Responsive: scale up from mobile base ─────────────────────── */
@media (min-width: 960px) {
	.cp-gallery {
		--cp-gap: var(--cp-gap-desktop);
	}

	.cp-gallery--masonry,
	.cp-gallery--grid,
	.cp-gallery--polaroid {
		grid-template-columns: repeat(var(--cp-gallery-admin-cols), 1fr);
	}
}
