:root {
  --bg: #0e1320;
  --panel: #161d2e;
  --panel-2: #1d2740;
  --line: #2a3550;
  --text: #e6ebf5;
  --muted: #8b97b3;
  --accent: #4da3ff;
  --accent-2: #7c5cff;
  --danger: #ff5c7a;
  --ok: #46d39a;
  --shadow: 0 8px 30px rgba(0,0,0,.45);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  user-select: none;
}

#app { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; touch-action: none; }

/* ---------- 加载 ---------- */
#loading {
  position: fixed; inset: 0; z-index: 999;
  background: radial-gradient(circle at 50% 40%, #1b2540, #0a0e18);
  display: flex; align-items: center; justify-content: center;
  transition: opacity .4s;
}
#loading.hidden { opacity: 0; pointer-events: none; }
.loader-box { text-align: center; }
.spinner {
  width: 54px; height: 54px; margin: 0 auto 18px;
  border: 4px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loader-text { color: var(--muted); letter-spacing: 1px; }

/* ---------- 顶部栏 ---------- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; height: 54px; z-index: 50;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px;
  background: linear-gradient(180deg, rgba(22,29,46,.96), rgba(22,29,46,.82));
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand .logo { color: var(--accent); font-size: 22px; }
.brand .title { font-weight: 700; font-size: 15px; letter-spacing: .5px; }
.brand .ver { color: var(--muted); font-size: 11px; border: 1px solid var(--line); padding: 1px 6px; border-radius: 8px; }

.mode-switch { display: flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.mode-btn {
  border: 0; background: transparent; color: var(--muted);
  padding: 7px 18px; border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 600;
  transition: .15s;
}
.mode-btn.active { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; box-shadow: var(--shadow); }

.top-actions { display: flex; align-items: center; gap: 6px; }
.tbtn {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  padding: 7px 11px; border-radius: 8px; cursor: pointer; font-size: 13px; transition: .15s;
}
.tbtn:hover { border-color: var(--accent); color: #fff; }
.tbtn.danger:hover { border-color: var(--danger); color: var(--danger); }
.tbtn:disabled { opacity: .4; cursor: not-allowed; }
.divider { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }

/* ---------- 面板 ---------- */
.panel {
  position: fixed; top: 62px; bottom: 30px; width: 248px; z-index: 40;
  background: linear-gradient(180deg, rgba(22,29,46,.96), rgba(18,24,38,.96));
  border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden;
}
#leftPanel { left: 12px; }
#rightPanel { right: 12px; width: 290px; }

.panel-section { padding: 12px; border-bottom: 1px solid var(--line); }
.panel-section h3 { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; margin-bottom: 10px; }

.tool-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.tool-btn {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  padding: 9px 4px; border-radius: 9px; cursor: pointer; font-size: 18px; transition: .15s;
}
.tool-btn span { font-size: 11px; }
.tool-btn:hover { border-color: var(--accent); }
.tool-btn.active { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-color: transparent; color: #fff; }

.brush-size { margin-top: 10px; }
.brush-size label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 5px; }
.brush-size input[type=range] { width: 100%; }
.fill-hint { margin-top: 10px; font-size: 12px; color: var(--accent); background: rgba(77,163,255,.1); padding: 6px 8px; border-radius: 7px; }

.palette { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; max-height: 220px; overflow-y: auto; padding-right: 2px; }
.swatch {
  position: relative; aspect-ratio: 1; border-radius: 8px; cursor: pointer;
  border: 2px solid transparent; display: flex; align-items: flex-end; justify-content: center;
  overflow: hidden; transition: .12s;
}
.swatch .chip { position: absolute; inset: 0; }
.swatch .nm { position: relative; font-size: 9px; color: #fff; background: rgba(0,0,0,.55); width: 100%; text-align: center; padding: 1px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.swatch:hover { transform: translateY(-2px); }
.swatch.active { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }
.palette-search { margin-top: 8px; }
.palette-search input { width: 100%; padding: 6px 8px; border-radius: 7px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 12px; }

/* ---------- 右侧 Tabs ---------- */
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tab { flex: 1; border: 0; background: transparent; color: var(--muted); padding: 11px 0; cursor: pointer; font-size: 13px; font-weight: 600; border-bottom: 2px solid transparent; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-body { flex: 1; overflow-y: auto; padding: 12px; }
.tab-pane { display: none; }
.tab-pane.active { display: block; }

.field { display: block; font-size: 12px; color: var(--muted); margin-bottom: 14px; }
.field span { color: var(--text); float: right; }
.field input[type=range] { width: 100%; margin-top: 6px; }
.field input[type=color] { width: 100%; height: 30px; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); margin-top: 6px; }
.field-col { font-size: 12px; color: var(--muted); margin-bottom: 14px; }
.field-col > span { display: block; margin-bottom: 6px; }
.vec3, .stat { background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 7px 9px; font-size: 12px; color: var(--text); font-family: monospace; }
.mini-btn { margin-top: 8px; border: 1px solid var(--line); background: var(--panel-2); color: var(--text); padding: 7px 10px; border-radius: 7px; cursor: pointer; font-size: 12px; }
.mini-btn:hover { border-color: var(--accent); }
.mini-btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-color: transparent; color: #fff; }

.empty-hint { color: var(--muted); font-size: 12px; line-height: 1.6; }
#inspectorContent .insp-row { display: flex; justify-content: space-between; font-size: 12px; padding: 6px 0; border-bottom: 1px dashed var(--line); }
#inspectorContent .insp-row b { color: var(--muted); font-weight: 500; }

/* ---------- 脚本 ---------- */
.script-tip { font-size: 11px; color: var(--muted); line-height: 1.6; margin-bottom: 10px; }
.script-tip code { background: var(--bg); padding: 1px 5px; border-radius: 4px; color: var(--accent); }
.script-actions { display: flex; gap: 8px; margin-bottom: 8px; }
#scriptCode {
  width: 100%; height: 200px; resize: vertical; background: #0b1020; color: #cfe3ff;
  border: 1px solid var(--line); border-radius: 8px; padding: 9px; font-family: "JetBrains Mono", Consolas, monospace;
  font-size: 12px; line-height: 1.5;
}
.console { margin-top: 10px; background: #0b1020; border: 1px solid var(--line); border-radius: 8px; height: 120px; overflow-y: auto; padding: 8px; font-family: monospace; font-size: 11px; color: #9fe6c0; }
.console .err { color: var(--danger); }
.console .line { white-space: pre-wrap; word-break: break-all; }

/* ---------- 弹窗 ---------- */
.modal { position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; }
.modal-card { width: 420px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal-head h2 { font-size: 15px; }
.modal-close { border: 0; background: transparent; color: var(--muted); font-size: 22px; cursor: pointer; }
.modal-body { padding: 16px; font-size: 13px; color: var(--muted); line-height: 1.7; }
.modal-actions { display: flex; gap: 8px; margin: 12px 0; }
.muted { color: var(--muted); font-size: 12px; }

/* ---------- 运行 HUD ---------- */
#playHud { position: fixed; inset: 0; z-index: 45; pointer-events: none; }
.crosshair { position: absolute; left: 50%; top: 50%; width: 18px; height: 18px; transform: translate(-50%,-50%); }
.crosshair::before, .crosshair::after { content: ""; position: absolute; background: rgba(255,255,255,.85); }
.crosshair::before { left: 50%; top: 0; width: 2px; height: 100%; transform: translateX(-50%); }
.crosshair::after { top: 50%; left: 0; height: 2px; width: 100%; transform: translateY(-50%); }
.play-hint { position: absolute; left: 50%; bottom: 64px; transform: translateX(-50%); background: rgba(0,0,0,.5); padding: 7px 14px; border-radius: 20px; font-size: 12px; color: #fff; }
.play-stats { position: absolute; left: 16px; top: 64px; background: rgba(0,0,0,.45); padding: 8px 12px; border-radius: 10px; font-size: 12px; font-family: monospace; color: #cfe3ff; line-height: 1.6; }

/* ---------- 底部状态栏 ---------- */
#statusbar {
  position: fixed; left: 0; right: 0; bottom: 0; height: 28px; z-index: 50;
  display: flex; align-items: center; gap: 18px; padding: 0 14px;
  background: rgba(14,19,32,.92); border-top: 1px solid var(--line);
  font-size: 12px; color: var(--muted); font-family: monospace;
}
#statTip { margin-left: auto; color: #5e6a86; }

/* 编辑/运行模式时隐藏面板 */
body.playing #leftPanel,
body.playing #rightPanel,
body.playing #topbar .top-actions,
body.playing #topbar .brand .ver { display: none; }
body.playing #topbar { background: linear-gradient(180deg, rgba(22,29,46,.6), rgba(22,29,46,0)); border-bottom: 0; }

/* 滚动条 */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 8px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- 移动端虚拟控制（仅触摸设备显示） ---------- */
#joystick {
  position: absolute; left: 22px; bottom: 92px; width: 112px; height: 112px;
  border-radius: 50%; background: rgba(255,255,255,.07); border: 2px solid rgba(255,255,255,.18);
  display: none; touch-action: none;
}
#joyKnob {
  position: absolute; left: 50%; top: 50%; width: 50px; height: 50px; margin: -25px 0 0 -25px;
  border-radius: 50%; background: rgba(77,163,255,.6); border: 2px solid rgba(255,255,255,.55);
}
#btnJump {
  position: absolute; right: 22px; bottom: 100px; width: 80px; height: 80px; border-radius: 50%;
  background: rgba(124,92,255,.5); border: 2px solid rgba(255,255,255,.4); color: #fff;
  font-size: 18px; font-weight: 700; cursor: pointer; display: none; pointer-events: auto; touch-action: none;
}
#btnJump:active { background: rgba(124,92,255,.85); }
@media (hover: none) and (pointer: coarse) {
  #joystick, #btnJump { display: block; }
  #joystick { pointer-events: auto; }
}
