/* ===== 主题变量 ===== */
:root {
  --bg: #0f1420;
  --bg-2: #161d2e;
  --panel: #1b2337;
  --line: #2a3450;
  --text: #e8ecf5;
  --muted: #8a95b0;
  --accent: #4ade80;      /* 蛇 / 主按钮 */
  --accent-dark: #22a559;
  --food: #ff5a6e;        /* 食物：与蛇对比明显 */
  --radius: 12px;
  --board-size: min(520px, 92vw, calc(100vh - 260px));
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(1200px 600px at 50% -10%, #1d2740 0%, var(--bg) 60%);
  color: var(--text);
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

body {
  display: flex;
  justify-content: center;
  padding: 20px 12px;
}

.app {
  width: max(var(--board-size), 300px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ===== 顶部 ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.title {
  margin: 0;
  font-size: 28px;
  letter-spacing: 2px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: var(--accent);
  box-shadow: 14px 0 0 -4px var(--accent-dark), 0 0 16px rgba(74, 222, 128, .5);
  margin-right: 10px;
}

.scores { display: flex; gap: 10px; align-items: stretch; }

.btn-lang {
  min-width: 56px;
  padding: 6px 10px;
  font-size: 13px;
  letter-spacing: 1px;
}

.score-box {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 14px;
  min-width: 82px;
  text-align: center;
}

.score-label { display: block; font-size: 12px; color: var(--muted); }
.score-value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ===== 控制栏 ===== */
.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.buttons, .settings { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.btn {
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 16px;
  cursor: pointer;
  transition: background .15s, transform .08s, box-shadow .15s, border-color .15s;
}
.btn:hover:not(:disabled) { background: #232d47; border-color: #3a4770; }
.btn:active:not(:disabled) { transform: translateY(1px) scale(.97); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #08240f;
}
.btn-primary:hover:not(:disabled) {
  background: #6ee7a0;
  border-color: #6ee7a0;
  box-shadow: 0 0 18px rgba(74, 222, 128, .45);
}

.field, .toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}

select {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px 8px;
  cursor: pointer;
}
select:disabled, .toggle input:disabled { cursor: not-allowed; opacity: .5; }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; cursor: pointer; }

/* ===== 游戏区域 ===== */
.board-wrap {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  min-width: 280px;
  min-height: 280px;
  align-self: center;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
  box-shadow: 0 20px 50px rgba(0, 0, 0, .45);
}

#board { display: block; width: 100%; height: 100%; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 14, 24, .72);
  backdrop-filter: blur(3px);
  transition: opacity .2s;
}
.overlay.hidden { opacity: 0; pointer-events: none; }

.panel {
  text-align: center;
  padding: 22px 26px;
  max-width: 88%;
}
.panel h2 { margin: 0 0 8px; font-size: 26px; }
.overlay-msg { margin: 0 0 14px; color: var(--muted); line-height: 1.6; }
.overlay-msg strong { color: var(--text); font-size: 20px; }

.help {
  list-style: none;
  padding: 0;
  margin: 0 0 18px;
  font-size: 14px;
  color: var(--muted);
  line-height: 2;
}

kbd {
  display: inline-block;
  min-width: 22px;
  padding: 0 6px;
  margin: 0 2px;
  font-family: inherit;
  font-size: 12px;
  line-height: 20px;
  color: var(--text);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
}

/* 多蛇计分板 */
.legend {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
}
.legend:empty { display: none; }
.chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  font-size: 13px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  transition: opacity .2s;
}
.chip.me { border-color: rgba(255, 255, 255, .35); }
.chip.dead { opacity: .4; }
.chip .dot { width: 10px; height: 10px; border-radius: 50%; }
.chip b { font-variant-numeric: tabular-nums; }

.status {
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}

/* 窄屏：控制栏居中排列 */
@media (max-width: 560px) {
  .title { font-size: 22px; }
  .controls { justify-content: center; }
  .score-box { min-width: 70px; padding: 4px 10px; }
}

/* ===== 触屏方向键 =====
   只在手机 / 平板上显示：game.js 检测到触屏后会给 <html> 加上 touch 类，电脑上不显示。 */
.dpad { display: none; }

html.touch {
  --dpad-key: clamp(48px, 14vw, 60px);
  /* 手机上让「标题栏 + 棋盘 + 计分 + 方向键」一屏放得下：打开页面不用滚动就能看到方向键 */
  --board-size: min(520px, 92vw, calc(100vh - 3 * var(--dpad-key) - 150px));
}
@supports (height: 100svh) {
  /* svh = 手机浏览器地址栏 / 工具栏展开时的可见高度；矮屏手机上方向键略小一点 */
  html.touch {
    --dpad-key: clamp(44px, min(14vw, 9svh), 60px);
    --board-size: min(520px, 92vw, calc(100svh - 3 * var(--dpad-key) - 150px));
  }
}

/* 手机上调整顺序：标题栏 → 棋盘 → 计分 → 方向键 → 按钮和设置（电脑上保持原样） */
html.touch body { padding-top: 10px; }
html.touch .app { gap: 10px; width: min(100%, 520px); }   /* 用满屏幕宽度，标题栏不换行 */
html.touch .board-wrap { min-width: 240px; min-height: 240px; }

/* 标题栏和计分条紧凑些：窄屏手机上也不换行（换行会把方向键挤出屏幕） */
html.touch .title { font-size: 20px; gap: 8px; }
html.touch .logo { margin-right: 6px; }
html.touch .scores { gap: 6px; }
html.touch .score-box { min-width: 60px; padding: 4px 8px; }
html.touch .btn-lang { min-width: 48px; padding: 6px 8px; }
html.touch .chip { padding: 3px 8px; font-size: 12px; gap: 5px; }
html.touch .topbar     { order: 1; }
html.touch .board-wrap { order: 2; }
html.touch .legend     { order: 3; }
html.touch .dpad       { order: 4; }
html.touch .controls   { order: 5; margin-top: 8px; }
html.touch .status     { order: 6; }

html.touch .dpad {
  display: grid;
  grid-template-columns: repeat(3, var(--dpad-key));
  grid-template-rows: repeat(3, var(--dpad-key));
  gap: 6px;
  justify-content: center;
  margin: 2px auto 0;
  touch-action: none;            /* 按方向键时不滚动、不缩放页面 */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.dpad-btn {
  display: grid;
  place-items: center;
  padding: 0;
  color: var(--text);
  background: linear-gradient(180deg, #222c45, var(--panel));
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 4px 0 #0a0e18, 0 10px 20px rgba(0, 0, 0, .35);
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform .06s, box-shadow .06s, background .1s, color .1s, border-color .1s;
}
.dpad-btn svg { width: 42%; height: 42%; pointer-events: none; }
.dpad-btn.up    { grid-area: 1 / 2; }
.dpad-btn.left  { grid-area: 2 / 1; }
.dpad-btn.right { grid-area: 2 / 3; }
.dpad-btn.down  { grid-area: 3 / 2; }
.dpad-btn.left svg  { transform: rotate(-90deg); }
.dpad-btn.right svg { transform: rotate(90deg); }
.dpad-btn.down svg  { transform: rotate(180deg); }
.dpad-btn.pressed {
  color: #08240f;
  background: var(--accent);
  border-color: var(--accent);
  transform: translateY(3px);
  box-shadow: 0 1px 0 #0a0e18, 0 0 18px rgba(74, 222, 128, .45);
}

.dpad-hub {
  grid-area: 2 / 2;
  place-self: center;
  width: 46%;
  height: 46%;
  border-radius: 50%;
  background: var(--bg-2);
  border: 1px solid var(--line);
}

/* 手机上棋盘较小：开始 / 暂停界面的文字排得紧凑些，保证按钮不被裁掉 */
html.touch .panel { padding: 14px 16px; max-width: 94%; }
html.touch .panel h2 { font-size: 22px; }
html.touch .help { font-size: 13px; line-height: 1.6; margin-bottom: 14px; }
html.touch .help li { margin: 5px 0; }

/* 手机横屏：方向键固定在右下角，棋盘按屏幕高度缩放并让出右侧位置 */
@media (orientation: landscape) and (max-height: 560px) {
  html.touch { --dpad-key: 56px; --board-size: min(520px, 92vw, calc(100vh - 32px)); }
  @supports (height: 100svh) {
    html.touch { --board-size: min(520px, 92vw, calc(100svh - 32px)); }
  }
  html.touch body { padding-right: calc(3 * var(--dpad-key) + 52px); }
  html.touch .dpad {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 5;
    margin: 0;
  }
}
