:root {
  color-scheme: light dark;
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --surface-2: #ece7dc;
  --text: #24211c;
  --muted: #6a6358;
  --line: #d9d2c3;
  --accent: #7d2a26;
  --accent-text: #fffdf8;
  --accent-soft: #f3e2dc;
  --warn-bg: #fbf0d3;
  --warn-text: #5e4608;
  --ok: #2f6b3f;
  --bad: #a2261f;
  --bad-bg: #fbe4e1;
  --info-bg: #e6edf2;
  --info-text: #274256;
  --ins-bg: #d8efd9;
  --del-bg: #f8d9d6;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --read: Charter, "Bitstream Charter", "Iowan Old Style", Georgia, Cambria, serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191816;
    --surface: #23211e;
    --surface-2: #2e2b27;
    --text: #e9e4da;
    --muted: #a39b8d;
    --line: #3d3933;
    --accent: #c9645c;
    --accent-text: #1b1917;
    --accent-soft: #3c2522;
    --warn-bg: #3b3115;
    --warn-text: #ecd693;
    --ok: #7fc08e;
    --bad: #ec8b84;
    --bad-bg: #40211e;
    --info-bg: #222d35;
    --info-text: #b6cfe0;
    --ins-bg: #1f4026;
    --del-bg: #4d2422;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.5 var(--ui); }
a { color: var(--accent); }
h1 { font-size: 1.6rem; line-height: 1.25; margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; margin: 2rem 0 .6rem; }
h3 { font-size: 1rem; margin: 1.2rem 0 .4rem; }
code { font-family: ui-monospace, Consolas, monospace; font-size: .9em; }

.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: .25rem 1.25rem; padding: .55rem 1rem; background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; color: var(--accent); text-decoration: none; letter-spacing: .01em; }
.nav { display: flex; flex-wrap: wrap; align-items: center; gap: .1rem .9rem; flex: 1; }
.nav a { color: var(--text); text-decoration: none; font-size: .94rem; padding: .15rem 0; }
.nav a:hover { color: var(--accent); text-decoration: underline; }
.nav-sep { width: 1px; height: 1em; background: var(--line); }
.account { display: flex; align-items: center; gap: .8rem; font-size: .9rem; margin-left: auto; }
.account a { color: var(--muted); }

.container { max-width: 980px; margin: 0 auto; padding: 1.25rem 1rem 4rem; }
.narrow { max-width: 440px; margin: 0 auto; padding: 1.5rem 1rem 4rem; }
.writing { max-width: 860px; margin: 0 auto; padding: 0 1rem 5rem; }
.login { padding-top: 12vh; }
.login-brand { color: var(--accent); font-size: 1.9rem; }

button, .button {
  display: inline-block; font: inherit; font-size: .94rem; line-height: 1.3; padding: .42rem .85rem; border-radius: 6px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer; text-decoration: none;
}
button:hover, .button:hover { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }
.primary, button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.quiet, button.quiet, .button.quiet { background: transparent; border-color: transparent; color: var(--muted); }
button.link { background: none; border: none; padding: 0; color: var(--accent); text-decoration: underline; font-size: .9rem; }
button.link.danger, .danger { color: var(--bad); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input, select, textarea { font: inherit; color: inherit; }
.input, .stack input:not([type=checkbox]), .stack select, .stack textarea, .filters input, .filters select {
  width: 100%; padding: .45rem .6rem; background: var(--surface); border: 1px solid var(--line); border-radius: 6px;
}
.stack { display: flex; flex-direction: column; gap: .8rem; }
.stack > label, .schema-form label { display: flex; flex-direction: column; gap: .25rem; font-size: .92rem; font-weight: 600; }
.stack > label.check, .check, .schema-form label.check { flex-direction: row; align-items: center; gap: .5rem; font-weight: 400; }
.stack > label .input, .stack > label input, .stack > label select, .stack > label textarea { font-weight: 400; }
.narrow-form { max-width: 460px; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; }
.hint { color: var(--muted); font-size: .88rem; margin: .2rem 0; }
.errorlist { list-style: none; margin: 0; padding: 0; color: var(--bad); font-size: .9rem; }
.empty { color: var(--muted); padding: 1.5rem 0; }

.messages { max-width: 980px; margin: .8rem auto 0; padding: 0 1rem; }
.message { padding: .5rem .8rem; border-radius: 6px; margin: 0 0 .5rem; background: var(--info-bg); color: var(--info-text); }
.message.success { background: var(--ins-bg); color: var(--text); }
.message.error { background: var(--bad-bg); color: var(--bad); }
.message.warn { background: var(--warn-bg); color: var(--warn-text); }
.secret { display: block; padding: .7rem .9rem; font-size: 1.05rem; background: var(--surface-2); border-radius: 6px; overflow-wrap: anywhere; user-select: all; }

.page-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; gap: .75rem; margin: .75rem 0 1rem; }
.page-head h1 { margin-bottom: .15rem; }

