/* TETRIS · 经典掌机风格 + 红白机手柄
 *
 * 设计约束（沿用项目约定）：
 *   - 无框架、无构建、无外部字体/CDN，改完即生效
 *   - 移动优先：整页**不滚动**，游戏区域吃掉剩下的全部空间（尺寸由 JS 算）
 *   - 像素感靠「等宽字体 + 方角 + 硬阴影」，不用图片
 *
 * 版面从上到下只有四层：顶栏（细）→ 升级进度条（6px）→ 场地 + 右侧窄条 → 手柄
 */

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

/*
 * 🔴 这一条不能删：
 *   下面 .scores / .menu / .save-row 都写了 display:flex，
 *   作者样式会盖掉浏览器给 [hidden] 的 display:none —— 面板就再也关不掉了
 *   （真踩过：高分榜一进页面就浮在那儿，点「关闭」没反应）。
 */
[hidden] { display: none !important; }

:root {
  --bg: #0b1020;
  --panel: #131a30;
  --panel-2: #0a0f1e;
  --line: #2c3a63;
  --ink: #e6ecff;
  --ink-dim: #8ea0cf;
  --accent: #67e8f9;
  --danger: #ef4444;
  --shadow: #05080f;

  /* 手柄配色：偏暗的「酒红」机身，才压得住深色底，不刺眼 */
  --pad-base: #6d2328;
  --pad-base-2: #3f1215;
  --pad-edge: #24090b;
  --pad-gold: #d9bd7c;
  --pad-cream: #f2e4c4;
  --key-face: #d8d4c9;
  --key-face-2: #a9a498;

  /* 手柄按键尺寸：随屏宽/屏高自适应，短屏（横屏）自动变小。
     这几个数是按「360px 宽的手机也放得下 十字键 + 两颗胶钮 + A/B」倒推出来的 */
  --key: min(56px, 15vw);          /* 十字键一格的宽度 */
  --keyh: calc(var(--key) * 0.9);  /* 高度略矮一点，别做成正方形 */
  --ab: min(62px, 16.5vw);
  --pill-w: 40px;
  --pill-h: 22px;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Courier New", monospace;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  /* 游戏页不滚动：一切尺寸都由视口反推，场地小了就小一点，不要出现滚动条 */
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background-image:
    linear-gradient(rgba(103, 232, 249, .04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(103, 232, 249, .04) 1px, transparent 1px);
  background-size: 22px 22px;
}

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

.tetris-app {
  height: 100vh;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  padding: calc(6px + env(safe-area-inset-top)) 10px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------- 顶栏（细） ---------- */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
}

.back-link {
  flex: 0 0 auto;
  font-size: 11px;
  color: var(--ink-dim);
  text-decoration: none;
  border-bottom: 1px dashed currentColor;
}

.stat {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  display: flex;
  align-items: baseline;
  gap: 3px;
  font-size: 11px;
  color: var(--ink-dim);
  white-space: nowrap;
  overflow: hidden;
}

.stat__value {
  font-size: 16px;
  font-weight: 700;
  color: var(--ink);
  line-height: 1;
}

.stat__value--level { color: var(--accent); }
.stat__sep { opacity: .5; }

/* 升档时闪一下（JS 加 .is-up，动画完自己摘掉） */
.stat__value.is-up { animation: level-up .9s ease-out; }

@keyframes level-up {
  0%   { color: #fff; text-shadow: 0 0 10px var(--accent); transform: scale(1.3); }
  100% { color: var(--accent); text-shadow: none; transform: scale(1); }
}

/* 右上角：声音 + 高分榜 */

.tools {
  flex: 0 0 auto;
  display: flex;
  gap: 4px;
}

.icon-btn {
  width: 29px;
  height: 29px;
  padding: 0;
  font-family: inherit;
  font-size: 14px;
  line-height: 1;
  color: var(--ink);
  background: var(--panel);
  border: 2px solid var(--line);
  cursor: pointer;
}

.icon-btn:active { transform: translate(1px, 1px); }

.icon-btn.is-off {
  color: #ff8a8a;
  background: rgba(239, 68, 68, .16);
  border-color: #7f2b2b;
  text-decoration: line-through;
}

/* ---------- 升级进度条（一条线，不占地方） ---------- */

.levelbar {
  flex: 0 0 auto;
  height: 6px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  overflow: hidden;
}

.levelbar__fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .12s linear;
}

/* ---------- 场地 + 右侧窄条 ---------- */

.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 8px;
}

