/*
 * Media Gallery – front end.
 * Inherits your theme's font and text color. Customize with CSS variables, e.g.
 *   .igmg { --igmg-gap: 8px; --igmg-radius: 0; }
 */

.igmg {
	--igmg-accent: #2b4c7e;
	--igmg-on-accent: #fff;
	--igmg-gap: 4px;
	--igmg-min: 180px;
	--igmg-radius: 3px;
	--igmg-tile: rgba(128, 128, 128, 0.16);
	margin: 1.5em 0;
	color: inherit;
	font: inherit;
}

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

/* Our elements set display in classes, so make sure the hidden attribute still wins over theme and class rules. */
.igmg [hidden],
.igmg-lb [hidden],
.igmg-lb[hidden] {
	display: none !important;
}

.igmg-size-small { --igmg-min: 120px; }
.igmg-size-medium { --igmg-min: 180px; }
.igmg-size-large { --igmg-min: 280px; --igmg-gap: 8px; }

/* ---------- Notice (editors only) ---------- */

.igmg-notice {
	margin: 0 0 1em;
	padding: 0.75em 1em;
	border-left: 4px solid #c98300;
	background: rgba(201, 131, 0, 0.1);
	font-size: 0.9em;
}

/* ---------- Top bar ---------- */

.igmg .igmg-bar {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: space-between;
	gap: 0.75em 1em;
	margin: 0 0 0.75em;
}

.igmg .igmg-summary {
	margin: 0;
	font-size: 0.9em;
	opacity: 0.75;
}

.igmg .igmg-bar-actions {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5em;
}

/* ---------- Buttons (defensive: themes style <button> heavily) ---------- */

.igmg .igmg-btn,
.igmg-lb .igmg-btn {
	-webkit-appearance: none;
	appearance: none;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 0.45em;
	min-height: 44px;
	margin: 0;
	padding: 0.55em 1.15em;
	border: 1px solid transparent;
	border-radius: 999px;
	background: none;
	box-shadow: none;
	color: inherit;
	font: inherit;
	font-size: 0.95em;
	font-weight: 600;
	line-height: 1.2;
	letter-spacing: normal;
	text-decoration: none;
	text-transform: none;
	white-space: nowrap;
	cursor: pointer;
	transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease, filter 0.15s ease;
}

.igmg .igmg-btn-solid,
.igmg-lb .igmg-btn-solid {
	background: var(--igmg-accent);
	color: var(--igmg-on-accent);
}

.igmg .igmg-btn-solid:hover,
.igmg-lb .igmg-btn-solid:hover {
	filter: brightness(1.1);
	color: var(--igmg-on-accent);
}

.igmg .igmg-btn-quiet {
	border-color: rgba(128, 128, 128, 0.45);
	background: transparent;
	color: inherit;
}

.igmg .igmg-btn-quiet:hover {
	border-color: currentColor;
}

.igmg-lb .igmg-btn-light {
	background: rgba(255, 255, 255, 0.14);
	color: #fff;
}

.igmg-lb .igmg-btn-light:hover {
	background: rgba(255, 255, 255, 0.24);
}

.igmg .igmg-btn:disabled,
.igmg-lb .igmg-btn:disabled {
	opacity: 0.45;
	cursor: not-allowed;
	filter: none;
}

.igmg .igmg-btn:focus-visible,
.igmg-lb .igmg-btn:focus-visible {
	outline: 2px solid var(--igmg-accent);
	outline-offset: 2px;
}

.igmg-lb .igmg-btn:focus-visible {
	outline-color: #fff;
}

.igmg .igmg-btn-note,
.igmg-lb .igmg-btn-note {
	font-weight: 400;
	font-size: 0.9em;
	opacity: 0.8;
}

.igmg .igmg-icon,
.igmg-lb .igmg-icon {
	display: block;
	flex: none;
	width: 1.15em;
	height: 1.15em;
}

/* ---------- Grid ---------- */

.igmg .igmg-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(min(var(--igmg-min), 100%), 1fr));
	gap: var(--igmg-gap);
	margin: 0;
	padding: 0;
	list-style: none;
}

.igmg .igmg-item {
	position: relative;
	margin: 0;
	padding: 0;
	aspect-ratio: 1 / 1;
	overflow: hidden;
	border-radius: var(--igmg-radius);
	background: var(--igmg-tile);
	list-style: none;
	transition: background-color 0.18s ease, opacity 0.18s ease;
}

