/* ============================================================
   个人笔记管理 — 样式表 v2.0
   支持目录树视图 + 文件系统 / 本地存储双模式
   ============================================================ */

/* --- CSS 变量（浅色主题） --- */
:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f7f8fa;
  --bg-tertiary: #eef0f4;
  --bg-hover: #e4e7ed;
  --bg-active: #dce3f5;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --text-tertiary: #94a3b8;
  --border: #e2e8f0;
  --border-light: #f1f5f9;
  --accent: #4f6ef7;
  --accent-hover: #3d5ce5;
  --accent-light: #eef1ff;
  --accent-bg: #f0f3ff;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --danger-light: #fef2f2;
  --success: #10b981;
  --success-light: #ecfdf5;
  --warning: #f59e0b;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.12);
  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --sidebar-width: 300px;
  --transition-fast: 0.15s ease;
  --transition: 0.25s ease;
  --font-mono: 'SF Mono', 'Cascadia Code', 'Consolas', 'Monaco', monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
    'Microsoft YaHei', 'Helvetica Neue', sans-serif;
}

/* --- 深色主题 --- */
[data-theme="dark"] {
  --bg-primary: #1a1b1f;
  --bg-secondary: #222327;
  --bg-tertiary: #2c2e33;
  --bg-hover: #34363b;
  --bg-active: #2d3250;
  --text-primary: #e4e6eb;
  --text-secondary: #9ca3af;
  --text-tertiary: #6b7280;
  --border: #383a3f;
  --border-light: #2c2e33;
  --accent: #5c7cf8;
  --accent-hover: #4a6cf7;
  --accent-light: #1e2440;
  --accent-bg: #1a1f35;
  --danger: #f87171;
  --danger-hover: #ef4444;
  --danger-light: #2d1b1b;
  --success: #34d399;
  --success-light: #1a2e24;
  --warning: #fbbf24;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.5);
}

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; overflow: hidden; }

body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- Layout --- */
#app { display: flex; height: 100vh; overflow: hidden; }

/* --- 侧边栏遮罩（移动端） --- */
.sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.4); z-index: 90;
  opacity: 0; transition: opacity var(--transition);
}
.sidebar-overlay.show { display: block; opacity: 1; }

/* --- 侧边栏 --- */
.sidebar {
  width: var(--sidebar-width); min-width: var(--sidebar-width);
  height: 100vh; background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  z-index: 100; transition: transform var(--transition);
}

.sidebar-header {
  padding: 16px 16px 8px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px;
}

.app-title {
  font-size: 17px; font-weight: 700;
  color: var(--text-primary); white-space: nowrap;
}

.sidebar-header-btns {
  display: flex; gap: 4px;
}

/* --- 存储模式指示器 --- */
.sidebar-mode {
  padding: 0 16px 10px;
  display: flex; align-items: center; gap: 8px;
}

.mode-indicator {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  white-space: nowrap;
}

.btn-folder {
  font-size: 11px !important;
  padding: 3px 10px !important;
  white-space: nowrap;
  color: var(--accent) !important;
  border-color: var(--accent) !important;
}
.btn-folder:hover {
  background: var(--accent-light) !important;
}

/* --- 搜索 --- */
.sidebar-search {
  padding: 0 16px 10px; position: relative;
}

.sidebar-search .search-icon {
  position: absolute; left: 28px; top: 50%;
  transform: translateY(-50%); font-size: 13px; pointer-events: none;
}

.sidebar-search input {
  width: 100%; padding: 7px 12px 7px 34px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg-primary); color: var(--text-primary);
  font-size: 13px; outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.sidebar-search input:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-light);
}
.sidebar-search input::placeholder { color: var(--text-tertiary); }

/* --- 目录树容器 --- */
.tree-container {
  flex: 1; overflow-y: auto; padding: 0 8px 8px;
  scroll-behavior: smooth;
}

.tree-empty {
  text-align: center; padding: 40px 20px; color: var(--text-tertiary);
  font-size: 13px;
}
.tree-empty p { margin-bottom: 4px; }
.tree-empty .sub { font-size: 12px; }

/* --- 树节点 --- */
.tree-node {
  display: flex; align-items: center; gap: 4px;
  padding: 5px 8px; margin-bottom: 1px;
  border-radius: var(--radius-sm); cursor: pointer;
  font-size: 13px; line-height: 1.5;
  transition: background var(--transition-fast);
  border: 1px solid transparent;
  user-select: none;
}
.tree-node:hover { background: var(--bg-hover); }

