/* ===========================================================
 * 反向贪吃蛇 3D · UI 样式
 * =========================================================== */

:root {
  --bg: #070b16;
  --panel: rgba(18, 26, 48, 0.72);
  --panel-solid: #121a30;
  --border: rgba(120, 150, 220, 0.18);
  --brand: #3b66f6;
  --brand-2: #60a5fa;
  --text: #eef3ff;
  --text-2: #9db0da;
  --text-3: #6b7ea8;
  --red: #ff2d55;
  --green: #0f9d58;
  --gold: #fbbf24;
  --r: 14px;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  user-select: none;
  /* 移动端：禁用页面滚动/下拉刷新/双击缩放，避免滑动操作与浏览器手势冲突 */
  overscroll-behavior: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}
/* 用 dvh 兜住移动端地址栏收起导致的高度跳变 */
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}
body.crash { animation: shake .35s ease; }
@keyframes shake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-7px, 4px); }
  40% { transform: translate(6px, -5px); }
  60% { transform: translate(-4px, 3px); }
  80% { transform: translate(5px, -2px); }
}

#stage { position: fixed; inset: 0; }
#stage canvas {
  display: block;
  width: 100% !important;   /* 始终跟随容器，避免 JS 尺寸异常时画布变形 */
  height: 100% !important;
  touch-action: none;
}

/* ============ HUD ============ */
.hud {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 14px 18px;
  background: linear-gradient(180deg, rgba(7, 11, 22, 0.92) 0%, rgba(7, 11, 22, 0) 100%);
  pointer-events: none;
}

.brand {
  display: flex; align-items: center; gap: 9px;
  font-weight: 800; font-size: 15px; letter-spacing: .3px;
}
.brand .logo {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, #4f74ff, #2547eb);
  display: grid; place-items: center; font-size: 16px;
  box-shadow: 0 4px 12px rgba(37, 71, 235, .4);
}
.brand small { display: block; font-size: 9.5px; color: var(--text-3); letter-spacing: 1.4px; font-weight: 700; }

.spacer { flex: 1; }

.pill {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-width: 62px; height: 42px; padding: 0 13px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 11px; backdrop-filter: blur(8px);
}
.pill b { font-size: 17px; line-height: 1; font-weight: 800; font-family: "SF Mono", monospace; }
.pill span { font-size: 9.5px; color: var(--text-3); letter-spacing: .8px; margin-top: 2px; }
.pill.lv b { color: var(--brand-2); }
.pill.bean b { color: var(--red); }
.pill.score b { color: var(--gold); }

.combo {
  font-size: 15px; font-weight: 800; color: var(--red);
  min-width: 34px; text-shadow: 0 0 12px rgba(255, 45, 85, .6);
}

.icon-btn {
  width: 42px; height: 42px; border-radius: 11px;
  background: var(--panel); border: 1px solid var(--border);
  color: var(--text-2); font-size: 17px; cursor: pointer;
  display: grid; place-items: center; pointer-events: auto;
  backdrop-filter: blur(8px); transition: .16s;
}
.icon-btn:hover { background: rgba(59, 102, 246, .2); color: var(--text); border-color: rgba(120, 150, 220, .4); }

.speed-btn { width: 52px; font-size: 13px; font-weight: 800; color: var(--gold); }
@media (max-width: 400px) {
  .hud { gap: 5px; padding-left: 8px; padding-right: 8px; }
  .speed-btn { width: 38px; font-size: 11px; }
}