.igmg .igmg-item::before,
.igmg .igmg-item::marker {
	content: none;
}

.igmg .igmg-open {
	-webkit-appearance: none;
	appearance: none;
	position: absolute;
	inset: 0;
	display: block;
	width: 100%;
	height: 100%;
	min-height: 0;
	margin: 0;
	padding: 0;
	border: 0;
	border-radius: 0;
	background: none;
	box-shadow: none;
	color: inherit;
	cursor: zoom-in;
}

.igmg .igmg-open:focus-visible {
	outline: 3px solid var(--igmg-accent);
	outline-offset: -3px;
}

.igmg .igmg-thumb {
	display: block;
	width: 100%;
	height: 100%;
	max-width: none;
	margin: 0;
	border-radius: 0;
	object-fit: cover;
	background: transparent;
	transition: transform 0.18s ease, border-radius 0.18s ease;
}

.igmg .igmg-play {
	position: absolute;
	top: 50%;
	left: 50%;
	display: grid;
	place-items: center;
	width: 48px;
	height: 48px;
	border-radius: 50%;
	background: rgba(0, 0, 0, 0.5);
	color: #fff;
	transform: translate(-50%, -50%);
	pointer-events: none;
}

.igmg .igmg-play .igmg-icon {
	width: 30px;
	height: 30px;
	margin-left: 3px;
}

.igmg .igmg-duration {
	position: absolute;
	left: 6px;
	bottom: 6px;
	padding: 1px 6px;
	border-radius: 3px;
	background: rgba(0, 0, 0, 0.62);
	color: #fff;
	font-size: 12px;
	line-height: 18px;
	font-variant-numeric: tabular-nums;
	pointer-events: none;
}

/* Per-item download button: always visible on touch screens, revealed on hover with a mouse. */
.igmg .igmg-item-dl {
	position: absolute;
	right: 6px;
	bottom: 6px;
	z-index: 2;
	display: grid;
	place-items: center;
	width: 40px;
	height: 40px;
	border-radius: 50%;
	background: rgba(255, 255, 255, 0.94);
	box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
	color: #1a1a1a;
	text-decoration: none;
	transition: opacity 0.15s ease, transform 0.15s ease, background-color 0.15s ease, color 0.15s ease;
}

.igmg .igmg-item-dl .igmg-icon {
	width: 20px;
	height: 20px;
}

.igmg .igmg-item-dl:hover,
.igmg .igmg-item-dl:focus-visible {
	background: var(--igmg-accent);
	color: var(--igmg-on-accent);
}

.igmg .igmg-item-dl:focus-visible {
	outline: 2px solid #fff;
	outline-offset: 2px;
}

@media (hover: hover) and (pointer: fine) {
	.igmg .igmg-item-dl {
		opacity: 0;
		transform: translateY(4px);
	}

	.igmg .igmg-item:hover .igmg-item-dl,
	.igmg .igmg-item:focus-within .igmg-item-dl {
		opacity: 1;
		transform: none;
	}
}

.igmg.igmg-size-small .igmg-item-dl {
	right: 4px;
	bottom: 4px;
	width: 32px;
	height: 32px;
}

.igmg.igmg-size-small .igmg-item-dl .igmg-icon {
	width: 17px;
	height: 17px;
}

/* ---------- Select mode ---------- */

.igmg .igmg-check {
	position: absolute;
	top: 8px;
	left: 8px;
	display: none;
	place-items: center;
	width: 26px;
	height: 26px;
	border: 2px solid #fff;
	border-radius: 50%;
	background: rgba(0, 0, 0, 0.28);
	box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
	color: transparent;
	pointer-events: none;
}

.igmg .igmg-check .igmg-icon {
	width: 16px;
	height: 16px;
	stroke-width: 3;
}

.igmg.is-selecting .igmg-check {
	display: grid;
}

.igmg.is-selecting .igmg-open {
	cursor: pointer;
}

.igmg.is-selecting .igmg-item-dl {
	display: none;
}

.igmg.is-selecting .igmg-item:not(.can-dl) {
	opacity: 0.3;
}

.igmg.is-selecting .igmg-item:not(.can-dl) .igmg-open {
	cursor: not-allowed;
}

