:root {
	color-scheme: light;
	font-family:
		Inter,
		ui-sans-serif,
		system-ui,
		-apple-system,
		BlinkMacSystemFont,
		"Segoe UI",
		sans-serif;
	font-synthesis: none;
	--ink: #24203b;
	--muted: #655f7b;
	--surface: rgba(255, 255, 255, 0.9);
	--border: rgba(74, 57, 111, 0.15);
	--purple: #6842b8;
	--purple-dark: #4d2c93;
	--mint: #b9f6d2;
	--shadow: 0 24px 70px rgba(69, 43, 112, 0.16);
	/* JavaScript overrides these values as the board and effects are created. */
	--columns: 4;
	--suck-rotation: 0deg;
	--confetti-color: #f5b82e;
	--confetti-drift: 0px;
	--confetti-drift-mid: 0px;
	--confetti-rise: -100px;
	--confetti-fall: 300px;
	--confetti-spin: 360deg;
	--confetti-spin-mid: 180deg;
	--confetti-delay: 0ms;
	--confetti-duration: 2000ms;
}

* {
	box-sizing: border-box;
}

body {
	min-width: 320px;
	min-height: 100vh;
	margin: 0;
	color: var(--ink);
	background:
		radial-gradient(
			circle at 15% 10%,
			rgba(255, 221, 143, 0.55),
			transparent 32rem
		),
		radial-gradient(
			circle at 85% 20%,
			rgba(195, 172, 255, 0.5),
			transparent 30rem
		),
		linear-gradient(145deg, #fffaf2 0%, #f5efff 52%, #edfbf3 100%);
}

button,
select {
	font: inherit;
}

button:focus-visible,
select:focus-visible {
	outline: 3px solid #f0a928;
	outline-offset: 3px;
}

.game-shell {
	width: min(100% - 2rem, 76rem);
	margin-inline: auto;
	padding-block: clamp(0.5rem, 1.5vw, 1rem);
}

.game-header {
	margin-bottom: clamp(0.4rem, 1vw, 0.75rem);
	text-align: center;
}

h1 {
	margin: 0;
	font-size: clamp(1.75rem, 4vw, 2.5rem);
	line-height: 1;
	letter-spacing: -0.035em;
}

.game-panel {
	width: fit-content;
	max-width: 100%;
	padding: clamp(0.75rem, 2vw, 1.25rem);
	margin-inline: auto;
	border: 1px solid var(--border);
	background: var(--surface);
	box-shadow: var(--shadow);
	backdrop-filter: blur(18px);
}

/* The controls and game content share the same three columns, keeping the board
   centered while reserving equal space for statistics and actions. */
.game-controls {
	display: grid;
	grid-template-columns:
		minmax(4.5rem, 11rem) minmax(0, calc(var(--columns) * 6.8rem))
		minmax(4.5rem, 11rem);
	gap: clamp(0.75rem, 2vw, 1.5rem);
	align-items: end;
	justify-content: center;
	margin-bottom: 1rem;
}

.level-control {
	display: grid;
	grid-column: 1;
	gap: 0.35rem;
	color: var(--muted);
	font-size: 0.78rem;
	font-weight: 800;
	letter-spacing: 0.06em;
	text-transform: uppercase;
}

#restart {
	grid-column: 3;
	width: 100%;
}

select {
	max-width: 100%;
	min-height: 2.75rem;
	padding: 0.55rem 2.5rem 0.55rem 0.8rem;
	border: 1px solid var(--border);
	color: var(--ink);
	background-color: #fff;
	font-size: 0.95rem;
	font-weight: 650;
	letter-spacing: normal;
	text-transform: none;
}

.primary-button,
.secondary-button {
	min-height: 2.75rem;
	padding: 0.65rem 1rem;
	font-weight: 750;
	cursor: pointer;
}

.primary-button {
	border: 1px solid var(--purple);
	color: #fff;
	background: var(--purple);
}

