/* ===== 基础变量 ===== */
:root {
  --bg-body: #f5f5f5;
  --bg-toolbar: #ffffff;
  --bg-editor: #fafafa;
  --bg-preview: #ffffff;
  --bg-hover: #f0f0f0;
  --border-color: #e0e0e0;
  --text-primary: #2c2c2c;
  --text-secondary: #666666;
  --text-muted: #999999;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --font-mono: 'SF Mono', 'Fira Code', 'JetBrains Mono', Monaco, Consolas, monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  --radius: 6px;
  --code-bg: #f6f8fa;
  --code-text: #24292f;
}

[data-theme="dark"] {
  --bg-body: #1a1a2e;
  --bg-toolbar: #16213e;
  --bg-editor: #1a1a2e;
  --bg-preview: #16213e;
  --bg-hover: #0f3460;
  --border-color: #2a2a4a;
  --text-primary: #e0e0e0;
  --text-secondary: #b0b0b0;
  --text-muted: #808080;
  --accent: #4cc9f0;
  --accent-hover: #90e0ef;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --code-bg: #161b22;
  --code-text: #c9d1d9;
}

/* ===== 重置与基础 ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg-body);
  color: var(--text-primary);
  overflow: hidden;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ===== 工具栏 ===== */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  height: 48px;
  background: var(--bg-toolbar);
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--shadow);
  z-index: 10;
  flex-shrink: 0;
}

.tool-dropdown {
  position: relative;
  display: inline-flex;
}

.tool-dropdown-menu {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin-top: 4px;
  min-width: 120px;
  background: var(--bg-toolbar);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 200;
  display: none;
  flex-direction: column;
  padding: 4px;
  gap: 2px;
}

.tool-dropdown:hover .tool-dropdown-menu,
.tool-dropdown:focus-within .tool-dropdown-menu {
  display: flex;
}

/* 连接桥：覆盖按钮与菜单之间的间隙，防止鼠标移动时丢失 hover */
.tool-dropdown::before {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  height: 6px;
  z-index: 199;
}

.tool-dropdown-item {
  display: block;
  width: 100%;
  padding: 6px 10px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-primary);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
}

.tool-dropdown-item:hover {
  background: var(--bg-hover);
}

.toolbar-link {
  display: inline-flex;
  align-items: center;
  padding: 0 8px;
  font-size: 13px;
  color: #22c55e;
  text-decoration: none;
  white-space: nowrap;
  transition: opacity 0.15s ease;
}

.toolbar-link:hover {
  opacity: 0.8;
  text-decoration: underline;
}

.toolbar-left, .toolbar-right {
  display: flex;
  align-items: center;
  gap: 2px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
  color: var(--text-primary);
  margin-right: 8px;
  text-decoration: none;
}

.logo:hover {
  color: var(--accent);
}

.logo svg {
  color: var(--accent);
}

.toolbar-divider {
  width: 1px;
  height: 24px;
  background: var(--border-color);
  margin: 0 6px;
}

.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
}

.tool-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.tool-btn:active {
  transform: scale(0.95);
}



/* ===== 文件侧边栏 ===== */
.file-sidebar {
  position: fixed;
  top: 48px;
  left: 0;
  bottom: 28px;
  width: 220px;
  background: var(--bg-toolbar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  z-index: 20;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
}

.file-sidebar.open {
  transform: translateX(0);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-color);
}

.sidebar-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.sidebar-close:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.file-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.file-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-primary);
  transition: background 0.15s;
  margin-bottom: 2px;
}

.file-item:hover {
  background: var(--bg-hover);
}

.file-item.active {
  background: var(--accent);
  color: white;
}

.file-item.active .file-actions svg {
  color: rgba(255,255,255,0.8);
}

.file-item.active .file-actions svg:hover {
  color: white;
}

.file-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-right: 6px;
}

.file-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.15s;
}

.file-item:hover .file-actions {
  opacity: 1;
}

.file-actions button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.file-actions button:hover {
  background: rgba(0,0,0,0.1);
  color: var(--text-primary);
}

.sidebar-footer {
  padding: 8px;
  border-top: 1px solid var(--border-color);
}

.sidebar-new-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 8px;
  border: 1px dashed var(--border-color);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}

.sidebar-new-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--bg-hover);
}

/* 侧边栏打开时编辑器偏移 */
body.sidebar-open .editor-container {
  padding-left: 220px;
}