/* Selected: the photo pulls in and the tile fills with the button color, like a mat around a print. */
.igmg .igmg-item.is-selected {
	background: var(--igmg-accent);
}

.igmg .igmg-item.is-selected .igmg-thumb {
	transform: scale(0.88);
	border-radius: 2px;
}

.igmg .igmg-item.is-selected .igmg-check {
	background: #fff;
	color: var(--igmg-accent);
}

.igmg .igmg-selectbar {
	position: sticky;
	bottom: max(12px, env(safe-area-inset-bottom));
	z-index: 5;
	display: flex;
	align-items: center;
	gap: 0.6em;
	margin-top: 12px;
	padding: 8px 8px 8px 18px;
	border-radius: 16px;
	background: rgba(22, 22, 24, 0.94);
	box-shadow: 0 8px 28px rgba(0, 0, 0, 0.28);
	color: #fff;
	-webkit-backdrop-filter: blur(10px);
	backdrop-filter: blur(10px);
}

.igmg .igmg-sel-info {
	display: flex;
	flex: 1;
	flex-direction: column;
	align-items: flex-start;
	min-width: 0;
	line-height: 1.3;
}

.igmg .igmg-sel-count {
	font-size: 0.92em;
	font-weight: 600;
}

.igmg .igmg-textbtn {
	-webkit-appearance: none;
	appearance: none;
	margin: 0;
	padding: 2px 0;
	border: 0;
	background: none;
	box-shadow: none;
	color: inherit;
	font: inherit;
	font-size: 0.85em;
	text-decoration: underline;
	text-underline-offset: 2px;
	text-transform: none;
	opacity: 0.8;
	cursor: pointer;
}

.igmg .igmg-textbtn:hover {
	opacity: 1;
}

.igmg .igmg-selectbar .igmg-btn-quiet {
	border-color: rgba(255, 255, 255, 0.35);
	color: #fff;
}

.igmg .igmg-selectbar .igmg-btn:focus-visible,
.igmg .igmg-textbtn:focus-visible {
	outline: 2px solid #fff;
	outline-offset: 2px;
}

/* ---------- Phones ---------- */

@media (max-width: 480px) {
	.igmg {
		--igmg-gap: 3px;
	}

	.igmg.igmg-size-small .igmg-grid { grid-template-columns: repeat(3, 1fr); }
	.igmg.igmg-size-medium .igmg-grid { grid-template-columns: repeat(2, 1fr); }
	.igmg.igmg-size-large .igmg-grid { grid-template-columns: 1fr; }

	.igmg .igmg-bar-actions {
		width: 100%;
	}

	.igmg .igmg-bar-actions .igmg-btn {
		flex: 1;
	}

	.igmg .igmg-selectbar {
		padding-left: 14px;
	}

	.igmg .igmg-selectbar .igmg-btn {
		padding-left: 0.9em;
		padding-right: 0.9em;
	}
}

/* ---------- Viewer ---------- */

html.igmg-lock,
html.igmg-lock body {
	overflow: hidden;
}

.igmg-lb {
	--igmg-accent: #2b4c7e; /* Replaced with the gallery's color when the viewer opens. */
	--igmg-on-accent: #fff;
	position: fixed;
	inset: 0;
	z-index: 999999;
	display: grid;
	grid-template-rows: auto minmax(0, 1fr) auto;
	background: #000;
	color: #f4f4f4;
	opacity: 0;
	overscroll-behavior: contain;
	transition: opacity 0.18s ease;
}

.igmg-lb.is-open {
	opacity: 1;
}

.igmg-lb .igmg-lb-top {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: max(8px, env(safe-area-inset-top)) 8px 4px 16px;
}

.igmg-lb .igmg-lb-count {
	font-size: 14px;
	font-variant-numeric: tabular-nums;
	opacity: 0.8;
}

.igmg-lb .igmg-lb-btn {
	-webkit-appearance: none;
	appearance: none;
	display: grid;
	place-items: center;
	width: 44px;
	height: 44px;
	min-height: 0;
	margin: 0;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: transparent;
	box-shadow: none;
	color: #fff;
	cursor: pointer;
	transition: background-color 0.15s ease;
}

.igmg-lb .igmg-lb-btn:hover {
	background: rgba(255, 255, 255, 0.14);
}

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

