/**
 * KW Gallery Widget — Frontend-CSS (§461).
 *
 * Token-System: --kwl-* fuer globale Plugin-Tokens, --kw-gal-* fuer Widget-lokal.
 * KEIN @layer (V4-Konflikt, §367).
 * Container-Queries auf innerem Grid (§-Regel: nicht auf Widget-Root).
 * Hover/Lift hinter (hover:hover) + (prefers-reduced-motion: no-preference).
 */

.kw-gallery {
	--kw-gal-cols-desktop: 4;
	--kw-gal-cols-tablet: 3;
	--kw-gal-cols-mobile: 2;
	--kw-gal-gap: 16px;
	--kw-gal-ratio: 4 / 3;
	--kw-gal-frame-pad: 12px;
	--kw-gal-frame-pad-bottom: 38px;

	display: flex;
	flex-direction: column;
	gap: var(--kwl-space-md, 1.25rem);
	color: var(--kwl-text, #1a202c);
}

/* Border/Background-Override mit höherer Spezifität gegen V4-Kit-CSS. Das
   Widget soll keinen eigenen Rahmen haben — die Bilder sind der visuelle
   Anker. Wenn die Galerie in einem Plugin-Card-Container liegt, wird auch
   dort der Card-Border zurückgezogen. */
.kw-gallery.kw-gallery,
.elementor-widget-kw_gallery .kw-gallery,
[data-kwl-card]:has(> .kw-gallery),
[data-kwl-card]:has(> .elementor-widget-kw_gallery) {
	background: transparent;
	border: none;
	border-radius: 0;
	box-shadow: none;
}

/* Spalten- und Ratio-Modifier (ersetzen Inline-Styles, §C5) */
.kw-gallery--cols-d-2 { --kw-gal-cols-desktop: 2; }
.kw-gallery--cols-d-3 { --kw-gal-cols-desktop: 3; }
.kw-gallery--cols-d-4 { --kw-gal-cols-desktop: 4; }
.kw-gallery--cols-d-5 { --kw-gal-cols-desktop: 5; }
.kw-gallery--cols-d-6 { --kw-gal-cols-desktop: 6; }

.kw-gallery--cols-t-1 { --kw-gal-cols-tablet: 1; }
.kw-gallery--cols-t-2 { --kw-gal-cols-tablet: 2; }
.kw-gallery--cols-t-3 { --kw-gal-cols-tablet: 3; }
.kw-gallery--cols-t-4 { --kw-gal-cols-tablet: 4; }

.kw-gallery--cols-m-1 { --kw-gal-cols-mobile: 1; }
.kw-gallery--cols-m-2 { --kw-gal-cols-mobile: 2; }

.kw-gallery--ratio-1-1 { --kw-gal-ratio: 1 / 1; }
.kw-gallery--ratio-4-3 { --kw-gal-ratio: 4 / 3; }
.kw-gallery--ratio-3-2 { --kw-gal-ratio: 3 / 2; }
.kw-gallery--ratio-16-9 { --kw-gal-ratio: 16 / 9; }
.kw-gallery--ratio-original { --kw-gal-ratio: auto; }

.kw-gallery__header {
	margin-bottom: 0;
}

.kw-gallery__heading {
	margin: 0;
}

/* ─── Toolbar (Suche + Filter-Pillen) ─────────────────────── */

.kw-gallery__toolbar {
	display: flex;
	gap: var(--kwl-space-sm, 0.75rem);
	row-gap: 12px;
	flex-wrap: wrap;
	align-items: center;
	justify-content: space-between;
}

.kw-gallery__search {
	display: flex;
	flex: 1 1 220px;
	min-width: 0;
	max-width: 320px;
}

.kw-gallery__search-input {
	width: 100%;
	padding: 0.6rem 0.9rem;
	border: 1px solid var(--kwl-border, #e2e8f0);
	border-radius: var(--kwl-radius-md, 8px);
	background: var(--kwl-bg, #fff);
	color: var(--kwl-text);
	font-family: var(--kwl-font-body, inherit);
	font-size: var(--kwl-fsize-small, 0.9rem);
	transition: border-color .15s ease, box-shadow .15s ease;
	min-height: 44px;
}

.kw-gallery__search-input::placeholder {
	color: var(--kwl-text-muted, #64748b);
}

.kw-gallery__search-input:focus {
	outline: none;
}

.kw-gallery__search-input:focus-visible {
	outline: 3px solid var(--kwl-focus, #7B6835);
	outline-offset: 2px;
	border-color: var(--kwl-accent, #BD9B5C);
}

/* Filter-Tabs: Text-basiert mit Gold-Underline beim aktiven Tab. Platzsparend,
   keine schweren Pillen-Backgrounds. Bei vielen Kategorien klappen sie in die
   nächste Zeile. */
.kw-gallery__pills {
	display: flex;
	flex-wrap: wrap;
	gap: 4px 18px;
	align-items: center;
	justify-content: flex-end;
}

/* V4-Pink-Defense: `.kw-gallery .kw-gallery__pill` (0,2,0) plus alle States
   explizit (`:hover`/`:focus`/`:active`/`:focus:not(:hover)`). */
.kw-gallery .kw-gallery__pill,
.kw-gallery .kw-gallery__pill:hover,
.kw-gallery .kw-gallery__pill:active,
.kw-gallery .kw-gallery__pill:focus,
.kw-gallery .kw-gallery__pill:focus:not(:hover) {
	position: relative;
	display: inline-flex;
	align-items: baseline;
	gap: 0.35em;
	padding: 8px 2px 10px;
	min-height: 44px;
	background: transparent;
	border: none;
	border-radius: 0;
	box-shadow: none;
	color: var(--kwl-text-muted, #64748b);
	font: inherit;
	font-size: var(--kwl-fsize-small, 0.95rem);
	font-weight: 500;
	letter-spacing: 0.01em;
	cursor: pointer;
	white-space: nowrap;
	text-decoration: none;
	transition: color .15s ease;
}

/* Gold-Underline-Indikator (immer im DOM, opacity-getoggelt für Smooth-Transition). */
.kw-gallery .kw-gallery__pill::after {
	content: "";
	position: absolute;
	left: 2px;
	right: 2px;
	bottom: 4px;
	height: 2px;
	background: var(--kwl-accent, #BD9B5C);
	border-radius: 1px;
	opacity: 0;
	transform: scaleX(0.4);
	transform-origin: center;
	transition: opacity .2s ease, transform .25s ease;
}

@media (hover: hover) {
	.kw-gallery .kw-gallery__pill:hover {
		color: var(--kwl-text, #1a202c);
	}
	.kw-gallery .kw-gallery__pill:hover::after {
		opacity: 0.4;
		transform: scaleX(0.6);
	}
}

.kw-gallery .kw-gallery__pill:focus-visible {
	outline: 3px solid var(--kwl-focus, #7B6835);
	outline-offset: 4px;
	border-radius: 4px;
}

.kw-gallery .kw-gallery__pill[aria-pressed="true"],
.kw-gallery .kw-gallery__pill[aria-pressed="true"]:hover,
.kw-gallery .kw-gallery__pill[aria-pressed="true"]:focus,
.kw-gallery .kw-gallery__pill[aria-pressed="true"]:focus:not(:hover) {
	color: var(--kwl-text, #1a202c);
	font-weight: 600;
}

.kw-gallery .kw-gallery__pill[aria-pressed="true"]::after {
	opacity: 1;
	transform: scaleX(1);
}

.kw-gallery__pill-count {
	font-variant-numeric: tabular-nums;
	font-size: 0.82em;
	font-weight: 400;
	color: var(--kwl-text-muted, #64748b);
	opacity: 0.75;
}

@media (prefers-reduced-motion: reduce) {
	.kw-gallery .kw-gallery__pill::after {
		transition: none;
	}
}

/* ─── Grid + Sections ─────────────────────────────────────── */

.kw-gallery__grid {
	display: flex;
	flex-direction: column;
	gap: var(--kwl-space-lg, 1.75rem);
	container-type: inline-size;
	container-name: kw-gallery;
	/* Bridge-Tokens fuer [data-kwl-link-scope]: Bild-Links nicht unterstreichen. */
	--kwl-link-underline: none;
	--kwl-link-color: var(--kwl-text, #1a202c);
	--kwl-link-hover-color: var(--kwl-text, #1a202c);
}

.kw-gallery__section {
	display: flex;
	flex-direction: column;
	gap: var(--kwl-space-sm, 0.75rem);
}

/* Spezifitaet (0,1,1) schlaegt UA-Default (0,1,0) — kein !important noetig. */
.kw-gallery__section[hidden] {
	display: none;
}

/* Performance §462: content-visibility erlaubt dem Browser, off-screen Sections
   komplett aus dem Layout/Paint auszuschliessen. contain-intrinsic-size verhindert
   Scroll-Anchor-Drift, indem der Container reservierte Hoehe behaelt.
   @supports-Gate fuer Safari < 18 (kein content-visibility-Support). */
@supports (content-visibility: auto) {
	.kw-gallery__section:not([hidden]) {
		content-visibility: auto;
		contain-intrinsic-size: auto 600px;
	}
}

.kw-gallery__section-header {
	display: flex;
	flex-direction: column;
	gap: 0.25rem;
}

.kw-gallery__section-heading {
	margin: 0;
	font-family: var(--kwl-font-heading, inherit);
	font-size: var(--kwl-fsize-h4, 1.25rem);
	color: var(--kwl-text);
}

.kw-gallery__section-subtitle {
	margin: 0;
	color: var(--kwl-text-muted, #64748b);
	font-size: var(--kwl-fsize-small, 0.9rem);
	max-width: 65ch;
}

/* ─── Items (Grid + Karte + Bild) ─────────────────────────── */

.kw-gallery__items {
	list-style: none;
	margin: 0;
	padding: 0;
	display: grid;
	grid-template-columns: repeat(var(--kw-gal-cols-mobile), 1fr);
	gap: var(--kw-gal-gap);
}

@media (min-width: 600px) {
	.kw-gallery__items {
		grid-template-columns: repeat(var(--kw-gal-cols-tablet), 1fr);
	}
}

@media (min-width: 1024px) {
	.kw-gallery__items {
		grid-template-columns: repeat(var(--kw-gal-cols-desktop), 1fr);
	}
}

/* Container-Query Fallback fuer schmale Container (z. B. Sidebar). */
@container kw-gallery (max-width: 480px) {
	.kw-gallery__items {
		grid-template-columns: repeat(var(--kw-gal-cols-mobile), 1fr);
	}
}

.kw-gallery__item {
	margin: 0;
}

.kw-gallery__item[hidden] {
	display: none;
}

/* `color: inherit` auf <a> wäre V4-anfällig (.elementor-kit-X a, 0,1,1 schlägt durch).
   Explizite Token-Farbe greift gegen den Pink-Reset (§274). */
.kw-gallery__link {
	display: block;
	color: var(--kwl-text, #1a202c);
	text-decoration: none;
	border-radius: var(--kwl-radius-md, 8px);
	transition: transform .25s ease, box-shadow .25s ease;
}

.kw-gallery__link:focus-visible {
	outline: 3px solid var(--kwl-focus, #7B6835);
	outline-offset: 3px;
}

.kw-gallery__figure {
	margin: 0;
	background: var(--kwl-bg, #fff);
	border-radius: var(--kwl-radius-md, 8px);
	overflow: hidden;
	box-shadow: var(--kwl-shadow-sm, 0 1px 2px rgba(0,0,0,.05));
	display: flex;
	flex-direction: column;
}

.kw-gallery__img {
	display: block;
	width: 100%;
	aspect-ratio: var(--kw-gal-ratio);
	object-fit: cover;
	background: color-mix(in oklch, var(--kwl-border, #e2e8f0) 50%, transparent);
}

/* Original-Ratio = keine aspect-ratio Erzwingung. */
.kw-gallery[data-kw-frame] .kw-gallery__img[height] {
	/* Browser nutzt natuerliches Verhaeltnis aus width/height. */
}

.kw-gallery__caption {
	padding: 0.55rem 0.75rem 0.7rem;
	color: var(--kwl-text);
	font-size: var(--kwl-fsize-small, clamp(0.78rem, 0.7rem + 0.3vw, 0.9rem));
	font-weight: 500;
	line-height: 1.35;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	line-clamp: 2;
	-webkit-box-orient: vertical;
	overflow: hidden;
	text-overflow: ellipsis;
	background: var(--kwl-bg);
}

/* Wenn keine Captions: rundes Bild ohne Footer-Padding. */
.kw-gallery:not(.kw-gallery--with-captions) .kw-gallery__figure {
	background: transparent;
	box-shadow: none;
	border-radius: var(--kwl-radius-md, 8px);
}

.kw-gallery:not(.kw-gallery--with-captions) .kw-gallery__img {
	border-radius: var(--kwl-radius-md, 8px);
	box-shadow: var(--kwl-shadow-sm, 0 1px 2px rgba(0,0,0,.05));
}

/* ─── Polaroid-Rahmen ────────────────────────────────────── */
/* Polaroid bleibt bewusst hell (Stil-Statement). Tokens verwenden, keine Hex. */

.kw-gallery[data-kw-frame="polaroid"] .kw-gallery__figure {
	background: var(--kwl-bg, #fff);
	padding: var(--kw-gal-frame-pad) var(--kw-gal-frame-pad) var(--kw-gal-frame-pad-bottom);
	border-radius: 3px;
	box-shadow:
		0 2px 4px color-mix(in oklch, black 8%, transparent),
		0 8px 16px color-mix(in oklch, black 6%, transparent);
}

.kw-gallery[data-kw-frame="polaroid"] .kw-gallery__img {
	border-radius: 1px;
}

.kw-gallery[data-kw-frame="polaroid"] .kw-gallery__caption {
	background: transparent;
	padding: 0.5rem 0 0;
	text-align: center;
	color: var(--kwl-text, #1a202c);
	font-family: var(--kwl-font-heading, inherit);
	font-style: italic;
}

/* ─── Hover-Lift ─────────────────────────────────────────── */

@media (hover: hover) and (prefers-reduced-motion: no-preference) {
	.kw-gallery--hover-lift .kw-gallery__link:hover .kw-gallery__figure,
	.kw-gallery--hover-lift .kw-gallery__link:focus-visible .kw-gallery__figure {
		transform: translateY(-3px);
		box-shadow: var(--kwl-shadow-hover, 0 8px 24px rgba(0,0,0,.12));
	}
}

/* ─── Empty/No-Results-Zustand ───────────────────────────── */

.kw-gallery--empty {
	padding: 2rem;
	text-align: center;
	color: var(--kwl-text-muted, #64748b);
	border: 1px dashed var(--kwl-border, #e2e8f0);
	border-radius: var(--kwl-radius-md, 8px);
}

.kw-gallery__empty {
	margin: 0;
	font-size: 1rem;
}

.kw-gallery__empty-hint {
	margin: 0.5rem 0 0;
	font-size: 0.85rem;
	color: var(--kwl-text-muted, #64748b);
}

.kw-gallery__no-results {
	margin: 1rem 0 0;
	padding: 1.5rem;
	text-align: center;
	color: var(--kwl-text-muted, #64748b);
	background: color-mix(in oklch, var(--kwl-border, #e2e8f0) 30%, transparent);
	border-radius: var(--kwl-radius-md, 8px);
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.85rem;
}

/* `display: flex` schlägt sonst den UA-Default `[hidden] { display: none }`
   (CSS-Doku: hidden/dialog + display:flex). Spezifität (0,2,0) reicht. */
.kw-gallery__no-results[hidden] {
	display: none;
}

.kw-gallery__no-results-text {
	margin: 0;
	font-size: var(--kwl-fsize-body, 1rem);
}

.kw-gallery__no-results-clear[hidden] {
	display: none;
}

/* ─── Forced Colors (Windows High Contrast) ──────────────── */

@media (forced-colors: active) {
	.kw-gallery__figure {
		border: 1px solid CanvasText;
	}
	.kw-gallery__pill[aria-pressed="true"] {
		border-width: 2px;
		border-color: Highlight;
		background: Highlight;
		color: HighlightText;
	}
}

/* ─── Helper: sr-only ────────────────────────────────────── */

.kw-gallery .kw-sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	border: 0;
	clip: rect(0 0 0 0);
	clip-path: inset(50%);
	overflow: hidden;
	white-space: nowrap;
}
