/* ==========================================================================
   函数图像工作台 —— 样式
   主题：body[data-theme="light"|"dark"]，所有颜色走 CSS 变量。
   画布配色不在这里，见 js/render/draw.js 的 PALETTES（两处需同步修改）。
   ========================================================================== */

:root {
  --bg: #f4f6fa;
  --bg-panel: #ffffff;
  --bg-elev: #ffffff;
  --bg-input: #f7f8fb;
  --border: #e2e6ee;
  --border-strong: #cbd2e0;
  --text: #1f2733;
  --text-dim: #67718a;
  --text-faint: #98a1b5;
  --accent: #2f6df6;
  --accent-soft: #e8efff;
  --danger: #e5484d;
  --ok: #12a065;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
  --radius: 10px;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "SF Mono", Menlo, monospace;
}

body[data-theme="dark"] {
  --bg: #12151c;
  --bg-panel: #181c25;
  --bg-elev: #1e232e;
  --bg-input: #131720;
  --border: #2a3040;
  --border-strong: #3a4155;
  --text: #e6e9f0;
  --text-dim: #9aa4bb;
  --text-faint: #6d7791;
  --accent: #5b8cff;
  --accent-soft: #1d2740;
  --danger: #ff6b6b;
  --ok: #2fd18b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

/* 必须放在带 display 的类选择器之后：hidden 属性要能压过 .item-body{display:flex} 之类的规则 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei",
        "PingFang SC", "Hiragino Sans GB", sans-serif;
  -webkit-font-smoothing: antialiased;
}

#app {
  display: grid;
  grid-template-rows: 52px 1fr 26px;
  height: 100vh;
}

.mono { font-family: var(--mono); }

/* ---------------- 顶栏 ---------------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 650;
  letter-spacing: .3px;
  flex: 0 0 auto;
}

.brand .logo {
  display: grid;
  place-items: center;
  width: 26px; height: 26px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), #7aa2ff);
  color: #fff;
  font-style: italic;
  font-weight: 700;
}

.tools { display: flex; align-items: center; gap: 6px; flex: 1 0 auto; }

.sep { width: 1px; height: 22px; background: var(--border); flex: 0 0 auto; }

.btn {
  appearance: none;
  border: 1px solid var(--border-strong);
  background: var(--bg-elev);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  transition: background .12s, border-color .12s, transform .06s;
}

.btn:hover { background: var(--accent-soft); border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.btn.primary:hover { background: var(--accent); color: #fff; }
.btn.icon { padding: 6px 9px; min-width: 32px; }
.mini {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  border-radius: 6px;
  padding: 2px 7px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}
.mini:hover { color: var(--accent); border-color: var(--accent); }
.tools a.btn { text-decoration: none; }
.analysis-form { padding: 10px 14px 14px; display: grid; gap: 9px; }
.analysis-form label { display: grid; gap: 3px; color: var(--text-dim); font-size: 12px; }
.analysis-form input, .analysis-form select { width: 100%; padding: 6px; border: 1px solid var(--border-strong); border-radius: 6px; background: var(--bg-input); color: var(--text); font: 13px var(--mono); }
.analysis-result { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 12px; line-height: 1.55; color: var(--text-dim); }
#scene-dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--bg-panel); color: var(--text); min-width: min(460px, 90vw); max-height: 75vh; overflow: auto; box-shadow: var(--shadow); }
#scene-dialog::backdrop { background: #14213a88; }
.scene-row { display: flex; align-items: center; gap: 7px; padding: 7px 0; border-bottom: 1px solid var(--border); }
.scene-row strong { flex: 1; }

/* 下拉菜单 */
.dropdown { position: relative; }
.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 260px;
  max-height: 70vh;
  overflow: auto;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  display: none;
  z-index: 40;
}
.dropdown.open .dropdown-menu { display: block; }
.dropdown-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 7px 10px;
  border-radius: 7px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
}
.dropdown-item:hover { background: var(--accent-soft); }
.dropdown-item small { display: block; color: var(--text-faint); font-size: 12px; }

/* ---------------- 三栏布局 ---------------- */
#body {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 320px;
  min-height: 0;
}

#sidebar, #inspector {
  background: var(--bg-panel);
  overflow-y: auto;
  overflow-x: hidden;
  min-width: 0;
}
#sidebar { border-right: 1px solid var(--border); }
#inspector { border-left: 1px solid var(--border); }

