:root {
  --ui-bg: #e8e4dc;
  --ui-panel: #f6f3ed;
  --ui-line: #d5cec3;
  --ui-ink: #2a2622;
  --ui-muted: #6f675c;
  --page: #fffcf7;
  --ink: #1a1613;
  --page-w: 6in;
  --page-h: 9in;
  --margin-t: 0.75in;
  --margin-b: 0.75in;
  --margin-outer: 0.7in;
  --margin-inner: 0.8in;
  --font-body: Garamond, "EB Garamond", "Palatino Linotype", Palatino, serif;
  --font-size: 11.5pt;
  --line-height: 1.35;
  --sidebar-w: 240px;
  --comments-w: 300px;
  --mark: rgba(255, 196, 72, 0.45);
  --mark-active: rgba(255, 160, 40, 0.65);
}

.setup-banner {
  grid-column: 1 / -1;
  grid-row: 1;
  background: #3b2f1f;
  color: #f6f0e4;
  padding: 0.7rem 1rem;
  font-size: 0.86rem;
  line-height: 1.4;
}

.setup-banner code {
  background: rgba(255, 255, 255, 0.1);
  padding: 0.05rem 0.3rem;
  border-radius: 4px;
}

.setup-banner[hidden],
.field[hidden],
.format-field[hidden] {
  display: none !important;
}

body:has(.setup-banner:not([hidden])) {
  grid-template-rows: auto minmax(0, 1fr);
}

body:has(.setup-banner:not([hidden])) > .sidebar,
body:has(.setup-banner:not([hidden])) > .workspace,
body:has(.setup-banner:not([hidden])) > .comments-pane {
  grid-row: 2;
}

.seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.3rem;
}

.seg button {
  border: 1px solid var(--ui-line);
  background: #fff;
  color: var(--ui-ink);
  border-radius: 6px;
  padding: 0.45rem 0.5rem;
  cursor: pointer;
  font-size: 0.82rem;
}

.seg button.active {
  background: #2a2622;
  color: #f6f3ed;
  border-color: #2a2622;
}

.edit-status {
  margin-top: 0.35rem;
  color: var(--ui-muted);
  font-size: 0.78rem;
}

.ledger-action {
  font-weight: 600;
  color: var(--ui-ink);
}

.ledger-card .body {
  font-size: 0.84rem;
  line-height: 1.35;
}

.comment-filters[hidden],
.comments-list[hidden] {
  display: none !important;
}

body.mode-edit .block[contenteditable="true"] {
  outline: 1px dashed transparent;
  border-radius: 2px;
  white-space: pre-wrap;
}

body.mode-edit .block[contenteditable="true"]:hover {
  outline-color: rgba(42, 38, 34, 0.25);
  background: rgba(255, 248, 230, 0.55);
}

body.mode-edit .block[contenteditable="true"]:focus {
  outline: 2px solid rgba(196, 154, 74, 0.7);
  background: rgba(255, 248, 230, 0.85);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--ui-bg);
  color: var(--ui-ink);
  font-family: "Source Serif 4", Georgia, serif;
}

body {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  overflow: hidden;
  transition: grid-template-columns 0.2s ease;
  min-height: 100%;
  height: 100%;
}

body.comments-open {
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--comments-w);
}

body > .sidebar,
body > .workspace,
body > .comments-pane {
  min-height: 0;
}

button, select, input {
  font: inherit;
}

.sidebar {
  background: var(--ui-panel);
  border-right: 1px solid var(--ui-line);
  padding: 1.1rem 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  overflow: auto;
  min-height: 0;
}

.sidebar-head h1 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.sidebar-head p {
  margin: 0.3rem 0 0;
  color: var(--ui-muted);
  font-size: 0.8rem;
  line-height: 1.35;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.78rem;
  color: var(--ui-muted);
}

.field span {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
}

