:root {
  --bg: #111318;
  --surface: #1c1f27;
  --surface-2: #262a35;
  --line: #333948;
  --text: #eceef3;
  --muted: #9aa1b2;
  --accent: #e5412d;
  --accent-2: #f7b733;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --bar-h: 48px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: var(--safe-t) var(--safe-r) 0 var(--safe-l);
  touch-action: none; /* 禁止双击缩放、下拉等手势 */
}

/* ---------- 顶部工具栏 ---------- */
.toolbar {
  flex: 0 0 var(--bar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}
.title { font-weight: 700; font-size: 16px; white-space: nowrap; display: flex; align-items: center; gap: 8px; }
.badge {
  font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--accent-2); font-variant-numeric: tabular-nums;
}
.actions { margin-left: auto; display: flex; gap: 6px; }
.tb {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 12px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px;
  font-size: 15px;
}
.tb span { font-size: 13px; }
.tb:hover { border-color: #4a5266; }
.tb:active { transform: scale(.96); }
.tb.primary { background: var(--accent); border-color: var(--accent); }
.tb.on { border-color: var(--accent-2); }
.tb[hidden] { display: none; }

/* ---------- 游戏画面 ---------- */
#stage {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
#screen {
  display: block;
  width: 256px;
  height: 240px;
  background: #000;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  box-shadow: 0 6px 30px rgba(0, 0, 0, .5);
}
.mask {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10, 11, 15, .6);
  text-align: center; font-size: 22px; font-weight: 700;
  z-index: 3;
}
.mask[hidden] { display: none; }
.mask small { display: block; margin-top: 6px; font-size: 13px; font-weight: 400; color: var(--muted); }
.mask p { margin: 0 0 6px; }
#paused-mask { cursor: pointer; }
.file-btn {
  display: inline-block; margin-top: 16px; padding: 10px 18px;
  background: var(--accent); border-radius: 10px; font-size: 15px; cursor: pointer;
}
.file-btn input { display: none; }

.help {
  flex: 0 0 auto;
  padding: 8px 12px calc(8px + var(--safe-b));
  text-align: center; color: var(--muted); font-size: 12px;
}
body.touch .help, body.pad-on .help { display: none; }

/* ---------- 选关弹窗 ---------- */
.modal {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  padding: calc(12px + var(--safe-t)) calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) calc(12px + var(--safe-l));
  background: rgba(5, 6, 9, .72);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.modal[hidden] { display: none; }
.sheet {
  width: min(760px, 100%);
  max-height: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px 16px 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 18px; }
.close {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--line); font-size: 16px;
}
.world-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  gap: 10px;
}
.world {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px;
}
.world-name { font-size: 12px; font-weight: 700; color: var(--muted); letter-spacing: .08em; margin: 0 0 6px 2px; }
.levels { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.lv {
  height: 40px;
  min-width: 0;
  padding: 0;
  white-space: nowrap;
  border-radius: 8px;
  border: 1px solid #454c5e;
  background: #313644;
  font-weight: 700;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}
.lv:hover { border-color: var(--accent-2); }
.lv:active { transform: scale(.94); }
.lv.castle { background: #3b2f35; border-color: #6a4450; }
.lv.current { background: var(--accent); border-color: #ff8a73; }
.sheet-tip { margin: 10px 0 0; text-align: center; color: var(--muted); font-size: 12px; }

/* ---------- 窄屏 / 横屏适配 ---------- */
@media (max-width: 640px) {
  .title { display: none; }
  .toolbar { padding: 0 6px; }
  .actions { margin: 0; width: 100%; justify-content: space-between; gap: 4px; }
  .tb { flex: 1; flex-direction: column; justify-content: center; gap: 0; height: 40px; padding: 0 2px; font-size: 16px; line-height: 1.1; }
  .tb span { font-size: 10px; }
}
/* 手机竖屏：每个大关一行，32 关无需滚动即可看全 */
@media (max-width: 480px) {
  .world-grid { grid-template-columns: 1fr; gap: 6px; }
  .world { display: flex; align-items: center; gap: 8px; padding: 6px 8px; }
  .world-name { flex: 0 0 42px; margin: 0; font-size: 11px; line-height: 1.2; }
  .levels { flex: 1; }
  .lv { height: 38px; }
}
@media (orientation: landscape) and (max-height: 520px) {
  :root { --bar-h: 38px; }
  .tb { height: 30px; }
  .title { font-size: 14px; }
}

/* ---------- 3D 模式 HUD ---------- */
.hud3d { position: absolute; pointer-events: none; z-index: 2; }
.hud-bar {
  display: flex; justify-content: space-between; gap: .6em;
  margin: .6em; padding: .45em .9em; border-radius: .8em;
  background: rgba(12, 16, 28, .38);
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
  color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, .5);
}
.hud-bar span { display: flex; flex-direction: column; align-items: center; line-height: 1.15; }
.hud-bar small { font-size: .62em; letter-spacing: .12em; opacity: .75; }
.hud-bar b { font-size: 1em; font-variant-numeric: tabular-nums; }
.hud-card {
  position: absolute; inset: 0; display: none;
  flex-direction: column; align-items: center; justify-content: center; gap: .5em;
  color: #fff; text-align: center;
}
.hud-card b { font-size: 2em; letter-spacing: .08em; text-shadow: 0 3px 12px rgba(0, 0, 0, .6); }
.hud-card small { font-size: .9em; opacity: .85; }
.hud-card.solid { background: radial-gradient(circle at 50% 45%, #25305a, #07090f 75%); }
