/* ============================================================
   第一性原理引擎 · Codex Agent 式 UI（双主题：深色 / 浅色）
   黑白灰为主 + 单一蓝色强调；左侧工作空间栏 + 右侧主推导区
   ============================================================ */

/* ---------- 主题变量 ---------- */
:root {
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  --ws-w: 312px;
}

:root,
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e0e10;
  --bg-main: #131316;
  --bg-sidebar: #0a0a0c;
  --surface: #1a1a1e;
  --surface-2: #222227;
  --surface-3: #2a2a30;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.16);
  --text: #ececef;
  --text-dim: #9d9da6;
  --text-faint: #63636c;
  --accent: #4d8dff;
  --accent-strong: #3b82f6;
  --accent-soft: rgba(77, 141, 255, 0.12);
  --accent-border: rgba(77, 141, 255, 0.45);
  --ok: #4ade80;
  --danger: #f87171;
  --amber: #d9a13a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 20px rgba(0, 0, 0, 0.18);
  --code-bg: rgba(0, 0, 0, 0.35);
  --input-bg: rgba(0, 0, 0, 0.28);
  /* 图谱灰阶（深色主题：白→灰） */
  --tree-c0: #f2f2f4; --tree-c1: #c9c9d0; --tree-c2: #a5a5ad; --tree-c3: #83838c;
  --tree-c4: #6a6a73; --tree-c5: #585860; --tree-c6: #4a4a51; --tree-c7: #3d3d44;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f4f5;
  --bg-main: #ffffff;
  --bg-sidebar: #ececee;
  --surface: #ffffff;
  --surface-2: #f6f6f7;
  --surface-3: #eaeaec;
  --border: rgba(0, 0, 0, 0.09);
  --border-strong: rgba(0, 0, 0, 0.18);
  --text: #17171a;
  --text-dim: #5c5c63;
  --text-faint: #9a9aa1;
  --accent: #2f6fdb;
  --accent-strong: #2563eb;
  --accent-soft: rgba(47, 111, 219, 0.09);
  --accent-border: rgba(47, 111, 219, 0.4);
  --ok: #16a34a;
  --danger: #dc2626;
  --amber: #b45309;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 6px 18px rgba(0, 0, 0, 0.06);
  --code-bg: rgba(0, 0, 0, 0.045);
  --input-bg: rgba(0, 0, 0, 0.03);
  /* 图谱灰阶（浅色主题：黑→灰） */
  --tree-c0: #1a1a1e; --tree-c1: #3c3c42; --tree-c2: #57575e; --tree-c3: #707078;
  --tree-c4: #86868e; --tree-c5: #9999a0; --tree-c6: #a9a9b0; --tree-c7: #b6b6bc;
}

/* ---------- 基础 ---------- */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  transition: background 0.25s ease, color 0.25s ease;
}

/* ============================================================
   布局
   ============================================================ */
.app {
  position: relative;
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---------- 侧栏 ---------- */
.workspace {
  width: var(--ws-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  transition: margin-left 0.25s ease, background 0.25s ease;
  z-index: 30;
}
.workspace.hidden { margin-left: calc(-1 * var(--ws-w)); }

.ws-head { padding: calc(16px + env(safe-area-inset-top)) 15px 13px; border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 13px; }
.brand-mark {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  font-size: 17px;
  border-radius: 9px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  flex-shrink: 0;
}
.brand h1 { font-size: 14.5px; font-weight: 650; letter-spacing: 0.2px; }
.brand p { font-size: 10px; color: var(--text-faint); margin-top: 1px; font-family: var(--mono); }

.ws-scroll {
  flex: 1; overflow-y: auto;
  padding: 13px 13px 10px;
  display: flex; flex-direction: column; gap: 18px;
}
.ws-scroll::-webkit-scrollbar { width: 8px; }
.ws-scroll::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 4px; }
.ws-section h3.ws-title {
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 1.1px;
  color: var(--text-faint);
  text-transform: uppercase;
  margin-bottom: 8px;
}

.ws-foot { padding: 11px 13px; border-top: 1px solid var(--border); display: flex; gap: 8px; }
.ws-foot .btn { flex: 1; justify-content: center; }

.ws-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 25;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s;
}
body.ws-open .ws-backdrop { opacity: 1; pointer-events: auto; }

