/**
 * Classic Snake Game — Serpiente Clásica
 * Paleta tomada de la captura de referencia:
 *  - Cabecera verde oscura:  #0e4b3e
 *  - Damero claro:           #edf7dc
 *  - Damero oscuro:          #ddf0c4
 *  - Fondo crema:            #f7f4e8
 *  - Serpiente:              #1f8a5f / cabeza #219e68
 *  - Manzana:                #e63b2e
 * Todas las clases llevan el prefijo csg- para no chocar con temas ni otros plugins.
 */

.csg-game {
	--csg-primary: #0e4b3e;
	--csg-max-width: 560px;
	--csg-board-light: #edf7dc;
	--csg-board-dark: #ddf0c4;
	--csg-cream: #f7f4e8;
	--csg-snake: #1f8a5f;
	--csg-snake-head: #219e68;
	--csg-apple: #e63b2e;
	--csg-border: #20241f;

	box-sizing: border-box;
	width: 100%;
	max-width: var(--csg-max-width);
	margin: 1.25rem auto;
	font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
	color: #17352c;
	-webkit-tap-highlight-color: transparent;
}

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

/* Los hijos de contenedores flex pierden el efecto de [hidden] en algunos
   navegadores: lo reforzamos para los iconos y textos alternativos. */
.csg-game [hidden] {
	display: none !important;
}

/* ---------- Cabecera ---------- */

.csg-game .csg-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.75rem;
	min-height: 58px;
	padding: 0.55rem 0.9rem;
	background: var(--csg-primary);
	border-radius: 12px 12px 0 0;
	color: #fff;
}

.csg-game .csg-score-box {
	display: flex;
	align-items: center;
	gap: 0.5rem;
	min-width: 76px;
	font-size: 1.15rem;
	font-weight: 700;
	line-height: 1;
}

.csg-game .csg-score {
	display: inline-block;
}

.csg-game .csg-score.csg-score--pop {
	animation: csg-pop 0.28s ease-out;
}

@keyframes csg-pop {
	0% { transform: scale(1); }
	40% { transform: scale(1.35); }
	100% { transform: scale(1); }
}

.csg-game .csg-record-box {
	flex: 1;
	text-align: center;
	font-size: 1rem;
	font-weight: 600;
	letter-spacing: 0.01em;
	white-space: nowrap;
}

.csg-game .csg-header-actions {
	display: flex;
	align-items: center;
	gap: 0.5rem;
	min-width: 76px;
	justify-content: flex-end;
}

.csg-game .csg-icon-btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 38px;
	height: 38px;
	padding: 0;
	border: 0;
	border-radius: 9px;
	background: rgba(255, 255, 255, 0.14);
	color: #fff;
	cursor: pointer;
	transition: background 0.15s ease, transform 0.1s ease;
}

.csg-game .csg-icon-btn:hover:not(:disabled) {
	background: rgba(255, 255, 255, 0.24);
}

.csg-game .csg-icon-btn:active:not(:disabled) {
	transform: scale(0.94);
}

.csg-game .csg-icon-btn:disabled {
	opacity: 0.45;
	cursor: default;
}

/* ---------- Tablero ---------- */

.csg-game .csg-board-wrap {
	position: relative;
	border: 1.5px solid var(--csg-border);
	border-top: 0;
	border-radius: 0 0 12px 12px;
	overflow: hidden;
	background: var(--csg-board-light);
	aspect-ratio: 1 / 1;
}

.csg-game .csg-canvas {
	display: block;
	width: 100%;
	height: 100%;
	touch-action: none; /* El JS controla el deslizamiento sobre el tablero. */
}

.csg-game .csg-canvas:focus-visible,
.csg-game button:focus-visible,
.csg-game input:focus-visible {
	outline: 3px solid #ffb703;
	outline-offset: 2px;
}

/* ---------- Capa superpuesta (inicio / pausa / fin) ---------- */

.csg-game .csg-overlay {
	position: absolute;
	inset: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 1rem;
	background: rgba(247, 244, 232, 0.82);
	backdrop-filter: blur(2px);
	-webkit-backdrop-filter: blur(2px);
	text-align: center;
	transition: opacity 0.18s ease;
}

.csg-game .csg-overlay.csg-overlay--hidden {
	opacity: 0;
	pointer-events: none;
	visibility: hidden;
}

.csg-game .csg-overlay-card {
	max-width: 340px;
}

.csg-game .csg-overlay-title {
	margin: 0 0 0.5rem;
	font-size: 1.65rem;
	font-weight: 800;
	color: var(--csg-primary);
	line-height: 1.15;
}

.csg-game .csg-overlay-text {
	margin: 0 0 1rem;
	font-size: 0.98rem;
	line-height: 1.45;
	color: #33544a;
}