.primary-button:hover {
	background: var(--purple-dark);
}

.primary-button:active {
	background: #3e2477;
	transform: translateY(1px);
}

.secondary-button {
	border: 1px solid var(--border);
	color: var(--purple-dark);
	background: #fff;
}

.secondary-button:hover {
	border-color: rgba(104, 66, 184, 0.35);
	background: #f8f4ff;
}

.secondary-button:active {
	background: #eee6fb;
	transform: translateY(1px);
}

.game-layout {
	display: grid;
	grid-template-columns:
		minmax(4.5rem, 11rem) minmax(0, calc(var(--columns) * 6.8rem))
		minmax(4.5rem, 11rem);
	gap: clamp(0.75rem, 2vw, 1.5rem);
	align-items: start;
	justify-content: center;
}

.scoreboard {
	display: grid;
	border-block: 1px solid var(--border);
}

.stat {
	display: grid;
	gap: 0.15rem;
	padding: 0.8rem 0.5rem;
	text-align: center;
}

.stat + .stat {
	border-top: 1px solid var(--border);
}

.stat-label {
	color: var(--muted);
	font-size: clamp(0.65rem, 2vw, 0.8rem);
	font-weight: 700;
	text-transform: uppercase;
}

.stat strong {
	font-size: clamp(1.1rem, 3vw, 1.4rem);
	font-variant-numeric: tabular-nums;
}

.game-board {
	display: grid;
	grid-template-columns: repeat(var(--columns), minmax(0, 1fr));
	gap: clamp(0.35rem, 1.5vw, 0.7rem);
	width: 100%;
	max-width: 100%;
	perspective: 70rem;
}

.game-actions {
	display: flex;
	justify-content: stretch;
	width: 100%;
	min-width: 0;
}

.sound-toggle {
	display: inline-flex;
	gap: 0.5rem;
	align-items: center;
	justify-content: center;
	width: 100%;
}

.sound-toggle-icon {
	font-size: 1.1rem;
	line-height: 1;
}

.memory-card {
	position: relative;
	padding: 0;
	border: 0;
	aspect-ratio: 1;
	background: transparent;
	cursor: pointer;
	transform-style: preserve-3d;
}

.memory-card-placeholder {
	display: block;
	visibility: hidden;
	aspect-ratio: 1;
	pointer-events: none;
}

/* Preserve the 3D space of every child so the faces and four edges rotate as one
   thick card instead of appearing as separate flat layers. */
.card-inner {
	--card-depth: 8px;
	position: absolute;
	inset: 0;
	transform: translateY(-1px) rotateX(3deg);
	transform-style: preserve-3d;
	transition: transform 420ms cubic-bezier(0.2, 0.75, 0.25, 1.15);
	will-change: transform;
}

.memory-card:hover .card-inner {
	transform: translateY(-4px) rotateX(3deg);
}

.memory-card[data-state="hidden"]:active .card-inner {
	transform: translateY(1px) rotateX(3deg);
}

.memory-card[data-state="revealed"] .card-inner,
.memory-card[data-state="matched"] .card-inner {
	transform: translateY(-1px) rotateX(3deg) rotate3d(1, -1, 0, 180deg);
}

.memory-card[data-state="revealed"]:hover .card-inner {
	transform: translateY(-4px) rotateX(3deg) rotate3d(1, -1, 0, 180deg);
}

.memory-card[data-state="matched"] {
	cursor: default;
}

/* A matched card first comes forward, then shrinks and twists toward its center. */
.memory-card.is-vanishing {
	z-index: 5;
	pointer-events: none;
	animation: matched-card-suction 700ms cubic-bezier(0.3, 0.8, 0.3, 1)
		forwards;
	transform-origin: center;
	will-change: filter, opacity, transform;
}

.memory-card.is-vanishing .card-face {
	box-shadow:
		0 0 0 4px rgba(255, 255, 255, 0.92),
		0 0 30px rgba(240, 169, 40, 0.95),
		0 14px 20px rgba(43, 31, 63, 0.28);
}

