:root {
  --bg: #ffffff;
  --bg-soft: #fafafa;
  --bg-elev: #ffffff;
  --fg: #1a1a1a;
  --fg-soft: #6b6b6b;
  --fg-muted: #9a9a9a;
  --border: #e8e8e8;
  --border-soft: #f0f0f0;
  --accent: #2d7ff9;
  --accent-hover: #1a6cd8;
  --accent-soft: #e8f0fe;
  --success: #22c55e;
  --danger: #ef4444;
  --warning: #f59e0b;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --fs-12: 12px;
  --fs-13: 13px;
  --fs-14: 14px;
  --fs-16: 16px;
  --fs-20: 20px;
  --fs-24: 24px;
  --fs-32: 32px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-6: 24px;
  --space-8: 32px;
  --space-12: 48px;
  --space-16: 64px;

  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-2: 0 4px 12px rgba(0, 0, 0, 0.08);

  --topbar-h: 56px;
  --sidebar-w: 240px;
  --ai-w: 360px;
}

.dark {
  --bg: #1a1a1a;
  --bg-soft: #222222;
  --bg-elev: #2a2a2a;
  --fg: #e8e8e8;
  --fg-soft: #a0a0a0;
  --fg-muted: #777777;
  --border: #333333;
  --border-soft: #2a2a2a;
  --accent: #4a9eff;
  --accent-hover: #5aaeff;
  --accent-soft: #1a2a4a;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow-2: 0 4px 12px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: var(--fs-14);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
}

button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

/* === Topbar === */
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 var(--space-4);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
  gap: var(--space-3);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
  flex: 1;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius-md);
  font-weight: 700;
  font-size: var(--fs-13);
  flex-shrink: 0;
}

.doc-title {
  font-size: var(--fs-14);
  font-weight: 500;
  color: var(--fg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

.tpl-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: var(--fs-13);
  color: var(--fg-soft);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.tpl-field:hover { border-color: var(--accent); background: var(--bg-soft); }
.tpl-label { font-size: var(--fs-12); color: var(--fg-soft); }
.tpl-select {
  border: none;
  background: transparent;
  color: var(--fg);
  font-size: var(--fs-13);
  font-family: inherit;
  cursor: pointer;
  outline: none;
  padding: 2px 4px;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 6px 14px;
  border-radius: var(--radius-md);
  font-size: var(--fs-13);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.btn:hover { background: var(--bg-soft); border-color: #d0d0d0; }
.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: #fff; }

.btn-icon {
  padding: 6px 8px;
  font-size: var(--fs-16);
  min-width: 32px;
}

/* === Layout === */
.layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100vh - var(--topbar-h));
}

/* === Sidebar (Document Tree) === */
.sidebar {
  width: var(--sidebar-w);
  border-right: 1px solid var(--border);
  background: var(--bg-soft);
  overflow-y: auto;
  flex-shrink: 0;
  padding: var(--space-2) 0;
}

.tree-section { margin-bottom: var(--space-2); }

.tree-section-head {
  padding: var(--space-1) var(--space-3);
  font-size: var(--fs-12);
  font-weight: 600;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.tree-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-1) var(--space-3);
  margin: 0 var(--space-1);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--fs-13);
  min-height: 32px;
  transition: background 0.1s;
}

.tree-item:hover { background: var(--border); }

.tree-item-active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 500;
}

.tree-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.tree-item-meta {
  font-size: var(--fs-12);
  color: var(--fg-muted);
  flex-shrink: 0;
  margin-left: var(--space-2);
}

.tree-empty {
  padding: var(--space-3) var(--space-3);
  font-size: var(--fs-12);
  color: var(--fg-muted);
  text-align: center;
}

.tree-action {
  padding: var(--space-1) var(--space-3);
  margin: var(--space-1);
  font-size: var(--fs-13);
  color: var(--accent);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background 0.1s;
}

.tree-action:hover { background: var(--accent-soft); }

/* === Editor Pane === */
.editor-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  min-width: 0;
}

.editor-header {
  padding: var(--space-6) var(--space-6) 0;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  flex-shrink: 0;
}

.editor-title-input {
  width: 100%;
  font-size: var(--fs-32);
  font-weight: 700;
  line-height: 1.3;
  border: none;
  outline: none;
  background: transparent;
  color: var(--fg);
  padding: 0;
}

.editor-title-input::placeholder { color: var(--fg-muted); font-weight: 500; }

.editor-meta {
  display: flex;
  gap: var(--space-3);
  margin-top: var(--space-2);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--border-soft);
}

.editor-status {
  font-size: var(--fs-12);
  color: var(--fg-muted);
}

.editor-content {
  flex: 1;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  padding: var(--space-6);
  font-size: var(--fs-16);
  line-height: 1.8;
  color: var(--fg);
  outline: none;
  overflow-y: auto;
  min-height: 200px;
}

.editor-content:empty:before {
  content: '开始写作...';
  color: var(--fg-muted);
  pointer-events: none;
}

