/* 贪吃蛇 · 诺基亚复刻（机身 = 用户给的贴图，屏幕内容全部手绘）
 *
 * 两条设计约束（改之前先读）：
 *   ① **机身不再用 CSS 画**：`img/phone.jpg` 就是机身（含屏幕凹槽和全部按键），
 *      这里只负责把 canvas **精确摆到屏幕上**、把**透明热区**摆到按键上。
 *      所有百分比都是**量出来的**（原图 956×1859，屏幕在 (170,251) 729×597），
 *      不是眼估的 —— 换贴图要重新量，方法见 `img/CREDITS.md`。
 *   ② **屏幕上没有一个 HTML 字**：那 88×72 个像素全由 canvas 手绘
 *      （`js/lcd-font.mjs` 的 5×7 点阵），放大多少倍都不糊。
 *
 * ⚠️ 热区是 `<button>`，但**故意不带任何可见样式**：看见的键是贴图里的键，
 *    热区只是盖在上面的透明命中框（按下去才浮一层白光，让人知道按到了）。
 */

*, *::before, *::after { box-sizing: border-box; }

/* [hidden] 必须压过下面那些 display:flex（否则「藏起来」的元素还会显示） */
[hidden] { display: none !important; }

:root {
  --bg: #14171c;
  --bg-soft: #1e232a;
  --ink: #e8ecf1;
  --ink-dim: #98a3b0;
  --line: #333a44;
  --blue: #3f7fd0;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100dvh;
  background: radial-gradient(120% 80% at 50% 0%, #232932 0%, var(--bg) 60%);
  color: var(--ink);
  font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- 页面骨架 ---------- */

.snake-app {
  min-height: 100dvh;
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

/* 顶栏沿用以前的样式：左边一个带下划线的返回链接，右边一排方图标按钮 */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  width: 100%;
  max-width: 480px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 0 6px;
  background: linear-gradient(180deg, var(--bg) 72%, rgba(20, 23, 28, 0));
}

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

.back-link {
  color: var(--ink-dim);
  text-decoration: none;
  font-size: 13px;
  border-bottom: 1px dashed currentColor;
  padding: 8px 2px;
}

.icon-btn {
  min-width: 40px;
  min-height: 40px;
  font-size: 17px;
  color: var(--ink);
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}

.icon-btn:active { background: #2a313a; }

.snake-app__note {
  margin: 0;
  max-width: 460px;
  text-align: center;
  font-size: 11px;
  line-height: 1.7;
  color: var(--ink-dim);
}

.snake-app__note b { color: var(--ink); font-weight: 600; }

/* ---------- 机身（贴图 + 屏幕 + 热区）----------
 * 贴图 956×1859（高宽比 1.9446）。宽度取 min(430px, 96vw)：
 * 手机竖屏一屏放不下整台机器（贴图本身就很高），但**屏幕 + 导航键那一截一定在首屏里**，
 * 下面的数字键露不全也没关系（方向键在导航键和键盘 2/4/6/8 上都有）。
 */

.phone {
  /* 宽度 = 贴图宽 × 视口剩余高度：**整台机器一屏正好放下**（用户口径：手机别太大），
     屏幕尺寸就由贴图自己的比例决定（屏幕占贴图宽度的 76.255%） */
  --w: min(430px, 96vw, calc((100dvh - 78px) / 1.9446));
  position: relative;
  width: var(--w);
  flex: 0 0 auto;
}

.phone__skin {
  display: block;
  width: 100%;
  height: auto;
  user-select: none;
  -webkit-user-drag: none;
}

/* ---------- 屏幕（贴图里那块黄色的位置，量出来的）---------- */

.lcd {
  position: absolute;
  left: 11.506%;
  top: 8.23%;
  width: 76.255%;
  height: 32.114%;
}

.lcd__canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  touch-action: none;      /* 在屏幕上滑动是操作，不是滚页面 */
}

/* 每个 LCD 像素之间的那道缝（--px 由 JS 按实际尺寸写进来）+ 玻璃反光 */
.lcd__grid,
.lcd__glare {
  position: absolute;
  inset: 0;
  border-radius: 2px;
  pointer-events: none;
}

.lcd__grid {
  background-image:
    repeating-linear-gradient(to right, rgba(24, 22, 8, 0.14) 0 1px, transparent 1px var(--px, 3px)),
    repeating-linear-gradient(to bottom, rgba(24, 22, 8, 0.14) 0 1px, transparent 1px var(--px, 3px));
  mix-blend-mode: multiply;
}

.lcd__glare {
  background: linear-gradient(152deg, rgba(255, 255, 255, 0.34) 0%, rgba(255, 255, 255, 0.05) 32%, rgba(255, 255, 255, 0) 52%);
}

.power-hint {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 7px 12px;
  font-family: inherit;
  font-size: 12px;
  color: #2a2c20;
  white-space: nowrap;
  background: rgba(248, 248, 240, 0.94);
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 999px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.45);
  cursor: pointer;
  animation: power-blink 1.6s ease-in-out infinite;
}

@keyframes power-blink {
  0%, 100% { opacity: 0.94; }
  50% { opacity: 0.6; }
}

/* ---------- 透明热区（位置 = 贴图里那 17 个键）---------- */

.hot {
  position: absolute;
  padding: 0;
  background: transparent;
  border: 0;
  border-radius: 14%;
  cursor: pointer;
  transition: background 60ms linear;
}

/* 按下去浮一层白光 —— 贴图上的键不会动，得让人知道按到了 */
.hot:active,
.hot.is-down {
  background: rgba(255, 255, 255, 0.28);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.55);
}