@keyframes matched-card-suction {
	0% {
		filter: brightness(1);
		opacity: 1;
		transform: translateY(0) scale(1) rotate(0);
	}

	22% {
		filter: brightness(1.2) saturate(1.12);
		transform: translateY(-9px) scale(1.13) rotate(0);
	}

	45% {
		filter: brightness(1.12) saturate(1.08);
		opacity: 1;
		transform: translateY(-6px) scale(0.94) rotate(0);
	}

	78% {
		filter: blur(1px) brightness(1.35) saturate(1.2);
		opacity: 0.86;
		transform: translateY(0) scale(0.38)
			rotate(calc(var(--suck-rotation) * 0.55));
	}

	100% {
		filter: blur(7px) brightness(1.7) saturate(1.25);
		opacity: 0;
		transform: translateY(2px) scale(0.01) rotate(var(--suck-rotation));
	}
}

.card-face {
	position: absolute;
	inset: 0;
	display: grid;
	place-items: center;
	overflow: hidden;
	border: 1px solid rgba(78, 59, 38, 0.42);
	backface-visibility: hidden;
	box-shadow: 0 7px 7px rgba(43, 31, 63, 0.28);
	transition: box-shadow 180ms ease;
}

.memory-card:hover .card-face {
	box-shadow: 0 10px 9px rgba(43, 31, 63, 0.3);
}

.memory-card[data-state="hidden"]:active .card-face {
	box-shadow: 0 4px 4px rgba(43, 31, 63, 0.24);
}

.memory-card[data-state="matched"] .card-face {
	box-shadow:
		0 0 0 3px var(--mint),
		0 7px 7px rgba(44, 154, 95, 0.24);
}

/* These four narrow planes connect the front and back faces to create thickness. */
.card-edge {
	position: absolute;
	display: block;
	pointer-events: none;
}

.card-edge-top,
.card-edge-bottom {
	left: 0;
	width: 100%;
	height: var(--card-depth);
}

.card-edge-left,
.card-edge-right {
	top: 0;
	width: var(--card-depth);
	height: 100%;
}

.card-edge-top {
	top: calc(var(--card-depth) / -2);
	background: #e0d1ae;
	transform: rotateX(90deg);
}

.card-edge-right {
	right: calc(var(--card-depth) / -2);
	background: #9d8962;
	transform: rotateY(90deg);
}

.card-edge-bottom {
	bottom: calc(var(--card-depth) / -2);
	background: #88734e;
	transform: rotateX(90deg);
}

.card-edge-left {
	left: calc(var(--card-depth) / -2);
	background: #baa77d;
	transform: rotateY(90deg);
}

.card-face::after {
	position: absolute;
	inset: 0;
	box-shadow:
		inset 2px 2px 0 rgba(255, 255, 255, 0.52),
		inset -2px -3px 0 rgba(46, 31, 18, 0.22);
	content: "";
	pointer-events: none;
}

.card-back {
	background: #0d3f9a url("images/card-backs/owl-geometric-blue.webp")
		center / cover no-repeat;
	transform: translateZ(calc(var(--card-depth) / 2));
}

.card-front {
	padding: 7%;
	background: #fff;
	transform: rotate3d(1, -1, 0, 180deg)
		translateZ(calc(var(--card-depth) / 2));
}

.card-front img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: contain;
	image-rendering: auto;
	user-select: none;
}

/* Confetti pieces begin at the viewport center; JavaScript supplies a different
   direction, color, delay, and spin for each piece through custom properties. */
.confetti {
	position: fixed;
	inset: 0;
	z-index: 100;
	overflow: hidden;
	pointer-events: none;
}