.panel { border-bottom: 1px solid var(--border); padding: 10px 12px 14px; }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.panel-head h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-faint);
  margin: 0;
  font-weight: 700;
}
.head-actions { display: flex; gap: 6px; align-items: center; }

/* ---------------- 曲线条目 ---------------- */
.item-list { display: flex; flex-direction: column; gap: 8px; }

.item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-input);
  padding: 7px 8px;
  transition: border-color .12s;
}
.item.selected { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.item.hidden-item { opacity: .48; }
.item.error-item { border-color: var(--danger); }

.item-head { display: flex; align-items: center; gap: 6px; }

.swatch {
  flex: 0 0 auto;
  width: 16px; height: 16px;
  border-radius: 5px;
  border: 1px solid rgba(0, 0, 0, .18);
  cursor: pointer;
  padding: 0;
}

.item input.expr {
  flex: 1 1 auto;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13.5px;
  padding: 3px 2px;
  outline: none;
}
.item input.expr::placeholder { color: var(--text-faint); }

.iconbtn {
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  padding: 3px 4px;
  border-radius: 6px;
  font-family: inherit;
}
.iconbtn:hover { color: var(--accent); background: var(--accent-soft); }
.iconbtn.danger:hover { color: var(--danger); background: rgba(229, 72, 77, .12); }

.item-type {
  flex: 0 0 auto;
  font-size: 11px;
  color: var(--text-dim);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 4px;
  font-family: var(--mono);
}

.item-body {
  margin-top: 7px;
  padding-top: 7px;
  border-top: 1px dashed var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field { display: flex; align-items: center; gap: 6px; }
.field > label { flex: 0 0 auto; color: var(--text-dim); font-size: 12.5px; font-family: var(--mono); }
.field input[type="text"], .field input[type="number"] {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 4px 6px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  outline: none;
}
.field input:focus { border-color: var(--accent); }
.field .row { display: flex; gap: 6px; align-items: center; }

.item-error {
  margin-top: 6px;
  color: var(--danger);
  font-size: 12.5px;
  font-family: var(--mono);
  word-break: break-all;
}
.item-hint { margin-top: 6px; color: var(--text-faint); font-size: 12px; }

/* ---------------- 参数滑块 ---------------- */
.param-list { display: flex; flex-direction: column; gap: 9px; }
.param { display: grid; grid-template-columns: 24px 1fr 62px 24px; gap: 7px; align-items: center; }
.param .name {
  font-family: var(--mono);
  font-style: italic;
  color: var(--accent);
  font-weight: 700;
  cursor: pointer;
  text-align: center;
}
.param .name:hover { text-decoration: underline; }
.param input[type="range"] { width: 100%; accent-color: var(--accent); }
.param input[type="number"] {
  width: 100%;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12.5px;
  padding: 2px 4px;
  outline: none;
}
.param .sweep { font-size: 12px; }
.param .sweep.on { color: var(--accent); background: var(--accent-soft); }
.param .item-hint {
  grid-column: 2 / 5;
  display: flex;
  gap: 6px;
  align-items: center;
  color: var(--text-faint);
  font-size: 12px;
  white-space: nowrap;
}
.param .item-hint input { width: 62px; }
.param-tip { grid-column: 1 / -1; white-space: normal; line-height: 1.4; }

/* ---------------- LaTeX 弹窗 ---------------- */
.latex-box { width: min(860px, 100%); }
.latex-target { color: var(--text-dim); font-size: 13px; margin-bottom: 8px; }
#latex-input {
  width: 100%;
  min-height: 74px;
  resize: vertical;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.6;
  padding: 8px 10px;
  outline: none;
}
#latex-input:focus { border-color: var(--accent); }
.latex-examples { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chip {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.latex-preview {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-input);
  padding: 10px 12px;
  min-height: 66px;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.latex-preview code { word-break: break-all; }
.latex-ok { color: var(--ok); font-weight: 600; }
.latex-bad { color: var(--danger); font-weight: 600; }
.latex-out { font-family: var(--mono); font-size: 12.5px; }
.latex-extra { color: var(--text-dim); font-size: 12.5px; }
.latex-warn { color: #d08700; font-size: 12.5px; }
.latex-caret {
  margin: 0;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-faint);
  white-space: pre;
  overflow-x: auto;
}
.latex-actions { display: flex; gap: 8px; align-items: center; margin-top: 12px; flex-wrap: wrap; }
.btn.latex { border-color: #8b5cf6; color: #8b5cf6; }
.btn.latex:hover { background: #8b5cf6; color: #fff; }

.mini-select {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text);
  padding: 3px 5px;
  font-size: 12.5px;
  font-family: inherit;
}

/* ---------------- 画布区 ---------------- */
#stage { position: relative; min-width: 0; background: var(--bg-panel); }

#board { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
#board.dragging { cursor: grabbing; }

.hud {
  position: absolute;
  left: 12px; top: 10px;
  pointer-events: none;
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--text-dim);
  background: color-mix(in srgb, var(--bg-panel) 88%, transparent);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  opacity: 0;
  transition: opacity .15s;
  white-space: pre;
}
.hud.show { opacity: 1; }

.view-controls {
  position: absolute;
  right: 12px; bottom: 44px;
  display: flex; flex-direction: column; gap: 6px;
}
.round {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--bg-elev);
  color: var(--text);
  cursor: pointer;
  font-size: 15px;
  font-family: inherit;
  box-shadow: var(--shadow);
}
.round:hover { border-color: var(--accent); color: var(--accent); }

.hint {
  position: absolute;
  bottom: 10px; left: 50%;
  transform: translateX(-50%);
  color: var(--text-faint);
  font-size: 12px;
  pointer-events: none;
}

/* ---------------- 交点 / 性质 ---------------- */
.intersection-list { display: flex; flex-direction: column; gap: 4px; }
.ix-item {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--mono);
  font-size: 13px;
  padding: 4px 6px;
  border-radius: 7px;
  background: var(--bg-input);
  border: 1px solid transparent;
}
.ix-item:hover { border-color: var(--accent); }
.ix-item .ix-idx { color: var(--text-faint); font-size: 12px; }
.ix-item .ix-pair { color: var(--text-dim); font-size: 12px; margin-left: auto; }

.empty-note { color: var(--text-faint); font-size: 12.5px; padding: 4px 0; }

.conic-info { display: flex; flex-direction: column; gap: 8px; }
.conic-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 9px; background: var(--bg-input); }
.conic-card h3 {
  margin: 0 0 6px;
  font-size: 13.5px;
  display: flex; align-items: center; gap: 6px;
}
.conic-card h3 .dot { width: 10px; height: 10px; border-radius: 50%; }
.conic-card .kind { color: var(--text-dim); font-weight: 400; font-size: 12.5px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: 12.5px; }
.facts dt { color: var(--text-dim); white-space: nowrap; }
.facts dd { margin: 0; font-family: var(--mono); word-break: break-all; }

