:root {
  --bg: #f6f3ee;
  --panel: #fffdfa;
  --ink: #2a2723;
  --muted: #8a837a;
  --line: #e8e2d9;
  --accent: #c8742c;
  --accent-soft: #f6e6d6;
  --tag: #2f7a5f;
  --tag-bg: #e3f0ea;
  --danger: #c0392b;
  --heat-0: #ebe5dc; --heat-1: #f0cfae; --heat-2: #e2a46a; --heat-3: #c8742c; --heat-4: #8f4c14;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 2px 8px rgba(60, 40, 20, .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18; --panel: #24221f; --ink: #ece7e0; --muted: #8f887e; --line: #36332e;
    --accent: #e09050; --accent-soft: #3a2c1f; --tag: #7fc9a8; --tag-bg: #213229; --danger: #e57366;
    --heat-0: #2e2b27; --heat-1: #5a3e25; --heat-2: #8a5a30; --heat-3: #c8742c; --heat-4: #f0a868;
    --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font: 15px/1.7 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }

.layout { display: flex; min-height: 100vh; max-width: 1080px; margin: 0 auto; }

/* ---------- 侧栏 ---------- */
.sidebar {
  width: 248px; flex-shrink: 0; padding: 24px 16px; position: sticky; top: 0; height: 100vh;
  overflow-y: auto; display: flex; flex-direction: column; gap: 14px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; padding: 0 8px; }
.brand img { width: 26px; height: 26px; }
.stats { display: flex; gap: 18px; padding: 0 8px; color: var(--muted); font-size: 12px; }
.stats b { display: block; color: var(--ink); font-size: 20px; font-weight: 600; line-height: 1.2; }
.heatmap { display: grid; grid-template-rows: repeat(7, 11px); grid-auto-flow: column; grid-auto-columns: 11px; gap: 3px; padding: 0 8px; }
.heatmap i { border-radius: 2px; background: var(--heat-0); }
.heatmap i[data-l="1"] { background: var(--heat-1); } .heatmap i[data-l="2"] { background: var(--heat-2); }
.heatmap i[data-l="3"] { background: var(--heat-3); } .heatmap i[data-l="4"] { background: var(--heat-4); }
.filters { display: flex; flex-direction: column; }
.filters button, .tag-tree .tag-row {
  text-align: left; border: 0; background: none; padding: 6px 10px; border-radius: 8px; width: 100%;
  display: flex; align-items: center; gap: 4px;
}
.filters button:hover, .tag-tree .tag-row:hover { background: var(--line); }
.filters button.active, .tag-tree .tag-row.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.section-title { color: var(--muted); font-size: 12px; padding: 0 10px; margin-top: 4px; }
.tag-tree, .tag-tree ul { list-style: none; margin: 0; padding: 0; }
.tag-tree ul { padding-left: 14px; }
.tag-tree .caret { width: 14px; display: inline-block; color: var(--muted); font-size: 10px; flex-shrink: 0; }
.tag-tree .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag-tree .count { color: var(--muted); font-size: 12px; }
.tag-tree li.collapsed > ul { display: none; }
.sidebar-foot { margin-top: auto; display: flex; justify-content: space-between; padding: 0 8px; }
.link-btn { border: 0; background: none; color: var(--muted); padding: 4px 0; }
.link-btn:hover { color: var(--accent); }

/* ---------- 主栏 ---------- */
.main { flex: 1; min-width: 0; padding: 16px 24px 80px; }
.topbar { display: flex; align-items: center; gap: 12px; height: 48px; margin-bottom: 8px; }
.topbar .title { font-weight: 600; flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#search {
  width: 220px; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 12px; outline: none;
}
#search:focus { border-color: var(--accent); }
.topbar .menu-btn { display: none; }

.icon-btn { border: 0; background: none; width: 34px; height: 34px; border-radius: 8px; font-size: 17px; line-height: 1; display: inline-grid; place-items: center; color: var(--muted); }
.icon-btn:hover { background: var(--line); color: var(--ink); }
.icon-btn.on { color: var(--danger); }
.btn { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 8px; padding: 6px 16px; text-decoration: none; display: inline-block; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:disabled { opacity: .45; cursor: default; }
.btn.small { padding: 2px 10px; font-size: 13px; }
.btn.ghost { background: none; }

.composer {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px 8px;
  box-shadow: var(--shadow); position: relative; transition: border-color .15s;
}
.composer:focus-within, .composer.drag { border-color: var(--accent); }
.composer textarea, .note textarea {
  width: 100%; border: 0; outline: none; resize: none; background: none; min-height: 72px; max-height: 50vh; line-height: 1.7;
}
.toolbar { display: flex; align-items: center; gap: 2px; border-top: 1px solid var(--line); padding-top: 6px; margin-top: 4px; }
.toolbar .spacer { flex: 1; }
.toolbar .hint { color: var(--muted); font-size: 12px; margin-right: 10px; }
.pending { display: flex; flex-wrap: wrap; gap: 8px; }
.pending:empty { display: none; }
.pending .chip { position: relative; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; font-size: 12px; background: var(--bg); }
.pending .chip img { width: 72px; height: 72px; object-fit: cover; display: block; }
.pending .chip .label { padding: 6px 26px 6px 8px; display: block; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pending .chip audio { display: block; height: 34px; max-width: 240px; }
.pending .chip .x {
  position: absolute; top: 2px; right: 2px; width: 20px; height: 20px; border-radius: 50%; border: 0;
  background: rgba(0, 0, 0, .55); color: #fff; font-size: 12px; line-height: 20px; padding: 0;
}
.recording { display: flex; align-items: center; gap: 10px; padding: 6px 0; color: var(--danger); font-size: 14px; }
.recording .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

.tag-suggest {
  position: absolute; z-index: 20; background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .12); min-width: 160px; max-height: 220px; overflow-y: auto; padding: 4px;
}
.tag-suggest button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 4px 10px; border-radius: 6px; color: var(--tag); }
.tag-suggest button.sel, .tag-suggest button:hover { background: var(--tag-bg); }