.editor-content h1 { font-size: var(--fs-24); font-weight: 700; margin: var(--space-6) 0 var(--space-3); line-height: 1.3; }
.editor-content h2 { font-size: var(--fs-20); font-weight: 600; margin: var(--space-6) 0 var(--space-3); line-height: 1.3; }
.editor-content h3 { font-size: var(--fs-16); font-weight: 600; margin: var(--space-4) 0 var(--space-2); }
.editor-content p { margin: var(--space-2) 0; }
.editor-content strong { font-weight: 600; }
.editor-content em { font-style: italic; }
.editor-content del { text-decoration: line-through; }
.editor-content code {
  background: var(--bg-soft);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 0.9em;
  border: 1px solid var(--border-soft);
}
.editor-content a { color: var(--accent); text-decoration: underline; }
.editor-content a:hover { color: var(--accent-hover); }
.editor-content blockquote {
  margin: var(--space-3) 0;
  padding: var(--space-2) var(--space-4);
  border-left: 3px solid var(--accent);
  background: var(--bg-soft);
  border-radius: var(--radius-sm);
  color: var(--fg-soft);
}
.editor-content ul, .editor-content ol { margin: var(--space-2) 0; padding-left: var(--space-6); }
.editor-content li { margin: var(--space-1) 0; }
.editor-content figure.editor-figure {
  margin: var(--space-4) 0;
}
.editor-content figure.editor-figure img {
  max-width: 100%;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-1);
}

/* === Floating Toolbar === */
.float-toolbar {
  position: fixed;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 6px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  transition: opacity 0.15s;
}

.float-toolbar.hidden { display: none; }

.ft-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--fg-soft);
  font-size: var(--fs-14);
  cursor: pointer;
  transition: background 0.1s;
}

.ft-btn:hover { background: var(--bg-soft); color: var(--fg); }

.ft-btn-ai {
  width: auto;
  padding: 0 10px;
  font-size: var(--fs-12);
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: var(--radius-md);
  font-weight: 500;
}

.ft-btn-ai:hover { background: var(--accent); color: #fff; }

/* === AI Assistant Panel === */
.ai-panel {
  width: var(--ai-w);
  border-left: 1px solid var(--border);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: width 0.2s, opacity 0.2s;
  overflow: hidden;
}

.ai-panel.collapsed {
  width: 0;
  border-left: none;
  opacity: 0;
}

.ai-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.ai-title {
  font-size: var(--fs-13);
  font-weight: 600;
}

.ai-close {
  font-size: var(--fs-14);
  color: var(--fg-muted);
  width: 28px;
  height: 28px;
  padding: 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.chat-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  padding: var(--space-6);
}

.chat-empty-icon { font-size: 40px; margin-bottom: var(--space-3); opacity: 0.5; }
.chat-empty-text { font-size: var(--fs-14); font-weight: 600; color: var(--fg); margin-bottom: var(--space-2); }
.chat-empty-hint { font-size: var(--fs-12); color: var(--fg-muted); line-height: 1.8; }

.chat-bubble {
  padding: var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--fs-13);
  line-height: 1.6;
  max-width: 100%;
  word-wrap: break-word;
}

.chat-bubble-user {
  background: var(--accent-soft);
  color: var(--fg);
  align-self: flex-end;
}

.chat-bubble-assistant {
  background: var(--bg-soft);
  color: var(--fg);
  border: 1px solid var(--border-soft);
  align-self: flex-start;
}

.chat-image {
  margin: var(--space-2) 0;
}

.chat-image img {
  max-width: 100%;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-1);
}

.chat-input-area {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-3);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3);
  font-size: var(--fs-13);
  line-height: 1.5;
  resize: none;
  outline: none;
  background: var(--bg-soft);
  transition: border-color 0.15s;
}

.chat-input:focus { border-color: var(--accent); }

.chat-send { flex-shrink: 0; }

/* === Modal === */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.4);
  align-items: center;
  justify-content: center;
}

.modal-backdrop.is-open { display: flex; }

.modal {
  background: var(--bg-elev);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  width: 480px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-6);
  border-bottom: 1px solid var(--border);
}

.modal-title { font-size: var(--fs-16); font-weight: 600; }

.modal-close {
  font-size: var(--fs-18);
  color: var(--fg-muted);
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
}

.modal-body { padding: var(--space-6); }

/* === Toast === */
.toast-root {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 10px 20px;
  border-radius: var(--radius-md);
  font-size: var(--fs-13);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-2);
  color: var(--fg);
  transition: opacity 0.2s;
  pointer-events: auto;
  white-space: nowrap;
}

.toast.is-success { border-color: var(--success); color: var(--success); }
.toast.is-error { border-color: var(--danger); color: var(--danger); }
.toast.is-warn { border-color: var(--warning); color: var(--warning); }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  background: var(--fg-muted);
}

.status-dot.is-ok { background: var(--success); }
.status-dot.is-down { background: var(--danger); }

/* === Responsive === */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .ai-panel { display: none; }
  .editor-header { padding: var(--space-4) var(--space-3) 0; }
  .editor-content { padding: var(--space-3); }
  .topbar-right .btn:not(.btn-primary) {
    display: none;
  }
}
