:root {
  --bg: #16161a;
  --bg2: #1e1e24;
  --bg3: #26262e;
  --fg: #e6e6ea;
  --muted: #9a9aa6;
  --accent: #a07bff;
  --accent2: #7c5cff;
  --border: #2e2e38;
  --danger: #ff6b6b;
  --ok: #5ad19a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f6f8; --bg2: #fff; --bg3: #ececed; --fg: #1c1c22;
    --muted: #6b6b76; --border: #e0e0e6;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }

/* ---- login ---- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 16px;
  padding: 28px 22px; width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px;
}
.login-card h1 { margin: 0 0 8px; font-size: 22px; text-align: center; }
.login-card input, .q-title, #searchInput {
  background: var(--bg3); border: 1px solid var(--border); color: var(--fg);
  border-radius: 10px; padding: 13px 14px; font-size: 16px; outline: none;
}
.login-card input:focus, .q-title:focus, #searchInput:focus { border-color: var(--accent); }
.login-card button {
  background: var(--accent2); color: #fff; border: 0; border-radius: 10px;
  padding: 13px; font-size: 16px; font-weight: 600; margin-top: 4px;
}
.err { color: var(--danger); font-size: 14px; text-align: center; min-height: 18px; }

/* ---- top bar ---- */
.bar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 6px;
  padding: calc(8px + var(--safe-top)) 10px 8px; background: var(--bg2);
  border-bottom: 1px solid var(--border);
}
.crumb { flex: 1; font-weight: 600; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon {
  background: transparent; border: 0; color: var(--fg); font-size: 20px;
  width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center;
}
.icon:active { background: var(--bg3); }
.icon.add { font-size: 26px; color: var(--accent); }
#backBtn { font-size: 30px; line-height: 1; }

/* ---- tree / lists ---- */
main { padding: 6px 0 calc(24px + var(--safe-bot)); }
.row {
  display: flex; align-items: center; gap: 10px; padding: 13px 16px;
  border-bottom: 1px solid var(--border); user-select: none;
}
.row:active { background: var(--bg3); }
.row .ic { width: 22px; text-align: center; flex-shrink: 0; color: var(--muted); }
.row .nm { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row.dir .nm { font-weight: 500; }
.row .chev { color: var(--muted); font-size: 13px; }
.children { margin-left: 14px; border-left: 1px solid var(--border); }
.empty { color: var(--muted); padding: 20px 16px; font-size: 14px; }

/* ---- note view ---- */
.note { padding: 16px 18px calc(40px + var(--safe-bot)); max-width: 760px; margin: 0 auto; }
.note h1, .note h2, .note h3 { line-height: 1.25; }
.note h1 { font-size: 26px; } .note h2 { font-size: 21px; } .note h3 { font-size: 18px; }
.note img, .note .embed { max-width: 100%; border-radius: 8px; height: auto; }
.note pre { background: var(--bg3); padding: 12px; border-radius: 8px; overflow-x: auto; }
.note code { background: var(--bg3); padding: 2px 5px; border-radius: 5px; font-size: 0.9em; }
.note pre code { background: none; padding: 0; }
.note blockquote { border-left: 3px solid var(--accent); margin: 12px 0; padding: 2px 14px; color: var(--muted); }
.note table { border-collapse: collapse; width: 100%; display: block; overflow-x: auto; }
.note th, .note td { border: 1px solid var(--border); padding: 6px 10px; }
.note a { color: var(--accent); }
.wikilink { color: var(--accent); text-decoration: underline dotted; cursor: pointer; }
.meta { background: var(--bg2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; margin-bottom: 16px; font-size: 13px; color: var(--muted); }
.meta b { color: var(--fg); font-weight: 600; }

/* floating edit button */
.fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--safe-bot)); z-index: 6;
  width: 56px; height: 56px; border-radius: 28px; border: 0; background: var(--accent2);
  color: #fff; font-size: 22px; box-shadow: 0 6px 18px rgba(0,0,0,.4);
}

/* ---- editor ---- */
.editor { display: flex; flex-direction: column; height: calc(100vh - 57px - var(--safe-top)); }
.editor textarea {
  flex: 1; width: 100%; border: 0; outline: none; resize: none; background: var(--bg);
  color: var(--fg); padding: 16px 18px; font: 15px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.edit-actions { display: flex; gap: 10px; padding: 10px 14px calc(10px + var(--safe-bot)); border-top: 1px solid var(--border); background: var(--bg2); }
.edit-actions button { flex: 1; padding: 13px; border-radius: 10px; border: 0; font-weight: 600; }

/* ---- buttons ---- */
.primary { background: var(--accent2); color: #fff; }
.ghost { background: var(--bg3); color: var(--fg); }

/* ---- quick sheet ---- */
.sheet-bg { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.5); display: flex; align-items: flex-end; }
.sheet { background: var(--bg2); width: 100%; border-radius: 18px 18px 0 0; padding: 10px 16px calc(16px + var(--safe-bot)); display: flex; flex-direction: column; gap: 12px; }
.sheet-grip { width: 40px; height: 4px; background: var(--border); border-radius: 2px; margin: 2px auto 6px; }
.seg { display: flex; background: var(--bg3); border-radius: 10px; padding: 3px; }
.seg button { flex: 1; border: 0; background: transparent; color: var(--muted); padding: 9px; border-radius: 8px; font-size: 14px; }
.seg button.active { background: var(--accent2); color: #fff; }
#qText { background: var(--bg3); border: 1px solid var(--border); color: var(--fg); border-radius: 10px; padding: 12px; font-size: 16px; resize: none; outline: none; }
#qText:focus { border-color: var(--accent); }
.sheet-actions { display: flex; gap: 10px; }
.sheet-actions button { flex: 1; padding: 13px; border-radius: 10px; border: 0; font-weight: 600; }
.q-msg { color: var(--ok); font-size: 14px; min-height: 18px; text-align: center; }

/* ---- search ---- */
.search-view { position: fixed; inset: 0; z-index: 15; background: var(--bg); display: flex; flex-direction: column; }
.search-head { display: flex; gap: 8px; padding: calc(8px + var(--safe-top)) 12px 8px; border-bottom: 1px solid var(--border); }
.search-head input { flex: 1; }
.search-head .ghost { padding: 0 14px; border-radius: 10px; }
.list { overflow-y: auto; }
.toast { position: fixed; left: 50%; bottom: calc(28px + var(--safe-bot)); transform: translateX(-50%); background: var(--bg3); color: var(--fg); border: 1px solid var(--border); padding: 10px 16px; border-radius: 10px; z-index: 50; font-size: 14px; }