.tree-node.active {
  background: var(--accent-bg);
  border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.tree-node.pinned .tree-icon {
  /* pinned indicator is handled by icon content */
}

.tree-toggle {
  width: 16px; text-align: center; font-size: 10px;
  color: var(--text-tertiary); flex-shrink: 0;
  cursor: pointer; transition: transform var(--transition-fast);
}

.tree-icon {
  font-size: 14px; flex-shrink: 0;
}

.tree-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--text-primary);
}

.tree-dir > .tree-name {
  font-weight: 600;
}

.tree-path-hint {
  font-size: 10px; color: var(--text-tertiary);
  margin-left: auto; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
  max-width: 100px;
}

.tree-children {
  /* container for child nodes, no extra styling needed */
}

/* --- 侧边栏底部 --- */
.sidebar-footer {
  padding: 8px 16px; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
}

.note-count {
  font-size: 11px; color: var(--text-tertiary); margin-left: auto;
}

/* --- 右键菜单 --- */
.context-menu {
  position: fixed; z-index: 1000;
  min-width: 180px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 6px;
  animation: ctx-fade-in 0.12s ease;
}

@keyframes ctx-fade-in {
  from { opacity: 0; transform: scale(0.95); }
  to   { opacity: 1; transform: scale(1); }
}

.ctx-item {
  padding: 7px 12px; border-radius: var(--radius-sm);
  font-size: 13px; cursor: pointer;
  color: var(--text-primary);
  transition: background var(--transition-fast);
  display: flex; align-items: center; gap: 6px;
}
.ctx-item:hover { background: var(--bg-hover); }

.ctx-danger { color: var(--danger) !important; }
.ctx-danger:hover { background: var(--danger-light) !important; }

.ctx-separator {
  height: 1px; background: var(--border);
  margin: 4px 8px;
}

/* --- 主内容区 --- */
.main-content {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; background: var(--bg-primary); min-width: 0;
}

/* --- 空状态 --- */
.empty-state {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  text-align: center; padding: 40px; color: var(--text-secondary);
}

.empty-state-icon { font-size: 64px; margin-bottom: 16px; opacity: 0.6; }

.empty-state h2 {
  font-size: 22px; font-weight: 700; color: var(--text-primary); margin-bottom: 8px;
}

.empty-state p { font-size: 15px; margin-bottom: 24px; color: var(--text-secondary); }

.shortcut-hints {
  display: flex; gap: 20px; margin-top: 20px;
  font-size: 12px; color: var(--text-tertiary); flex-wrap: wrap; justify-content: center;
}

.shortcut-hints kbd {
  display: inline-block; padding: 2px 6px; font-size: 11px;
  font-family: var(--font-mono); background: var(--bg-tertiary);
  border: 1px solid var(--border); border-radius: 3px;
  box-shadow: 0 1px 0 var(--border);
}

/* --- 编辑器容器 --- */
.editor-container {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; padding: 20px 24px; gap: 10px;
}

/* --- 编辑器工具栏 --- */
.editor-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
  padding-bottom: 6px; border-bottom: 1px solid var(--border-light);
  min-height: 36px;
}

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

.note-location {
  font-size: 12px; color: var(--text-tertiary);
  display: flex; align-items: center; gap: 4px;
}

.tags-input {
  padding: 5px 10px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--bg-primary);
  color: var(--text-primary); font-size: 12px; outline: none;
  width: 180px; transition: border-color var(--transition-fast);
}
.tags-input:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-light);
}
.tags-input::placeholder { color: var(--text-tertiary); }

/* 视图切换按钮组 */
.view-toggles {
  display: flex; background: var(--bg-tertiary);
  border-radius: var(--radius-sm); padding: 2px; gap: 2px;
}

.view-btn {
  padding: 4px 10px; border: none; background: transparent;
  color: var(--text-secondary); font-size: 12px; font-family: inherit;
  border-radius: 4px; cursor: pointer;
  transition: all var(--transition-fast); white-space: nowrap;
}
.view-btn.active {
  background: var(--bg-primary); color: var(--text-primary);
  box-shadow: var(--shadow-sm); font-weight: 600;
}
.view-btn:hover:not(.active) { color: var(--text-primary); }

