/* ---------- Design tokens ----------
   "Ledger": paper-toned neutral, a single ink-green accent, hairline rules
   instead of shadows/gradients, a serif for headings, tabular monospace for
   figures. Almost everything is token-driven, so a reskin happens here. */
:root {
  color-scheme: light dark;

  --font-sans: -apple-system, "Segoe UI", system-ui, Roboto, Helvetica, Arial, sans-serif;
  --font-display: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --font-mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, monospace;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-8: 3rem;

  --radius-sm: 3px;
  --radius-md: 4px;
  --radius-lg: 6px;

  --bg: #f6f4ee;
  --bg-elevated: #ffffff;
  --bg-subtle: #efece2;
  --bg-hover: #ece9dd;

  --border: #ddd7c8;
  --border-strong: #c9c2ae;

  --text: #1f241f;
  --text-muted: #5c6158;
  --text-faint: #8b8f81;

  --accent: #2f5233;
  --accent-hover: #24401f;
  --accent-soft: #e3e8de;
  --accent-contrast: #f6f4ee;

  --success: #2f5233;
  --success-bg: #e3e8de;
  --warning: #8f6a17;
  --warning-bg: #efe6cf;
  --danger: #9c3327;
  --danger-bg: #f4e4e1;
  --danger-muted: #a4685f;
  --danger-muted-bg: #f4e4e1;
  --danger-faint: #a4756c;
  --info-bg: #efe6cf;
  --info: #6a5312;

  --tile-blue: #2f5233;
  --tile-blue-bg: #e3e8de;
  --tile-purple: #6a5312;
  --tile-purple-bg: #efe6cf;
  --tile-pink: #9c3327;
  --tile-pink-bg: #f4e4e1;

  --header-bg: #ffffff;
  --header-border: #c9c2ae;
  --header-text: #1f241f;
  --header-text-muted: #5c6158;
  --header-hover: #efece2;
  --header-accent: #2f5233;
  --heading-weight: 400;

  --shadow-sm: none;
  --shadow-md: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14160f;
    --bg-elevated: #1b1e15;
    --bg-subtle: #1f2318;
    --bg-hover: #232819;

    --border: #33362a;
    --border-strong: #43483a;

    --text: #ece7da;
    --text-muted: #a9ad9e;
    --text-faint: #7c8073;

    --accent: #86b586;
    --accent-hover: #9bc49b;
    --accent-soft: #24301f;
    --accent-contrast: #10130c;

    --success: #86b586;
    --success-bg: #24301f;
    --warning: #d1a34a;
    --warning-bg: #332a12;
    --danger: #e0958a;
    --danger-bg: #3a2420;
    --danger-muted: #cf9a92;
    --danger-muted-bg: #3a2420;
    --danger-faint: #b58f88;
    --info-bg: #332a12;
    --info: #d1a34a;

    --tile-blue: #86b586;
    --tile-blue-bg: #24301f;
    --tile-purple: #d1a34a;
    --tile-purple-bg: #332a12;
    --tile-pink: #e0958a;
    --tile-pink-bg: #3a2420;

    --header-bg: #1b1e15;
    --header-border: #43483a;
    --header-text: #ece7da;
    --header-text-muted: #a9ad9e;
    --header-hover: #232819;
    --header-accent: #86b586;

    --shadow-sm: none;
    --shadow-md: none;
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { font-family: var(--font-sans); margin: 0; color: var(--text); background: var(--bg); line-height: 1.5; font-size: 15px; }
h1, h2, h3 { font-family: var(--font-display); font-weight: var(--heading-weight); line-height: 1.25; }
h1 { font-size: 1.6rem; margin: 0 0 var(--space-2); }
h2 { font-size: 1.15rem; margin: var(--space-6) 0 var(--space-3); }
h2:first-child { margin-top: 0; }
a { color: var(--accent); }
a:hover { color: var(--accent-hover); }
.muted { color: var(--text-muted); }
.small { font-size: 0.82rem; }
.error-text { color: var(--danger); }
p { margin: 0 0 var(--space-3); }

/* ---------- Header ---------- */
.site-header { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); flex-wrap: wrap;
  padding: var(--space-3) var(--space-5); background: var(--header-bg); border-bottom: 1px solid var(--header-border); }
