:root {
  --bg: #f6f3ee;
  --side: #efeae2;
  --paper: #fffdf9;
  --text: #24211d;
  --muted: #8a8278;
  --line: #e4ddd2;
  --line-strong: #d3cabc;
  --hover: rgba(60, 45, 30, 0.06);
  --active: rgba(60, 45, 30, 0.1);
  --accent: #b4532a;
  --accent-ink: #fff;
  --accent-soft: rgba(180, 83, 42, 0.1);
  --quote: #f3ece2;
  --shadow: 0 1px 2px rgba(40, 30, 20, 0.04), 0 8px 30px rgba(40, 30, 20, 0.06);
  --serif: "Literata", Georgia, "Times New Roman", serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #171513;
  --side: #1d1b18;
  --paper: #201e1b;
  --text: #ebe6de;
  --muted: #8f877c;
  --line: #2f2b27;
  --line-strong: #3d3833;
  --hover: rgba(255, 245, 230, 0.05);
  --active: rgba(255, 245, 230, 0.09);
  --accent: #e07a4b;
  --accent-ink: #1a1310;
  --accent-soft: rgba(224, 122, 75, 0.14);
  --quote: #26231f;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.25);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171513;
    --side: #1d1b18;
    --paper: #201e1b;
    --text: #ebe6de;
    --muted: #8f877c;
    --line: #2f2b27;
    --line-strong: #3d3833;
    --hover: rgba(255, 245, 230, 0.05);
    --active: rgba(255, 245, 230, 0.09);
    --accent: #e07a4b;
    --accent-ink: #1a1310;
    --accent-soft: rgba(224, 122, 75, 0.14);
    --quote: #26231f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-family: var(--sans); font-size: 14px; -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input { font: inherit; color: inherit; }
svg { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

.layout { display: flex; height: 100vh; height: 100dvh; }

/* ---------- sidebar ---------- */
.sidebar {
  width: 272px; flex: none; display: flex; flex-direction: column; gap: 10px;
  background: var(--side); border-right: 1px solid var(--line); padding: 16px 12px 12px;
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 6px 6px; }
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--serif); font-size: 19px; font-weight: 600; letter-spacing: -0.01em; }
.logo { width: 22px; height: 22px; border-radius: 6px; background: var(--accent); position: relative; }
.logo::after { content: ""; position: absolute; left: 6px; right: 6px; top: 7px; height: 2px; background: var(--accent-ink); box-shadow: 0 4px 0 var(--accent-ink); border-radius: 2px; }
.new-btn {
  display: flex; align-items: center; justify-content: center; gap: 8px; height: 38px; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink); font-weight: 500;
  transition: transform 0.15s var(--ease), filter 0.2s;
}
.new-btn:hover { filter: brightness(1.05); }
.new-btn:active { transform: scale(0.98); }
.new-btn svg { width: 16px; height: 16px; stroke-width: 2; }
.new-btn.inline { padding: 0 18px; margin-top: 18px; }
.search { position: relative; }
.search svg { position: absolute; left: 10px; top: 50%; width: 15px; height: 15px; transform: translateY(-50%); color: var(--muted); }
.search input {
  width: 100%; height: 34px; border-radius: 9px; border: 1px solid var(--line); background: var(--paper);
  padding: 0 10px 0 32px; outline: none; transition: border-color 0.2s;
}
.search input:focus { border-color: var(--line-strong); }
.pages { flex: 1; overflow-y: auto; margin: 4px -4px 0; padding: 0 4px; }
.page-item {
  display: flex; flex-direction: column; gap: 2px; width: 100%; text-align: left; border-radius: 9px;
  padding: 9px 10px; transition: background 0.15s;
}
.page-item:hover { background: var(--hover); }
.page-item.active { background: var(--active); }
.page-item .t { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.page-item .t.untitled { color: var(--muted); font-weight: 400; }
.page-item .s { font-size: 12px; color: var(--muted); }
.pages .none { color: var(--muted); padding: 12px 10px; font-size: 13px; }
.side-foot { display: flex; gap: 4px; border-top: 1px solid var(--line); padding-top: 10px; }
.ghost-btn {
  display: inline-flex; align-items: center; gap: 7px; height: 34px; padding: 0 10px; border-radius: 9px;
  color: var(--muted); transition: background 0.15s, color 0.15s; white-space: nowrap;
}
.ghost-btn:hover { background: var(--hover); color: var(--text); }
.ghost-btn svg { width: 16px; height: 16px; }
.i-moon { display: none; }
:root[data-theme="dark"] .i-sun { display: none; }
:root[data-theme="dark"] .i-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: none; }
  :root:not([data-theme="light"]) .i-moon { display: block; }
}