/* --- 笔记标题输入 --- */
.note-title-input {
  width: 100%; padding: 4px 0;
  border: none; border-bottom: 2px solid transparent;
  background: transparent; font-size: 24px; font-weight: 700;
  font-family: inherit; color: var(--text-primary); outline: none;
  transition: border-color var(--transition-fast);
}
.note-title-input:focus { border-bottom-color: var(--accent); }
.note-title-input::placeholder { color: var(--text-tertiary); font-weight: 400; }

/* --- 编辑 & 预览区域 --- */
.editor-area {
  flex: 1; display: flex; gap: 0; overflow: hidden; min-height: 0;
  border: 1px solid var(--border); border-radius: var(--radius);
  transition: border-color var(--transition-fast);
}
.editor-area:focus-within { border-color: var(--accent); }

.editor-area.mode-edit .edit-pane { flex: 1; display: flex; }
.editor-area.mode-edit .preview-pane { display: none !important; }

.editor-area.mode-preview .edit-pane { display: none !important; }
.editor-area.mode-preview .preview-pane { flex: 1; display: flex !important; }

.editor-area.mode-split .edit-pane { flex: 1; display: flex; }
.editor-area.mode-split .preview-pane {
  flex: 1; display: flex !important; border-left: 1px solid var(--border);
}

.edit-pane { min-width: 0; min-height: 0; }

.note-content-input {
  width: 100%; height: 100%; padding: 16px;
  border: none; background: transparent;
  color: var(--text-primary); font-size: 15px;
  font-family: var(--font-mono); line-height: 1.7;
  resize: none; outline: none; tab-size: 2;
}
.note-content-input::placeholder {
  color: var(--text-tertiary); font-family: var(--font-sans);
}

.preview-pane { min-width: 0; min-height: 0; overflow-y: auto; }

#markdown-preview { padding: 20px 24px; }

/* --- Markdown 渲染样式 --- */
.markdown-body {
  font-size: 15px; line-height: 1.75; color: var(--text-primary);
  word-wrap: break-word;
}
.markdown-body > *:first-child { margin-top: 0 !important; }
.markdown-body > *:last-child  { margin-bottom: 0 !important; }

.markdown-body h1 { font-size: 28px; font-weight: 700; margin: 28px 0 16px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.markdown-body h2 { font-size: 22px; font-weight: 700; margin: 24px 0 12px; }
.markdown-body h3 { font-size: 18px; font-weight: 600; margin: 20px 0 10px; }
.markdown-body h4 { font-size: 16px; font-weight: 600; margin: 16px 0 8px; }
.markdown-body h5, .markdown-body h6 { font-size: 14px; font-weight: 600; margin: 12px 0 6px; color: var(--text-secondary); }

.markdown-body p { margin: 0 0 12px; }
.markdown-body a { color: var(--accent); }
.markdown-body a:hover { text-decoration: underline; }
.markdown-body strong { font-weight: 700; }

.markdown-body ul, .markdown-body ol { padding-left: 24px; margin: 0 0 12px; }
.markdown-body li { margin-bottom: 4px; }
.markdown-body li > ul, .markdown-body li > ol { margin-bottom: 0; }

.markdown-body blockquote {
  margin: 12px 0; padding: 8px 16px;
  border-left: 4px solid var(--accent);
  background: var(--accent-light);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-secondary);
}
.markdown-body blockquote > :last-child { margin-bottom: 0; }

.markdown-body code {
  padding: 2px 6px; font-family: var(--font-mono); font-size: 13px;
  background: var(--bg-tertiary); border-radius: 4px; color: var(--danger);
}
[data-theme="dark"] .markdown-body code { color: #f87171; }

.markdown-body pre {
  margin: 12px 0; padding: 16px; background: var(--bg-tertiary);
  border-radius: var(--radius-sm); overflow-x: auto;
  font-size: 13px; line-height: 1.6;
}
.markdown-body pre code {
  padding: 0; background: none; color: var(--text-primary); font-size: inherit;
}

.markdown-body table { width: 100%; border-collapse: collapse; margin: 12px 0; }
.markdown-body th, .markdown-body td { padding: 8px 12px; border: 1px solid var(--border); text-align: left; }
.markdown-body th { background: var(--bg-tertiary); font-weight: 600; }
.markdown-body tr:nth-child(even) td { background: var(--bg-secondary); }

.markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; }
.markdown-body img { max-width: 100%; border-radius: var(--radius-sm); }
.markdown-body input[type="checkbox"] { margin-right: 6px; }

