:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --grid: #172033;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22c55e;
  --food: #f43f5e;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

.app {
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 16px max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h1 { margin: 0; font-size: 1.6rem; letter-spacing: 0.02em; }

.scores { display: flex; gap: 8px; }
.scores > div {
  background: var(--panel);
  border-radius: 8px;
  padding: 6px 12px;
  min-width: 70px;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.label {
  display: block;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--muted);
}

.board-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

.board { position: relative; }

canvas {
  display: block;
  background: var(--panel);
  border-radius: 12px;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  padding: 24px;
  background: rgba(15, 23, 42, 0.85);
  border-radius: 12px;
  justify-content: safe center;
  overflow-y: auto;
}
.overlay.hidden, .overlay .hidden { display: none; }
.overlay-title { margin: 0; font-size: 2rem; font-weight: 800; }
.overlay-text { margin: 0 0 8px; color: var(--muted); max-width: 28ch; }

.btn {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 10px;
  padding: 12px 28px;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: #052e16; }

.name-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
#name {
  font: inherit;
  width: 14rem;
  max-width: 100%;
  padding: 10px 12px;
  border: 2px solid #334155;
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  text-align: center;
  -webkit-user-select: text;
  user-select: text;
}
#name:focus { outline: none; border-color: var(--accent); }
.player { margin: 0; color: var(--muted); font-size: 0.9rem; }
.player strong { color: var(--text); }
.link {
  font: inherit;
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}

.leaders { width: min(16rem, 100%); margin-top: 6px; font-size: 0.85rem; }
.leaders-title {
  margin: 0 0 4px;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--muted);
}
.leaders ol { margin: 0; padding: 0; list-style: none; counter-reset: rank; }
.leaders li {
  counter-increment: rank;
  display: flex;
  gap: 8px;
  padding: 2px 8px;
  border-radius: 6px;
}
.leaders li::before { content: counter(rank) "."; width: 1.6em; text-align: right; color: var(--muted); }
.leaders li.me { background: rgba(34, 197, 94, 0.15); }
.leaders .who { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaders .pts { font-weight: 700; font-variant-numeric: tabular-nums; }
.leaders-msg { margin: 4px 0 0; color: var(--muted); }
.leaders-msg:empty { display: none; }

.pad {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(3, 64px);
  grid-template-rows: repeat(3, 56px);
  gap: 6px;
  justify-content: center;
  grid-template-areas:
    ".    up    ."
    "left mid   right"
    ".    down  .";
}
.dir {
  font-size: 1.2rem;
  border: 0;
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  touch-action: manipulation;
  cursor: pointer;
}
.dir:active { background: #334155; }
.up { grid-area: up; }
.down { grid-area: down; }
.left { grid-area: left; }
.right { grid-area: right; }
.mid { grid-area: mid; font-size: 0.9rem; color: var(--muted); }

/* Keyboard users on desktop rarely need the pad; keep it but smaller. */
@media (hover: hover) and (pointer: fine) {
  .pad { grid-template-columns: repeat(3, 48px); grid-template-rows: repeat(3, 40px); }
}
