:root {
  --bg: #0f1420;
  --bg2: #161e2e;
  --bg3: #1d2839;
  --line: #2a3a55;
  --text: #e6ecf5;
  --dim: #8fa3bf;
  --accent: #4f8cff;
  --accent2: #ff6b6b;
  --green: #2ecc71;
}

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

/* ---------------- 布局 ---------------- */
body { display: flex; flex-direction: column; }
.app-header {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px;
  background: var(--bg2);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  font-size: 26px; font-weight: 700; color: var(--accent);
  width: 40px; height: 40px; display: flex; align-items: center; justify-content: center;
  background: var(--bg3); border-radius: 10px;
}
.brand h1 { font-size: 18px; }
.brand .sub { font-size: 12px; color: var(--dim); }
.mode-nav { display: flex; gap: 6px; margin-left: auto; }
.mode-tab {
  background: var(--bg3); color: var(--dim); border: 1px solid var(--line);
  padding: 8px 16px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.mode-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.panel-toggle {
  display: block; background: var(--bg3); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 16px;
}

.app-main { flex: 1 1 auto; display: flex; overflow: hidden; }
.canvas-area { flex: 1 1 auto; position: relative; min-width: 0; }
.view { position: absolute; inset: 0; }
.hidden { display: none !important; }
.view-hint {
  position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);
  color: var(--dim); font-size: 12px; background: rgba(15,20,32,0.7);
  padding: 4px 12px; border-radius: 20px; pointer-events: none;
}

#canvas2d, #view3d { width: 100%; height: 100%; display: block; background: var(--bg); touch-action: none; }
#view3d canvas { display: block; }

/* ---------------- 侧边面板 ---------------- */
.panel {
  width: 360px; flex: 0 0 auto;
  background: var(--bg2); border-left: 1px solid var(--line);
  display: flex; flex-direction: column; overflow: hidden;
}
.panel-tabs { display: flex; border-bottom: 1px solid var(--line); }
.panel-tab {
  flex: 1; background: none; color: var(--dim); border: none;
  padding: 12px; cursor: pointer; font-size: 14px; border-bottom: 2px solid transparent;
}
.panel-tab.active { color: var(--text); border-bottom-color: var(--accent); }
.panel-close {
  margin-left: auto; background: transparent; color: var(--dim);
  border: 1px solid var(--line); border-radius: 6px;
  width: 30px; height: 30px; cursor: pointer; font-size: 14px; flex: 0 0 auto;
}
.panel-close:hover { color: #fff; border-color: var(--accent2); }
.range-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0; }
.range-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--dim); }
.range-grid input { background: var(--bg3); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 6px; }
.zoom-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 8px; }
.panel { transition: transform .22s ease, width .22s ease; overflow: hidden; }
.panel.collapsed { transform: translateX(110%); width: 0 !important; min-width: 0 !important; border: none; }
.panel-body { padding: 12px; overflow-y: auto; flex: 1 1 auto; }

