:root {
  --bg: #f7f6f3;
  --panel: #efede8;
  --border: #dedad2;
  --text: #24232a;
  --muted: #7a7783;
  --accent: #7c5cdb;
  --accent-soft: rgba(124, 92, 219, .12);
  --selection: rgba(124, 92, 219, .22);
  --match: rgba(255, 196, 0, .35);
  --code: #b0456a;
  --code-bg: rgba(0, 0, 0, .05);
  --warn: #b5590f;
  --ok: #2f8a4f;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --editor-size: 17px;
  --sidebar-w: 290px;
  --top-h: 48px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1e24;
    --panel: #26262e;
    --border: #36363f;
    --text: #dcdbe2;
    --muted: #8d8b98;
    --accent: #a58bff;
    --accent-soft: rgba(165, 139, 255, .14);
    --selection: rgba(165, 139, 255, .28);
    --match: rgba(255, 196, 0, .25);
    --code: #f08cb0;
    --code-bg: rgba(255, 255, 255, .06);
    --warn: #f0a050;
    --ok: #6fcf8f;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button {
  font: inherit; color: inherit; background: var(--panel);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.icon { background: none; border: none; font-size: 20px; padding: 6px 10px; min-width: 40px; min-height: 40px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.pad { padding: 12px; }

/* ---------- login ---------- */
#login { display: grid; place-items: center; height: 100%; padding: 16px; }
#login-form { display: flex; flex-direction: column; gap: 12px; width: min(320px, 100%); text-align: center; align-items: center; }
#login-form h1 { margin: 0 0 8px; font-weight: 600; }
#login-form img { border-radius: 18px; }
#login-form input, #login-form button { width: 100%; padding: 12px; font-size: 16px; border-radius: 10px; }
#login-form input { border: 1px solid var(--border); background: var(--panel); color: var(--text); }
#login-form button { background: var(--accent); color: white; border: none; }
#login-error { color: var(--warn); min-height: 1.2em; margin: 0; }

/* ---------- layout ---------- */
#app { display: flex; height: 100dvh; overflow: hidden; }
#sidebar {
  width: var(--sidebar-w); flex: none; display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--border);
  margin-left: calc(-1 * var(--sidebar-w)); transition: margin-left .18s ease;
  padding-top: env(safe-area-inset-top);
}
#app.sidebar-open #sidebar { margin-left: 0; }
#scrim { display: none; }
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

@media (max-width: 800px) {
  #sidebar { position: fixed; z-index: 20; top: 0; bottom: 0; left: 0; width: min(86vw, 340px); margin-left: 0; transform: translateX(-100%); transition: transform .2s ease, visibility .2s; visibility: hidden; }
  #app.sidebar-open #sidebar { transform: none; visibility: visible; box-shadow: 0 0 30px rgba(0,0,0,.25); }
  #app.sidebar-open #scrim { display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0,0,0,.35); }
}

.side-head { padding: 10px 10px 6px; }
#search {
  width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 16px;
}
.side-actions { display: flex; gap: 6px; padding: 4px 10px 8px; }
.side-actions button { flex: 1; font-size: 13px; padding: 7px 4px; background: var(--bg); }
.side-scroll { flex: 1; overflow-y: auto; padding: 0 4px 20px; -webkit-overflow-scrolling: touch; }
.side-foot { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); font-size: 12px; }
body:not(.disconnected) #devices { color: var(--ok); }
body.disconnected #devices { color: var(--warn); }
body.disconnected #devices::after { content: ' offline'; }

#sidebar h4 { margin: 12px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.tree-item {
  display: flex; align-items: center; gap: 4px; width: 100%;
  padding: 6px 8px 6px calc(10px + var(--depth, 0) * 14px);
  border-radius: 6px; color: var(--text); text-decoration: none; cursor: pointer;
  font-size: 14px; line-height: 1.3; user-select: none;
}
.tree-item:hover { background: var(--accent-soft); }
.tree-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 500; }
.tree-item.folder { font-weight: 500; }
.tree-item.folder.selected .label { text-decoration: underline; text-decoration-color: var(--accent); text-underline-offset: 3px; }
.tree-item .chev { width: 12px; color: var(--muted); font-size: 11px; flex: none; }
.tree-item.note { padding-left: calc(26px + var(--depth, 0) * 14px); }
.tree-item .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (pointer: coarse) { .tree-item { padding-top: 10px; padding-bottom: 10px; font-size: 15px; } }