body.sidebar-open .resizer {
  margin-left: 220px;
}

/* ===== 编辑器主体 ===== */
.editor-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

.pane {
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.editor-pane {
  flex: 1 1 50%;
  min-width: 200px;
}

.preview-pane {
  flex: 1 1 50%;
  min-width: 200px;
  background: var(--bg-preview);
}

.pane-label {
  position: absolute;
  top: 8px;
  right: 12px;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  pointer-events: none;
  opacity: 0.7;
}

/* ===== 编辑器 ===== */
.editor-wrapper {
  display: flex;
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
}

.line-numbers {
  width: 0;
  padding: 24px 0;
  border-right: 1px solid var(--border-color);
  background: var(--bg-editor);
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.7;
  color: var(--text-muted);
  text-align: right;
  overflow: hidden;
  white-space: pre;
  user-select: none;
  transition: width 0.15s ease, padding 0.15s ease;
}

.line-numbers.show {
  width: 48px;
  padding: 24px 8px 24px 4px;
}

#editor {
  flex: 1;
  height: 100%;
  padding: 24px 28px;
  border: none;
  outline: none;
  resize: none;
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.7;
  color: var(--text-primary);
  background: var(--bg-editor);
  tab-size: 2;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

#editor::placeholder {
  color: var(--text-muted);
}

/* ===== 预览区域 ===== */
#preview {
  width: 100%;
  height: 100%;
  padding: 24px 32px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  font-size: 15px;
  line-height: 1.7;
  color: var(--text-primary);
}

/* ===== 拖拽分隔线 ===== */
.resizer {
  width: 6px;
  cursor: col-resize;
  background: var(--border-color);
  transition: background 0.15s;
  flex-shrink: 0;
  position: relative;
}

.resizer:hover,
.resizer.dragging {
  background: var(--accent);
}

.resizer::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2px;
  height: 24px;
  background: var(--text-muted);
  border-radius: 1px;
  opacity: 0.5;
}

/* ===== 预览样式 ===== */
.preview-content h1,
.preview-content h2,
.preview-content h3,
.preview-content h4,
.preview-content h5,
.preview-content h6 {
  margin-top: 24px;
  margin-bottom: 12px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-primary);
}

.preview-content h1 { font-size: 2em; border-bottom: 2px solid var(--border-color); padding-bottom: 8px; }
.preview-content h2 { font-size: 1.6em; border-bottom: 1px solid var(--border-color); padding-bottom: 6px; }
.preview-content h3 { font-size: 1.3em; }
.preview-content h4 { font-size: 1.1em; }
.preview-content h5 { font-size: 1em; }
.preview-content h6 { font-size: 0.9em; color: var(--text-secondary); }

.preview-content p {
  margin-bottom: 14px;
}

.preview-content a {
  color: var(--accent);
  text-decoration: none;
}

.preview-content a:hover {
  text-decoration: underline;
}

.preview-content strong { font-weight: 600; }
.preview-content em { font-style: italic; }

.preview-content blockquote {
  margin: 16px 0;
  padding: 12px 16px;
  border-left: 4px solid var(--accent);
  background: var(--bg-hover);
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--text-secondary);
}

.preview-content blockquote p:last-child { margin-bottom: 0; }

.preview-content ul,
.preview-content ol {
  margin: 12px 0;
  padding-left: 28px;
}

.preview-content li {
  margin-bottom: 4px;
}

.preview-content li > ul,
.preview-content li > ol {
  margin: 4px 0;
}

.preview-content li.task-list-item {
  list-style: none;
  padding-left: 0;
}

.preview-content input[type="checkbox"] {
  margin-right: 8px;
  vertical-align: middle;
}

.preview-content hr {
  border: none;
  border-top: 2px solid var(--border-color);
  margin: 24px 0;
}

.preview-content code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-hover);
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--accent);
}

.preview-content pre {
  margin: 16px 0;
  padding: 16px;
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  overflow-x: auto;
  position: relative;
}

.preview-content pre code {
  background: transparent;
  padding: 0;
  color: var(--code-text);
  font-size: 13px;
  line-height: 1.6;
}

.preview-content pre .hljs-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px 10px;
  font-size: 11px;
  color: var(--text-secondary);
  background: var(--bg-hover);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
}

.preview-content pre:hover .hljs-copy-btn {
  opacity: 1;
}

