/**
 * EP 3D Viewer — front-end styles
 *
 * Visitor-facing, so this deliberately does not import the EP admin chrome.
 * Every colour is a custom property with a neutral fallback, so a theme can
 * restyle the viewer without touching the plugin:
 *
 *   .ep-3d-wrap { --ep-3d-accent: var(--brand-accent); }
 */

.ep-3d-wrap {
	--ep-3d-bg: #f4f3f0;
	--ep-3d-ink: #1c1b19;
	--ep-3d-accent: #2f6f5e;
	--ep-3d-on-accent: #ffffff;
	--ep-3d-radius: 10px;
	--ep-3d-hairline: rgba(28, 27, 25, 0.14);

	position: relative;
	margin: 0 0 1.5rem;
}

/* The server-rendered container. The runtime builds a <model-viewer> inside it
   — see js/ep-3d-viewer.js for why the element cannot be rendered server-side. */
.ep-3d-mount {
	position: relative;
	display: block;
	width: 100%;
	background-color: var(--ep-3d-bg);
	border-radius: var(--ep-3d-radius);
	overflow: hidden;
}

/* Label definitions are data carriers, never visible. Hidden here rather than
   in script so they cannot flash before the runtime boots. */
.ep-3d-spot {
	display: none;
}

/* What a visitor without JavaScript (or before it runs) actually sees. */
.ep-3d-fallback {
	display: block;
	width: 100%;
	height: 100%;
	margin: 0;
	object-fit: cover;
}

.ep-3d-fallback--text {
	display: flex;
	align-items: center;
	justify-content: center;
	min-height: inherit;
	padding: 2rem 1.5rem;
	text-align: center;
	font-size: 0.9rem;
	opacity: 0.6;
}

model-viewer.ep-3d {
	display: block;
	width: 100%;
	/* Set inline by the runtime; this is the floor for a caller who supplies
	   no height. */
	min-height: 240px;
	background-color: var(--ep-3d-bg);
	border-radius: var(--ep-3d-radius);
	overflow: hidden;
	/* Without this the browser scrolls the page when a touch drag starts on the
	   model, so one-finger orbit never works on a phone. */
	touch-action: pan-y;
	--poster-color: transparent;
}

.ep-3d-wrap--bordered .ep-3d-mount {
	box-shadow: inset 0 0 0 1px var(--ep-3d-hairline);
}

/* ── poster ─────────────────────────────────────────────────────────── */

model-viewer.ep-3d[poster]::part(default-poster) {
	background-size: cover;
	background-position: center;
}

.ep-3d-cta {
	position: absolute;
	left: 50%;
	bottom: 1rem;
	transform: translateX(-50%);
	z-index: 2;
	display: inline-flex;
	align-items: center;
	gap: 0.45em;
	padding: 0.5em 1em;
	border: 0;
	border-radius: 999px;
	background: var(--ep-3d-accent);
	color: var(--ep-3d-on-accent);
	font: inherit;
	font-size: 0.85rem;
	line-height: 1.2;
	cursor: pointer;
	box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
}

.ep-3d-cta:hover,
.ep-3d-cta:focus-visible {
	filter: brightness(1.08);
}

/* Labels are anchored to a model nobody can see yet, so they must not float
   over an empty box while it waits to be revealed. The runtime adds
   .ep-3d-shown when the model is actually on screen. */
.ep-3d-mount:not(.ep-3d-shown) .ep-3d-hotspot {
	opacity: 0;
	pointer-events: none;
}

/* ── progress ───────────────────────────────────────────────────────── */

.ep-3d-progress {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 0;
	height: 3px;
	background: var(--ep-3d-hairline);
	border-radius: 0 0 var(--ep-3d-radius) var(--ep-3d-radius);
	overflow: hidden;
	transition: opacity 0.3s ease 0.2s;
}

.ep-3d-progress-fill {
	display: block;
	height: 100%;
	background: var(--ep-3d-accent);
	transform: scaleX(0);
	transform-origin: left center;
	transition: transform 0.2s ease;
}

.ep-3d-progress--done {
	opacity: 0;
}