.field strong {
  color: var(--ui-ink);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.field select,
.page-jump input {
  width: 100%;
  border: 1px solid var(--ui-line);
  background: #fff;
  color: var(--ui-ink);
  border-radius: 6px;
  padding: 0.45rem 0.6rem;
}

.field input[type="range"] {
  width: 100%;
  accent-color: #5b5348;
}

.format-seg {
  grid-template-columns: 1fr 1fr;
}

.format-seg button strong,
.format-seg button em {
  font-size: 1rem;
  font-family: Georgia, serif;
}

.format-seg button.active {
  background: #2a2622;
  color: #f6f3ed;
  border-color: #2a2622;
}

.sidebar-actions {
  display: grid;
  gap: 0.4rem;
}

.sidebar-actions button,
.pager button,
.card-actions button,
.composer-actions button,
.selection-bar button,
.ghost-close {
  border: 1px solid var(--ui-line);
  background: #fff;
  color: var(--ui-ink);
  border-radius: 6px;
  padding: 0.45rem 0.7rem;
  cursor: pointer;
}

.sidebar-actions button {
  width: 100%;
}

.sidebar-actions button:hover,
.pager button:hover:not(:disabled),
.card-actions button:hover,
.composer-actions button:hover,
.ghost-close:hover {
  background: #efebe3;
}

.field input[type="text"] {
  width: 100%;
  border: 1px solid var(--ui-line);
  background: #fff;
  color: var(--ui-ink);
  border-radius: 6px;
  padding: 0.45rem 0.6rem;
}

.primary {
  background: #2a2622 !important;
  color: #f6f3ed !important;
  border-color: #2a2622 !important;
}

.sidebar-meta {
  margin-top: auto;
  padding-top: 0.85rem;
  border-top: 1px solid var(--ui-line);
  display: grid;
  gap: 0.3rem;
  font-size: 0.78rem;
}

.sidebar-meta span {
  color: var(--ui-muted);
  display: inline-block;
  min-width: 3.2rem;
}

.sidebar-meta em {
  font-style: normal;
  font-variant-numeric: tabular-nums;
}

.workspace {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--ui-line);
  background: var(--ui-panel);
  flex-shrink: 0;
}

.title-block .eyebrow {
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ui-muted);
}

#docTitle {
  font-size: 0.92rem;
  margin-top: 0.1rem;
}

.pager {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex-shrink: 0;
}

.pager button {
  white-space: nowrap;
}

.pager button:disabled {
  opacity: 0.4;
  cursor: default;
}

.page-jump {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  color: var(--ui-muted);
  font-size: 0.86rem;
}

.page-jump input {
  width: 3.2rem;
  text-align: center;
  padding: 0.35rem 0.3rem;
}