.result { display: block; padding: 8px 10px; border-radius: 6px; text-decoration: none; color: var(--text); }
.result:hover { background: var(--accent-soft); }
.result strong { display: block; font-size: 14px; font-weight: 500; }
.result span { display: block; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- top bar ---------- */
#topbar {
  position: relative; display: flex; align-items: center; gap: 4px;
  min-height: calc(var(--top-h) + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 6px 0;
  border-bottom: 1px solid var(--border); background: var(--bg);
}
#title { flex: 1; min-width: 0; margin: 0 4px; font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: text; }
#status { font-size: 12px; color: var(--muted); white-space: nowrap; transition: color .3s; }
#status[data-state="saved"] { color: var(--ok); }
#status[data-state="offline"], #status[data-state="conflict"], #status[data-state="error"] { color: var(--warn); font-weight: 600; }
#status.pulse { color: var(--accent); }
#menu {
  position: absolute; right: 8px; top: calc(100% + 4px); z-index: 30;
  display: flex; flex-direction: column; min-width: 190px; padding: 4px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.18);
}
#menu button { border: none; background: none; text-align: left; padding: 10px 12px; }
#menu button:hover { background: var(--accent-soft); }
#menu [data-action="delete"] { color: var(--warn); }

#banner { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 8px 12px; font-size: 14px; border-bottom: 1px solid var(--border); }
#banner[data-kind="warn"] { background: rgba(240, 160, 80, .15); }
#banner[data-kind="info"] { background: var(--accent-soft); }
.banner-msg { flex: 1 1 220px; }
.banner-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.banner-actions button { font-size: 13px; padding: 5px 10px; }

/* ---------- content ---------- */
#content { flex: 1; overflow: hidden; position: relative; display: flex; flex-direction: column; }
#editor { flex: 1; min-height: 0; }
#editor .cm-editor { height: 100%; }
#preview { flex: none; overflow-y: auto; -webkit-overflow-scrolling: touch; }
#content:has(#preview:not([hidden])) { overflow-y: auto; display: block; }
#empty { margin: auto; text-align: center; color: var(--muted); padding: 24px; }
#empty button { background: var(--accent); color: white; border: none; padding: 10px 16px; border-radius: 10px; }

#backlinks { max-width: 760px; margin: 0 auto; width: 100%; padding: 12px 16px calc(40px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
#editor:not([hidden]) ~ #backlinks { display: none; }
#backlinks h4 { margin: 4px 0 8px; font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: .05em; }
.backlink { display: block; padding: 6px 0; text-decoration: none; color: var(--text); }
.backlink strong { color: var(--accent); font-weight: 500; margin-right: 8px; }
.backlink span { color: var(--muted); font-size: 13px; }

/* ---------- rendered markdown ---------- */
.markdown { max-width: 760px; margin: 0 auto; padding: 16px 16px 24px; font-size: var(--editor-size); line-height: 1.65; overflow-wrap: anywhere; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4 { line-height: 1.3; margin: 1.3em 0 .5em; }
.markdown h1 { font-size: 1.6em; } .markdown h2 { font-size: 1.35em; } .markdown h3 { font-size: 1.15em; }
.markdown > :first-child { margin-top: 0; }
.markdown a { color: var(--accent); text-decoration: none; }
.markdown a:hover { text-decoration: underline; }
.markdown a.unresolved { opacity: .6; text-decoration: underline dotted; }
.markdown a.tag { background: var(--accent-soft); padding: 1px 6px; border-radius: 10px; font-size: .9em; }
.markdown code { font-family: var(--mono); font-size: .88em; background: var(--code-bg); color: var(--code); padding: 1px 5px; border-radius: 4px; }
.markdown pre { background: var(--code-bg); padding: 12px; border-radius: 8px; overflow-x: auto; }
.markdown pre code { background: none; color: var(--text); padding: 0; }
.markdown blockquote { margin: 1em 0; padding: 4px 14px; border-left: 3px solid var(--accent); color: var(--muted); }
.markdown hr { border: none; border-top: 1px solid var(--border); margin: 1.6em 0; }
.markdown img { max-width: 100%; height: auto; border-radius: 6px; }
.markdown table { border-collapse: collapse; display: block; overflow-x: auto; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 10px; }
.markdown ul, .markdown ol { padding-left: 1.5em; }
.markdown li.task-item { list-style: none; margin-left: -1.3em; }
.markdown li.task-item input { width: 18px; height: 18px; margin: 0 8px 0 0; vertical-align: -3px; accent-color: var(--accent); cursor: pointer; }
.markdown li.task-item:has(> input:checked) { color: var(--muted); text-decoration: line-through; }
.markdown .callout { border-left-width: 4px; background: var(--accent-soft); border-radius: 6px; color: var(--text); padding: 8px 14px; }
.markdown .callout-title { font-weight: 600; margin-bottom: 4px; }
.markdown .callout-warning, .markdown .callout-caution, .markdown .callout-danger { border-left-color: var(--warn); background: rgba(240, 160, 80, .12); }
.markdown .callout-tip, .markdown .callout-success { border-left-color: var(--ok); background: rgba(80, 180, 110, .12); }
.frontmatter { font-size: 13px; border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; margin-bottom: 16px; background: var(--panel); }
.fm-row { display: flex; gap: 10px; }
.fm-key { color: var(--muted); min-width: 90px; }
@media (prefers-color-scheme: dark) { .markdown font[color] { filter: brightness(1.6) saturate(.9); } }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; transition: all .25s; z-index: 50; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
