* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #f0ebe3;
  --hud-bg: #e6dfd5;
  --floor: #faf7f2;
  --wall: #c4b8a8;
  --wall-top: #d4c9bb;
  --player: #a8c8e8;
  --player-dark: #8ab0d4;
  --box: #f0c89a;
  --box-dark: #ddb07a;
  --goal: #b8dca0;
  --goal-glow: rgba(184, 220, 160, 0.5);
  --btn-bg: #d6cec4;
  --btn-active: #c4bab0;
  --text: #5a5248;
  --text-light: #8a7e72;
  --gate-red: #e8a8a8;
  --gate-red-glow: rgba(232, 168, 168, 0.5);
  --gate-blue: #a8b8e8;
  --gate-blue-glow: rgba(168, 184, 232, 0.5);
  --gate-yellow: #e8dca0;
  --gate-yellow-glow: rgba(232, 220, 160, 0.5);
  --box-red: #e8a0a0;
  --box-red-dark: #d08080;
  --box-blue: #a0b0e0;
  --box-blue-dark: #8098c8;
  --box-yellow: #e8d890;
  --box-yellow-dark: #d0c070;
  --crumble: #e8e0d4;
  --crumble-crack: #c4b8a8;
  --pit: #8a7e72;
  --pit-dark: #6a5e52;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  font-family: 'Segoe UI', system-ui, sans-serif;
  color: var(--text);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 500px;
  margin: 0 auto;
}

#hud {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: var(--hud-bg);
  font-size: 16px;
  font-weight: 600;
}

#game-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-light);
  letter-spacing: 0.5px;
}

#level-name {
  font-size: 18px;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 3px;
}

#grid-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  min-height: 0;
  position: relative;
}

#grid {
  display: grid;
  gap: 2px;
  background: #d6cec4;
  border-radius: 8px;
  padding: 2px;
  position: relative;
}

.cell {
  position: relative;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cell-empty {
  background: transparent;
}

.cell-floor {
  background: var(--floor);
}

.cell-wall {
  background: var(--wall);
  border-bottom: 3px solid #b0a494;
  border-top: 1px solid var(--wall-top);
}

.cell-goal {
  background: var(--floor);
}

.cell-goal::after {
  content: '';
  position: absolute;
  width: 40%;
  height: 40%;
  background: var(--goal);
  border-radius: 50%;
  box-shadow: 0 0 8px var(--goal-glow), 0 0 16px var(--goal-glow);
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 0.8; }
  50% { transform: scale(1.2); opacity: 1; }
}

.entity {
  position: absolute;
  border-radius: 6px;
  transition: left 0.12s ease-out, top 0.12s ease-out;
  z-index: 2;
}

.entity-bump {
  animation: bump 0.2s ease;
}

@keyframes bump {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(var(--bump-x, 0), var(--bump-y, 0)); }
  50% { transform: translate(0, 0); }
  75% { transform: translate(var(--bump-x, 0), var(--bump-y, 0)); }
}

.entity-player {
  background: radial-gradient(circle at 40% 35%, #c0daf0, var(--player) 60%, var(--player-dark));
  border-radius: 50%;
  border: 2px solid var(--player-dark);
  border-bottom: 3px solid #7a9cb8;
  box-shadow: 0 2px 4px rgba(0,0,0,0.15), inset 0 1px 2px rgba(255,255,255,0.4);
}

.entity-player::after {
  content: '';
  position: absolute;
  width: 40%;
  height: 20%;
  background: rgba(255,255,255,0.35);
  border-radius: 50%;
  top: 18%;
  left: 30%;
}

.entity-box {
  background: var(--box);
  border-bottom: 3px solid var(--box-dark);
  border-radius: 4px;
}

.entity-box::after {
  content: '';
  position: absolute;
  top: 15%;
  left: 15%;
  right: 15%;
  bottom: 25%;
  border: 2px solid var(--box-dark);
  border-radius: 2px;
  opacity: 0.4;
}

.entity-box-red {
  background: var(--box-red);
  border-bottom-color: var(--box-red-dark);
}
.entity-box-red::after {
  border-color: var(--box-red-dark);
}

.entity-box-blue {
  background: var(--box-blue);
  border-bottom-color: var(--box-blue-dark);
}
.entity-box-blue::after {
  border-color: var(--box-blue-dark);
}

.entity-box-yellow {
  background: var(--box-yellow);
  border-bottom-color: var(--box-yellow-dark);
}
.entity-box-yellow::after {
  border-color: var(--box-yellow-dark);
}

.cell-gate {
  background: var(--floor);
}

.cell-gate::after {
  content: '';
  position: absolute;
  width: 50%;
  height: 50%;
  border-radius: 4px;
  transform: rotate(45deg);
  animation: pulse 1.5s ease-in-out infinite;
}

.cell-gate-red::after {
  background: var(--gate-red);
  box-shadow: 0 0 8px var(--gate-red-glow), 0 0 16px var(--gate-red-glow);
}

.cell-gate-blue::after {
  background: var(--gate-blue);
  box-shadow: 0 0 8px var(--gate-blue-glow), 0 0 16px var(--gate-blue-glow);
}

.cell-gate-yellow::after {
  background: var(--gate-yellow);
  box-shadow: 0 0 8px var(--gate-yellow-glow), 0 0 16px var(--gate-yellow-glow);
}

.cell-crumble {
  background: var(--crumble);
}

.cell-crumble::before,
.cell-crumble::after {
  content: '';
  position: absolute;
  background: var(--crumble-crack);
  border-radius: 1px;
}

.cell-crumble::before {
  width: 40%;
  height: 2px;
  top: 35%;
  left: 20%;
  transform: rotate(-15deg);
}

.cell-crumble::after {
  width: 30%;
  height: 2px;
  top: 60%;
  left: 45%;
  transform: rotate(25deg);
}

.cell-pit {
  background: var(--pit);
  border: 2px inset var(--pit-dark);
  box-shadow: inset 0 2px 6px rgba(0,0,0,0.3);
  animation: crumbleAway 0.3s ease;
}

@keyframes crumbleAway {
  0% { background: var(--crumble); transform: scale(1); }
  50% { transform: scale(0.95); }
  100% { background: var(--pit); transform: scale(1); }
}

#controls {
  padding: 12px 20px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

#dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.dpad-row {
  display: flex;
  gap: 4px;
}

