:root {
  color-scheme: dark;
  --bg: #0e1620;
  --panel: #16222e;
  --panel-elevated: #1e2c3b;
  --border: #22303e;
  --text: #e8eef5;
  --muted: #8fa3b8;
  --accent: #3ec6ff;
  --accent-hover: #29b6f6;
  --brand: #e74c3c;
  --success: #2ecc71;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  max-width: 520px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 14px max(16px, env(safe-area-inset-bottom));
}

/* Topbar */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  min-height: 32px;
}

.home-link {
  color: var(--text);
  font-weight: 600;
  text-decoration: none;
  font-size: 0.95rem;
}
.home-link:hover { text-decoration: underline; }

.account {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
}
.account strong {
  max-width: 14ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.account a, .sign-in-link {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}
.account a:hover, .sign-in-link:hover {
  text-decoration: underline;
}

/* Mode selector */
.mode-select {
  display: flex;
  width: 100%;
  background: var(--panel);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
}

.mode-btn {
  flex: 1;
  background: transparent;
  border: 0;
  border-radius: 7px;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 8px 4px;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.active {
  background: var(--accent);
  color: #04121c;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

/* HUD */
.hud {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  width: 100%;
}

.hud-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
}
.hud-item span {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.hud-item strong {
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}

/* Stage & Canvas */
.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel);
  border-radius: 12px;
  border: 1px solid var(--border);
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
}

#board {
  display: block;
  touch-action: none;
  cursor: pointer;
}

/* Overlay */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(10, 16, 24, 0.82);
  backdrop-filter: blur(4px);
  z-index: 10;
}

.card {
  width: min(360px, 95%);
  padding: 24px 20px;
  background: var(--panel-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  text-align: center;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.6);
}

.card h2 {
  margin: 0 0 10px;
  font-size: 1.5rem;
}
.card p {
  margin: 6px 0;
  color: var(--text);
  line-height: 1.4;
}
.save-note {
  color: var(--muted);
  font-size: 0.85rem;
}
.overlay-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 18px;
}

/* Buttons */
button {
  font-family: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  padding: 8px 16px;
  border-radius: 8px;
  border: 0;
  background: var(--accent);
  color: #04121c;
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.1s ease;
}
button:active { transform: scale(0.97); }
button.quiet {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
button.quiet:hover {
  background: rgba(255, 255, 255, 0.05);
}
button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Palette */
.palette {
  display: flex;
  justify-content: space-between;
  width: 100%;
  gap: 8px;
  padding: 4px 0;
}

.palette-btn {
  flex: 1;
  aspect-ratio: 1 / 1;
  max-width: 64px;
  min-height: 48px;
  padding: 0;
  border-radius: 12px;
  border: 2px solid transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 1.25rem;
  color: #ffffff;
  position: relative;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.palette-btn[data-color="1"] { color: #111827; }
.palette-btn:hover {
  transform: translateY(-2px);
}
.palette-btn.current {
  border-color: #ffffff;
  box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px #ffffff;
  opacity: 0.75;
  cursor: default;
  transform: none;
}
.palette-btn .key-hint {
  position: absolute;
  bottom: 2px;
  font-size: 0.65rem;
  opacity: 0.7;
}

/* Footer controls */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  font-size: 0.85rem;
  margin-top: 4px;
}
.controls button {
  padding: 6px 12px;
  font-size: 0.8rem;
}
.version {
  color: var(--muted);
  font-size: 0.75rem;
  margin-left: auto;
}

/* Leaderboards */
.leaderboard-section {
  width: 100%;
  margin-top: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}
.leaderboard-section h2 {
  margin: 0 0 12px;
  font-size: 1.15rem;
}
.leaderboard-section h3 {
  margin: 0 0 8px;
  font-size: 0.95rem;
  color: var(--muted);
}
.leaderboard-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 440px) {
  .leaderboard-columns {
    grid-template-columns: 1fr;
  }
}

.rank-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.rank-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  padding: 4px 6px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.02);
}
.rank-pos {
  width: 2.2ch;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
.player-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.score-moves {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.score-time {
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}
.me-highlight {
  margin-top: 10px;
  padding: 8px;
  background: rgba(62, 198, 255, 0.12);
  border: 1px solid rgba(62, 198, 255, 0.3);
  border-radius: 6px;
  font-size: 0.85rem;
}
.muted {
  color: var(--muted);
  font-size: 0.85rem;
}
