:root {
  --bg: #f7f6f3; --panel: #ffffff; --panel2: #f1efea; --text: #26241f; --muted: #8a867c;
  --line: #e4e1d9; --accent: #5b6ee1; --accent-fg: #fff; --hover: rgba(0,0,0,.05);
  --sel: rgba(91,110,225,.12); --danger: #c0453a; --code: #efece5; --mark: #fff0a8;
  --fs: 16px; --editor-font: ui-sans-serif, -apple-system, "Segoe UI", Roboto, sans-serif;
  --shadow: 0 8px 30px rgba(0,0,0,.18);
}
:root[data-theme="dark"] {
  --bg: #17181b; --panel: #1f2125; --panel2: #25282d; --text: #e6e4df; --muted: #8d8f96;
  --line: #31343a; --hover: rgba(255,255,255,.07); --sel: rgba(120,140,255,.18);
  --code: #2a2d33; --mark: #6b5d1a; --shadow: 0 8px 30px rgba(0,0,0,.6);
}
:root[data-theme="sepia"] {
  --bg: #efe6d2; --panel: #f7f0de; --panel2: #eadfc6; --text: #4a3f2c; --muted: #8f8266;
  --line: #ddd0b0; --code: #e6dabb; --mark: #f3dd8a;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #17181b; --panel: #1f2125; --panel2: #25282d; --text: #e6e4df; --muted: #8d8f96;
    --line: #31343a; --hover: rgba(255,255,255,.07); --sel: rgba(120,140,255,.18);
    --code: #2a2d33; --mark: #6b5d1a; --shadow: 0 8px 30px rgba(0,0,0,.6);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 ui-sans-serif, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; cursor: pointer; border-radius: 8px; padding: 6px 9px; }
button:hover { background: var(--hover); }
button.primary { background: var(--accent); color: var(--accent-fg); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--danger); }
button.ghost { border: 1px solid var(--line); }
input, select { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---- layout ---- */
#app { display: grid; grid-template-columns: 230px 320px 1fr; height: 100%; }
#app.focus { grid-template-columns: 0 0 1fr; }
#app.focus #side, #app.focus #listpane { display: none; }
#side { background: var(--panel2); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
#listpane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
#editor { background: var(--panel); display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }

/* ---- sidebar ---- */
.brand { display: flex; align-items: center; gap: 8px; padding: 14px 14px 8px; font-weight: 700; font-size: 18px; letter-spacing: .2px; }
.brand .logo { width: 26px; height: 26px; border-radius: 8px; background: var(--accent); color: var(--accent-fg); display: grid; place-items: center; font-size: 15px; }
.sidebtns { padding: 4px 12px 8px; display: flex; gap: 6px; }
.sidebtns button { flex: 1; }
#nav { flex: 1; overflow: auto; padding: 0 8px 8px; }
.si { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 8px; cursor: pointer; user-select: none; }
.si:hover { background: var(--hover); }
.si.on { background: var(--sel); font-weight: 600; }
.si .ic { width: 20px; text-align: center; }
.si .lb { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.si .ct { color: var(--muted); font-size: 12px; }
.si .nbm { padding: 0 6px; opacity: 0; color: var(--muted); }
.si:hover .nbm, .si.on .nbm { opacity: 1; }
.si.dropping { outline: 2px dashed var(--accent); }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; margin: 0 5px; background: var(--muted); }
.sh { display: flex; justify-content: space-between; align-items: center; margin: 14px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); }
.sh button { padding: 0 6px; font-size: 16px; }
.tagcloud { display: flex; flex-wrap: wrap; gap: 5px; padding: 2px 10px; }
.chip { display: inline-flex; align-items: center; gap: 4px; background: var(--hover); border-radius: 999px; padding: 2px 9px; font-size: 12px; cursor: pointer; }
.chip.on { background: var(--accent); color: var(--accent-fg); }
.sidefoot { border-top: 1px solid var(--line); padding: 6px 8px; display: flex; gap: 2px; flex-wrap: wrap; }
.sidefoot button { flex: 1; font-size: 13px; white-space: nowrap; }