/* 表达式 */
.preset-bar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.preset-bar label { font-size: 12px; color: var(--dim); }
select, input[type="text"], input[type="number"] {
  background: var(--bg3); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 8px; font-size: 13px; outline: none;
}
select:focus, input:focus { border-color: var(--accent); }
.expr-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.expr-row {
  background: var(--bg3); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; display: flex; align-items: flex-start; gap: 8px; flex-wrap: wrap;
}
.expr-row input[type="checkbox"] { margin-top: 4px; }
.color-dot { width: 12px; height: 12px; border-radius: 50%; margin-top: 5px; flex: 0 0 auto; }
.expr-row select { max-width: 110px; }
.expr-inputs { flex: 1 1 100%; display: flex; flex-direction: column; gap: 6px; }
.expr-inputs-box { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.expr-inputs-box input[type="text"] { flex: 1 1 auto; min-width: 140px; }
.expr-inputs-box input[type="number"] { width: 70px; }
.range-label { color: var(--dim); font-size: 13px; }
.mini-btn {
  background: var(--bg3); color: var(--accent2); border: 1px solid var(--line);
  border-radius: 6px; padding: 4px 10px; cursor: pointer; font-weight: 700; font-size: 13px; flex: 0 0 auto;
}
.mini-btn:hover { border-color: var(--accent2); color: #fff; }
.icon-btn {
  background: transparent; color: var(--dim); border: none; cursor: pointer;
  font-size: 14px; padding: 4px 6px; border-radius: 6px;
}
.icon-btn:hover { color: var(--accent2); }

button.small {
  background: var(--accent); color: #fff; border: none; border-radius: 6px;
  padding: 6px 12px; cursor: pointer; font-size: 13px;
}
button.small.ghost { background: transparent; color: var(--dim); border: 1px solid var(--line); }
button.small.add { background: var(--bg3); color: var(--text); border: 1px solid var(--accent); }
.empty { color: var(--dim); font-size: 13px; padding: 8px 0; }

/* 参数 */
.param-add { display: flex; gap: 6px; margin-bottom: 6px; }
.param-add input[type="text"] { flex: 1; }
.param-add input[type="number"] { width: 80px; }
.param-error { color: var(--accent2); font-size: 12px; min-height: 16px; }
.param-list { display: flex; flex-direction: column; gap: 8px; }
.param-row {
  background: var(--bg3); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; display: flex; align-items: center; gap: 8px;
}
.param-name { font-size: 13px; min-width: 42px; color: var(--accent); }
.param-row input[type="range"] { flex: 1; accent-color: var(--accent); }
.param-row input[type="number"] { width: 74px; }

/* 视图 */
.panel-body h3 { margin: 12px 0 6px; font-size: 14px; color: var(--dim); }
.panel-body h3:first-child { margin-top: 0; }
label.row { display: flex; align-items: center; gap: 8px; font-size: 14px; margin-bottom: 8px; cursor: pointer; }
.panel-body > button { margin: 6px 8px 6px 0; }

/* ---------------- 计算 ---------------- */
.calc-panel { padding: 24px; max-width: 720px; margin: 0 auto; height: 100%; overflow-y: auto; }
.calc-panel h2 { font-size: 22px; margin-bottom: 8px; }
.dim { color: var(--dim); font-size: 13px; margin-bottom: 16px; }
.calc-form { display: flex; flex-direction: column; gap: 12px; }
.calc-form label { font-size: 13px; color: var(--dim); }
.calc-form input { width: 100%; padding: 10px; font-size: 15px; }
.calc-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px;
}
.calc-grid label { grid-column: span 1; }
button.primary {
  background: var(--accent); color: #fff; border: none; border-radius: 8px;
  padding: 12px; font-size: 15px; cursor: pointer;
}
.calc-output { margin-top: 18px; display: flex; flex-direction: column; gap: 8px; }
.calc-line {
  background: var(--bg3); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 14px; font-size: 14px; font-family: Consolas, "Courier New", monospace;
}
.calc-line.ok { border-left: 3px solid var(--green); }
.calc-line.err { border-left: 3px solid var(--accent2); }
.calc-line.dim { color: var(--dim); }

/* ---------------- 数学键盘 ---------------- */
.math-keyboard {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 100;
  background: #141b2b;
  border-top: 1px solid var(--line);
  transform: translateY(105%);
  transition: transform .2s ease;
  padding: 8px 8px 12px;
  user-select: none;
}
.math-keyboard.visible { transform: translateY(0); }
.kb-tabbar {
  display: flex; gap: 6px; align-items: center; margin-bottom: 8px;
}
.kb-tab {
  background: var(--bg3); color: var(--dim); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 14px; cursor: pointer; font-size: 14px;
}
.kb-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.kb-close {
  margin-left: auto; background: transparent; color: var(--dim);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; cursor: pointer;
}
.kb-grid {
  display: grid; grid-template-columns: repeat(10, 1fr); gap: 6px;
}
.kb-key {
  background: var(--bg3); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 0; font-size: 15px; cursor: pointer;
  min-height: 40px; font-family: Consolas, "Segoe UI", monospace;
}
.kb-key:active { background: var(--accent); }
.kb-editrow {
  display: flex; gap: 6px; margin-top: 8px;
}
.kb-editrow .kb-key { flex: 1; }
.kb-done { background: var(--accent); color: #fff; border-color: var(--accent); flex: 1.6 !important; }
@media (max-width: 720px) {
  .kb-grid { grid-template-columns: repeat(8, 1fr); }
}

/* 颜色选择器 */
.color-wrap { position: relative; display: inline-flex; align-items: center; }
.color-dot {
  cursor: pointer; transition: transform .1s;
}
.color-dot:hover { transform: scale(1.25); }

@media (max-width: 720px) {
  .brand .sub { display: none; }
  .panel-toggle { display: block; }
  .app-header { gap: 8px; padding: 8px 12px; }
  .brand { gap: 6px; min-width: 0; }
  .brand h1 { font-size: 16px; white-space: nowrap; }
  .logo { width: 34px; height: 34px; font-size: 22px; border-radius: 8px; }
  .panel {
    position: absolute; right: 0; top: 61px; bottom: 0; z-index: 20;
    width: min(90vw, 340px); box-shadow: -8px 0 24px rgba(0,0,0,0.4);
    transition: transform .22s ease;
  }
  .panel.collapsed { transform: translateX(105%); }
  .mode-nav { gap: 4px; }
  .mode-tab { padding: 7px 10px; font-size: 13px; white-space: nowrap; }
  .panel-toggle { padding: 6px 9px; }
}
