:root {
  color-scheme: dark;
  --bg: #0b0f14;
  --panel: #121a23;
  --line: #223042;
  --text: #e6edf6;
  --muted: #8b9bb0;
  --accent: #4cc2ff;
  --floor: #141d27;
  --wall-a: #33465c;
  --wall-b: #232f3d;
  --box-a: #d99a55;
  --box-b: #a8652f;
  --done-a: #63d98a;
  --done-b: #2f9e5b;
  /* 非棋盘区域预留高度：竖屏留 HUD + 操作栏 + 提示，横屏更紧凑 */
  --chrome: 250px;
}

@media (orientation: landscape) {
  :root {
    --chrome: 170px;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background:
    radial-gradient(circle at 50% -20%, #16324a 0%, var(--bg) 60%),
    var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei',
    sans-serif;
  /* 棋盘上要滑动，禁止页面拖动/缩放/回弹 */
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overflow: hidden;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
}

.app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: min(100%, 520px);
  padding: max(12px, env(safe-area-inset-top)) 14px calc(12px + env(safe-area-inset-bottom));
}

/* ---------- HUD ---------- */

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
}

.hud-title {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
  min-width: 0;
}

.hud-title strong {
  font-size: 15px;
  letter-spacing: 0.5px;
}

.hud-title span {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hud-stats {
  display: flex;
  gap: 16px;
  margin: 0;
}

.hud-stats div {
  text-align: right;
}

.hud-stats dt {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 1px;
}

.hud-stats dd {
  margin: 0;
  font: 700 17px/1.1 ui-monospace, SFMono-Regular, Menlo, monospace;
  font-variant-numeric: tabular-nums;
}

/* ---------- 棋盘 ---------- */

.stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  touch-action: none;
}

.board {
  position: relative;
  width: min(92vw, 480px, calc(max(0px, 100dvh - var(--chrome)) * var(--ratio, 1)));
  aspect-ratio: var(--cols, 7) / var(--rows, 7);
  border-radius: 10px;
  background: #0a1119;
  box-shadow:
    0 0 0 1px rgba(120, 170, 220, 0.14),
    0 18px 40px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.cell,
.box,
.player {
  position: absolute;
  left: 0;
  top: 0;
  width: calc(100% / var(--cols, 7));
  height: calc(100% / var(--rows, 7));
}

.cell.floor {
  background: var(--floor);
  box-shadow: inset 0 0 0 1px rgba(90, 130, 170, 0.07);
}

.cell.wall {
  background: linear-gradient(180deg, var(--wall-a), var(--wall-b));
  box-shadow:
    inset 0 0 0 1px rgba(140, 185, 230, 0.16),
    inset 0 -3px 0 rgba(0, 0, 0, 0.22);
  border-radius: 3px;
}

.cell.goal::after {
  content: '';
  position: absolute;
  inset: 33%;
  border-radius: 50%;
  background: rgba(76, 194, 255, 0.5);
  box-shadow: 0 0 8px rgba(76, 194, 255, 0.45);
}

.box {
  z-index: 2;
  transition: transform 0.1s ease-out;
  padding: 1px;
  pointer-events: none;
}

.box::before {
  content: '';
  position: absolute;
  inset: 9%;
  border-radius: 6px;
  background: linear-gradient(160deg, var(--box-a), var(--box-b));
  box-shadow:
    inset 0 0 0 2px rgba(255, 222, 176, 0.35),
    inset 0 -4px 0 rgba(0, 0, 0, 0.18),
    0 2px 6px rgba(0, 0, 0, 0.35);
}

.box.done::before {
  background: linear-gradient(160deg, var(--done-a), var(--done-b));
  box-shadow:
    inset 0 0 0 2px rgba(210, 255, 225, 0.45),
    0 0 12px rgba(90, 230, 140, 0.35);
}

.player {
  z-index: 3;
  transition: transform 0.09s ease-out;
  pointer-events: none;
}

.player::before {
  content: '';
  position: absolute;
  inset: 15%;
  border-radius: 50%;
  background: radial-gradient(circle at 38% 32%, #9ae6ff, #2b8cc4 70%, #1b6288);
  box-shadow:
    0 0 10px rgba(76, 194, 255, 0.45),
    inset 0 0 0 2px rgba(255, 255, 255, 0.24);
}

/* 首次搭建棋盘时不要播放位移动画 */
.board.no-anim .box,
.board.no-anim .player {
  transition: none;
}

/* ---------- 通关遮罩 ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border-radius: 10px;
  background: rgba(5, 10, 16, 0.84);
  backdrop-filter: blur(2px);
  padding: 16px;
  text-align: center;
}

.overlay[hidden] {
  display: none;
}

.overlay-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 1px;
}

.overlay-text {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.6;
}

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

.bar {
  display: flex;
  gap: 10px;
  width: 100%;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition:
    background 0.12s ease,
    border-color 0.12s ease,
    transform 0.08s ease;
}

button:active {
  transform: scale(0.97);
  border-color: var(--accent);
}

.bar button {
  flex: 1;
  height: 52px;
  font-size: 15px;
}

.primary {
  min-width: 140px;
  height: 46px;
  font-weight: 700;
  color: #04121c;
  background: var(--accent);
  border-color: var(--accent);
}

.hint {
  margin: 0;
  min-height: 18px;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

/* ---------- 关卡面板 ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
}

.sheet[hidden] {
  display: none;
}

.sheet-panel {
  width: min(100%, 480px);
  max-height: 78vh;
  overflow-y: auto;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  border-radius: 18px 18px 0 0;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.sheet-head h2 {
  margin: 0;
  font-size: 16px;
  letter-spacing: 0.5px;
}

.sheet-close {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  font-size: 15px;
}

.sheet-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 10px;
}

.level-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 10px 4px;
  min-height: 62px;
}

.level-btn .level-num {
  font: 700 16px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}

.level-btn .level-meta {
  font-size: 10px;
  color: var(--muted);
  text-align: center;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.level-btn.done {
  border-color: rgba(90, 230, 140, 0.45);
}

.level-btn.done .level-meta {
  color: #63d98a;
}

.level-btn.current {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(76, 194, 255, 0.35);
}

@media (orientation: landscape) and (max-height: 460px) {
  .hud-title strong {
    font-size: 13px;
  }
  .bar button {
    height: 44px;
  }
  .hint {
    display: none;
  }
}
