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

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface2: #0f3460;
  --accent: #e94560;
  --accent2: #533483;
  --text: #eee;
  --text-dim: #999;
  --gold: #ffd700;
  --silver: #c0c0c0;
  --black-rep: #444;
  --p0: #e94560;
  --p1: #4ecdc4;
  --p2: #ffe66d;
  --p3: #a78bfa;
  /* Board: 13 cells + 12 gaps(1px) + 2 border(2px) + 2 padding(1px) = 13*cell + 18px */
  --cell-size: calc((100vw - 2rem - 18px) / 13);
}

html { font-size: 16px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

.screen { display: none; flex-direction: column; align-items: center; padding: 1rem; flex: 1; }
.screen.active { display: flex; }

#home { justify-content: center; gap: 1.5rem; text-align: center; }
#home h1 { font-size: 2.5rem; letter-spacing: 0.1em; color: var(--accent); }
#home p { color: var(--text-dim); max-width: 320px; }

.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 0.5rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
  color: #fff;
  min-width: 200px;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(0.96); }
.btn-primary { background: var(--accent); }
.btn-secondary { background: var(--surface2); }

input, select {
  padding: 0.75rem;
  border: 2px solid var(--surface2);
  border-radius: 0.5rem;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  width: 100%;
  max-width: 280px;
}
input:focus, select:focus { outline: none; border-color: var(--accent); }

.form-group { display: flex; flex-direction: column; gap: 0.5rem; align-items: center; }
.form-group label { color: var(--text-dim); font-size: 0.875rem; }

.name-hint {
  color: var(--accent);
  font-size: 0.8rem;
  min-height: 1.2em;
  transition: opacity 0.2s;
}

/* Lobby */
#lobby { justify-content: center; gap: 1rem; text-align: center; }
.game-code {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.3em;
  color: var(--gold);
  background: var(--surface);
  padding: 0.75rem 1.5rem;
  border-radius: 0.5rem;
  cursor: pointer;
}
.game-code:active { opacity: 0.7; }
.player-list { list-style: none; display: flex; flex-direction: column; gap: 0.5rem; }
.player-list li {
  padding: 0.5rem 1rem;
  background: var(--surface);
  border-radius: 0.5rem;
  min-width: 200px;
}
.waiting-dots::after { content: ''; animation: dots 1.5s infinite; }
@keyframes dots {
  0% { content: '.'; }
  33% { content: '..'; }
  66% { content: '...'; }
}

/* Toast */
.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface2);
  color: var(--text);
  padding: 0.5rem 1.25rem;
  border-radius: 2rem;
  font-size: 0.85rem;
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
  z-index: 200;
}
.toast.show { opacity: 1; }

/* Game Board */
#game-screen { padding: 0.5rem 1rem; gap: 0.5rem; }

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 520px;
  padding: 0.25rem 0;
}
.game-header .code { color: var(--text-dim); font-size: 0.75rem; cursor: pointer; }
.game-header .turn-info { font-weight: 600; font-size: 0.9rem; }
.btn-quit {
  background: transparent;
  border: 1px solid var(--text-dim);
  color: var(--text-dim);
  font-size: 0.7rem;
  padding: 0.2rem 0.5rem;
  border-radius: 0.3rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn-quit:active { opacity: 0.6; }

.scoreboard {
  display: flex;
  gap: 0.35rem;
  width: 100%;
  max-width: 520px;
  flex-wrap: wrap;
  justify-content: center;
}
.score-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.3rem 0.5rem;
  border-radius: 0.5rem;
  background: var(--surface);
  font-size: 0.75rem;
  min-width: 60px;
  border: 2px solid transparent;
  transition: border-color 0.2s;
}
.score-card.active { border-color: var(--accent); }
.score-card.is-me .name { text-decoration: underline; text-underline-offset: 2px; }
.score-card .name { font-weight: 600; margin-bottom: 0.1rem; }
.score-card .score { font-size: 1rem; font-weight: 700; }
.score-card .repellers { font-size: 0.65rem; color: var(--text-dim); }

