/* ---------- Tokens ---------- */
/* One set of variables; light-dark() picks the value from the resolved colour
   scheme, which follows the system unless <html data-theme> overrides it. */
:root {
  color-scheme: light dark;

  /* Warm charcoal (dark) and soft neutral grey (light) surfaces, one blue accent.
     Amber only around writing: caret, selection, search match, pin. */
  --bg: light-dark(#ebeae7, #191715);        /* sidebar and page chrome */
  --surface: light-dark(#f8f8f6, #211f1c);   /* writing surface, selected row */
  --ink: light-dark(#1d1c1a, #ede8e0);
  --muted: light-dark(#5e5a54, #aba398);
  --line: light-dark(#d9d6d1, #36322d);
  --hover: light-dark(#e1dfdb, #2a2723);
  --accent: light-dark(#2c64c4, #8fb8f0);     /* fills: primary button, checked segment */
  --on-accent: light-dark(#ffffff, #191715);
  --link: var(--accent);                      /* accent as text, focus ring, active marker */
  --danger: light-dark(#b3261e, #f09b90);
  --amber: light-dark(#9a5f00, #eab04e);      /* pin marker, caret */
  --mark: light-dark(#f6e2a4, #50401c);       /* search match */
  --select: light-dark(#f3dfa2, #5c4a21);     /* text selection */
  --toast-bg: var(--ink);
  --toast-ink: var(--surface);

  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  --tap: 40px;
  --radius: 8px;
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }
@media (pointer: coarse) { :root { --tap: 44px; } }

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }
:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.muted { color: var(--muted); }
.small { font-size: 0.8125rem; }
kbd {
  display: inline-block; min-width: 1.5em; padding: 0 0.4em;
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px;
  background: var(--surface); color: var(--ink);
  font: 0.75rem/1.5 var(--mono); text-align: center;
}
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }
::selection { background: var(--select); color: inherit; }
input, textarea { caret-color: var(--amber); }
.noscript { padding: 24px; }

/* ---------- Controls ---------- */
.icon { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: var(--tap); height: var(--tap);
  border: 0; border-radius: var(--radius); background: none; color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--ink); }
.icon-btn[aria-pressed="true"] { color: var(--amber); }
.icon-btn[aria-pressed="true"] .icon { fill: currentColor; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 14px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--ink); font-weight: 500; font-size: 0.9375rem;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: minmax(18rem, 22rem) 1fr; height: 100dvh; }
.sidebar {
  display: flex; flex-direction: column; min-height: 0;
  border-right: 1px solid var(--line);
  padding-left: env(safe-area-inset-left);
}
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--surface); }
.only-narrow { display: none; }

@media (max-width: 759px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; }
  .app[data-view="list"] .pane,
  .app[data-view="editor"] .sidebar { display: none; }
  .only-narrow { display: inline-grid; }
}

/* ---------- Sidebar ---------- */
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 8px 4px 16px;
}
.brand { margin: 0; line-height: 0; }
.brand img { height: 28px; width: auto; }
.logo-light { display: block; }
.logo-dark { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo-light { display: none; }
  :root:not([data-theme="light"]) .logo-dark { display: block; }
}
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: block; }

.tools { display: flex; gap: 8px; padding: 8px 16px 12px; }
.search {
  position: relative; flex: 1; min-width: 0; display: block; color: var(--muted);
}
.search .icon { position: absolute; left: 11px; top: 50%; translate: 0 -50%; pointer-events: none; }
.search input {
  width: 100%; height: var(--tap); padding: 0 10px 0 38px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--ink); font-size: 1rem;
}
.search input::placeholder { color: var(--muted); opacity: 1; }
.search input:focus-visible { outline-offset: 0; border-color: var(--link); }

.list-scroll { flex: 1; min-height: 0; overflow-y: auto; padding-bottom: 24px; scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
.group-label {
  padding: 14px 16px 6px; color: var(--muted);
  font: 600 0.6875rem/1 var(--sans); letter-spacing: 0.08em; text-transform: uppercase;
}
.note-list { list-style: none; margin: 0; padding: 0; }
.note-link {
  display: grid; grid-template-columns: 1fr auto; column-gap: 12px; row-gap: 2px;
  padding: 10px 16px; border-top: 1px solid var(--line);
  color: inherit; text-decoration: none;
}
.note-list li:last-child .note-link { border-bottom: 1px solid var(--line); }
.note-link:hover { background: var(--hover); }
.note-link[aria-current="page"] { background: var(--surface); box-shadow: inset 3px 0 0 var(--link); }
.note-link:focus-visible { outline-offset: -2px; }
.note-title {
  font: 650 1rem/1.35 var(--sans); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.note-date {
  display: flex; align-items: center; gap: 5px; align-self: center;
  color: var(--muted); font-size: 0.75rem; font-variant-numeric: tabular-nums; white-space: nowrap;
}
.note-date .icon { width: 12px; height: 12px; fill: currentColor; color: var(--amber); }
.note-preview {
  grid-column: 1 / -1; color: var(--muted); font-size: 0.875rem; line-height: 1.4;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.note-preview.is-empty { font-style: italic; }

.list-empty { padding: 28px 20px; max-width: 22rem; }
.list-empty h2 { font: 650 1.125rem/1.3 var(--sans); margin-bottom: 6px; }
.list-empty p { color: var(--muted); margin-bottom: 16px; }

/* ---------- Editor ---------- */
.editor { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.editor-bar {
  display: flex; align-items: center; gap: 2px; flex: none;
  padding: 8px 10px 8px max(10px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
}
.status { margin-left: auto; margin-right: 8px; color: var(--muted); font-size: 0.8125rem; }
.status[data-state="failed"] { color: var(--danger); font-weight: 600; }
.editor-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.doc { max-width: 46rem; margin: 0 auto; padding: 36px 40px 30vh; }
.doc-title {
  display: block; width: 100%; padding: 0; border: 0; outline: 0; background: none;
  font: 750 2rem/1.2 var(--sans); letter-spacing: -0.02em;
}
.doc-title::placeholder, .doc-body::placeholder { color: var(--muted); opacity: 0.7; }
.doc-meta { margin: 8px 0 22px; color: var(--muted); font-size: 0.8125rem; font-variant-numeric: tabular-nums; }
.doc-body {
  display: block; width: 100%; min-height: 50dvh; padding: 0; border: 0; outline: 0; resize: none;
  background: none; overflow: hidden;
  font: 1.0625rem/1.7 var(--sans);
}

@media (max-width: 759px) {
  .doc { padding: 20px 18px 40vh; }
  .doc-title { font-size: 1.75rem; }
  .doc-body { font-size: 1.0625rem; }
}

.blank { margin: auto; padding: 24px; max-width: 26rem; }
.blank h2 { font: 700 1.5rem/1.3 var(--sans); margin-bottom: 8px; }
.blank p { color: var(--muted); margin-bottom: 10px; }
.blank .hint { font-size: 0.875rem; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 16px; bottom: max(16px, env(safe-area-inset-bottom)); z-index: 20;
  display: flex; align-items: center; gap: 16px; max-width: min(28rem, calc(100vw - 32px));
  padding: 10px 10px 10px 16px; border-radius: var(--radius);
  background: var(--toast-bg); color: var(--toast-ink); font-size: 0.9375rem;
}
.toast button {
  flex: none; min-height: 36px; padding: 0 12px; border: 0; border-radius: 6px;
  background: none; color: inherit; font-weight: 700; text-decoration: underline; text-underline-offset: 3px;
}
.toast button:hover { background: color-mix(in srgb, currentColor 14%, transparent); }
.toast button:focus-visible { outline-color: currentColor; }
@media (max-width: 759px) { .toast { left: 12px; right: 12px; max-width: none; } }

/* ---------- Settings dialog ---------- */
dialog {
  width: min(34rem, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  padding: 0; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); color: var(--ink);
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgb(12 11 10 / 0.5); } /* ::backdrop may not inherit tokens */
.dialog-head {
  display: flex; align-items: center; justify-content: space-between; flex: none;
  padding: 8px 8px 8px 20px; border-bottom: 1px solid var(--line);
}
.dialog-head h2 { font: 700 1.25rem/1.2 var(--sans); }
.dialog-body { overflow-y: auto; padding: 4px 20px 20px; }
.group { padding: 18px 0; border-top: 1px solid var(--line); }
.group:first-child { border-top: 0; }
.group > h3 {
  padding: 0; margin-bottom: 10px; font: 600 0.6875rem/1.2 var(--sans);
  letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted);
}
.group p + p { margin-top: 8px; }
.keys + p { margin-top: 10px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }

.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span {
  display: grid; place-items: center; min-width: 5.5rem; min-height: var(--tap); padding: 0 14px;
  color: var(--muted); font-size: 0.9375rem;
}
.segmented label + label span { border-left: 1px solid var(--line); }
.segmented input:checked + span { background: var(--accent); color: var(--on-accent); font-weight: 600; }
.segmented input:focus-visible + span { outline: 2px solid var(--link); outline-offset: -3px; }

.keys { margin: 0; display: grid; gap: 6px; }
.keys div { display: flex; justify-content: space-between; gap: 16px; font-size: 0.9375rem; }
.keys dt { white-space: nowrap; }
.keys dd { margin: 0; color: var(--muted); text-align: right; }

/* ---------- 404 and other static pages ---------- */
body.static { height: auto; min-height: 100dvh; overflow: auto; background: var(--surface); }
.lost { max-width: 30rem; margin: 0 auto; padding: 16vh 24px 48px; }
.lost img { height: 26px; width: auto; margin-bottom: 48px; }
.lost .code { font: 0.8125rem/1 var(--mono); color: var(--muted); letter-spacing: 0.04em; margin-bottom: 14px; }
.lost h1 { font: 750 2rem/1.2 var(--sans); letter-spacing: -0.02em; margin-bottom: 10px; }
.lost p { color: var(--muted); margin-bottom: 24px; }
.lost a.btn { text-decoration: none; }

/* ---------- Motion ---------- */
@media (prefers-reduced-motion: no-preference) {
  .btn, .icon-btn, .note-link, .toast button { transition: background-color 0.12s, border-color 0.12s, color 0.12s; }
}
