:root {
  --bg: #0f1420;
  --bg2: #171e2e;
  --panel: #1c2436;
  --border: #2a3450;
  --text: #e8edf7;
  --muted: #8b96ad;
  --accent: #ffb020;
  --accent2: #4cc2ff;
  --green: #35d07f;
  --red: #ff5f6d;
  --road: #2b3348;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: radial-gradient(1200px 700px at 50% -10%, #1b2438, var(--bg));
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

#app { display: flex; flex-direction: column; height: 100vh; }

/* 顶部栏 */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #ff7a30);
  display: grid; place-items: center;
  font-weight: 800; font-size: 22px; color: #1a1200;
}
.title { font-size: 18px; font-weight: 700; }
.title small { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }

.hud { display: flex; gap: 8px; }
.hud-item {
  display: flex; flex-direction: column; align-items: center;
  min-width: 64px; padding: 6px 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
}
.hud-item .label { font-size: 11px; color: var(--muted); }
.hud-item span:last-child { font-size: 18px; font-weight: 700; }
#score { color: var(--accent); }
#collisions { color: var(--red); }

.top-actions { display: flex; gap: 8px; }
.btn {
  padding: 8px 16px; border-radius: 8px; border: none; cursor: pointer;
  background: var(--accent); color: #1a1200; font-weight: 700; font-size: 14px;
}
.btn:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }

/* 主体 */
#main { display: flex; flex: 1; min-height: 0; }
#canvas-wrap { flex: 1; position: relative; min-width: 0; }
#game { display: block; width: 100%; height: 100%; cursor: crosshair; }

#hint {
  position: absolute; top: 18px; left: 50%; transform: translateX(-50%);
  padding: 10px 20px; border-radius: 20px;
  background: rgba(20, 24, 34, .85); border: 1px solid var(--accent);
  color: var(--accent); font-size: 15px; font-weight: 600;
  pointer-events: none; transition: opacity .2s;
  box-shadow: 0 4px 20px rgba(0,0,0,.4);
}
#hint:empty { opacity: 0; }

/* 侧边栏 */
#panel {
  width: 300px; flex-shrink: 0; overflow-y: auto;
  background: var(--bg2); border-left: 1px solid var(--border);
  padding: 16px;
}
.panel-block { margin-bottom: 20px; }
.panel-block h3 {
  font-size: 13px; color: var(--muted); text-transform: uppercase;
  letter-spacing: 1px; margin-bottom: 10px;
}

.level-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.level-card {
  padding: 10px; border-radius: 10px; cursor: pointer;
  background: var(--panel); border: 1px solid var(--border);
  transition: all .15s;
}
.level-card:hover { border-color: var(--accent2); }
.level-card.active { border-color: var(--accent); background: rgba(255,176,32,.08); }
.level-card .lname { font-weight: 700; font-size: 14px; }
.level-card .ldesc { font-size: 11px; color: var(--muted); margin-top: 2px; }

.tool-row { display: flex; flex-wrap: wrap; gap: 6px; }
.tool {
  flex: 1 1 40%; padding: 8px; border-radius: 8px; cursor: pointer;
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  font-size: 12px;
}
.tool:hover { border-color: var(--accent2); }
.tool.active { border-color: var(--accent); background: rgba(255,176,32,.12); }

.tip { font-size: 12px; color: var(--muted); margin-top: 8px; line-height: 1.5; }

.keys { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.key {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; font-size: 13px;
}
.key span { color: var(--muted); font-size: 12px; }
.key { font-family: "SF Mono", monospace; }

.switch {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0; font-size: 14px; cursor: pointer; color: var(--text);
}
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }

/* 弹窗 */
#modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: grid; place-items: center; z-index: 100;
}
#modal.hidden { display: none; }
.hidden { display: none !important; }
.modal-card {
  width: 460px; max-width: 90vw;
  background: var(--bg2); border: 1px solid var(--border); border-radius: 16px;
  padding: 28px;
}
.modal-card h2 { margin-bottom: 16px; }
.modal-card ul { list-style: none; }
.modal-card li { padding: 8px 0; font-size: 14px; line-height: 1.6; border-bottom: 1px solid var(--border); }
.modal-card li b { color: var(--accent); }
.modal-card .btn { margin-top: 20px; width: 100%; }

/* ===== 手机触控控件 ===== */
#touch-controls {
  display: none;
  position: absolute; inset: 0;
  pointer-events: none;
  z-index: 20;
}
.tc-left, .tc-right {
  position: absolute; bottom: 26px;
  display: flex; gap: 14px; align-items: flex-end;
  pointer-events: none;
}
.tc-left { left: 20px; }
.tc-right { right: 20px; }
.tc-btn {
  pointer-events: auto;
  width: 74px; height: 74px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.25);
  background: rgba(20, 24, 34, 0.55);
  color: #fff; font-size: 26px; font-weight: 800;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.tc-btn.small { width: 60px; height: 60px; font-size: 14px; }
.tc-btn:active, .tc-btn.pressed {
  background: rgba(255, 176, 32, 0.75);
  border-color: var(--accent);
  color: #1a1200;
}

/* 触屏设备显示控件、隐藏键盘说明 */
@media (pointer: coarse) {
  #touch-controls { display: block; }
  #hint { top: 10px; font-size: 13px; padding: 8px 14px; max-width: 92vw; }
}

@media (max-width: 760px) {
  #main { flex-direction: column; }
  #canvas-wrap { flex: 1; min-height: 0; }
  #panel {
    width: 100%; max-height: 32vh;
    border-left: none; border-top: 1px solid var(--border);
  }
  #topbar { padding: 8px 12px; flex-wrap: wrap; gap: 6px; }
  .hud { order: 3; width: 100%; justify-content: space-between; }
  .hud-item { min-width: 0; flex: 1; padding: 4px 6px; }
  .hud-item span:last-child { font-size: 15px; }
  .title { font-size: 15px; }
  .logo { width: 32px; height: 32px; font-size: 18px; }
  .keys { display: none; } /* 手机上隐藏键盘说明 */
  .level-list { grid-template-columns: repeat(3, 1fr); }
  .modal-card { padding: 20px; }
}

/* 横屏手机：侧边栏收起为底部小条 */
@media (max-width: 900px) and (orientation: landscape) and (pointer: coarse) {
  #panel { display: none; }
}
