:root {
  --bg: #f3f2f1;
  --surface: #ffffff;
  --surface-2: #f0eef2;
  --hover: rgba(119, 25, 170, .07);
  --active: rgba(119, 25, 170, .13);
  --border: #e1dfe4;
  --text: #201f24;
  --muted: #6d6a73;
  --primary: #7719aa;
  --primary-dark: #5f1488;
  --primary-soft: #f3e9f9;
  --danger: #c93a42;
  --nav-bg: #faf9fb;
  --paper: #ffffff;
  --shadow: 0 1px 2px rgba(20, 16, 30, .06), 0 2px 8px rgba(20, 16, 30, .06);
  --shadow-lg: 0 10px 40px rgba(20, 16, 30, .22);
  --radius: 8px;
  --mark: rgba(255, 213, 0, .55);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1a1e; --surface: #252429; --surface-2: #2e2d33; --hover: rgba(200, 150, 240, .08); --active: rgba(200, 150, 240, .16);
    --border: #3a3840; --text: #ebe9ef; --muted: #a09ca8; --primary: #a466d8; --primary-dark: #c996ea; --primary-soft: #3a2a47;
    --danger: #ff6b73; --nav-bg: #211f25; --paper: #252429; --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-lg: 0 10px 40px rgba(0, 0, 0, .6); --mark: rgba(255, 213, 0, .4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1a1e; --surface: #252429; --surface-2: #2e2d33; --hover: rgba(200, 150, 240, .08); --active: rgba(200, 150, 240, .16);
  --border: #3a3840; --text: #ebe9ef; --muted: #a09ca8; --primary: #a466d8; --primary-dark: #c996ea; --primary-soft: #3a2a47;
  --danger: #ff6b73; --nav-bg: #211f25; --paper: #252429; --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-lg: 0 10px 40px rgba(0, 0, 0, .6); --mark: rgba(255, 213, 0, .4); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body, #app { margin: 0; height: 100%; }
body {
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; overflow: hidden;
}
a { color: var(--primary); }
h1, h2, h3 { margin: 0; }
h2 { font-size: 16px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.pad { padding: 12px 16px; }
.spacer { flex: 1; }
.loading, .empty { padding: 40px 16px; text-align: center; color: var(--muted); }
.empty .btn { margin-top: 8px; }

/* ---------- controls */
input[type=text], input[type=password], input[type=search], select, textarea {
  font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 35%, transparent); border-color: var(--primary); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 500;
  color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: 7px 14px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { color: var(--danger); }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.icon-btn {
  border: 0; background: transparent; cursor: pointer; font: inherit; font-size: 18px; line-height: 1; color: var(--muted);
  padding: 5px 8px; border-radius: 6px; flex: none;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.link-btn { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 6px 0; font: inherit; text-align: left; }
.link-btn:hover { color: var(--text); }

/* ---------- forms and dialogs */
.form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field-label { font-size: 12px; font-weight: 600; color: var(--muted); }
.field-hint { font-size: 12px; color: var(--muted); }
.form-actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.form-error { color: var(--danger); font-size: 13px; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .12); }
.swatch.active { border-color: var(--text); }
.swatch-none { background: linear-gradient(135deg, transparent 45%, var(--danger) 45%, var(--danger) 55%, transparent 55%), var(--surface); }

.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(15, 12, 20, .45); display: flex;
  align-items: flex-start; justify-content: center; padding: 60px 16px; overflow-y: auto;
}
.modal { width: 100%; max-width: 440px; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow-lg); }
.modal-wide { max-width: 640px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 16px; }
.choose-list { display: flex; flex-direction: column; gap: 2px; max-height: 60vh; overflow-y: auto; }
.choose-item {
  display: flex; align-items: center; gap: 10px; text-align: left; border: 0; background: none; color: var(--text);
  font: inherit; padding: 9px 10px; border-radius: 6px; cursor: pointer;
}
.choose-item:hover { background: var(--hover); }
.shortcuts { border-collapse: collapse; width: 100%; }
.shortcuts td { padding: 6px 8px; border-bottom: 1px solid var(--border); }
kbd { font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 1px 6px; white-space: nowrap; }

