:root {
  --bg-app: #f6f5f1;
  --bg-surface: #ffffff;
  --bg-subtle: #efede7;
  --bg-hover: #e5e2d9;
  --text-main: #1b1b18;
  --text-secondary: #57564f;
  --text-muted: #8a887e;
  --border: #dfdcd2;
  --border-strong: #c8c4b7;
  --accent: #1d5c4b;
  --accent-hover: #154538;
  --accent-soft: rgba(29, 92, 75, 0.1);
  --code-bg: #f3f2ed;
  --mark-bg: #fef08a;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.12);
  --font-sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", "Cascadia Code", "SFMono-Regular", Consolas, "Liberation Mono", "Microsoft YaHei", monospace;
}

[data-theme="dark"] {
  --bg-app: #121316;
  --bg-surface: #191b1f;
  --bg-subtle: #22252b;
  --bg-hover: #2c3038;
  --text-main: #eceef2;
  --text-secondary: #a6adbb;
  --text-muted: #6f7785;
  --border: #2a2e36;
  --border-strong: #3c424d;
  --accent: #34d399;
  --accent-hover: #10b981;
  --accent-soft: rgba(52, 211, 153, 0.14);
  --code-bg: #14161a;
  --mark-bg: rgba(250, 204, 21, 0.28);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 14px 36px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-app);
  color: var(--text-main);
  font-family: var(--font-sans);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* Top Header */
.topbar {
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  gap: 10px;
  z-index: 20;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-logo {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  letter-spacing: -0.5px;
}
[data-theme="dark"] .brand-logo { color: #0c1713; }

.room-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s;
  max-width: 240px;
}
.room-badge:hover { background: var(--bg-hover); }
.room-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sync-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--bg-subtle);
  white-space: nowrap;
}
.sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f59e0b;
  transition: background 0.2s;
}
.sync-dot.online { background: #10b981; box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.18); }

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* Collaborator Avatars */
.peers-bar {
  display: flex;
  align-items: center;
  gap: 5px;
}
.peer-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px 3px 5px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  cursor: pointer;
  white-space: nowrap;
}
.peer-avatar {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 32px;
  padding: 0 11px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-main);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-subtle); border-color: var(--border-strong); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