/* ---- list pane ---- */
.lhead { padding: 12px 12px 6px; display: flex; align-items: center; gap: 6px; }
.lhead h2 { margin: 0; font-size: 17px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lsearch { padding: 0 12px 8px; display: flex; gap: 6px; }
.lsearch input { flex: 1; min-width: 0; }
.lsearch select { width: 92px; }
#notes { flex: 1; overflow: auto; padding: 0 8px 12px; }
.card { padding: 10px 12px; border-radius: 10px; cursor: pointer; border: 1px solid transparent; margin-bottom: 2px; }
.card:hover { background: var(--hover); }
.card.on { background: var(--sel); border-color: var(--accent); }
.card .t { font-weight: 600; display: flex; gap: 6px; align-items: center; }
.card .t span.tt { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .sn { color: var(--muted); font-size: 13px; margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; word-break: break-word; }
.card .mt { display: flex; gap: 8px; align-items: center; font-size: 11.5px; color: var(--muted); margin-top: 5px; flex-wrap: wrap; }
.card mark, .md mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 2px; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; }

/* ---- editor ---- */
.etool { display: flex; align-items: center; gap: 2px; padding: 8px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.etool .sp { flex: 1; }
.etool button { min-width: 34px; }
.etool button.on { background: var(--sel); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin: 0 4px; }
.seg button { border-radius: 0; padding: 4px 9px; font-size: 13px; }
.seg button.on { background: var(--accent); color: var(--accent-fg); }
.backbtn, .menubtn { display: none; }
#title { border: 0; background: transparent; font-size: 26px; font-weight: 700; padding: 14px 20px 4px; width: 100%; border-radius: 0; }
#title:focus { border: 0; }
.meta { display: flex; align-items: center; gap: 8px; padding: 0 20px 8px; flex-wrap: wrap; font-size: 13px; }
.meta select { padding: 3px 8px; font-size: 13px; }
.tags { display: flex; gap: 5px; flex-wrap: wrap; align-items: center; }
.tags .chip button { padding: 0 2px; font-size: 12px; color: var(--muted); }
.tags input { border: 0; background: transparent; padding: 3px 4px; width: 110px; font-size: 13px; }
.fmt { display: flex; gap: 1px; padding: 2px 14px 6px; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.fmt button { padding: 3px 8px; font-size: 13px; color: var(--muted); min-width: 30px; }
.fmt button:hover { color: var(--text); }
.panes { flex: 1; display: flex; min-height: 0; position: relative; }
#body { flex: 1; min-width: 0; resize: none; border: 0; border-radius: 0; background: transparent; padding: 16px 22px 40px; font: var(--fs)/1.65 var(--editor-font); outline: none; tab-size: 2; }
#preview { flex: 1; min-width: 0; overflow: auto; padding: 16px 26px 40px; }
.panes.split #body { border-right: 1px solid var(--line); }
.panes.edit #preview { display: none; }
.panes.preview #body { display: none; }
#info { width: 260px; border-left: 1px solid var(--line); overflow: auto; padding: 12px 14px; background: var(--panel2); font-size: 13px; }
#info h4 { margin: 14px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); }
#info h4:first-child { margin-top: 0; }
#info a { display: block; padding: 3px 0; color: var(--text); text-decoration: none; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#info a:hover { color: var(--accent); }
.status { display: flex; align-items: center; gap: 14px; padding: 5px 16px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.status .sp { flex: 1; }
.status .err { color: var(--danger); }
.prog { width: 70px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; display: inline-block; vertical-align: middle; margin-left: 4px; }
.prog i { display: block; height: 100%; background: var(--accent); }
.banner { background: var(--panel2); border-bottom: 1px solid var(--line); padding: 8px 16px; display: flex; gap: 8px; align-items: center; }
.banner span { flex: 1; }
#edEmpty { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 20px; }
#edEmpty .big { font-size: 46px; }
#lockScreen { flex: 1; display: grid; place-items: center; padding: 20px; }
#lockScreen .box { width: min(360px, 100%); text-align: center; display: grid; gap: 10px; }
#lockScreen .big { font-size: 44px; }
#lockScreen input { text-align: center; }

/* ---- rendered markdown ---- */
.md { font-size: var(--fs); line-height: 1.7; word-wrap: break-word; }
.md > :first-child { margin-top: 0; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.25; margin: 1.3em 0 .5em; }
.md h1 { font-size: 1.7em; border-bottom: 1px solid var(--line); padding-bottom: .2em; }
.md h2 { font-size: 1.4em; } .md h3 { font-size: 1.18em; } .md h4 { font-size: 1em; }
.md p { margin: .6em 0; }
.md a { color: var(--accent); }
.md code { background: var(--code); border-radius: 5px; padding: .1em .35em; font: .9em ui-monospace, SFMono-Regular, Menlo, monospace; }
.md pre { background: var(--code); border-radius: 10px; padding: 12px 14px; overflow: auto; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: .8em 0; padding: .1em 1em; border-left: 3px solid var(--accent); color: var(--muted); }
.md ul, .md ol { padding-left: 1.5em; margin: .5em 0; }
.md li.task { list-style: none; margin-left: -1.3em; }
.md li.task input { margin-right: 4px; transform: scale(1.15); vertical-align: -1px; }
.md .done { text-decoration: line-through; color: var(--muted); }
.md img { max-width: 100%; border-radius: 8px; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.4em 0; }
.md .tw { overflow: auto; }
.md table { border-collapse: collapse; margin: .8em 0; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 11px; }
.md th { background: var(--panel2); }
.md .callout { border-left: 4px solid var(--accent); background: var(--panel2); border-radius: 8px; padding: 8px 14px; margin: .9em 0; }
.md .callout .ct { font-size: 11px; font-weight: 700; letter-spacing: .8px; color: var(--accent); }
.md .callout.warning, .md .callout.danger { border-color: var(--danger); }
.md .callout.warning .ct, .md .callout.danger .ct { color: var(--danger); }
.md .callout p { margin: .3em 0; }
.md .wl { color: var(--accent); text-decoration: none; border-bottom: 1px dashed var(--accent); cursor: pointer; }
.md .wl.missing { opacity: .65; border-bottom-style: dotted; }

/* ---- overlays ---- */
#modal, #palette { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: flex-start; justify-content: center; padding: 8vh 12px 12px; z-index: 50; }
.mbox { background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); width: min(520px, 100%); max-height: 84vh; overflow: auto; padding: 18px; }
.mbox.wide { width: min(860px, 100%); }
.mbox h3 { margin: 0 0 12px; }
.mbox .row { display: flex; gap: 8px; align-items: center; margin: 8px 0; flex-wrap: wrap; }
.mbox .row > label { min-width: 90px; color: var(--muted); }
.mbox input[type=text], .mbox input[type=password] { width: 100%; }
.mbox .foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid transparent; padding: 0; }
.swatch.on { border-color: var(--text); }
.pbox { background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); width: min(580px, 100%); overflow: hidden; }
.pbox input { width: 100%; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; padding: 14px 16px; font-size: 16px; }
#plist { max-height: 56vh; overflow: auto; padding: 6px; }
.pi { display: flex; justify-content: space-between; gap: 10px; padding: 8px 12px; border-radius: 8px; cursor: pointer; }
.pi.on { background: var(--sel); }
.pi .h { color: var(--muted); font-size: 12px; white-space: nowrap; }
.pi .l { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vlist { display: grid; grid-template-columns: 200px 1fr; gap: 12px; }
.vlist .vi { padding: 7px 9px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.vlist .vi:hover { background: var(--hover); }
.vlist .vi.on { background: var(--sel); }
.vlist pre { margin: 0; white-space: pre-wrap; background: var(--code); padding: 10px; border-radius: 8px; max-height: 50vh; overflow: auto; font-size: 13px; }
#graphc { width: 100%; height: 62vh; background: var(--panel2); border-radius: 10px; display: block; }
#toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 999px; font-size: 14px; z-index: 80; box-shadow: var(--shadow); }
.copybox { display: flex; gap: 6px; } .copybox input { flex: 1; min-width: 0; }
#printArea { display: none; }

/* ---- login / share pages ---- */
.center { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.loginbox { width: min(340px, 100%); background: var(--panel); padding: 26px; border-radius: 16px; box-shadow: var(--shadow); display: grid; gap: 12px; text-align: center; }
.loginbox .logo { width: 46px; height: 46px; margin: 0 auto; border-radius: 14px; background: var(--accent); color: var(--accent-fg); display: grid; place-items: center; font-size: 24px; }
.loginbox h1 { margin: 0; font-size: 22px; }
.loginbox .err { color: var(--danger); min-height: 1.2em; font-size: 14px; }
.sharewrap { max-width: 780px; margin: 0 auto; padding: 28px 22px 60px; }
.sharewrap .bar { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 13px; margin-bottom: 18px; }
.sharewrap h1.st { font-size: 2em; margin: 0 0 .2em; }

/* ---- mobile ---- */
@media (max-width: 900px) {
  #app { display: block; position: relative; }
  #side { position: fixed; top: 0; bottom: 0; left: 0; width: 270px; z-index: 30; transform: translateX(-102%); transition: transform .2s; box-shadow: var(--shadow); }
  #app.side-open #side { transform: none; }
  #listpane, #editor { position: absolute; inset: 0; }
  #app[data-m="list"] #editor { display: none; }
  #app[data-m="editor"] #listpane { display: none; }
  .backbtn, .menubtn { display: inline-block; }
  #info { position: absolute; right: 0; top: 0; bottom: 0; z-index: 5; box-shadow: var(--shadow); }
  #title { font-size: 22px; padding: 10px 14px 2px; }
  .meta { padding: 0 14px 8px; }
  #body { padding: 12px 14px 40px; font-size: 16px; }
  #preview { padding: 12px 16px 40px; }
  .vlist { grid-template-columns: 1fr; }
  .panes.split { flex-direction: column; }
  .panes.split #body { border-right: 0; border-bottom: 1px solid var(--line); }
  .panes.split #body, .panes.split #preview { flex: 1 1 50%; min-height: 0; }
  .hide-m { display: none; }
  #plist { max-height: 38vh; }
  #palette { padding-top: 4vh; }
}

/* ---- print ---- */
@media print {
  html, body { height: auto; background: #fff !important; color: #000 !important; }
  #app, #modal, #palette, #toast { display: none !important; }
  #printArea { display: block !important; padding: 0; color: #000; background: #fff; }
  #printArea .pnote { page-break-after: always; break-after: page; }
  #printArea .pnote:last-child { page-break-after: auto; break-after: auto; }
  #printArea h1.ptitle { font-size: 26px; margin: 0 0 4px; border: 0; }
  #printArea .pmeta { color: #555; font-size: 12px; margin-bottom: 16px; border-bottom: 1px solid #ccc; padding-bottom: 8px; }
  #printArea .md, #printArea .md * { color: #000 !important; }
  #printArea .md a { text-decoration: underline; }
  #printArea .md pre, #printArea .md code { background: #f2f2f2 !important; }
  #printArea .md pre { white-space: pre-wrap; border: 1px solid #ddd; }
  #printArea .md th { background: #eee !important; }
  #printArea .md .callout { background: #f6f6f6 !important; border-color: #888 !important; }
  .sharewrap .bar button { display: none; }
  .sharewrap { padding: 0; max-width: none; }
}