/* ---------- 主区 ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; background: var(--bg-main); transition: background 0.25s ease; }
.main-top {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: calc(10px + env(safe-area-inset-top)) 18px 10px; /* 顶部预留状态栏安全区（PWA standalone） */
  border-bottom: 1px solid var(--border);
  background: var(--bg-main);
  z-index: 10;
}
.main-body { flex: 1; overflow-y: auto; padding: 20px 24px 80px; max-width: 780px; width: 100%; margin: 0 auto; }
.main-body::-webkit-scrollbar { width: 9px; }
.main-body::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 5px; }



/* ============================================================
   通用组件
   ============================================================ */
.btn {
  font-family: inherit;
  font-size: 12.5px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 13px;
  cursor: pointer;
  transition: all 0.14s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.btn:hover { border-color: var(--border-strong); background: var(--surface-3); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-block { width: 100%; justify-content: center; }
.btn-primary {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  color: #fff;
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent); border-color: var(--accent); }
.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-ghost.danger:hover { color: var(--danger); border-color: var(--danger); }
.btn-small { padding: 5px 10px; font-size: 11.5px; border-radius: 7px; }

.icon-btn {
  width: 29px; height: 29px;
  display: grid; place-items: center;
  font-size: 14px;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
  transition: all 0.14s ease;
}
.icon-btn:hover { color: var(--text); background: var(--surface-2); border-color: var(--border); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.health-badge {
  font-size: 11px;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-faint);
  margin-left: auto;
  font-family: var(--mono);
}
.health-badge.ok { color: var(--ok); border-color: var(--border); }
.health-badge.err { color: var(--danger); border-color: var(--border); }

/* ---------- 输入区 ---------- */
.input-card { padding: 14px 16px 13px; }
.seg { display: flex; background: var(--input-bg); border: 1px solid var(--border); border-radius: 9px; padding: 3px; gap: 3px; }
.seg-btn {
  flex: 1;
  font-family: inherit;
  font-size: 12.5px;
  padding: 7px 0;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.15s ease;
}
.seg-btn.active {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}
.mode-hint { margin: 9px 2px 8px; font-size: 11.5px; color: var(--text-faint); min-height: 15px; }
#inputText {
  width: 100%;
  resize: vertical;
  font-family: inherit;
  font-size: 13.5px;
  line-height: 1.7;
  color: var(--text);
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 11px 13px;
  outline: none;
  transition: border-color 0.18s, box-shadow 0.18s;
}
#inputText:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#inputText::placeholder { color: var(--text-faint); }
.input-row { display: flex; align-items: center; justify-content: space-between; margin-top: 10px; }
.char-count { font-size: 11px; color: var(--text-faint); font-family: var(--mono); }

/* ============================================================
   主区内容
   ============================================================ */
.main-content { margin-top: 16px; display: flex; flex-direction: column; gap: 13px; }

.main-empty { text-align: center; padding: 64px 20px; color: var(--text-dim); }
.main-empty .empty-icon { font-size: 38px; margin-bottom: 12px; opacity: 0.85; }
.main-empty p { font-size: 14px; }
.main-empty .empty-sub { font-size: 11.5px; color: var(--text-faint); margin-top: 8px; }