.stage {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.scaler-frame {
  position: relative;
  flex-shrink: 0;
}

.scaler {
  transform-origin: top left;
  position: absolute;
  top: 0;
  left: 0;
}

.spread {
  display: flex;
  background: #d9d2c7;
  box-shadow:
    0 1px 2px rgba(42, 38, 34, 0.06),
    0 14px 32px rgba(42, 38, 34, 0.12);
}

.spread.single {
  background: transparent;
  box-shadow: none;
}

.page {
  width: var(--page-w);
  height: var(--page-h);
  background: var(--page);
  color: var(--ink);
  position: relative;
  overflow: hidden;
}

.spread:not(.single) .page.left {
  border-right: 1px solid #e2dbd0;
  box-shadow: inset -8px 0 14px -14px rgba(0, 0, 0, 0.2);
}

.spread:not(.single) .page.right {
  box-shadow: inset 8px 0 14px -14px rgba(0, 0, 0, 0.2);
}

.spread.single .page {
  box-shadow:
    0 1px 2px rgba(42, 38, 34, 0.06),
    0 14px 32px rgba(42, 38, 34, 0.12);
}

.page .inner {
  height: 100%;
  padding: var(--margin-t) var(--margin-outer) var(--margin-b) var(--margin-inner);
  display: flex;
  flex-direction: column;
  font-family: var(--font-body);
  font-size: var(--font-size);
  line-height: var(--line-height);
  hyphens: auto;
}

.page.right .inner {
  padding-left: var(--margin-outer);
  padding-right: var(--margin-inner);
}

.page .folio {
  margin-top: auto;
  padding-top: 0.5rem;
  font-size: 9pt;
  color: #6a6258;
  text-align: center;
  letter-spacing: 0.04em;
  font-variant-numeric: tabular-nums;
}

.block {
  margin: 0;
  text-align: justify;
  text-indent: 0;
}

.block.body { text-indent: 1.6em; }
.block.body.noindent { text-indent: 0; }

.block.part,
.block.chapter {
  margin: 1.6in 0 0.9em;
  text-align: center;
  font-size: 0.9em;
  font-weight: 700;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: #5f574d;
}

.block.part-title,
.block.chapter-title {
  text-align: center;
  font-size: 1.75em;
  font-weight: 700;
  line-height: 1.15;
  text-wrap: balance;
}
.block.part-title { margin-bottom: 1.6em; }
.block.chapter-title { margin-bottom: 0.45em; }

.block.subtitle {
  text-align: center;
  font-style: italic;
  font-size: 1.12em;
  margin-bottom: 1.8em;
}

.block.epigraph {
  text-align: center;
  font-style: italic;
  padding: 0 0.4in;
  margin-bottom: 0.25em;
}

.block.attr {
  text-align: center;
  font-size: 0.88em;
  margin-bottom: 2em;
}

.block.heading {
  margin: 1.5em 0 0.8em;
  font-size: 1.13em;
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  text-wrap: balance;
}

.block.heading.first-on-page { margin-top: 0.2em; }

.block.display {
  text-align: left;
  line-height: 1.25;
  margin: 0 0 0.22em;
}
.block.display.group-first { margin-top: 0.75em; }
.block.display.group-last { margin-bottom: 0.75em; }
.block.display.center {
  text-align: center;
  margin: 1.2em 0 0.5em;
}

.block.list {
  text-align: left;
  line-height: 1.2;
  padding-left: 0.35in;
  margin: 0 0 0.15em;
}
.block.list.group-first { margin-top: 0.6em; }
.block.list.group-last { margin-bottom: 0.6em; }
.block strong {
  font-weight: 700;
}
.block em {
  font-style: italic;
}

.cover-like {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  height: 100%;
  gap: 0.8rem;
  padding: 0.5in;
}

.cover-like .kicker {
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 0.72em;
  color: #6a6258;
}

.cover-like h1 {
  margin: 0;
  font-size: 1.55em;
  line-height: 1.2;
  font-weight: 700;
  text-wrap: balance;
}

.cover-like p {
  margin: 0;
  color: #6a6258;
  font-size: 0.95em;
  font-style: italic;
}

.measure {
  position: absolute;
  left: -10000px;
  top: 0;
  width: var(--page-w);
  height: var(--page-h);
  visibility: hidden;
  pointer-events: none;
}

.measure .inner {
  height: 100%;
  padding: var(--margin-t) var(--margin-outer) var(--margin-b) var(--margin-inner);
  font-family: var(--font-body);
  font-size: var(--font-size);
  line-height: var(--line-height);
  display: flex;
  flex-direction: column;
}

mark.c-mark {
  background: var(--mark);
  color: inherit;
  border-radius: 2px;
  cursor: pointer;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

mark.c-mark.active {
  background: var(--mark-active);
  outline: 1px solid rgba(180, 110, 20, 0.45);
}

.comments-pane {
  display: none;
  background: var(--ui-panel);
  border-left: 1px solid var(--ui-line);
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

body.comments-open .comments-pane {
  display: flex;
}

.comments-head {
  padding: 0.9rem 0.9rem 0.55rem;
  border-bottom: 1px solid var(--ui-line);
  flex-shrink: 0;
}

.comments-head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.55rem;
}

.comments-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.ghost-close {
  padding: 0.28rem 0.55rem;
  font-size: 0.75rem;
}

.comment-filters {
  display: flex;
  gap: 0.35rem;
}

.chip {
  border: 1px solid var(--ui-line);
  background: #fff;
  color: var(--ui-muted);
  border-radius: 999px;
  padding: 0.22rem 0.65rem;
  font-size: 0.75rem;
  cursor: pointer;
}

.chip.active {
  background: #2a2622;
  color: #f6f3ed;
  border-color: #2a2622;
}

.comments-list {
  flex: 1;
  overflow: auto;
  padding: 0.7rem;
  display: grid;
  gap: 0.65rem;
  align-content: start;
}

.comments-empty {
  color: var(--ui-muted);
  font-size: 0.86rem;
  line-height: 1.4;
  padding: 0.75rem 0.25rem;
}

.comment-card {
  background: #fff;
  border: 1px solid var(--ui-line);
  border-radius: 8px;
  padding: 0.7rem 0.75rem;
  display: grid;
  gap: 0.4rem;
}

.comment-card.active {
  border-color: #c49a4a;
  box-shadow: 0 0 0 1px rgba(196, 154, 74, 0.35);
}

.comment-card .meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.55rem;
  font-size: 0.72rem;
  color: var(--ui-muted);
}

.comment-card .meta .tag {
  color: #4f6b45;
  font-weight: 600;
}

.comment-card .quote {
  font-size: 0.82rem;
  font-style: italic;
  color: #5a5349;
  line-height: 1.35;
}

.comment-card .body,
.reply .body {
  font-size: 0.88rem;
  line-height: 1.4;
  white-space: pre-wrap;
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.15rem;
}

.card-actions button {
  padding: 0.28rem 0.5rem;
  font-size: 0.72rem;
}

.reply {
  border-top: 1px solid var(--ui-line);
  padding-top: 0.4rem;
}

.reply-form {
  display: grid;
  gap: 0.4rem;
}

.reply-form textarea,
#composerBody {
  width: 100%;
  border: 1px solid var(--ui-line);
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  resize: vertical;
  background: #fff;
  color: var(--ui-ink);
}