.confetti-piece {
	position: absolute;
	top: 44%;
	left: 50%;
	width: 10px;
	height: 10px;
	background: var(--confetti-color);
	opacity: 0;
	animation: confetti-burst var(--confetti-duration)
		cubic-bezier(0.16, 0.64, 0.3, 1) var(--confetti-delay) forwards;
	will-change: transform, opacity;
}

.confetti-piece--round {
	border-radius: 50%;
}

.confetti-piece--streamer {
	width: 5px;
	height: 17px;
}

@keyframes confetti-burst {
	0% {
		opacity: 0;
		transform: translate3d(-50%, -50%, 0) rotate(0deg) scale(0.35);
	}

	8% {
		opacity: 1;
	}

	42% {
		opacity: 1;
		transform: translate3d(
				calc(-50% + var(--confetti-drift-mid)),
				var(--confetti-rise),
				0
			)
			rotate(var(--confetti-spin-mid)) scale(1);
	}

	100% {
		opacity: 0;
		transform: translate3d(
				calc(-50% + var(--confetti-drift)),
				var(--confetti-fall),
				0
			)
			rotate(var(--confetti-spin)) scale(0.8);
	}
}

.win-dialog {
	width: min(calc(100% - 2rem), 28rem);
	padding: 2rem;
	border: 1px solid var(--border);
	color: var(--ink);
	background: #fff;
	box-shadow: var(--shadow);
	text-align: center;
}

.win-dialog::backdrop {
	background: rgba(30, 22, 48, 0.55);
	backdrop-filter: blur(4px);
}

.win-dialog h2 {
	margin: 0 0 0.5rem;
	font-size: 1.8rem;
}

.win-dialog p {
	margin: 0 0 1.4rem;
	color: var(--muted);
}

.dialog-actions {
	display: flex;
	gap: 0.65rem;
	justify-content: center;
}

.visually-hidden {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

@media (max-width: 35rem) {
	.game-shell {
		width: min(100% - 1rem, 76rem);
	}

	.game-controls {
		grid-template-columns: minmax(0, 1fr);
	}

	.level-control,
	select {
		width: 100%;
	}

	#restart {
		width: 100%;
	}

	.game-board {
		gap: 0.3rem;
	}
}

@media (max-width: 52rem) {
	.game-panel {
		width: 100%;
	}

	.game-controls {
		grid-template-columns: minmax(0, 1fr) auto;
		justify-content: stretch;
	}

	.level-control,
	#restart {
		grid-column: auto;
	}

	#restart {
		width: auto;
	}

	.game-layout {
		grid-template-columns: minmax(0, 1fr);
	}

	.scoreboard {
		grid-template-columns: repeat(3, 1fr);
	}

	.stat + .stat {
		border-top: 0;
		border-left: 1px solid var(--border);
	}

	.game-board {
		margin-inline: auto;
	}

	.game-actions {
		justify-content: center;
	}

	.sound-toggle {
		width: min(100%, 12rem);
	}
}

@media (max-width: 35rem) {
	.game-controls {
		grid-template-columns: minmax(0, 1fr);
	}

	.level-control,
	#restart {
		grid-column: 1;
		width: 100%;
	}
}

/* Honor the user's operating-system preference by removing decorative movement
   and reducing essential card state changes to an almost instant transition. */
@media (prefers-reduced-motion: reduce) {
	.confetti {
		display: none;
	}

	.card-inner {
		transition-duration: 0.01ms;
	}

	.memory-card.is-vanishing {
		animation-duration: 0.01ms;
	}

	.memory-card:hover .card-inner {
		transform: translateY(-1px) rotateX(3deg);
	}

	.memory-card[data-state="revealed"] .card-inner,
	.memory-card[data-state="matched"] .card-inner {
		transform: translateY(-1px) rotateX(3deg) rotate3d(1, -1, 0, 180deg);
	}

	.memory-card[data-state="revealed"]:hover .card-inner {
		transform: translateY(-1px) rotateX(3deg) rotate3d(1, -1, 0, 180deg);
	}
}