.menu { position: relative; }
.menu summary { list-style: none; cursor: pointer; }
.menu summary::-webkit-details-marker { display: none; }
.menu-items { position: absolute; right: 0; top: calc(100% + 4px); z-index: 20; min-width: 15rem; padding: .3rem; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 20px rgb(0 0 0 / .15); }
.menu-items button { display: block; width: 100%; text-align: left; border: none; background: none; padding: .45rem .6rem; }
.menu-items button:hover { background: var(--surface-2); }

.filters { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.filters input[type=search] { flex: 2 1 14rem; width: auto; }
.filters select { flex: 1 1 9rem; width: auto; min-width: 0; }

.entries { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
.entry { padding: .65rem .9rem; border-bottom: 1px solid var(--line); }
.entry:last-child { border-bottom: none; }
.entry-title { font-weight: 600; text-decoration: none; color: var(--text); overflow-wrap: anywhere; }
.entry-title:hover { color: var(--accent); text-decoration: underline; }
.entry-meta { display: flex; flex-wrap: wrap; gap: .25rem .6rem; align-items: center; margin-top: .15rem; font-size: .85rem; color: var(--muted); }
.tag { display: inline-block; padding: .03rem .45rem; border-radius: 4px; font-size: .78rem; font-weight: 500; background: var(--surface-2); color: var(--muted); white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; text-decoration: none; }
.tag.case { background: var(--accent-soft); color: var(--accent); }
.tag.draft { background: var(--warn-bg); color: var(--warn-text); }
.tag.bad { background: var(--bad-bg); color: var(--bad); }
.pager { display: flex; gap: 1rem; justify-content: center; padding: 1rem; color: var(--muted); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
.table { width: 100%; border-collapse: collapse; font-size: .94rem; }
.table th, .table td { padding: .55rem .8rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tr:last-child td { border-bottom: none; }
.table th { font-size: .8rem; color: var(--muted); font-weight: 600; }
.table .muted { color: var(--muted); }
.table .actions { text-align: right; white-space: nowrap; }
.table .actions form { display: inline-block; margin-left: .7rem; }

/* Editor */
.editor-bar { position: sticky; top: 0; z-index: 10; display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: .3rem 1rem; padding: .55rem 0; background: var(--bg); border-bottom: 1px solid var(--line); margin-bottom: 1rem; }
.editor-title { display: flex; align-items: center; gap: .55rem; min-width: 0; flex: 1 1 12rem; }
.editor-doc-title { font-weight: 600; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.editor-actions { display: flex; flex-wrap: wrap; align-items: center; gap: .55rem; justify-content: flex-end; }
.status { font-size: .86rem; color: var(--muted); }
.status[data-tone=ok] { color: var(--ok); }
.status[data-tone=warn] { color: var(--warn-text); background: var(--warn-bg); padding: .1rem .5rem; border-radius: 4px; }
.status[data-tone=bad] { color: var(--bad); font-weight: 600; }
.banner { padding: .65rem .9rem; border-radius: 8px; margin-bottom: .75rem; }
.banner p { margin: 0 0 .4rem; }
.banner p:last-child { margin-bottom: 0; }
.banner.info { background: var(--info-bg); color: var(--info-text); }
.banner.warn { background: var(--warn-bg); color: var(--warn-text); }
.banner.error { background: var(--bad-bg); color: var(--bad); }
.sheet { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 1.25rem; overflow-wrap: anywhere; }
.field-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: .9rem 1.1rem; }
.field-grid .field-long_text, .field-grid .field-file { grid-column: 1 / -1; }
.field { margin-bottom: .2rem; min-width: 0; }
.field-label { margin-bottom: .22rem; }
.field-label .label-text { font-size: .86rem; font-weight: 600; color: var(--muted); }
.required { color: var(--accent); font-weight: 500; font-size: .76rem; }
textarea.input { display: block; resize: vertical; }
.prose { font-family: var(--read); font-size: 1.1rem; line-height: 1.65; min-height: 12rem; padding: .75rem .9rem; overflow: hidden; }
.section-title { font-size: .95rem; font-weight: 700; color: var(--accent); margin: 1.5rem 0 .7rem; padding-bottom: .25rem; border-bottom: 1px solid var(--line); }
.field-group:first-child .section-title, .sheet > .section-title:first-child { margin-top: 0; }
.page .field-grid { margin-bottom: .9rem; }
#pages { margin-top: 1.2rem; }
.page { border-top: 2px solid var(--line); padding-top: .8rem; margin-top: 1.4rem; }
.page .field { margin-bottom: .9rem; }
.page-head-row { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: .3rem 1rem; margin-bottom: .55rem; }
.page-number { font-size: .95rem; margin: 0; color: var(--muted); }
.page-tools { display: flex; gap: .8rem; }
.page-add { margin-top: 1rem; }
.editor-foot { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 1rem; padding-top: 1rem; }

.ref { position: relative; }
.chips { list-style: none; display: flex; flex-wrap: wrap; gap: .3rem; margin: 0; padding: 0; }
.chips:not(:empty) { margin-bottom: .3rem; }
.chip { display: inline-flex; align-items: center; gap: .3rem; padding: .22rem .3rem .22rem .6rem; background: var(--accent-soft); color: var(--accent); border-radius: 6px; font-size: .94rem; max-width: 100%; }
.chip-bad { background: var(--bad-bg); color: var(--bad); }
.chip-remove { border: none; background: none; color: inherit; padding: 0 .3rem; font-size: 1.05rem; line-height: 1; }
.ref-results { position: absolute; left: 0; right: 0; top: 100%; z-index: 15; list-style: none; margin: 2px 0 0; padding: .25rem; max-height: 16rem; overflow-y: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 6px 20px rgb(0 0 0 / .15); }
.ref-results button { display: block; width: 100%; text-align: left; border: none; background: none; padding: .35rem .5rem; }
.ref-results button:hover, .ref-results button:focus { background: var(--surface-2); }
.ref-create { color: var(--accent); }
.ref-note { padding: .35rem .5rem; color: var(--muted); font-size: .9rem; }

.upload-item { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; margin-bottom: .5rem; }
.upload-name { font-size: .9rem; overflow-wrap: anywhere; }
.upload-progress { display: block; width: 100%; margin-top: .4rem; }
.upload-input { max-width: 100%; }
.upload-error { margin-top: .4rem; }
img.preview { display: block; max-width: 100%; max-height: 420px; border-radius: 4px; }
audio.preview { display: block; width: 100%; max-width: 480px; }

/* Reading */
.value { margin-bottom: .9rem; }
.value-label { font-size: .84rem; font-weight: 600; color: var(--muted); }
.prose-read { font-family: var(--read); font-size: 1.1rem; line-height: 1.65; }
.prose-read p { margin: 0 0 .9em; }
.used-by ul, .problems { list-style: none; margin: 0; padding: 0; }
.used-by li { padding: .2rem 0; }

/* History and diff */
.history { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
.history-item { padding: .65rem .9rem; border-bottom: 1px solid var(--line); }
.history-item:last-child { border-bottom: none; }
.history-head { display: flex; flex-wrap: wrap; align-items: center; gap: .3rem .6rem; }
.history-head a { font-weight: 600; }
.history-changes { margin: .15rem 0 0; font-size: .9rem; }
.diff { margin-bottom: 1.5rem; }
.diff-field h3 { font-size: .9rem; color: var(--muted); }
.diff-lines { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: .6rem .8rem; font-family: var(--read); overflow-wrap: anywhere; }
.diff-line { white-space: pre-wrap; min-height: 1.2em; }
.diff-gap { color: var(--muted); }
ins { background: var(--ins-bg); text-decoration: none; }
del { background: var(--del-bg); }

/* Validation */
.problem-group h2 { display: flex; align-items: center; gap: .5rem; }
.count { font-size: .8rem; font-weight: 600; padding: 0 .45rem; background: var(--surface-2); border-radius: 9px; }
.problems { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
.problems li { display: flex; flex-wrap: wrap; align-items: center; gap: .2rem .55rem; padding: .5rem .9rem; border-bottom: 1px solid var(--line); }
.problems li:last-child { border-bottom: none; }
.problem-text { color: var(--muted); font-size: .92rem; }

/* Schema */
.schema-section { margin-bottom: 1rem; }
.schema-section-head { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem .7rem; margin-bottom: .4rem; }
.schema-section-head .input { flex: 1 1 14rem; width: auto; font-weight: 600; }
.schema-group-title { font-size: .9rem; color: var(--muted); margin: 0 0 .4rem; }
.schema-empty { padding: .6rem .9rem; color: var(--muted); font-size: .9rem; }
.type-rows, .schema-fields { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
.type-row { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: .5rem; padding: .55rem .9rem; border-bottom: 1px solid var(--line); }
.type-row:last-child, .schema-field:last-child { border-bottom: none; }
.type-name { font-weight: 600; }
.schema-field { border-bottom: 1px solid var(--line); }
.schema-field summary { padding: .6rem .9rem; cursor: pointer; display: flex; flex-wrap: wrap; align-items: center; gap: .3rem .5rem; }
.schema-label { font-weight: 600; }
.schema-form { padding: .4rem .9rem 1rem; max-width: 560px; }
form[data-field-add].schema-form { padding: 0; }
.option-rows { display: flex; flex-direction: column; gap: .35rem; margin-bottom: .45rem; }
.option-row { display: flex; align-items: center; gap: .6rem; }
.option-row .input { flex: 1; min-width: 0; }
.token-form { margin-top: .8rem; max-width: 460px; flex-wrap: nowrap; }
.token-form button { white-space: nowrap; }
.stat-line { color: var(--muted); }

@media (max-width: 640px) {
  .sheet { padding: .85rem; }
  .prose { font-size: 1.05rem; }
  .editor-actions { width: 100%; justify-content: space-between; }
  .nav-sep { display: none; }
  .nav { order: 3; flex-basis: 100%; }
  .token-form { flex-wrap: wrap; }
}
