:root {
	color-scheme: light dark;

	/* Page */
	--page-background: #e8ebf0;
	--page-text: #14171f;
	--page-text-muted: #5b6474;
	--page-padding: 1.5rem;

	/* Surfaces (cards, panels) */
	--surface: #ffffff;
	--surface-text: #14171f;
	--surface-text-muted: #5b6474;
	--surface-divider: #e3e6ec;
	--surface-hover: #f1f3f7;

	/* Nav / headers / buttons */
	--nav: #1d2330;
	--nav-text: #ffffff;
	--nav-text-muted: #9aa4b5;
	--nav-text-submuted: #3a4356;
	--nav-height: 6rem;

	/* Form fields */
	--field: #eef0f5;
	--field-focus: #e0e4ec;
	--field-text: #14171f;
	--field-placeholder: #6b7383;
	--field-autofill: var(--field-text);
	--field-autofill-text: var(--field);

	/* Feedback */
	--error: #c62828;
	--focus-ring: #000000;
	--skeleton-base: #e3e6ec;
	--skeleton-shine: #cfd4dd;

	--nav-shadow: 0 -0.125rem 0.5rem #0000004d;

	/* Corner clipping. */
	--corner-path-large: polygon(
		0 0,
		calc(100% - 1.5rem) 0,
		100% 1.5rem,
		100% 100%,
		1.5rem 100%,
		0 calc(100% - 1.5rem)
	);

	--corner-path-small: polygon(
		0 0,
		calc(100% - 0.5rem) 0,
		100% 0.5rem,
		100% 100%,
		0.5rem 100%,
		0 calc(100% - 0.5rem)
	);

	--corner-path-small-left: polygon(
		0 0,
		100% 0,
		100% 100%,
		0.5rem 100%,
		0 calc(100% - 0.5rem)
	);

	--corner-path-small-025: polygon(
		0 0,
		calc(100% - 0.354rem) 0,
		100% 0.354rem,
		100% 100%,
		0.354rem 100%,
		0 calc(100% - 0.354rem)
	);

	--corner-path-small-avoid-right: polygon(
		0 0,
		calc(100% - 1.061rem) 0,
		100% 1.061rem,
		100% 100%,
		0.5rem 100%,
		0 calc(100% - 0.5rem)
	);

	--corner-path-small-avoid-left: polygon(
		0 0,
		calc(100% - 0.5rem) 0,
		100% 0.5rem,
		100% 100%,
		1.061rem 100%,
		0 calc(100% - 1.061rem)
	);
}

@media (prefers-color-scheme: dark) {
	:root {
		--page-background: #222222;
		--page-text: #ffffff;
		--page-text-muted: #888888;

		--surface: #111111;
		--surface-text: #ffffff;
		--surface-text-muted: #8c8c8c;
		--surface-divider: #222222;
		--surface-hover: #1a1a1a;

		--nav: #000000;
		--nav-text: #ffffff;
		--nav-text-muted: #777777;
		--nav-text-submuted: #343434;

		--field: #262626;
		--field-focus: #363636;
		--field-text: #ffffff;
		--field-placeholder: #888888;

		--error: #ff5c5c;
		--focus-ring: #ffffff;
		--skeleton-base: #222222;
		--skeleton-shine: #343434;

		--nav-shadow: 0 -0.125rem 0.5rem #ffffff1a;
	}
}

/* Pages */
.page {
	position: absolute;
	inset: 0;
	overflow: auto;

	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: safe center;
	gap: 1rem;

	padding: var(--page-padding);
	padding-bottom: calc(var(--nav-height) + var(--page-padding)); /* clear the fixed nav */

	background: var(--page-background);
	color: var(--page-text);
}

.element { box-sizing: border-box; }

.element-container,
.element-section,
.element-card {
	display: flex;
	flex-direction: column;
}

.element-text { white-space: pre-wrap; }

/* Images: a wrapper (styled by variant) around the real <img>, which stays hidden until loaded. */
.element-image { display: block; }

.element-image > img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: inherit;
	border: 0;
	outline: 0;
}

/* Color images: an accent-colored backdrop (styled by variant) that shows through
   until the real image has loaded, then fades it in. */
.element-color-image { display: block; }

.element-color-image .color-image-inner {
	display: block;
	width: 100%;
	height: 100%;
	overflow: hidden;
}

.element-color-image img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
	object-position: center;
	border: 0;
	outline: 0;
	opacity: 0;
	transition: opacity 0.15s ease;
}

.element-color-image[data-state="loaded"] img { opacity: 1; }

.element-image:not([data-state="loaded"]) > img { visibility: hidden; }

.element-logo { display: block; color: inherit; }

.element-logo > svg {
	display: block;
	width: 100%;
	height: 100%;
}

/* Buttons: one look, everywhere */

:where(.element-button) {
	cursor: pointer;
	background: transparent;
	color: inherit;
	transition: transform 0.15s ease, opacity 0.15s ease;
}

