:root {
	--bg: #0d0f14;
	--panel: rgba(22, 25, 33, 0.82);
	--panel-border: rgba(255, 255, 255, 0.08);
	--text: #e6ebf2;
	--text-dim: #9aa4b2;
	--accent: #4db2ff;
	--accent-soft: rgba(77, 178, 255, 0.18);
	--radius: 12px;
	--gutter: 12px;
	--toolbar-height: 60px;
	--safe-top: env(safe-area-inset-top, 0px);
	--safe-right: env(safe-area-inset-right, 0px);
	--safe-bottom: env(safe-area-inset-bottom, 0px);
	--safe-left: env(safe-area-inset-left, 0px);
	--font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	color-scheme: dark;
}

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

html,
body {
	height: 100%;
	margin: 0;
	overflow: hidden;
	background: var(--bg);
	color: var(--text);
	font-family: var(--font);
	/* Stop pull-to-refresh and rubber-banding from hijacking gestures on mobile. */
	overscroll-behavior: none;
	-webkit-tap-highlight-color: transparent;
	-webkit-text-size-adjust: 100%;
}

/* ------------------------------------------------------------------ canvas */

.stage {
	position: fixed;
	inset: 0;
	touch-action: none; /* All gestures are handled in JavaScript. */
	user-select: none;
	-webkit-user-select: none;
	-webkit-touch-callout: none;
	cursor: crosshair;
}

.stage__layer {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	display: block;
}

#overlay {
	pointer-events: none;
}

/* --------------------------------------------------------------------- HUD */

.hud {
	position: fixed;
	top: calc(var(--gutter) + var(--safe-top));
	left: calc(var(--gutter) + var(--safe-left));
	/* Fixed width: changing numbers must never resize the panel. */
	width: min(310px, calc(100vw - 2 * var(--gutter)));
	padding: 10px 12px;
	border-radius: var(--radius);
	background: var(--panel);
	border: 1px solid var(--panel-border);
	backdrop-filter: blur(8px);
	-webkit-backdrop-filter: blur(8px);
	font-size: 13px;
	line-height: 1.35;
	pointer-events: none;
	z-index: 2;
}

.hud button,
.hud a {
	pointer-events: auto;
}

.hud__title-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 12px;
}

.hud__title {
	margin: 0;
	font-size: 15px;
	font-weight: 600;
	letter-spacing: 0.01em;
}

/* Wrap "N-body Gravity" between the words, never at the hyphen. */
.nowrap {
	white-space: nowrap;
}

/* The starting hints: one gesture per line, struck through as you do each. */
.hud__hint {
	display: grid;
	grid-template-columns: auto 1fr;
	gap: 3px 10px;
	margin: 8px 0 0;
	padding: 0;
	list-style: none;
	color: var(--text-dim);
	font-size: 12px;
	transition: opacity 600ms ease 600ms;
}

.hud__hint[hidden] {
	display: none;
}

/* Everything done: fade away. */
.hud__hint--finished {
	opacity: 0;
}

.hud__hint li {
	display: grid;
	grid-column: 1 / -1;
	grid-template-columns: subgrid;
}

.hud__hint-gesture {
	color: var(--text);
	font-weight: 500;
}

.hud__hint--done span {
	text-decoration: line-through;
	opacity: 0.5;
}

/* Read by screen readers, not shown. */
.visually-hidden {
	position: absolute;
	width: 1px;
	height: 1px;
	overflow: hidden;
	clip-path: inset(50%);
	white-space: nowrap;
}

.hud__stats {
	display: grid;
	gap: 2px;
	margin: 8px 0 0;
	font-variant-numeric: tabular-nums;
}

.hud__stats div {
	display: flex;
	justify-content: space-between;
	gap: 12px;
}

.hud__stats dt {
	color: var(--text-dim);
}

.hud__stats dd {
	margin: 0;
	text-align: right;
	white-space: nowrap;
}

.hud__value--slow {
	color: #ffb454;
}