/* --- 编辑器底部状态栏 --- */
.editor-footer {
  display: flex; align-items: center; justify-content: space-between;
  padding-top: 2px; font-size: 12px; color: var(--text-tertiary);
  flex-shrink: 0;
}
.save-status { transition: opacity var(--transition-fast); }
.save-status.saving { color: var(--warning); }
.save-status.saved  { color: var(--success); }
.save-status.error  { color: var(--danger); }

/* --- 按钮 --- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 4px; padding: 8px 16px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg-primary); color: var(--text-primary);
  font-size: 13px; font-family: inherit; font-weight: 500;
  cursor: pointer; transition: all var(--transition-fast);
  white-space: nowrap; user-select: none;
}
.btn:hover { background: var(--bg-hover); }
.btn:active { transform: scale(0.97); }

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

.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-light); color: var(--danger-hover); }

.btn-icon {
  width: 34px; height: 34px; padding: 0;
  border-radius: var(--radius-sm); font-size: 16px;
}
.btn-icon:hover { background: var(--bg-hover); }

.btn-sm {
  padding: 5px 10px; font-size: 12px; border-radius: var(--radius-sm);
  height: 30px;
}

.btn-lg {
  padding: 12px 28px; font-size: 15px; border-radius: var(--radius);
}

/* --- Toast 通知 --- */
.toast-container {
  position: fixed; top: 20px; right: 20px; z-index: 1000;
  display: flex; flex-direction: column; gap: 8px; pointer-events: none;
}

.toast {
  padding: 10px 16px; border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 500; color: #ffffff;
  box-shadow: var(--shadow-md);
  animation: toast-in 0.3s ease;
  pointer-events: auto; max-width: 320px;
}
.toast.removing { animation: toast-out 0.25s ease forwards; }

.toast-success { background: var(--success); }
.toast-error   { background: var(--danger); }
.toast-info    { background: var(--accent); }

@keyframes toast-in {
  from { opacity: 0; transform: translateX(40px); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes toast-out {
  from { opacity: 1; transform: translateX(0); }
  to   { opacity: 0; transform: translateX(40px); }
}

/* --- 滚动条 --- */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--text-tertiary); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }
.tree-container::-webkit-scrollbar { width: 4px; }

/* --- 移动端菜单按钮 --- */
.mobile-menu-btn {
  display: none; position: fixed; top: 12px; left: 12px; z-index: 110;
  width: 40px; height: 40px; font-size: 20px;
  background: var(--bg-primary); box-shadow: var(--shadow-md);
  border: 1px solid var(--border);
}

/* --- 打印样式 --- */
@media print {
  .sidebar, .mobile-menu-btn, .sidebar-overlay,
  .editor-toolbar, .editor-footer, .toast-container, .context-menu {
    display: none !important;
  }
  .main-content { margin-left: 0 !important; overflow: visible !important; }
  .editor-container { padding: 0 !important; }
  .note-title-input { border: none !important; }
  .editor-area { border: none !important; }
  .note-content-input { font-family: inherit !important; }
}

/* --- 响应式 --- */
@media (max-width: 768px) {
  .sidebar {
    position: fixed; left: 0; top: 0; height: 100vh;
    transform: translateX(-100%); box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: translateX(0); }

  .mobile-menu-btn { display: flex; }
  .main-content { margin-left: 0 !important; }

  .editor-container { padding: 16px 12px; }

  .editor-toolbar { flex-direction: column; align-items: stretch; }
  .toolbar-left, .toolbar-right { flex-wrap: wrap; }
  .tags-input { width: 140px; }
  .note-title-input { font-size: 20px; }

  .editor-area.mode-split { flex-direction: column; }
  .editor-area.mode-split .preview-pane {
    border-left: none; border-top: 1px solid var(--border);
  }
}

@media (max-width: 480px) {
  .sidebar { width: 85vw; min-width: 85vw; }
  .editor-container { padding: 12px 8px; }
  .view-btn { padding: 4px 8px; font-size: 11px; }
}
