:root {
  --bg: #ffffff;
  --side: #f7f7f6;
  --ink: #1b1b1b;
  --muted: #6a6a6a;
  --line: #e2e2e0;
  --hover: #ececea;
  --current: #e3e9f3;
  --link: #1d4f91;
  --code: #f4f4f2;
  --chart: #1c2b72;
  --danger: #a12a2a;
  --kw: #7a3e9d;
  --str: #2f6f44;
  --num: #9a5b13;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151515;
    --side: #1b1b1b;
    --ink: #e7e7e5;
    --muted: #9b9b98;
    --line: #2f2f2e;
    --hover: #272727;
    --current: #25324a;
    --link: #8fb5f2;
    --code: #1f1f1f;
    --chart: #b3c3f7;
    --danger: #f08a8a;
    --kw: #c9a0e6;
    --str: #8fca9f;
    --num: #e0b071;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.6 var(--sans);
}

a { color: var(--link); }
h1, h2, h3, p, ul, ol, dl, dd, pre { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }

button {
  cursor: pointer;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 4px 10px;
}
button:hover { background: var(--hover); }

input, select, textarea {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 5px 8px;
}

:focus-visible { outline: 2px solid var(--link); outline-offset: 1px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- sidebar ---------- */

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--side);
  border-right: 1px solid var(--line);
  font-size: 14px;
}

.side-head { padding: 14px 12px 10px; display: grid; gap: 10px; }
.brand { font-weight: 600; font-size: 16px; color: var(--ink); text-decoration: none; }
#filter { width: 100%; }

.tree-wrap { flex: 1; min-height: 0; overflow-y: auto; padding: 0 6px 8px; display: flex; flex-direction: column; }
.tree { flex: 1; }
.tree, .tree ul { list-style: none; padding: 0; }
.tree ul { padding-left: 14px; }

.row {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 28px;
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--ink);
  text-decoration: none;
  cursor: pointer;
}
.row:hover { background: var(--hover); }
.term-row { padding-left: 22px; }
.term-row.current { background: var(--current); font-weight: 600; }
.term-row .where { margin-left: auto; color: var(--muted); font-size: 12px; font-weight: 400; }

.folder-row { font-weight: 600; user-select: none; }
.twist { width: 10px; flex: none; color: var(--muted); font-size: 10px; }
.twist::before { content: "\25B8"; display: inline-block; }
.folder.open > .folder-row .twist::before { transform: rotate(90deg); }
.folder-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { color: var(--muted); font-weight: 400; font-size: 12px; }

.tools { margin-left: auto; display: flex; gap: 2px; visibility: hidden; }
.folder-row:hover .tools, .folder-row:focus-within .tools { visibility: visible; }
.tools button { padding: 0 6px; line-height: 20px; border-color: transparent; background: transparent; color: var(--muted); }
.tools button:hover { color: var(--ink); background: var(--bg); border-color: var(--line); }

.drop { outline: 2px dashed var(--link); outline-offset: -2px; border-radius: 4px; }
.tree-note { padding: 4px 6px 4px 22px; color: var(--muted); font-size: 13px; }
.add-row { padding: 2px 0 2px 16px; }
.inline-input { width: 100%; min-width: 0; padding: 2px 6px; font-weight: 400; }

.side-foot { padding: 10px 12px 12px; border-top: 1px solid var(--line); display: grid; gap: 8px; }
.add-bar { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.chart-link {
  display: block;
  text-align: center;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--bg);
  color: var(--ink);
  text-decoration: none;
}
.chart-link:hover { background: var(--hover); }
.chart-link.current { background: var(--current); }
.mode { color: var(--muted); font-size: 12px; line-height: 1.4; }
.byline { color: var(--muted); font-size: 12px; line-height: 1.4; }
.byline a { color: inherit; }

/* ---------- main ---------- */

main { min-width: 0; overflow-y: auto; padding: 36px 44px 64px; }
main:focus { outline: none; }
main > * { max-width: 720px; }

h1 { font-size: 30px; line-height: 1.2; font-weight: 650; }
h2 { font-size: 13px; font-weight: 600; color: var(--muted); }
h3 { font-size: 16px; margin-top: 20px; }

