:root {
	--board-size: min(88vw, 66dvh, 620px);
	--ink: #fff9ec;
	--night: #15213d;
	--panel: rgba(19, 31, 58, 0.82);
	--line: rgba(255, 255, 255, 0.18);
	--gold: #ffd166;
	--green: #82e3a8;
	--blue: #7cc7ff;
	--red: #ff7a7a;
	--light-square: #f4d7a1;
	--dark-square: #81573e;
	--shadow: 0 18px 46px rgba(6, 12, 28, 0.34);
}

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

html, body {
	height: 100%;
	min-height: 100%;
	overflow: hidden;
}

body {
	margin: 0;
	height: 100vh;
	height: 100dvh;
	min-height: 100vh;
	min-height: 100dvh;
	font-family: Nunito, system-ui, sans-serif;
	color: var(--ink);
	background:
		radial-gradient(circle at 18% 14%, rgba(255, 209, 102, 0.3), transparent 26%),
		radial-gradient(circle at 88% 18%, rgba(124, 199, 255, 0.26), transparent 25%),
		radial-gradient(circle at 50% 92%, rgba(130, 227, 168, 0.2), transparent 30%),
		linear-gradient(150deg, #0d152c 0%, #1b315d 52%, #5e3f2d 100%);
	overflow: hidden;
	touch-action: manipulation;
}

button, a {
	font: inherit;
	-webkit-touch-callout: none;
}

.app-shell {
	height: 100vh;
	height: 100dvh;
	min-height: 100vh;
	min-height: 100dvh;
	max-height: 100vh;
	max-height: 100dvh;
	display: grid;
	grid-template-rows: auto minmax(0, 1fr) auto;
	gap: clamp(0.45rem, 1.5vw, 0.8rem);
	padding: calc(0.5rem + env(safe-area-inset-top)) calc(0.55rem + env(safe-area-inset-right)) calc(0.65rem + env(safe-area-inset-bottom)) calc(0.55rem + env(safe-area-inset-left));
	overflow: hidden;
}

.topbar,
.board-panel,
.side-panel,
.status-text {
	border: 1px solid var(--line);
	box-shadow: var(--shadow);
	backdrop-filter: blur(14px);
}

.topbar {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: 0.75rem;
	padding: clamp(0.55rem, 1.8vw, 0.9rem) clamp(0.65rem, 2vw, 1rem);
	border-radius: 1.35rem;
	background: rgba(10, 18, 38, 0.78);
}

.eyebrow,
.subtitle,
.version-pill,
.panel-kicker,
.mode-help,
.help-card p,
.status-text {
	margin: 0;
}

.eyebrow,
.panel-kicker {
	color: var(--gold);
	font-size: clamp(0.68rem, 2.2vw, 0.78rem);
	font-weight: 900;
	letter-spacing: 0.16em;
	text-transform: uppercase;
}

h1 {
	margin: 0;
	font-family: Fredoka, system-ui, sans-serif;
	font-size: clamp(1.7rem, 6vw, 3rem);
	line-height: 0.95;
	text-shadow: 0 4px 0 rgba(0, 0, 0, 0.18);
}

.subtitle {
	margin-top: 0.28rem;
	max-width: 36rem;
	color: rgba(255, 249, 236, 0.86);
	font-size: clamp(0.84rem, 2.5vw, 0.98rem);
}

.version-pill {
	display: inline-flex;
	gap: 0.25rem;
	margin-top: 0.38rem;
	padding: 0.2rem 0.52rem;
	border-radius: 999px;
	background: rgba(255, 255, 255, 0.12);
	border: 1px solid rgba(255, 255, 255, 0.22);
	color: rgba(255, 249, 236, 0.9);
	font-size: 0.72rem;
	font-weight: 900;
}

.toolbar-actions,
.segmented-controls {
	display: flex;
	flex-wrap: wrap;
	gap: 0.45rem;
	justify-content: flex-end;
	align-items: center;
}

#goBack,
button {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	border: 1px solid rgba(255, 255, 255, 0.18);
	border-radius: 999px;
	padding: 0.58rem 0.82rem;
	background: linear-gradient(135deg, #e6a23c, #9b5f3b);
	color: var(--ink);
	font-weight: 900;
	text-align: center;
	text-decoration: none;
	cursor: pointer;
	-webkit-tap-highlight-color: transparent;
}

button[disabled] {
	cursor: not-allowed;
	filter: grayscale(0.5);
	opacity: 0.62;
}

#goBack:hover,
#goBack:focus-visible,
button:hover,
button:focus-visible {
	outline: none;
	border-color: rgba(255, 255, 255, 0.58);
}

.play-layout {
	min-height: 0;
	display: grid;
	grid-template-columns: minmax(0, 1fr) minmax(16rem, 0.45fr);
	align-items: stretch;
	justify-items: center;
	gap: clamp(0.55rem, 1.5vw, 0.9rem);
	overflow: hidden;
}

.board-panel {
	width: 100%;
	min-height: 0;
	display: grid;
	place-items: center;
	align-self: stretch;
	padding: clamp(0.35rem, 1.5vw, 0.6rem);
	border-radius: 1.5rem;
	background: rgba(255, 255, 255, 0.13);
	touch-action: pan-y;
	user-select: none;
	-webkit-user-select: none;
	-webkit-touch-callout: none;
}

.chess-board {
	display: grid;
	grid-template-columns: repeat(8, 1fr);
	grid-template-rows: repeat(8, 1fr);
	width: var(--board-size);
	max-width: 100%;
	max-height: 100%;
	aspect-ratio: 1;
	border: 6px solid rgba(46, 29, 24, 0.9);
	border-radius: 1.2rem;
	overflow: hidden;
	box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.16), 0 18px 40px rgba(0, 0, 0, 0.28);
	background: #2b1f1b;
	touch-action: pan-y;
}

.square {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	border: 0;
	border-radius: 0;
	padding: 0;
	background: var(--light-square);
	color: #1f2937;
	box-shadow: none;
	font-size: calc(var(--board-size) / 13);
	line-height: 1;
	text-shadow: 0 2px 0 rgba(255, 255, 255, 0.4);
	touch-action: pan-y;
	user-select: none;
	-webkit-user-select: none;
}

.square.dark { background: var(--dark-square); }
.square.light { background: var(--light-square); }
.square.black-piece { color: #131827; text-shadow: 0 2px 0 rgba(255, 255, 255, 0.24); }
.square.white-piece { color: #fffaf1; text-shadow: 0 2px 0 rgba(53, 32, 24, 0.55); }

.square.black-piece,
.square.white-piece {
	touch-action: none;
}

.square.drag-source {
	opacity: 0.58;
}

.piece-icon {
	display: block;
	object-fit: contain;
	width: 72%;
	height: 72%;
	pointer-events: none;
	filter: drop-shadow(0 1px 0 rgba(0, 0, 0, 0.24));
}

.piece-p { width: 62%; height: 62%; }
.piece-n { width: 70%; height: 70%; }
.piece-r { width: 69%; height: 69%; }
.piece-b { width: 68%; height: 68%; }
.piece-q { width: 74%; height: 74%; }
.piece-k { width: 74%; height: 74%; }

.piece-black {
	filter: brightness(0.74) contrast(1.02) saturate(0.94) drop-shadow(0 1px 0 rgba(255, 255, 255, 0.2));
}

.drag-ghost-piece {
	position: fixed;
	left: 0;
	top: 0;
	margin: 0;
	z-index: 80;
	pointer-events: none;
	display: flex;
	align-items: center;
	justify-content: center;
	opacity: 0.98;
	border-radius: 0;
	box-shadow: 0 18px 34px rgba(5, 15, 25, 0.28);
	background: transparent;
	transform: translate3d(0, 0, 0);
	will-change: transform;
	transition: transform 130ms cubic-bezier(0.2, 0.9, 0.2, 1.04), opacity 130ms ease;
}

.drag-ghost-piece .piece-icon {
	width: 72%;
	height: 72%;
}

.drag-ghost-piece.is-dropping {
	opacity: 0.9;
}

.drag-ghost-piece.is-returning {
	transition-duration: 150ms;
	opacity: 0.86;
}

.square::after {
	content: attr(data-label);
	position: absolute;
	left: 0.22rem;
	bottom: 0.16rem;
	color: rgba(35, 25, 19, 0.54);
	font-size: clamp(0.48rem, 1.6vw, 0.7rem);
	font-weight: 900;
	pointer-events: none;
}

.square.dark::after { color: rgba(255, 249, 236, 0.58); }

.square.selected {
	box-shadow: inset 0 0 0 5px rgba(255, 209, 102, 0.95);
	z-index: 2;
}

.square.legal::before,
.square.capture::before {
	content: '';
	position: absolute;
	inset: 34%;
	border-radius: 999px;
	background: rgba(31, 41, 55, 0.34);
	pointer-events: none;
}

.square.capture::before {
	inset: 12%;
	background: transparent;
	border: 5px solid rgba(255, 122, 122, 0.88);
}

.square.last-move {
	box-shadow: inset 0 0 0 100vmax rgba(130, 227, 168, 0.22), inset 0 0 0 4px rgba(18, 116, 58, 0.62);
}

.square.bot-last-move {
	box-shadow: inset 0 0 0 100vmax rgba(124, 199, 255, 0.36), inset 0 0 0 6px rgba(0, 76, 151, 0.78);
}

.square.last-move-from::before {
	content: '';
	position: absolute;
	inset: 36%;
	border-radius: 999px;
	background: rgba(255, 180, 50, 0.75);
	pointer-events: none;
	z-index: 1;
}

.square.king-check {
	background: radial-gradient(circle, #ffd6d6 0%, #ff6161 48%, #9f1111 100%);
	box-shadow: inset 0 0 0 7px #fff1f1, inset 0 0 0 12px #d40000, 0 0 0 4px rgba(255, 0, 0, 0.38);
	z-index: 3;
}

.side-panel {
	width: min(100%, 24rem);
	min-height: 0;
	align-self: stretch;
	display: grid;
	grid-template-rows: auto auto minmax(0, 1fr) auto;
	gap: 0.65rem;
	padding: clamp(0.6rem, 1.8vw, 0.8rem);
	border-radius: 1.35rem;
	background: var(--panel);
	overflow: hidden;
}

.mode-card,
.status-card,
.move-card,
.help-card {
	border: 1px solid rgba(255, 255, 255, 0.14);
	border-radius: 1rem;
	background: rgba(255, 255, 255, 0.09);
	padding: 0.75rem;
}

.mode-card,
.status-card,
.help-card {
	display: grid;
	gap: 0.55rem;
}

.mode-button {
	flex: 1 1 6rem;
	background: rgba(255, 255, 255, 0.12);
}

.difficulty-row {
	display: flex;
	align-items: center;
	gap: 0.55rem;
	margin-top: 0.2rem;
}

.difficulty-row label {
	font-size: 0.82rem;
	font-weight: 800;
	color: rgba(255, 249, 236, 0.78);
	text-transform: uppercase;
	letter-spacing: 0.06em;
	white-space: nowrap;
}

.difficulty-row select {
	flex: 1;
	padding: 0.4rem 0.5rem;
	border-radius: 0.6rem;
	border: 1px solid rgba(255, 255, 255, 0.16);
	background: rgba(0, 0, 0, 0.28);
	color: var(--ink);
	font-size: 0.82rem;
	font-weight: 700;
	cursor: pointer;
	-webkit-appearance: none;
	appearance: none;
	background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23ffd166'/%3E%3C/svg%3E");
	background-repeat: no-repeat;
	background-position: right 0.5rem center;
	background-size: 0.65rem;
	padding-right: 1.6rem;
}

.difficulty-row select:focus-visible {
	outline: none;
	border-color: var(--gold);
}

.mode-button.active {
	background: linear-gradient(135deg, #2f80ed, #55c58f);
}

.mode-help,
.help-card p {
	color: rgba(255, 249, 236, 0.82);
	font-size: 0.9rem;
	line-height: 1.35;
}

.status-grid {
	display: grid;
	grid-template-columns: repeat(4, minmax(0, 1fr));
	gap: 0.45rem;
}

.status-grid div {
	display: grid;
	gap: 0.2rem;
	padding: 0.52rem;
	border-radius: 0.8rem;
	background: rgba(0, 0, 0, 0.18);
}

.status-grid span {
	color: rgba(255, 249, 236, 0.68);
	font-size: 0.72rem;
	font-weight: 900;
	text-transform: uppercase;
}

.status-grid strong {
	font-size: 0.95rem;
}

.move-card {
	min-height: 0;
	display: grid;
	grid-template-rows: auto minmax(0, 1fr);
	gap: 0.55rem;
	overflow: hidden;
}

.move-list {
	min-height: 0;
	margin: 0;
	padding: 0 0 0 1.35rem;
	overflow: auto;
	color: rgba(255, 249, 236, 0.88);
	font-weight: 800;
}

.move-list li {
	margin: 0 0 0.28rem;
	padding-left: 0.15rem;
}

.move-list span {
	display: inline-block;
	min-width: 3.7rem;
}

.status-text {
	padding: 0.58rem 0.9rem;
	border-radius: 999px;
	background: rgba(10, 18, 38, 0.82);
	text-align: center;
	font-weight: 900;
}

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

@media (min-width: 1060px) {
	:root { --board-size: min(66dvh, 54vw, 660px); }
}

@media (max-width: 850px) {
	:root { --board-size: min(92vw, 54dvh, 560px); }

	html,
	body {
		height: auto;
		min-height: 100%;
		overflow-x: hidden;
		overflow-y: auto;
	}

	body {
		min-height: 100vh;
		min-height: 100dvh;
	}

	.app-shell {
		height: auto;
		min-height: 100vh;
		min-height: 100dvh;
		max-height: none;
		overflow: visible;
	}

	.topbar {
		align-items: flex-start;
	}

	.subtitle { display: none; }

	.toolbar-actions {
		max-width: 13.5rem;
	}

	.toolbar-actions #goBack,
	.toolbar-actions button {
		flex: 1 1 5.9rem;
		padding: 0.5rem 0.58rem;
		font-size: 0.84rem;
	}

	.play-layout {
		grid-template-columns: 1fr;
		grid-template-rows: minmax(0, auto) minmax(0, 1fr);
		overflow: visible;
	}

	.side-panel {
		width: 100%;
		grid-template-columns: 1fr 1fr;
		grid-template-rows: auto minmax(0, 1fr);
		align-self: stretch;
		max-height: 100%;
	}

	.move-card { grid-row: span 2; }
	.help-card { display: none; }
}

@media (max-width: 560px) {
	:root { --board-size: min(94vw, 50dvh, 440px); }

	.app-shell {
		gap: 0.42rem;
		padding: calc(0.36rem + env(safe-area-inset-top)) calc(0.4rem + env(safe-area-inset-right)) calc(0.45rem + env(safe-area-inset-bottom)) calc(0.4rem + env(safe-area-inset-left));
	}

	.topbar {
		padding: 0.48rem 0.56rem;
		border-radius: 1rem;
	}

	h1 { font-size: clamp(1.35rem, 7vw, 1.9rem); }
	.eyebrow { font-size: 0.58rem; }
	.version-pill { display: none; }

	.toolbar-actions {
		gap: 0.32rem;
		max-width: 12rem;
	}

	.toolbar-actions #goBack,
	.toolbar-actions button {
		flex-basis: 5.2rem;
		padding: 0.42rem 0.45rem;
		font-size: 0.76rem;
	}

	.board-panel {
		padding: 0.28rem;
		border-radius: 1rem;
	}

	.chess-board {
		border-width: 4px;
		border-radius: 0.9rem;
	}

	.square { font-size: clamp(1.35rem, 9.5vw, 2.85rem); }
	.square::after { display: none; }

	.side-panel {
		grid-template-columns: 1fr;
		grid-template-rows: auto auto minmax(0, 1fr);
		gap: 0.42rem;
		padding: 0.48rem;
		border-radius: 1rem;
	}

	.mode-card,
	.status-card,
	.move-card {
		padding: 0.48rem;
		border-radius: 0.78rem;
	}

	.mode-help { display: none; }

	.status-grid {
		grid-template-columns: repeat(4, minmax(0, 1fr));
		gap: 0.3rem;
	}

	.status-grid div { padding: 0.38rem; }
	.status-grid span { font-size: 0.6rem; }
	.status-grid strong { font-size: 0.8rem; }

	.move-list {
		display: flex;
		gap: 0.6rem;
		padding: 0;
		list-style-position: inside;
		overflow-x: auto;
		overflow-y: hidden;
		white-space: nowrap;
		font-size: 0.82rem;
	}

	.move-list li { margin: 0; }

	.status-text {
		padding: 0.48rem 0.62rem;
		font-size: 0.82rem;
		border-radius: 0.9rem;
	}
}

@media (max-height: 620px) and (max-width: 850px) {
	:root { --board-size: min(58vw, 48dvh, 360px); }

	.play-layout {
		grid-template-columns: minmax(0, auto) minmax(12rem, 1fr);
		grid-template-rows: 1fr;
	}

	.side-panel {
		grid-template-columns: 1fr;
		grid-template-rows: auto auto minmax(0, 1fr);
	}

	.mode-help,
	.help-card,
	.subtitle,
	.version-pill {
		display: none;
	}
}

.promotion-dialog,
.game-over-dialog {
	position: fixed;
	inset: 0;
	z-index: 100;
	display: grid;
	place-items: center;
	padding: 1rem;
}

.promotion-dialog[hidden],
.game-over-dialog[hidden] {
	display: none;
}

.promotion-backdrop,
.game-over-backdrop {
	position: fixed;
	inset: 0;
	background: rgba(0, 0, 0, 0.55);
	backdrop-filter: blur(4px);
}

.promotion-card,
.game-over-card {
	position: relative;
	z-index: 1;
	display: grid;
	gap: 0.65rem;
	padding: 1.2rem 1.8rem 1.6rem;
	border-radius: 1.5rem;
	background: rgba(19, 31, 58, 0.96);
	border: 1px solid rgba(255, 255, 255, 0.16);
	box-shadow: 0 24px 56px rgba(6, 12, 28, 0.5);
	text-align: center;
}

.promotion-card p,
.game-over-card p {
	margin: 0;
	color: rgba(255, 249, 236, 0.82);
	font-size: 0.95rem;
	font-weight: 700;
}

.game-over-card {
	width: min(92vw, 23rem);
	background:
		radial-gradient(circle at top, rgba(255, 209, 102, 0.18), transparent 44%),
		rgba(19, 31, 58, 0.98);
}

.game-over-card .panel-kicker {
	color: var(--gold);
}

.game-over-card h2 {
	margin: 0;
	font-family: Fredoka, system-ui, sans-serif;
	font-size: clamp(2rem, 9vw, 3rem);
	line-height: 1;
}

.game-over-actions {
	display: flex;
	justify-content: center;
	gap: 0.6rem;
	flex-wrap: wrap;
}

.game-over-actions button {
	min-width: 8rem;
}

.promotion-choices {
	display: flex;
	gap: 0.6rem;
	justify-content: center;
}

.promotion-piece {
	width: 3.5rem;
	height: 3.5rem;
	display: grid;
	place-items: center;
	border-radius: 1rem;
	border: 2px solid rgba(255, 255, 255, 0.18);
	background: rgba(255, 255, 255, 0.1);
	line-height: 1;
	cursor: pointer;
	transition: background 0.15s, border-color 0.15s;
	padding: 0;
	color: var(--ink);
}

.promotion-piece .piece-icon {
	width: 70%;
	height: 70%;
}

.promotion-piece:hover,
.promotion-piece:focus-visible {
	background: rgba(255, 209, 102, 0.25);
	border-color: var(--gold);
}

.promotion-piece:active {
	transform: scale(0.94);
}

@media (max-width: 560px) {
	.promotion-card,
	.game-over-card {
		padding: 1rem 1.2rem 1.2rem;
		gap: 0.5rem;
	}

	.promotion-piece {
		width: 2.8rem;
		height: 2.8rem;
	}

	.promotion-choices {
		gap: 0.4rem;
	}
}
