:root {
  --bg: #f7f4ee;
  --panel: #fffdf9;
  --side: #efeae0;
  --text: #1c1a17;
  --muted: #6f685d;
  --line: #e2dbcd;
  --gold: #a87724;
  --gold-soft: rgba(200, 150, 60, .16);
  --mark: rgba(200, 150, 60, .35);
  --code: #f0ebe1;
  --danger: #b3261e;
  --ok: #2e7d32;
  --shadow: 0 10px 30px rgba(40, 30, 10, .12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111111; --panel: #181715; --side: #151413; --text: #ece7de; --muted: #9a9286;
    --line: #2a2723; --gold: #d9a54a; --gold-soft: rgba(200, 150, 60, .14); --mark: rgba(200, 150, 60, .38);
    --code: #1f1d1a; --danger: #ff6b5e; --ok: #74c37a; --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111111; --panel: #181715; --side: #151413; --text: #ece7de; --muted: #9a9286;
  --line: #2a2723; --gold: #d9a54a; --gold-soft: rgba(200, 150, 60, .14); --mark: rgba(200, 150, 60, .38);
  --code: #1f1d1a; --danger: #ff6b5e; --ok: #74c37a; --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.6 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
}
a { color: var(--gold); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, textarea, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 8px 14px; cursor: pointer; font-weight: 600; font-size: 15px; white-space: nowrap;
  color: var(--text); text-decoration: none !important;
}
.btn:hover { border-color: var(--gold); }
.btn.primary { background: #c8963c; border-color: #c8963c; color: #111; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 14px; border-radius: 8px; }
.btn.wide { width: 100%; padding: 12px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  border: 0; background: transparent; width: 40px; height: 40px; border-radius: 10px;
  font-size: 20px; cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--gold-soft); }
.link-btn { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 4px 0; font-size: 14px; text-align: left; }
.link-btn:hover { color: var(--gold); }
.pill { font-size: 12px; background: var(--gold-soft); color: var(--gold); border-radius: 99px; padding: 0 8px; font-weight: 600; }
.pill:empty { display: none; }

/* ---------- вход ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px; box-shadow: var(--shadow);
}
.login-card label { display: block; font-size: 14px; color: var(--muted); margin: 14px 0; }
.login-card input {
  display: block; width: 100%; margin-top: 6px; padding: 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); font-size: 16px;
}
.brand { display: flex; align-items: center; gap: 10px; font-family: "Playfair Display", Georgia, serif; font-size: 20px; color: var(--text); font-weight: 600; }
.brand:hover { text-decoration: none; }
.brand.big { font-size: 28px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; flex: none;
  background: #111; color: #c8963c; border: 1.5px solid #c8963c; font-style: italic; font-size: 20px;
}
.brand.big .brand-mark { width: 44px; height: 44px; font-size: 26px; }

/* ---------- каркас ---------- */
.app { display: flex; height: 100%; }
.side {
  width: 300px; flex: none; background: var(--side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; height: 100%; overflow: hidden;
  padding-top: env(safe-area-inset-top);
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.side-nav { padding: 6px 8px; }
.side-link { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; color: var(--text); font-weight: 500; }
.side-link:hover, .side-link.active { background: var(--gold-soft); text-decoration: none; }
.side-title {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted);
  padding: 14px 18px 4px; font-weight: 600;
}
.tree-filter {
  width: 120px; border: 1px solid var(--line); background: var(--panel); border-radius: 6px;
  padding: 2px 8px; font-size: 13px; text-transform: none; letter-spacing: 0;
}
.fav-list { padding: 0 8px; max-height: 28vh; overflow: auto; }
.fav-list:empty::after { content: "Нажмите ☆ у заметки"; display: block; color: var(--muted); font-size: 13px; padding: 4px 10px; }
.tree { flex: 1; overflow: auto; padding: 0 8px 12px; font-size: 14.5px; }
.tree details > summary { list-style: none; }
.tree details > summary::-webkit-details-marker { display: none; }
.t-row {
  display: flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 6px; cursor: pointer;
  color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.45;
}
.t-row:hover { background: var(--gold-soft); text-decoration: none; }
.t-row.active { background: var(--gold-soft); color: var(--gold); font-weight: 600; }
.t-row .t-name { overflow: hidden; text-overflow: ellipsis; }
.t-row .t-ico { flex: none; width: 14px; text-align: center; color: var(--muted); font-size: 11px; transition: transform .15s; }
.tree details[open] > summary .t-ico { transform: rotate(90deg); }
.t-row .t-cnt { margin-left: auto; color: var(--muted); font-size: 12px; }
.t-children { padding-left: 14px; border-left: 1px solid var(--line); margin-left: 14px; }
.side-foot { display: flex; gap: 14px; padding: 10px 18px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); flex-wrap: wrap; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; }
.top {
  display: flex; align-items: center; gap: 10px; padding: 10px 20px;
  padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--bg); position: sticky; top: 0; z-index: 5;
}
.search-wrap { flex: 1; position: relative; display: flex; align-items: center; max-width: 720px; }
.search-ico { position: absolute; left: 12px; color: var(--muted); font-size: 18px; pointer-events: none; }
#q {
  width: 100%; padding: 10px 40px 10px 36px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); font-size: 16px; outline: none;
}
#q:focus { border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); }
.search-wrap kbd { position: absolute; right: 10px; font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 5px; padding: 0 6px; }
.add-btn { margin-left: auto; }
.view { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }
.page { max-width: 860px; margin: 0 auto; padding: 24px 28px 120px; }
.page.wide { max-width: 1100px; }

