:root {
  --bg: #faf9f6;
  --surface: #ffffff;
  --surface-2: #f3f0e9;
  --fg: #2b2a27;
  --muted: #78736a;
  --border: #e4dfd5;
  --accent: #8a5a2b;
  --accent-bg: #f6ede2;
  --primary: #3b5d47;
  --primary-fg: #ffffff;
  --danger: #b4423b;
  --danger-bg: #fceeed;
  --add-bg: #e8f5eb;
  --add-fg: #1d5c32;
  --del-bg: #fbeaea;
  --del-fg: #8c2420;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  --font-serif: "Noto Serif SC", "Source Han Serif SC", "Songti SC", SimSun, serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151518;
    --surface: #1d1d22;
    --surface-2: #25252b;
    --fg: #dcd9d1;
    --muted: #8f8b83;
    --border: #32323a;
    --accent: #d0a16f;
    --accent-bg: #2e251c;
    --primary: #5b876a;
    --primary-fg: #ffffff;
    --danger: #e06c64;
    --danger-bg: #362020;
    --add-bg: #1b3123;
    --add-fg: #94d8aa;
    --del-bg: #381e1e;
    --del-fg: #f09c99;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--fg); font-family: var(--font-sans); font-size: 14px; }
body { display: flex; flex-direction: column; overflow: hidden; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 12px; }
.hint { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 0 0 8px; }
.full-width { width: 100%; }

/* 顶栏 */
.topbar {
  height: 48px; flex: 0 0 48px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px; background: var(--surface); border-bottom: 1px solid var(--border); gap: 10px;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand { font-family: var(--font-serif); font-weight: 700; font-size: 15px; color: var(--fg); }
.brand-sub { font-weight: 600; color: var(--accent); font-size: 13px; }
.sep { color: var(--border); }

.toggle-public { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); cursor: pointer; }
.pill {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--fg);
  border-radius: 999px; padding: 4px 10px; font-size: 12px; cursor: pointer; white-space: nowrap;
}
.pill:hover { border-color: var(--accent); }
.pill-usage { color: var(--accent); background: var(--accent-bg); border-color: transparent; }

/* 按钮与表单 */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--fg);
  border-radius: 6px; padding: 6px 11px; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-sm { padding: 4px 9px; font-size: 12px; }