.brand { font-family: var(--font-display); font-size: 1.15rem; color: var(--header-text); text-decoration: none; }
.header-nav { display: flex; align-items: center; gap: var(--space-4); font-size: 0.9rem; }
.header-nav a { color: var(--header-text-muted); text-decoration: none; }
.header-nav a:hover { color: var(--header-text); }
form.inline { display: inline; margin: 0; }
.link-button { background: none; border: 0; padding: 0; color: var(--header-text-muted); font: inherit; cursor: pointer; }
.link-button:hover { color: var(--header-text); background: none; }

/* ---------- Layout ---------- */
.container { max-width: 1000px; margin: 0 auto; padding: var(--space-6) var(--space-5) var(--space-8); }
.container-wide { max-width: 1600px; }
.narrow { max-width: 420px; margin: var(--space-6) auto; }
.narrow-form { max-width: 560px; }
.page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-4); flex-wrap: wrap; margin-bottom: var(--space-5); }
.page-header .actions { display: flex; gap: var(--space-2); align-items: center; }
.page-header p { margin: 0; }
.crumbs { margin: 0 0 var(--space-1); font-size: 0.88rem; }
.crumbs a { text-decoration: none; }
.card { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: var(--space-5); }
.card + .card { margin-top: var(--space-5); }
.empty { color: var(--text-muted); }

/* ---------- Forms ---------- */
label { display: block; font-weight: 500; font-size: 0.9rem; margin: 0 0 0.3rem; }
input, select, textarea { font: inherit; color: var(--text); background: var(--bg-elevated); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); padding: 0.45rem 0.6rem; width: 100%; }
input[type=file] { padding: 0.35rem; }
textarea { display: block; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
form.stacked > label { margin-top: var(--space-4); }
form.stacked > label:first-of-type { margin-top: 0; }
form.stacked > button { margin-top: var(--space-5); }
button, .button { display: inline-flex; align-items: center; gap: 0.35rem; font: inherit; font-size: 0.9rem; font-weight: 500;
  padding: 0.5rem 0.95rem; border-radius: var(--radius-sm); border: 1px solid var(--border-strong); background: var(--bg-elevated);
  color: var(--text); text-decoration: none; cursor: pointer; white-space: nowrap; }
button:hover, .button:hover { background: var(--bg-hover); color: var(--text); }
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
button.primary:hover, .button.primary:hover { background: var(--accent-hover); color: var(--accent-contrast); }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
button.danger:hover { background: var(--danger-bg); }
button.small, .button.small { padding: 0.25rem 0.6rem; font-size: 0.8rem; }
.danger-zone { margin-top: var(--space-6); }

/* ---------- Flash ---------- */
.flash { padding: var(--space-3) var(--space-4); border-radius: var(--radius-md); margin-bottom: var(--space-4); border: 1px solid var(--border); background: var(--bg-elevated); }
.flash-success { background: var(--success-bg); color: var(--success); border-color: transparent; }
.flash-error { background: var(--danger-bg); color: var(--danger); border-color: transparent; }
.flash-warning { background: var(--warning-bg); color: var(--warning); border-color: transparent; }
.flash a { color: inherit; }

/* ---------- Bibliography list ---------- */
.bib-list { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--space-3); }
.bib-list li { display: flex; justify-content: space-between; align-items: center; gap: var(--space-4); flex-wrap: wrap; padding: var(--space-4) var(--space-5); }
.bib-list li + li { margin-top: 0; }
.bib-link { display: flex; flex-direction: column; text-decoration: none; color: var(--text); }
.bib-link strong { font-family: var(--font-display); font-weight: 400; font-size: 1.1rem; }