.active-filter { margin: 16px 0 0; color: var(--muted); font-size: 13px; display: flex; gap: 8px; align-items: center; }
.active-filter button { border: 0; background: var(--line); border-radius: 6px; padding: 0 8px; font-size: 12px; }

.list { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
.note { position: relative; background: var(--panel); border-radius: var(--radius); padding: 12px 16px 14px; box-shadow: var(--shadow); border: 1px solid transparent; }
.note.editing { border-color: var(--accent); }
.note-head { display: flex; align-items: center; justify-content: space-between; position: relative; height: 26px; }
.note-head time { color: var(--muted); font-size: 12px; }
.note-head .more { width: 28px; height: 26px; opacity: .6; }
.note:hover .more { opacity: 1; }
.menu {
  position: absolute; right: 0; top: 28px; z-index: 10; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, .12); padding: 4px; min-width: 140px;
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 6px 12px; border-radius: 6px; font-size: 14px; }
.menu button:hover { background: var(--bg); }
.menu .danger { color: var(--danger); }
.note-body { white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; }
.note-body .tag, .tag-inline {
  color: var(--tag); background: var(--tag-bg); border-radius: 4px; padding: 0 4px; cursor: pointer; text-decoration: none;
}
.note-body a { color: var(--accent); word-break: break-all; }
.note-media { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 6px; margin-top: 8px; }
.note-media:empty { display: none; }
.note-media .img { aspect-ratio: 1; border-radius: 8px; overflow: hidden; background: var(--bg); cursor: zoom-in; position: relative; }
.note-media .img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.note-media .wide { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; position: relative; }
.note-media audio { width: 100%; max-width: 360px; height: 40px; }
.note-media .file { color: var(--accent); font-size: 14px; }
.note-media .del {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 50%; border: 0; padding: 0;
  background: rgba(0, 0, 0, .6); color: #fff; font-size: 12px; z-index: 2;
}
.note-media .wide .del { position: static; flex-shrink: 0; }
.note-links { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.note-links:empty { display: none; }
.link-card {
  display: block; border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: 8px; padding: 8px 12px;
  text-decoration: none; color: inherit; background: var(--bg);
}
.link-card:hover { border-color: var(--accent); }
.link-card .t { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-card .d { color: var(--muted); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.link-card .s { color: var(--muted); font-size: 12px; }
.link-card.pending .t { color: var(--muted); font-weight: 400; }
.edit-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 8px; }
.edit-actions .spacer { flex: 1; }
.empty { text-align: center; color: var(--muted); padding: 60px 0; }
.sentinel { height: 40px; }

.lightbox { position: fixed; inset: 0; background: rgba(0, 0, 0, .88); z-index: 100; display: grid; place-items: center; cursor: zoom-out; }
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 96vw; max-height: 96vh; object-fit: contain; }

dialog { border: 0; border-radius: 14px; padding: 0; max-width: 680px; width: calc(100% - 32px); background: var(--panel); color: var(--ink); }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog-body { padding: 20px 24px; }
.dialog-body h2 { margin: 0 0 8px; font-size: 18px; }
.dialog-body h3 { font-size: 14px; margin: 16px 0 6px; }
.dialog-body p { margin: 0; color: var(--muted); font-size: 13px; }
.dialog-body pre { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; overflow-x: auto; margin: 0; white-space: pre; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

.toast {
  position: fixed; left: 50%; bottom: 32px; transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: 8px 18px; border-radius: 20px; font-size: 14px; z-index: 200; pointer-events: none;
}

/* ---------- 登录 ---------- */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { background: var(--panel); padding: 32px; border-radius: 16px; box-shadow: var(--shadow); width: 100%; max-width: 340px; text-align: center; display: flex; flex-direction: column; gap: 12px; align-items: center; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; color: var(--muted); font-size: 13px; }
.login-card input { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; background: var(--bg); outline: none; }
.login-card input:focus { border-color: var(--accent); }
.login-card .btn { width: 100%; padding: 8px; }
.login-card .error { color: var(--danger); font-size: 13px; min-height: 1em; }

/* ---------- 移动端 ---------- */
@media (max-width: 760px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 50; background: var(--bg); transform: translateX(-100%);
    transition: transform .2s; width: 270px; padding-top: max(24px, env(safe-area-inset-top));
  }
  .sidebar.open { transform: none; box-shadow: 4px 0 24px rgba(0, 0, 0, .15); }
  .scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .3); z-index: 40; display: none; }
  .sidebar.open + .scrim { display: block; }
  .topbar .menu-btn { display: inline-grid; }
  .main { padding: 8px 12px 60px; padding-top: max(8px, env(safe-area-inset-top)); }
  #search { width: 130px; }
  .toolbar .hint { display: none; }
  .composer textarea { font-size: 16px; } /* 防止 iOS 聚焦缩放 */
  .note-media { grid-template-columns: repeat(3, 1fr); }
}