.scrim { display: none; }
.only-mobile { display: none; }

/* ---------- главная ---------- */
.h-title { font-family: "Playfair Display", Georgia, serif; font-size: 30px; margin: 4px 0 18px; font-weight: 600; }
.h-title i { color: var(--gold); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; margin-bottom: 18px; }
.card h3 { margin: 0 0 10px; font-size: 15px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.capture textarea {
  width: 100%; min-height: 96px; resize: vertical; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); padding: 12px; font-size: 16px; line-height: 1.5; outline: none;
}
.capture textarea:focus { border-color: var(--gold); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.list { display: flex; flex-direction: column; }
.li {
  display: block; padding: 10px 6px; border-bottom: 1px solid var(--line); color: var(--text);
}
.li:last-child { border-bottom: 0; }
.li:hover { text-decoration: none; background: var(--gold-soft); border-radius: 8px; }
.li-title { font-weight: 600; display: flex; gap: 8px; align-items: baseline; }
.li-title .when { margin-left: auto; color: var(--muted); font-weight: 400; font-size: 13px; white-space: nowrap; }
.li-path { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li-snip { color: var(--muted); font-size: 14px; margin-top: 3px; overflow-wrap: anywhere; }
.li-snip mark, .li-title mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
.tile {
  display: block; padding: 12px 14px; border-radius: 12px; background: var(--panel); border: 1px solid var(--line); color: var(--text);
}
.tile:hover { border-color: var(--gold); text-decoration: none; }
.tile b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile span { font-size: 13px; color: var(--muted); }

/* ---------- заметка ---------- */
.crumbs { font-size: 13px; color: var(--muted); margin-bottom: 6px; overflow-wrap: anywhere; }
.crumbs a { color: var(--muted); }
.crumbs a:hover { color: var(--gold); }
.note-head { display: flex; align-items: flex-start; gap: 10px; }
.note-title { font-family: "Playfair Display", Georgia, serif; font-size: 32px; line-height: 1.2; margin: 0 0 6px; font-weight: 600; flex: 1; overflow-wrap: anywhere; }
.note-actions { display: flex; gap: 6px; flex: none; padding-top: 4px; }
.meta { font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.fm { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 18px; }
.fm .kv { font-size: 13px; background: var(--code); border: 1px solid var(--line); border-radius: 8px; padding: 2px 8px; }
.fm .kv b { color: var(--muted); font-weight: 500; margin-right: 4px; }
.tag { display: inline-block; font-size: .86em; background: var(--gold-soft); color: var(--gold); border-radius: 99px; padding: 0 8px; margin: 0 1px; font-weight: 500; }
.tag:hover { text-decoration: none; filter: brightness(1.15); }

.md { overflow-wrap: anywhere; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.3; margin: 1.5em 0 .5em; }
.md h1 { font-size: 1.6em; } .md h2 { font-size: 1.35em; border-bottom: 1px solid var(--line); padding-bottom: .2em; }
.md h3 { font-size: 1.15em; } .md h4 { font-size: 1em; }
.md > :first-child { margin-top: 0; }
.md p, .md ul, .md ol { margin: .6em 0; }
.md ul, .md ol { padding-left: 1.5em; }
.md li { margin: .2em 0; }
.md li.task { list-style: none; margin-left: -1.4em; }
.md li.task > input { width: 18px; height: 18px; vertical-align: -3px; margin: 0 8px 0 0; accent-color: #c8963c; cursor: pointer; }
.md li.task.done { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--line); }
.md blockquote { margin: .8em 0; padding: .5em 1em; border-left: 3px solid #c8963c; background: var(--gold-soft); border-radius: 0 10px 10px 0; }
.md blockquote > :first-child { margin-top: 0; } .md blockquote > :last-child { margin-bottom: 0; }
.md code { background: var(--code); border-radius: 5px; padding: .1em .35em; font-size: .9em; font-family: ui-monospace, "Cascadia Code", Consolas, monospace; }
.md pre { background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; overflow: auto; font-size: 14px; line-height: 1.5; }
.md pre code { background: none; padding: 0; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.6em 0; }
.md img, .md video { max-width: 100%; border-radius: 10px; height: auto; }
.md .tbl { overflow-x: auto; margin: .8em 0; border: 1px solid var(--line); border-radius: 10px; }
.md table { border-collapse: collapse; width: 100%; font-size: 14.5px; }
.md th, .md td { border-bottom: 1px solid var(--line); padding: 7px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--code); font-weight: 600; white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }
.md td + td, .md th + th { border-left: 1px solid var(--line); }
.md a.wl { border-bottom: 1px dashed currentColor; }
.md a.wl:hover { text-decoration: none; border-bottom-style: solid; }
.md a.wl.missing { color: var(--muted); }
.md a.ext::after { content: "↗"; font-size: .75em; margin-left: 2px; }
.md mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 2px; }
.md .embed-file { display: inline-flex; gap: 6px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--code); }
.md .callout { border-left-width: 3px; }
.md .callout-title { font-weight: 700; margin-bottom: .3em; }
.backlinks { margin-top: 40px; border-top: 1px solid var(--line); padding-top: 16px; }
.backlinks h3 { font-size: 14px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 6px; }

/* ---------- редактор ---------- */
.editor { display: flex; flex-direction: column; height: 100%; }
.ed-bar {
  display: flex; align-items: center; gap: 6px; padding: 8px 16px; border-bottom: 1px solid var(--line);
  background: var(--panel); flex-wrap: wrap; position: sticky; top: 0; z-index: 3;
}
.ed-bar .ed-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1 1 160px; }
.ed-bar .ed-name small { display: block; color: var(--muted); font-weight: 400; font-size: 12px; }
.ed-tools { display: flex; gap: 2px; overflow-x: auto; padding: 6px 12px; border-bottom: 1px solid var(--line); background: var(--panel); scrollbar-width: none; }
.ed-tools button {
  flex: none; border: 1px solid transparent; background: none; border-radius: 8px; min-width: 38px; height: 36px;
  cursor: pointer; font-weight: 600; padding: 0 8px; color: var(--text);
}
.ed-tools button:hover { border-color: var(--line); background: var(--bg); }
.ed-wrap { flex: 1; position: relative; display: flex; min-height: 0; }
.ed-wrap textarea {
  flex: 1; width: 100%; border: 0; outline: none; resize: none; background: var(--bg);
  padding: 20px max(20px, calc((100% - 860px) / 2)) 50vh; font: 16.5px/1.65 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  tab-size: 2;
}
.ed-wrap .ed-preview { flex: 1; overflow: auto; padding: 20px 28px 120px; border-left: 1px solid var(--line); display: none; }
.editor.split .ed-preview { display: block; }
.editor.split textarea { padding: 20px 24px 50vh; }
.ed-status { font-size: 13px; color: var(--muted); }
.ed-status.dirty { color: var(--gold); }
.ac {
  position: fixed; z-index: 30; background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); width: min(420px, calc(100vw - 32px)); max-height: 280px; overflow: auto;
}
.ac div { padding: 8px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.ac div:last-child { border-bottom: 0; }
.ac div.sel, .ac div:hover { background: var(--gold-soft); }
.ac small { display: block; color: var(--muted); font-size: 12px; }

/* ---------- формы / модалки ---------- */
.field { display: block; margin: 0 0 14px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 16px; outline: none;
}
.field input:focus, .field select:focus { border-color: var(--gold); }
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 40; display: grid; place-items: center; padding: 16px; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 20px; width: min(520px, 100%); box-shadow: var(--shadow); max-height: 90vh; overflow: auto; }
.modal-card h3 { margin: 0 0 14px; }
.menu-list { display: flex; flex-direction: column; gap: 4px; }
.menu-list button { text-align: left; border: 0; background: none; padding: 11px 12px; border-radius: 10px; cursor: pointer; font-size: 16px; }
.menu-list button:hover { background: var(--gold-soft); }
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(150%);
  background: #1c1a17; color: #f4efe6; padding: 10px 18px; border-radius: 12px; box-shadow: var(--shadow);
  transition: transform .25s, visibility .25s; visibility: hidden; z-index: 50; max-width: calc(100vw - 32px); font-size: 15px; border: 1px solid #c8963c55;
}
.toast.show { transform: translateX(-50%) translateY(0); visibility: visible; }
.empty { color: var(--muted); padding: 30px 0; text-align: center; }
.loading { color: var(--muted); padding: 40px 0; text-align: center; }

/* ---------- телефон ---------- */
@media (max-width: 899px) {
  .only-desktop { display: none !important; }
  .only-mobile { display: inline-grid; place-items: center; }
  .side {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 20; width: min(330px, 86vw);
    transform: translateX(-102%); transition: transform .22s ease; box-shadow: var(--shadow);
  }
  .app.side-open .side { transform: none; }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 15; }
  .top { padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); gap: 6px; }
  .page { padding: 16px 16px 120px; }
  .note-title { font-size: 25px; }
  .h-title { font-size: 25px; }
  .add-btn { padding: 8px 12px; font-size: 18px; }
  .ed-wrap textarea { padding: 14px 16px 50vh; font-size: 16px; }
  .editor.split .ed-preview { display: none; }
  .ed-bar { padding: 8px 12px; }
  .md table { font-size: 13.5px; }
}
@media (min-width: 900px) {
  .only-mobile { display: none !important; }
}
