html, body {
	margin: 0;
	padding: 0;
	height: 100%;
	overflow: hidden;
	background: #1b1712;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
	color: #fff8ec;
	touch-action: none;
	-webkit-user-select: none;
	user-select: none;
}
canvas#game { display: block; width: 100%; height: 100%; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; }

/* ---------- shared ---------- */
.round-btn {
	position: fixed;
	width: 42px; height: 42px;
	border-radius: 50%;
	display: flex; align-items: center; justify-content: center;
	background: rgba(20, 16, 12, 0.55);
	border: 1px solid rgba(255, 240, 220, 0.18);
	-webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
	cursor: pointer; font-size: 18px; z-index: 30;
}
#sound-toggle { top: 14px; right: 14px; }
#sound-toggle.muted { opacity: 0.6; }

.btn {
	border: none;
	border-radius: 12px;
	padding: 14px 30px;
	font-weight: 800;
	font-size: 1rem;
	letter-spacing: 0.04em;
	text-transform: uppercase;
	cursor: pointer;
	color: #1f1406;
	background: linear-gradient(180deg, #ffb347, #ff7a1a);
	box-shadow: 0 4px 0 #b34a00, 0 10px 24px rgba(255, 110, 20, 0.35);
	transition: transform 0.1s ease, box-shadow 0.1s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #b34a00, 0 4px 10px rgba(255, 110, 20, 0.3); }
.btn.ghost { background: rgba(255, 248, 236, 0.1); color: #fff8ec; box-shadow: inset 0 0 0 1px rgba(255, 248, 236, 0.3); }

/* ---------- menu (left sidebar; the stage drives itself on the right) ---------- */
#menu {
	position: fixed; top: 0; bottom: 0; left: 0; z-index: 20;
	width: 400px; max-width: 100%;
	display: flex; flex-direction: column; gap: 14px;
	padding: 20px; box-sizing: border-box;
	background: linear-gradient(90deg, rgba(14, 11, 8, 0.82) 0%, rgba(14, 11, 8, 0.66) 75%, rgba(14, 11, 8, 0) 100%);
	overflow-y: auto;
}
.brand { padding-right: 20px; }
.logo {
	font-size: clamp(2rem, 5vw, 3rem);
	font-weight: 900; letter-spacing: 0.02em; line-height: 1;
	text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35), 0 8px 30px rgba(0, 0, 0, 0.4);
}
.logo span { color: #ffb347; }
.logo em {
	font-style: normal; font-size: 0.45em; vertical-align: super; margin-left: 8px;
	padding: 2px 8px; border-radius: 6px; background: #ff7a1a; color: #1f1406;
}
.brand p { margin: 10px 0 0; color: #f1e4cf; font-size: 0.9rem; line-height: 1.4; }

.menu-panel { padding-right: 0; flex: 1; min-height: 0; display: flex; flex-direction: column; }
#menu { overflow: hidden; }
#stage-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding-right: 24px; }
.panel-title { font-size: 0.72rem; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: #cbb89c; margin-bottom: 8px; }
#stage-list { display: flex; flex-direction: column; gap: 8px; }
.stage-card {
	display: grid; grid-template-columns: 40px 1fr auto; grid-template-rows: auto auto; column-gap: 10px; align-items: center;
	text-align: left; cursor: pointer;
	padding: 9px 12px; border-radius: 12px;
	border: 2px solid transparent;
	background: rgba(255, 248, 236, 0.07);
	transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease;
	position: relative; overflow: hidden; isolation: isolate; flex-shrink: 0;
}
.stage-card::before { content: ""; position: absolute; inset: 0; opacity: 0.3; z-index: -1; }
.st-snow::before { background: linear-gradient(90deg, #9cc3ee, transparent); }
.st-mud::before { background: linear-gradient(90deg, #7a5a34, transparent); }
.st-water::before { background: linear-gradient(90deg, #1f8aa0, transparent); }
.st-rock::before { background: linear-gradient(90deg, #c0552f, transparent); }
.st-sand::before { background: linear-gradient(90deg, #e8b366, transparent); }
.stage-card:hover { transform: translateX(3px); background: rgba(255, 248, 236, 0.12); }
.stage-card.selected { border-color: #ff9a3c; background: rgba(255, 154, 60, 0.16); box-shadow: 0 0 0 3px rgba(255, 154, 60, 0.18); }
.st-icon { font-size: 1.5rem; grid-row: 1 / 3; text-align: center; }
.st-name { font-weight: 800; font-size: 0.98rem; }
.st-terrain { font-size: 0.76rem; color: #e8d8bf; grid-column: 2; }
.st-best { font-size: 0.74rem; color: #ffd28a; grid-column: 3; grid-row: 1 / 3; text-align: right; font-variant-numeric: tabular-nums; }
.dim { color: rgba(255, 255, 255, 0.25); }

.menu-row { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; padding-right: 24px; flex-shrink: 0; }
#color-list { display: flex; gap: 8px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; cursor: pointer; border: 2px solid rgba(255, 255, 255, 0.25); box-shadow: inset 0 -4px 6px rgba(0, 0, 0, 0.25); }
.swatch.selected { border-color: #fff; box-shadow: 0 0 0 3px rgba(255, 154, 60, 0.7), inset 0 -4px 6px rgba(0, 0, 0, 0.25); }
#start-btn { width: 100%; }
.hint { margin-top: 12px; font-size: 0.74rem; color: #bda98c; line-height: 1.5; }

/* ---------- HUD ---------- */
.hud-panel {
	position: fixed; z-index: 10;
	background: rgba(20, 16, 12, 0.5);
	border: 1px solid rgba(255, 240, 220, 0.16);
	-webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
	border-radius: 14px;
}
.hud-tl { top: 14px; left: 14px; padding: 10px 16px; min-width: 150px; }
#hud-stage { font-size: 0.75rem; font-weight: 700; color: #e8d8bf; letter-spacing: 0.05em; }
#hud-time { font-size: 1.9rem; font-weight: 900; font-variant-numeric: tabular-nums; line-height: 1.15; }
#hud-cp { font-size: 0.8rem; font-weight: 800; color: #ffb347; }
.hud-map { top: 64px; right: 14px; padding: 6px; }
#minimap { display: block; width: 150px; height: 150px; }
#cam-btn { top: 14px; right: 64px; }

.hud-speedo { position: fixed; z-index: 10; right: 22px; bottom: 22px; width: 150px; height: 150px; }
.gauge {
	position: absolute; inset: 0; border-radius: 50%;
	background: radial-gradient(circle, rgba(20, 16, 12, 0.72) 58%, rgba(20, 16, 12, 0.4) 60%);
	border: 2px solid rgba(255, 240, 220, 0.2);
}
.gauge-ticks {
	position: absolute; inset: 8px; border-radius: 50%;
	background: conic-gradient(from -120deg, #ffb347 0deg, #ff5a1a 240deg, transparent 240deg);
	-webkit-mask: radial-gradient(circle, transparent 60%, #000 61%, #000 66%, transparent 67%);
	mask: radial-gradient(circle, transparent 60%, #000 61%, #000 66%, transparent 67%);
	opacity: 0.8;
}
#gauge-fill {
	position: absolute; left: 50%; top: 14px; width: 4px; height: 62px; margin-left: -2px;
	background: linear-gradient(#fff, #ff7a1a); border-radius: 2px;
	transform-origin: 50% 61px; transform: rotate(-120deg);
	box-shadow: 0 0 8px rgba(255, 122, 26, 0.8);
	transition: transform 0.08s linear;
}
.speed-num { position: absolute; left: 0; right: 0; top: 78px; text-align: center; }
#speed-val { font-size: 1.8rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.unit { display: block; font-size: 0.7rem; color: #cbb89c; font-weight: 700; margin-top: -4px; }
#surface-tag {
	position: absolute; left: 50%; bottom: -14px; transform: translateX(-50%);
	white-space: nowrap; padding: 3px 10px; border-radius: 999px;
	font-size: 0.72rem; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase;
	background: #ffb347; color: #1f1406; opacity: 0; transition: opacity 0.2s;
}
#surface-tag.show { opacity: 1; }

#toast {
	position: fixed; z-index: 12; left: 50%; top: 18%;
	transform: translate(-50%, -10px); opacity: 0;
	padding: 10px 20px; border-radius: 12px;
	font-weight: 800; font-size: 1.05rem;
	background: rgba(20, 16, 12, 0.7); border: 1px solid rgba(255, 240, 220, 0.2);
	transition: opacity 0.2s, transform 0.2s; pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.good { border-color: #39d353; color: #b9f7c4; }
#toast.warn { border-color: #ffb347; color: #ffd9a8; }

#countdown {
	position: fixed; z-index: 15; left: 50%; top: 40%; transform: translate(-50%, -50%);
	font-size: clamp(4rem, 14vw, 8rem); font-weight: 900; color: #fff;
	text-shadow: 0 6px 0 rgba(0, 0, 0, 0.3), 0 0 40px rgba(255, 122, 26, 0.8);
	pointer-events: none;
}
#countdown.pop { animation: pop 0.6s ease-out; }
@keyframes pop { 0% { transform: translate(-50%, -50%) scale(1.8); opacity: 0; } 30% { opacity: 1; } 100% { transform: translate(-50%, -50%) scale(1); } }

/* ---------- results / pause ---------- */
.overlay {
	position: fixed; inset: 0; z-index: 25;
	display: flex; align-items: center; justify-content: center;
	background: radial-gradient(circle, rgba(10, 8, 6, 0.25), rgba(10, 8, 6, 0.7));
	padding: 20px;
}
.card {
	text-align: center; min-width: 280px; max-width: 420px; width: 100%;
	background: rgba(22, 17, 12, 0.78); border: 1px solid rgba(255, 240, 220, 0.16);
	-webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
	border-radius: 20px; padding: 26px 24px;
	box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
	animation: rise 0.35s ease-out;
}
@keyframes rise { from { transform: translateY(20px); opacity: 0; } }
.card h1 { margin: 4px 0 8px; font-size: 1.8rem; font-weight: 900; }
.res-stage { font-size: 0.8rem; color: #cbb89c; font-weight: 700; letter-spacing: 0.06em; }
.stars { font-size: 2.6rem; color: #ffc53d; letter-spacing: 6px; text-shadow: 0 0 20px rgba(255, 197, 61, 0.5); }
.res-time { font-size: 2.4rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.res-best { color: #cbb89c; margin: 6px 0 16px; font-size: 0.9rem; }
.badge {
	display: inline-block; margin-top: 6px; padding: 4px 12px; border-radius: 999px;
	background: linear-gradient(90deg, #ffc53d, #ff7a1a); color: #1f1406;
	font-weight: 900; font-size: 0.75rem; letter-spacing: 0.1em; text-transform: uppercase;
}
.btn-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 8px; }

#loading {
	position: fixed; z-index: 40; left: 50%; top: 50%; transform: translate(-50%, -50%);
	display: flex; align-items: center; gap: 12px;
	padding: 14px 20px; border-radius: 14px; background: rgba(20, 16, 12, 0.85);
	font-weight: 700;
}
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, 0.2); border-top-color: #ff9a3c; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- touch controls ---------- */
#touch { display: none; }
.t-btn {
	position: fixed; z-index: 11;
	width: 70px; height: 70px; border-radius: 50%;
	display: flex; align-items: center; justify-content: center;
	background: rgba(20, 16, 12, 0.45); border: 1px solid rgba(255, 240, 220, 0.25);
	font-size: 24px; font-weight: 900; color: #ffe2bf;
}
.t-btn.down { background: rgba(255, 154, 60, 0.45); }
.t-btn.small { width: 50px; height: 50px; font-size: 16px; }
.t-btn.big { width: 86px; height: 86px; }
#t-left { left: 18px; bottom: 20px; }
#t-right { left: 100px; bottom: 20px; }
#t-gas { right: 18px; bottom: 18px; }
#t-brake { right: 116px; bottom: 20px; }
#t-hb { right: 36px; bottom: 116px; }
#t-reset { left: 36px; bottom: 104px; }

@media (hover: none) and (pointer: coarse) {
	body.racing #touch { display: block; }
	.hint { display: none; }
	.hud-speedo { right: auto; left: 50%; top: 6px; bottom: auto; transform: translateX(-50%) scale(0.55); transform-origin: top center; }
	#toast { top: 34%; }
}

@media (max-width: 760px) {
	#menu { width: 330px; padding: 12px; gap: 10px; }
	.brand p { display: none; }
	.logo { font-size: 1.8rem; }
	.stage-card { padding: 6px 10px; }
	.hud-map { display: none; }
}
@media (max-height: 640px) {
	.brand p, .hint { display: none; }
	.stage-card { padding: 6px 10px; }
	#menu { gap: 8px; }
}
@media (max-height: 480px) {
	.brand p { display: none; }
	.logo { font-size: 1.7rem; }
	.stage-card { padding: 5px 10px; }
	.st-terrain { display: none; }
}

/* ---------- wallet / garage / payout ---------- */
.wallet {
	display: inline-flex; align-items: center; gap: 8px; margin-top: 12px;
	padding: 6px 14px; border-radius: 999px;
	background: linear-gradient(180deg, rgba(255, 197, 61, 0.2), rgba(255, 122, 26, 0.12));
	border: 1px solid rgba(255, 197, 61, 0.45);
	font-weight: 900; font-size: 1.05rem; color: #ffd86b; font-variant-numeric: tabular-nums;
}
.st-name small { font-weight: 600; font-size: 0.72rem; color: #cbb89c; margin-left: 4px; }
.btn-pair { display: flex; gap: 10px; }
.btn-pair .btn { flex: 1; padding-left: 12px; padding-right: 12px; }
#start-btn { width: auto; flex: 1.6; }
#menu-msg { min-height: 1em; font-size: 0.8rem; color: #ffb347; font-weight: 700; opacity: 0; transition: opacity 0.2s; }
#menu-msg.show { opacity: 1; }
.swatch.locked { position: relative; opacity: 0.75; }
.swatch.locked::after { content: "🔒"; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 12px; }
.swatch.metal { background-image: linear-gradient(135deg, rgba(255,255,255,0.45), rgba(255,255,255,0) 45%, rgba(0,0,0,0.25)) !important; }
#color-list { flex-wrap: wrap; }

.pay { margin: 4px 0 14px; text-align: left; background: rgba(255, 248, 236, 0.05); border-radius: 12px; padding: 10px 12px; }
.pay-title { font-size: 0.7rem; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: #cbb89c; margin-bottom: 6px; }
.pay-row { display: flex; justify-content: space-between; gap: 12px; font-size: 0.86rem; padding: 3px 0; opacity: 0; animation: payIn 0.3s ease-out forwards; }
.pay-row b { color: #9be89b; font-variant-numeric: tabular-nums; }
.pay-row.neg b { color: #ff8a7a; }
@keyframes payIn { from { opacity: 0; transform: translateX(-8px); } to { opacity: 1; transform: none; } }
.pay-total { display: flex; justify-content: space-between; border-top: 1px solid rgba(255, 240, 220, 0.18); margin-top: 6px; padding-top: 6px; font-weight: 900; font-size: 1.05rem; }
.pay-total b { color: #ffd86b; }
.pay-wallet { text-align: right; font-size: 0.8rem; color: #cbb89c; margin-top: 2px; }
.pay-wallet b { color: #ffd86b; font-variant-numeric: tabular-nums; }
#results .card { max-width: 460px; max-height: calc(100vh - 40px); overflow-y: auto; }

.garage-card { max-width: 560px; text-align: left; max-height: calc(100vh - 40px); overflow-y: auto; }
.garage-head { display: flex; align-items: center; justify-content: space-between; }
.garage-head .wallet { margin-top: 0; }
.garage-sub { color: #cbb89c; font-size: 0.85rem; margin: 6px 0 12px; }
.upg { display: grid; grid-template-columns: 44px 1fr auto; gap: 12px; align-items: center; padding: 10px 12px; border-radius: 12px; background: rgba(255, 248, 236, 0.06); margin-bottom: 8px; }
.upg-icon { font-size: 1.6rem; text-align: center; }
.upg-name { font-weight: 800; }
.upg-desc { font-size: 0.78rem; color: #d9c8ae; margin: 2px 0 6px; }
.pips { display: flex; gap: 4px; }
.pips i { width: 18px; height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.15); }
.pips i.on { background: linear-gradient(90deg, #ffb347, #ff7a1a); box-shadow: 0 0 6px rgba(255, 122, 26, 0.6); }
.btn.small { padding: 9px 14px; font-size: 0.85rem; border-radius: 10px; min-width: 88px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
#garage-close { width: 100%; margin-top: 6px; }
@media (max-height: 640px) {
	.card { padding: 16px 18px; }
	.card h1 { font-size: 1.4rem; margin: 2px 0 4px; }
	.stars { font-size: 1.8rem; }
	.res-time { font-size: 1.8rem; }
	.res-best { margin: 2px 0 8px; }
	.pay-row { padding: 1px 0; font-size: 0.8rem; }
}
