:root {
  --bg: #0f1410;
  --panel: #172018;
  --snake: #7dce6a;
  --head: #c6f5b8;
  --food: #e85d4c;
  --text: #e7f0e4;
  --muted: #9aaf96;
  --accent: #7dce6a;
  --pad: #243326;
  --safe: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
}

body {
  display: grid;
  place-items: start center;
  background: radial-gradient(circle at top, #1c2a1d 0%, var(--bg) 55%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  touch-action: manipulation;
}

.wrap {
  width: min(520px, 100%);
  padding: 12px 12px calc(16px + var(--safe));
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 100dvh;
}

header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

h1 {
  margin: 0;
  font-size: clamp(20px, 6vw, 28px);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.scores {
  display: flex;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
}

.scores strong {
  color: var(--text);
  font-size: 17px;
  margin-left: 4px;
}

.board-wrap {
  position: relative;
  width: min(100%, calc(100dvh - 280px), 520px);
  aspect-ratio: 1;
  margin: 0 auto;
  border: 2px solid #314233;
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
  flex: 0 0 auto;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #121a13;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px;
  background: rgba(10, 14, 10, 0.84);
  overflow: auto;
}

.overlay.hidden {
  display: none;
}

#overlay-title {
  margin: 0;
  font-size: clamp(22px, 7vw, 32px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

#overlay-text {
  margin: 0;
  max-width: 320px;
  color: var(--muted);
  line-height: 1.4;
  font-size: 14px;
  text-align: center;
}

.setup {
  width: min(340px, 100%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 6px 0 4px;
}

.setup.hidden {
  display: none;
}

.group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}

.group > span {
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip, button {
  border: 0;
  cursor: pointer;
  font: inherit;
}

.chip {
  flex: 1 1 auto;
  border-radius: 999px;
  padding: 8px 10px;
  background: #243326;
  color: var(--text);
  font-size: 13px;
}

.chip.active {
  background: var(--accent);
  color: #10210e;
  font-weight: 700;
}

#start {
  border-radius: 999px;
  padding: 10px 22px;
  background: var(--accent);
  color: #10210e;
  font-weight: 700;
}

#start:hover, .chip:hover, .dir:hover, .pause:hover {
  filter: brightness(1.08);
}

.legend {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 14px;
  font-size: 12px;
  color: var(--muted);
}

.legend li {
  display: flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.pad {
  display: none;
  width: min(240px, 70vw);
  margin: 0 auto;
  user-select: none;
}

.pad.show {
  display: grid;
  justify-items: center;
  gap: 8px;
}

.pad-mid {
  display: grid;
  grid-template-columns: 64px 64px 64px;
  gap: 8px;
}

.dir, .pause {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  background: var(--pad);
  color: var(--text);
  font-size: 22px;
}

.pause {
  font-size: 16px;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  text-align: center;
}

@media (pointer: coarse), (max-width: 720px) {
  .pad {
    display: grid;
    justify-items: center;
    gap: 8px;
  }
}

@media (max-height: 700px) {
  .wrap {
    min-height: auto;
    padding-top: 8px;
  }

  .dir, .pause {
    width: 56px;
    height: 56px;
  }

  .pad-mid {
    grid-template-columns: 56px 56px 56px;
  }
}

.legend li.out {
  opacity: 0.55;
}

.legend li.out .name {
  text-decoration: line-through;
}
