:root {
  color-scheme: light;
  --page-bg: #f3f3ef;
  --panel-bg: rgba(255, 255, 255, 0.82);
  --panel-border: rgba(17, 17, 17, 0.12);
  --text-main: #151515;
  --text-muted: #595959;
  --grid-line: rgba(255, 255, 255, 0.05);
  --board-bg: #111111;
  --food: #ef4444;
  --shadow: 0 24px 60px rgba(17, 17, 17, 0.12);
  --radius-xl: 24px;
  --radius-lg: 16px;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Avenir Next", "Helvetica Neue", "Noto Sans SC", sans-serif;
  color: var(--text-main);
  background: linear-gradient(180deg, #efeee7 0%, #e6e4dc 100%);
}

body, button {
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button {
  font: inherit;
}

/* ── 全局：居中卡片 ── */
.app-shell {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}

/* ── 卡片：左右布局 ── */
.game-panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
  padding: 14px;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 14px;
  max-width: 720px;
  width: 100%;
  max-height: calc(100vh - 16px);
}

/* ── 左侧：游戏画布 ── */
.board-shell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
}

#game-board {
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  max-height: calc(100vh - 44px);
  border-radius: calc(var(--radius-xl) - 4px);
  background:
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px),
    #111111;
  background-size: 20px 20px;
  outline: none;
}

/* ── 游戏结束遮罩 ── */
.board-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  border-radius: calc(var(--radius-xl) - 4px);
  background: rgba(14, 14, 14, 0.84);
  color: #f5f5ef;
  backdrop-filter: blur(12px);
}

.board-overlay.is-hidden {
  display: none !important;
}

.overlay-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.overlay-copy {
  margin: 6px 0 0;
  font-size: 0.88rem;
  line-height: 1.5;
  color: rgba(245, 245, 239, 0.82);
}

.overlay-copy.is-hidden {
  display: none;
}

.overlay-restart {
  margin-top: 14px;
  padding: 9px 24px;
  border: none;
  border-radius: 10px;
  background: var(--food);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 100ms ease, box-shadow 100ms ease, background 100ms ease;
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.35);
}

.overlay-restart:hover {
  background: #dc2626;
  box-shadow: 0 4px 14px rgba(239, 68, 68, 0.45);
  transform: translateY(-1px);
}

.overlay-restart:active {
  transform: translateY(1px);
  box-shadow: 0 1px 4px rgba(239, 68, 68, 0.3);
}

.overlay-restart.is-hidden {
  display: none;
}

/* ── 右侧：信息 + 控制 ── */
.sidebar {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  flex-shrink: 0;
  width: 170px;
  padding: 4px 0;
}

h1 {
  margin: 0;
  font-size: 1.6rem;
  line-height: 1;
  letter-spacing: -0.04em;
  white-space: nowrap;
  text-align: center;
}

.social-links {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.social-link {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: rgba(17, 17, 17, 0.04);
  border: 1px solid rgba(17, 17, 17, 0.08);
  color: var(--text-muted);
  text-decoration: none;
  transition: background 120ms ease, color 120ms ease, transform 120ms ease;
}

.social-link:hover {
  background: rgba(17, 17, 17, 0.10);
  color: var(--text-main);
  transform: translateY(-1px);
}

.social-link:active {
  transform: translateY(1px);
}

.stat-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
}

.stat-card {
  padding: 8px 14px;
  border-radius: 14px;
  background: rgba(17, 17, 17, 0.04);
  border: 1px solid rgba(17, 17, 17, 0.08);
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.stat-value {
  display: block;
  margin-top: 2px;
  font-size: 1.2rem;
  line-height: 1;
}

/* ── 方向键 ── */
.controls-panel {
  width: 100%;
}

.controls-grid {
  display: grid;
  grid-template-columns: repeat(3, 44px);
  justify-content: center;
  gap: 5px;
}

.control-button {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(180deg, #f7f7f2 0%, #e8e7e0 100%);
  color: var(--text-main);
  font-size: 1.2rem;
  font-weight: 600;
  border: 1px solid rgba(17, 17, 17, 0.10);
  border-bottom: 3px solid rgba(17, 17, 17, 0.15);
  box-shadow:
    0 3px 6px rgba(0, 0, 0, 0.10),
    0 1px 3px rgba(0, 0, 0, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  cursor: pointer;
  transition:
    transform 80ms ease,
    box-shadow 80ms ease,
    border-bottom-width 80ms ease,
    background 80ms ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  position: relative;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.control-button svg {
  width: 18px;
  height: 18px;
}

.control-button:hover {
  background: linear-gradient(180deg, #ffffff 0%, #eeede6 100%);
  box-shadow:
    0 3px 8px rgba(0, 0, 0, 0.12),
    0 1px 3px rgba(0, 0, 0, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.9);
}

.control-button:active,
.control-button.is-pressed {
  transform: translateY(2px);
  border-bottom-width: 1px;
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.10),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
  background: linear-gradient(180deg, #e8e7e0 0%, #deddd6 100%);
}

.control-button:focus-visible {
  outline: 3px solid rgba(239, 68, 68, 0.35);
  outline-offset: 2px;
}

/* ── 手机竖屏：上下布局 ── */
@media (max-width: 520px), (max-aspect-ratio: 4/5) {
  .game-panel {
    flex-direction: column;
    align-items: center;
    padding: 12px;
    gap: 10px;
    max-width: 400px;
  }

  /* sidebar 变成顶栏 + 底栏 */
  .sidebar {
    width: 100%;
    flex-direction: column;
    gap: 10px;
    order: -1;
  }

  /* 标题 + 分数放一行 */
  .sidebar h1 {
    display: none;
  }

  .social-links {
    display: none;
  }

  .stat-group {
    flex-direction: row;
    gap: 8px;
    width: 100%;
  }

  .stat-group::before {
    content: "x-snake";
    font-size: 1.4rem;
    font-weight: 800;
    letter-spacing: -0.04em;
    margin-right: auto;
    align-self: center;
  }

  .stat-card {
    min-width: 68px;
    padding: 5px 10px;
  }

  .stat-value {
    font-size: 1rem;
  }

  /* 控制键移到画布下方 */
  .controls-panel {
    position: fixed;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    width: auto;
    z-index: 10;
  }

  .controls-grid {
    grid-template-columns: repeat(3, 46px);
    gap: 5px;
  }

  .control-button {
    width: 46px;
    height: 46px;
    border-radius: 11px;
  }

  .control-button svg {
    width: 17px;
    height: 17px;
  }

  #game-board {
    max-height: calc(100vh - 240px);
  }
}
