:root {
  --bg: #1b1b1f;
  --bg-2: #222227;
  --bg-3: #2b2b31;
  --line: #34343b;
  --text: #dcdde1;
  --muted: #8b8c95;
  --accent: #8b6cff;
  --accent-2: #a58bff;
  --link: #a58bff;
  --missing: #e0857a;
  --tag-bg: #33294f;
  --tag-fg: #c7b8ff;
  --danger: #ff6b6b;
  --code-bg: #26262c;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", Tahoma, sans-serif;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff; --bg-2: #f6f6f8; --bg-3: #ececf0; --line: #e1e1e6;
    --text: #222228; --muted: #74747e; --accent: #6b4dff; --accent-2: #5a3cf0;
    --link: #5a3cf0; --missing: #c4513f; --tag-bg: #ece7ff; --tag-fg: #5132d6;
    --code-bg: #f3f3f6; color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--sans); }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.grow { flex: 1; }
.error { color: var(--danger); min-height: 1.3em; margin: 0; font-size: 14px; }

.primary { background: var(--accent); color: #fff; padding: 8px 16px; border-radius: var(--radius); font-weight: 600; }
.primary:hover { background: var(--accent-2); }
.ghost { padding: 6px 10px; border-radius: var(--radius); color: var(--muted); }
.ghost:hover, .icon-btn:hover { background: var(--bg-3); color: var(--text); }
.icon-btn { width: 32px; height: 32px; border-radius: var(--radius); color: var(--muted); font-size: 18px; line-height: 1; display: inline-grid; place-items: center; flex: none; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .2px; }
.brand.big { font-size: 22px; justify-content: center; margin-bottom: 8px; }
.logo { width: 20px; height: 20px; border-radius: 6px; background: linear-gradient(135deg, var(--accent), #4fc3f7); display: inline-block; }
.brand.big .logo { width: 28px; height: 28px; }

/* Login */
.login { height: 100%; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 340px; background: var(--bg-2); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: grid; gap: 14px; }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 9px 11px; outline: none; }
.login-card input:focus { border-color: var(--accent); }

/* Layout */
.app { display: flex; height: 100%; }
.sidebar { width: 270px; flex: none; background: var(--bg-2); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 8px 14px; }
.search { margin: 0 12px 8px; padding: 7px 10px; background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); outline: none; }
.search:focus { border-color: var(--accent); }
.chip-row { padding: 0 12px 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--tag-bg); color: var(--tag-fg); border-radius: 99px; padding: 2px 4px 2px 10px; font-size: 13px; }
.chip button { width: 18px; height: 18px; border-radius: 50%; line-height: 1; }
.chip button:hover { background: rgba(255,255,255,.15); }
.note-list { flex: 1; overflow-y: auto; padding: 2px 6px; min-height: 80px; }
.note-item { display: block; width: 100%; text-align: start; padding: 6px 10px; border-radius: 6px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-item:hover { background: var(--bg-3); }
.note-item.active { background: var(--tag-bg); color: var(--tag-fg); }
.note-item .snippet { display: block; font-size: 12.5px; color: var(--muted); white-space: normal; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.note-item mark { background: rgba(255, 214, 10, .3); color: inherit; border-radius: 2px; }
.list-empty { padding: 10px; color: var(--muted); font-size: 13px; }
.side-section { border-top: 1px solid var(--line); padding: 10px 12px; max-height: 30%; overflow-y: auto; }
.side-title { font-size: 11.5px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.count { background: var(--bg-3); border-radius: 99px; padding: 0 7px; margin-inline-start: 4px; text-transform: none; }
.tag-list { display: flex; flex-wrap: wrap; gap: 5px; }
.tag-list .tag { cursor: pointer; }
.side-foot { border-top: 1px solid var(--line); padding: 8px 10px; display: flex; justify-content: space-between; align-items: center; }

.menu-wrap { position: relative; }
.menu { position: absolute; bottom: 110%; inset-inline-end: 0; min-width: 230px; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 5px; box-shadow: 0 10px 30px rgba(0,0,0,.35); z-index: 20; }
.menu.right { bottom: auto; top: 110%; }
.menu button { display: block; width: 100%; text-align: start; padding: 7px 10px; border-radius: 6px; }
.menu button:hover { background: var(--bg-3); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.danger { color: var(--danger); }

/* Main */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.empty { flex: 1; display: grid; place-items: center; text-align: center; }
.empty-title { font-size: 18px; font-weight: 600; margin: 0 0 4px; }
.editor-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.note-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.title { flex: 1; min-width: 0; background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 4px 8px; font-size: 20px; font-weight: 700; outline: none; }
.title:hover { border-color: var(--line); }
.title:focus { border-color: var(--accent); background: var(--bg-2); }
.mode-switch { display: flex; background: var(--bg-3); border-radius: var(--radius); padding: 3px; }
.mode-switch button { padding: 3px 10px; border-radius: 6px; font-size: 13px; color: var(--muted); }
.mode-switch button.on { background: var(--bg); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.save-state { font-size: 12.5px; color: var(--muted); min-width: 64px; text-align: end; }

.banner { margin: 10px 16px 0; padding: 10px 12px; border-radius: var(--radius); background: #4a3517; color: #ffd59a; display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; font-size: 14px; }
@media (prefers-color-scheme: light) { .banner { background: #fff1d6; color: #7a4b00; } }
.banner button { background: rgba(255,255,255,.12); padding: 4px 10px; border-radius: 6px; font-weight: 600; }
@media (prefers-color-scheme: light) { .banner button { background: rgba(0,0,0,.07); } }

.panes { flex: 1; display: flex; min-height: 0; }
.editor, .preview { flex: 1; min-width: 0; overflow-y: auto; padding: 20px 28px 40px; }
.editor { background: var(--bg); border: 0; outline: none; resize: none; font: 14.5px/1.65 var(--mono); tab-size: 4; }
.panes.split .editor { border-inline-end: 1px solid var(--line); }
.panes.edit .preview, .panes.preview .editor { display: none; }
.panes.preview .preview, .panes.edit .editor { max-width: 860px; margin: 0 auto; width: 100%; }

.note-foot { border-top: 1px solid var(--line); padding: 10px 16px; display: flex; gap: 28px; flex-wrap: wrap; max-height: 28%; overflow-y: auto; background: var(--bg-2); }
.foot-col { min-width: 200px; flex: 1; }
.backlink { display: block; padding: 4px 0; }
.backlink .ctx { display: block; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.edited { font-size: 12.5px; align-self: flex-end; }

/* Rendered markdown */
.md { word-wrap: break-word; }
.md > :first-child { margin-top: 0; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { line-height: 1.25; margin: 1.4em 0 .5em; }
.md h1 { font-size: 1.85em; } .md h2 { font-size: 1.5em; } .md h3 { font-size: 1.25em; } .md h4 { font-size: 1.08em; }
.md p { margin: .6em 0; }
.md a { color: var(--link); text-decoration: none; }
.md a:hover { text-decoration: underline; }
.md a.wikilink.missing { color: var(--missing); opacity: .85; }
.md a.wikilink.missing::after { content: " ＋"; font-size: .75em; }
.md code { font-family: var(--mono); font-size: .9em; background: var(--code-bg); padding: .1em .35em; border-radius: 4px; }
.md pre { background: var(--code-bg); padding: 12px 14px; border-radius: var(--radius); overflow-x: auto; direction: ltr; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: .8em 0; padding: .2em 1em; border-inline-start: 3px solid var(--accent); color: var(--muted); }
.md ul, .md ol { padding-inline-start: 1.6em; margin: .5em 0; }
.md li { margin: .15em 0; }
.md li.task { list-style: none; margin-inline-start: -1.4em; }
.md li.task input { margin-inline-end: .5em; vertical-align: middle; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.5em 0; }
.md img { max-width: 100%; border-radius: 6px; }
.md table { border-collapse: collapse; margin: .8em 0; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 10px; }
.md th { background: var(--bg-2); }
.tag { display: inline-block; background: var(--tag-bg); color: var(--tag-fg); border-radius: 99px; padding: 0 8px; font-size: .88em; text-decoration: none !important; cursor: pointer; }

/* Graph */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: center; z-index: 50; padding: 3vh 3vw; }
.graph-card { width: 100%; height: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 14px; display: flex; flex-direction: column; overflow: hidden; }
.graph-head { display: flex; align-items: center; gap: 16px; padding: 8px 10px 8px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.graph-head label { display: flex; align-items: center; gap: 5px; cursor: pointer; }
#graphCanvas { flex: 1; width: 100%; min-height: 0; cursor: grab; touch-action: none; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--bg-3); border: 1px solid var(--line); padding: 9px 16px; border-radius: 10px; z-index: 60; box-shadow: 0 8px 24px rgba(0,0,0,.3); font-size: 14px; }

.mobile-only { display: none; }
@media (max-width: 760px) {
  .mobile-only { display: inline-grid; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  [dir="rtl"] .sidebar { transform: translateX(100%); }
  .sidebar.open { transform: none; }
  .mode-switch button[data-mode="split"] { display: none; }
  .editor, .preview { padding: 16px; }
  .save-state { display: none; }
}
.md mark { background: rgba(255, 214, 10, .35); color: inherit; border-radius: 2px; padding: 0 2px; }
@media (max-width: 760px) { .note-foot { max-height: 22%; } }
