:root {
  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #efede6;
  --text: #24231f;
  --muted: #6f6c63;
  --border: #dcd9cf;
  --accent: #2f6f5e;
  --accent-soft: #dcebe5;
  --danger: #a23b2a;
  --warn-bg: #fbefd2;
  --warn-text: #6a4b06;
  --select: #e4efe9;
  --mode-normal: #2f6f5e;
  --mode-insert: #2d5fa8;
  --mode-visual: #8a4fa0;
  --mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --surface: #1d1f23;
    --surface-2: #25282d;
    --text: #e3e1da;
    --muted: #8d8b84;
    --border: #33363c;
    --accent: #7ec4a8;
    --accent-soft: #23362f;
    --danger: #e98a78;
    --warn-bg: #3a3020;
    --warn-text: #f0d394;
    --select: #243129;
    --mode-normal: #4e9c80;
    --mode-insert: #5a8fd8;
    --mode-visual: #b07cc6;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 var(--sans);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--accent); text-decoration: none; }
a:hover, a:focus-visible { text-decoration: underline; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

h1 { font-size: 1.6rem; line-height: 1.25; margin: 0 0 1rem; }
h2 { font-size: 1.05rem; margin: 2rem 0 .6rem; }

kbd {
  font: 0.78rem var(--mono);
  padding: 0 .35em;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  white-space: nowrap;
}

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden, [hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.inline-form { display: inline; margin: 0; }

/* Top bar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .55rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  font-size: .9rem;
}

.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; min-width: 0; }
.crumbs__sep { color: var(--muted); }
.crumbs__item { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 22ch; }
.brand { font-family: var(--mono); font-weight: 700; color: var(--text); }
.topbar__user { display: flex; align-items: center; gap: .4rem; white-space: nowrap; }

/* Main */

.main {
  flex: 1;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  padding: 2rem 1.25rem 4rem;
}

.page-header { display: flex; align-items: baseline; gap: .75rem; }
.lead { color: var(--muted); margin-top: -.5rem; }

.badge {
  font: .72rem var(--mono);
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: .1em .5em;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  white-space: nowrap;
}

.empty { color: var(--muted); padding: 1.5rem 0; }

/* Lists */

.list { list-style: none; padding: 0; margin: 0; border-top: 1px solid var(--border); }

.item {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: 1rem;
  padding: .65rem .9rem;
  border-bottom: 1px solid var(--border);
  border-left: 3px solid transparent;
}

.item.is-selected { background: var(--select); border-left-color: var(--accent); }
.item.is-selected .item__link::before { content: "›"; position: absolute; left: .25rem; color: var(--accent); font-family: var(--mono); }
.item__link { font-weight: 600; color: var(--text); }
.item__title { flex: 1; min-width: 0; }
.item__title .muted { display: block; font-size: .85rem; overflow-wrap: anywhere; }
.item__meta { margin-left: auto; color: var(--muted); font-size: .82rem; white-space: nowrap; }
.item__desc { flex-basis: 100%; margin: .15rem 0 0; color: var(--muted); font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item--row { gap: .75rem; align-items: center; }

/* Forms */

.form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1rem; max-width: 480px; }
.form--inline { display: flex; flex-wrap: wrap; gap: .5rem; max-width: none; }
.form--inline input[type=email] { flex: 1; min-width: 14rem; }
.field { display: grid; grid-template-columns: minmax(0, 1fr); gap: .3rem; }
.field input, .field select { width: 100%; min-width: 0; }
.field label { font-size: .85rem; color: var(--muted); }
.form__actions { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }

input[type=text], input[type=email], input[type=password], select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .45rem .6rem;
}

input:focus, select:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }

input[type=submit], button {
  font: inherit;
  font-weight: 600;
  color: var(--bg);
  background: var(--accent);
  border: 0;
  border-radius: 6px;
  padding: .45rem 1rem;
  cursor: pointer;
}

.button--ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); font-weight: 400; padding: .3rem .7rem; }
.button--ghost:hover, .button--ghost:focus-visible { color: var(--danger); border-color: var(--danger); }

.errors { color: var(--danger); margin: 0; padding-left: 1.2rem; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 1.25rem 1.5rem; margin-bottom: 1.5rem; }
.panel h2 { margin-top: 0; }
.panel--narrow { max-width: 440px; margin: 3rem auto; }
.panel--narrow .form { max-width: none; }

.invite-link { font: .78rem var(--mono); flex: 1; min-width: 12rem; color: var(--muted); }

/* Memo view */

.memo__header { margin-bottom: 1.5rem; }
.memo__header h1 { margin-bottom: .2rem; }
.memo__header p { margin: 0; font-size: .85rem; }

.prose { font-size: 1rem; line-height: 1.7; overflow-wrap: break-word; }
.prose h1, .prose h2, .prose h3 { margin: 1.6em 0 .5em; line-height: 1.3; }
.prose h1 { font-size: 1.45rem; }
.prose h2 { font-size: 1.2rem; }
.prose h3 { font-size: 1.05rem; }
.prose code { font: .88em var(--mono); background: var(--surface-2); padding: .1em .3em; border-radius: 4px; }
.prose pre { background: var(--surface-2); padding: .9rem 1rem; border-radius: 8px; overflow-x: auto; }
.prose pre code { background: none; padding: 0; }
.prose blockquote { margin: 1rem 0; padding: .1rem 1rem; border-left: 3px solid var(--accent); color: var(--muted); }
.prose table { border-collapse: collapse; margin: 1rem 0; }
.prose th, .prose td { border: 1px solid var(--border); padding: .35rem .7rem; }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 2rem 0; }
.prose li.task { list-style: none; margin-left: -1.3rem; }
.prose li.task input { margin-right: .5rem; }
.prose mark { background: var(--warn-bg); color: var(--warn-text); }
.prose img { max-width: 100%; }

