/* Layout strategy:
   - The canvas keeps its logical 900x520 internal coords (no math change in
     rendering or pathfinding). CSS scales the rendered size to fit the
     viewport while preserving aspect ratio.
   - All overlays (menu, lobby, game-over) are position:fixed over the whole
     viewport so they never clip to the canvas wrapper. This kills the
     "wicked scroll" where the menu's overflow bled inside the canvas. */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: #0a0e1a;
  color: #fff;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#stage {
  width: 100%;
  max-width: 1100px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}

#canvasWrap {
  position: relative;
  width: 100%;
  max-width: 900px;
  aspect-ratio: 900 / 520;
  background: linear-gradient(#1a2540, #0a1224);
  border: 2px solid #2c3e50;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

/* HUD overlays the canvas. The score panel pins to the right edge with a
   guaranteed minimum width so it never falls off small viewports. */
#hud {
  position: absolute;
  inset: 0;
  padding: 10px 14px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  pointer-events: none;
}

#players {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 auto;
  min-width: 0;
}
.barWrap {
  pointer-events: none;
}
.label {
  font-size: 10px;
  letter-spacing: 2px;
  opacity: 0.75;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 240px;
}
.bar {
  width: 220px;
  max-width: 100%;
  height: 12px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 3px;
  overflow: hidden;
}
.bar .fill {
  height: 100%;
  background: linear-gradient(#27ae60, #1e8449);
  transition: width 0.1s linear;
}

#scorePanel {
  flex: 0 0 auto;
  min-width: 76px;
  text-align: right;
}
#scorePanel .label { display: block; }
.bigNum {
  font-size: clamp(22px, 3.6vw, 36px);
  font-weight: 800;
  line-height: 1;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.7);
}
.subLabel {
  font-size: 11px;
  opacity: 0.65;
  letter-spacing: 1px;
  margin-top: 4px;
  white-space: nowrap;
}

#banner {
  position: absolute;
  top: 70px; left: 0; right: 0;
  text-align: center;
  pointer-events: none;
  font-weight: 600;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.7);
}

#connStatus {
  position: absolute;
  bottom: 8px; left: 12px;
  font-size: 11px;
  letter-spacing: 2px;
  pointer-events: none;
}
#connStatus.connected { color: #7ed957; }
#connStatus.disconnected { color: #ff6b6b; }

/* Live in-game leaderboard pinned to the bottom-right of the canvas. */
#liveLeaderboard {
  position: absolute;
  right: 12px;
  bottom: 28px;
  width: 180px;
  max-width: 40%;
  background: rgba(8, 12, 24, 0.78);
  border: 1px solid rgba(241, 196, 15, 0.4);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 11px;
  pointer-events: none;
  color: #fff;
  z-index: 5;
}
#liveLeaderboard[hidden] { display: none; }
.liveLbHeader {
  font-size: 10px;
  letter-spacing: 2px;
  opacity: 0.75;
  margin-bottom: 4px;
  color: #f1c40f;
}
#liveLbList {
  list-style: decimal;
  padding-left: 22px;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
#liveLbList li {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#liveLbList li.you {
  color: #f1c40f;
  font-weight: 700;
}

#help {
  width: 100%;
  max-width: 900px;
  padding: 10px 14px;
  font-size: 12px;
  background: rgba(8, 12, 24, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 6px;
  color: rgba(255, 255, 255, 0.78);
  line-height: 1.5;
}
kbd {
  background: #1f2937;
  padding: 1px 6px;
  border-radius: 3px;
  border: 1px solid #34495e;
  font-family: ui-monospace, monospace;
  font-size: 11px;
}

/* ===== Overlays ===== */

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 50;
  background: radial-gradient(ellipse at center, rgba(8, 12, 24, 0.85), rgba(8, 12, 24, 0.96));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#menuOverlay,
#lobbyOverlay,
#gameOverOverlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: clamp(16px, 4vw, 48px);
  z-index: 50;
  background: radial-gradient(ellipse at center, rgba(8, 12, 24, 0.92), rgba(8, 12, 24, 0.98));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
#menuOverlay[hidden],
#lobbyOverlay[hidden],
#gameOverOverlay[hidden] { display: none; }

#menuCard {
  width: 100%;
  max-width: 540px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  align-items: stretch;
  margin: auto;
}

#menuCard h2 {
  margin: 0;
  font-size: clamp(28px, 4.5vw, 40px);
  letter-spacing: 4px;
  text-align: center;
}
.tagline {
  font-size: 11px;
  opacity: 0.7;
  letter-spacing: 3px;
  text-align: center;
  margin-bottom: 8px;
}

#nameRow {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: stretch;
}
#nameRow label {
  font-size: 11px;
  letter-spacing: 1.5px;
  opacity: 0.75;
}
input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  color: #fff;
  font: inherit;
  font-size: 15px;
}
input[type="text"]:focus {
  outline: none;
  border-color: #f1c40f;
  background: rgba(255, 255, 255, 0.10);
}