.hud__stats abbr {
	text-decoration: none;
	cursor: help;
	pointer-events: auto;
}

.hud--compact .hud__stats {
	display: none;
}

/* ----------------------------------------------------------------- buttons */

.icon-button {
	display: inline-grid;
	place-items: center;
	width: 44px;
	height: 44px;
	padding: 0;
	border: 0;
	border-radius: 10px;
	background: transparent;
	color: var(--text);
	font: 600 16px/1 var(--font);
	cursor: pointer;
	transition:
		background-color 120ms ease,
		color 120ms ease;
}

.icon-button:hover {
	background: rgba(255, 255, 255, 0.08);
}

.icon-button:focus-visible,
.help__close:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 2px;
}

.icon-button[aria-pressed="true"],
.icon-button[aria-expanded="true"] {
	background: var(--accent-soft);
	color: var(--accent);
}

/* The pause button shows "play" while paused, without the "selected" tint. */
.icon-button[data-action="pause"][aria-pressed="true"] {
	background: transparent;
	color: var(--text);
}

.icon-button[hidden] {
	display: none;
}

.icon-button--small {
	width: 26px;
	height: 26px;
	border-radius: 50%;
	font-size: 14px;
	background: rgba(255, 255, 255, 0.08);
}

.icon {
	width: 22px;
	height: 22px;
	fill: currentColor;
}

.icon--stroke {
	fill: none;
	stroke: currentColor;
	stroke-width: 1.8;
	stroke-linecap: round;
	stroke-linejoin: round;
}

.icon__fill {
	fill: currentColor;
}

.icon--play,
[aria-pressed="true"] > .icon--pause {
	display: none;
}

[aria-pressed="true"] > .icon--play {
	display: block;
}

/* ----------------------------------------------------------------- toolbar */

.toolbar {
	position: fixed;
	left: 50%;
	bottom: calc(var(--gutter) + var(--safe-bottom));
	transform: translateX(-50%);
	display: flex;
	align-items: center;
	gap: 4px;
	max-width: calc(100vw - 2 * var(--gutter) - var(--safe-left) - var(--safe-right));
	padding: 6px;
	border-radius: 16px;
	background: var(--panel);
	border: 1px solid var(--panel-border);
	backdrop-filter: blur(8px);
	-webkit-backdrop-filter: blur(8px);
	box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
	z-index: 3;
	overflow-x: auto;
	scrollbar-width: none;
}

.toolbar__group {
	display: flex;
	gap: 2px;
	padding: 0 4px;
	margin: 0 2px;
	border-inline: 1px solid var(--panel-border);
}

/* Scenario and Add: an icon for what's selected. */
.menu-button__icon {
	position: relative;
	display: inline-grid;
	place-items: center;
}

/* The "+" on the Add button's icon. */
.menu-button__badge {
	position: absolute;
	right: -6px;
	bottom: -5px;
	width: 13px;
	height: 13px;
	border-radius: 50%;
	background: var(--accent);
	fill: none;
	stroke: var(--bg);
	stroke-width: 2;
	stroke-linecap: round;
}

/* ------------------------------------------------------------ toolbar menus */

.toolbar-menu {
	position: fixed;
	inset: auto auto calc(var(--toolbar-height) + 2 * var(--gutter) + var(--safe-bottom)) 50%;
	transform: translateX(-50%);
	width: min(340px, calc(100vw - 2 * var(--gutter)));
	max-height: min(70dvh, 600px);
	margin: 0;
	padding: 0;
	overflow-y: auto;
	border: 1px solid var(--panel-border);
	border-radius: var(--radius);
	background: rgb(22, 25, 33);
	color: var(--text);
	box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
	font: 13px/1.35 var(--font);
}

.toolbar-menu__hint {
	margin: 0;
	padding: 10px 14px 6px;
	color: var(--text-dim);
	font-size: 12px;
}

