/* ============ 基础与主题变量 ============ */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --paper: #ffffff;
  --border: #e4e7ec;
  --text: #1f2329;
  --muted: #8f959e;
  --accent: #3d6df2;
  --accent-soft: #eef3ff;
  --code-bg: #f4f6f8;
  --code-text: #c7254e;
  --quote-text: #4b5563;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
}

[data-theme="dark"] {
  --bg: #101318;
  --panel: #171c23;
  --paper: #171c23;
  --border: #2b323c;
  --text: #e8ebf0;
  --muted: #7c8592;
  --accent: #6a8dff;
  --accent-soft: #20293c;
  --code-bg: #1f2630;
  --code-text: #ff7b9c;
  --quote-text: #a8b3c1;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background .2s, color .2s;
}

button, input, select { font-family: inherit; }

.app { display: flex; height: 100vh; overflow: hidden; }

/* ============ 侧边栏 ============ */
.sidebar {
  width: 272px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 12px;
}

.brand {
  font-weight: 700;
  font-size: 17px;
  padding: 2px 8px;
  letter-spacing: .5px;
}

.new-btn {
  padding: 10px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: filter .15s;
}
.new-btn:hover { filter: brightness(1.1); }

.search {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  outline: none;
}
.search:focus { border-color: var(--accent); }

.doc-list {
  flex: 1;
  overflow-y: auto;
  margin: 0 -4px;
  padding: 0 4px;
}

.doc-item {
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border: 1px solid transparent;
  transition: background .12s;
}
.doc-item:hover { background: var(--bg); }
.doc-item.active { background: var(--accent-soft); }

.doc-item-title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.doc-item.active .doc-item-title { color: var(--accent); font-weight: 600; }

.doc-item-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
}

.del-btn {
  opacity: 0;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 13px;
  padding: 2px 5px;
  border-radius: 5px;
  line-height: 1;
}
.doc-item:hover .del-btn { opacity: .7; }
.del-btn:hover { opacity: 1; background: rgba(128, 128, 128, .18); }

.empty-tip {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 24px 0;
}

.sidebar-footer {
  font-size: 12px;
  color: var(--muted);
  padding: 10px 8px 0;
  border-top: 1px solid var(--border);
}

/* ============ 主区域 ============ */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: 52px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 14px;
  cursor: pointer;
  transition: background .12s;
}
.icon-btn:hover { background: var(--accent-soft); }

.menu-btn { display: none; }

.status-area {
  font-size: 13px;
  color: var(--muted);
  display: flex;
  gap: 6px;
  align-items: center;
  min-width: 0;
}
.save-status.saving { color: #d97706; }

.topbar-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
  align-items: center;
}

.topbar-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: background .12s;
}
.topbar-btn:hover { background: var(--accent-soft); }

.export-wrap { position: relative; }

.export-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
  min-width: 170px;
  display: none;
  z-index: 30;
}
.export-menu.show { display: block; }

.export-menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 12px;
  border: none;
  background: none;
  color: var(--text);
  font-size: 13px;
  border-radius: 7px;
  cursor: pointer;
}
.export-menu button:hover { background: var(--accent-soft); }

/* ============ 文档纸面 ============ */
.page {
  flex: 1;
  overflow-y: auto;
  padding: 28px 16px 80px;
}

.paper {
  max-width: 860px;
  margin: 0 auto;
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 40px 56px 72px;
}

#docTitle {
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  font-size: 30px;
  font-weight: 700;
  color: var(--text);
  padding: 4px 0 12px;
  border-bottom: 1px solid transparent;
}
#docTitle:focus { border-bottom-color: var(--border); }
#docTitle::placeholder { color: var(--muted); opacity: .6; }

/* ============ 工具栏 ============ */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px;
  padding: 8px 0;
  margin-bottom: 10px;
  background: var(--paper);
  border-bottom: 1px solid var(--border);
}

.tb-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tb-btn:hover { background: var(--accent-soft); color: var(--accent); }
.tb-btn.active { background: var(--accent-soft); color: var(--accent); }

.toolbar .sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 6px;
}

#blockSelect {
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--paper);
  color: var(--text);
  font-size: 13px;
  padding: 0 6px;
  outline: none;
  cursor: pointer;
}

/* ============ 正文编辑区 ============ */
#editor {
  min-height: 55vh;
  outline: none;
  font-size: 16px;
  line-height: 1.9;
  word-break: break-word;
}
#editor:empty::before {
  content: attr(data-placeholder);
  color: var(--muted);
  pointer-events: none;
}

#editor h1 { font-size: 28px; line-height: 1.4; margin: 18px 0 10px; }
#editor h2 { font-size: 22px; line-height: 1.5; margin: 16px 0 8px; }
#editor h3 { font-size: 18px; line-height: 1.5; margin: 14px 0 6px; }
#editor p { margin: 6px 0; }
#editor ul, #editor ol { padding-left: 1.8em; margin: 8px 0; }

#editor blockquote {
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  color: var(--quote-text);
  padding: 10px 16px;
  margin: 12px 0;
  border-radius: 0 8px 8px 0;
}
#editor blockquote p { margin: 4px 0; }

#editor pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  padding: 14px 16px;
  border-radius: 10px;
  overflow-x: auto;
  font-family: "Cascadia Code", Consolas, "Courier New", monospace;
  font-size: 14px;
  line-height: 1.7;
  margin: 12px 0;
  white-space: pre-wrap;
}
#editor code {
  background: var(--code-bg);
  border-radius: 4px;
  padding: 2px 6px;
  font-family: "Cascadia Code", Consolas, "Courier New", monospace;
  font-size: .88em;
  color: var(--code-text);
}
#editor pre code { padding: 0; background: none; color: inherit; }

#editor a { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
#editor img { max-width: 100%; border-radius: 10px; margin: 8px 0; }
#editor hr { border: none; border-top: 1px solid var(--border); margin: 24px 0; }

/* ============ 滚动条 ============ */
.doc-list::-webkit-scrollbar, .page::-webkit-scrollbar { width: 8px; }
.doc-list::-webkit-scrollbar-thumb, .page::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* ============ 移动端 ============ */
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-105%);
    transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .menu-btn { display: inline-flex; }
  .page { padding: 0 0 60px; }
  .paper { padding: 24px 20px 60px; border-radius: 0; border: none; }
}

/* ============ 页面内弹窗 ============ */
.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .4);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-mask.show { display: flex; }

.modal {
  width: 340px;
  max-width: calc(100vw - 40px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 20px;
}

.modal-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 14px;
  line-height: 1.6;
}

#modalInput {
  width: 100%;
  padding: 9px 10px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  margin-bottom: 16px;
  display: none;
}
#modalInput:focus { border-color: var(--accent); }

.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.modal-actions button {
  padding: 7px 16px;
  font-size: 13px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
}
.modal-actions button:hover { background: var(--accent-soft); }
#modalOk {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
#modalOk:hover { filter: brightness(1.08); }

/* ============ 打印 ============ */
@media print {
  body { background: #fff; color: #000; }
  .app { display: block; height: auto; }
  .sidebar, .topbar, .toolbar { display: none !important; }
  .page { overflow: visible; padding: 0; }
  .paper { box-shadow: none; border: none; max-width: 100%; padding: 0; border-radius: 0; }
  #editor { min-height: auto; }
  #docTitle { border-bottom: none; }
}