/* ---------- 目标卡片 ---------- */
.goal-card {
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 15px 17px;
  box-shadow: var(--shadow);
  border-left: 3px solid var(--accent);
}
.goal-card .goal-tag {
  font-size: 10.5px; font-weight: 600; letter-spacing: 1px;
  color: var(--text-faint);
  text-transform: uppercase;
  display: flex; align-items: center; gap: 7px;
  margin-bottom: 7px;
}
.goal-card .goal-essence { font-size: 16px; font-weight: 650; line-height: 1.6; color: var(--text); }
.goal-card .goal-source { margin-top: 9px; }
.goal-card .goal-source summary { cursor: pointer; font-size: 11.5px; color: var(--text-faint); user-select: none; }
.goal-card .goal-source summary:hover { color: var(--text-dim); }
.goal-card .goal-source pre {
  white-space: pre-wrap; word-break: break-word;
  font-family: inherit; font-size: 12px; line-height: 1.8;
  color: var(--text-dim);
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin-top: 7px;
}

/* ---------- 节点卡片 ---------- */
.node-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 16px;
  box-shadow: var(--shadow);
  animation: cardIn 0.32s cubic-bezier(0.2, 0.8, 0.3, 1);
  position: relative;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.node-card.flash { animation: flashCard 1.5s ease; }
.node-card.actionable {
  border-color: var(--accent-border);
  box-shadow: 0 0 0 1px var(--accent-soft), 0 0 18px var(--accent-soft), var(--shadow);
}
@keyframes cardIn {
  from { opacity: 0; transform: translateY(10px) scale(0.99); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes flashCard {
  0%, 100% { box-shadow: var(--shadow); }
  25% { box-shadow: 0 0 0 3px var(--accent-soft), 0 0 24px var(--accent-soft); }
}

.node-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.depth-badge {
  font-size: 10.5px; font-weight: 650;
  padding: 2px 9px;
  border-radius: 999px;
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-family: var(--mono);
}
.mode-badge { font-size: 10.5px; color: var(--text-faint); border: 1px solid var(--border); border-radius: 999px; padding: 2px 8px; }
.action-badge {
  font-size: 10.5px; font-weight: 650;
  color: #fff;
  background: var(--accent-strong);
  border-radius: 999px;
  padding: 2px 10px;
  box-shadow: 0 0 0 1px var(--accent-border);
}
.node-time { font-size: 10.5px; color: var(--text-faint); margin-left: auto; font-family: var(--mono); }

.node-label { font-size: 14px; font-weight: 650; color: var(--text); margin-bottom: 6px; }
.node-label .origin { font-size: 11px; font-weight: 400; color: var(--text-faint); margin-left: 8px; }

.node-principle { font-size: 14px; line-height: 1.85; color: var(--text); white-space: pre-wrap; word-break: break-word; }

.guidance-tag {
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--surface-2);
  border-left: 3px solid var(--border-strong);
  border-radius: 0 8px 8px 0;
  padding: 7px 11px;
  line-height: 1.7;
}
.guidance-tag b { color: var(--text-dim); font-weight: 600; }

.node-keywords { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 10px; }
.kw {
  font-size: 10.5px;
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 2px 7px;
  font-family: var(--mono);
}
.kw::before { content: "#"; color: var(--text-faint); margin-right: 3px; }

.node-actions { display: flex; gap: 7px; margin-top: 11px; }
.action-btn {
  font-family: inherit;
  font-size: 11.5px;
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 11px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.14s ease;
}
.action-btn:hover { color: var(--text); border-color: var(--border-strong); background: var(--surface-2); }
.action-btn:disabled { opacity: 0.4; cursor: not-allowed; }

details.think-block { margin-top: 9px; }
details.think-block summary {
  cursor: pointer;
  font-size: 11.5px;
  color: var(--text-faint);
  user-select: none;
  display: inline-flex; align-items: center; gap: 6px;
  transition: color 0.14s;
}
details.think-block summary:hover { color: var(--text-dim); }
details.think-block pre {
  white-space: pre-wrap; word-break: break-word;
  font-family: inherit; font-size: 11.5px; line-height: 1.75;
  color: var(--text-faint);
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 9px 11px;
  margin-top: 7px;
  max-height: 220px;
  overflow-y: auto;
}

/* 继续推导输入区 */
.derive-box {
  margin-top: 11px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2);
  padding: 9px 11px;
}
.derive-head { display: flex; align-items: baseline; gap: 9px; flex-wrap: wrap; margin-bottom: 7px; }
.derive-title { font-size: 12px; font-weight: 650; color: var(--text); }
.derive-tip { font-size: 10.5px; color: var(--text-faint); }
.derive-input {
  width: 100%;
  resize: vertical;
  font-family: inherit;
  font-size: 12px;
  line-height: 1.7;
  color: var(--text);
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 10px;
  outline: none;
  transition: border-color 0.16s, box-shadow 0.16s;
}
.derive-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.derive-input::placeholder { color: var(--text-faint); }
.derive-actions { display: flex; gap: 8px; margin-top: 8px; }
.derive-actions .action-btn { width: 100%; justify-content: center; background: var(--surface); }