#modeList {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.modeBtn {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  column-gap: 14px;
  align-items: center;
  padding: 14px 18px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  color: #fff;
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.modeBtn:hover { background: rgba(255, 255, 255, 0.10); }
.modeBtn.primary { border-color: #f1c40f; }
.modeBtn .modeTitle {
  grid-column: 1;
  grid-row: 1;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.modeBtn .modeDesc {
  grid-column: 1;
  grid-row: 2;
  font-size: 11px;
  opacity: 0.65;
  margin-top: 4px;
}
.modeBtn .badge {
  grid-column: 2;
  grid-row: 1 / span 2;
  font-size: 11px;
  letter-spacing: 2px;
  padding: 6px 10px;
  background: rgba(241, 196, 15, 0.18);
  border-radius: 3px;
  color: #f1c40f;
  white-space: nowrap;
}

#customModeRow {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  padding: 12px 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
}
#customModeRow > span {
  font-size: 12px;
  opacity: 0.7;
  flex: 1 0 100%;
  margin-bottom: 4px;
}
#customModeRow select {
  flex: 1 1 auto;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  color: #fff;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
#customModeRow select:focus { outline: none; border-color: #f1c40f; }
#customModeRow select option { background: #1a2540; }
#customMaxPlayers {
  width: 54px;
  padding: 8px 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  color: #fff;
  font: inherit;
  font-size: 14px;
  text-align: center;
  -moz-appearance: textfield;
}
#customMaxPlayers:focus { outline: none; border-color: #f1c40f; }
#customMaxPlayers::-webkit-inner-spin-button,
#customMaxPlayers::-webkit-outer-spin-button { -webkit-appearance: none; }
.customLabel { font-size: 12px; opacity: 0.7; }

#joinRow {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
#joinRow span { font-size: 12px; opacity: 0.7; flex: 1 0 100%; }
#joinCodeInput {
  flex: 1 1 auto;
  text-transform: uppercase;
  letter-spacing: 3px;
  font-weight: 700;
  text-align: center;
}
button {
  padding: 8px 16px;
  background: #34495e;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 4px;
  color: #fff;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  letter-spacing: 1px;
  transition: background 0.15s;
}
button:hover { background: #4a6377; }
button.primary {
  background: #f1c40f;
  color: #1a1a1a;
  border-color: #f1c40f;
  font-weight: 700;
}
button.primary:hover { background: #f4d03f; }

#leaderboardPanel {
  margin-top: 6px;
}
#leaderboardPanel h3 {
  font-size: 13px;
  letter-spacing: 3px;
  margin: 0 0 8px;
  opacity: 0.85;
}
.lbTabs {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}
.lbTab {
  flex: 1;
  padding: 6px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 4px;
  font-size: 11px;
  letter-spacing: 1px;
  cursor: pointer;
  color: #fff;
}
.lbTab.active {
  background: rgba(241, 196, 15, 0.18);
  border-color: #f1c40f;
  color: #f1c40f;
}
#leaderboardList { padding-left: 24px; font-size: 13px; margin: 0; }
#leaderboardList li { margin: 2px 0; opacity: 0.85; }

/* ===== Lobby ===== */

#lobbyCard {
  width: 100%;
  max-width: 480px;
  padding: 24px;
  background: rgba(20, 28, 48, 0.85);
  border: 1px solid rgba(241, 196, 15, 0.4);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin: auto;
}
#lobbyCard h3 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 3px;
  text-align: center;
}
.lobbyMeta {
  font-size: 12px;
  opacity: 0.75;
  text-align: center;
}
.lobbyCodeBox {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 14px;
  background: rgba(0, 0, 0, 0.35);
  border-radius: 6px;
}
.lobbyCodeBox .codeLabel {
  font-size: 11px;
  letter-spacing: 2px;
  opacity: 0.7;
}
#roomCodeDisplay {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 6px;
  color: #f1c40f;
}
#lobbyPlayers {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#lobbyPlayers li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  font-size: 14px;
}
#lobbyPlayers li.empty {
  opacity: 0.5;
  font-style: italic;
}
.colorChip {
  width: 14px; height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, 0.4);
  flex: 0 0 auto;
}
.lobbyActions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* ===== Game over ===== */
#gameOverCard {
  width: 100%;
  max-width: 460px;
  padding: 32px 24px;
  background: rgba(20, 28, 48, 0.9);
  border: 1px solid rgba(192, 57, 43, 0.55);
  border-radius: 8px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin: auto;
}
#gameOverCard h2 {
  margin: 0;
  font-size: 36px;
  letter-spacing: 6px;
  color: #c0392b;
}
#gameOverReason {
  font-size: 14px;
  opacity: 0.75;
  letter-spacing: 1px;
}
#gameOverFinal {
  font-size: 18px;
  font-weight: 600;
  margin-top: 4px;
}

/* ===== Small viewport tweaks ===== */
@media (max-width: 540px) {
  #stage { padding: 8px; }
  .bar { width: 160px; }
  .label { max-width: 160px; }
  #lobbyCard, #menuCard, #gameOverCard { padding: 18px; }
}
