:root {
  --bg: #0f172a;
  --card: #111827;
  --line: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --soft: #cbd5e1;
  --green: #22c55e;
  --violet: #a855f7;
  --pause: #ef4444;
  --gold: #e6c15a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
html { background: var(--bg); }
body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font: 16px/1.4 "Segoe UI", "PingFang SC", "Noto Sans SC", "Hiragino Sans", "Noto Sans JP", "Malgun Gothic", "Noto Sans KR", "Segoe UI Historic", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, select { font: inherit; color: inherit; }
button {
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:focus-visible, select:focus-visible, a:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}
a { color: #38bdf8; }

.menu {
  min-height: 100dvh;
  overflow: auto;
}
.menu-inner {
  width: min(520px, 100%);
  margin: 0 auto;
  padding: 20px 20px calc(28px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
}
.brand svg { width: 88px; height: 88px; }
.brand h1 { margin: 0; font-size: 28px; }
.subtitle { margin: 0; color: var(--muted); text-align: center; }

.play {
  min-height: 52px;
  font-size: 18px;
  font-weight: 700;
}
.play.single { background: var(--green); color: #fff; }
.play.ai { background: var(--violet); color: #fff; }
.record { margin: -6px 0 0; color: var(--muted); font-size: 14px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.card h2 { margin: 0; font-size: 16px; }
.hint { margin: 0; color: var(--muted); font-size: 13px; }
.swatches {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding-bottom: 2px;
}
.swatch {
  width: 46px;
  height: 46px;
  flex: 0 0 auto;
  border-radius: 50%;
  border: 4px solid transparent;
  padding: 0;
}
.swatch[aria-pressed="true"] { border-color: #fff; }
.now { margin: 0; color: #e2e8f0; }
.lang select {
  width: 100%;
  min-height: 48px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 0;
  background: #1e293b;
  color: #fff;
}
.links { display: flex; flex-direction: column; align-items: center; gap: 10px; padding-top: 4px; }
.links a { font-size: 15px; }

#game {
  position: fixed;
  inset: 0;
  z-index: 2;
  background: var(--bg);
  user-select: none;
  padding:
    max(8px, env(safe-area-inset-top))
    max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(8px, env(safe-area-inset-left));
  display: grid;
  grid-template-areas:
    "head"
    "label"
    "board"
    "controls";
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 8px;
  overflow: hidden;
  overscroll-behavior: none;
}
.game-head {
  grid-area: head;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
}
.icon-btn, .pause-btn {
  background: #334155;
  color: #fff;
  min-width: 48px;
  height: 40px;
}
.pause-btn { background: var(--pause); padding: 0 12px; }
.icon-btn.off { background: #1f2937; }
.titles { min-width: 0; }
.mode {
  font-size: 17px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.score {
  color: var(--soft);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.snake-label {
  grid-area: label;
  margin: 0;
  min-height: 18px;
  text-align: center;
  font-size: 14px;
  font-weight: 700;
}
.board-wrap {
  grid-area: board;
  min-height: 0;
  height: 100%;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  overflow: hidden;
  touch-action: none;
}
canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}
.controls {
  grid-area: controls;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  min-height: 0;
}
.status {
  margin: 0;
  min-height: 18px;
  color: var(--pause);
  text-align: center;
  font-size: 13px;
}
.control-row {
  width: 100%;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  justify-items: center;
}
.switch-btn {
  width: 52px;
  height: 52px;
  font-size: 22px;
  background: #334155;
}
.pad { direction: ltr; }
.arrows {
  display: grid;
  grid-template-columns: 64px 64px 64px;
  gap: 6px;
  direction: ltr;
}
.arrows button, .spacer { width: 64px; height: 48px; }
.arrows button { background: #1f7a4d; color: #fff; font-size: 20px; }
.spacer { grid-column: 1; }
.up { grid-column: 2; grid-row: 1; }
.left { grid-column: 1; grid-row: 2; }
.down { grid-column: 2; grid-row: 2; }
.right { grid-column: 3; grid-row: 2; }
.joystick {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: #1f2937;
  border: 2px solid #475569;
  position: relative;
  touch-action: none;
  direction: ltr;
}
.knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 28px;
  height: 28px;
  margin: -14px 0 0 -14px;
  border-radius: 50%;
  background: #fff;
  pointer-events: none;
}

@media (orientation: landscape) {
  #game {
    grid-template-areas:
      "head head"
      "label label"
      "board controls";
    grid-template-columns: minmax(0, 1fr) 240px;
    grid-template-rows: auto auto minmax(0, 1fr);
  }
  .control-row { grid-template-columns: 1fr; }
  .controls { justify-content: center; overflow: auto; }
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 5;
  background: rgba(15, 23, 42, .72);
  display: grid;
  place-items: center;
  padding: 24px;
}
.dialog {
  width: min(360px, 100%);
  background: var(--card);
  border: 1px solid var(--gold);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.dialog h2 { margin: 0; font-size: 22px; }
.dialog p { margin: 0; color: var(--soft); }
.dialog button {
  min-height: 48px;
  background: var(--green);
  color: #fff;
  font-weight: 700;
}