.preview-content pre .hljs-copy-btn:hover {
  background: var(--border-color);
  color: var(--text-primary);
}

.preview-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
  font-size: 14px;
}

.preview-content th,
.preview-content td {
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  text-align: left;
}

.preview-content th {
  background: var(--bg-hover);
  font-weight: 600;
}

.preview-content tr:nth-child(even) {
  background: rgba(0,0,0,0.02);
}

.preview-content img {
  max-width: 100%;
  border-radius: var(--radius);
  margin: 12px 0;
}

/* 脚注 */
.preview-content .footnote-ref {
  font-size: 0.85em;
  vertical-align: super;
}

.preview-content .footnote-ref a {
  color: var(--accent);
  text-decoration: none;
  padding: 0 2px;
  border-radius: 3px;
}

.preview-content .footnote-ref a:hover {
  text-decoration: underline;
  background: var(--bg-hover);
}

.preview-content .footnotes {
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid var(--border-color);
  font-size: 0.9em;
  color: var(--text-secondary);
}

.preview-content .footnotes ol {
  padding-left: 20px;
}

.preview-content .footnotes li {
  margin-bottom: 6px;
}

.preview-content .footnotes li a {
  color: var(--accent);
  text-decoration: none;
  margin-left: 4px;
}

.preview-content .footnotes li a:hover {
  text-decoration: underline;
}

/* 目录 */
.preview-content .toc {
  background: var(--bg-hover);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin: 16px 0;
}

.preview-content .toc-title {
  font-weight: 600;
  margin-bottom: 10px;
  font-size: 1.1em;
}

.preview-content .toc ul {
  margin: 0;
  padding-left: 20px;
}

.preview-content .toc li {
  margin-bottom: 4px;
}

.preview-content .toc a {
  color: var(--text-secondary);
}

.preview-content .toc a:hover {
  color: var(--accent);
}

/* Callouts */
.preview-content .callout {
  margin: 16px 0;
  padding: 16px 20px;
  border-radius: var(--radius);
  border-left: 4px solid;
}

.preview-content .callout-info {
  background: rgba(37, 99, 235, 0.08);
  border-color: #2563eb;
}

.preview-content .callout-warning {
  background: rgba(245, 158, 11, 0.08);
  border-color: #f59e0b;
}

.preview-content .callout-danger {
  background: rgba(239, 68, 68, 0.08);
  border-color: #ef4444;
}

.preview-content .callout-success {
  background: rgba(34, 197, 94, 0.08);
  border-color: #22c55e;
}

.preview-content .callout-title {
  font-weight: 600;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Mermaid */
.preview-content .mermaid {
  text-align: center;
  margin: 16px 0;
}

/* 高亮文本 */
.preview-content mark {
  background: #fef08a;
  color: #854d0e;
  padding: 1px 4px;
  border-radius: 3px;
}

[data-theme="dark"] .preview-content mark {
  background: #854d0e;
  color: #fef08a;
}

/* KaTeX 适配 */
.preview-content .katex-display {
  margin: 16px 0;
  overflow-x: auto;
}

/* ===== 状态栏 ===== */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 28px;
  padding: 0 16px;
  background: var(--bg-toolbar);
  border-top: 1px solid var(--border-color);
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.status-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-right {
  display: flex;
  align-items: center;
}

.sync-scroll-label {
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  user-select: none;
  color: var(--text-muted);
  font-size: 12px;
}

.sync-scroll-label input[type="checkbox"] {
  cursor: pointer;
  accent-color: var(--accent);
}

.status-sep {
  color: var(--border-color);
  user-select: none;
}

#save-status.saving { color: var(--accent); }

/* ===== 弹窗 ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal-overlay.active {
  display: flex;
}

.modal {
  background: var(--bg-toolbar);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  width: 90%;
  max-width: 480px;
  max-height: 80vh;
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
  font-size: 16px;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
}

.modal-close:hover {
  color: var(--text-primary);
}

.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
}

/* ===== 图片预览 ===== */
.img-preview-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.9);
  z-index: 2000;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}

.img-preview-overlay.active {
  display: flex;
}

.img-preview-overlay img {
  max-width: 90vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 4px;
}

.shortcut-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.shortcut-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-color);
}

.shortcut-table td:first-child {
  white-space: nowrap;
  width: 40%;
}