.popup-menu {
  position: fixed; z-index: 60; min-width: 200px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: var(--shadow-lg); padding: 5px; display: flex; flex-direction: column;
}
.popup-menu > button { text-align: left; padding: 8px 10px; border-radius: 5px; color: var(--text); background: none; border: 0; font: inherit; cursor: pointer; white-space: nowrap; }
.popup-menu > button:hover { background: var(--hover); }
.popup-menu > button.danger { color: var(--danger); }
.popup-sep { height: 1px; background: var(--border); margin: 4px 2px; }
.popup-palette { min-width: 0; }
.palette { display: grid; grid-template-columns: repeat(5, 26px); gap: 6px; padding: 4px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 100; background: #2b2830; color: #fff;
  padding: 10px 18px; border-radius: 8px; box-shadow: var(--shadow-lg); max-width: calc(100% - 32px);
}
.toast-error { background: var(--danger); }

/* ---------- login */
.login-page { height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg);
  padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
.login-logo { display: flex; align-items: center; gap: 10px; font-size: 20px; font-weight: 700; margin-bottom: 6px; }
.login-logo img { width: 34px; height: 34px; }

/* ---------- shell */
.shell { height: 100%; display: flex; flex-direction: column; }
.topbar {
  flex: none; display: flex; align-items: center; gap: 12px; height: 50px; padding: 0 10px 0 14px;
  background: var(--primary); color: #fff;
}
:root[data-theme="dark"] .topbar { background: #3b1654; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .topbar { background: #3b1654; } }
.topbar .icon-btn { color: rgba(255, 255, 255, .85); }
.topbar .icon-btn:hover { background: rgba(255, 255, 255, .15); color: #fff; }
.logo { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; color: #fff; text-decoration: none; }
.logo img { width: 24px; height: 24px; border-radius: 5px; box-shadow: 0 0 0 1px rgba(255, 255, 255, .5); }
.nav-toggle { display: none; }
.search-wrap { position: relative; flex: 0 1 460px; margin: 0 auto; }
.search-input { background: rgba(255, 255, 255, .92) !important; color: #201f24 !important; border: 0 !important; height: 34px; }
.search-results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0; z-index: 40; max-height: 70vh; overflow-y: auto;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow-lg); padding: 4px;
}
.sr-item { display: block; width: 100%; text-align: left; border: 0; background: none; color: inherit; font: inherit; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.sr-item:hover { background: var(--hover); }
.sr-title { font-weight: 600; }
.sr-where { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin: 2px 0; }
.sr-snippet { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
mark { background: var(--mark); color: inherit; border-radius: 2px; }
::highlight(search) { background-color: rgba(255, 196, 0, .6); color: inherit; }
.save-status { font-size: 12px; color: rgba(255, 255, 255, .8); min-width: 80px; text-align: right; white-space: nowrap; }
.save-status.status-error { color: #fff; background: var(--danger); padding: 2px 8px; border-radius: 99px; }

.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 250px 280px 1fr; }
.pane { min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.pane-head { flex: none; display: flex; align-items: center; gap: 8px; padding: 14px 14px 10px; }
.back-btn { display: none; }

/* ---------- notebooks */
.nav-pane { background: var(--nav-bg); border-right: 1px solid var(--border); }
.nav-pane .pane-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.notebooks { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.notebook { margin-bottom: 4px; }
.notebook-head, .section-item {
  display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 4px; border-radius: 6px; cursor: pointer; user-select: none;
}
.notebook-head:hover, .section-item:hover { background: var(--hover); }
.notebook-head { font-weight: 600; }
.chev { width: 14px; text-align: center; color: var(--muted); font-size: 11px; flex: none; }
.nb-icon { width: 14px; height: 18px; border-radius: 2px 4px 4px 2px; flex: none; box-shadow: inset 3px 0 0 rgba(0, 0, 0, .18); }
.nb-name, .sec-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sections { padding-left: 16px; }
.section-item.active { background: var(--active); font-weight: 600; }
.sec-tab { width: 5px; height: 16px; border-radius: 2px; flex: none; display: inline-block; }
.sec-count { font-size: 11px; color: var(--muted); }
.more { font-size: 16px; padding: 2px 6px; opacity: 0; }
.notebook-head:hover .more, .section-item:hover .more, .page-item:hover .more, .section-item.active .more, .page-item.active .more { opacity: 1; }
.add-link { border: 0; background: none; color: var(--muted); cursor: pointer; font: inherit; font-size: 13px; padding: 4px 6px 4px 38px; }
.add-link:hover { color: var(--primary); }
.nav-foot { flex: none; padding: 10px 12px 14px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; }
.ghost { opacity: .35; }

/* ---------- pages */
.pages-pane { background: var(--surface); border-right: 1px solid var(--border); }
.pages-head { border-top: 4px solid var(--primary); padding-top: 10px; border-bottom: 1px solid var(--border); }
.pages-title { flex: 1; min-width: 0; }
.pages-title h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-list { flex: 1; overflow-y: auto; padding: 6px; }
.page-item { display: flex; align-items: center; gap: 4px; padding: 7px 6px 7px 10px; border-radius: 6px; cursor: pointer; user-select: none; }
.page-item:hover { background: var(--hover); }
.page-item.active { background: var(--active); }
.page-item.level-1 { margin-left: 18px; }
.page-item.level-2 { margin-left: 36px; }
.page-item-main { flex: 1; min-width: 0; }
.page-item-title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-item.level-0 .page-item-title { font-weight: 600; }
.page-item-title.untitled { color: var(--muted); font-style: italic; }
.page-item-sub { display: flex; gap: 6px; font-size: 12px; color: var(--muted); overflow: hidden; white-space: nowrap; }
.page-date { flex: none; }
.page-preview { overflow: hidden; text-overflow: ellipsis; }

/* ---------- editor */
.editor-pane { background: var(--bg); }
.editor-top { flex: none; display: flex; align-items: center; gap: 4px; background: var(--surface); border-bottom: 1px solid var(--border); padding: 4px 8px; }
.toolbar { flex: 1; display: flex; align-items: center; flex-wrap: wrap; gap: 2px; min-width: 0; }
.tb-group { display: flex; align-items: center; gap: 1px; padding-right: 3px; margin-right: 3px; border-right: 1px solid var(--border); }
.tb-group:last-child, .tb-right { border-right: 0; }
.tb-right { margin-left: auto; }
.tb-btn {
  min-width: 28px; height: 30px; border: 0; background: none; color: var(--text); border-radius: 5px; cursor: pointer;
  font: inherit; font-size: 15px; padding: 0 4px; line-height: 1;
}
.tb-btn:hover { background: var(--hover); }
.tb-btn.on { background: var(--active); color: var(--primary); }
.tb-select { width: auto; height: 30px; padding: 0 6px; font-size: 13px; border-color: transparent; background: none; }
.tb-select:hover { border-color: var(--border); }
.tb-marker { background: #fff176; color: #201f24; padding: 0 3px; border-radius: 2px; font-weight: 600; }
.tb-color { border-bottom: 3px solid #d9434b; font-weight: 700; padding: 0 2px; }
.conflict { flex: none; background: #fff4ce; color: #4a3b00; padding: 8px 12px; border-bottom: 1px solid #f0d77a; }
.conflict-inner { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.editor-scroll { flex: 1; overflow-y: auto; position: relative; padding: 24px; cursor: text; }
.paper {
  max-width: 900px; min-height: calc(100% - 8px); margin: 0 auto; background: var(--paper); border-radius: 4px;
  box-shadow: var(--shadow); padding: 36px 48px 120px;
}
.page-title { font-size: 28px !important; font-weight: 600; border: 0 !important; border-bottom: 1px solid var(--border) !important; border-radius: 0 !important; padding: 4px 0 8px !important; background: none !important; outline: none !important; }
.page-meta { font-size: 12px; color: var(--muted); margin: 8px 0 18px; }
.editor-empty { margin: auto; }
.empty-logo { width: 64px; height: 64px; opacity: .5; margin-bottom: 10px; }

.content {
  outline: none; min-height: 300px; font-size: 15px; line-height: 1.55; white-space: pre-wrap; tab-size: 4;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.content.is-empty::before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; position: absolute; }
.content.dropping { outline: 2px dashed var(--primary); outline-offset: 8px; }
.content > :first-child { margin-top: 0; }
.content p { margin: 0 0 .5em; }
.content h1 { font-size: 24px; margin: 1em 0 .4em; }
.content h2 { font-size: 20px; margin: 1em 0 .4em; }
.content h3 { font-size: 17px; margin: 1em 0 .3em; }
.content h1, .content h2, .content h3 { color: var(--primary); }
.content ul, .content ol { margin: 0 0 .5em; padding-left: 26px; }
.content blockquote { margin: 0 0 .5em; padding: 4px 14px; border-left: 3px solid var(--primary); color: var(--muted); }
.content pre { background: var(--surface-2); border-radius: 6px; padding: 10px 12px; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; overflow-x: auto; }
.content code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; }
.content hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }
.content img { max-width: 100%; height: auto; border-radius: 4px; vertical-align: bottom; cursor: pointer; }
.content img.selected { outline: 3px solid var(--primary); outline-offset: 2px; }
.content a { cursor: pointer; }
.content a.file-link { display: inline-block; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; text-decoration: none; color: var(--text); }
.content table { border-collapse: collapse; margin: 0 0 .8em; min-width: 50%; }
.content td, .content th { border: 1px solid var(--border); padding: 6px 10px; vertical-align: top; min-width: 60px; }
.content th { background: var(--surface-2); }
.content [style*="background-color"] { color: #201f24; }
/* to-do list: the checkbox is drawn in the left padding of each item */
.content ul.todo { list-style: none; padding-left: 2px; }
.content ul.todo > li { position: relative; padding-left: 28px; }
.content ul.todo > li::before {
  content: ""; position: absolute; left: 0; top: .2em; width: 16px; height: 16px; border: 1.5px solid var(--muted);
  border-radius: 3px; background: var(--surface); cursor: pointer; box-sizing: border-box;
}
.content ul.todo > li.checked::before {
  background: var(--primary); border-color: var(--primary);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='white' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}
.content ul.todo > li.checked { color: var(--muted); text-decoration: line-through; }

.bubble {
  position: absolute; z-index: 5; display: flex; align-items: center; gap: 2px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: var(--shadow-lg); padding: 3px; font-size: 13px; white-space: nowrap;
}
.bubble button { border: 0; background: none; color: var(--text); font: inherit; padding: 5px 8px; border-radius: 5px; cursor: pointer; }
.bubble button:hover { background: var(--hover); }
.bubble button.danger { color: var(--danger); }
.bubble-url { color: var(--muted); padding: 0 6px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; }

/* ---------- recycle bin */
.trash-list { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-direction: column; }
.trash-list li { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.trash-main { flex: 1; min-width: 0; }

/* ---------- tablets: the notebooks pane gets narrower */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 210px 240px 1fr; }
  .paper { padding: 28px 28px 100px; }
  .editor-scroll { padding: 14px; }
}

/* ---------- phones: one pane at a time */
@media (max-width: 800px) {
  .topbar { gap: 6px; padding: 0 6px; }
  .nav-toggle { display: inline-block; }
  .logo-text { display: none; }
  .save-status { min-width: 0; }
  .search-results { position: fixed; top: 50px; left: 6px; right: 6px; }
  .layout { display: block; position: relative; }
  .pane { position: absolute; inset: 0; }
  .layout .pane { display: none; }
  .layout.view-nav .nav-pane, .layout.view-pages .pages-pane, .layout.view-editor .editor-pane { display: flex; }
  .back-btn { display: inline-block; }
  .more { opacity: 1; }
  .notebook-head, .section-item, .page-item { padding-top: 10px; padding-bottom: 10px; }
  .editor-top { align-items: flex-start; }
  .toolbar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .tb-right { margin-left: 0; }
  .editor-scroll { padding: 0; }
  .paper { border-radius: 0; box-shadow: none; padding: 16px 16px 80px; min-height: 100%; }
  .page-title { font-size: 22px !important; }
  .modal-backdrop { padding: 16px 8px; }
}

/* ---------- printing: just the page */
@media print {
  body { overflow: visible; background: #fff; }
  .topbar, .nav-pane, .pages-pane, .editor-top, .conflict, .bubble, .toast { display: none !important; }
  .shell, .layout, .pane, .editor-scroll { display: block !important; position: static !important; overflow: visible !important; height: auto !important; }
  .paper { box-shadow: none; padding: 0; max-width: none; }
  .page-title { border: 0 !important; }
}