.toolbar-menu__list {
	margin: 0;
	padding: 4px;
	list-style: none;
}

.toolbar-menu__item {
	display: grid;
	grid-template-columns: 24px 1fr;
	align-items: center;
	gap: 12px;
	width: 100%;
	padding: 7px 10px;
	border: 0;
	border-radius: 8px;
	background: transparent;
	color: var(--text);
	font: inherit;
	text-align: left;
	cursor: pointer;
}

.toolbar-menu__item:hover {
	background: rgba(255, 255, 255, 0.07);
}

/* What's selected: the running scenario, or what clicks add. */
.toolbar-menu__item[aria-current="true"] {
	background: var(--accent-soft);
	color: var(--accent);
}

.toolbar-menu__item .icon {
	width: 24px;
	height: 24px;
}

.toolbar-menu__text {
	display: grid;
	min-width: 0;
}

.toolbar-menu__name {
	font-weight: 600;
}

.toolbar-menu__description {
	overflow: hidden;
	color: var(--text-dim);
	font-size: 12px;
	white-space: nowrap;
	text-overflow: ellipsis;
}

.toolbar-menu button:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: -2px;
}

/* ------------------------------------------------------------ settings GUI */

.lil-gui.lil-root {
	--background-color: rgba(22, 25, 33, 0.92);
	--widget-color: #2a2f3a;
	--hover-color: #343a47;
	--focus-color: #3d4452;
	--number-color: var(--accent);
	--string-color: #a2db3c;
	--font-family: var(--font);
	border-radius: var(--radius);
	border: 1px solid var(--panel-border);
	backdrop-filter: blur(8px);
	-webkit-backdrop-filter: blur(8px);
	overflow: hidden;
}

/* The panel is a scrolling flex column: keep its title bar from being squeezed. */
.lil-gui.lil-root > .lil-title {
	flex-shrink: 0;
}

/* Randomize and Reset, over the right end of the panel's title bar. */
.panel-actions {
	position: absolute;
	top: 0;
	right: 6px;
	height: var(--title-height);
	display: flex;
	align-items: center;
	gap: 4px;
}

.panel-action {
	height: 22px;
	white-space: nowrap;
	padding: 0 8px;
	border: 0;
	border-radius: 6px;
	background: rgba(255, 255, 255, 0.08);
	color: var(--text);
	font: 500 12px/1 var(--font);
	cursor: pointer;
}

.panel-action:hover {
	background: rgba(255, 255, 255, 0.14);
}

.panel-action:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 1px;
}

.lil-gui.autoPlace {
	top: calc(var(--gutter) + var(--safe-top));
	right: calc(var(--gutter) + var(--safe-right));
	max-height: calc(
		100dvh - var(--toolbar-height) - 3 * var(--gutter) - var(--safe-top) - var(--safe-bottom)
	);
	overflow-y: auto;
	z-index: 4;
}

/* Phones and short screens: the panel becomes a bottom sheet above the toolbar. */
@media (max-width: 700px), (max-height: 500px) {
	:root {
		--gutter: 8px;
	}

	.lil-gui.autoPlace {
		top: auto;
		left: calc(var(--gutter) + var(--safe-left));
		right: calc(var(--gutter) + var(--safe-right));
		bottom: calc(var(--toolbar-height) + 2 * var(--gutter) + var(--safe-bottom));
		width: auto !important;
		max-height: 55dvh;
	}

	.hud {
		font-size: 12px;
		padding: 8px 10px;
	}

	.hud__title {
		font-size: 14px;
	}

	.icon-button {
		width: 42px;
		height: 42px;
	}

	/* Five buttons beside the title. */
	.hud .icon-button {
		width: 38px;
		height: 38px;
	}

	.hud__actions {
		gap: 4px;
	}
}

/* Narrow phones: tighter spacing so all eight toolbar buttons fit while paused. */
@media (max-width: 400px) {
	.icon-button {
		width: 40px;
		height: 40px;
	}

	.toolbar {
		gap: 2px;
		padding: 4px;
	}

	.toolbar__group {
		padding: 0 2px;
		margin: 0 1px;
	}
}