.dpad-btn {
  width: 64px;
  height: 64px;
  border: none;
  border-radius: 12px;
  background: var(--btn-bg);
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.1s;
}

.dpad-btn:active {
  background: var(--btn-active);
  transform: scale(0.95);
}

.dpad-center {
  background: transparent;
  cursor: default;
}

#action-btns {
  display: flex;
  gap: 16px;
  width: 100%;
  justify-content: center;
}

#action-btns button {
  padding: 14px 28px;
  border: none;
  border-radius: 12px;
  background: var(--btn-bg);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  min-width: 110px;
}

#action-btns button:active {
  background: var(--btn-active);
}

/* Checkmark overlay */
#checkmark-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(240, 235, 227, 0.75);
  z-index: 10;
  animation: fadeIn 0.3s ease;
}

#checkmark-overlay.hidden {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

#checkmark-svg {
  width: 120px;
  height: 120px;
}

.checkmark-circle {
  fill: none;
  stroke: var(--goal);
  stroke-width: 6;
  stroke-dasharray: 276;
  stroke-dashoffset: 276;
  animation: drawCircle 0.5s ease forwards;
}

.checkmark-path {
  fill: none;
  stroke: #6ab04c;
  stroke-width: 7;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 80;
  stroke-dashoffset: 80;
  animation: drawCheck 0.4s 0.4s ease forwards;
}

@keyframes drawCircle {
  to { stroke-dashoffset: 0; }
}

@keyframes drawCheck {
  to { stroke-dashoffset: 0; }
}

/* Win challenge arrows */
#win-challenge {
  margin-top: 16px;
  opacity: 0;
  animation: fadeIn 0.3s 0.8s ease forwards;
}

#challenge-arrows {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.challenge-arrow {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  background: var(--btn-bg);
  color: var(--text-light);
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, color 0.15s, transform 0.15s;
}

.challenge-arrow.done {
  background: var(--goal);
  color: #fff;
  transform: scale(1.1);
}

.challenge-arrow.active {
  background: var(--btn-active);
  animation: arrowPulse 0.8s ease-in-out infinite;
}

@keyframes arrowPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

#challenge-hint {
  text-align: center;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-light);
}

/* Key hints */
#key-hints {
  font-size: 12px;
  color: var(--text-light);
  text-align: center;
}

/* Level selector */
#level-select {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.2s ease;
}

#level-select.hidden {
  display: none;
}

#level-select-inner {
  background: var(--bg);
  border-radius: 16px;
  padding: 24px;
  max-width: 340px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

#level-select-title {
  font-size: 20px;
  font-weight: 700;
  text-align: center;
  margin-bottom: 16px;
}

#level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.level-tile {
  aspect-ratio: 1;
  border: none;
  border-radius: 10px;
  background: var(--btn-bg);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  -webkit-tap-highlight-color: transparent;
  position: relative;
}

.level-tile:active {
  background: var(--btn-active);
}

.level-tile.current {
  outline: 3px solid var(--player);
}

.level-tile .tile-check {
  font-size: 10px;
  color: #6ab04c;
}

#level-select-close {
  display: block;
  width: 100%;
  margin-top: 16px;
  padding: 12px;
  border: none;
  border-radius: 10px;
  background: var(--btn-bg);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

#complete-msg {
  text-align: center;
  margin-top: 8px;
}

#complete-msg.hidden {
  display: none;
}

#complete-title {
  font-size: 24px;
  font-weight: 800;
  color: var(--text);
  animation: bounceIn 0.5s 0.5s ease both;
}

#complete-text {
  font-size: 14px;
  color: var(--text-light);
  margin-top: 4px;
  opacity: 0;
  animation: fadeIn 0.3s 0.8s ease forwards;
}

@keyframes bounceIn {
  0% { transform: scale(0); opacity: 0; }
  60% { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}