.btn-xs { padding: 3px 7px; font-size: 12px; }
.btn-primary { background: var(--primary); color: var(--primary-fg); border-color: var(--primary); }
.btn-accent { background: var(--accent-bg); color: var(--accent); border-color: var(--accent); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger-ghost { color: var(--danger); border-color: transparent; background: transparent; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

input[type="text"], input[type="number"], input[type="password"], input[type="url"], select, textarea {
  width: 100%; background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 8px; font-family: inherit; font-size: 13px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-bg); border-color: var(--accent); }
.select-sm { width: auto; max-width: 200px; padding: 4px 8px; font-size: 13px; }
.select-xs { width: auto; padding: 3px 6px; font-size: 12px; }
.mono-sm { font-family: var(--font-mono); font-size: 12px; line-height: 1.45; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.field > span { font-size: 12px; color: var(--muted); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.banner { padding: 7px 14px; font-size: 12px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); }
.banner-warn { background: var(--accent-bg); color: var(--accent); }

/* 三栏主布局 */
.workspace {
  flex: 1; min-height: 0; display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 310px;
}
.panel { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--border); }
.panel-right { border-right: 0; border-left: 1px solid var(--border); padding: 10px 12px; overflow-y: auto; gap: 12px; }
.panel-center { background: var(--bg); border-right: 0; }
.panel-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.panel-header h3 { margin: 0; font-size: 13px; font-weight: 600; }

/* 左栏 */
.panel-left { overflow: hidden; }
.chapters-section { flex: 0 0 42%; display: flex; flex-direction: column; min-height: 0; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.chapter-list { list-style: none; padding: 0; margin: 0; overflow-y: auto; flex: 1; }
.chapter-item {
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  padding: 6px 8px; border-radius: 6px; cursor: pointer; margin-bottom: 2px;
}
.chapter-item:hover { background: var(--surface-2); }
.chapter-item.active { background: var(--accent-bg); color: var(--accent); font-weight: 600; }
.chapter-item .ch-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.chapter-item .ch-meta { font-size: 11px; color: var(--muted); white-space: nowrap; }
.status-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 5px; }
.st-outline { background: var(--muted); }
.st-draft { background: var(--accent); }
.st-final { background: var(--primary); }

.memory-section { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 8px 12px; }
.tabs-sm { display: flex; gap: 4px; margin-bottom: 8px; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.tab-btn { border: 0; background: transparent; color: var(--muted); font-size: 12px; padding: 4px 8px; border-radius: 5px; cursor: pointer; }
.tab-btn.active { background: var(--surface-2); color: var(--fg); font-weight: 600; }
.mem-body { flex: 1; overflow-y: auto; padding-right: 2px; }
.recaps-list { display: flex; flex-direction: column; gap: 8px; }
.recap-card { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 8px; font-size: 12px; line-height: 1.5; }
.recap-card strong { color: var(--accent); }

/* 中栏 */
.empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.editor-container { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chapter-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.chapter-bar-left, .chapter-bar-right { display: flex; align-items: center; gap: 8px; }
.chapter-bar-left { flex: 1; min-width: 0; }
.badge { background: var(--surface-2); color: var(--muted); padding: 2px 7px; border-radius: 4px; font-size: 12px; white-space: nowrap; }
.ch-title-input { font-weight: 600; font-size: 15px; max-width: 320px; border-color: transparent; background: transparent; }
.ch-title-input:hover, .ch-title-input:focus { border-color: var(--border); background: var(--bg); }
.save-state { font-size: 12px; color: var(--muted); }
.save-state.dirty { color: var(--accent); }

.blueprint-box { background: var(--surface); border-bottom: 1px solid var(--border); padding: 6px 16px; font-size: 12px; }
.blueprint-box summary { cursor: pointer; color: var(--muted); user-select: none; }
.blueprint-grid { margin-top: 8px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.blueprint-grid .field:first-child, .blueprint-grid .field:last-child { grid-column: 1 / -1; }

.view-tabs {
  display: flex; gap: 6px; padding: 6px 16px;
  background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.view-tab {
  border: 0; background: transparent; color: var(--muted); padding: 4px 10px;
  border-radius: 5px; font-size: 12px; cursor: pointer; position: relative;
}
.view-tab.active { background: var(--surface); color: var(--fg); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); margin-left: 4px; vertical-align: middle; }

.stream-panel {
  margin: 10px 16px 0; border: 1px solid var(--accent); border-radius: 8px;
  background: var(--surface); display: flex; flex-direction: column; max-height: 45%;
}
.stream-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; background: var(--accent-bg); border-bottom: 1px solid var(--border); font-size: 12px; font-weight: 600;
}
.stream-actions { display: flex; align-items: center; gap: 6px; }
.stream-preview {
  margin: 0; padding: 12px 16px; overflow-y: auto; white-space: pre-wrap;
  font-family: var(--font-serif); font-size: 15px; line-height: 1.85;
}

.view-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.prose-editor {
  flex: 1; width: 100%; border: 0; border-radius: 0; resize: none;
  padding: 20px clamp(16px, 6vw, 72px); background: var(--bg); color: var(--fg);
  font-family: var(--font-serif); font-size: 17px; line-height: 1.95; outline: none;
}
.prose-editor:focus { outline: none; }

/* Diff */
.diff-toolbar, .versions-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.diff-container { flex: 1; overflow-y: auto; padding: 12px 16px; font-family: var(--font-serif); font-size: 15px; line-height: 1.8; }
.diff-line { padding: 2px 8px; border-radius: 3px; white-space: pre-wrap; word-break: break-word; }
.diff-same { color: var(--muted); }
.diff-add { background: var(--add-bg); color: var(--add-fg); }
.diff-del { background: var(--del-bg); color: var(--del-fg); text-decoration: line-through; }

/* 版本列表 */
.versions-list { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.version-card {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
}
.version-info { display: flex; flex-direction: column; gap: 3px; }
.version-top { display: flex; align-items: center; gap: 8px; }
.src-tag { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--accent); }

/* 右栏 */
.director-group { border: 1px solid var(--border); border-radius: 8px; padding: 10px; background: var(--bg); }
.group-title { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 8px; display: block; }
.action-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.recap-box { margin-top: 8px; padding: 8px; border-radius: 6px; background: var(--surface); border: 1px solid var(--border); font-size: 12px; line-height: 1.5; }
.findings-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; max-height: 300px; overflow-y: auto; }
.finding-card { background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 8px; font-size: 12px; line-height: 1.5; }
.finding-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.finding-actions { display: flex; gap: 4px; margin-top: 6px; }
.finding-card.accepted { border-color: var(--primary); }
.finding-card.ignored { opacity: 0.55; }

/* 弹窗 */
.modal { border: 0; border-radius: 10px; padding: 0; background: var(--surface); color: var(--fg); max-width: 480px; width: 92vw; box-shadow: 0 18px 40px rgba(0,0,0,0.28); }
.modal::backdrop { background: rgba(0, 0, 0, 0.45); }
.modal-lg { max-width: 780px; }
.modal-card { padding: 18px 20px; display: flex; flex-direction: column; gap: 10px; }
.modal-card h3 { margin: 0 0 4px; font-size: 16px; }
.modal-card h4 { margin: 8px 0 4px; font-size: 13px; color: var(--muted); }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.radio-row { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.radio-row input { width: auto; }
.byok-fields { border: 1px solid var(--border); border-radius: 8px; padding: 10px; background: var(--bg); }
.preset-row { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }

/* 用量看板 */
.usage-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.u-card { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px; }
.u-card .u-label { font-size: 11px; color: var(--muted); }
.u-card .u-val { font-size: 17px; font-weight: 700; margin: 2px 0; }
.budget-bar-box { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 6px; }
.budget-row { display: flex; justify-content: space-between; font-size: 12px; }
.progress { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress-fill { height: 100%; background: var(--primary); width: 0%; transition: width 0.2s; }
.progress-fill.warn { background: var(--accent); }
.progress-fill.danger { background: var(--danger); }
.budget-edit { display: flex; align-items: center; gap: 8px; font-size: 12px; margin-top: 2px; }
.budget-edit input { width: 130px; }
.usage-tables { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tbl-scroll { max-height: 200px; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; }
.tbl { width: 100%; border-collapse: collapse; font-size: 12px; }
.tbl th, .tbl td { padding: 5px 7px; border-bottom: 1px solid var(--border); text-align: left; }
.tbl th { color: var(--muted); font-weight: 600; background: var(--surface-2); }

.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: #222; color: #fff; padding: 8px 16px; border-radius: 8px; font-size: 13px;
  z-index: 999; box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}

@media (max-width: 1024px) {
  body { overflow: auto; }
  .workspace { grid-template-columns: 1fr; }
  .panel { border-right: 0; border-bottom: 1px solid var(--border); }
  .prose-editor { min-height: 55vh; }
  .usage-cards { grid-template-columns: repeat(2, 1fr); }
  .usage-tables { grid-template-columns: 1fr; }
}
