:root {
  --bg: #f7f4ee;
  --surface: #fffdf9;
  --ink: #2b2620;
  --muted: #8a8177;
  --line: #e5ded2;
  --accent: #b4593a;
  --accent-soft: #f3e2da;
  --danger: #a33a2a;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(43,38,32,.06);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.6;
}

body { display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px 8px;
}

.brand { font-size: 19px; font-weight: 600; letter-spacing: .08em; }
.brand-sub { font-size: 12px; color: var(--muted); font-weight: 400; letter-spacing: .15em; margin-left: 6px; }

.status { font-size: 12px; color: var(--muted); }
.status.ok::before { content: "● "; color: #5a9367; }
.status.err::before { content: "● "; color: var(--danger); }

.tabs {
  display: flex;
  gap: 4px;
  padding: 0 14px 8px;
  border-bottom: 1px solid var(--line);
}

.tab {
  flex: 1;
  padding: 8px 4px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
}
.tab.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

main { flex: 1; min-height: 0; }
.view { display: none; height: 100%; }
.view.active { display: block; }

.list-wrap, .search-wrap, .editor-wrap { height: 100%; display: flex; flex-direction: column; }

.list-head {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 16px 18px 8px; font-size: 13px; color: var(--muted);
}
.meta { font-size: 12px; }

.list { flex: 1; overflow-y: auto; padding: 0 14px 20px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  margin-bottom: 10px;
  cursor: pointer;
}
.card:active { background: var(--accent-soft); }
.card-title { font-size: 15px; font-weight: 600; }
.card-desc { font-size: 12px; color: var(--muted); margin-top: 4px; }
.empty, .search-status { padding: 24px 18px; color: var(--muted); font-size: 13px; }

.editor-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.editor-title { flex: 1; font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.editor-actions { display: flex; gap: 8px; }

.btn {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 13px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn:disabled { opacity: .45; cursor: default; }

.editor-status { padding: 6px 16px; font-size: 12px; color: var(--muted); min-height: 28px; }
.editor-status.dirty { color: var(--accent); }

.editor {
  flex: 1;
  width: 100%;
  border: 0;
  outline: 0;
  resize: none;
  padding: 12px 18px 40px;
  background: var(--surface);
  color: var(--ink);
  font-family: ui-monospace, "SFMono-Regular", Consolas, "Microsoft YaHei", monospace;
  font-size: 15px;
  line-height: 1.9;
  tab-size: 2;
}

.preview {
  flex: 1;
  overflow-y: auto;
  padding: 10px 18px 40px;
  background: var(--surface);
  font-size: 16px;
  line-height: 1.9;
}
.preview h2 { font-size: 20px; margin: 28px 0 12px; }
.preview h3 { font-size: 17px; margin: 22px 0 10px; }
.preview p { margin: 0 0 12px; }
.preview hr { border: 0; border-top: 1px solid var(--line); margin: 24px 0; }
.preview blockquote { margin: 12px 0; padding-left: 12px; border-left: 3px solid var(--accent); color: var(--muted); }
.preview code { background: var(--bg); padding: 1px 4px; border-radius: 4px; }

.editor-overlay {
  position: absolute; inset: 0;
  background: var(--bg);
  z-index: 10;
}
.editor-overlay .editor-panel { height: 100%; display: flex; flex-direction: column; }

.search-box { display: flex; gap: 8px; padding: 16px 14px 4px; }
.search-box input {
  flex: 1; min-width: 0;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  outline: 0;
}
.search-box input:focus { border-color: var(--accent); }

.results { flex: 1; overflow-y: auto; padding: 4px 14px 20px; }
.result {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px;
}
.result-line { font-size: 12px; color: var(--accent); margin-bottom: 6px; }
.result-text { font-size: 13px; white-space: pre-wrap; word-break: break-word; color: var(--ink); }
.result mark { background: #f5d9b8; padding: 0 2px; }

.modal {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(43,38,32,.4);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.modal-card {
  width: 100%; max-width: 360px;
  background: var(--surface); border-radius: 14px; padding: 20px;
}
.modal-card h3 { margin: 0 0 8px; font-size: 16px; }
.modal-card p { margin: 0 0 16px; font-size: 13px; color: var(--muted); }
.modal-actions { display: flex; flex-direction: column; gap: 8px; }

.toast {
  position: fixed; left: 50%; bottom: 32px; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: #fff;
  padding: 9px 18px; border-radius: 20px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 40;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (min-width: 720px) {
  body { max-width: 860px; margin: 0 auto; }
  .editor-overlay { position: fixed; }
}
