:root {
  --bg: #f2efe7;
  --panel: #fffdf7;
  --panel-2: #faf7ee;
  --ink: #221e17;
  --ink-soft: #454034;
  --muted: #706957;
  --line: #ddd6c6;
  --line-strong: #c8c0ab;
  --accent: #b23c14;
  --accent-ink: #ffffff;
  --accent-soft: #fbe9df;
  --ok: #1e7d46;
  --ok-soft: #e2f2e7;
  --warn: #9a6a12;
  --warn-soft: #f8eed3;
  --code-bg: #efe9da;
  --shadow: 0 10px 30px rgba(50, 40, 20, 0.12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16140f;
    --panel: #211e18;
    --panel-2: #26221b;
    --ink: #ece6d8;
    --ink-soft: #cfc7b4;
    --muted: #a39b88;
    --line: #38332a;
    --line-strong: #4c4636;
    --accent: #e2703a;
    --accent-ink: #1c130c;
    --accent-soft: #3d2718;
    --ok: #5cc98a;
    --ok-soft: #1d3325;
    --warn: #e0b95c;
    --warn-soft: #38300f;
    --code-bg: #2e2a20;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}
a { color: var(--accent); }
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: var(--ink); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
  z-index: 30;
}
.brand {
  font-family: Georgia, "Songti SC", serif;
  font-size: 19px; font-weight: 700; letter-spacing: 0.2px;
  white-space: nowrap; color: var(--ink); text-decoration: none;
}
.brand .dot { color: var(--accent); }
.brand small {
  display: block; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-weight: 400;
  font-size: 11px; color: var(--muted); letter-spacing: 0.4px;
}
.doc-title {
  flex: 1 1 auto; min-width: 0;
  border: 1px solid transparent; border-radius: 8px;
  background: transparent;
  font-size: 16px; font-weight: 600;
  padding: 7px 10px;
}
.doc-title:hover { border-color: var(--line); }
.doc-title:focus { outline: none; border-color: var(--accent); background: var(--panel-2); }
.save-state { font-size: 12px; color: var(--muted); white-space: nowrap; }
.save-state b { color: var(--ok); font-weight: 600; }
.lang-link { font-size: 13px; color: var(--ink-soft); text-decoration: none; white-space: nowrap; border: 1px solid var(--line); border-radius: 999px; padding: 5px 11px; }
.lang-link:hover { border-color: var(--ink-soft); }
.btn {
  border: 1px solid var(--line-strong);
  background: var(--panel);
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 14px; font-weight: 600;
  white-space: nowrap; text-decoration: none; display: inline-block;
}
.btn:hover { border-color: var(--ink-soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); padding: 9px 20px; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { border-color: transparent; color: var(--ink-soft); }
.btn.ghost:hover { background: var(--panel-2); border-color: var(--line); }
.btn.small { padding: 5px 12px; font-size: 13px; }

/* ---------- app shell ---------- */
.app { height: 100vh; height: 100svh; min-height: 560px; display: flex; flex-direction: column; }

/* ---------- toolbar ---------- */
.toolbar {
  display: flex; align-items: center; gap: 2px; flex-wrap: wrap;
  padding: 6px 12px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
  z-index: 25;
  position: relative;
}
.toolbar select { border: 1px solid var(--line); border-radius: 7px; background: var(--panel); padding: 5px 6px; font-size: 13px; margin-right: 6px; }
.tbtn {
  border: 1px solid transparent; background: transparent; color: var(--ink-soft);
  border-radius: 7px; min-width: 34px; height: 32px; padding: 0 7px;
  font-size: 14px; display: inline-flex; align-items: center; justify-content: center; gap: 4px;
}
.tbtn:hover { background: var(--panel); border-color: var(--line); }
.tbtn b { font-weight: 800; }
.tbtn i { font-style: italic; font-family: Georgia, serif; }
.tbtn code { font-family: ui-monospace, Menlo, monospace; font-size: 12px; background: var(--code-bg); padding: 1px 4px; border-radius: 4px; }
.tsep { width: 1px; height: 22px; background: var(--line); margin: 0 6px; }
.toolbar .spacer { flex: 1; }
.wordcount { font-size: 12px; color: var(--muted); white-space: nowrap; margin-right: 6px; }
.tbtn mark { background: #ffe58a; color: #3a2f00; padding: 0 3px; border-radius: 3px; }
.notes-btn { font-weight: 600; color: var(--ink); border-color: var(--line); background: var(--panel); }
.notes-btn .count { font-size: 11px; font-weight: 700; background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 1px 7px; }
.notes-pop { width: min(340px, calc(100vw - 24px)); }
.notes-pop .row.first { justify-content: flex-start; margin-top: 0; }
.notes-pop .hint { font-size: 12px; color: var(--muted); margin: 10px 0 0; line-height: 1.5; }
.notes-list { list-style: none; margin: 10px 0 0; padding: 0; max-height: min(320px, 50vh); overflow-y: auto; border-top: 1px solid var(--line); }
.notes-list li { display: flex; align-items: center; border-bottom: 1px solid var(--line); }
.notes-list li.on { background: var(--accent-soft); }
.note-open { flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; padding: 8px 6px; color: var(--ink); display: flex; gap: 8px; align-items: baseline; cursor: pointer; }
.note-open span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.note-open small { color: var(--muted); font-size: 11.5px; white-space: nowrap; }
.note-del { border: 0; background: transparent; color: var(--muted); font-size: 18px; width: 30px; height: 30px; border-radius: 6px; cursor: pointer; }
.note-del:hover { color: #c0392b; background: var(--panel-2); }
.drawer-head .flavor select { border: 1px solid var(--line); border-radius: 7px; background: var(--panel); padding: 5px 6px; font-size: 13px; }

.popover {
  position: absolute; top: calc(100% + 4px); left: 12px;
  background: var(--panel); border: 1px solid var(--line-strong);
  border-radius: 10px; box-shadow: var(--shadow);
  padding: 12px; width: min(360px, calc(100vw - 24px));
  display: none; z-index: 60;
}
.popover.open { display: block; }
.popover label { display: block; font-size: 12px; color: var(--muted); margin: 8px 0 4px; }
.popover label:first-child { margin-top: 0; }
.popover input { width: 100%; border: 1px solid var(--line); border-radius: 7px; background: var(--panel-2); padding: 7px 9px; font-size: 14px; }
.popover .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }

/* ---------- layout ---------- */
.layout { flex: 1 1 auto; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 348px; }
.editor-scroll { overflow-y: auto; padding: 28px 20px 90px; }
.paper {
  max-width: 780px; margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 2px 14px rgba(50, 40, 20, 0.06);
  padding: 52px 60px 64px;
  min-height: 100%;
}
.editor {
  outline: none;
  font-family: Georgia, "Songti SC", "Noto Serif SC", "SimSun", serif;
  font-size: 18px; line-height: 1.85; color: var(--ink);
  caret-color: var(--accent);
  overflow-wrap: anywhere;
}
.editor:lang(zh-TW) { font-family: Georgia, "Songti TC", "Noto Serif TC", "PMingLiU", serif; }
.editor:empty::before { content: attr(data-placeholder); color: var(--muted); }
.editor h1 { font-size: 32px; line-height: 1.3; margin: 0.2em 0 0.6em; font-weight: 700; }
.editor h2 { font-size: 25px; line-height: 1.35; margin: 1.3em 0 0.5em; font-weight: 700; }
.editor h3 { font-size: 21px; line-height: 1.4; margin: 1.2em 0 0.4em; font-weight: 700; }
.editor h4, .editor h5, .editor h6 { font-size: 18px; margin: 1.1em 0 0.4em; }
.editor p { margin: 0.55em 0; }
.editor a { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
.editor code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.86em; background: var(--code-bg); padding: 2px 6px; border-radius: 5px; }
.editor pre { background: var(--code-bg); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; overflow-x: auto; margin: 0.9em 0; white-space: pre; }
.editor pre code { background: transparent; padding: 0; font-size: 14px; line-height: 1.6; }
.editor blockquote { margin: 0.9em 0; padding: 4px 0 4px 18px; border-left: 3px solid var(--accent); color: var(--ink-soft); font-style: italic; }
.editor img { max-width: 100%; height: auto; border-radius: 6px; border: 1px solid var(--line); }
.editor ul, .editor ol { margin: 0.5em 0; padding-left: 1.4em; }
.editor li { margin: 0.25em 0; }
.editor table { border-collapse: collapse; margin: 0.9em 0; font-size: 15px; width: 100%; }
.editor th, .editor td { border: 1px solid var(--line-strong); padding: 7px 10px; text-align: left; }
.editor th { background: var(--panel-2); }
.editor mark { background: #ffe58a; color: #3a2f00; padding: 0 2px; border-radius: 3px; }
.editor li[data-task] { list-style: none; position: relative; }
.editor li[data-task]::before {
  content: ""; position: absolute; left: -1.35em; top: 0.42em;
  width: 0.95em; height: 0.95em; box-sizing: border-box;
  border: 2px solid var(--line-strong); border-radius: 4px; background: var(--panel); cursor: pointer;
}
.editor li[data-task="1"]::before { background: var(--accent); border-color: var(--accent); }
.editor li[data-task="1"]::after {
  content: ""; position: absolute; left: -1.05em; top: 0.55em; width: 0.28em; height: 0.52em;
  border: solid var(--accent-ink); border-width: 0 2px 2px 0; transform: rotate(45deg); pointer-events: none;
}
.editor li[data-task="1"] { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--line-strong); }
.editor hr { border: none; border-top: 1px solid var(--line-strong); margin: 1.4em 0; }

/* ---------- sidebar ---------- */
.sidebar { border-left: 1px solid var(--line); background: var(--panel-2); overflow-y: auto; padding: 18px 16px 60px; display: flex; flex-direction: column; gap: 26px; }
.side-sec h2 { margin: 0 0 4px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.side-sec h2 .tag { font-size: 11px; font-weight: 600; color: var(--accent); background: var(--accent-soft); border-radius: 999px; padding: 2px 8px; }
.side-sec .hint { font-size: 12.5px; color: var(--muted); line-height: 1.6; margin: 0 0 12px; }
.clip { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 13px; margin-bottom: 10px; }
.clip h3 { margin: 0 0 5px; font-size: 14px; }
.clip p { margin: 0 0 8px; font-size: 13px; line-height: 1.65; color: var(--ink-soft); }
.clip .src { font-size: 12px; color: var(--muted); word-break: break-all; margin-bottom: 9px; }
.clip .actions { display: flex; gap: 8px; }
.mini-btn { border: 1px solid var(--line-strong); background: var(--panel); border-radius: 999px; padding: 4px 11px; font-size: 12.5px; font-weight: 600; color: var(--ink); }
.mini-btn:hover { border-color: var(--accent); color: var(--accent); }
.mini-btn.danger:hover { border-color: #c0392b; color: #c0392b; }
.add-form { background: var(--panel); border: 1px dashed var(--line-strong); border-radius: 10px; padding: 12px 13px; }
.add-form label { display: block; font-size: 12px; color: var(--muted); margin: 8px 0 4px; }
.add-form label:first-child { margin-top: 0; }
.add-form input, .add-form textarea { width: 100%; border: 1px solid var(--line); border-radius: 7px; background: var(--panel-2); padding: 7px 9px; font-size: 13.5px; }
.add-form textarea { min-height: 64px; resize: vertical; }
.add-form .row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

.fidelity-summary { border-radius: 10px; padding: 12px 13px; margin-bottom: 10px; background: var(--ok-soft); border: 1px solid var(--ok); font-size: 13.5px; line-height: 1.6; }
.fidelity-summary b { font-size: 15px; }
.check-row { display: flex; align-items: center; gap: 9px; padding: 7px 2px; border-bottom: 1px solid var(--line); font-size: 13.5px; }
.check-row:last-child { border-bottom: none; }
.check-row .st { flex: 0 0 auto; width: 20px; height: 20px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; }
.st.pass { background: var(--ok); color: #fff; }
.st.unused { background: var(--line); color: var(--muted); }
.st.noguarantee { background: var(--warn-soft); color: var(--warn); border: 1px solid var(--warn); }
.check-row .lbl { flex: 1; }
.check-row .note { font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.check-row.is-unused .lbl { color: var(--muted); }
.side-more { font-size: 13px; color: var(--muted); }

/* ---------- md preview drawer ---------- */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(20, 16, 8, 0.42); display: none; z-index: 80; }
.drawer-backdrop.open { display: block; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: min(560px, 100vw);
  background: var(--panel); z-index: 90;
  box-shadow: var(--shadow);
  transform: translateX(102%);
  visibility: hidden;
  transition: transform 0.22s ease, visibility 0s linear 0.22s;
  display: flex; flex-direction: column;
}
.drawer.open { transform: translateX(0); visibility: visible; transition: transform 0.22s ease; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: 16px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer pre { flex: 1; overflow: auto; margin: 0; padding: 18px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13.5px; line-height: 1.7; white-space: pre-wrap; word-break: break-word; color: var(--ink); }
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%) translateY(16px);
  background: var(--ink); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: all 0.2s ease; z-index: 120;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- content pages / landing ---------- */
.site { max-width: 860px; margin: 0 auto; padding: 48px 16px 24px; line-height: 1.75; font-size: 17px; }
.site h1 { font-family: Georgia, "Songti SC", serif; font-size: 36px; line-height: 1.25; margin: 0 0 14px; }
.site h2 { font-family: Georgia, "Songti SC", serif; font-size: 26px; line-height: 1.3; margin: 44px 0 12px; }
.site h3 { font-size: 18px; margin: 26px 0 6px; }
.site p, .site li { color: var(--ink-soft); }
.site .lede { font-size: 19px; color: var(--ink-soft); }
.site .updated { font-size: 13px; color: var(--muted); margin: -6px 0 10px; }
.site .tldr { background: var(--accent-soft); border-left: 4px solid var(--accent); border-radius: 8px; padding: 14px 18px; margin: 18px 0 8px; }
.site .tldr ul { margin: 8px 0 0; padding-left: 1.2em; }
.site .tldr li { margin: 4px 0; }
.site code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.88em; background: var(--code-bg); padding: 2px 6px; border-radius: 5px; }
.site pre { background: var(--code-bg); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; overflow-x: auto; font-size: 14px; line-height: 1.6; }
.site pre code { background: none; padding: 0; }
.site table { border-collapse: collapse; width: 100%; font-size: 15px; margin: 14px 0; display: block; overflow-x: auto; }
.site th, .site td { border: 1px solid var(--line-strong); padding: 8px 11px; text-align: left; vertical-align: top; }
.site th { background: var(--panel-2); }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 14px; margin: 18px 0; padding: 0; list-style: none; }
.cards li { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; color: var(--ink-soft); }
.cards li b { display: block; color: var(--ink); margin-bottom: 4px; font-size: 16px; }
.cards a { text-decoration: none; }
.cta { display: flex; gap: 12px; flex-wrap: wrap; margin: 22px 0; }
.faq h3 { margin-top: 22px; }
.site-header { display: flex; align-items: center; gap: 14px; padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.site-header nav { margin-left: auto; display: flex; gap: 14px; align-items: center; flex-wrap: wrap; font-size: 14px; }
.site-header nav a { color: var(--ink-soft); text-decoration: none; }
.site-header nav a:hover { color: var(--accent); }
.crumbs { font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.crumbs a { color: var(--muted); }
.footer { border-top: 1px solid var(--line); margin-top: 40px; padding: 26px 16px 40px; font-size: 14px; color: var(--muted); }
.footer .inner { max-width: 860px; margin: 0 auto; display: flex; flex-wrap: wrap; gap: 10px 20px; }
.footer a { color: var(--muted); }
.footer .copy { flex-basis: 100%; }

/* ---------- mobile ---------- */
.mobile-tabs { display: none; }
@media (max-width: 920px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { border-left: none; border-top: 1px solid var(--line); display: none; }
  body.show-side .sidebar { display: flex; }
  body.show-side .editor-scroll { display: none; }
  .editor-scroll { padding: 16px 10px 60px; }
  .paper { padding: 26px 18px 40px; }
  .editor { font-size: 17px; }
  .brand small { display: none; }
  .save-state { display: none; }
  .topbar { flex-wrap: wrap; row-gap: 8px; padding: 10px 12px; gap: 10px; }
  .brand { font-size: 17px; }
  .doc-title { order: 3; flex: 1 1 52%; font-size: 15px; }
  .wordcount { display: none; }
  #btnPreview { order: 4; }
  .lang-link { margin-left: auto; }
  .mobile-tabs { display: flex; gap: 6px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex: 0 0 auto; }
  .mobile-tabs button { flex: 1; border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px; padding: 7px 0; font-size: 14px; font-weight: 600; color: var(--ink-soft); }
  .mobile-tabs button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
  .btn.primary { padding: 8px 14px; }
  .site h1 { font-size: 29px; }
  .site h2 { font-size: 23px; }
}
@media (prefers-reduced-motion: reduce) {
  .drawer, .toast { transition: none; }
}