/* ---------- main ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 10px; min-height: 56px; padding: 8px 16px;
  border-bottom: 1px solid var(--line); background: var(--bg); position: relative; z-index: 3;
}
.icon-btn { width: 36px; height: 36px; flex: none; display: grid; place-items: center; border-radius: 9px; color: var(--muted); transition: background 0.15s, color 0.15s; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn svg { width: 19px; height: 19px; }
.toolbar { display: flex; align-items: center; gap: 2px; overflow-x: auto; scrollbar-width: none; min-width: 0; }
.toolbar::-webkit-scrollbar { display: none; }
.toolbar button {
  min-width: 32px; height: 32px; flex: none; padding: 0 7px; border-radius: 8px; color: var(--muted);
  font-size: 13px; font-weight: 600; display: grid; place-items: center; transition: background 0.15s, color 0.15s;
}
.toolbar button:hover { background: var(--hover); color: var(--text); }
.toolbar button.on { background: var(--active); color: var(--text); }
.toolbar button svg { width: 17px; height: 17px; }
.toolbar button b, .toolbar button i, .toolbar button u, .toolbar button s { font-family: var(--serif); font-size: 15px; }
.toolbar .sep { width: 1px; height: 18px; background: var(--line-strong); margin: 0 5px; flex: none; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 6px; flex: none; position: relative; }
.status { color: var(--muted); font-size: 12px; white-space: nowrap; margin-right: 4px; }
.status.err { color: #c0392b; }
.ai-btn {
  display: inline-flex; align-items: center; gap: 8px; height: 36px; padding: 0 14px; border-radius: 10px;
  background: var(--text); color: var(--bg); font-weight: 500; white-space: nowrap;
  transition: transform 0.15s var(--ease), opacity 0.2s;
}
.ai-btn:hover { opacity: 0.9; }
.ai-btn:active { transform: scale(0.98); }
.ai-btn:disabled { opacity: 0.4; pointer-events: none; }
.ai-btn svg { width: 17px; height: 17px; }
.menu {
  position: absolute; right: 0; top: 44px; min-width: 220px; background: var(--paper); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow); padding: 6px; z-index: 10;
}
.menu button { display: block; width: 100%; text-align: left; padding: 9px 10px; border-radius: 8px; }
.menu button:hover { background: var(--hover); }
.menu .danger { color: #c0392b; }

/* ---------- paper / editor ---------- */
.paper-wrap { flex: 1; overflow-y: auto; position: relative; padding: 40px 24px 120px; }
.paper {
  max-width: 760px; margin: 0 auto; background: var(--paper); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: var(--shadow); padding: 56px 72px 80px; min-height: calc(100% - 20px);
}
.title {
  width: 100%; border: 0; outline: none; background: transparent; font-family: var(--serif);
  font-size: 34px; font-weight: 700; letter-spacing: -0.015em; line-height: 1.2; padding: 0;
}
.title::placeholder { color: var(--line-strong); }
.meta { color: var(--muted); font-size: 12.5px; margin: 10px 0 30px; padding-bottom: 22px; border-bottom: 1px solid var(--line); }
.editor {
  outline: none; font-family: var(--serif); font-size: 18px; line-height: 1.75; min-height: 50vh;
  word-wrap: break-word; caret-color: var(--accent);
}
.editor:empty::before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; }
.editor > *:first-child { margin-top: 0; }
.editor p { margin: 0 0 1em; }
.editor h1 { font-size: 30px; line-height: 1.25; font-weight: 700; letter-spacing: -0.015em; margin: 1.4em 0 0.55em; }
.editor h2 { font-size: 23px; line-height: 1.3; font-weight: 700; letter-spacing: -0.01em; margin: 1.6em 0 0.5em; }
.editor h3 {
  font-family: var(--sans); font-size: 13px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--accent); margin: 1.8em 0 0.6em;
}
.editor ul, .editor ol { margin: 0 0 1em; padding-left: 1.4em; }
.editor li { margin: 0.25em 0; padding-left: 0.2em; }
.editor li::marker { color: var(--accent); }
.editor blockquote {
  margin: 1.2em 0; padding: 14px 20px; border-left: 3px solid var(--accent); background: var(--quote);
  border-radius: 0 10px 10px 0; font-style: italic;
}
.editor blockquote p:last-child { margin-bottom: 0; }
.editor strong { font-weight: 700; }
.editor em { font-style: italic; }
.editor hr { border: 0; height: 1px; background: var(--line-strong); margin: 2.2em auto; width: 40%; position: relative; overflow: visible; }
.editor[contenteditable="false"] { opacity: 0.55; }
::selection { background: var(--accent-soft); }
.editor.flash { animation: flash 1.2s var(--ease); }
@keyframes flash { 0% { background: var(--accent-soft); } 100% { background: transparent; } }