.element-button:hover { transform: scale(1.1); }
.element-button:active { transform: scale(1.05); }

.element-button[data-muted] {
	cursor: default;
	opacity: 0.5;
	pointer-events: none;
}

[data-variant~="primary"] {
	align-self: center;
	padding: 0.4rem 1.25rem;
	background: var(--nav);
	color: var(--nav-text);
	clip-path: var(--corner-path-small);
}

[data-variant~="square"] {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	flex: 0 0 auto;
	width: 2rem;
	height: 2rem;
	padding: 0;
}

.element-icon {
	display: block;
	width: 1rem;
	height: 1rem;
	pointer-events: none;
}

[data-variant~="primary"]:focus-visible { outline: none; box-shadow: inset 0 0 0 0.15rem var(--focus-ring); }

/* Layout / text variants */

[data-variant="centered-column"] { align-items: center; gap: 1rem; }
[data-variant="title"] { font-size: 1.5rem; font-weight: 600; }
[data-variant="body"] { font-size: 1rem; }
[data-variant="muted"] { color: var(--page-text-muted); font-size: 1rem; }

[data-variant="error"] {
	color: var(--error);
	font-size: 0.95rem;
	font-weight: 500;
	min-height: 1.2em;
	text-align: center;
}

/* Inputs */

input[type="text"],
input[type="password"] {
	display: block;
	width: 100%;
	padding: 0.6rem;
	background: var(--field);
	color: var(--field-text);
	caret-color: var(--field-text);
	clip-path: var(--corner-path-small);
	outline: none;
	transition: none;
}

input[type="text"]:focus,
input[type="password"]:focus {
	background: var(--field-focus);
}

input::placeholder { color: var(--field-placeholder); opacity: 1; }

input[type="file"] {
	display: block;
	width: 100%;
	padding: 0.75rem;
	background: var(--field);
	color: var(--field-text);
	clip-path: var(--corner-path-large);
	outline: none;
}

/* Match the app's primary-button look for the native "Browse"/"Choose files" control.
   Both pseudo-elements are needed: Firefox/Chromium use ::file-selector-button,
   older WebKit/Safari uses ::-webkit-file-upload-button. */
input[type="file"]::file-selector-button,
input[type="file"]::-webkit-file-upload-button {
	cursor: pointer;
	margin-right: 0.75rem;
	padding: 0.4rem 1.25rem;
	border: none;
	background: var(--nav);
	color: var(--nav-text);
	font: inherit;
	clip-path: var(--corner-path-small-avoid-left);
	transition: transform 0.15s ease, opacity 0.15s ease;
}
input[type="file"]::file-selector-button:hover,
input[type="file"]::-webkit-file-upload-button:hover {
	transform: scale(1.05);
}
input[type="file"]::file-selector-button:active,
input[type="file"]::-webkit-file-upload-button:active {
	transform: scale(1);
}

/*
 * Autofilled fields invert the field colours. It's painted with an inset
 * shadow, which sits above the browser's colour and disappears instantly when
 * the field stops being autofilled. Two separate rules: an unknown pseudo-class
 * would invalidate a combined selector list.
 */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
	-webkit-box-shadow: 0 0 0 100vmax var(--field-autofill) inset;
	-webkit-text-fill-color: var(--field-autofill-text);
	caret-color: var(--field-autofill-text);
}

input:autofill,
input:autofill:hover,
input:autofill:focus,
input:autofill:active {
	box-shadow: 0 0 0 100vmax var(--field-autofill) inset;
	-webkit-text-fill-color: var(--field-autofill-text);
	caret-color: var(--field-autofill-text);
}

/* Card */

[data-variant="card"] {
	width: min(90%, 20rem);
	background: var(--surface);
	color: var(--surface-text);
	clip-path: var(--corner-path-large);
	overflow: hidden;
}

[data-variant="card-header"] {
	flex-direction: row;
	align-items: center;
	gap: 0.75rem;
	padding: 1rem;
	background: var(--nav);
	color: var(--nav-text);
}

[data-variant="card-logo"] { flex: 0 0 auto; width: 2rem; height: 2rem; object-fit: contain; }
[data-variant="card-title"] { font-size: 1.5rem; font-weight: 600; white-space: nowrap; }

[data-variant="card-body"] { gap: 0.5rem; padding: 2rem; }
[data-variant="card-body"] [data-variant~="primary"] { margin-top: 1rem; }
[data-variant="card-body-centered"] { align-items: center; gap: 0.75rem; padding: 2rem; }

[data-variant="field-label"] { color: var(--surface-text); font-size: 1rem; line-height: 1.2; }

[data-variant="card-value"] {
	font-size: 1.25rem;
	font-weight: 600;
	text-align: center;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	max-width: 100%;
}

/* Inside a card, "muted" should follow the surface, not the page */
[data-variant="card"] [data-variant="muted"] { color: var(--surface-text-muted); }

/* Popups */

.element-popup { z-index: 100; }

