/* sheaf: a quiet, text-first design. Warm paper in light mode, soft charcoal in dark mode,
   one teal accent. The theme follows the OS; theme.js applies a saved override
   (data-theme="light" | "dark" on <html>) before first paint. */

:root {
  color-scheme: light;
  --bg: #fbfaf7;
  --surface: #ffffff;
  --fg: #23211e;
  --muted: #645f59;
  --line: #e4e0d8;
  --line-strong: #cfc9bf;
  --control: #857f76; /* borders of inputs and buttons: 3:1 against the page (WCAG 1.4.11) */
  --accent: #0b6660;
  --accent-fg: #ffffff;
  --accent-soft: #e2f0ee;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --code: #f1eee8;
  --mark: #fbe7a1;
  --shadow: 0 1px 2px rgb(35 33 30 / 6%), 0 4px 12px rgb(35 33 30 / 5%);

  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Source Serif 4", Charter, Georgia, serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --measure: 44rem;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #17181a;
    --surface: #1f2124;
    --fg: #e9e6e0;
    --muted: #a59f96;
    --line: #2f3135;
    --line-strong: #43464b;
    --control: #7d8189;
    --accent: #62bdb5;
    --accent-fg: #0d1a19;
    --accent-soft: #1b2f2d;
    --danger: #f19188;
    --danger-soft: #3a2220;
    --code: #232529;
    --mark: #5d4d12;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 12px rgb(0 0 0 / 25%);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #17181a;
  --surface: #1f2124;
  --fg: #e9e6e0;
  --muted: #a59f96;
  --line: #2f3135;
  --line-strong: #43464b;
  --control: #7d8189;
  --accent: #62bdb5;
  --accent-fg: #0d1a19;
  --accent-soft: #1b2f2d;
  --danger: #f19188;
  --danger-soft: #3a2220;
  --code: #232529;
  --mark: #5d4d12;
  --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 12px rgb(0 0 0 / 25%);
}