.csg-game .csg-overlay-score {
	margin: 0 0 1rem;
	font-size: 1.1rem;
	color: #17352c;
}

.csg-game .csg-overlay-new-record {
	margin: 0 0 0.5rem;
	font-size: 1.05rem;
	font-weight: 800;
	color: #c1271b;
}

.csg-game .csg-btn-primary {
	display: inline-block;
	padding: 0.72rem 2rem;
	border: 0;
	border-radius: 999px;
	background: var(--csg-primary);
	color: #fff;
	font-size: 1.02rem;
	font-weight: 700;
	cursor: pointer;
	box-shadow: 0 4px 14px rgba(14, 75, 62, 0.3);
	transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

.csg-game .csg-btn-primary:hover {
	filter: brightness(1.12);
	transform: translateY(-1px);
}

.csg-game .csg-btn-primary:active {
	transform: translateY(0) scale(0.97);
}

.csg-game .csg-btn-secondary {
	padding: 0.55rem 1.1rem;
	border: 2px solid var(--csg-primary);
	border-radius: 999px;
	background: transparent;
	color: var(--csg-primary);
	font-size: 0.92rem;
	font-weight: 700;
	cursor: pointer;
}

.csg-game .csg-btn-secondary:hover {
	background: var(--csg-primary);
	color: #fff;
}

.csg-game .csg-leaderboard-form {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
	justify-content: center;
	margin-top: 1rem;
}

.csg-game .csg-leaderboard-form[hidden] {
	display: none;
}

.csg-game .csg-leaderboard-name {
	flex: 1 1 150px;
	max-width: 200px;
	padding: 0.55rem 0.8rem;
	border: 1.5px solid #9db8ad;
	border-radius: 999px;
	font-size: 0.95rem;
	background: #fff;
	color: #17352c;
}

.csg-game .csg-leaderboard-msg {
	flex-basis: 100%;
	margin: 0.25rem 0 0;
	font-size: 0.9rem;
	font-weight: 600;
}

/* ---------- Controles táctiles ---------- */

.csg-game .csg-pad {
	display: none;
	grid-template-columns: repeat(3, 62px);
	grid-template-rows: repeat(3, 62px);
	grid-template-areas:
		". arriba ."
		"izquierda . derecha"
		". abajo .";
	gap: 6px;
	justify-content: center;
	margin-top: 0.9rem;
}

.csg-game .csg-pad-up { grid-area: arriba; }
.csg-game .csg-pad-left { grid-area: izquierda; }
.csg-game .csg-pad-down { grid-area: abajo; }
.csg-game .csg-pad-right { grid-area: derecha; }

.csg-game .csg-pad-btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 62px;
	height: 62px;
	padding: 0;
	border: 1.5px solid var(--csg-primary);
	border-radius: 16px;
	background: #fff;
	color: var(--csg-primary);
	cursor: pointer;
	touch-action: none;
	user-select: none;
	-webkit-user-select: none;
	transition: background 0.1s ease, color 0.1s ease, transform 0.08s ease;
}

.csg-game .csg-pad-btn:active,
.csg-game .csg-pad-btn.csg-pad-btn--active {
	background: var(--csg-primary);
	color: #fff;
	transform: scale(0.95);
}

/* Siempre visibles si el ajuste es «Mostrar siempre». */
.csg-game.csg-controls-always .csg-pad {
	display: grid;
}

/* Automático: solo pantallas táctiles o estrechas. */
@media (pointer: coarse), (max-width: 640px) {
	.csg-game.csg-controls-auto .csg-pad {
		display: grid;
	}
}

/* ---------- Ayuda y accesibilidad ---------- */

.csg-game .csg-help {
	margin: 0.65rem 0 0;
	text-align: center;
	font-size: 0.82rem;
	color: #5c7268;
}

.csg-game .screen-reader-text {
	position: absolute !important;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

@media (max-width: 420px) {
	.csg-game .csg-header {
		min-height: 52px;
		padding: 0.45rem 0.6rem;
		gap: 0.4rem;
	}

	.csg-game .csg-score-box {
		min-width: 58px;
		font-size: 1.05rem;
	}

	.csg-game .csg-record-box {
		font-size: 0.92rem;
	}

	.csg-game .csg-header-actions {
		min-width: 58px;
	}

	.csg-game .csg-icon-btn {
		width: 34px;
		height: 34px;
	}

	.csg-game .csg-overlay-title {
		font-size: 1.4rem;
	}

	.csg-game .csg-help {
		display: none;
	}
}

@media (prefers-reduced-motion: reduce) {
	.csg-game .csg-score.csg-score--pop {
		animation: none;
	}

	.csg-game .csg-overlay,
	.csg-game .csg-icon-btn,
	.csg-game .csg-btn-primary,
	.csg-game .csg-pad-btn {
		transition: none;
	}
}