/* ---------- Entries table ---------- */
.add-article { margin-bottom: var(--space-6); }
.add-form { display: grid; grid-template-columns: 1fr 1fr auto; gap: var(--space-4); align-items: end; }
.add-article p { margin: var(--space-3) 0 0; }
@media (max-width: 720px) { .add-form { grid-template-columns: 1fr; } }
table.entries { border-collapse: collapse; width: 100%; font-size: 0.92rem; background: var(--bg-elevated); border: 1px solid var(--border); }
table.entries th, table.entries td { text-align: left; padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--border); vertical-align: top; }
table.entries th { font-weight: 500; color: var(--text-muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; background: var(--bg-subtle); }
table.entries .num { text-align: right; font-variant-numeric: tabular-nums; }
.entry-ref { color: var(--text); text-decoration: none; }
.entry-ref:hover { color: var(--accent); }
.badge { display: inline-block; padding: 0.1rem 0.5rem; border-radius: 999px; font-size: 0.75rem; font-weight: 500; }
.badge-draft { background: var(--bg-subtle); color: var(--text-muted); }
.badge-complete { background: var(--success-bg); color: var(--success); }
@media (max-width: 640px) {
  table.entries thead { display: none; }
  table.entries tr { display: grid; grid-template-columns: auto 1fr; gap: 0 var(--space-3); padding: var(--space-2) 0; border-bottom: 1px solid var(--border); }
  table.entries td { border: 0; padding: 0.2rem 0.75rem; }
  table.entries td:nth-child(2) { grid-column: 2; }
  table.entries td:nth-child(n+3) { grid-column: 2; }
  table.entries .num { text-align: left; }
}

/* ---------- Entry editor ---------- */
.edit-layout { display: grid; gap: var(--space-6); }
.edit-layout.has-article { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
@media (max-width: 1100px) { .edit-layout.has-article { grid-template-columns: 1fr; } }
.section { margin-bottom: var(--space-4); }
.field-label { font-family: var(--font-display); font-weight: 600; font-size: 1rem; margin-bottom: 0; }
.entry-form .rte { margin: 0.35rem 0 0; }
.entry-form .rte-body { min-height: 5.5rem; max-height: none; font-size: 0.95rem; }
.section-reference .rte-body { min-height: 3.5rem; font-family: var(--font-display); }
.section-reference .rte-body p { padding-left: 2rem; text-indent: -2rem; }
.sort-keys { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); margin-top: var(--space-2); }
.sort-keys label { margin: 0; font-weight: 400; color: var(--text-muted); }
.small-input { width: 16rem; padding: 0.2rem 0.4rem; font-size: 0.85rem; }
.tiny-input { width: 5.5rem; padding: 0.2rem 0.4rem; font-size: 0.85rem; }
.form-footer { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3) 0; position: sticky; bottom: 0;
  background: var(--bg); border-top: 1px solid var(--border); z-index: 2; }
.form-footer select { width: auto; }
.inline-label { margin: 0; }
.save-state { font-size: 0.88rem; }
.entry-tools { display: grid; gap: var(--space-3); margin-top: var(--space-6); padding-top: var(--space-5); border-top: 1px solid var(--border); }
.inline-form { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); margin: 0; }
.inline-form label { margin: 0; min-width: 14rem; font-weight: 400; color: var(--text-muted); }
.inline-form input { width: auto; flex: 1 1 14rem; }
.article-panel { position: sticky; top: var(--space-4); align-self: start; display: flex; flex-direction: column; gap: var(--space-2);
  height: calc(100vh - 2 * var(--space-4)); }
