:root {
  --bg: #fbfbfa;
  --surface: #ffffff;
  --surface-2: #f1f1ee;
  --text: #1c1c1a;
  --muted: #6b6b66;
  --border: #e3e3de;
  --accent: #5847d6;
  --accent-soft: #ecebfb;
  --on-accent: #ffffff;
  --mark: #fff0a3;
  --danger: #b3261e;
  --radius: 12px;
  --shadow: 0 6px 24px rgb(0 0 0 / .12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141413; --surface: #1c1c1b; --surface-2: #272725; --text: #ecebe6; --muted: #9c9b94;
    --border: #2f2f2c; --accent: #a397ff; --accent-soft: #2a2740; --on-accent: #141413;
    --mark: #594a10; --danger: #ff8a80; --shadow: 0 6px 24px rgb(0 0 0 / .5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141413; --surface: #1c1c1b; --surface-2: #272725; --text: #ecebe6; --muted: #9c9b94;
  --border: #2f2f2c; --accent: #a397ff; --accent-soft: #2a2740; --on-accent: #141413;
  --mark: #594a10; --danger: #ff8a80; --shadow: 0 6px 24px rgb(0 0 0 / .5); color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
h1 { font-size: 1.6rem; line-height: 1.25; margin: .4em 0 .5em; }
h2 { font-size: 1.1rem; margin: 1.6em 0 .6em; }
code { font: .9em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: .1em .35em; border-radius: 6px; word-break: break-all; }
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 .1em; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* --- каркас --- */
.app { min-height: 100dvh; }
.sidebar { display: none; }
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between;
  padding: calc(6px + env(safe-area-inset-top)) 8px 6px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 650; color: var(--text); text-decoration: none; }
.topbar-actions { display: flex; }
main { width: 100%; max-width: 760px; margin: 0 auto; padding: 8px 16px calc(150px + env(safe-area-inset-bottom)); }
main.narrow { max-width: 420px; padding-top: 12vh; }

.bottom-nav {
  position: fixed; inset: auto 0 0 0; z-index: 12; display: flex; background: var(--surface);
  border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
}
.bottom-nav a {
  flex: 1 1 0; min-width: 0; min-height: 58px; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px; padding: 0 1px; color: var(--muted); text-decoration: none;
  font-size: 10px; letter-spacing: -0.01em;
}
.bottom-nav a span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bottom-nav a[aria-current="page"], .nav-more summary.is-active { color: var(--accent); }
.nav-more { flex: 1 1 0; min-width: 0; display: flex; }
.nav-more summary {
  flex: 1; min-height: 58px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; color: var(--muted); font-size: 10px; cursor: pointer; list-style: none;
}
.nav-more summary::-webkit-details-marker { display: none; }
.nav-more[open] summary { color: var(--text); }
.more-sheet {
  position: fixed; z-index: 12; right: 8px; bottom: calc(64px + env(safe-area-inset-bottom)); min-width: 220px;
  display: flex; flex-direction: column; padding: 6px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
}
.bottom-nav .more-sheet a {
  flex-direction: row; justify-content: flex-start; gap: 12px; min-height: 48px; padding: 0 12px;
  border-radius: 10px; font-size: 16px; color: var(--text);
}
.bottom-nav .more-sheet a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.more-sheet .count { margin-left: auto; }
.bottom-nav a[aria-current="page"] { color: var(--accent); }
.fab {
  position: fixed; z-index: 11; right: 16px; bottom: calc(74px + env(safe-area-inset-bottom));
  width: 60px; height: 60px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: var(--on-accent); box-shadow: var(--shadow);
}
.fab:active { transform: scale(.95); }

@media (min-width: 820px) {
  .app { display: grid; grid-template-columns: 248px 1fr; }
  .sidebar {
    display: flex; flex-direction: column; gap: 12px; position: sticky; top: 0; height: 100dvh;
    padding: 20px 12px; border-right: 1px solid var(--border); background: var(--surface);
  }
  .sidebar .brand { padding: 0 12px; font-size: 1.05rem; }
  .topbar, .bottom-nav { display: none; }
  main { padding: 24px 32px 96px; }
  .fab { right: 32px; bottom: 32px; }
}
.sidebar-search { display: flex; align-items: center; gap: 8px; padding: 0 12px; border-radius: 10px; background: var(--surface-2); color: var(--muted); }
.sidebar-search input { border: 0; background: transparent; padding: 10px 0; min-height: 0; }
.side-nav, .side-bottom { display: flex; flex-direction: column; gap: 2px; }
.side-bottom { margin-top: auto; }
.side-nav a, .side-bottom a, .side-bottom button {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px;
  color: var(--text); text-decoration: none; font: inherit; text-align: left;
}
.side-nav a:hover, .side-bottom a:hover, .side-bottom button:hover { background: var(--surface-2); }
.side-nav a[aria-current="page"], .side-bottom a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.side-nav .count { margin-left: auto; }

/* --- элементы --- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 0 16px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text);
  font: inherit; font-weight: 550; text-decoration: none; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.small { min-height: 38px; padding: 0 12px; font-size: .92rem; }
.btn.wide { width: 100%; }
.btn.selected, .btn:disabled { background: var(--accent-soft); border-color: transparent; color: var(--accent); cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; border: 0; border-radius: 10px;
  background: transparent; color: var(--text); cursor: pointer;
}
.linklike { border: 0; background: none; padding: 0; font: inherit; color: var(--accent); cursor: pointer; }
.danger { color: var(--danger); }
input, select, textarea {
  width: 100%; font: inherit; font-size: 16px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; min-height: 44px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
textarea { resize: vertical; line-height: 1.5; }
label { display: grid; gap: 4px; font-size: .9rem; color: var(--muted); }
.count { font-size: .8rem; color: var(--muted); background: var(--surface-2); border-radius: 999px; padding: 1px 8px; }
.chip { font-size: .78rem; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); text-decoration: none; }
.chip-quiet { background: var(--surface-2); color: var(--muted); }
.tag { font-size: .85rem; color: var(--muted); text-decoration: none; }
a.tag:hover { color: var(--accent); }
.tag-active { color: var(--accent); }
.notice { padding: 10px 14px; border-radius: 10px; background: var(--accent-soft); }
.notice.error { background: color-mix(in srgb, var(--danger) 14%, transparent); }
.empty { color: var(--muted); padding: 16px 0; }
.more { display: inline-block; padding: 12px 0; }
.form-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.hint { font-size: .85rem; }

/* --- списки заметок --- */
.section-head { display: flex; align-items: center; gap: 10px; }
.section-head .btn { margin-left: auto; }
.note-list { display: flex; flex-direction: column; border-top: 1px solid var(--border); }
.note-row { display: flex; border-bottom: 1px solid var(--border); }
.note-item {
  flex: 1; min-width: 0; display: grid; gap: 3px; padding: 14px 4px; min-height: 60px;
  color: var(--text); text-decoration: none;
}
.note-open {
  flex: none; width: 52px; display: grid; place-items: center; color: var(--muted);
}
.note-item:hover, .note-open:hover { background: var(--surface-2); }
.read-toggle { flex: none; display: grid; place-items: center; width: 48px; }
.read-check {
  width: 30px; height: 30px; display: grid; place-items: center; padding: 0; cursor: pointer;
  border: 2px solid var(--border); border-radius: 8px; background: var(--surface); color: transparent;
}
.read-check:hover { border-color: var(--accent); color: var(--muted); }
.read-check.is-read { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.note-row.is-read .note-item-title { color: var(--muted); text-decoration: line-through; text-decoration-thickness: 1px; }
.note-row.is-read .note-item-excerpt { opacity: .7; }
.read-date { color: var(--accent); font-size: .85rem; }
.read-progress { margin: -4px 0 8px; font-size: .9rem; }
.btn.selected-read { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.wikilink .read-mark { color: var(--accent); font-weight: 700; }
.prose .wikilink.is-read { text-decoration: line-through; text-decoration-color: var(--muted); }
.note-open:hover { color: var(--accent); }
.note-item-title { font-weight: 600; overflow-wrap: anywhere; }
.note-item-excerpt {
  color: var(--muted); font-size: .93rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  overflow-wrap: anywhere;
}
.note-item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; font-size: .85rem; }
.pager { display: flex; gap: 8px; justify-content: space-between; padding: 16px 0; }

/* --- поиск --- */
.search-form { display: grid; gap: 10px; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; }
.filters select { flex: 1 1 140px; width: auto; }
.active-tags, .tag-cloud { display: flex; flex-wrap: wrap; gap: 6px 14px; }

/* --- заметка --- */
.note-head h1 { margin-bottom: .3em; overflow-wrap: anywhere; }
.note-url { display: inline-flex; align-items: center; gap: 6px; margin-top: 10px; min-height: 36px; overflow-wrap: anywhere; }
.note-actions { margin: 14px 0 6px; }
.note-section { border-top: 1px solid var(--border); margin-top: 28px; }
.move { display: flex; flex-wrap: wrap; gap: 8px; }
.attachments { display: grid; gap: 12px; margin-bottom: 12px; }
.attachment img { display: block; max-width: 100%; max-height: 360px; border-radius: 10px; border: 1px solid var(--border); }
.attachment-row { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; padding-top: 6px; overflow-wrap: anywhere; }
.attachment-row form { margin-left: auto; }
.archived-attachments { margin: 4px 0 12px; }
.archived-attachments summary { cursor: pointer; padding: 6px 0; }
.upload { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.upload input { flex: 1 1 220px; width: auto; }
.note-delete { margin-top: 36px; padding: 12px 0; }
.note-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.link-form { display: flex; flex-wrap: wrap; gap: 8px; }
.link-form input { flex: 1 1 200px; width: auto; }
.note-section h3 { font-size: 1rem; margin: 1.4em 0 .4em; }
.backlink-group { display: flex; align-items: center; gap: 8px; margin: 1em 0 .3em; font-size: .9rem; color: var(--muted); font-weight: 600; }

.prose { overflow-wrap: anywhere; }
.prose img { max-width: 100%; }
.prose pre { background: var(--surface-2); padding: 12px 14px; border-radius: 10px; overflow-x: auto; }
.prose pre code { background: none; padding: 0; word-break: normal; }
.prose blockquote { margin: 1em 0; padding: 2px 16px; border-left: 3px solid var(--border); color: var(--muted); }
.prose table { border-collapse: collapse; display: block; overflow-x: auto; }
.prose th, .prose td { border: 1px solid var(--border); padding: 6px 10px; }
.prose .wikilink { text-decoration: none; background: var(--accent-soft); padding: 0 4px; border-radius: 4px; }
.prose .wikilink.missing { background: none; border-bottom: 1px dashed var(--accent); border-radius: 0; }
.offline-content { white-space: pre-wrap; font: inherit; overflow-wrap: anywhere; }

/* --- редактор --- */
.editor-form { display: grid; gap: 12px; }
.title-input { font-size: 1.3rem; font-weight: 600; border: 0; padding-left: 0; background: transparent; }
.title-input:focus { outline: none; }
.tabs { display: flex; align-items: center; gap: 4px; margin-bottom: 6px; flex-wrap: wrap; }
.tabs button { border: 0; background: none; font: inherit; padding: 8px 12px; border-radius: 8px; color: var(--muted); cursor: pointer; min-height: 40px; }
.tabs button[aria-selected="true"] { background: var(--surface-2); color: var(--text); }
.tabs .hint { margin-left: auto; }
.editor textarea { min-height: 45vh; font: 15px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; }
.preview { min-height: 45vh; padding: 4px 2px; }
.fields { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.sticky-actions { position: sticky; bottom: calc(66px + env(safe-area-inset-bottom)); padding: 10px 0; background: var(--bg); }
@media (min-width: 820px) { .sticky-actions { bottom: 0; } }

/* --- быстрый захват --- */
.capture {
  width: min(560px, calc(100vw - 24px)); border: 1px solid var(--border); border-radius: 16px; padding: 14px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.capture::backdrop { background: rgb(0 0 0 / .4); }
.capture form { display: grid; gap: 10px; }
.capture-head { display: flex; align-items: center; justify-content: space-between; }
@media (max-width: 819px) {
  .capture { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: 16px 16px 0 0; padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
}

/* --- прочее --- */
.card { border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 16px 16px; margin-bottom: 14px; background: var(--surface); }
.login { display: grid; gap: 14px; }
.login form { display: grid; gap: 12px; }
.outbox-banner { margin: 8px 16px 0; padding: 8px 12px; border-radius: 10px; background: var(--accent-soft); font-size: .9rem; }
@media (min-width: 820px) { .outbox-banner { margin: 16px 32px 0; } }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(150px + env(safe-area-inset-bottom)); z-index: 20;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 10px; background: var(--text); color: var(--bg); box-shadow: var(--shadow);
}
