:root {
  --bg: #07080f;
  --panel: rgba(12, 16, 28, 0.75);
  --panel-border: rgba(148, 163, 184, 0.18);
  --fg: #f5f5ff;
  --muted: #9fa3c5;
  --accent: #7dd3fc;
  --p1: #ff6b6b;
  --p2: #4dd4ac;
  --p3: #ffd86b;
  --p4: #b98bff;
}

/* RESET */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }
body {
  height: 100%;
  overflow: hidden;
  color: var(--fg);
  font-family: "Inter", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

/* --- Top bar: centered title, actions flanking --- */

.top {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 0.4rem 0.75rem 0.5rem;
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}

.top-side {
  display: flex;
  align-items: center;
  gap: 8px;
}

.top-side-left  { justify-content: flex-start; }
.top-side-right { justify-content: flex-end; }

.top-center {
  text-align: center;
}

.top-center h1 {
  margin: 0;
  font-size: clamp(1.4rem, 3vw, 2.1rem);
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

/* --- Buttons (matches Hearts/Minesweeper pattern) --- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border-radius: 999px;
  border: 1px solid transparent;
  padding: 0.35rem 0.85rem;
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  background: transparent;
  color: var(--text, var(--fg));
  white-space: nowrap;
  transition: background 0.15s ease, border-color 0.15s ease,
    transform 0.08s ease;
}

.btn.ghost {
  border-color: rgba(255, 255, 255, 0.18);
  background: rgba(6, 11, 26, 0.9);
}

/* Uniform sizing for top-bar text buttons */
.top-side > .btn.ghost:not(.icon-btn):not(.lives-btn) {
  min-width: 5.2rem;
  justify-content: center;
}

.btn.ghost:hover {
  background: rgba(40, 50, 70, 0.8);
  transform: translateY(-1px);
}

/* Muted state */
.icon-btn[aria-pressed="true"] svg { opacity: 0.45; }

/* Mode select styled like a ghost button */
select.btn {
  appearance: none;
  -webkit-appearance: none;
  padding: 0.35rem 1.4rem 0.35rem 0.85rem;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6' fill='%239fa3c5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.55rem center;
  height: 100%;
  font-size: 0.78rem;
  line-height: 1;
}

/* --- Lives stepper --- */

.lives-stepper {
  display: inline-flex;
  align-items: center;
  gap: 0;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  background: rgba(6, 11, 26, 0.9);
  overflow: hidden;
}

.lives-btn {
  border: none !important;
  border-radius: 0 !important;
  padding: 0.35rem 0.55rem;
  font-size: 0.85rem;
  min-width: 1.6rem;
  text-align: center;
}

.lives-display {
  font-size: 0.78rem;
  font-weight: 600;
  min-width: 2.2rem;
  text-align: center;
  color: var(--fg);
  padding: 0 0.15rem;
}

.lives-stepper.locked {
  opacity: 0.45;
  pointer-events: none;
}

/* --- Bot toggles (inline in top bar) --- */

.bot-toggles {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  background: rgba(6, 11, 26, 0.9);
}

.bot-label {
  font-family: "Inter", system-ui, sans-serif;
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
  color: var(--fg);
  opacity: 1;
  margin-right: 2px;
}

.bot-group {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.bot-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  user-select: none;
}

.win-count {
  font-family: "Inter", system-ui, sans-serif;
  font-size: 0.65rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  min-width: 0.7em;
  text-align: center;
  color: var(--muted);
  opacity: 0.85;
}

/* Color the win counter to match its player */
.bot-group[data-player="left"]   .win-count { color: var(--p1); }
.bot-group[data-player="right"]  .win-count { color: var(--p2); }
.bot-group[data-player="top"]    .win-count { color: var(--p3); }
.bot-group[data-player="bottom"] .win-count { color: var(--p4); }

.bot-pill input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.bot-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.bot-dot.p1 { background: var(--p1); box-shadow: 0 0 5px var(--p1); }
.bot-dot.p2 { background: var(--p2); box-shadow: 0 0 5px var(--p2); }
.bot-dot.p3 { background: var(--p3); box-shadow: 0 0 5px var(--p3); }
.bot-dot.p4 { background: var(--p4); box-shadow: 0 0 5px var(--p4); }

.bot-slider {
  position: relative;
  width: 24px;
  height: 13px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.bot-slider::after {
  content: "";
  position: absolute;
  top: 1.5px;
  left: 1.5px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.2s ease, background 0.2s ease;
}

.bot-diff-select {
  appearance: none;
  -webkit-appearance: none;
  background: rgba(6, 11, 26, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  color: var(--fg);
  font-family: "Inter", system-ui, sans-serif;
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0;
  padding: 2px 14px 2px 6px;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='5'%3E%3Cpath d='M0 0l4 5 4-5' fill='%239fa3c5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 3px center;
}
.bot-diff-select:hover { border-color: var(--accent); }

.bot-pill input:checked + .bot-slider {
  background: rgba(125, 211, 252, 0.25);
  border-color: rgba(125, 211, 252, 0.4);
}

.bot-pill input:checked + .bot-slider::after {
  transform: translateX(11px);
  background: var(--accent);
}

/* --- Wrapper: fills remaining space, centers game --- */

.wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: clip;
  min-height: 0;
  pointer-events: none;
}

/* GAME CONTAINER — square, fills available space below header */
.game-container {
  --size: min(96vw, calc(100dvh - 70px));
  position: relative;
  width: var(--size);
  height: var(--size);
  max-width: 820px;
  max-height: 820px;
  pointer-events: auto;
}

canvas {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
  display: block;
  background: transparent;
  pointer-events: none;
}

/* --- Controls modal --- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-overlay[hidden] { display: none; }

.modal {
  background: #0f1220;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 1.2rem 1.5rem;
  max-width: 400px;
  width: 90vw;
  color: var(--fg);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}
.modal-header h2 {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.06em;
}
.modal-body {
  padding: 0;
}
.modal-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
}
.modal-close:hover { color: var(--fg); }

.key-grid {
  display: grid;
  grid-template-columns: auto 1fr 1fr;
  gap: 6px 12px;
  align-items: center;
}

.key-grid .player-label {
  font-size: 0.75rem;
  font-weight: 700;
  white-space: nowrap;
}

.key-btn {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  color: var(--fg);
  font-size: 0.75rem;
  font-weight: 600;
  font-family: inherit;
  padding: 4px 10px;
  min-width: 40px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s;
}
.key-btn:hover { border-color: var(--accent); }
.key-btn.listening {
  border-color: var(--accent);
  background: rgba(125, 211, 252, 0.12);
  animation: pulse-key 0.8s ease infinite alternate;
}
@keyframes pulse-key {
  from { box-shadow: 0 0 0 0 rgba(125, 211, 252, 0.3); }
  to   { box-shadow: 0 0 8px 2px rgba(125, 211, 252, 0.3); }
}

.key-hint {
  font-size: 0.65rem;
  color: var(--muted);
  margin-top: 10px;
  text-align: center;
}

/* --- Stats modal --- */

.modal-stats { max-width: 440px; }

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat-card {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  padding: 10px 14px;
  text-align: center;
}

.stat-value {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 2px;
}

.stats-section-title {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 14px 0 6px;
}

.stats-player-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 8px;
}

.stats-player-card {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  padding: 8px 6px;
  text-align: center;
}

.stats-player-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin: 0 auto 4px;
}

.stats-player-name {
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--fg);
  margin-bottom: 2px;
}

.stats-player-wins {
  font-size: 1.1rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.stats-player-rate {
  font-size: 0.55rem;
  color: var(--muted);
  font-weight: 600;
}

/* DISABLED (hidden in 2/3 player modes) */
.disabled { display: none !important; }

/* --- Responsive: stack on small screens --- */
@media (max-width: 600px) {
  .top {
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 6px;
    padding: 0.3rem 0.5rem;
  }
  .top-side-left, .top-side-right {
    justify-content: center;
  }
  .top-center h1 {
    font-size: 1.2rem;
  }
  .btn {
    padding: 0.3rem 0.65rem;
    font-size: 0.7rem;
  }
  .game-container {
    --size: min(96vw, calc(100dvh - 100px));
  }
}