.hot:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: -2px;
}

.hot--soft-left { left: 3.661%; top: 51.21%; width: 26.151%; height: 6.993%; }
.hot--soft-right { left: 68.515%; top: 51.21%; width: 27.197%; height: 6.993%; }

.hot--green { left: 5.23%; top: 63.583%; width: 23.536%; height: 7.531%; }
.hot--red { left: 75.837%; top: 63.583%; width: 20.397%; height: 7.531%; }

/* 导航键：一整块命中区，里面切成 3×3（正中间那颗是确定） */
.nav {
  position: absolute;
  left: 29.812%;
  top: 52.824%;
  width: 38.18%;
  height: 15.6%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
}

.nav .hot { position: static; border-radius: 22%; }
.hot--nav-up { grid-area: 1 / 2; }
.hot--nav-left { grid-area: 2 / 1; }
.hot--nav-ok { grid-area: 2 / 2; }
.hot--nav-right { grid-area: 2 / 3; }
.hot--nav-down { grid-area: 3 / 2; }

/* 数字键：三列（列宽按贴图里实际的分缝量 —— 左列偏窄是拍照透视，照抄才对齐） */
.hot--k1, .hot--k4, .hot--k7 { left: 5.23%; width: 24.268%; }
.hot--k2, .hot--k5, .hot--k8 { left: 30.858%; width: 35.251%; }
.hot--k3, .hot--k6, .hot--k9 { left: 67.469%; width: 28.766%; }

.hot--k1, .hot--k2, .hot--k3 { top: 71.974%; height: 8.607%; }
.hot--k4, .hot--k5, .hot--k6 { top: 81.549%; height: 8.607%; }
.hot--k7, .hot--k8, .hot--k9 { top: 91.178%; height: 8.607%; }

/* ---------- 大屏 / 横屏 ---------- */

@media (min-width: 620px) {
  .phone { --w: min(430px, calc((100dvh - 90px) / 1.9446)); }
}

@media (orientation: landscape) and (max-height: 560px) {
  .phone { --w: min(96vw, calc((100dvh - 60px) / 1.9446)); }
  .snake-app__note { display: none; }
}