/* ---------- empty state ---------- */
.empty { max-width: 420px; margin: 14vh auto 0; text-align: center; color: var(--muted); }
.empty h2 { font-family: var(--serif); color: var(--text); font-size: 26px; margin: 20px 0 8px; }
.empty p { margin: 0; line-height: 1.6; }
.empty .new-btn { display: inline-flex; }
.empty-mark { width: 54px; height: 54px; margin: 0 auto; border-radius: 14px; border: 1.5px dashed var(--line-strong); position: relative; }
.empty-mark::after { content: ""; position: absolute; left: 15px; right: 15px; top: 19px; height: 2px; background: var(--line-strong); box-shadow: 0 7px 0 var(--line-strong), 0 14px 0 var(--line-strong); border-radius: 2px; }

/* ---------- AI overlay ---------- */
.ai-overlay { position: absolute; inset: 0; display: grid; place-items: start center; padding-top: 18vh; background: color-mix(in srgb, var(--bg) 40%, transparent); backdrop-filter: blur(2px); z-index: 2; }
.ai-card { background: var(--paper); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 26px 30px; text-align: center; max-width: 340px; margin: 0 16px; }
.ai-title { font-family: var(--serif); font-size: 19px; font-weight: 600; margin-top: 16px; }
.ai-step { color: var(--accent); font-size: 13px; margin-top: 6px; }
.ai-note { color: var(--muted); font-size: 12.5px; line-height: 1.5; margin-top: 12px; }
.spinner { width: 30px; height: 30px; margin: 0 auto; border-radius: 50%; border: 2.5px solid var(--line); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 11px 16px; border-radius: 11px; box-shadow: var(--shadow); font-size: 13.5px; z-index: 50; max-width: calc(100vw - 32px);
  animation: rise 0.25s var(--ease);
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } }

.only-mobile { display: none; }
.backdrop { display: none; }

@media (max-width: 860px) {
  .only-mobile { display: grid; }
  .sidebar {
    position: fixed; z-index: 30; left: 0; top: 0; bottom: 0; transform: translateX(-100%);
    transition: transform 0.25s var(--ease); box-shadow: var(--shadow);
  }
  .layout.side-open .sidebar { transform: none; }
  .layout.side-open .backdrop { display: block; position: fixed; inset: 0; z-index: 25; background: rgba(0, 0, 0, 0.3); }
  .topbar { padding: 8px 10px; flex-wrap: wrap; }
  .toolbar { order: 3; width: 100%; padding-top: 4px; }
  .ai-btn span, .ghost-btn#undoAi span { display: none; }
  .ai-btn { padding: 0 11px; }
  .paper-wrap { padding: 16px 0 100px; }
  .paper { border-radius: 0; border-left: 0; border-right: 0; padding: 30px 18px 60px; box-shadow: none; }
  .title { font-size: 27px; }
  .editor { font-size: 17px; }
}

@media print {
  .sidebar, .topbar, .backdrop, .toast, .ai-overlay { display: none !important; }
  .layout, .main, .paper-wrap { display: block; height: auto; overflow: visible; padding: 0; }
  body { background: #fff; color: #000; }
  .paper { border: 0; box-shadow: none; padding: 0; max-width: none; }
  .meta { display: none; }
}
