:root {
  --bg-dark: #0d0f1a;
  --bg-panel: #1a1d33;
  --text: #e0e8ff;
  --muted: #a0a8c8;
  --accent-blue: #6fa8ff;
  --accent-pink: #ff7ac2;
  --accent-green: #6fe3a2;
  --accent-red: #ff6f6f;
  --accent-yellow: #ffd86b;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: radial-gradient(circle at top, #1e2245, var(--bg-dark) 70%);
  color: var(--text);
  font-family: "Lexend", sans-serif;
  height: 100%;
  overflow: hidden;
}

/* Header & Layout */
.app-header {
  padding: 10px 16px;
  border-bottom: 1px solid #303560;
}

.title-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.limit-badge {
  font-size: 28px;
  font-weight: 900;
  color: var(--accent-blue);
  line-height: 1;
  text-shadow: 0 0 8px var(--accent-blue);
}

.game-title {
  font-size: 28px;
  font-weight: 900;
  background: linear-gradient(45deg, var(--accent-blue), var(--accent-pink));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  line-height: 1;
}

.score-display {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-family: "Space Mono", monospace;
}

.score-label {
  font-size: 14px;
  color: var(--muted);
}

.score-value {
  font-size: 28px;
  font-weight: 900;
  color: var(--accent-green);
  text-shadow: 0 0 10px var(--accent-green);
}

.hint-btn {
  appearance: none;
  border: 1px solid #303560;
  background: #1e2340;
  color: var(--text);
  border-radius: 999px;
  width: 28px;
  height: 28px;
  font-weight: 900;
  font-size: 14px;
  line-height: 28px;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: all 0.2s;
}

.hint-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 0 20px var(--accent-blue);
}

.subtitle {
  color: var(--muted);
  font-size: 11px;
  margin-top: 4px;
}

.layout {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 12px;
  padding: 12px;
  height: calc(100vh - 75px);
}

.left-panel,
.right-panel {
  height: 100%;
}

.left-panel {
  background: #000;
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 1px solid #303560;
  position: relative;
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
}

.right-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding-right: 4px;
}

.right-panel::-webkit-scrollbar {
  width: 8px;
}

.right-panel::-webkit-scrollbar-track {
  background: var(--bg-dark);
  border-radius: 4px;
}

.right-panel::-webkit-scrollbar-thumb {
  background: #303560;
  border-radius: 4px;
}

.right-panel::-webkit-scrollbar-thumb:hover {
  background: #4a4f80;
}

/* Panels */
.status-box,
.compass-box,
.move-box,
.legend-box,
.leaderboard-box {
  background: var(--bg-panel);
  border: 1px solid #303560;
  border-radius: 12px;
  padding: 12px;
  box-shadow: var(--shadow);
}

.status-box {
  display: flex;
  justify-content: space-around;
  text-align: center;
}

.status-item {
  display: flex;
  flex-direction: column;
}

.status-item .label {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 4px;
}

.status-item .value {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent-green);
  font-family: "Space Mono";
}

/* Compass */
.compass-header,
.move-header,
.legend-header,
.leaderboard-header {
  font-weight: 700;
  margin-bottom: 8px;
  text-align: center;
  color: var(--accent-blue);
  font-size: 14px;
}

.compass-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: #262a47;
  border-radius: 8px;
}

