:root {
  --bg: #f3f4f7; --panel: #ffffff; --ink: #1d2230; --muted: #667085; --line: #dde1e8;
  --brand: #2f8f4e; --brand-ink: #fff; --danger: #c0392b; --ok: #1f8a4c; --focus: #3b82f6;
  --radius: 8px; --shadow: 0 1px 3px rgba(16,24,40,.08), 0 4px 12px rgba(16,24,40,.06);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; color: var(--ink);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #11151d; --panel: #1a1f2a; --ink: #e7eaf0; --muted: #98a2b3; --line: #2c3341; --shadow: 0 1px 3px rgba(0,0,0,.4); }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 14px; }
a { color: var(--focus); }
h1 { font-size: 22px; } h2 { font-size: 17px; margin: 0 0 8px; } h3 { font-size: 14px; margin: 12px 0 6px; }
.hint, .muted, small { color: var(--muted); }
.share-hint code { user-select: all; color: var(--ink); background: rgba(127,127,127,.15); padding: 1px 6px; border-radius: 4px; }
.error { color: var(--danger); } .ok { color: var(--ok); }
.mono { font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.grow { flex: 1; }
[hidden] { display: none !important; }

/* buttons & inputs */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 7px 12px; border-radius: 6px; cursor: pointer; font: inherit; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: #9aa3b2; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn-primary:hover { filter: brightness(1.08); border-color: var(--brand); }
.btn-danger { color: var(--danger); }
.btn-small { padding: 4px 9px; font-size: 13px; }
.btn-discord { background: #5865f2; border-color: #5865f2; font-size: 15px; padding: 10px 18px; }
.icon-btn { border: 0; background: none; color: var(--muted); font-size: 16px; cursor: pointer; padding: 4px 8px; }
input[type=text], input[type=search], input[type=number], select, textarea {
  font: inherit; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; width: 100%; min-width: 0;
}
textarea { resize: vertical; font-family: inherit; }
input[type=color] { width: 38px; height: 30px; padding: 2px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); flex: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-weight: 600; font-size: 13px; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.insp-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.insp-row > input[type=text], .insp-row > input[type=search] { flex: 1; min-width: 160px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 14px; padding: 8px 16px; background: #151f33; color: #fff; min-height: 52px; position: sticky; top: 0; z-index: 20; }
.topbar a { color: #fff; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; text-decoration: none; }
.brand img { width: 30px; height: 30px; }
#userbar { display: flex; align-items: center; gap: 12px; margin-left: auto; }
.navlink { text-decoration: none; opacity: .85; } .navlink:hover { opacity: 1; }
.me { display: flex; align-items: center; gap: 6px; }
.me img { width: 24px; height: 24px; border-radius: 50%; }
.doc-title { display: flex; flex-direction: column; min-width: 0; }
.doc-title #guide-title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-title .muted { color: #b9c2d3; font-size: 12px; }
.topbar .actions { display: flex; gap: 8px; margin-left: auto; }
.topbar .btn:not(.btn-primary) { background: transparent; color: #fff; border-color: rgba(255,255,255,.35); }
.guide-page #userbar { margin-left: 4px; }

/* pages */
.page { max-width: 1100px; margin: 0 auto; padding: 24px 16px 60px; }
.narrow-page { display: grid; place-items: center; min-height: 100vh; }
.narrow { max-width: 440px; background: var(--panel); padding: 32px; border-radius: 12px; box-shadow: var(--shadow); text-align: center; }
.narrow .stack { text-align: left; }
.narrow-logo { width: 72px; height: 72px; }
.empty-state { text-align: center; padding: 60px 16px; }

.guide-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.guide-card { display: flex; flex-direction: column; background: var(--page); color: #fff; text-decoration: none; border-radius: 10px; overflow: hidden; box-shadow: var(--shadow); min-height: 170px; transition: transform .1s; }
.guide-card:hover { transform: translateY(-2px); }
.guide-card-band { height: 18px; background: var(--accent); clip-path: polygon(0 0, 100% 0, 100% 100%, 55% 100%, 30% 35%, 0 35%); }
.guide-card-body { padding: 14px 16px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.guide-card-body h2 { color: #fff; }
.guide-card-body p { margin: 0; color: #cdd5e3; font-size: 13px; }
.guide-card .muted { color: #9aa6bb; }
.badges { margin-top: auto; display: flex; gap: 6px; flex-wrap: wrap; padding-top: 10px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: rgba(255,255,255,.14); color: #fff; }
.badge-admin { background: #b23b3b; } .badge-editor { background: var(--brand); }
.create-form { margin-top: 24px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; background: var(--panel); border: 1px dashed var(--line); padding: 14px; border-radius: var(--radius); }
.create-form h2 { margin: 0 8px 0 0; }
.create-form input { flex: 2; min-width: 220px; } .create-form select { flex: 1; min-width: 150px; width: auto; }

/* admin */
.status-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.role-picker { display: flex; flex-wrap: wrap; gap: 6px; max-height: 260px; overflow: auto; padding: 6px 0; }
.role { border: 1px solid var(--line); border-radius: 99px; padding: 3px 10px; font-size: 13px; }
.role.on { border-color: var(--brand); background: rgba(47,143,78,.12); }
.role-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.scroll-x { overflow-x: auto; }
table.audit { width: 100%; border-collapse: collapse; font-size: 13px; }
table.audit td, table.audit th { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.audit td.mono { max-width: 420px; overflow-wrap: anywhere; }

/* workspace */
.guide-page { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
.workspace { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); min-height: 0; }
.preview-hidden .workspace { grid-template-columns: minmax(0, 1fr) 0; }
.preview-hidden .preview-pane { display: none; }
.reader .workspace { grid-template-columns: 1fr; }
.edit-pane { display: flex; flex-direction: column; min-height: 0; position: relative; border-right: 1px solid var(--line); }
.edit-scroll { flex: 1; overflow: auto; padding: 18px 0; background: #5a5f6a; }
.preview-pane { display: flex; flex-direction: column; min-height: 0; }
.preview-bar { display: flex; align-items: center; gap: 8px; padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--line); }
#preview-frame { flex: 1; width: 100%; border: 0; background: #5a5f6a; }
.lock-banner { display: flex; gap: 12px; align-items: center; justify-content: center; padding: 8px 12px; background: #fff3cd; color: #5c4400; font-size: 13px; }
.empty-guide { padding: 14px 18px; background: var(--panel); border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.empty-guide h2, .empty-guide p { margin: 0; }
.empty-guide .insp-row > input { max-width: 640px; }
.readonly .toolbar { opacity: .5; pointer-events: none; }
.settings-readonly .tab-pane input, .settings-readonly .tab-pane select, .settings-readonly .tab-pane textarea, .settings-readonly .tab-pane button { pointer-events: none; opacity: .6; }

/* toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 4px; padding: 6px 8px; background: var(--panel); border-bottom: 1px solid var(--line); }
.tb-group { display: flex; gap: 2px; align-items: center; padding-right: 6px; margin-right: 2px; border-right: 1px solid var(--line); }
.tb-group:last-child { border-right: 0; }
.tb { min-width: 30px; height: 30px; border: 1px solid transparent; background: none; border-radius: 5px; cursor: pointer; color: var(--ink); font: inherit; padding: 0 6px; }
.tb:hover { background: rgba(127,127,127,.15); }
.tb.on { background: rgba(47,143,78,.2); border-color: rgba(47,143,78,.5); }
.tb-accent { color: var(--brand); font-weight: 700; }
.toolbar select { width: auto; height: 30px; padding: 2px 6px; }
.toolbar input[type=color] { width: 30px; height: 28px; }

/* the editable page: A4 width, the guide's own margins and the print page's text styles (injected as #guide-theme),
   so lines wrap where they do in the PDF. Only page breaks differ; the dashed "Page N" lines show where they fall. */
.edit-scroll { overflow-x: hidden; }
.ed-page { width: 210mm; margin: 0 auto; background: var(--pg-band) top center / 100% auto no-repeat, var(--pg-bg); padding: var(--m-top) var(--m-right) var(--m-bottom) var(--m-left); box-shadow: 0 2px 12px rgba(0,0,0,.4); min-height: 297mm; position: relative; }
.ed-page .ProseMirror { outline: none; min-height: 200mm; }
.ed-page .ProseMirror > :first-child { margin-top: 0; }
.ed-page h1[data-toc="false"]::after, .ed-page h2[data-toc="false"]::after { content: ' (not in contents)'; font: 400 11px system-ui, sans-serif; color: #98a2b3; }
.ed-page hr.ProseMirror-selectednode { border-top-color: var(--focus); }
.ed-page p.is-editor-empty:first-child::before { content: attr(data-placeholder); color: #8a93a6; float: left; height: 0; pointer-events: none; }
.ed-page .ProseMirror-selectednode { outline: 2px solid var(--focus); outline-offset: 2px; }
/* blank lines are the guide's spacing: show them faintly in the editor (not in the PDF) */
.ed-page .guide > p:has(> br.ProseMirror-trailingBreak:only-child) { background: rgba(255,255,255,.025); box-shadow: inset 2px 0 0 rgba(255,255,255,.14); }
.ed-page .guide > p.has-focus:has(> br.ProseMirror-trailingBreak:only-child) { background: rgba(140,200,255,.14); }
.ed-page pre code { background: none; padding: 0; }
.ed-page .tableWrapper { overflow-x: auto; }
.ed-page table.tbl-plain td, .ed-page table.tbl-plain th { outline: 1px dashed rgba(255,255,255,.18); outline-offset: -1px; }
.ed-page .selectedCell::after { content: ''; position: absolute; inset: 0; background: rgba(59,130,246,.3); pointer-events: none; }
.ed-page td, .ed-page th { position: relative; }
.ed-page .column-resize-handle { position: absolute; right: -2px; top: 0; bottom: 0; width: 4px; background: var(--focus); pointer-events: none; }
.ed-page td[data-align="center"], .ed-page th[data-align="center"] { text-align: center; }
.ed-page td[data-align="right"], .ed-page th[data-align="right"] { text-align: right; }
.ed-figure { position: relative; }
.ed-figure[data-label]:not([data-label=""])::after { content: attr(data-label); position: absolute; top: 4px; left: 4px; font: 11px system-ui, sans-serif; background: rgba(0,0,0,.65); color: #fff; padding: 1px 6px; border-radius: 3px; }
.ed-figure-missing { border: 2px dashed #ff6b6b; color: #ff9b9b; padding: 30px 10px; font: 13px system-ui, sans-serif; }
.ed-column { outline: 1px dashed rgba(255,255,255,.22); outline-offset: 2px; }
.columns-bordered .ed-column { outline: 0; }
.ed-columns-wrap { position: relative; }
.ed-col-handle { position: absolute; top: 0; bottom: 0; width: 14px; margin-left: -7px; cursor: col-resize; z-index: 3; }
.ed-col-handle::before { content: ''; position: absolute; left: 6px; top: 0; bottom: 0; width: 2px; background: var(--pg-accent); opacity: 0; transition: opacity .1s; }
.ed-col-handle::after { content: '⋮'; position: absolute; left: 0; top: 50%; transform: translateY(-50%); width: 14px; height: 28px; line-height: 28px; text-align: center; background: var(--pg-accent); color: #111; border-radius: 4px; font: 700 14px system-ui, sans-serif; opacity: 0; transition: opacity .1s; }
.ed-columns-wrap:hover .ed-col-handle::before, .ed-columns-wrap:hover .ed-col-handle::after, .ed-columns-wrap.dragging .ed-col-handle::before, .ed-columns-wrap.dragging .ed-col-handle::after { opacity: 1; }
.ed-columns-wrap.dragging { cursor: col-resize; user-select: none; }
.ed-col-tip { display: none; position: absolute; top: -26px; left: 50%; transform: translateX(-50%); background: #111; color: #fff; font: 12px system-ui, sans-serif; padding: 3px 8px; border-radius: 4px; z-index: 4; white-space: nowrap; }
.ed-page-break { margin: 14px 0; border-top: 2px dashed rgba(255,255,255,.4); text-align: center; height: 0; clear: both; }
.ed-page-break span { position: relative; top: -10px; background: var(--pg-bg); padding: 0 8px; font: 11px system-ui, sans-serif; color: #b6bfd0; }
.ed-toc { border: 1px dashed var(--pg-accent); padding: 10px 14px; margin: 8px 0 14px; font-size: .92em; text-align: left; }
.ed-toc-title { font-weight: 700; margin-bottom: 4px; }
.ed-toc-title span { font-weight: 400; font: 11px system-ui, sans-serif; color: #98a2b3; margin-left: 6px; }
.ed-toc ul { list-style: none; margin: 0; padding: 0; }
.ed-page .guide .ed-toc li::marker { content: none; }
.ed-page .guide .ed-toc li { padding-left: 0; }
.ed-page .guide .ed-toc li.l2 { padding-left: 18px; opacity: .85; } .ed-page .guide .ed-toc li.l3 { padding-left: 36px; opacity: .7; }
.ed-ref { display: inline-block; padding: 0 6px; margin: 0 1px; border-radius: 4px; background: rgba(140,200,255,.18); border: 1px solid rgba(140,200,255,.55); color: #cfe6ff; font-size: .92em; line-height: 1.3; cursor: pointer; text-align: left; }
.ed-ref::before { content: '↪ '; opacity: .7; }
.ed-ref-broken { background: rgba(255,82,82,.2); border-color: #ff5252; color: #ffb4b4; }
.ed-ref.ProseMirror-selectednode { outline: 2px solid var(--focus); }
/* where the PDF's pages start, from the last preview layout */
.ed-pagemark { position: relative; height: 0; margin: 0 calc(-1 * var(--m-right)) 0 calc(-1 * var(--m-left)); border-top: 1px dashed rgba(255,255,255,.35); pointer-events: none; user-select: none; clear: both; }
.ed-pagemark span { position: absolute; right: 6px; top: -9px; font: 600 10px/16px system-ui, sans-serif; color: #c9d1df; background: var(--pg-bg); padding: 0 6px; border-radius: 3px; border: 1px solid rgba(255,255,255,.25); }
.ed-pagemark.cont span::after { content: ' (continues partway through)'; font-weight: 400; opacity: .8; }

/* inspector */
.inspector { position: absolute; right: 14px; top: 58px; width: 270px; max-height: calc(100% - 72px); overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 6px 24px rgba(0,0,0,.25); padding: 10px 12px; z-index: 5; display: flex; flex-direction: column; gap: 4px; }
.insp-section { display: flex; flex-direction: column; gap: 8px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.insp-section:last-child { border-bottom: 0; padding-bottom: 0; }
.insp-section h3 { margin: 4px 0 0; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.insp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.insp-grid .tb { border: 1px solid var(--line); font-size: 12px; }
.insp-grid4 { grid-template-columns: repeat(4, 1fr); }

/* modal */
.modal-back { position: fixed; inset: 0; background: rgba(10,14,22,.55); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--panel); color: var(--ink); border-radius: 12px; width: min(520px, 100%); max-height: calc(100vh - 32px); overflow: auto; padding: 16px 20px 20px; box-shadow: 0 10px 40px rgba(0,0,0,.35); }
.modal-wide { width: min(880px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.modal-head h2 { margin: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tab { border: 0; background: none; padding: 8px 12px; cursor: pointer; color: var(--muted); font: inherit; border-bottom: 2px solid transparent; }
.tab.on { color: var(--ink); border-bottom-color: var(--brand); font-weight: 600; }
.people-group { border: 1px solid var(--line); border-radius: 6px; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.cover-thumb { width: 120px; height: 68px; border: 1px solid var(--line); border-radius: 4px; display: grid; place-items: center; overflow: hidden; font-size: 11px; }
.cover-thumb img { max-width: 100%; max-height: 100%; }
.member { display: flex; align-items: center; gap: 8px; padding: 5px 0; border-bottom: 1px solid var(--line); }
.member img, .avatar-ph { width: 26px; height: 26px; border-radius: 50%; background: var(--line); flex: none; }
.ref-list { max-height: 280px; overflow: auto; border: 1px solid var(--line); border-radius: 6px; }
.ref-item { display: flex; gap: 8px; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 6px 10px; cursor: pointer; font: inherit; color: var(--ink); }
.ref-item:hover { background: rgba(127,127,127,.1); }
.ref-item.on { background: rgba(47,143,78,.18); }
.ref-item.lvl-1 { font-weight: 700; } .ref-item.lvl-2 { padding-left: 24px; } .ref-item.lvl-3 { padding-left: 40px; } .ref-item.lvl-4 { padding-left: 56px; }
.ref-icon { width: 18px; color: var(--muted); }
.ref-formats { display: flex; flex-direction: column; gap: 4px; }
.emoji-grid { display: grid; grid-template-columns: repeat(9, 1fr); gap: 4px; }
.emoji { font-size: 22px; border: 1px solid var(--line); background: none; border-radius: 6px; padding: 4px; cursor: pointer; }
.emoji-grid .hint { grid-column: 1 / -1; }
.hist { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.pub { border: 1px solid var(--line); border-radius: 6px; padding: 8px; margin-bottom: 8px; display: flex; flex-direction: column; gap: 4px; }
.pub-notes { white-space: pre-wrap; font-size: 13px; }
.rev-list { max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: 6px; margin-top: 6px; }
.rev { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 5px 8px; cursor: pointer; font: inherit; color: var(--ink); }
.rev:hover { background: rgba(127,127,127,.1); }
.rev-kind { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--line); }
.rev-publish { background: var(--brand); color: #fff; } .rev-manual { background: #3b82f6; color: #fff; }
.hist-detail { margin-top: 10px; }
.diff { font-family: ui-monospace, Menlo, monospace; font-size: 12px; max-height: 260px; overflow: auto; border: 1px solid var(--line); border-radius: 6px; padding: 6px; }
.diff .add { color: var(--ok); } .diff .del { color: var(--danger); }
.problems { background: rgba(192,57,43,.08); border: 1px solid rgba(192,57,43,.35); border-radius: 6px; padding: 8px 12px; }
.problems ul, .problems ol { margin: 6px 0; padding-left: 20px; }
.import-report ul { padding-left: 18px; font-size: 13px; max-height: 200px; overflow: auto; }

/* toast */
.toast { position: fixed; left: 50%; bottom: 20px; transform: translate(-50%, 30px); background: #1d2230; color: #fff; padding: 10px 16px; border-radius: 8px; opacity: 0; transition: all .2s; z-index: 100; max-width: 90vw; pointer-events: none; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast-error { background: var(--danger); } .toast-ok { background: var(--ok); }

@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; }
  .preview-pane { display: none; }
  .reader .preview-pane { display: flex; }
  .inspector { position: static; width: auto; margin: 8px; }
  .hist, .grid3 { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; }
}
