*,
*::before,
*::after { box-sizing: border-box; }

/* ---------- 颜色与变量 ---------- */
:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --text: #1a1a1a;
  --muted: #888888;
  --border: #e5e5e5;
  --primary: #111111;
  --primary-hover: #333333;
  --danger: #d93838;
  --mark: #fef08a;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #191919;
    --panel: #1e1e1e;
    --text: #e0e0e0;
    --muted: #888888;
    --border: #333333;
    --primary: #ffffff;
    --primary-hover: #cccccc;
    --danger: #f87171;
    --mark: #5a4a13;
  }
}

:root[data-theme="dark"] {
  --bg: #191919;
  --panel: #1e1e1e;
  --text: #e0e0e0;
  --muted: #888888;
  --border: #333333;
  --primary: #ffffff;
  --primary-hover: #cccccc;
  --danger: #f87171;
  --mark: #5a4a13;
}

/* ---------- 基础 ---------- */
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3 { line-height: 1.3; font-weight: 600; }
h1 { font-size: 1.4rem; margin: 0; letter-spacing: -0.02em; }
h2 { font-size: 1.1rem; margin: 0 0 1rem; }

.muted { color: var(--muted); }
.small { font-size: .85rem; font-weight: normal; }

/* ---------- 表单与按钮 ---------- */
input[type=text],
input[type=search],
input[type=password],
input[type=number],
input[type=datetime-local],
select,
textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 10px 12px;
  min-height: 42px;
  outline: none;
  transition: border-color .15s;
}
input:focus,
select:focus,
textarea:focus { border-color: var(--primary); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: .9rem;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: all .15s;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--border); text-decoration: none; }
.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--bg);
}
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { color: var(--danger); border-color: transparent; }
.btn.danger:hover { background: rgba(217, 56, 56, .1); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: .85rem; }

label {
  display: block;
  font-size: .9rem;
  color: var(--muted);
  margin-bottom: 12px;
}
label input,
label select { margin-top: 6px; color: var(--text); }
.form label:last-of-type { margin-bottom: 16px; }

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: .95rem;
  cursor: pointer;
}
.check input { width: auto; min-height: 0; margin: 0; accent-color: var(--primary); }

/* ---------- 提示 ---------- */
.alert {
  padding: 10px 14px;
  border-radius: 4px;
  margin-bottom: 16px;
  font-size: .9rem;
  border: 1px solid var(--border);
  background: var(--bg);
}
.alert.error   { border-color: var(--danger); color: var(--danger); }
.alert.success { border-color: #16a34a; color: #16a34a; }
.alert.info    { border-color: var(--primary); color: var(--muted); }

/* ---------- 顶栏与布局 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}
.brand {
  flex: 1;
  font-weight: 600;
  font-size: 1.05rem;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: -0.01em;
}
.brand:hover { text-decoration: none; color: var(--text); }

.icon-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  font-size: 1.2rem;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 4px;
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); background: var(--border); }
.icon-btn.theme-toggle { display: inline-flex !important; }

.layout { display: flex; align-items: flex-start; }

.sidebar {
  position: sticky;
  top: 56px;
  flex: 0 0 240px;
  width: 240px;
  height: calc(100vh - 56px);
  overflow-y: auto;
  padding: 24px 16px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.side-title {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  padding: 4px 8px 12px;
}

.side-nav { display: flex; flex-direction: column; gap: 2px; }
.side-bottom { padding-top: 16px; }

.side-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 36px;
  padding: 6px 8px;
  border-radius: 4px;
  color: var(--text);
  font-size: .92rem;
  transition: background .15s;
}
.side-link:hover { background: var(--border); text-decoration: none; }
.side-link.active { background: var(--border); font-weight: 600; }
.side-link .count { font-size: .8rem; color: var(--muted); font-weight: normal; }
.side-link.side-manage { color: var(--muted); font-size: .85rem; margin-top: 4px; }

.main {
  flex: 1;
  min-width: 0;
  max-width: 800px;
  margin: 0 auto;
  padding: 32px 32px 80px;
}

.overlay { display: none; }

/* ---------- 页面头 ---------- */
.page-head {
  display: flex;
  align-items: center;
  gap: 16px;
  justify-content: space-between;
  margin-bottom: 24px;
  flex-wrap: wrap;
}
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- 搜索 ---------- */
.searchbar { display: flex; gap: 8px; margin-bottom: 24px; }
.searchbar input { flex: 1; border: none; border-bottom: 1px solid var(--border); border-radius: 0; padding-left: 0; }
.searchbar input:focus { border-bottom-color: var(--primary); }

/* ---------- 文档列表 ---------- */
.doc-list { display: flex; flex-direction: column; gap: 0; }

.doc-card {
  display: block;
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  transition: opacity .15s;
}
.doc-card:last-child { border-bottom: none; }
.doc-card:hover { text-decoration: none; opacity: 0.7; }

.doc-title { font-weight: 500; font-size: 1.05rem; margin-bottom: 6px; word-break: break-word; }
.doc-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-size: .8rem;
  color: var(--muted);
  margin-bottom: 8px;
}
.doc-excerpt {
  font-size: .9rem;
  color: var(--muted);
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

.badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 3px;
  background: var(--border);
  color: var(--muted);
  font-size: .75rem;
}

mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 2px; }

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 32px;
}

/* ---------- 文档详情 ---------- */
.doc-view .page-head { margin-bottom: 12px; border-bottom: 1px solid var(--border); padding-bottom: 16px; }

.prose { font-size: 1rem; line-height: 1.8; word-wrap: break-word; }
.prose h1, .prose h2, .prose h3, .prose h4 { margin: 1.8em 0 .8em; line-height: 1.3; font-weight: 600; }
.prose h1 { font-size: 1.6rem; }
.prose h2 { font-size: 1.3rem; }
.prose h3 { font-size: 1.1rem; }
.prose p { margin: 1em 0; }
.prose ul, .prose ol { padding-left: 1.5em; margin: 1em 0; }
.prose li { margin: .4em 0; }
.prose blockquote {
  margin: 1.5em 0;
  padding: 4px 16px;
  border-left: 3px solid var(--primary);
  color: var(--muted);
  background: var(--bg);
}
.prose code {
  font-family: var(--mono);
  font-size: .88em;
  background: var(--border);
  border-radius: 4px;
  padding: 2px 6px;
}
.prose pre {
  background: #f6f8fa; /* 可以自行改成暗色 */
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 16px;
  overflow-x: auto;
  margin: 1.5em 0;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .prose pre { background: #0d1117; }
}
:root[data-theme="dark"] .prose pre { background: #0d1117; }

.prose pre code { background: none; padding: 0; font-size: .85rem; }
.prose img { max-width: 100%; height: auto; border-radius: 4px; margin: 1em 0; }
.prose table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5em 0;
  font-size: .9rem;
  display: block;
  overflow-x: auto;
}
.prose th, .prose td { border: 1px solid var(--border); padding: 8px 12px; text-align: left; }
.prose th { font-weight: 600; }
.prose hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }
.prose a { color: var(--primary); text-decoration: underline; text-underline-offset: 3px; }

/* ---------- 编辑器 ---------- */
.editor input[type=text] { 
  font-size: 1.3rem; 
  font-weight: 600; 
  border: none; 
  padding: 8px 0; 
  margin-bottom: 8px;
}
.editor input[type=text]:focus { border: none; }

.form-row { display: flex; gap: 16px; margin: 16px 0; }
.form-row .grow { flex: 1; margin-bottom: 0; }

.editor textarea {
  font-family: var(--mono);
  font-size: .95rem;
  line-height: 1.7;
  min-height: 400px;
  resize: vertical;
  margin: 16px 0;
  border: none;
  background: transparent;
  padding: 8px 0;
}
.editor textarea:focus { border: none; }

.btn-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.btn-row .hint { margin-left: auto; color: var(--muted); font-size: .85rem; }

/* ---------- 卡片（分类/设置页） ---------- */
.card {
  padding: 16px 0;
  margin-bottom: 24px;
  border-bottom: 1px solid var(--border);
}
.card:last-child { border-bottom: none; }

.cat-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 0;
  border-bottom: 1px dashed var(--border);
  flex-wrap: wrap;
}
.cat-row:last-child { border-bottom: none; }

.cat-form { 
  display: flex; 
  align-items: center; 
  gap: 8px; 
  flex: 1 1 auto; 
  min-width: 0;
}
.cat-form input[type="text"] { flex: 1; min-width: 0; }
.cat-form .sort-input { width: 80px !important; flex: none !important; }

/* ---------- 登录 / 初始化 ---------- */
body.bare { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 24px; }
.auth-main { width: 100%; max-width: 340px; }
.auth-box h1 { margin-bottom: 8px; text-align: center; }
.auth-box .muted { margin-top: 0; margin-bottom: 24px; font-size: .9rem; text-align: center; }
.auth-box .btn { width: 100%; margin-top: 8px; }

/* ---------- 响应式 ---------- */
@media (max-width: 760px) {
  .icon-btn { display: inline-flex; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    height: 100vh;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .22s ease;
    padding-top: calc(16px + env(safe-area-inset-top));
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    width: 82vw;
    max-width: 300px;
    background: var(--bg);
    box-shadow: none;
    border-right: 1px solid var(--border);
  }
  body.sidebar-open .sidebar { 
    transform: translateX(0); 
    box-shadow: 4px 0 24px rgba(0, 0, 0, .1);
  }

  body.sidebar-open .overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgba(0, 0, 0, .3);
  }

  .main { padding: 24px 20px calc(60px + env(safe-area-inset-bottom)); }
  .topbar { padding: 0 16px; }
  .page-head h1 { font-size: 1.25rem; }

  .form-row { flex-direction: column; gap: 0; }
  .form-row .grow { margin-bottom: 12px; }

  .btn { min-height: 44px; }
  .side-link { min-height: 44px; }
  .sort-input { width: 64px !important; }

  .cat-row .cat-form { width: 100%; flex: 1 1 100%; }
  .cat-row > form:last-child { width: 100%; margin-top: 4px; }
  .cat-row > form:last-child button { width: 100%; }

  .prose { font-size: .97rem; }
}

@media (max-width: 380px) {
  .brand { font-size: .98rem; }
  .btn-row .hint { display: none; }
}

/* 去掉悬停时的乱七八糟效果 */
@media (hover: none) {
  .doc-card:hover { opacity: 1; }
  .side-link:hover { background: transparent; }
}