.compass-north,
.compass-south {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.compass-middle {
  display: flex;
  align-items: center;
  gap: 16px;
}

.compass-west,
.compass-east {
  display: flex;
  align-items: center;
  gap: 4px;
}

.compass-center {
  font-size: 32px;
}

.compass-arrow {
  font-size: 24px;
  color: var(--accent-yellow);
}

.compass-label {
  font-weight: 700;
  font-size: 16px;
  color: var(--accent-blue);
  font-family: "Space Mono";
}

/* Move Box */
.move-inputs {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 12px;
}

.move-box label {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.input-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.count-input,
.direction-input {
  width: 100%;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid #4a4f80;
  background: var(--bg-dark);
  color: var(--text);
  font-family: "Space Mono";
  font-size: 16px;
  transition: all 0.2s;
}

.count-input:focus,
.direction-input:focus {
  outline: none;
  border-color: var(--accent-blue);
  box-shadow: 0 0 8px rgba(111, 168, 255, 0.3);
}

.direction-input {
  cursor: pointer;
}

.note {
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  margin-top: 6px;
}

/* Legend */
.legend-items {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #262a47;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
}

.legend-icon {
  font-size: 20px;
  width: 24px;
  text-align: center;
}

.yellow-dot {
  color: var(--accent-yellow);
}

.player-dot {
  color: var(--accent-pink);
}

/* Leaderboard */
.leaderboard-list {
  display: grid;
  gap: 4px;
  font-family: "Space Mono";
  font-size: 12px;
  max-height: 250px;
  overflow-y: auto;
}

.leaderboard-list::-webkit-scrollbar {
  width: 6px;
}

.leaderboard-list::-webkit-scrollbar-track {
  background: var(--bg-dark);
  border-radius: 3px;
}

.leaderboard-list::-webkit-scrollbar-thumb {
  background: #303560;
  border-radius: 3px;
}

.leaderboard-list .row {
  display: flex;
  justify-content: space-between;
  background: #262a47;
  padding: 6px 10px;
  border-radius: 6px;
  align-items: center;
}

.leaderboard-list .row .rank {
  font-weight: 700;
  color: var(--accent-blue);
  min-width: 30px;
}

.leaderboard-list .row .name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-list .row .score {
  color: var(--accent-green);
  font-weight: 700;
}

/* Buttons */
button.primary {
  width: 100%;
  background: linear-gradient(45deg, var(--accent-blue), var(--accent-pink));
  border: none;
  color: #fff;
  padding: 10px;
  border-radius: 8px;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  text-shadow: 0 1px 3px #0005;
  transition: all 0.2s;
}

button.primary:hover {
  transform: scale(1.02);
  box-shadow: 0 4px 20px rgba(111, 168, 255, 0.5);
}

button.primary:active {
  transform: scale(0.98);
}

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  background: #0d0f1acc;
  backdrop-filter: blur(8px);
  z-index: 100;
  overflow-y: auto;
  padding: 20px;
}

.overlay.visible {
  display: grid;
}

.card {
  background: var(--bg-panel);
  border-radius: 16px;
  padding: 24px;
  width: min(540px, 92vw);
  text-align: center;
  box-shadow: var(--shadow);
  border: 1px solid #303560;
}

.card h2 {
  margin-top: 0;
  background: linear-gradient(45deg, var(--accent-blue), var(--accent-pink));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.card.setup input,
.card.setup select {
  width: 100%;
  padding: 10px;
  margin-top: 4px;
  border-radius: 8px;
  border: 1px solid #4a4f80;
  background: var(--bg-dark);
  color: var(--text);
  font-family: "Space Mono";
  transition: all 0.2s;
}

.card.setup input:focus,
.card.setup select:focus {
  outline: none;
  border-color: var(--accent-blue);
  box-shadow: 0 0 8px rgba(111, 168, 255, 0.3);
}

.card.setup select {
  cursor: pointer;
}

.card.setup label {
  display: block;
  margin-bottom: 12px;
  text-align: left;
  font-weight: 600;
}

/* Avatar Selector */
.avatar-selector {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-top: 8px;
  padding: 8px;
  background: var(--bg-dark);
  border-radius: 8px;
  border: 1px solid #4a4f80;
}

.avatar-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px;
  border-radius: 8px;
  border: 2px solid transparent;
  background: #262a47;
  cursor: pointer;
  transition: all 0.2s;
}

.avatar-option:hover {
  background: #2e3351;
  border-color: var(--accent-blue);
  transform: scale(1.05);
}

.avatar-option.selected {
  background: linear-gradient(45deg, var(--accent-blue), var(--accent-pink));
  border-color: transparent;
  box-shadow: 0 0 12px rgba(111, 168, 255, 0.5);
}

.avatar-option img {
  width: 48px;
  height: 48px;
  display: block;
}

.avatar-option span {
  font-size: 10px;
  color: var(--text);
  font-weight: 600;
  text-align: center;
}

.avatar-option.selected span {
  color: white;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.tips-list {
  margin: 12px 0 16px;
  padding-left: 20px;
  text-align: left;
  font-size: 14px;
  line-height: 1.6;
}

.tips-list li {
  margin-bottom: 6px;
}

.instructions-text {
  font-size: 14px;
  color: var(--text);
  text-align: left;
  line-height: 1.6;
  margin-bottom: 12px;
}

.countdown {
  font-family: "Space Mono";
  color: var(--accent-pink);
  margin: 12px 0;
  font-size: 16px;
  font-weight: 700;
}

/* Ladder & Level Complete */
.level-score-display {
  display: flex;
  justify-content: center;
  margin: 20px 0;
}

.level-score-display .score-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.level-score-display .label {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.level-score-display .value {
  font-size: 32px;
  font-weight: 900;
  color: var(--accent-green);
  font-family: "Space Mono";
  text-shadow: 0 0 12px var(--accent-green);
}

.ladder-message {
  margin: 16px 0;
  line-height: 1.6;
}

.ladder-steps {
  font-family: "Space Mono";
  font-size: 18px;
  font-weight: 700;
  color: var(--accent-yellow);
  margin-top: 12px;
}

/* Game Over */
.final-score-display {
  margin: 16px 0;
  font-size: 20px;
}

#finalScore {
  color: var(--accent-green);
  font-weight: 900;
  font-size: 36px;
  font-family: "Space Mono";
  text-shadow: 0 0 12px var(--accent-green);
}

.final-level-display {
  margin: 8px 0;
  font-size: 16px;
  color: var(--muted);
}

#finalLevel {
  color: var(--accent-blue);
  font-weight: 700;
  font-size: 24px;
  font-family: "Space Mono";
}

.gameover-message {
  margin: 16px 0;
  line-height: 1.6;
  color: var(--muted);
}

/* Responsive */
@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .left-panel {
    min-height: 500px;
  }

  body {
    overflow: auto;
  }

  .game-title {
    font-size: 24px;
  }

  .limit-badge {
    font-size: 24px;
  }

  .score-value {
    font-size: 24px;
  }
}

@media (max-width: 600px) {
  .compass-middle {
    gap: 8px;
  }

  .compass-center {
    font-size: 24px;
  }

  .compass-arrow {
    font-size: 20px;
  }

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

  .avatar-option img {
    width: 40px;
    height: 40px;
  }

  .avatar-option span {
    font-size: 9px;
  }
}

@media (max-width: 400px) {
  .avatar-selector {
    grid-template-columns: repeat(2, 1fr);
  }
}