@media (max-width: 360px) {
	.icon-button {
		width: 38px;
		height: 38px;
	}

	.toolbar__group {
		padding: 0 2px;
	}
}

/* -------------------------------------------------------------------- help */

.help {
	width: min(560px, calc(100vw - 2 * var(--gutter)));
	max-height: calc(100dvh - 2 * var(--gutter) - var(--safe-top) - var(--safe-bottom));
	padding: 0;
	border: 1px solid var(--panel-border);
	border-radius: var(--radius);
	background: #161a22;
	color: var(--text);
	box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.help::backdrop {
	background: rgba(5, 6, 10, 0.6);
	backdrop-filter: blur(2px);
}

.help__body {
	padding: 20px 22px;
	font-size: 14px;
	line-height: 1.5;
}

.help h2 {
	margin: 0 0 8px;
	font-size: 20px;
}

.help h3 {
	margin: 18px 0 6px;
	font-size: 14px;
	text-transform: uppercase;
	letter-spacing: 0.06em;
	color: var(--text-dim);
}

/* The help shows only the input sections for this device. */
:root[data-pointer="touch"] .help [data-for="mouse"],
:root[data-pointer="mouse"] .help [data-for="touch"] {
	display: none;
}

.help ul {
	margin: 0;
	padding-left: 18px;
}

.help a {
	color: var(--accent);
}

.help__keys {
	display: grid;
	grid-template-columns: max-content 1fr;
	gap: 4px 14px;
	margin: 0;
}

.help__keys dd {
	margin: 0;
}

kbd {
	display: inline-block;
	min-width: 1.6em;
	padding: 1px 6px;
	border: 1px solid var(--panel-border);
	border-bottom-width: 2px;
	border-radius: 5px;
	background: #222834;
	font:
		12px/1.4 ui-monospace,
		SFMono-Regular,
		Menlo,
		monospace;
	text-align: center;
}

.help__close {
	display: block;
	margin: 20px 0 0 auto;
	padding: 10px 20px;
	border: 0;
	border-radius: 8px;
	background: var(--accent);
	color: #04121f;
	font: 600 14px var(--font);
	cursor: pointer;
}

/* ------------------------------------------------------------ toast, fatal */

.toast {
	position: fixed;
	left: 50%;
	bottom: calc(var(--toolbar-height) + 2 * var(--gutter) + var(--safe-bottom) + 8px);
	transform: translate(-50%, 8px);
	padding: 8px 14px;
	border-radius: 999px;
	background: rgba(22, 25, 33, 0.92);
	border: 1px solid var(--panel-border);
	font-size: 13px;
	white-space: nowrap;
	max-width: calc(100vw - 2 * var(--gutter));
	overflow: hidden;
	text-overflow: ellipsis;
	opacity: 0;
	pointer-events: none;
	transition:
		opacity 160ms ease,
		transform 160ms ease;
	z-index: 5;
}

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

.fatal {
	position: fixed;
	inset: auto 16px 50% 16px;
	margin: 0;
	padding: 16px;
	border-radius: var(--radius);
	background: #3a1d22;
	color: #ffd7dc;
	text-align: center;
	z-index: 10;
}

@media (prefers-reduced-motion: reduce) {
	.toast,
	.icon-button,
	.hud__hint {
		transition: none;
	}
}

/* ------------------------------------------------- HUD actions, recording */

.hud__actions {
	display: flex;
	gap: 6px;
}

.icon--small {
	width: 15px;
	height: 15px;
}

.icon-button[data-action="record"] {
	color: #ff6b6b;
}

.icon-button[data-action="record"][aria-pressed="true"] {
	background: rgba(255, 107, 107, 0.25);
}

.rec {
	position: fixed;
	top: calc(var(--gutter) + var(--safe-top));
	left: 50%;
	transform: translateX(-50%);
	display: inline-flex;
	align-items: center;
	gap: 6px;
	padding: 6px 12px;
	border: 1px solid rgba(255, 107, 107, 0.5);
	border-radius: 999px;
	background: rgba(40, 14, 18, 0.88);
	color: #ffd7dc;
	font: 600 13px var(--font);
	font-variant-numeric: tabular-nums;
	cursor: pointer;
	z-index: 6;
}

.rec[hidden] {
	display: none;
}

.rec__dot {
	width: 9px;
	height: 9px;
	border-radius: 50%;
	background: #ff4d4d;
	animation: rec-pulse 1s ease-in-out infinite alternate;
}

.rec__hint {
	color: rgba(255, 215, 220, 0.7);
	font-weight: 400;
}

@keyframes rec-pulse {
	to {
		opacity: 0.35;
	}
}

.recording__media {
	display: block;
	width: 100%;
	max-height: 55dvh;
	object-fit: contain;
	border-radius: 8px;
	background: var(--bg);
}

.recording__media[hidden] {
	display: none;
}

.recording__actions {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 8px;
}

.recording__actions .help__close {
	margin: 0 0 0 auto;
}

.pill-button {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	min-height: 36px;
	padding: 8px 14px;
	border: 1px solid var(--panel-border);
	border-radius: 999px;
	background: rgba(255, 255, 255, 0.06);
	color: var(--text);
	font: 500 13px var(--font);
	text-decoration: none;
	cursor: pointer;
}

.pill-button:hover {
	background: rgba(255, 255, 255, 0.12);
}

.pill-button:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 2px;
}