/* Modal: backdrop + centred panel */
.element-popup[data-mode="modal"] {
	position: fixed;
	inset: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 1rem;
	box-sizing: border-box;
	background: #00000099;
	visibility: hidden;
	opacity: 0;
	transition: opacity 0.2s ease, visibility 0s linear 0.2s;
}
.element-popup[data-mode="modal"][data-open] {
	visibility: visible;
	opacity: 1;
	transition-delay: 0s;
}
.element-popup[data-mode="modal"] > .popup-panel {
	display: flex;
	flex-direction: column;
	max-width: 100%;
	max-height: 100%;
	min-height: 0;
	transform: scale(0.96);
	transition: transform 0.2s ease;
}
.element-popup[data-mode="modal"][data-open] > .popup-panel { transform: none; }

/* Menu popup */
.element-popup[data-mode="menu"] {
	display: contents;
}

.element-popup[data-mode="menu"] > .popup-panel {
	position: fixed;
	z-index: 110;
	box-sizing: border-box;

	min-width: 12rem;
	max-width: 20rem;

	background: #000;
	color: #fff;

	clip-path: var(--corner-path-large);

	visibility: hidden;
	pointer-events: none;

	transition: visibility 0s linear 0.2s;
}

.element-popup[data-mode="menu"][data-open] > .popup-panel {
	visibility: visible;
	pointer-events: auto;
	transition-delay: 0s;
}

.popup-menu-wrapper {
	display: grid;
	grid-template-rows: 0fr;
	transition: grid-template-rows 0.2s ease;

	/* The zero-height track clips the overflowing list. */
	overflow: hidden;
}

.element-popup[data-mode="menu"][data-open] > .popup-panel > .popup-menu-wrapper {
	grid-template-rows: 1fr;
}

.popup-menu-list {
	/*
	 * Keep the list at its intrinsic height even when the grid track is
	 * collapsed to 0fr. The wrapper, not the list, does the clipping.
	 */
	height: max-content;
	min-height: 0;
	overflow: visible;

	display: flex;
	flex-direction: column;
}

.popup-menu-list > * {
	flex: 0 0 auto;
}

/*
 * Downward-opening menu:
 * pin the full-height list to the bottom of the growing track.
 *
 * At 0fr the list therefore sits completely above the visible area.
 * As the track grows downward, the list moves downward with it.
 */
.element-popup[data-mode="menu"]
	> .popup-panel[data-flip="down"]
	> .popup-menu-wrapper
	> .popup-menu-list {
	align-self: end;
}

/*
 * Upward-opening menu:
 * pin the list to the top of the track.
 *
 * The panel itself grows upward because `position()` uses `bottom`,
 * so the list moves upward with the panel.
 */
.element-popup[data-mode="menu"]
	> .popup-panel[data-flip="up"]
	> .popup-menu-wrapper
	> .popup-menu-list {
	align-self: start;
}

.popup-menu-divider {
	height: 1px;
	flex: 0 0 1px;
	margin: 0 1.5rem;
	background: #ffffff26;
}

/* A divider sits before the item it separates (see popup.js). If that item is
   hidden - e.g. a permission-gated Edit/Upload row - the divider would otherwise
   be left dangling on its own, so hide it along with the item. */
.popup-menu-divider:has(+ [hidden]) {
	display: none;
}

/*
 * Menu item: the default look for anything placed inside a menu-mode popup.
 * This was originally introduced (and styled) only for Resonance's audio/
 * playlist context menus, but a dark, full-width, centered menu row is the
 * generic look any popup menu should have -- so it now lives here as the
 * default `menu-item` variant, scoped to the popup itself rather than to
 * any one page.
 */
.element-popup[data-mode="menu"] [data-variant~="menu-item"] {
	appearance: none;
	-webkit-appearance: none;
	box-sizing: border-box;

	display: flex;
	align-items: center;
	justify-content: center;

	width: 100%;
	min-width: 0;
	margin: 0;
	padding: 0.6rem 1rem;

	border: 0;
	border-radius: 0;

	font: inherit;
	text-align: center;
	white-space: normal;
	user-select: none;

	background: transparent;
	color: #fff;

	/* Explicitly override the global ButtonElement transform. */
	transform: none;

	transition:
		background-color 0.2s ease,
		transform 0.15s ease,
		opacity 0.15s ease;
}

.element-popup[data-mode="menu"] [data-variant~="menu-item"]:hover {
	background: #181818;
	transform: scale(1.07);
}

.element-popup[data-mode="menu"] [data-variant~="menu-item"]:active {
	background: #ffffff14;
	transform: scale(1.05);
}

.element-popup[data-mode="menu"] [data-variant~="menu-item"] > span {
	display: inline-block;
	transition: transform 0.15s ease;
}

.element-popup[data-mode="menu"] [data-variant~="menu-item"]:active > span {
	transform: scale(0.9);
}

.element-popup[data-mode="menu"] [data-variant~="menu-item"]:focus-visible {
	outline: none;
	box-shadow: inset 0 0 0 0.15rem var(--focus-ring);
}