.wikilink { border-bottom: 1px dashed var(--accent); }
.wikilink--missing { color: var(--muted); border-bottom-color: var(--muted); }

.backlinks { margin-top: 3rem; padding-top: 1rem; border-top: 1px solid var(--border); font-size: .9rem; }
.backlinks h2 { margin-top: 0; font-size: .85rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }

/* Editor */

.editor__header { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; }
.editor__state { font: .8rem var(--mono); color: var(--muted); white-space: nowrap; }
.editor__state.is-dirty { color: var(--warn-text); background: var(--warn-bg); padding: 0 .4rem; border-radius: 4px; }
.editor__notice { background: var(--warn-bg); color: var(--warn-text); padding: .6rem .9rem; border-radius: 8px; margin-bottom: 1rem; font-size: .9rem; }
.editor__fallback { width: 100%; min-height: 60vh; font: .95rem var(--mono); }

.editor__mount .cm-editor {
  min-height: 65vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font: .95rem/1.65 var(--mono);
  color: var(--text);
}
.editor__mount .cm-editor.cm-focused { outline: none; border-color: var(--accent); }
.editor__mount .cm-scroller { font-family: var(--mono); }
.editor__mount .cm-content { padding: 1rem 0; caret-color: var(--accent); }
.editor__mount .cm-gutters { background: var(--surface); color: var(--muted); border-right: 1px solid var(--border); border-radius: 8px 0 0 8px; }
.editor__mount .cm-activeLine { background: var(--select); }
.editor__mount .cm-activeLineGutter { background: var(--select); }
.editor__mount .cm-fat-cursor { background: var(--accent) !important; color: var(--bg) !important; }
.editor__mount .cm-selectionBackground, .editor__mount .cm-focused .cm-selectionBackground { background: var(--accent-soft) !important; }
.editor__mount .cm-panels { background: var(--surface-2); color: var(--text); border-top: 1px solid var(--border); font-family: var(--mono); }
.editor__mount .cm-panels input { color: var(--text); background: transparent; }

.md-h1 { font-weight: 700; font-size: 1.3em; }
.md-h2 { font-weight: 700; font-size: 1.15em; }
.md-h3 { font-weight: 700; }
.md-strong { font-weight: 700; }
.md-em { font-style: italic; }
.md-strike { text-decoration: line-through; }
.md-link { color: var(--accent); }
.md-code { color: var(--mode-visual); }
.md-quote { color: var(--muted); font-style: italic; }
.md-mark, .md-list { color: var(--muted); }

/* Status line */

.statusline {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .25rem 1.25rem .25rem 0;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  font: .8rem var(--mono);
}

.statusline__mode {
  align-self: stretch;
  display: flex;
  align-items: center;
  padding: 0 .9rem;
  margin: -.25rem 0;
  background: var(--mode-normal);
  color: #fff;
  font-weight: 700;
}
.statusline__mode[data-mode=insert] { background: var(--mode-insert); }
.statusline__mode[data-mode^=v] { background: var(--mode-visual); }
.statusline__message { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.statusline__hints { color: var(--muted); white-space: nowrap; overflow: hidden; }
.statusline__hints kbd { margin-left: .4rem; }
.flash { color: var(--accent); }
.flash--alert { color: var(--danger); }

/* Overlays */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 12vh 1rem 1rem;
  background: rgb(0 0 0 / .35);
}

.palette, .help {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 20px 50px rgb(0 0 0 / .3);
}

.palette { max-width: 620px; overflow: hidden; }
.palette__input { width: 100%; border: 0 !important; border-bottom: 1px solid var(--border) !important; border-radius: 0 !important; padding: .9rem 1.1rem !important; font-size: 1.05rem !important; box-shadow: none !important; }
.palette__list { list-style: none; margin: 0; padding: .3rem; max-height: 50vh; overflow-y: auto; }
.palette__item { display: flex; align-items: baseline; gap: .75rem; padding: .45rem .75rem; border-radius: 6px; cursor: pointer; }
.palette__item.is-selected { background: var(--select); }
.palette__kind { font: .7rem var(--mono); text-transform: uppercase; color: var(--muted); width: 5.5rem; flex-shrink: 0; }
.palette__label { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette__context { margin-left: auto; color: var(--muted); font-size: .82rem; white-space: nowrap; }
.palette__empty { padding: .6rem .75rem; color: var(--muted); }
.palette__hint { border-top: 1px solid var(--border); padding: .45rem 1rem; font-size: .78rem; color: var(--muted); }

.help { max-width: 860px; padding: 1.5rem 1.75rem; max-height: 80vh; overflow-y: auto; }
.help h2 { margin: 0 0 1rem; font-size: 1.2rem; }
.help h3 { margin: 0 0 .5rem; font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.help__columns { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 1.5rem; }
.help dl { display: grid; grid-template-columns: auto 1fr; gap: .35rem .75rem; margin: 0; font-size: .88rem; }
.help dt { white-space: nowrap; }
.help dd { margin: 0; color: var(--muted); }

@media (max-width: 640px) {
  .topbar__user .muted, .topbar__user kbd { display: none; }
  .statusline__hints { display: none; }
  .item__meta { margin-left: 0; flex-basis: 100%; }
}