.article-head { display: flex; justify-content: space-between; gap: var(--space-3); font-size: 0.9rem; }
.article-frame { flex: 1 1 auto; width: 100%; min-height: 24rem; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: #fff; }
.article-text summary { cursor: pointer; font-size: 0.9rem; color: var(--text-muted); }
.article-text pre { max-height: 40vh; overflow: auto; white-space: pre-wrap; font-size: 0.85rem; background: var(--bg-elevated);
  border: 1px solid var(--border); padding: var(--space-3); border-radius: var(--radius-sm); }
@media (max-width: 1100px) { .article-panel { position: static; height: 80vh; } }

/* Rendered rich text (sanitize_rich_text() output: p/ul/ol/li/strong/em/a/br) */
.rich-text > :first-child { margin-top: 0; }
.rich-text > :last-child { margin-bottom: 0; }
.rich-text p { margin: 0 0 var(--space-2); }
.rich-text ul, .rich-text ol { margin: 0 0 var(--space-2); padding-left: 1.5rem; }
.rich-text li > ul, .rich-text li > ol { margin: 0; }
.rich-text a { color: var(--accent); }
.rich-text h2, .rich-text h3 { font-family: var(--font-display); font-weight: 600; color: var(--text); margin: var(--space-4) 0 var(--space-2); line-height: 1.25; }
.rich-text h2 { font-size: 1.2rem; }
.rich-text h3 { font-size: 1.02rem; }
.rich-text blockquote { margin: 0 0 var(--space-2); padding: 0.1rem 0 0.1rem var(--space-3); border-left: 3px solid var(--border-strong); color: var(--text-muted); }
.rich-text code { font-family: var(--font-mono); font-size: 0.85em; background: var(--bg-subtle); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 0.05em 0.3em; }
.rich-text pre { font-family: var(--font-mono); font-size: 0.85rem; background: var(--bg-subtle); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: var(--space-2) var(--space-3); margin: 0 0 var(--space-2); white-space: pre-wrap; overflow-wrap: anywhere; }
.rich-text ul.checklist { list-style: none; padding-left: 0.1rem; }
.rich-text ul.checklist > li { position: relative; padding-left: 1.6rem; }
.rich-text ul.checklist > li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0.18em;
  width: 1rem;
  height: 1rem;
  box-sizing: border-box;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  font-size: 0.75rem;
  line-height: 0.85rem;
  text-align: center;
  color: var(--accent-contrast);
}
.rich-text ul.checklist > li.checked::before { content: '\2713'; background: var(--accent); border-color: var(--accent); }
.rte-body ul.checklist > li::before { cursor: pointer; }

/* Rich-text editor (rich-text-editor.js) */
form.stacked label.field-label { margin-bottom: 0; }
.rte {
  margin: 0.35rem 0 var(--space-4);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
.rte:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.rte-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-subtle);
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}
.rte-toolbar .rte-btn {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
  padding: 0.25rem 0.6rem;
  min-width: 2rem;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 500;
}
.rte-toolbar .rte-btn:hover { background: var(--bg-hover); color: var(--text); }
.rte-toolbar .rte-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--border); }
.rte-toolbar .rte-btn strong, .rte-toolbar .rte-btn em { font-family: var(--font-display); font-size: 0.95rem; }
.rte-toolbar .rte-btn code { font-family: var(--font-mono); font-size: 0.8rem; background: none; border: none; padding: 0; }
.rte-sep { width: 1px; align-self: stretch; margin: 3px 4px; background: var(--border); }
.rte-body {
  min-height: 9rem;
  max-height: 32rem;
  overflow-y: auto;
  padding: 0.5rem 0.65rem;
  font-size: 0.92rem;
  color: var(--text);
  outline: none;
  cursor: text;
}


/* Nine editors side by side with the article: a tighter toolbar. */
.entry-form .rte-toolbar .rte-btn { padding: 0.2rem 0.45rem; min-width: 1.7rem; font-size: 0.8rem; }
.entry-form .rte-sep { margin: 3px 2px; }