.pill-button[aria-pressed="true"] {
	background: var(--accent-soft);
	border-color: var(--accent);
	color: var(--accent);
}

.pill-button--danger {
	color: #ff9b9b;
}

.pill-button[hidden] {
	display: none;
}

/* --------------------------------------------------------------- inspector */

.inspector {
	position: fixed;
	left: calc(var(--gutter) + var(--safe-left));
	bottom: calc(var(--toolbar-height) + 2 * var(--gutter) + var(--safe-bottom));
	width: min(280px, calc(100vw - 2 * var(--gutter)));
	padding: 10px 12px 12px;
	border-radius: var(--radius);
	background: var(--panel);
	border: 1px solid var(--panel-border);
	backdrop-filter: blur(8px);
	-webkit-backdrop-filter: blur(8px);
	font-size: 13px;
	z-index: 4;
}

.inspector[hidden] {
	display: none;
}

.inspector__header {
	display: flex;
	align-items: center;
	gap: 8px;
}

.inspector__swatch {
	width: 14px;
	height: 14px;
	border-radius: 50%;
	box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.15);
}

.inspector__title {
	flex: 1;
	margin: 0;
	font-size: 14px;
	font-weight: 600;
}

.inspector__fields {
	display: grid;
	grid-template-columns: max-content 1fr;
	gap: 2px 12px;
	margin: 8px 0 10px;
	font-variant-numeric: tabular-nums;
}

.inspector__fields dt[title]:not([title=""]),
#settings-panel .lil-controller[title] > .lil-name {
	cursor: help;
}

.inspector__fields dt {
	color: var(--text-dim);
}

.inspector__fields dd {
	margin: 0;
}

.inspector__actions {
	display: flex;
	gap: 8px;
}

.inspector__actions .pill-button {
	flex: 1;
}

/* Sound button: speaker with waves when on, a cross when off. */
[data-action="sound"] .icon__sound-off,
[data-action="sound"][aria-pressed="false"] .icon__sound-on {
	display: none;
}

[data-action="sound"][aria-pressed="false"] .icon__sound-off {
	display: inline;
}

/* The pressed tint is for tools; this button shows its state with its icon. */
.hud .icon-button[data-action="sound"][aria-pressed="true"] {
	background: rgba(255, 255, 255, 0.08);
	color: var(--text);
}