.board-wrap {
  position: relative;
  /* 宽高由 JS 按可用空间算（见 game.mjs 的 layout()），这里给个兜底 */
  width: 200px;
  height: 400px;
  background: var(--panel-2);
  border: 3px solid var(--line);
  box-shadow: 3px 3px 0 var(--shadow);
}

.board-wrap--over { border-color: var(--danger); }

.overlay--over .overlay__title { color: #ff8a8a; text-shadow: 0 0 12px rgba(239, 68, 68, .5); }

/* 正在录名字（手机键盘弹起来）时才改成靠上，不然会被键盘顶掉。
   ⚠️ 不能用 :focus-within —— 按钮被点过之后也一直算「有焦点」，
   下次打开界面就莫名其妙地跑到上面去了（真踩过）。 */
.overlay--typing { justify-content: flex-start; }

.board {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* 右侧窄条：三格「下一块」+（隔开一段）一格「暂存」 */

.side {
  flex: 0 0 auto;
  width: 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.side__label {
  margin: 0;
  font-size: 9px;
  line-height: 1.2;
  color: var(--ink-dim);
  white-space: nowrap;
}

/* 暂存区和「下一块」之间拉开距离 + 一条细线，免得看成同一组 */
.side__label--hold {
  width: 100%;
  margin-top: 12px;
  padding-top: 8px;
  text-align: center;
  color: var(--pad-gold);
  border-top: 1px solid rgba(217, 189, 124, .28);
}

.side__box {
  display: block;
  width: 36px;
  height: 36px;
  background: var(--panel-2);
  border: 2px solid var(--line);
}

.side__box--hold { border-color: #6b5a2a; }

/* ---------- 主界面（经典封面 + 两个入口） ---------- */

.title {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 14px;
  text-align: center;
  background: #070c18;
}

.title__card {
  position: relative;
  width: 100%;
  max-width: 340px;
  aspect-ratio: 4 / 3;
}

.title__art {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

/* 主界面底图：经典封面的贴图（没这个文件时下面的 canvas 手绘版会露出来） */
.title__pic {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  image-rendering: pixelated;
}

/* 主界面上的烟花层 */
.title__fx {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.title__btn { width: 156px; }

/* ---------- 高分榜 / 菜单面板 ---------- */

.scores,
.menu {
  position: absolute;
  inset: 0;
  z-index: 8;      /* 🔴 必须高于 .title / .overlay(5)，否则开榜了却看不见 = 「打不开」 */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px;
  text-align: center;
  background: rgba(6, 10, 22, 0.94);
}

.scores__title,
.menu__title {
  margin: 0;
  font-size: 15px;
  letter-spacing: 1px;
}

/* 榜单下面那行小字：说明这份是「共享榜（服务器）」还是「本机榜（连不上）」 */
.scores__note {
  width: 100%;
  margin: 0;
  font-size: 10px;
  line-height: 1.3;
  color: var(--ink-dim);
  text-align: center;
}

.scores__note:empty { display: none; }

.scores__list {
  width: 100%;
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.score-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 5px 7px;
  font-size: 12px;
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--line);
}

.score-row__rank {
  flex: 0 0 auto;
  width: 14px;
  color: var(--pad-gold);
  font-weight: 700;
}

.score-row__name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;   /* 名字不限长度，太长就省略号 */
}

.score-row__value {
  flex: 0 0 auto;
  font-weight: 700;
  color: var(--accent);
}

.score-row__meta {
  flex: 0 0 auto;
  font-size: 9px;
  color: var(--ink-dim);
}

.score-row--empty {
  justify-content: center;
  color: var(--ink-dim);
}

/* 菜单里的按钮排一列，宽度一致 */
.menu .btn { width: 132px; }

/* ---------- 覆盖层与提示 ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;       /* 平时居中 */
  gap: 10px;
  padding: 18px 16px;
  text-align: center;
  background: rgba(6, 10, 22, 0.93);
}

.overlay__title {
  margin: 0;
  font-size: 26px;
  letter-spacing: 3px;
  color: var(--accent);
  text-shadow: 0 0 12px rgba(103, 232, 249, .45);
}

.overlay__stats {
  margin: 0;
  font-size: 13px;
  letter-spacing: 1px;
  color: var(--ink);
}

.overlay__tip {
  margin: 0;
  font-size: 12px;
  line-height: 1.7;
  color: var(--ink-dim);
}

/* 上榜录名字 */

.save-row {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  justify-content: center;
}

.save-row__input {
  width: 100px;
  min-height: 34px;
  padding: 4px 8px;
  font-family: inherit;
  font-size: 13px;
  color: var(--ink);
  background: var(--panel-2);
  border: 2px solid var(--line);
  outline: none;
}

.save-row__input:focus { border-color: var(--accent); }

.toast {
  position: absolute;
  left: 50%;
  top: 8px;
  z-index: 12;     /* 提示永远在最上面 */
  transform: translateX(-50%);
  margin: 0;
  padding: 4px 8px;
  font-size: 11px;
  white-space: nowrap;
  color: #06210f;
  background: var(--accent);
  border: 1px solid #a5f3fc;
  opacity: 0;
  transition: opacity .2s ease-out;
  pointer-events: none;
}

.toast--show { opacity: 1; }

/* ---------- 按钮 ---------- */

.btn {
  padding: 10px 16px;
  min-height: 42px;
  font-family: inherit;
  font-size: 13px;
  color: var(--ink);
  background: var(--panel);
  border: 2px solid var(--line);
  box-shadow: 2px 2px 0 var(--shadow);
  cursor: pointer;
}

.btn--small {
  padding: 6px 10px;
  min-height: 34px;
  font-size: 12px;
}

.btn:active { transform: translate(1px, 1px); box-shadow: 1px 1px 0 var(--shadow); }
.btn:disabled { opacity: .45; }

.btn--primary {
  color: #06210f;
  background: var(--accent);
  border-color: #a5f3fc;
  font-weight: 700;
}

.btn--ghost {
  color: var(--ink-dim);
  background: transparent;
}

/* ---------- 手柄（红白机造型，往「实物」上做） ---------- */

.pad {
  position: relative;
  flex: 0 0 auto;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 10px 18px;
  border-radius: 14px;
  /* 机身：暗酒红塑料 + 上面的高光/下面的暗部，做出曲面感 */
  background:
    linear-gradient(180deg, rgba(255, 255, 255, .12), rgba(0, 0, 0, .26)),
    linear-gradient(180deg, var(--pad-base), var(--pad-base-2));
  border: 1px solid var(--pad-edge);
  box-shadow:
    inset 0 0 0 1px rgba(0, 0, 0, .30),          /* 内圈暗线 */
    inset 0 0 0 6px rgba(217, 189, 124, .10),    /* 红白机那块金面板的意思，压得很淡 */
    inset 0 1px 0 rgba(255, 255, 255, .22),      /* 顶部高光 */
    inset 0 -3px 6px rgba(0, 0, 0, .35),         /* 底部暗部 */
    0 5px 12px rgba(0, 0, 0, .45);               /* 落在页面上的影子 */
}

/* 十字键：用一整块内联 SVG 画「十」字（两根长条同一填充叠在一起，交接处没有缝）。
   早先用两根各带描边的 div 拼，交接处会露出彼此的边框 —— 看着就是「穿模」 */

.dpad {
  position: relative;
  flex: 0 0 auto;
  width: calc(var(--key) * 3);
  height: calc(var(--keyh) * 3);
  /* 按下时的位移加在这一层，见下面 .dpad.is-* —— 整块一起动，箭头和中心圆点才不会掉队 */
  transition: transform .06s ease-out;
}

.dpad__cross {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  /* drop-shadow 跟着「两块的并集」走，描边不会跑到十字中间去 */
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .45));
  transition: filter .06s ease-out;
}

/* 按下时整块十字往那个方向「压」一点、并亮一档 —— 真十字键是按下去会翘的。
   ⚠️ 位移挂在 .dpad 这一层，不是挂在 .dpad__cross 上：
      四个小三角（.dpad__btn）和中间的圆点（.dpad__hub）都是它的兄弟节点，
      只动 SVG 的话，箭头和圆点会留在原地不动 —— 看着就是「十字跑了、字没跟上」。
   ⚠️ 不能给按钮本身加背景：那会在十字上盖出一块方角，看着就像穿模。 */
.dpad.is-press .dpad__cross { filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .45)) brightness(1.3); }
.dpad.is-up { transform: translateY(-2%); }
.dpad.is-down { transform: translateY(2%); }
.dpad.is-left { transform: translateX(-2%); }
.dpad.is-right { transform: translateX(2%); }