.settings { display: flex; flex-wrap: wrap; gap: 10px 14px; }
.switch { display: inline-flex; align-items: center; gap: 5px; font-size: 12.5px; color: var(--text-dim); cursor: pointer; }
.switch input { accent-color: var(--accent); }

/* ---------------- 状态栏 ---------------- */
#statusbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 0 12px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12px;
}

/* ---------------- 弹窗 ---------------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(10, 14, 22, .5);
  display: grid; place-items: center;
  z-index: 100;
  padding: 20px;
}
.modal-box {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  width: min(760px, 100%);
  max-height: 86vh;
  display: flex; flex-direction: column;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 14px 18px 20px; overflow: auto; }
.modal-body h3 { font-size: 14px; margin: 18px 0 6px; }
.modal-body h3:first-child { margin-top: 0; }
.modal-body code, .modal-body kbd {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
  font-family: var(--mono);
  font-size: 12.5px;
}
.modal-body table { border-collapse: collapse; width: 100%; font-size: 13px; }
.modal-body th, .modal-body td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; }
.modal-body th { background: var(--bg-input); font-weight: 600; }
.modal-body ul { padding-left: 20px; }

/* ---------------- 窄屏 ---------------- */
@media (max-width: 1180px) {
  #body { grid-template-columns: 280px minmax(0, 1fr); }
  #inspector { display: none; }
}
@media (max-width: 760px) {
  #body { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  #sidebar { max-height: 42vh; border-right: 0; border-bottom: 1px solid var(--border); }
  .brand-text { display: none; }
}