.crumb { color: var(--muted); font-size: 13px; margin-bottom: 4px; }
.entry-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.entry-head h1 { margin-right: auto; }
.aka { color: var(--muted); font-size: 14px; margin-top: 4px; }

.body { margin-top: 16px; }
.body > * + * { margin-top: 14px; }
.body ul, .body ol { padding-left: 22px; }
.lead { font-size: 18px; line-height: 1.5; }

code { font-family: var(--mono); font-size: 0.9em; background: var(--code); padding: 1px 4px; border-radius: 3px; }
pre {
  background: var(--code);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 12px 14px;
  overflow-x: auto;
  font: 13.5px/1.55 var(--mono);
}
pre code { background: none; padding: 0; font-size: inherit; }
.k { color: var(--kw); }
.s { color: var(--str); }
.n { color: var(--num); }
.c { color: var(--muted); }
.f { color: var(--link); }

.rels { margin-top: 24px; border-top: 1px solid var(--line); display: grid; grid-template-columns: max-content 1fr; }
.rels dt, .rels dd { padding: 8px 0; border-bottom: 1px solid var(--line); }
.rels dt { padding-right: 24px; color: var(--muted); font-size: 14px; }

.term-link { text-decoration-thickness: 1px; text-underline-offset: 2px; }
.unwritten { color: var(--muted); border-bottom: 1px dashed var(--muted); }
button.unwritten { background: none; border: 0; border-bottom: 1px dashed var(--muted); border-radius: 0; padding: 0; }
button.unwritten:hover { color: var(--ink); background: none; }

.meta { color: var(--muted); font-size: 13px; margin-top: 16px; }

.home .lead { margin-top: 8px; }
.home h2 { margin-top: 28px; }
.index { list-style: none; padding: 0; margin-top: 8px; border-top: 1px solid var(--line); }
.index li { padding: 8px 0; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 180px 1fr; gap: 16px; }
.index span { color: var(--muted); font-size: 14px; }

/* ---------- editor ---------- */

.editor { display: grid; gap: 14px; }
.editor label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.editor input, .editor select, .editor textarea { font-size: 15px; color: var(--ink); width: 100%; }
.editor textarea { font: 14px/1.55 var(--mono); resize: vertical; min-height: 300px; tab-size: 4; }
.two { display: grid; grid-template-columns: 2fr 1fr; gap: 12px; }
.hint { color: var(--muted); font-size: 13px; margin-top: -8px; }
.actions { display: flex; gap: 8px; align-items: center; }
.actions .right { margin-left: auto; }
.primary { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.primary:hover { background: var(--ink); opacity: 0.85; }
.danger { color: var(--danger); }

/* ---------- word chart ---------- */

.seg { display: flex; }
.seg button { border-radius: 0; }
.seg button:first-child { border-radius: 4px 0 0 4px; }
.seg button:last-child { border-radius: 0 4px 4px 0; border-left: 0; }
.seg button[aria-pressed="true"] { background: var(--current); }

.chart { max-width: 960px; }
.wc { margin-top: 20px; }
.wc svg { display: block; margin: 0 auto; height: auto; max-height: 70vh; overflow: visible; }
.wc text { font-weight: 600; fill: var(--chart); }
.wc text.muted { fill: var(--muted); }
.wc a:hover text, .wc a:focus text { text-decoration: underline; }
.wc [data-missing] { cursor: default; }
.editing .wc [data-missing] { cursor: pointer; }

/* ---------- notice ---------- */

.notice {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 4px;
  font-size: 14px;
  max-width: 90vw;
  z-index: 10;
}

/* ---------- small screens ---------- */

.topbar { display: none; }

@media (max-width: 720px) {
  body { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); }
  .topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--line);
    background: var(--side);
  }
  .sidebar { display: none; position: fixed; inset: 47px 0 0 0; z-index: 5; border-right: 0; font-size: 16px; }
  .nav-open .sidebar { display: flex; }
  .side-head .brand { display: none; }
  main { padding: 24px 18px 48px; }
  .two { grid-template-columns: 1fr; }
  .index li { grid-template-columns: 1fr; gap: 0; }
  .rels { grid-template-columns: 1fr; }
  .rels dt { border-bottom: 0; padding-bottom: 0; }
  .rels dd { padding-top: 2px; }
}
