/* CoWrite - 人机协同写作平台 */
:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #e4e7ec;
  --text: #1f2328;
  --text2: #6a737d;
  --primary: #2563eb;
  --primary-h: #1d4ed8;
  --danger: #dc2626;
  --ai: #7c3aed;
  --ai-bg: #f5f0ff;
  --sidebar-w: 280px;
  --chat-w: 360px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px;
}
#app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- 顶栏 ---------- */
.topbar {
  height: 48px; background: #fff; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; padding: 0 14px; flex-shrink: 0;
}
.brand { font-weight: 700; font-size: 16px; }
.brand .sub { font-weight: 400; color: var(--text2); font-size: 12px; margin-left: 6px; }
.topbar-right { display: flex; gap: 8px; align-items: center; }
.model-select {
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px; font-size: 13px;
  background: #fff; max-width: 280px; color: var(--text);
}
.model-select option { padding: 4px; }

/* ---------- 按钮 ---------- */
.btn {
  border: 1px solid var(--border); background: #fff; color: var(--text);
  border-radius: 6px; padding: 6px 14px; font-size: 13px; cursor: pointer; transition: all .15s;
}
.btn:hover { border-color: var(--primary); color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-h); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; }
.btn.ghost:hover { border-color: var(--border); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { padding: 3px 10px; font-size: 12px; }
.iconbtn {
  border: none; background: transparent; cursor: pointer; font-size: 14px;
  padding: 3px 6px; border-radius: 4px; color: var(--text2); position: relative;
}
.iconbtn:hover { background: #f0f2f5; color: var(--text); }
.iconbtns { display: flex; gap: 2px; align-items: center; }
.badge {
  position: absolute; top: -2px; right: -4px; background: var(--ai); color: #fff;
  font-size: 10px; min-width: 14px; height: 14px; line-height: 14px; text-align: center;
  border-radius: 7px; padding: 0 3px;
}

/* ---------- 主布局 ---------- */
.main { flex: 1; display: flex; overflow: hidden; }

/* ---------- 侧栏 ---------- */
.sidebar {
  width: var(--sidebar-w); background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.sidebar-head {
  padding: 10px 12px 6px; display: flex; justify-content: space-between; align-items: center;
}
.sidebar-title { font-weight: 600; font-size: 13px; color: var(--text2); }
.searchbox { padding: 4px 10px 8px; }
.searchbox input {
  width: 100%; border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px;
  font-size: 13px; background: var(--bg);
}
.searchbox input:focus { outline: none; border-color: var(--primary); background: #fff; }
.tree { flex: 1; overflow-y: auto; padding: 4px 6px 20px; }

/* 树节点 */
.tnode { user-select: none; }
.tnode-row {
  display: flex; align-items: center; gap: 5px; padding: 4px 6px; border-radius: 5px;
  cursor: pointer; font-size: 13.5px; white-space: nowrap; overflow: visible; position: relative;
}
.tnode-row:hover { background: #f0f4f9; }
.tnode-row.active { background: #dbeafe; color: var(--primary); font-weight: 600; }
.tnode-row .twisty { width: 14px; text-align: center; color: var(--text2); font-size: 10px; flex-shrink: 0; }
.tnode-row .ticon { flex-shrink: 0; }
.tnode-row .tlabel { overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.tnode-row .tnode-menu {
  display: none; gap: 2px; flex-shrink: 0;
}
.tnode-row:hover .tnode-menu { display: flex; }
.tnode-children { margin-left: 14px; border-left: 1px dashed #e0e4ea; }

/* 搜索结果 */
.search-item {
  padding: 6px 10px; border-radius: 6px; cursor: pointer; border-bottom: 1px solid var(--bg);
}
.search-item:hover { background: #f0f4f9; }
.search-item .si-path { font-size: 12px; color: var(--text2); margin-bottom: 2px; }
.search-item .si-snip { font-size: 12px; color: var(--text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- 编辑区 ---------- */
.editor-area { flex: 1; overflow: hidden; display: flex; flex-direction: column; position: relative; background: #fff; }
.editor-placeholder { flex: 1; display: flex; align-items: center; justify-content: center; }
.ph-inner { text-align: center; color: var(--text2); }
.ph-icon { font-size: 48px; margin-bottom: 14px; }
.ph-hint { font-size: 12px; margin-top: 8px; }

/* vditor 适配 */
.editor-area .vditor { flex: 1; border-radius: 0; }
.editor-area .vditor-toolbar { border-bottom: 1px solid var(--border) !important; padding: 0 6px; }
.editor-area .vditor-reset { font-size: 15px; max-width: 860px; margin: 0 auto !important; padding: 24px 40px !important; }

/* ---------- AI 对话面板 ---------- */
.chat-panel {
  width: var(--chat-w); background: var(--panel); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.chat-panel.hidden { display: none; }
.chat-head {
  height: 40px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px; border-bottom: 1px solid var(--border); font-weight: 600; font-size: 13px;
}
.chat-msgs { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.chat-empty { color: var(--text2); text-align: center; margin-top: 40px; font-size: 13px; line-height: 2; }
.msg { display: flex; flex-direction: column; gap: 3px; max-width: 100%; }
.msg .who { font-size: 11px; color: var(--text2); display: flex; align-items: center; gap: 6px; }
.msg .body {
  padding: 8px 11px; border-radius: 10px; font-size: 13.5px; line-height: 1.65;
  word-break: break-word; white-space: pre-wrap;
}
.msg.user .body { background: var(--primary); color: #fff; align-self: flex-end; border-bottom-right-radius: 3px; }
.msg.ai .body { background: #f0f2f5; align-self: flex-start; border-bottom-left-radius: 3px; }
.msg.ai .body.md { white-space: normal; }
.msg .body pre { background: #282c34; color: #abb2bf; padding: 10px; border-radius: 6px; overflow-x: auto; font-size: 12px; }
.msg .body code { font-family: "SF Mono", Consolas, monospace; }
.msg .who .src { font-size: 10px; background: var(--ai-bg); color: var(--ai); padding: 1px 6px; border-radius: 8px; }
.msg.streaming .body::after { content: '▊'; color: var(--ai); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0; } }

.chat-input-area { padding: 10px; border-top: 1px solid var(--border); display: flex; gap: 8px; }
.chat-input-area textarea {
  flex: 1; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px;
  resize: none; height: 68px; font-family: inherit;
}
.chat-input-area textarea:focus { outline: none; border-color: var(--primary); }

/* 知识库选择器 */
.kb-selector { border-bottom: 1px solid var(--border); padding: 10px 12px; background: var(--ai-bg); }
.kb-title { font-size: 12px; color: var(--ai); font-weight: 600; margin-bottom: 6px; }
.kb-checklist { display: flex; flex-wrap: wrap; gap: 6px; }
.kb-chip {
  display: inline-flex; align-items: center; gap: 4px; border: 1px solid #d8ccf7; background: #fff;
  border-radius: 14px; padding: 3px 10px; font-size: 12px; cursor: pointer; color: var(--text);
}
.kb-chip.on { background: var(--ai); color: #fff; border-color: var(--ai); }

/* ---------- 划词工具条 ---------- */
.selection-toolbar {
  position: fixed; z-index: 1200; background: #1f2328; border-radius: 8px; padding: 4px;
  display: flex; gap: 2px; box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
.stbtn {
  border: none; background: transparent; color: #d0d7de; font-size: 12.5px; padding: 5px 9px;
  border-radius: 6px; cursor: pointer; white-space: nowrap;
}
.stbtn:hover { background: #316dca; color: #fff; }

/* ---------- 模态框 ---------- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(15,20,30,.45); z-index: 1300;
  display: flex; align-items: center; justify-content: center;
}
.modal {
  background: #fff; border-radius: 12px; width: 640px; max-width: 94vw; max-height: 88vh;
  display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal-head {
  padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 700; font-size: 15px;
  display: flex; justify-content: space-between; align-items: center;
}
.modal-body { padding: 18px; overflow-y: auto; }
.modal.wide { width: 900px; }
.modal.mid { width: 780px; }

/* revise 弹窗 */
.revise-modal { width: 720px; }
.revise-selected {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px;
  font-size: 13px; color: var(--text2); max-height: 110px; overflow-y: auto; line-height: 1.6;
  white-space: pre-wrap; margin-bottom: 10px;
}
.revise-input {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; font-size: 13.5px;
  resize: vertical; min-height: 60px; font-family: inherit; margin-bottom: 10px;
}
.revise-input:focus { outline: none; border-color: var(--ai); }
.revise-output {
  border: 1px dashed #d8ccf7; background: var(--ai-bg); border-radius: 8px; padding: 12px;
  font-size: 13.5px; line-height: 1.7; min-height: 140px; max-height: 300px; overflow-y: auto;
  white-space: pre-wrap; margin-bottom: 12px;
}
.revise-placeholder { color: #b7a8e0; }
.revise-actions { display: flex; gap: 10px; align-items: center; }
.chk { font-size: 13px; color: var(--text2); display: flex; gap: 5px; align-items: center; cursor: pointer; }
.flex1 { flex: 1; }

/* ---------- 设置页 ---------- */
.form-row { margin-bottom: 14px; }
.form-row label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: var(--text); }
.form-row .hint { font-size: 12px; color: var(--text2); font-weight: 400; margin-top: 4px; }
.input, .form-row input[type=text], .form-row input[type=password], .form-row select, .form-row textarea {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px 11px; font-size: 13.5px;
  font-family: inherit; background: #fff;
}
.input:focus, .form-row input:focus, .form-row select:focus { outline: none; border-color: var(--primary); }

.source-card, .kb-card {
  border: 1px solid var(--border); border-radius: 10px; padding: 14px; margin-bottom: 12px; background: #fafbfc;
}
.source-card .sc-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.sc-head .sc-name { font-weight: 700; font-size: 14px; flex: 1; }
.tag {
  font-size: 11px; padding: 2px 8px; border-radius: 10px; background: #eef1f4; color: var(--text2);
}
.tag.on { background: #dcfce7; color: #15803d; }
.tag.off { background: #fee2e2; color: #b91c1c; }
.model-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.model-chip {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: #fff;
  border-radius: 16px; padding: 4px 10px; font-size: 12.5px; cursor: pointer;
}
.model-chip.on { border-color: var(--primary); background: #dbeafe; color: var(--primary); font-weight: 600; }
.model-chip .rm { color: var(--text2); font-size: 11px; }
.model-chip .rm:hover { color: var(--danger); }
.inline-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------- 项目页 ---------- */
.proj-card {
  border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-bottom: 14px; background: #fff;
}
.proj-card .pc-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.pc-head .pc-name { font-weight: 700; font-size: 15px; flex: 1; }
.proj-paths { font-size: 12.5px; color: var(--text2); line-height: 1.9; margin-bottom: 8px; }
.proj-kbs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }

/* 项目执行控制台 */
.run-panel { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; margin-top: 14px; }
.run-log { max-height: 380px; overflow-y: auto; padding: 12px; background: #0d1117; color: #c9d1d9; font-size: 12.5px; font-family: "SF Mono", Consolas, monospace; line-height: 1.8; }
.run-log .rl-status { color: #58a6ff; }
.run-log .rl-file { color: #7ee787; }
.run-log .rl-err { color: #f85149; }
.run-log .rl-plan { color: #d2a8ff; }
.run-btns { display: flex; gap: 8px; padding: 10px 12px; background: #f6f8fa; border-top: 1px solid var(--border); align-items: center; }
.run-btns input { flex: 1; border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px; font-size: 13px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%); z-index: 2000;
  background: #1f2328; color: #fff; padding: 10px 20px; border-radius: 8px; font-size: 13.5px;
  box-shadow: 0 6px 24px rgba(0,0,0,.3); max-width: 80vw;
}
.toast.err { background: var(--danger); }

/* 滚动条 */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #d0d7de; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #afb8c0; }

/* 响应式：窄屏收起侧栏 */
@media (max-width: 1100px) {
  .chat-panel { position: fixed; right: 0; top: 48px; bottom: 0; z-index: 900; box-shadow: -8px 0 24px rgba(0,0,0,.12); }
}