.dpad__btn {
  position: absolute;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--key);
  height: var(--keyh);
  padding: 0;
  font-family: inherit;
  font-size: calc(var(--keyh) * 0.3);
  line-height: 1;
  color: #9a9aa4;
  text-shadow: 0 1px 0 rgba(0, 0, 0, .6);
  background: transparent;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  touch-action: none;          /* 按住连发时别触发页面滚动 */
  user-select: none;
  -webkit-user-select: none;
}

.dpad__btn--up { left: var(--key); top: 0; }
.dpad__btn--left { left: 0; top: var(--keyh); }
.dpad__btn--right { left: calc(var(--key) * 2); top: var(--keyh); }
.dpad__btn--down { left: var(--key); top: calc(var(--keyh) * 2); }

.dpad__btn:active { color: #f0f0f4; }

.dpad__hub {
  position: absolute;
  z-index: 2;
  left: var(--key);
  top: var(--keyh);
  width: var(--key);
  height: var(--keyh);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.dpad__hub::before {
  content: '';
  width: 34%;
  height: 34%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #3c3c42, #0c0c0e 75%);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, .18);
}

/* 中间两颗小胶钮：横着并排，红白机上印的是 SELECT / START */

.pills {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 5px;
}

.pill {
  width: var(--pill-w);
  height: var(--pill-h);
  padding: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 7px;
  letter-spacing: .2px;
  line-height: 1;
  color: #cfcfd6;
  background: linear-gradient(180deg, #3c3c43, #1b1b1f);
  border: 1px solid #0b0b0d;
  border-radius: 11px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .18),
    0 1px 0 #170607;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.pill:active {
  transform: translateY(1px);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, .6);
  background: linear-gradient(180deg, #2b2b31, #141417);
}

.pill:disabled {
  opacity: .55;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, .6);
  background: linear-gradient(180deg, #2b2b31, #141417);
}

/* A / B：两颗暗红圆钮，带一点球面感 */

.abtn {
  flex: 0 0 auto;
  align-self: center;      /* 和十字键的中间对齐，不再吊在顶上 */
  display: flex;
  align-items: center;
  gap: 9px;
}

.abtn__btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  width: var(--ab);
  height: var(--ab);
  padding: 0;
  font-family: inherit;
  color: var(--pad-cream);
  background: radial-gradient(circle at 34% 26%, #e0605f, #94202a 72%);
  border: 1px solid #4d0d13;
  border-radius: 50%;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, .28),
    inset 0 -3px 5px rgba(0, 0, 0, .35),
    0 3px 0 #26080b;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.abtn__btn--a { transform: translateY(-7px); }
.abtn__btn--b { transform: translateY(7px); }

.abtn__btn:active {
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, .5);
  background: radial-gradient(circle at 34% 26%, #b6555a, #6d151c 72%);
}

/* 两颗都是「按下去 = 往下沉」，别再一颗往上跑 */
.abtn__btn--a:active { transform: translateY(-2px) scale(.96); }
.abtn__btn--b:active { transform: translateY(12px) scale(.96); }

.abtn__letter {
  font-size: calc(var(--ab) * 0.34);
  font-weight: 700;
  line-height: 1;
}

.abtn__icon {
  font-size: calc(var(--ab) * 0.19);
  line-height: 1;
  opacity: .75;
}

/* ---------- 状态修饰 ---------- */

.is-hidden { display: none; }

/* ---------- 短屏 / 横屏：把按键缩小，别把场地挤没了 ---------- */

@media (max-height: 620px) {
  :root {
    --key: min(46px, 12vw);
    --ab: min(50px, 13vw);
    --pill-w: 34px;
    --pill-h: 19px;
  }
  .pad { padding: 8px 8px 10px; }
  .pill { font-size: 6px; }
}

/* 很窄的手机：再收一档，保证 十字键 + 两个胶钮 + A/B 能并排放下 */
@media (max-width: 350px) {
  :root {
    --key: min(50px, 13.5vw);
    --ab: min(54px, 15vw);
    --pill-w: 36px;
  }
}

@media (min-width: 620px) {
  :root {
    --key: 64px;
    --ab: 70px;
    --pill-w: 46px;
    --pill-h: 24px;
  }
  .tetris-app { max-width: 520px; }
  .pill { font-size: 8px; }
}