/* ---- base ---- */

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-underline-offset: .18em; text-decoration-thickness: 1px; }
a:hover { text-decoration-thickness: 2px; }
::selection { background: var(--accent-soft); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
[hidden] { display: none !important; }
hr { border: 0; border-top: 1px solid var(--line); margin: 2rem 0; }
code, pre, kbd { font-family: var(--mono); }
.muted, time { color: var(--muted); font-size: .875rem; }
time { font-variant-numeric: tabular-nums; white-space: nowrap; }

.skip { position: absolute; left: -999px; top: .5rem; background: var(--surface); padding: .5rem .75rem; border-radius: var(--radius); z-index: 10; }
.skip:focus { left: .5rem; }

/* ---- header ---- */

.bar {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: .25rem 1rem;
  max-width: var(--measure); margin: 0 auto; padding: 1rem 1.25rem .5rem;
}
.brand {
  display: inline-flex; align-items: center; gap: .5rem;
  font-weight: 700; font-size: 1.15rem; letter-spacing: -.01em; color: var(--fg); text-decoration: none;
}
.brand::before { content: ""; width: .6rem; height: .6rem; border-radius: 50%; background: var(--accent); }
.bar nav { display: flex; align-items: center; flex-wrap: wrap; gap: .15rem; }
.bar nav > a:not(.btn) { color: var(--muted); text-decoration: none; padding: .4rem .6rem; border-radius: var(--radius); font-size: .95rem; }
.bar nav > a:not(.btn):hover { color: var(--fg); background: var(--accent-soft); }
.bar nav .btn { margin-left: .35rem; background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.bar nav .btn:hover { filter: brightness(1.08); }
.bar nav form.inline { margin: 0 0 0 .15rem; }
.bar nav form button { border-color: transparent; background: transparent; color: var(--muted); }
.bar nav form button:hover { color: var(--fg); background: var(--accent-soft); }

main { max-width: var(--measure); margin: 0 auto; padding: 0 1.25rem 5rem; }
main:focus { outline: none; }
h1 { font-size: 1.75rem; line-height: 1.2; letter-spacing: -.015em; margin: 1.25rem 0 1rem; }
h2 { font-size: 1.2rem; }

.row { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem 1rem; flex-wrap: wrap; }

/* ---- controls ---- */

.btn, button {
  font: inherit; font-size: .95rem; line-height: 1.2;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 2.25rem; padding: .35rem .9rem;
  border: 1px solid var(--control); border-radius: var(--radius);
  background: var(--surface); color: var(--fg); text-decoration: none; cursor: pointer;
}
.btn:hover, button:hover { border-color: var(--accent); text-decoration: none; }
button:disabled { opacity: .5; cursor: default; }
.primary, button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.primary:hover, button.primary:hover { filter: brightness(1.08); }
.danger, button.danger { color: var(--danger); }
.danger:hover, button.danger:hover { background: var(--danger-soft); border-color: var(--danger); }

input, textarea, select {
  font: inherit; color: var(--fg); background: var(--surface);
  border: 1px solid var(--control); border-radius: var(--radius); padding: .5rem .7rem;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
input::placeholder { color: var(--muted); opacity: 1; }
input[type=checkbox] { accent-color: var(--accent); }

.icon-btn { min-width: 2.25rem; padding: .35rem .5rem; border-color: transparent; background: transparent; color: var(--muted); font-size: 1.05rem; }
.icon-btn:hover { color: var(--fg); background: var(--accent-soft); border-color: transparent; }

.error { color: var(--danger); background: var(--danger-soft); border: 1px solid var(--danger); border-radius: var(--radius); padding: .6rem .9rem; }
.banner {
  margin: 0 0 1rem; padding: .65rem .9rem; background: var(--accent-soft);
  border: 1px solid var(--accent); border-left-width: 4px; border-radius: var(--radius);
}
.live-status:empty { display: none; }
body.saving .live-status { display: none; }

/* ---- search ---- */

.search { display: flex; gap: .5rem; margin: 1rem 0 1.5rem; }
.search input[type=search] {
  flex: 1; min-width: 0; font-size: 1.1rem; padding: .75rem 1rem; border-radius: 12px;
  box-shadow: var(--shadow);
}
.search select { padding: .5rem; }

/* ---- note lists and results ---- */

.notes { list-style: none; padding: 0; margin: 0; }
.notes li {
  display: flex; justify-content: space-between; align-items: baseline; gap: .25rem 1rem; flex-wrap: wrap;
  padding: .8rem .25rem; border-bottom: 1px solid var(--line);
}
.notes li:last-child { border-bottom: 0; }
.notes li > a { font-weight: 550; color: var(--fg); text-decoration: none; overflow-wrap: anywhere; }
.notes li > a:hover { color: var(--accent); text-decoration: underline; }
.results li { display: block; }
.results .row { align-items: baseline; }
.note-title { font-weight: 600; font-size: 1.05rem; color: var(--fg); text-decoration: none; overflow-wrap: anywhere; }
a.note-title:hover { color: var(--accent); text-decoration: underline; }
.snippet { margin: .25rem 0 0; color: var(--muted); font-size: .92rem; overflow-wrap: anywhere; }
mark { background: var(--mark); color: var(--fg); border-radius: 3px; padding: 0 .15em; }
main h2:not(.prose h2):not(.issue-group h2) { font-size: .75rem; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin: 1.5rem 0 .25rem; }
.sort { margin: 0; }
.sort b { color: var(--fg); }

.tags { margin: .4rem 0 0; display: flex; flex-wrap: wrap; gap: .3rem; font-size: .8rem; }
a.tag {
  text-decoration: none; background: var(--accent-soft); color: var(--accent);
  border-radius: 999px; padding: .08rem .6rem; line-height: 1.5;
}
a.tag:hover { text-decoration: underline; }
.tag-cloud { list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: .6rem .9rem; }
.tag-cloud li { display: inline-flex; align-items: center; gap: .35rem; }
.tag-cloud a.tag { font-size: .95rem; padding: .2rem .8rem; }

/* ---- a note ---- */

.note-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin: 1.25rem 0 .25rem; }
.note-head h1 { margin: 0; font-family: var(--serif); font-size: 2.1rem; line-height: 1.15; font-weight: 700; overflow-wrap: anywhere; }
.note-head .btn { flex: none; margin-top: .25rem; }
.note-meta { margin: .5rem 0 1.5rem; display: flex; flex-wrap: wrap; align-items: center; gap: .4rem .75rem; }
.note-meta .tags { margin: 0; }

.prose, .ProseMirror { font-family: var(--serif); font-size: 1.125rem; line-height: 1.7; overflow-wrap: break-word; }
.prose > :first-child { margin-top: 0; }
:is(.prose, .ProseMirror) :is(h1, h2, h3, h4, h5, h6) { font-family: var(--sans); line-height: 1.25; letter-spacing: -.01em; margin: 2em 0 .5em; }
:is(.prose, .ProseMirror) h1 { font-size: 1.6rem; }
:is(.prose, .ProseMirror) h2 { font-size: 1.3rem; text-transform: none; letter-spacing: -.01em; color: var(--fg); }
:is(.prose, .ProseMirror) h3 { font-size: 1.12rem; }
:is(.prose, .ProseMirror) :is(h4, h5, h6) { font-size: 1rem; }
:is(.prose, .ProseMirror) > :first-child { margin-top: 0; }
:is(.prose, .ProseMirror) p { margin: 0 0 1em; }
:is(.prose, .ProseMirror) :is(ul, ol) { padding-left: 1.5rem; margin: 0 0 1em; }
:is(.prose, .ProseMirror) li { margin: .2em 0; }
:is(.prose, .ProseMirror) li > :is(ul, ol) { margin: .2em 0 0; }
:is(.prose, .ProseMirror) li > p { margin: 0; }
:is(.prose, .ProseMirror) :is(td, th) > p { margin: 0; }
:is(.prose, .ProseMirror) li::marker { color: var(--muted); }
:is(.prose, .ProseMirror) ul:has(> li > input[type=checkbox]) { list-style: none; padding-left: .25rem; }
:is(.prose, .ProseMirror) li > input[type=checkbox] { margin: 0 .5rem 0 0; vertical-align: -.1em; width: 1rem; height: 1rem; }
:is(.prose, .ProseMirror) blockquote {
  margin: 1.25em 0; padding: .1em 1.1rem; border-left: 3px solid var(--accent); color: var(--muted); font-style: italic;
}
:is(.prose, .ProseMirror) blockquote p:last-child { margin-bottom: 0; }
:is(.prose, .ProseMirror) :is(code) { background: var(--code); border-radius: 4px; padding: .12em .35em; font-size: .88em; }
:is(.prose, .ProseMirror) pre {
  background: var(--code); border: 1px solid var(--line); border-radius: 10px;
  padding: .9rem 1.1rem; overflow-x: auto; margin: 1.25em 0; font-size: .88rem; line-height: 1.55;
}
:is(.prose, .ProseMirror) pre code { background: none; padding: 0; font-size: inherit; }
:is(.prose, .ProseMirror) a { text-decoration-line: underline; }
.prose a.tag { text-decoration: none; }
.prose a.tag:hover { text-decoration: underline; }
:is(.prose, .ProseMirror) :is(img, video) { max-width: 100%; height: auto; border-radius: 6px; }
:is(.prose, .ProseMirror) hr { margin: 2.5rem 0; }
:is(.prose, .ProseMirror) :is(table) {
  display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; margin: 1.25em 0;
  font-family: var(--sans); font-size: .95rem; line-height: 1.45;
}
:is(.prose, .ProseMirror) :is(th, td) { border: 1px solid var(--line-strong); padding: .45rem .8rem; text-align: left; }
:is(.prose, .ProseMirror) th { background: var(--code); font-weight: 600; }
:is(.prose, .ProseMirror) tbody tr:nth-child(even) td { background: color-mix(in srgb, var(--code) 45%, transparent); }

.backlinks { margin-top: 3rem; padding-top: .5rem; border-top: 1px solid var(--line); }
.backlinks ul { list-style: none; padding: 0; margin: .25rem 0 0; display: flex; flex-wrap: wrap; gap: .25rem .5rem; }
.backlinks li a { display: inline-block; padding: .2rem .7rem; border: 1px solid var(--control); border-radius: 999px; text-decoration: none; font-size: .92rem; }
.backlinks li a:hover { border-color: var(--accent); background: var(--accent-soft); }

.manage { margin-top: 2.5rem; color: var(--muted); font-size: .92rem; }
.manage summary { cursor: pointer; padding: .4rem 0; }
.inline { display: inline-flex; gap: .5rem; margin: .5rem 1rem .5rem 0; flex-wrap: wrap; }

/* ---- editing ---- */

.edit { margin-top: .5rem; }
input.title { width: 100%; font-family: var(--serif); font-size: 1.75rem; font-weight: 700; margin: 1rem 0; padding: .5rem .75rem; }
.edit h1 { font-family: var(--serif); font-size: 1.75rem; margin: .75rem 0; overflow-wrap: anywhere; }
.edit textarea {
  width: 100%; min-height: 60vh; font-family: var(--mono); font-size: .92rem; line-height: 1.6;
  padding: 1rem; resize: vertical;
}
.actions {
  display: flex; flex-wrap: wrap; gap: .5rem; margin-top: 1rem; padding: .75rem 0;
}
.edit .actions { position: sticky; bottom: 0; background: linear-gradient(to top, var(--bg) 75%, transparent); z-index: 2; }
.conflict textarea { min-height: 35vh; }

.toolbar {
  display: flex; align-items: center; gap: .2rem; margin: .5rem 0; padding: .3rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  position: sticky; top: 0; z-index: 3; overflow-x: auto; scrollbar-width: thin;
}
.toolbar button {
  flex: none; min-width: 2.25rem; min-height: 2.25rem; padding: .2rem .5rem;
  border-color: transparent; background: transparent; color: var(--fg); font-size: .92rem;
}
.toolbar button:hover { background: var(--accent-soft); border-color: transparent; }
.toolbar .sep { flex: none; width: 1px; align-self: stretch; margin: .3rem .25rem; background: var(--line); }
.toolbar[hidden] { display: none !important; }

#editor { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 1.25rem 1.5rem; min-height: 60vh; }
#editor:focus-within { border-color: var(--accent); }
#editor .ProseMirror { outline: none; min-height: 56vh; }
#editor .selectedCell:after { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.ProseMirror-gapcursor:after { border-top-color: var(--fg) !important; }
.ProseMirror-selectednode { outline-color: var(--accent); }

/* ---- login, conflict, audit ---- */

.login { display: grid; gap: 1rem; max-width: 22rem; margin: 1.5rem auto 0; padding: 1.5rem; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.login label { display: grid; gap: .3rem; font-size: .9rem; color: var(--muted); }
.login input { color: var(--fg); font-size: 1rem; }
.login .actions { padding: 0; margin: .25rem 0 0; }
.login .actions button { width: 100%; min-height: 2.6rem; }
.login-title { text-align: center; margin: 4rem 0 0; }

.filters { display: flex; flex-wrap: wrap; gap: .4rem; font-size: .88rem; margin: 0 0 1rem; }
.filters a { text-decoration: none; color: var(--muted); border: 1px solid var(--control); border-radius: 999px; padding: .15rem .75rem; }
.filters a:hover { border-color: var(--accent); color: var(--fg); }
.filters .on { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.issue-group { margin: 0 0 1rem; padding: .75rem 1rem; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; }
.issue-group h2 a { text-decoration: none; }
.issue-group h2 a:hover { text-decoration: underline; }
.issue-group h2 { font-size: 1.05rem; margin: 0 0 .35rem; letter-spacing: 0; text-transform: none; color: var(--fg); }
.issue-group ul { margin: 0; padding-left: 1.1rem; }
.issue-group li { margin: .2rem 0; }
.issue-group code { font-size: .78rem; background: var(--code); border-radius: 999px; padding: .1rem .55rem; color: var(--muted); }

/* ---- small screens and touch ---- */

@media (max-width: 40rem) {
  .bar { padding: .75rem 1rem .25rem; }
  main { padding: 0 1rem 4rem; }
  .bar nav { width: 100%; margin: 0 -.35rem; }
  .bar nav > a:not(.btn) { padding: .55rem .6rem; }
  h1 { font-size: 1.5rem; }
  .note-head h1 { font-size: 1.75rem; }
  .prose, .ProseMirror { font-size: 1.06rem; }
  #editor { padding: .75rem 1rem; }
  .edit textarea { min-height: 55vh; padding: .75rem; }
  .search { flex-wrap: wrap; }
  .search select { flex: 1; }
  .notes li, .results .row { display: block; }
  .notes li { padding: .9rem .1rem; }
  .notes li > time, .results .row > time { display: block; margin-top: .1rem; }
  .search select { flex: 0 0 auto; }
  .login-title { margin-top: 2rem; }
}

@media (pointer: coarse) {
  .btn, button, select, input:not([type=checkbox]) { min-height: 2.75rem; }
  .toolbar button { min-width: 2.75rem; min-height: 2.75rem; }
  .bar nav > a:not(.btn) { padding-block: .7rem; }
}

@media (prefers-reduced-motion: no-preference) {
  .btn, button, a, input, textarea { transition: background-color .12s, border-color .12s, color .12s; }
}

@media print {
  .bar, .backlinks, .manage, .actions, .toolbar, .search { display: none !important; }
  body { background: #fff; color: #000; }
}

/* Keyboard shortcut help */
dialog.shortcuts { background: var(--surface); color: var(--fg); border: 1px solid var(--line-strong); border-radius: var(--radius); padding: 1rem 1.5rem; max-width: min(28rem, 92vw); }
dialog.shortcuts::backdrop { background: rgb(0 0 0 / .4); }
dialog.shortcuts h2 { margin: 0 0 .75rem; font-size: 1.1rem; }
dialog.shortcuts dl { display: grid; grid-template-columns: auto 1fr; gap: .4rem 1rem; margin: 0; }
dialog.shortcuts dt, dialog.shortcuts dd { margin: 0; }
dialog.shortcuts kbd { background: var(--bg); border: 1px solid var(--control); border-radius: 4px; padding: .05rem .4rem; font-size: .85em; }

.upload-status { margin: .5rem 0; color: var(--muted); font-size: .9rem; }
.upload-status.error { color: var(--danger); background: var(--danger-soft); border-radius: var(--radius); padding: .5rem .75rem; }