/* ============ 进度条 ============ */
.progress-wrap {
  position: fixed; top: 72px; left: 50%; transform: translateX(-50%); z-index: 19;
  display: flex; align-items: center; gap: 10px;
  background: var(--panel); border: 1px solid var(--border);
  padding: 8px 14px; border-radius: 99px; backdrop-filter: blur(10px);
  width: min(340px, 80vw);
}
.progress-track {
  flex: 1; height: 7px; border-radius: 99px;
  background: rgba(255, 255, 255, .1); overflow: hidden;
}
.progress-fill {
  height: 100%; border-radius: 99px; width: 0%;
  background: linear-gradient(90deg, #ff5c7a, #ff2d55);
  box-shadow: 0 0 12px rgba(255, 45, 85, .7);
  transition: width .3s ease;
}
.progress-txt { font-size: 11px; font-weight: 800; color: var(--red); font-family: monospace; min-width: 34px; text-align: right; }

/* ============ 底部提示 ============ */
.hint {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 18;
  font-size: 11.5px; color: var(--text-3);
  background: var(--panel); border: 1px solid var(--border);
  padding: 7px 16px; border-radius: 99px; backdrop-filter: blur(8px);
  white-space: nowrap;
  max-width: 94vw; overflow: hidden; text-overflow: ellipsis;
}
/*触屏提示默认隐藏，仅触屏设备显示 */
.hint-touch { display: none; }
body.touch .hint-touch {
  display: block;
  bottom: 172px;      /* 让到虚拟方向键上方 */
  white-space: normal; text-align: center; line-height: 1.7;
}
.hint b { color: var(--text-2); }
.hint kbd {
  display: inline-block; padding: 1px 5px; margin: 0 2px;
  border: 1px solid var(--border); border-radius: 4px;
  font-family: monospace; font-size: 10px; background: rgba(255, 255, 255, .06);
}

/* ============ 覆盖层 ============ */
.overlay {
  position: fixed; inset: 0; z-index: 40;
  display: none; place-items: center;
  background: rgba(5, 8, 18, .82);
  backdrop-filter: blur(10px);
  padding: 20px;
}
.overlay.show { display: grid; animation: fadeIn .28s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.ov-box {
  width: min(430px, 100%);
  background: linear-gradient(160deg, #16224a 0%, #0e1730 100%);
  border: 1px solid rgba(120, 150, 220, .24);
  border-radius: 22px; padding: 34px 30px;
  text-align: center;
  max-height: calc(100dvh - 40px);
  overflow-y: auto;
  touch-action: pan-y;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .6);
  animation: pop .32s cubic-bezier(.2, 1.2, .3, 1) both;
}
@keyframes pop { from { opacity: 0; transform: scale(.9) translateY(14px); } to { opacity: 1; transform: none; } }

.ov-icon { font-size: 48px; margin-bottom: 12px; line-height: 1; }
.ov-icon.win { animation: bounce .6s ease; }
.ov-icon.dead { animation: shakeIcon .5s ease; }
@keyframes bounce { 0%, 100% { transform: none; } 40% { transform: scale(1.22) rotate(8deg); } }
@keyframes shakeIcon { 0%, 100% { transform: none; } 25% { transform: rotate(-10deg) scale(1.1); } 75% { transform: rotate(10deg) scale(1.1); } }

.ov-box h2 { font-size: 26px; font-weight: 800; letter-spacing: -.5px; margin-bottom: 9px; }
.ov-sub { font-size: 13.5px; color: var(--text-2); line-height: 1.7; margin-bottom: 22px; }
.ov-sub b { color: var(--gold); font-family: monospace; font-size: 15px; }

.ov-stats {
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
  margin-bottom: 22px;
}
.ov-stats div {
  background: rgba(255, 255, 255, .05);
  border: 1px solid var(--border);
  border-radius: 12px; padding: 13px 10px;
}
.ov-stats b { display: block; font-size: 25px; font-weight: 800; font-family: monospace; color: var(--brand-2); }
.ov-stats span { font-size: 11px; color: var(--text-3); }

/* 规则说明 */
.ov-rules {
  display: grid; grid-template-columns: 1fr 1fr; gap: 9px;
  margin-bottom: 18px;
}
.rule {
  display: flex; align-items: center; gap: 10px;
  background: rgba(255, 255, 255, .05);
  border: 1px solid var(--border);
  border-radius: 11px; padding: 10px 12px;
  text-align: left;
}
.rk {
  width: 28px; height: 28px; flex: none;
  display: grid; place-items: center;
  background: rgba(59, 102, 246, .2); border: 1px solid rgba(120, 150, 220, .3);
  border-radius: 7px; font-size: 14px; color: var(--brand-2); font-weight: 700;
}
.rv { font-size: 11.5px; color: var(--text-2); line-height: 1.4; }
.rv em { font-style: normal; color: var(--red); font-weight: 800; font-size: 13px; }

.ov-note {
  font-size: 12px; color: #ffd166;
  background: rgba(255, 209, 102, .1);
  border: 1px solid rgba(255, 209, 102, .26);
  border-radius: 10px; padding: 10px 12px; margin-bottom: 20px;
}
.ov-note b { color: var(--red); }

.ov-btn {
  width: 100%; height: 47px; border-radius: 13px;
  font-size: 15px; font-weight: 700; cursor: pointer;
  background: rgba(255, 255, 255, .07);
  border: 1px solid var(--border);
  color: var(--text); margin-bottom: 10px;
  transition: .16s;
}
.ov-btn:hover { background: rgba(255, 255, 255, .13); transform: translateY(-1px); }
.ov-btn.primary {
  background: linear-gradient(135deg, #4f74ff, #2547eb);
  border-color: transparent;
  box-shadow: 0 8px 24px rgba(37, 71, 235, .45);
}
.ov-btn.primary:hover { box-shadow: 0 12px 30px rgba(37, 71, 235, .55); }

.ov-tip { font-size: 11.5px; color: var(--text-3); margin-top: 6px; }

/* ============ 移动端方向键 ============ */
/* 由 JS 检测触屏后给 body 加 .touch 类来显示，比媒体查询可靠（iOS Safari 不稳定） */
.dpad { display: none; }
body.touch .dpad {
  display: grid;
  position: fixed; z-index: 25;
  right: 14px; bottom: 14px;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 46px);
  gap: 4px;
  opacity: .92;
}
.dpad button {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; color: var(--text-2); font-size: 19px;
  backdrop-filter: blur(8px); cursor: pointer;
  display: grid; place-items: center;
  transition: .12s;
  touch-action: manipulation;
}
.dpad button:active { background: rgba(59, 102, 246, .45); color: #fff; transform: scale(.94); }
.dpad .empty { visibility: hidden; }
body.touch .hint { display: none; }
/* 触屏设备顶部安全区（避开刘海/挖孔） */
body.touch .hud { padding-top: calc(14px + env(safe-area-inset-top)); }
body.touch .progress-wrap { top: calc(70px + env(safe-area-inset-top)); }

/* 横屏手机：可用高度紧张 → 方向键改为横排单行，压缩底部占位 */
@media (orientation: landscape) and (max-height: 500px) {
  body.touch .dpad {
    grid-template-columns: repeat(4, 36px);
    grid-template-rows: 36px;
    right: 8px; bottom: 8px; gap: 3px;
    opacity: .88;
  }
  body.touch .dpad button { border-radius: 10px; font-size: 17px; }
  body.touch .dpad .empty { display: none; }
  body.touch .dpad button { order: 0; }
  /* 进度条移到顶部，压缩高度 */
  body.touch .progress-wrap {
    top: calc(6px + env(safe-area-inset-top));
    padding: 4px 10px; width: min(230px, 46vw);
  }
  body.touch .progress-track { height: 4px; }
  body.touch .progress-txt { font-size: 10px; min-width: 28px; }
  body.touch .hint-touch { display: none; }
  /* HUD 压缩：单行紧凑 */
  body.touch .hud { padding-top: calc(44px + env(safe-area-inset-top)); padding-bottom: 4px; gap: 6px; }
  body.touch .pill { height: 28px; padding: 0 9px; min-width: 44px; }
  body.touch .pill b { font-size: 13px; }
  body.touch .pill span { font-size: 8.5px; margin-top: 0; }
  body.touch .icon-btn { width: 30px; height: 30px; font-size: 14px; }
  body.touch .brand { display: none; }
  body.touch .combo { font-size: 13px; min-width: 26px; }
}

/* ============ 十关说明与道具 ============ */
.level-info {
  position: fixed; z-index: 19; top: 112px; left: 50%; transform: translateX(-50%);
  width: min(680px, 94vw); text-align: center; pointer-events: none;
}
.level-info strong { font-size: 14px; letter-spacing: .4px; }
.level-info p { font-size: 12px; color: var(--text-2); margin-top: 5px; line-height: 1.5; }
.effects { display: flex; justify-content: center; gap: 8px; margin-top: 9px; }
.effects span { font-size: 11px; padding: 4px 9px; color: #a5b4cd; border: 1px solid var(--border); background: #10192d; border-radius: 5px; font-variant-numeric: tabular-nums; }
.effects #effectShield.on { color: #82e5ff; border-color: #82e5ff; }
.effects #effectSlow.on { color: #8ce8ae; border-color: #8ce8ae; }
.effects #effectDouble.on { color: #ffd36b; border-color: #ffd36b; }
.play-guide { position: fixed; z-index: 18; bottom: 62px; left: 50%; transform: translateX(-50%); width: min(680px, 94vw); text-align: center; color: #a9bad7; font-size: 11px; line-height: 1.8; pointer-events: none; }
.terrain-legend { display: flex; justify-content: center; gap: 15px; flex-wrap: wrap; }
.terrain-legend span { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.swatch { width: 10px; height: 10px; border: 1px solid #56709d; display: inline-block; }
.swatch.safe { background: #16213a; }
.swatch.warning { background: #ffb52e; border-color: #ffb52e; }
.swatch.danger { background: #ff2349; border-color: #ff2349; }
.swatch.rock { background: #667085; border-color: #8993a8; border-radius: 3px; }
.shield-key { color: #82e5ff; }.slow-key { color: #8ce8ae; }.double-key { color: #ffd36b; }
.boost-btn { position: fixed; z-index: 25; bottom: 24px; left: 24px; width: 106px; min-height: 62px; padding: 10px; color: #ffdc8b; background: #2a2419; border: 1px solid #8f7137; border-radius: 12px; cursor: pointer; font-size: 14px; font-weight: 700; touch-action: none; }
.boost-btn small { display: block; margin-top: 4px; font-size: 10px; font-weight: 500; }
.boost-btn.active { background: #ffd36b; color: #20190d; border-color: #ffe3a3; }
.ov-logo { font-size: 10px; letter-spacing: 2.5px; color: #a8bfff; margin-bottom: 14px; }
.ov-power-guide { font-size: 12px; line-height: 1.7; color: #b7c7e6; margin: -5px 0 19px; }
.ov-note { line-height: 1.85; }
button:focus-visible { outline: 2px solid #ffdc8b; outline-offset: 3px; }
body.touch .level-info { top: calc(109px + env(safe-area-inset-top)); }
body.touch .play-guide { bottom: calc(178px + env(safe-area-inset-bottom)); font-size: 10px; width: 94vw; }
body.touch .terrain-legend { gap: 3px 11px; }
body.touch .play-guide p { max-width: 360px; margin: 4px auto 0; }
body.touch .boost-btn { bottom: calc(35px + env(safe-area-inset-bottom)); left: 18px; width: 110px; min-height: 80px; }
body.touch .dpad { bottom: calc(14px + env(safe-area-inset-bottom)); }
@media (orientation: landscape) and (max-height: 500px) {
  body.touch .level-info { top: calc(82px + env(safe-area-inset-top)); }
  body.touch .level-info strong { font-size: 11px; }
  body.touch .level-info p { font-size: 10px; margin-top: 2px; }
  body.touch .effects { margin-top: 4px; }
  body.touch .effects span { padding: 2px 6px; font-size: 10px; }
  body.touch .play-guide { bottom: 7px; width: 52vw; font-size: 9px; line-height: 1.4; }
  body.touch .terrain-legend { gap: 2px 8px; }
  body.touch .boost-btn { bottom: 8px; left: 8px; min-height: 42px; width: 95px; padding: 5px; font-size: 11px; }
  body.touch .boost-btn small { margin-top: 1px; font-size: 9px; }
}

/* ============ 响应式 ============ */
@media (max-width: 640px) {
  .hud { padding: 11px 13px; gap: 8px; }
  .brand { font-size: 13px; }
  .brand small { display: none; }
  .brand > div:last-child { display: none; }
  .brand .logo { width: 28px; height: 28px; }
  .combo { min-width: 0; font-size: 12px; }
  .icon-btn { width: 34px; height: 34px; flex-shrink: 0; }
  .pill { min-width: 44px; height: 38px; padding: 0 8px; flex-shrink: 0; }
  .pill b { font-size: 15px; }
  .progress-wrap { top: 62px; }
  .ov-box { padding: 28px 22px; border-radius: 18px; }
  .ov-box h2 { font-size: 22px; }
}