kbd {
  display: inline-block;
  padding: 2px 8px;
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--bg-hover);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  box-shadow: 0 1px 0 var(--border-color);
  margin: 0 2px;
}

/* ===== 响应式 ===== */
@media (max-width: 768px) {
  .toolbar-left .tool-btn:nth-child(n+8) {
    display: none;
  }

  .file-sidebar {
    width: 180px;
  }

  body.sidebar-open .editor-container {
    padding-left: 180px;
  }

  body.sidebar-open .resizer {
    margin-left: 180px;
  }

  .editor-container {
    flex-direction: column;
  }

  .resizer {
    display: none;
  }

  .editor-pane {
    width: 100% !important;
    flex: 4 1 0 !important;
    min-height: 0;
  }

  .preview-pane {
    width: 100% !important;
    flex: 6 1 0 !important;
    min-height: 0;
  }

  .preview-only .editor-pane {
    display: none;
  }

  .preview-only .preview-pane {
    display: flex;
  }
}

/* 仅预览模式 */
.preview-only .editor-pane {
  display: none;
}

.preview-only .resizer {
  display: none;
}

.preview-only .preview-pane {
  flex: 1 !important;
  width: 100% !important;
}

/* 滚动条美化 */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* 选中文字 */
::selection {
  background: var(--accent);
  color: white;
}

[data-theme="dark"] ::selection {
  background: var(--accent);
  color: #1a1a2e;
}

/* 同步滚动按钮激活状态 */
#sync-scroll-toggle.active,
#line-numbers-toggle.active {
  color: var(--accent);
}

/* ===== Tooltip ===== */
.tooltip {
  position: fixed;
  padding: 5px 10px;
  background: var(--text-primary);
  color: var(--bg-toolbar);
  font-size: 12px;
  border-radius: 4px;
  pointer-events: none;
  white-space: nowrap;
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.05s;
  line-height: 1.4;
}

.tooltip.show {
  opacity: 1;
}

.tooltip::after {
  content: '';
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 4px solid var(--text-primary);
}

/* ===== 打印样式（导出 PDF） ===== */
@media print {
  html, body {
    height: auto !important;
    overflow: visible !important;
  }

  .app {
    height: auto !important;
  }

  .toolbar,
  .file-sidebar,
  .editor-wrapper,
  .resizer,
  .status-bar,
  .modal-overlay,
  .tooltip,
  #sidebar-overlay,
  #help-modal {
    display: none !important;
  }

  body {
    background: #fff !important;
    color: #000 !important;
  }

  .editor-container {
    display: block !important;
    height: auto !important;
  }

  .editor-pane,
  .preview-pane {
    display: none !important;
    flex: none !important;
    width: 100% !important;
    height: auto !important;
    overflow: visible !important;
  }

  .preview-pane {
    display: block !important;
  }

  #preview {
    padding: 0 !important;
    background: #fff !important;
    color: #000 !important;
    max-width: 100% !important;
    overflow: visible !important;
    height: auto !important;
  }

  #preview a {
    color: #000 !important;
    text-decoration: underline !important;
  }

  #preview pre,
  #preview code {
    background: #f6f8fa !important;
    border: 1px solid #e0e0e0 !important;
  }

  #preview blockquote {
    border-left-color: #ccc !important;
    background: #f8f9fa !important;
  }

  #preview img,
  #preview svg {
    max-width: 100% !important;
    height: auto !important;
    page-break-inside: avoid;
  }

  #preview table {
    page-break-inside: avoid;
    width: 100% !important;
    table-layout: fixed !important;
    max-width: 100% !important;
  }

  #preview th,
  #preview td {
    word-wrap: break-word !important;
    overflow-wrap: break-word !important;
    word-break: break-word !important;
    padding: 6px 8px !important;
    font-size: 12px !important;
  }

  #preview tr {
    page-break-inside: avoid;
  }

  #preview pre {
    white-space: pre-wrap !important;
    word-break: break-word !important;
  }

  #preview code {
    word-break: break-word !important;
  }

  /* 避免分页切断代码块、表格和列表 */
  #preview pre,
  #preview blockquote,
  #preview table,
  #preview figure,
  #preview ul,
  #preview ol {
    page-break-inside: avoid;
  }

  #preview h1,
  #preview h2,
  #preview h3,
  #preview h4,
  #preview h5,
  #preview h6 {
    page-break-after: avoid;
  }
}
