/* ==========================================================================
   运维知识库 —— 苹果风格 UI
   ========================================================================== */

:root {
  --sidebar-width: 280px;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI",
    "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  --font-mono: "SF Mono", "JetBrains Mono", "Cascadia Code", Consolas, "Courier New", monospace;

  --accent: #0a84ff;
  --accent-hover: #0071e3;
  --danger: #ff3b30;

  --bg: #f5f5f7;
  --bg-elevated: #ffffff;
  --bg-sidebar: #ececf0;
  --border: rgba(0, 0, 0, 0.08);
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  --hover: rgba(0, 0, 0, 0.04);
  --code-bg: rgba(0, 0, 0, 0.04);
}

html[data-theme="dark"] {
  --bg: #1d1d1f;
  --bg-elevated: #2c2c2e;
  --bg-sidebar: #232325;
  --border: rgba(255, 255, 255, 0.1);
  --text: #f5f5f7;
  --text-secondary: #98989d;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.4);
  --hover: rgba(255, 255, 255, 0.06);
  --code-bg: rgba(255, 255, 255, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ---- 顶栏 ---- */
.topbar {
  height: 52px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 20;
  backdrop-filter: saturate(180%) blur(20px);
}

.topbar-inner {
  max-width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  font-weight: 600;
  font-size: 16px;
}

.brand-logo {
  width: 26px;
  height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  border-radius: 7px;
  font-size: 14px;
}

/* 搜索框 */
.search-box {
  flex: 1;
  max-width: 560px;
  margin: 0 auto;
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 10px;
  color: var(--text-secondary);
  pointer-events: none;
}

.search-box input {
  flex: 1;
  min-width: 0;
  height: 34px;
  padding: 0 12px 0 32px;
  border: none;
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: box-shadow 0.15s ease;
}

.search-box input:focus {
  box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.35);
}

.search-results {
  position: absolute;
  top: 42px;
  left: 0;
  right: 0;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-height: 60vh;
  overflow-y: auto;
  z-index: 30;
}

.search-item {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}

.search-item:last-child {
  border-bottom: none;
}

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

.search-item .path {
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 2px;
}

.search-item .snippet {
  color: var(--text-secondary);
  font-size: 12.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-all;
}

.search-empty {
  padding: 16px;
  color: var(--text-secondary);
  text-align: center;
  font-size: 13px;
}

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

/* ---- 按钮 ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 16px;
  border: none;
  border-radius: 980px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  background: var(--bg);
  color: var(--text);
  transition: all 0.15s ease;
  white-space: nowrap;
}

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

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

.btn.primary:hover {
  background: var(--accent-hover);
}

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

.btn.danger:hover {
  background: rgba(255, 59, 48, 0.1);
}

.btn.ghost {
  background: transparent;
  color: var(--text-secondary);
}

.btn.ghost:hover {
  background: var(--hover);
  color: var(--text);
}

.btn-icon {
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 980px;
  font-size: 16px;
  cursor: pointer;
  background: transparent;
  color: var(--text-secondary);
  transition: all 0.15s ease;
}

.btn-icon:hover {
  background: var(--hover);
  color: var(--text);
}

.btn-icon.accent {
  color: var(--accent);
  font-size: 20px;
  font-weight: 500;
}

/* ---- 布局 ---- */
.layout {
  display: flex;
  height: calc(100vh - 52px);
}

.sidebar {
  width: var(--sidebar-width);
  flex: 0 0 var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px 6px;
}

.sidebar-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
}

.sidebar-foot {
  padding: 10px 14px;
  font-size: 12px;
  color: var(--text-secondary);
  border-top: 1px solid var(--border);
}

.filing-links {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  text-align: center;
}

.filing-links a {
  display: block;
  color: var(--text-secondary);
  text-decoration: none;
  line-height: 1.5;
  white-space: nowrap;
}

.filing-links a:hover {
  color: var(--text);
  text-decoration: underline;
}

.filing-links img {
  width: 16px;
  height: 16px;
  vertical-align: middle;
  margin-right: 4px;
}

.tree {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
}

/* 目录树 */
.tree-node {
  user-select: none;
}

.tree-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  transition: background 0.12s ease;
}

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

.tree-row.active {
  background: var(--accent);
  color: #fff;
}

.tree-row .twisty {
  width: 16px;
  flex: 0 0 16px;
  text-align: center;
  font-size: 10px;
  color: var(--text-secondary);
  transition: transform 0.15s ease;
}