.igmg-lb .igmg-lb-btn .igmg-icon {
	width: 24px;
	height: 24px;
}

.igmg-lb .igmg-lb-stage {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	min-height: 0;
	padding: 0 64px;
}

.igmg-lb .igmg-lb-media {
	display: block;
	width: auto;
	height: auto;
	max-width: 100%;
	max-height: 100%;
	margin: 0;
	border-radius: 0;
	box-shadow: none;
	object-fit: contain;
	-webkit-user-select: none;
	user-select: none;
}

.igmg-lb video.igmg-lb-media {
	width: 100%;
	height: 100%;
	background: transparent;
}

.igmg-lb .igmg-lb-nav {
	position: absolute;
	top: 50%;
	background: rgba(0, 0, 0, 0.4);
	transform: translateY(-50%);
}

.igmg-lb .igmg-lb-prev { left: 10px; }
.igmg-lb .igmg-lb-next { right: 10px; }

.igmg-lb .igmg-lb-bottom {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 10px 16px;
	padding: 10px 16px max(14px, env(safe-area-inset-bottom));
}

.igmg-lb .igmg-lb-caption {
	flex: 1 1 240px;
	max-height: 4.5em;
	margin: 0;
	overflow: auto;
	font-size: 15px;
	line-height: 1.45;
}

.igmg-lb .igmg-lb-actions {
	display: flex;
	flex-wrap: wrap;
	justify-content: flex-end;
	gap: 8px;
	margin-left: auto;
}

.igmg-lb .igmg-lb-nodl {
	font-size: 14px;
	opacity: 0.7;
}

@media (max-width: 600px) {
	.igmg-lb .igmg-lb-stage {
		padding: 0;
	}

	.igmg-lb .igmg-lb-nav {
		width: 40px;
		height: 40px;
	}

	.igmg-lb .igmg-lb-prev { left: 6px; }
	.igmg-lb .igmg-lb-next { right: 6px; }

	.igmg-lb .igmg-lb-actions {
		width: 100%;
	}

	.igmg-lb .igmg-lb-actions .igmg-btn {
		flex: 1;
	}
}

/* ---------- Toast ---------- */

.igmg-toast {
	position: fixed;
	left: 50%;
	bottom: calc(20px + env(safe-area-inset-bottom));
	z-index: 1000000;
	display: flex;
	align-items: center;
	gap: 10px;
	max-width: min(92vw, 420px);
	padding: 12px 18px;
	border-radius: 12px;
	background: rgba(22, 22, 24, 0.96);
	box-shadow: 0 8px 28px rgba(0, 0, 0, 0.3);
	color: #fff;
	font-size: 15px;
	line-height: 1.4;
	opacity: 0;
	pointer-events: none;
	transform: translate(-50%, 16px);
	transition: opacity 0.2s ease, transform 0.2s ease;
}

.igmg-toast.is-visible {
	opacity: 1;
	transform: translate(-50%, 0);
}

.igmg-spinner {
	flex: none;
	width: 16px;
	height: 16px;
	border: 2px solid rgba(255, 255, 255, 0.3);
	border-top-color: #fff;
	border-radius: 50%;
	animation: igmg-spin 0.8s linear infinite;
}

@keyframes igmg-spin {
	to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
	.igmg *,
	.igmg-lb,
	.igmg-toast {
		transition: none !important;
	}

	.igmg-spinner {
		animation-duration: 2.4s;
	}
}

/* ---------- Show more ---------- */

.igmg .igmg-more {
	display: flex;
	justify-content: center;
	margin-top: 16px;
}

.igmg .igmg-more .igmg-btn {
	min-width: 220px;
}

.igmg .igmg-btn.is-busy,
.igmg-lb .igmg-lb-btn.is-busy {
	position: relative;
	pointer-events: none;
}

.igmg .igmg-more .igmg-btn.is-busy .igmg-more-label {
	opacity: 0.7;
}

.igmg-lb .igmg-lb-btn.is-busy .igmg-icon {
	opacity: 0;
}

.igmg-lb .igmg-lb-btn.is-busy::after {
	content: "";
	position: absolute;
	width: 18px;
	height: 18px;
	border: 2px solid rgba(255, 255, 255, 0.35);
	border-top-color: #fff;
	border-radius: 50%;
	animation: igmg-spin 0.8s linear infinite;
}