/* ── hotspots ───────────────────────────────────────────────────────── */

.ep-3d-hotspot {
	display: block;
	width: 22px;
	height: 22px;
	padding: 0;
	border: 2px solid var(--ep-3d-on-accent);
	border-radius: 50%;
	background: var(--ep-3d-accent);
	color: var(--ep-3d-on-accent);
	font: inherit;
	font-size: 11px;
	font-weight: 600;
	line-height: 18px;
	text-align: center;
	cursor: pointer;
	box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
	/* model-viewer positions hotspots by transform; anything that changes box
	   size on hover would make them drift, so only colour/shadow animate. */
	transition: background-color 0.15s ease, box-shadow 0.15s ease;
}

.ep-3d-hotspot:hover,
.ep-3d-hotspot:focus-visible {
	background: var(--ep-3d-ink);
	outline: none;
	box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.85), 0 1px 5px rgba(0, 0, 0, 0.35);
}

/* Occlusion, and why this is gated on a class.
 *
 * model-viewer sets data-visible on hotspots facing the camera when they carry
 * data-visibility-attribute="visible", so hiding :not([data-visible]) puts the
 * labels on the far side of the model out of sight — which is what you want on
 * an anatomical or technical reference.
 *
 * But absence of that attribute means two different things: "rotated round the
 * back" and "nothing has computed visibility yet". Hiding on absence alone
 * fails CLOSED — in any situation where model-viewer's render loop does not
 * run (a background tab is the everyday one), every label is invisible with no
 * way back. So the runtime adds .ep-3d-managed only once it has actually seen
 * a hotspot gain data-visible, and until then the labels simply show. */
.ep-3d-managed .ep-3d-hotspot:not([data-visible]) {
	opacity: 0;
	pointer-events: none;
}

.ep-3d-annotation {
	position: absolute;
	left: 50%;
	bottom: calc(100% + 9px);
	transform: translateX(-50%);
	min-width: 6em;
	max-width: 15em;
	width: max-content;
	padding: 0.4em 0.65em;
	border-radius: 6px;
	background: var(--ep-3d-ink);
	color: #fff;
	font-size: 0.78rem;
	font-weight: 400;
	line-height: 1.35;
	text-align: left;
	opacity: 0;
	visibility: hidden;
	transition: opacity 0.15s ease;
	pointer-events: none;
}

.ep-3d-annotation::after {
	content: "";
	position: absolute;
	top: 100%;
	left: 50%;
	transform: translateX(-50%);
	border: 5px solid transparent;
	border-top-color: var(--ep-3d-ink);
}

.ep-3d-hotspot:hover .ep-3d-annotation,
.ep-3d-hotspot:focus-visible .ep-3d-annotation,
.ep-3d-hotspot[aria-expanded="true"] .ep-3d-annotation {
	opacity: 1;
	visibility: visible;
}

/* ── caption, failure, AR ───────────────────────────────────────────── */

.ep-3d-caption {
	margin: 0.6rem 0 0;
	font-size: 0.85rem;
	line-height: 1.45;
	opacity: 0.75;
}

.ep-3d-ar {
	position: absolute;
	right: 0.75rem;
	bottom: 0.75rem;
	z-index: 2;
	display: inline-flex;
	align-items: center;
	gap: 0.4em;
	padding: 0.45em 0.85em;
	border: 1px solid var(--ep-3d-hairline);
	border-radius: 999px;
	background: rgba(255, 255, 255, 0.92);
	color: var(--ep-3d-ink);
	font: inherit;
	font-size: 0.8rem;
	cursor: pointer;
}

/* Appended by the runtime only when the model actually fails, so it needs no
   show/hide rule — its presence is the signal. */
.ep-3d-error {
	position: absolute;
	inset: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 0;
	padding: 1.5rem;
	text-align: center;
	font-size: 0.9rem;
	line-height: 1.5;
	opacity: 0.7;
}

@media (prefers-reduced-motion: reduce) {
	.ep-3d-progress-fill,
	.ep-3d-annotation,
	.ep-3d-hotspot {
		transition: none;
	}
}