.tree-row.active .twisty,
.tree-row.active .tree-file-icon {
  color: #fff;
}

.tree-row.open .twisty {
  transform: rotate(90deg);
}

.tree-row .tree-file-icon {
  flex: 0 0 16px;
  font-size: 13px;
  color: var(--text-secondary);
}

.tree-row .tree-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-children {
  margin-left: 14px;
  padding-left: 8px;
  border-left: 1px solid var(--border);
}

.tree-children.hidden {
  display: none;
}

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

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  min-height: 52px;
}

.breadcrumb {
  font-size: 14px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.breadcrumb b {
  color: var(--text);
  font-weight: 600;
}

.toolbar-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

.content-area {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.content-wrap {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  position: relative;
}

/* ---- Markdown 正文 ---- */
.markdown-body {
  max-width: 860px;
  margin: 0 auto;
  padding: 32px 40px 80px;
}

.markdown-body.empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  max-width: none;
}

.placeholder {
  text-align: center;
  color: var(--text-secondary);
}

.placeholder-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.placeholder p {
  margin: 4px 0;
  font-size: 16px;
}

.placeholder-sub {
  font-size: 13px !important;
  opacity: 0.7;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  font-weight: 650;
  line-height: 1.3;
  margin: 1.6em 0 0.6em;
}

.markdown-body h1 {
  font-size: 1.8em;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3em;
}

.markdown-body h2 {
  font-size: 1.4em;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.25em;
}

.markdown-body h3 {
  font-size: 1.15em;
}

.markdown-body p {
  margin: 0.8em 0;
}

.markdown-body a {
  color: var(--accent);
  text-decoration: none;
}

.markdown-body a:hover {
  text-decoration: underline;
}

.markdown-body code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 5px;
}

.markdown-body pre {
  background: var(--code-bg);
  padding: 14px 16px;
  border-radius: 10px;
  overflow-x: auto;
  line-height: 1.5;
}

.markdown-body pre code {
  background: transparent;
  padding: 0;
  font-size: 13px;
}

.markdown-body blockquote {
  margin: 1em 0;
  padding: 0.4em 1em;
  border-left: 3px solid var(--accent);
  color: var(--text-secondary);
  background: var(--hover);
  border-radius: 0 8px 8px 0;
}

.markdown-body table {
  border-collapse: collapse;
  margin: 1em 0;
  width: 100%;
  font-size: 14px;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

.markdown-body th {
  background: var(--hover);
  font-weight: 600;
}

.markdown-body img {
  max-width: 100%;
  border-radius: 8px;
}

.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2em 0;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 1.6em;
}

/* 搜索高亮 */
mark {
  background: rgba(10, 132, 255, 0.18);
  color: inherit;
  padding: 0 1px;
  border-radius: 2px;
}

/* 正文搜索命中高亮 */
mark.search-hit {
  background: rgba(255, 204, 0, 0.35);
  color: inherit;
  padding: 0 2px;
  border-radius: 3px;
}

mark.search-hit.active {
  background: rgba(255, 149, 0, 0.6);
  outline: 2px solid rgba(255, 149, 0, 0.5);
  outline-offset: 0;
}

/* ---- 搜索命中导航条（搜索框右侧，搜索生效时缩小搜索框） ---- */
.match-nav {
  flex-shrink: 0;
  margin-left: 8px;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 0 2px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}

.match-nav .match-count {
  font-size: 13px;
  color: var(--text-secondary);
  padding: 0 8px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.match-nav .btn {
  height: 30px;
  padding: 0 8px;
  font-size: 13px;
}

.match-nav.hidden {
  display: none;
}

/* ---- 阅读页右侧目录（TOC） ---- */
.toc-side {
  flex: 0 0 240px;
  width: 240px;
  overflow-y: auto;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  padding: 16px 12px 24px;
  position: relative;
}

.toc-toggle {
  display: none;
  width: 100%;
  height: 36px;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  border-radius: 8px;
}

.toc-toggle:hover {
  background: var(--hover);
  color: var(--text);
}

.toc-side-head {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  margin: 4px 8px 10px;
}

.toc-list ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-item {
  margin: 2px 0;
  line-height: 1.5;
}

.toc-item a {
  display: block;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 13px;
  padding: 4px 8px;
  border-radius: 6px;
  border-left: 2px solid transparent;
}

.toc-item a:hover {
  color: var(--text);
  background: var(--hover);
}

.toc-item a.active {
  color: var(--accent);
  background: rgba(10, 132, 255, 0.08);
  border-left-color: var(--accent);
}

.toc-item.toc-l1 a {
  font-weight: 600;
  color: var(--text);
}

.toc-item.toc-l2 a {
  padding-left: 20px;
}

.toc-item.toc-l3 a {
  padding-left: 32px;
}

/* 窄窗口：右侧目录折叠为图标 */
@media (max-width: 900px) {
  .toc-side {
    flex: 0 0 40px;
    width: 40px;
    padding: 8px 4px;
    overflow: hidden;
  }

  .toc-side .toc-side-head,
  .toc-side .toc-list {
    display: none;
  }

  .toc-toggle {
    display: flex;
  }

  .toc-side.expanded {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 260px;
    flex-basis: 260px;
    overflow-y: auto;
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.12);
    z-index: 40;
  }

  .toc-side.expanded .toc-side-head,
  .toc-side.expanded .toc-list {
    display: block;
  }

  .toc-side.expanded .toc-toggle {
    margin-bottom: 8px;
  }
}