/* 向下箭头 */
.chain-arrow {
  display: flex; flex-direction: column; align-items: center;
  color: var(--text-faint);
  margin: 5px 0 1px;
  gap: 1px;
  font-size: 11px;
  user-select: none;
}
.chain-arrow .line { width: 2px; height: 22px; background: linear-gradient(180deg, var(--border-strong), var(--accent)); border-radius: 2px; opacity: 0.8; }
.chain-arrow .arr { font-size: 11px; color: var(--accent); line-height: 1; }

/* 加载骨架卡 */
.node-card.loading .skeleton {
  border-radius: 6px;
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
.loading-line { display: flex; align-items: center; gap: 9px; margin-top: 4px; }
.loading-status { font-size: 12px; color: var(--text-dim); display: flex; align-items: center; gap: 9px; }
.spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--surface-3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* 错误卡 */
.node-card.error { border-color: var(--danger); }
.error-msg { color: var(--danger); font-size: 12.5px; line-height: 1.7; word-break: break-word; }

/* 收起占位 */
.chain-collapsed {
  text-align: center;
  padding: 20px;
  color: var(--text-faint);
  font-size: 12px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.16s;
}
.chain-collapsed:hover { color: var(--text-dim); border-color: var(--accent); }

/* ---------- 底部操作条 ---------- */
.chain-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  align-items: center;
}
.chain-actions .btn { background: var(--surface-2); }

/* ---------- 总结卡片 ---------- */
.summary-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 15px 17px;
  box-shadow: var(--shadow);
  border-left: 3px solid var(--border-strong);
}
.summary-card .summary-head { display: flex; align-items: center; gap: 9px; margin-bottom: 10px; }
.summary-card .summary-head h3 { font-size: 13.5px; font-weight: 650; }
.summary-card .summary-body { font-size: 13px; line-height: 1.85; color: var(--text); }
.summary-card .summary-body .muted { color: var(--text-faint); font-size: 11.5px; }
.summary-loading { display: flex; align-items: center; gap: 10px; color: var(--text-dim); font-size: 12.5px; padding: 6px 0; }
.summary-themes { margin-top: 12px; }
.summary-themes h4 { font-size: 11px; color: var(--text-dim); font-weight: 600; margin-bottom: 6px; }
.summary-actions { margin-top: 12px; }
.summary-actions h4 { font-size: 11px; color: var(--text-dim); font-weight: 600; margin-bottom: 6px; }
.summary-actions li { font-size: 12.5px; margin: 4px 0 4px 17px; color: var(--text); }
.summary-card .btn-small { margin-left: auto; }

/* Markdown 渲染 */
.md { font-size: 13px; line-height: 1.85; }
.md h2, .md h3, .md h4 { margin: 11px 0 5px; color: var(--text); }
.md h2 { font-size: 15.5px; } .md h3 { font-size: 14px; } .md h4 { font-size: 13px; }
.md p { margin: 5px 0; }
.md strong { color: var(--text); font-weight: 700; }
.md code {
  font-family: var(--mono);
  font-size: 11.5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
.md pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 7px 0;
}
.md pre code { background: none; border: none; padding: 0; color: var(--text-dim); }
.md ul, .md ol { margin: 5px 0 5px 19px; }
.md li { margin: 3px 0; }
.md blockquote {
  border-left: 3px solid var(--accent);
  padding: 3px 11px;
  margin: 7px 0;
  color: var(--text-dim);
  background: var(--accent-soft);
  border-radius: 0 7px 7px 0;
}
.md a { color: var(--accent); }

