/* ============ 全局 ============ */
:root {
  --bg: #0a0d16;
  --bg2: #0f1422;
  --panel: #131a2b;
  --panel2: #182136;
  --border: #26314d;
  --text: #e8edf7;
  --muted: #8b96ad;
  --accent: #6d8dff;
  --accent2: #9db4ff;
  --c-x: #e5484d;
  --c-y: #30a46c;
  --c-z: #0091ff;
  --c-a: #f97316;
  --c-b: #a78bfa;
  --c-c: #22d3ee;
  --c-blue: #60a5fa;
  --c-green: #4ade80;
  --c-yellow: #facc15;
  --c-pink: #f472b6;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(1200px 800px at 70% -10%, #16203a 0%, var(--bg) 55%);
  color: var(--text);
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  font-size: 14px;
  overflow: hidden;
}

#app { display: grid; grid-template-columns: 400px 1fr; height: 100vh; }

/* ============ 侧栏 ============ */
#sidebar {
  display: flex; flex-direction: column;
  background: linear-gradient(180deg, var(--panel) 0%, #0d1220 100%);
  border-right: 1px solid var(--border);
  min-height: 0;
}
.brand { padding: 16px 18px 10px; border-bottom: 1px solid var(--border); }
.brand h1 { margin: 0; font-size: 19px; letter-spacing: 0.5px; }
.brand h1::first-letter { color: var(--accent2); }
.brand p { margin: 4px 0 0; font-size: 11.5px; color: var(--muted); }