.composer-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.4rem;
}

.selection-bar {
  position: fixed;
  z-index: 40;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}

.selection-bar button {
  background: #2a2622;
  color: #f6f3ed;
  border-color: #2a2622;
  padding: 0.45rem 0.8rem;
}

.composer {
  position: fixed;
  z-index: 50;
  left: 50%;
  top: 18%;
  transform: translateX(-50%);
  width: min(420px, calc(100vw - 2rem));
  background: #fff;
  border: 1px solid var(--ui-line);
  border-radius: 10px;
  padding: 0.9rem;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
  display: grid;
  gap: 0.55rem;
}

.composer[hidden],
.selection-bar[hidden],
.reply-form[hidden] {
  display: none !important;
}

.composer-quote {
  font-size: 0.86rem;
  font-style: italic;
  color: #5a5349;
  line-height: 1.35;
  max-height: 4.5em;
  overflow: auto;
}

@media (max-width: 1100px) {
  body.comments-open {
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  }

  body.comments-open .comments-pane {
    position: fixed;
    right: 0;
    top: 0;
    bottom: 0;
    width: min(320px, 92vw);
    z-index: 30;
    box-shadow: -12px 0 30px rgba(0, 0, 0, 0.12);
  }
}

@media (max-width: 960px) {
  body,
  body.comments-open {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--ui-line);
    padding: 0.65rem 0.75rem;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.45rem 0.6rem;
    align-items: end;
  }

  .sidebar-head { display: none; }
  .sidebar-actions,
  .sidebar-meta { display: none; }
  .field:nth-of-type(1) { grid-column: 1 / -1; }
  .field:nth-of-type(5),
  .field:nth-of-type(6) { display: none; }

  .topbar { padding: 0.55rem 0.75rem; }
  .title-block .eyebrow { display: none; }

  #docTitle {
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 36vw;
  }
}