/* ---- 编辑器 ---- */
.editor {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.editor-main {
  flex: 1;
  display: flex;
  min-height: 0;
}

.editor-main textarea {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  resize: none;
  padding: 24px 32px;
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.7;
  background: var(--bg-elevated);
  color: var(--text);
}

.editor-preview {
  display: none;
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  padding: 24px 32px;
  border-left: 1px solid var(--border);
  background: var(--bg);
}

.editor-preview.markdown-body {
  max-width: none;
  margin: 0;
}

.editor.show-preview .editor-preview {
  display: block;
}

.editor-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
}

#editorHint {
  font-size: 13px;
  color: var(--text-secondary);
}

.editor-actions {
  display: flex;
  gap: 8px;
}

/* ---- 弹窗 ---- */
.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.modal {
  width: 360px;
  max-width: calc(100vw - 32px);
  background: var(--bg-elevated);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 24px;
}

.modal.wide {
  width: 520px;
}

.modal h3 {
  margin: 0 0 4px;
  font-size: 18px;
}

.modal-desc {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--text-secondary);
}

.modal code {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 4px;
}

.modal label {
  display: block;
  margin-bottom: 14px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

.modal input,
.modal textarea {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-family: var(--font-sans);
  outline: none;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

.modal textarea {
  font-family: var(--font-mono);
  resize: vertical;
}

.modal input:focus,
.modal textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.25);
}

.modal-error {
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
  margin-bottom: 6px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* ---- Toast ---- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg-elevated);
  padding: 10px 20px;
  border-radius: 980px;
  font-size: 14px;
  box-shadow: var(--shadow);
  z-index: 200;
  animation: toast-in 0.2s ease;
}

.toast.error {
  background: var(--danger);
  color: #fff;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

/* ---- 滚动条 ---- */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

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

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

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

/* ---- 右键菜单 ---- */
.context-menu {
  position: fixed;
  z-index: 300;
  min-width: 168px;
  padding: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.context-menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  text-align: left;
  border-radius: 8px;
  cursor: pointer;
}

.context-menu-item:hover {
  background: var(--hover);
}

.context-menu-item.danger {
  color: var(--danger);
}

.context-menu-item.danger:hover {
  background: rgba(255, 59, 48, 0.1);
}

.cmi-icon {
  width: 16px;
  text-align: center;
  font-size: 13px;
}

.context-menu-sep {
  height: 1px;
  margin: 4px 6px;
  background: var(--border);
}

/* ---- 行内命名输入框 ---- */
.inline-rename {
  margin: 2px 0;
}

.inline-rename input {
  width: 100%;
  height: 28px;
  padding: 0 8px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 13px;
  outline: none;
  box-shadow: 0 0 0 2px rgba(10, 132, 255, 0.2);
}

/* ---- 就地重命名输入框（替换 .tree-label） ---- */
.tree-row .rename-input {
  flex: 1;
  min-width: 0;
  height: 24px;
  padding: 0 6px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 13px;
  outline: none;
  box-shadow: 0 0 0 2px rgba(10, 132, 255, 0.2);
}

/* ---- 拖拽反馈 ---- */
.tree-row.dragging {
  opacity: 0.4;
}

.tree-row.drop-inside {
  background: rgba(10, 132, 255, 0.14);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.tree-row.drop-before {
  box-shadow: inset 0 2px 0 var(--accent);
}

.tree-row.drop-after {
  box-shadow: inset 0 -2px 0 var(--accent);
}