#tabnav {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
}
.tab-btn {
  text-align: left; padding: 8px 10px; font-size: 12.5px;
  background: transparent; color: var(--muted);
  border: 1px solid transparent; border-radius: 8px; cursor: pointer;
  transition: all .15s;
}
.tab-btn:hover { background: var(--panel2); color: var(--text); }
.tab-btn.active { background: linear-gradient(135deg, #2b3c66, #1e2a4a); color: #fff; border-color: #3d4f7e; }

#controls { flex: 1; overflow-y: auto; padding: 12px 14px 24px; min-height: 0; }
.panel h2 { font-size: 15.5px; margin: 4px 0 8px; }
.panel h3 { font-size: 12px; color: var(--accent2); margin: 14px 0 6px; text-transform: uppercase; letter-spacing: .6px; }
.panel .desc { color: var(--muted); font-size: 12.5px; line-height: 1.65; }
.panel p { line-height: 1.6; }

.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.card {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  background: var(--panel2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px; color: var(--text); cursor: pointer; text-align: left; transition: all .15s;
}
.card:hover { border-color: var(--accent); transform: translateY(-1px); background: #1d2947; }
.card span { font-size: 20px; }
.card b { font-size: 13px; }
.card i { font-size: 11px; color: var(--muted); font-style: normal; }

/* 输入矩阵 */
.matgrid { display: grid; gap: 5px; width: fit-content; padding: 8px; background: #0c101d; border: 1px solid var(--border); border-radius: var(--radius); position: relative; }
.matgrid::before, .matgrid::after { position: absolute; top: -4px; bottom: -4px; width: 3px; content: "["; color: var(--muted); font-size: 60px; line-height: 1.3; font-weight: 100; }
.matgrid::before { left: -4px; content: "["; }
.matgrid::after { right: -4px; content: "]"; }
.m2 { grid-template-columns: repeat(2, 58px); }
.m3 { grid-template-columns: repeat(3, 58px); }
.v3 { grid-template-columns: repeat(3, 58px); }
.cell {
  width: 58px; padding: 5px 2px; text-align: center;
  background: #0a0f1c; color: #cfe3ff; border: 1px solid #2c3a5e; border-radius: 6px;
  font-family: "Cascadia Code", Consolas, monospace; font-size: 13px;
}
.cell:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(109,141,255,.25); }
.coordcells .cell { width: 58px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 5px 10px; font-size: 12px; background: #182238; color: #c7d3ee;
  border: 1px solid var(--border); border-radius: 999px; cursor: pointer; transition: all .15s;
}
.chip:hover { border-color: var(--accent); color: #fff; }
.chip.on { background: #2c4174; border-color: var(--accent2); color: #fff; }

.row { display: flex; align-items: center; gap: 8px; margin: 6px 0; flex-wrap: wrap; }
.seg { gap: 10px; }
.seg label { display: flex; align-items: center; gap: 4px; color: var(--muted); cursor: pointer; font-size: 12.5px; }
.seg label:has(input:checked) { color: var(--text); }
.checks { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; margin: 8px 0; }
.checks label { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12.5px; cursor: pointer; }
.checks label:has(input:checked) { color: var(--text); }
.pow-row { display: flex; align-items: center; gap: 6px; }
.powk { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12.5px; flex: 1; min-width: 0; }
.powk input[type="range"] { width: 56px; flex: 0 0 56px; }
i.c-blue { display: inline-block; width: 9px; height: 9px; border-radius: 2px; background: var(--c-blue); }
i.c-green { display: inline-block; width: 9px; height: 9px; border-radius: 2px; background: var(--c-green); }
i.c-yellow { display: inline-block; width: 9px; height: 9px; border-radius: 2px; background: var(--c-yellow); }
i.c-pink { display: inline-block; width: 9px; height: 9px; border-radius: 2px; background: var(--c-pink); }

select, .mini {
  background: #182238; color: var(--text); border: 1px solid var(--border);
  border-radius: 7px; padding: 5px 9px; font-size: 12.5px; cursor: pointer;
}
.mini:hover { border-color: var(--accent); }

input[type="range"] { flex: 1; accent-color: var(--accent); min-width: 80px; }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); }

.solbox { background: #0c101d; border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; margin-top: 10px; }
.sol-head { font-size: 12px; color: var(--accent2); font-weight: 600; margin-bottom: 6px; }
.solbox .val { font-family: "Cascadia Code", Consolas, monospace; font-size: 13.5px; color: #ffe08a; }
.solbox .dim { font-family: "Cascadia Code", Consolas, monospace; font-size: 13.5px; color: #8ee6c9; }

.steps {
  margin: 10px 0 0; padding: 10px 12px; background: #0c101d; border: 1px solid var(--border);
  border-radius: var(--radius); color: #9fb4dd; font-size: 11.5px; line-height: 1.7;
  white-space: pre-wrap; font-family: "Cascadia Code", Consolas, monospace; max-height: 180px; overflow-y: auto;
}

.refs { list-style: none; padding: 0; margin: 0; }
.refs li { margin-bottom: 10px; }
.refs a { color: var(--accent2); text-decoration: none; font-weight: 600; display: block; font-size: 13px; }
.refs a:hover { text-decoration: underline; }
.refs span { color: var(--muted); font-size: 12px; line-height: 1.55; display: block; margin-top: 2px; }

/* ============ 右侧舞台 ============ */
#right { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#stage { position: relative; flex: 1; min-height: 0; }
#viewport { position: absolute; inset: 0; }
#viewport canvas { display: block; }

#hud {
  position: absolute; top: 12px; left: 12px; z-index: 5;
  display: flex; flex-direction: column; gap: 6px; pointer-events: none;
}
.hud-item {
  background: rgba(10, 14, 24, .82); border: 1px solid rgba(90, 110, 160, .4);
  border-radius: 8px; padding: 6px 11px; font-size: 12.5px; color: #dbe6ff;
  backdrop-filter: blur(4px);
}
.hud-item b { color: #ffe08a; font-family: "Cascadia Code", Consolas, monospace; font-weight: 600; }
.hud-item .hl-green { color: #7dedb2; }
.hud-item .hl-red { color: #ff8f8f; }
.hud-item .hl-blue { color: #8ab4ff; }

#legend {
  position: absolute; left: 12px; bottom: 12px; z-index: 5; pointer-events: none;
  background: rgba(10, 14, 24, .78); border: 1px solid rgba(90, 110, 160, .35);
  border-radius: 8px; padding: 8px 12px; font-size: 12px; color: #aebcd9;
  display: flex; flex-direction: column; gap: 3px; backdrop-filter: blur(4px);
}
#legend b { color: #e6ecfa; }
.leg { display: flex; align-items: center; gap: 7px; }
.leg i { display: inline-block; width: 14px; height: 4px; border-radius: 2px; }

#hint {
  position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); z-index: 5;
  background: rgba(10, 14, 24, .65); border-radius: 999px; padding: 4px 14px;
  font-size: 11.5px; color: #74809c; pointer-events: none;
}
#errbox {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 20;
  max-width: 560px; background: #2a1216; border: 1px solid #8e3541; color: #ffb4bd;
  border-radius: 10px; padding: 16px 20px; font-size: 13px; line-height: 1.6;
  white-space: pre-wrap; font-family: Consolas, monospace;
}

/* ============ 底部概念栏 ============ */
#explainbar { position: relative; border-top: 1px solid var(--border); background: rgba(15, 20, 34, .96); }
#explain-wrap { max-height: 230px; overflow-y: auto; padding: 14px 22px 16px; }
#explainbar.collapsed #explain-wrap { display: none; }
.explain h3 { margin: 2px 0 6px; font-size: 14px; color: var(--accent2); }
.explain p { color: #c6d1e8; line-height: 1.75; margin: 6px 0; }
.explain ul { color: #c6d1e8; margin: 6px 0; padding-left: 22px; line-height: 1.8; }
.explain li { margin-bottom: 3px; }
.explain .katex-display { margin: 8px 0; }
#explain-toggle {
  position: absolute; right: 12px; top: -14px; z-index: 6;
  background: #1c2745; color: #b9c9f2; border: 1px solid #33456f; border-radius: 999px;
  padding: 5px 14px; font-size: 12px; cursor: pointer;
}
#explain-toggle:hover { background: #243357; }

/* 滚动条 */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #2a3550; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #3a4a70; }
::-webkit-scrollbar-track { background: transparent; }

@media (max-width: 1100px) {
  #app { grid-template-columns: 340px 1fr; }
  .m3 { grid-template-columns: repeat(3, 48px); }
  .m2 { grid-template-columns: repeat(2, 48px); }
  .cell { width: 48px; }
}