/* ============================================================
   侧栏组件
   ============================================================ */
.current-session {
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  background: var(--surface);
  padding: 11px 12px;
  cursor: pointer;
  transition: all 0.15s;
}
.current-session:hover { border-color: var(--accent); }
.current-session.empty { border-style: dashed; cursor: default; opacity: 0.65; text-align: center; font-size: 11.5px; color: var(--text-faint); padding: 16px 10px; }
.cs-essence { font-size: 12.5px; font-weight: 600; line-height: 1.55; color: var(--text); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.cs-meta { display: flex; align-items: center; gap: 7px; margin-top: 7px; }
.cs-depth {
  font-size: 10px; font-weight: 650;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: 999px;
  padding: 1px 8px;
  font-family: var(--mono);
}
.cs-time { font-size: 10px; color: var(--text-faint); margin-left: auto; }

.history-list { display: flex; flex-direction: column; gap: 6px; }
.history-empty { font-size: 11px; color: var(--text-faint); text-align: center; padding: 13px 0; }
.history-item {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 8px 10px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.14s ease;
  position: relative;
}
.history-item:hover { background: var(--surface); border-color: var(--border); }
.history-item.active { background: var(--accent-soft); border-color: var(--accent-border); }
.hi-essence { font-size: 11.5px; color: var(--text-dim); line-height: 1.5; flex: 1; min-width: 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.history-item.active .hi-essence { color: var(--text); }
.hi-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; flex-shrink: 0; }
.hi-depth { font-size: 9.5px; font-weight: 650; color: var(--accent); font-family: var(--mono); }
.hi-time { font-size: 9.5px; color: var(--text-faint); }
.hi-del {
  position: absolute;
  top: 5px; right: 5px;
  width: 20px; height: 20px;
  display: grid; place-items: center;
  font-size: 11px;
  border: none;
  border-radius: 5px;
  background: var(--surface-3);
  color: var(--text-faint);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.14s;
}
.history-item:hover .hi-del { opacity: 1; }
.hi-del:hover { color: var(--danger); }

/* 图谱 */
.viz-wrap {
  position: relative;
  height: 240px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  overflow: hidden;
}
#vizSvg { width: 100%; height: 100%; display: block; cursor: grab; }
#vizSvg.dragging { cursor: grabbing; }
.viz-empty {
  position: absolute; inset: 0;
  display: grid; place-content: center;
  text-align: center;
  color: var(--text-faint);
  font-size: 12px;
  line-height: 1.9;
  pointer-events: none;
}
.viz-empty span { font-size: 10.5px; }
.viz-tools { display: flex; gap: 4px; position: absolute; top: 6px; right: 6px; z-index: 2; }
.viz-tools .icon-btn { width: 22px; height: 22px; font-size: 11px; background: var(--surface-2); border-color: var(--border); }

/* 图谱 SVG 元素 */
.fp-node rect.node-box { fill: var(--surface-2); stroke-width: 1.4; transition: stroke-width 0.15s, filter 0.15s; }
.fp-node:hover rect.node-box { stroke-width: 2; filter: drop-shadow(0 0 5px var(--nc)); }
.fp-node .node-label-t { fill: var(--text); font-size: 11.5px; font-weight: 600; text-anchor: middle; }
.fp-node .node-depth-t { fill: var(--nc); font-size: 9.5px; font-weight: 700; text-anchor: middle; font-family: var(--mono); }
.fp-edge { fill: none; stroke-width: 1.5; opacity: 0.55; }

/* ============================================================
   Toast
   ============================================================ */
.toast {
  position: fixed;
  left: 50%; bottom: 26px;
  transform: translateX(-50%) translateY(18px);
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  padding: 10px 18px;
  font-size: 12.5px;
  color: var(--text);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: all 0.28s ease;
  z-index: 99;
  max-width: 80vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { border-color: var(--danger); color: var(--danger); }

/* ---------- 基础：防键盘缩放 / 触摸行为 ---------- */
html { touch-action: manipulation; } /* 消除双击缩放与双击延迟 */

/* ============================================================
   移动端适配（≤900px）
   ============================================================ */
@media (max-width: 900px) {
  .workspace { position: fixed; left: 0; top: 0; bottom: 0; margin-left: 0; transform: translateX(-100%); }
  .workspace.hidden { margin-left: 0; transform: translateX(-100%); }
  .workspace.open { transform: translateX(0); }
  body.ws-open { overflow: hidden; }
  .main-body { padding: 12px 12px calc(60px + env(safe-area-inset-bottom)); }
  .ws-head { padding-top: calc(14px + env(safe-area-inset-top)); }

  /* 输入框字号 ≥16px：iOS 聚焦时不自动缩放页面 */
  #inputText, .derive-input { font-size: 16px; }
  .input-card { padding: 12px 12px 12px; }
  .mode-hint { font-size: 12px; line-height: 1.6; }

  /* 触控目标 ≥44px */
  .seg-btn { padding: 11px 0; font-size: 14px; }
  .btn { padding: 11px 14px; font-size: 14px; }
  .btn-small { padding: 9px 12px; font-size: 13px; }
  .action-btn { padding: 9px 13px; font-size: 13px; }
  .icon-btn { width: 38px; height: 38px; font-size: 17px; }
  .sidebar-toggle, #btnTheme { width: 38px; height: 38px; }
  .derive-go, .derive-actions .action-btn { min-height: 42px; }
  .main-top { padding: calc(10px + env(safe-area-inset-top)) 12px 10px; gap: 8px; }

  /* 侧栏：全宽抽屉 + 更紧凑 */
  .workspace { width: min(86vw, 340px); }
  .brand h1 { font-size: 15px; }
  .history-item { padding: 10px 12px; }
  .hi-essence { font-size: 12.5px; }
  .current-session { padding: 12px; }

  /* 节点卡片：更紧凑 + 底部安全区 */
  .node-card { padding: 12px 13px; }
  .node-principle { font-size: 14.5px; }
  .goal-card { padding: 13px 14px; }
  .goal-card .goal-essence { font-size: 15.5px; }

  /* 操作条：不悬浮，跟随页面流放在最底部；移动端只保留「生成总结」 */
  .chain-actions {
    position: static;
    display: flex;
    flex-wrap: wrap;
    padding: 11px 13px;
    gap: 8px;
    margin-top: 2px;
  }
  .chain-actions .btn { flex-shrink: 0; }
  .chain-actions .ca-derive,
  .chain-actions .ca-redo,
  .chain-actions .ca-collapse { display: none; } /* 只留生成总结 */

  /* 图谱高度适配 */
  .viz-wrap { height: 220px; }

  /* 总结卡片 */
  .summary-card { padding: 13px 14px; }

  /* 思考流高度 */
  .think-stream { max-height: 110px; }
}

@media (max-width: 400px) {
  .goal-card .goal-essence { font-size: 15px; }
  .node-principle { font-size: 14px; }
  .derive-tip { display: none; } /* 窄屏隐藏辅助说明，减少换行 */
}

/* ============================================================
   PWA standalone：状态栏覆盖内容时强制顶部留白
   （iOS 添加到主屏幕 / 桌面安装应用时生效）
   ============================================================ */
@media (display-mode: standalone) {
  .main-top { padding-top: max(10px, env(safe-area-inset-top)); }
  .ws-head { padding-top: max(16px, env(safe-area-inset-top)); }
}