[data-theme="dark"] .btn-primary { color: #0c1713; font-weight: 600; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

/* Formatting Toolbar */
.toolbar {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  background: var(--bg-app);
  border-bottom: 1px solid var(--border);
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }

.tool-group {
  display: flex;
  align-items: center;
  gap: 3px;
  flex-shrink: 0;
}
.tool-sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 4px;
}
.tool-btn {
  height: 28px;
  min-width: 28px;
  padding: 0 7px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12.5px;
  font-weight: 600;
  font-family: var(--font-sans);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tool-btn:hover { background: var(--bg-hover); color: var(--text-main); }
.tool-btn.active { background: var(--accent-soft); color: var(--accent); }

.view-seg {
  display: inline-flex;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px;
  flex-shrink: 0;
}
.view-seg button {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  padding: 3px 9px;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
}
.view-seg button.active {
  background: var(--bg-surface);
  color: var(--text-main);
  box-shadow: var(--shadow-sm);
}

/* Main Workspace Layout */
.workspace {
  height: calc(100% - 52px - 40px - 28px);
  display: flex;
  position: relative;
  overflow: hidden;
}

.pane-editor {
  flex: 1 1 50%;
  min-width: 240px;
  position: relative;
  background: var(--bg-surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.gutter {
  width: 5px;
  background: var(--border);
  cursor: col-resize;
  flex-shrink: 0;
  transition: background 0.15s;
}
.gutter:hover, .gutter.dragging { background: var(--accent); }

.pane-preview {
  flex: 1 1 50%;
  min-width: 240px;
  background: var(--bg-app);
  overflow-y: auto;
  padding: 28px 36px 80px;
}

.workspace[data-mode="edit"] .pane-preview,
.workspace[data-mode="edit"] .gutter { display: none; }
.workspace[data-mode="preview"] .pane-editor,
.workspace[data-mode="preview"] .gutter { display: none; }
.workspace[data-mode="preview"] .pane-preview { background: var(--bg-surface); }
.workspace[data-mode="preview"] .preview-inner { max-width: 820px; margin: 0 auto; }

/* Editor + Remote Cursor Mirror Overlay */
.editor-stage {
  position: relative;
  flex: 1;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.editor-textarea,
.editor-mirror {
  margin: 0;
  padding: 24px 28px 80px;
  width: 100%;
  height: 100%;
  font-family: var(--font-mono);
  font-size: 14.5px;
  line-height: 1.72;
  letter-spacing: 0;
  tab-size: 2;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  border: none;
  outline: none;
}

.editor-textarea {
  position: absolute;
  inset: 0;
  resize: none;
  background: transparent;
  color: var(--text-main);
  caret-color: var(--accent);
  overflow-y: auto;
  z-index: 2;
}

.editor-mirror {
  position: absolute;
  inset: 0;
  color: transparent;
  pointer-events: none;
  overflow: hidden;
  z-index: 3;
}

.remote-sel {
  border-radius: 2px;
}

.remote-caret {
  position: relative;
  display: inline-block;
  width: 0;
  height: 1.2em;
  vertical-align: text-bottom;
  border-left: 2px solid var(--peer-color, #2563eb);
  margin-left: -1px;
  margin-right: -1px;
}

.remote-flag {
  position: absolute;
  bottom: 100%;
  left: -2px;
  background: var(--peer-color, #2563eb);
  color: #fff;
  font-family: var(--font-sans);
  font-size: 10.5px;
  font-weight: 600;
  line-height: 1.2;
  padding: 2px 5px;
  border-radius: 3px 3px 3px 0;
  white-space: nowrap;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  pointer-events: none;
}

/* TOC Drawer */
.toc-panel {
  width: 230px;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px 14px;
  flex-shrink: 0;
  display: none;
}
.toc-panel.open { display: block; }
.toc-header {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.toc-item {
  display: block;
  padding: 5px 8px;
  font-size: 13px;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.toc-item:hover { background: var(--bg-subtle); color: var(--text-main); }
.toc-lv-2 { padding-left: 18px; }
.toc-lv-3 { padding-left: 30px; font-size: 12.5px; }
.toc-lv-4, .toc-lv-5, .toc-lv-6 { padding-left: 40px; font-size: 12px; }

/* Status Footer */
.statusbar {
  height: 28px;
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11.5px;
  color: var(--text-muted);
}
.status-left, .status-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

/* Markdown Typography */
.preview-inner {
  line-height: 1.75;
  font-size: 15.5px;
  color: var(--text-main);
  word-break: break-word;
}
.preview-inner h1, .preview-inner h2, .preview-inner h3,
.preview-inner h4, .preview-inner h5, .preview-inner h6 {
  margin-top: 1.35em;
  margin-bottom: 0.55em;
  line-height: 1.32;
  font-weight: 700;
}
.preview-inner h1:first-child, .preview-inner h2:first-child { margin-top: 0; }
.preview-inner h1 { font-size: 1.85em; padding-bottom: 0.3em; border-bottom: 1px solid var(--border); }
.preview-inner h2 { font-size: 1.45em; padding-bottom: 0.25em; border-bottom: 1px solid var(--border); }
.preview-inner h3 { font-size: 1.2em; }
.preview-inner p { margin: 0 0 1em; }
.preview-inner .md-link { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
.preview-inner .md-mark { background: var(--mark-bg); color: inherit; padding: 1px 4px; border-radius: 3px; }
.preview-inner .md-hr { border: none; border-top: 1px solid var(--border); margin: 1.8em 0; }
.preview-inner .md-img { max-width: 100%; border-radius: 8px; border: 1px solid var(--border); margin: 8px 0; }

.md-code-inline {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  padding: 1px 5px;
  border-radius: 4px;
}

.md-code-wrap {
  margin: 1em 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--code-bg);
}
.md-code-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 12px;
  background: var(--bg-subtle);
  border-bottom: 1px solid var(--border);
  font-size: 11.5px;
  color: var(--text-muted);
}
.md-code-lang { font-family: var(--font-mono); text-transform: uppercase; font-weight: 600; }
.md-copy-btn {
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-secondary);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 11px;
  cursor: pointer;
}
.md-copy-btn:hover { color: var(--text-main); border-color: var(--border-strong); }
.md-pre {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13.5px;
  line-height: 1.6;
}
.hl-keyword { color: #b4236c; font-weight: 600; }
.hl-string { color: #166534; }
.hl-comment { color: #8a887e; font-style: italic; }
.hl-number { color: #b45309; }
[data-theme="dark"] .hl-keyword { color: #f472b6; }
[data-theme="dark"] .hl-string { color: #86efac; }
[data-theme="dark"] .hl-number { color: #fcd34d; }

.md-quote {
  margin: 1em 0;
  padding: 8px 16px;
  border-left: 4px solid var(--accent);
  background: var(--bg-surface);
  border-radius: 0 6px 6px 0;
  color: var(--text-secondary);
}
.md-quote p:last-child { margin-bottom: 0; }

.md-alert {
  margin: 1em 0;
  padding: 12px 16px;
  border-left: 4px solid #3b82f6;
  background: var(--bg-surface);
  border-radius: 0 8px 8px 0;
  box-shadow: var(--shadow-sm);
}
.md-alert-title { font-weight: 700; font-size: 13.5px; margin-bottom: 4px; }
.md-alert-body p:last-child { margin-bottom: 0; }
.md-alert-note { border-left-color: #3b82f6; }
.md-alert-note .md-alert-title { color: #3b82f6; }
.md-alert-tip { border-left-color: #10b981; }
.md-alert-tip .md-alert-title { color: #10b981; }
.md-alert-important { border-left-color: #8b5cf6; }
.md-alert-important .md-alert-title { color: #8b5cf6; }
.md-alert-warning { border-left-color: #f59e0b; }
.md-alert-warning .md-alert-title { color: #d97706; }
.md-alert-caution { border-left-color: #ef4444; }
.md-alert-caution .md-alert-title { color: #ef4444; }

.md-table-wrap { overflow-x: auto; margin: 1.1em 0; }
.md-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-surface);
  font-size: 14.5px;
}
.md-table th, .md-table td {
  border: 1px solid var(--border);
  padding: 8px 12px;
}
.md-table th { background: var(--bg-subtle); font-weight: 600; }

.md-list { padding-left: 22px; margin: 0.6em 0 1em; }
.md-list li { margin: 4px 0; }
.md-task-list { list-style: none; padding-left: 4px; }
.md-task-label { display: inline-flex; align-items: flex-start; gap: 8px; cursor: pointer; }
.md-task-checkbox { margin-top: 5px; width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.md-task-done span { text-decoration: line-through; color: var(--text-muted); }

/* Math Styling */
.math-inline { font-family: "Cambria Math", "Times New Roman", serif; font-size: 1.06em; padding: 0 2px; }
.math-block {
  font-family: "Cambria Math", "Times New Roman", serif;
  font-size: 1.2em;
  text-align: center;
  padding: 14px;
  margin: 1em 0;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-x: auto;
}
.math-frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; padding: 0 3px; font-size: 0.9em; }
.math-num { border-bottom: 1px solid currentColor; padding: 0 3px; line-height: 1.15; }
.math-den { padding: 0 3px; line-height: 1.15; }
.math-radicand { border-top: 1px solid currentColor; padding: 0 2px; }
.math-rm { font-family: var(--font-sans); font-style: normal; }

/* Modals & Toast */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal-backdrop.open { display: flex; }
.modal-card {
  width: 100%;
  max-width: 480px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 20px;
  max-height: 85vh;
  overflow-y: auto;
}
.modal-title { font-size: 16px; font-weight: 700; margin: 0 0 12px; display: flex; justify-content: space-between; align-items: center; }
.field-label { display: block; font-size: 12.5px; color: var(--text-secondary); margin: 12px 0 5px; font-weight: 600; }
.field-input {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--bg-app);
  color: var(--text-main);
  font-size: 13.5px;
}
.color-swatches { display: flex; gap: 8px; margin-top: 6px; flex-wrap: wrap; }
.swatch {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid transparent; cursor: pointer;
}
.swatch.selected { border-color: var(--text-main); transform: scale(1.12); }

.list-box { margin-top: 10px; border: 1px solid var(--border); border-radius: 8px; max-height: 220px; overflow-y: auto; }
.list-row {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  cursor: pointer;
}
.list-row:last-child { border-bottom: none; }
.list-row:hover { background: var(--bg-subtle); }

.toast {
  position: fixed;
  bottom: 42px;
  left: 50%;
  transform: translateX(-50%) translateY(14px);
  background: #18181b;
  color: #fff;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
  box-shadow: var(--shadow-lg);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 820px) {
  .hide-mobile { display: none !important; }
  .pane-preview { padding: 18px 18px 70px; }
  .editor-textarea, .editor-mirror { padding: 18px 16px 70px; font-size: 14px; }
  .toc-panel.open { position: absolute; right: 0; top: 0; bottom: 0; z-index: 30; box-shadow: var(--shadow-lg); }
}

@media print {
  .topbar, .toolbar, .statusbar, .pane-editor, .gutter, .toc-panel { display: none !important; }
  html, body, .workspace, .pane-preview { height: auto !important; overflow: visible !important; background: #fff !important; color: #000 !important; }
  .pane-preview { display: block !important; padding: 0 !important; }
}
