:root {
  color-scheme: dark;
  --background: #18181b;
  --page: #0d0d0f;
  --surface: #27272a;
  --border: #3f3f46;
  --text: #fafafa;
  --muted: #a1a1aa;
  --accent: #93c5fd;
  --danger: #fca5a5;
  --selected: rgba(59, 130, 246, 0.16);
  --selected-border: rgba(59, 130, 246, 0.65);
  --footer-height: 42px;
  --sans: "Segoe UI Variable", "Segoe UI", system-ui, sans-serif;
  --mono: "Cascadia Mono", Consolas, monospace;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { background: var(--page); height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-rows: 36px 48px auto minmax(0, 1fr) var(--footer-height);
  background: var(--background);
  color: var(--text);
  border: 1px solid var(--border);
  font: 14px / 20px var(--sans);
}
button, input, select { font: inherit; color: inherit; }
button, select { cursor: pointer; }
button:disabled { opacity: .45; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.titlebar, .searchbar, .folderbar, footer {
  display: flex;
  align-items: center;
  min-width: 0;
  padding: 0 16px;
}
.titlebar {
  -webkit-app-region: drag;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  padding-right: 0;
}
.titlebar:has([data-size="full"][aria-pressed="true"]) { -webkit-app-region: no-drag; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; }
#build-version { color: var(--muted); font-size: 11px; font-weight: 400; }
.window-actions, .size-presets {
  -webkit-app-region: no-drag;
  display: flex;
  align-items: center;
}
.window-actions { height: 100%; gap: 8px; }
.size-presets {
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 7px;
}
.size-presets .size-preset {
  display: grid;
  place-items: center;
  width: 27px;
  height: 23px;
  padding: 0;
  border-radius: 4px;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}
.size-presets button[aria-pressed="true"] {
  border-color: var(--selected-border);
  background: var(--selected);
  color: var(--accent);
}
/* One window glyph at three scales; fullscreen uses the same icon font. */
[data-size="compact"] .icon { font-size: 11px; }
[data-size="default"] .icon { font-size: 14px; }
[data-size="large"] .icon { font-size: 17px; }
[data-size="full"] .icon { font-size: 15px; }
.mark {
  color: var(--accent);
  background: var(--selected);
  border-radius: 4px;
  width: 20px;
  line-height: 20px;
  text-align: center;
}
.close {
  -webkit-app-region: no-drag;
  width: 44px;
  height: 100%;
  border: 0;
  background: transparent;
  font-size: 20px;
}
.close:hover { background: var(--surface); }
.searchbar { gap: 12px; border-bottom: 1px solid var(--border); }
.search-icon {
  width: 14px;
  height: 14px;
  border: 1.5px solid var(--muted);
  border-radius: 50%;
  position: relative;
  flex: none;
}
.search-icon::after {
  content: "";
  position: absolute;
  width: 5px;
  height: 1.5px;
  background: var(--muted);
  right: -4px;
  bottom: -2px;
  transform: rotate(45deg);
}
#search {
  flex: 1;
  min-width: 0;
  height: 36px;
  border: 0;
  border-radius: 6px;
  padding: 0 8px;
  background: transparent;
  font-size: 16px;
  line-height: 24px;
}
input::placeholder { color: var(--muted); }
select {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  padding: 6px 8px;
}
.folderbar {
  gap: 8px;
  flex-wrap: wrap;
  padding-block: 8px;
  border-bottom: 1px solid var(--border);
}
.folderbar select { flex: 1 1 140px; min-width: 0; max-width: 520px; }
.icon {
  display: inline-block;
  font: 13px / 1 "Segoe Fluent Icons", "Segoe MDL2 Assets", sans-serif;
  vertical-align: -1px;
}
.folder-icon {
  position: relative;
  flex: 0 0 16px;
  height: 11px;
  margin: 3px 2px 0;
  border: 1.5px solid var(--accent);
  border-radius: 2px;
}
.folder-icon::before {
  content: "";
  position: absolute;
  top: -4px;
  left: -1px;
  width: 7px;
  height: 4px;
  border: 1.5px solid var(--accent);
  border-bottom: 0;
  border-radius: 2px 2px 0 0;
}
button:not(.close) {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  padding: 5px 8px;
  font-size: 12px;
  line-height: 18px;
  white-space: nowrap;
}
button:not(.close):not(:disabled):hover { border-color: #71717a; }
button.accent { background: var(--selected); border-color: var(--selected-border); }
button.quiet { background: transparent; border-color: transparent; }
button.danger { color: var(--danger); }
button.danger-solid { background: #7f1d1d; border-color: #b91c1c; color: #fff; }
main { display: grid; grid-template-columns: minmax(0, min(40%, 400px)) minmax(0, 1fr); min-height: 0; }
body[data-view="folders"] { grid-template-rows: 36px auto minmax(0, 1fr) var(--footer-height); }
body[data-view="folders"] .searchbar,
body[data-view="folders"] main > :is(.list-pane, .preview) { display: none; }
body[data-view="folders"] main { grid-template-columns: minmax(0, 1fr); }
#folder-view { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.tree-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: min(100%, 900px);
  padding: 12px 16px 8px;
}
.tree-heading h1 { margin: 0; font-size: 16px; }
#folder-tree { width: min(100%, 900px); overflow: auto; padding: 2px 12px 12px; }
.tree-children { margin-left: 12px; padding-left: 10px; border-left: 1px solid var(--border); }
.tree-row {
  display: grid;
  grid-template-columns: 18px 15px minmax(0, 1fr);
  align-items: center;
  gap: 5px;
  min-height: 29px;
  padding: 2px 6px;
  border: 1px solid transparent;
  border-radius: 5px;
  cursor: pointer;
}
.tree-row:hover { background: var(--surface); }
.tree-arrow { color: var(--muted); font-size: 16px; text-align: center; }
.tree-arrow[data-disclosure] { cursor: pointer; }
#folder-tree [aria-selected="true"] > .tree-row { background: var(--selected); border-color: var(--selected-border); }
#folder-tree [role="treeitem"]:focus-visible { outline: none; }
#folder-tree [role="treeitem"]:focus-visible > .tree-row { outline: 2px solid var(--accent); outline-offset: -2px; }
.actions-menu {
  position: fixed;
  inset: auto;
  top: anchor(bottom);
  right: anchor(right);
  margin: 6px 0 0;
  padding: 5px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #202024;
  color: var(--text);
  box-shadow: 0 8px 26px #0009;
}
#folder-actions { display: flex; align-items: center; gap: 2px; }
#library-actions-button { anchor-name: --library-actions; }
#library-actions { position-anchor: --library-actions; }
.actions-menu:popover-open { display: grid; gap: 3px; }
.actions-menu button { text-align: left; width: 100%; }
.list-pane, .preview { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.list-pane { border-right: 1px solid var(--border); }
#folder-browser {
  flex: none;
  margin: 6px 8px 0;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
#folder-browser summary {
  padding: 3px 8px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
}
#folder-count { margin-left: 4px; }
#folder-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 105px;
  overflow: auto;
}
#folder-list button {
  display: grid;
  grid-template-columns: 16px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 3px 7px;
  text-align: left;
  background: transparent;
  border-color: transparent;
  color: #d4d4d8;
}
#folder-list button:hover { background: var(--surface); }
.folder-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-chevron { color: var(--muted); font-size: 17px; }
.row-icon { width: 14px; height: 9px; margin-inline: 0; border-color: #8fa7bf; }
.row-icon::before { border-color: #8fa7bf; }
.list-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 10px 12px 4px 16px;
  color: var(--muted);
  font-size: 12px;
}
#count { margin-left: 4px; }
#results { list-style: none; overflow-y: auto; padding: 4px 8px 8px; margin: 0; }
#results li {
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr);
  column-gap: 7px;
  padding: 4px 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
}
.note-icon {
  grid-row: span 2;
  align-self: center;
  width: 11px;
  height: 14px;
  border: 1px solid var(--muted);
  border-radius: 2px;
  background: linear-gradient(var(--muted), var(--muted)) center 5px / 5px 1px no-repeat,
              linear-gradient(var(--muted), var(--muted)) center 8px / 5px 1px no-repeat;
}
.result-title, .result-location {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.result-title {
  font-weight: 600;
}
.result-location { color: var(--muted); font-size: 11px; line-height: 16px; }
#results li:hover { background: var(--surface); }
#results li[aria-selected="true"] { background: var(--selected); border-color: var(--selected-border); }
#empty { color: var(--muted); margin: 12px 16px; }
.preview-heading { padding: 16px 20px 8px; }
#reader, #editor { display: flex; flex: 1; flex-direction: column; min-height: 0; }
#path { margin: 0 0 4px; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.preview-heading h1 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 18px;
  line-height: 24px;
  margin: 0;
  overflow-wrap: anywhere;
}
.preview-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px 8px;
  margin: 0 16px 8px;
  max-width: 760px;
}
.preview-mode, .snippet-actions { display: flex; align-items: center; gap: 2px; }
.preview-mode {
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.preview-mode button { background: transparent; border-color: transparent; color: var(--muted); }
.preview-mode button[aria-pressed="true"] {
  background: var(--selected);
  border-color: var(--selected-border);
  color: var(--accent);
}
.markdown-icon { font-family: var(--mono); color: #c4b5fd; }
#body, #markdown {
  margin: 0 8px 8px;
  padding: 4px 12px 12px;
  min-height: 0;
  overflow: auto;
  overflow-wrap: anywhere;
  font: 14px / 22px var(--sans);
}
#body { white-space: pre-wrap; max-width: 78ch; }
#markdown > :first-child { margin-top: 0; }
#markdown h1, #markdown h2, #markdown h3 { line-height: 1.3; margin-bottom: 8px; }
#markdown h1 { font-size: 24px; }
#markdown h2 { font-size: 20px; }
#markdown h3 { font-size: 16px; }
#markdown p { margin: 0 0 12px; }
#markdown :is(p, ul, ol, blockquote) { max-width: 72ch; }
#markdown ul, #markdown ol { padding-left: 24px; }
#markdown blockquote { margin: 0 0 12px; padding: 4px 12px; border-left: 3px solid #a78bfa; color: #d4d4d8; }
#markdown blockquote > :last-child { margin-bottom: 0; }
#markdown code, #markdown pre { font: 12px / 19px var(--mono); background: var(--surface); border-radius: 4px; }
#markdown code { padding: 2px 4px; }
#markdown pre { padding: 8px 12px; overflow: auto; }
#markdown pre code { padding: 0; }
#markdown table { border-collapse: collapse; margin-bottom: 12px; }
#markdown th, #markdown td { border: 1px solid var(--border); padding: 4px 8px; }
#markdown th { background: var(--surface); }
.md-link { color: #c4b5fd; text-decoration: underline; }
footer {
  gap: 10px;
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--background) 85%, black);
  font: 12px / 18px var(--sans);
}
#toggle-folder-tree {
  display: flex;
  align-items: center;
  gap: 5px;
  flex: none;
  height: 26px;
  padding: 0 8px;
  border: 1px solid #484850;
  border-radius: 5px;
  background: linear-gradient(#303036, #232328);
  color: #bdbdc5;
  box-shadow: inset 0 1px #ffffff19, 0 1px 1px #0009;
}
#toggle-folder-tree:hover { background: linear-gradient(#41414d, #303039); }
#toggle-folder-tree[aria-pressed="true"] {
  background: linear-gradient(#223046, #2b3b55);
  border-color: #5f7da3;
  color: #c2d9fb;
  box-shadow: inset 0 2px 4px #101c2d, 0 1px 0 #ffffff17;
}
#toggle-folder-tree:active { transform: translateY(1px); box-shadow: inset 0 2px 4px #0008; }
.tree-toggle-icon {
  width: 15px;
  height: 13px;
  background:
    linear-gradient(currentColor, currentColor) 2px 1px / 1px 10px no-repeat,
    linear-gradient(currentColor, currentColor) 2px 5px / 5px 1px no-repeat,
    linear-gradient(currentColor, currentColor) 2px 10px / 5px 1px no-repeat,
    linear-gradient(currentColor, currentColor) 7px 2px / 7px 1px no-repeat,
    linear-gradient(currentColor, currentColor) 7px 6px / 7px 1px no-repeat,
    linear-gradient(currentColor, currentColor) 7px 11px / 7px 1px no-repeat;
}
.footer-state { display: flex; flex: 1; align-items: center; gap: 7px; min-width: 0; }
.footer-hints { min-width: 0; max-width: 45%; text-align: right; }
.footer-hints:has(#status:not(:empty)) { max-width: 60%; }
.footer-hints:has(#status:not(:empty), .shortcut:not(#shortcut):not([hidden])) #shortcut,
.footer-hints:has(#status:not(:empty)) .shortcut { display: none; }
#copy-hint[hidden] { display: inline !important; visibility: hidden; }
#footer-context, #status { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#footer-context { color: #d4d4d8; }
#footer-count, #footer-draft { flex: none; white-space: nowrap; }
#footer-count { color: var(--muted); }
#footer-count::before, #footer-draft::before { content: "·"; margin-right: 7px; color: var(--muted); }
#footer-draft { color: #f5cd89; }
#status { color: var(--accent); }
#status[data-error="true"] { color: var(--danger); }
.shortcut { color: var(--muted); white-space: nowrap; }
#copy {
  flex: none;
  width: 90px;
  background: var(--selected);
  border: 1px solid var(--selected-border);
  border-radius: 6px;
  padding: 3px 8px;
  font-weight: 600;
}
#copy:not(:disabled):hover { background: var(--surface); }
kbd { margin-left: 4px; color: var(--muted); font: 11px var(--mono); }

dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #202024;
  color: var(--text);
  box-shadow: 0 16px 48px #0008;
}
dialog::backdrop { background: #0009; }
dialog h2, #editor h2 { margin: 0 0 8px; font-size: 18px; line-height: 24px; }
#editor-form label { display: block; color: #d4d4d8; margin: 8px 0 4px; font-size: 12px; }
#editor-form input, #editor-form select, #editor-form textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 8px;
  color: var(--text);
  background: var(--background);
  font: inherit;
}
#editor-form textarea { font-family: var(--mono); resize: vertical; }
#editor { padding: 12px 16px; }
#editor form { display: flex; flex: 1; flex-direction: column; min-height: 0; }
#editor .editor-fields {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
  overflow: auto;
  padding-inline: 1px;
}
#editor .editor-fields > :not(#body-field) { flex: none; }
#editor #body-field { display: flex; flex: 1; flex-direction: column; min-height: 134px; }
#editor textarea { flex: 1; min-height: 108px; resize: none; }
#editor .dialog-actions { flex: none; margin-top: 8px; }
.form-error { color: var(--danger); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

@media (max-width: 700px) {
  .folderbar, .searchbar, footer { padding-inline: 12px; }
  .preview-heading { padding: 12px 12px 8px; }
  .preview-tools { margin-inline: 10px; }
  .list-heading { padding-inline: 10px; }
  #body, #markdown { padding-inline: 4px; }
  #results li { padding-block: 2px; }
  .folderbar { padding-block: 5px; }
  #folder-list { max-height: 61px; }
  #editor .editor-fields {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: auto auto minmax(114px, 1fr) auto;
    column-gap: 8px;
  }
  #editor label[for="item-name"] { grid-area: 1 / 1; }
  #editor label[for="item-parent"] { grid-area: 1 / 2; }
  #editor #item-name { grid-area: 2 / 1; }
  #editor #item-parent { grid-area: 2 / 2; }
  #editor #body-field { grid-area: 3 / 1 / 4 / -1; min-height: 114px; }
  #editor textarea { min-height: 88px; }
  #editor-error { grid-column: 1 / -1; }
  footer { gap: 8px; }
  footer:has(#footer-draft:not([hidden])) #tree-hint { display: none; }
}

#result-pages { display: flex; flex: 0 0 auto; align-items: center; justify-content: space-between; gap: 4px; padding: 4px 8px; border-top: 1px solid var(--border); }
#result-pages button { padding: 3px 6px; }
#page-range { font-size: 11px; white-space: nowrap; color: var(--muted); }

/* ------------------------------------------------------------------ */
/* Web-only: render the desktop size presets as a centered "window"   */
/* inside the page. `full` keeps the original full-viewport behavior. */
/* ------------------------------------------------------------------ */
html { height: 100%; }
body { height: 100vh; }

body[data-palette-preset="compact"],
body[data-palette-preset="default"],
body[data-palette-preset="large"] {
  height: auto;
  min-height: 0;
  margin: 0 auto;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 24px 60px #000a, 0 2px 6px #0006;
}

.web-help, .actions-menu a {
  color: var(--accent);
  padding: 4px 8px;
  text-decoration: none;
}
.web-help:hover, .actions-menu a:hover { text-decoration: underline; }
#slash-search { flex: none; min-width: 29px; border: 1px solid var(--border); font: 14px var(--mono); }
.clipboard-fallback { position: fixed; left: -10000px; }
.size-preset .icon::before {
  content: "";
  display: block;
  width: .9em;
  height: .7em;
  border: 1px solid currentColor;
  border-radius: 2px;
}
[data-size="full"] .icon::before { border-style: dashed; }

@media (max-width: 540px) {
  .titlebar { padding-inline: 8px; }
  .window-actions { gap: 3px; }
  body .size-presets { display: none; }
  body:not([data-view="folders"]) main {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 35%) minmax(0, 65%);
  }
  body:not([data-view="folders"]) main:has(#editor:not([hidden])) {
    grid-template-rows: minmax(0, 1fr);
  }
  main:has(#editor:not([hidden])) .list-pane { display: none; }
  #search-hint, #tree-hint { display: none; }
  .preview-tools { flex-wrap: wrap; }
}
/* The body needs an explicit height for `1fr` rows to work inside a sized
   window. We bind each preset to the same pixel dimensions the desktop app
   uses, capped by the viewport so a small screen never clips the window. */
body[data-palette-preset="compact"] {
  width: min(640px, calc(100vw - 24px));
  height: min(450px, calc(100vh - 24px));
  margin-top: max(12px, calc((100vh - 450px) / 2));
  margin-bottom: max(12px, calc((100vh - 450px) / 2));
}
body[data-palette-preset="default"] {
  width: min(880px, calc(100vw - 24px));
  height: min(580px, calc(100vh - 24px));
  margin-top: max(12px, calc((100vh - 580px) / 2));
  margin-bottom: max(12px, calc((100vh - 580px) / 2));
}
body[data-palette-preset="large"] {
  width: min(1120px, calc(100vw - 24px));
  height: min(720px, calc(100vh - 24px));
  margin-top: max(12px, calc((100vh - 720px) / 2));
  margin-bottom: max(12px, calc((100vh - 720px) / 2));
}

/* On narrow viewports the centered window collapses to full width so the
   responsive rules above still apply. */
@media (max-width: 700px) {
  body[data-palette-preset="compact"],
  body[data-palette-preset="default"],
  body[data-palette-preset="large"] {
    width: 100vw;
    height: 100dvh;
    margin: 0;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
  }
}