.board-container {
  position: relative;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.board {
  display: grid;
  grid-template-columns: repeat(13, var(--cell-size));
  grid-template-rows: repeat(13, var(--cell-size));
  gap: 1px;
  background: var(--surface);
  border: 2px solid var(--surface2);
  border-radius: 0.375rem;
  padding: 1px;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  position: relative;
  cursor: default;
  transition: background 0.15s;
  border-radius: 2px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Board number in each cell */
.cell-number {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.55);
  color: rgba(255, 255, 255, 0.35);
  font-weight: 700;
  pointer-events: none;
  line-height: 1;
}

/* Start field highlight (setup phase) */
.cell.start-field {
  background: rgba(76, 175, 80, 0.08);
}

.cell.valid-move {
  cursor: pointer;
  outline: 2.5px solid var(--accent);
  outline-offset: -2.5px;
}
.cell.valid-move:active { background: rgba(233, 69, 96, 0.25); }

/* Push step 1: selectable pieces (cyan pulsing outline) */
.cell.push-selectable {
  cursor: pointer;
}
.cell.push-selectable::before {
  content: '';
  position: absolute;
  inset: 1px;
  border: 2.5px solid #00e5ff;
  border-radius: 4px;
  z-index: 3;
  animation: pushPulse 1s infinite;
}

/* Push step 2: selected piece */
.cell.push-selected {
  cursor: pointer;
  background: rgba(0, 229, 255, 0.15);
}
.cell.push-selected::before {
  content: '';
  position: absolute;
  inset: 1px;
  border: 2.5px solid #00e5ff;
  border-radius: 4px;
  z-index: 3;
}

/* Push step 2: direction options (where the piece would go) */
.cell.push-direction {
  cursor: pointer;
}
.cell.push-direction::before {
  content: '';
  position: absolute;
  inset: 1px;
  border: 2.5px solid #00e5ff;
  border-radius: 4px;
  z-index: 3;
  animation: pushPulse 0.8s infinite;
}

@keyframes pushPulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

.cell .piece {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 80%;
  height: 80%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.35);
  font-weight: 700;
  color: #fff;
  transition: transform 0.2s;
  z-index: 1;
}

/* Black repeller: ring with hole so the number shows through */
.piece.black {
  background: transparent;
  border: calc(var(--cell-size) * 0.12) solid #555;
  box-shadow: 0 0 0 1px #333;
}

/* Silver & gold: solid, cover the number */
.piece.silver { background: var(--silver); color: #333; border: 1.5px solid #ddd; }
.piece.gold { background: var(--gold); color: #333; border: 1.5px solid #ffeb3b; box-shadow: 0 0 6px rgba(255, 215, 0, 0.4); }

.piece.player {
  border: 2.5px solid;
  position: relative;
}
.piece.player.is-me {
  box-shadow: 0 0 0 1.5px var(--bg), 0 0 0 3px currentColor;
}
.piece.player::after {
  content: attr(data-repellers);
  position: absolute;
  bottom: -2px;
  right: -2px;
  font-size: 0.5rem;
  background: rgba(0,0,0,0.75);
  border-radius: 50%;
  width: 12px;
  height: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  line-height: 1;
}

.piece.p0 { background: var(--p0); border-color: #ff6b81; color: #fff; }
.piece.p1 { background: var(--p1); border-color: #7ee8de; color: #1a1a2e; }
.piece.p2 { background: var(--p2); border-color: #fff09e; color: #1a1a2e; }
.piece.p3 { background: var(--p3); border-color: #c4b5fd; color: #fff; }

/* Push info bar */
.push-info {
  width: 100%;
  max-width: 520px;
  padding: 0.5rem;
  background: rgba(0, 229, 255, 0.1);
  border: 1px solid rgba(0, 229, 255, 0.3);
  border-radius: 0.5rem;
  font-size: 0.8rem;
  text-align: center;
  color: #00e5ff;
}
.btn-push-off {
  display: inline-block;
  margin-top: 0.4rem;
  padding: 0.4rem 1rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 0.4rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn-push-off:active { transform: scale(0.96); }

/* Event log */
.event-log {
  width: 100%;
  max-width: 520px;
  max-height: 60px;
  overflow-y: auto;
  padding: 0.4rem 0.5rem;
  background: var(--surface);
  border-radius: 0.5rem;
  font-size: 0.7rem;
  color: var(--text-dim);
}
.event-log div { padding: 0.1rem 0; }
.event-log:empty { display: none; }

/* Game over */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}
.overlay.active { display: flex; }
.overlay-content {
  background: var(--surface);
  padding: 2rem;
  border-radius: 1rem;
  text-align: center;
  max-width: 320px;
  width: 100%;
}
.overlay-content h2 { font-size: 1.5rem; margin-bottom: 1rem; color: var(--gold); }
.overlay-content .final-scores { margin: 1rem 0; }
.overlay-content .final-scores div { padding: 0.3rem 0; }

/* My games */
.my-games { width: 100%; max-width: 320px; }
.my-games h3 { margin-bottom: 0.5rem; font-size: 0.9rem; color: var(--text-dim); }
.game-link {
  display: block;
  padding: 0.5rem;
  background: var(--surface);
  border-radius: 0.5rem;
  margin-bottom: 0.5rem;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.game-link:active { background: var(--surface2); }
.game-link .meta { font-size: 0.75rem; color: var(--text-dim); }

/* Responsive */
@media (min-width: 500px) {
  :root { --cell-size: min(calc((100vw - 2rem - 18px) / 13), 40px); }
}
@media (min-width: 700px) {
  :root { --cell-size: min(calc((100vw - 2rem - 18px) / 13), 46px); }
  .game-header, .scoreboard, .event-log, .push-info { max-width: 600px; }
}

@media (max-width: 359px) {
  #game-screen { padding: 0.25rem 0.5rem; gap: 0.25rem; }
  :root { --cell-size: calc((100vw - 1rem - 18px) / 13); }
  .scoreboard { gap: 0.25rem; }
  .score-card { padding: 0.2rem 0.35rem; min-width: 50px; font-size: 0.65rem; }
  .score-card .score { font-size: 0.85rem; }
}
