/* Book Editor — a writing desk that lays type out as a real B-format book.
   Interior follows Tschichold's Penguin Composition Rules: justified setting,
   1em paragraph indent, no indent on opening paragraphs (dropcap instead),
   small-caps running heads + contents, centred arabic folios. Cover is the
   classic orange tri-band. CSP-clean: system fonts only, no CDN. */

:root {
  /* B-format-ish page the user asked for */
  --pw: 13cm;
  --ph: 19.5cm;
  --mtop: 1.7cm;
  --mbot: 1.7cm;
  --mx: 1.6cm;
  --gap: 26px;                 /* space between page sheets (screen px) */

  --paper: #fbf9f3;
  --desk: #e4e0d8;
  --ink: #1a1712;
  --chrome: #efece6;
  --rule: #d9d3c7;
  --penguin: #EA5B0C;
  --cover: #FF6900;       /* cover band colour (Penguin genre coding) */
  --cover-ink: #111;      /* band text colour, auto black/white for contrast */

  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;
  --sans: "Gill Sans", "Gill Sans MT", Futura, "Trebuchet MS", Avenir, -apple-system, system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }
body {
  display: grid;
  grid-template-columns: 58px auto 1fr;
  grid-template-rows: 1fr;
  height: 100vh;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--desk);
}

/* ── Icon rail ───────────────────────────────────────────────────────────── */
.rail {
  background: var(--chrome); border-right: 1px solid var(--rule);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 12px 0;
}
.rail-btn {
  position: relative; width: 42px; height: 42px;
  border: 1px solid transparent; border-radius: 11px; background: transparent;
  display: flex; align-items: center; justify-content: center;
  color: #5a5346; cursor: pointer;
}
.rail-btn:hover { background: rgba(0,0,0,0.05); }
.rail-btn.active { background: #fff; border-color: var(--rule-strong); box-shadow: 0 1px 4px var(--shadow); color: var(--ink); }
.rail-btn svg { width: 22px; height: 22px; }
.rail-badger { width: 26px; height: 26px; object-fit: contain; }
.rail-badge {
  position: absolute; top: -3px; right: -3px;
  min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px;
  background: var(--penguin); color: #fff; font-size: 10px; font-weight: 700;
  display: none; align-items: center; justify-content: center;
}
.rail-badge.show { display: flex; }

/* ── Drawer + panels ─────────────────────────────────────────────────────── */
.drawer {
  width: 270px; background: var(--chrome); border-right: 1px solid var(--rule);
  overflow-y: auto; padding: 18px 16px;
}
.drawer[hidden] { display: none; }
.drawer-panel { display: none; flex-direction: column; gap: 16px; }
.drawer-panel.active { display: flex; }
.drawer-h { font-family: var(--serif); font-size: 16px; font-weight: 500; margin: 0; }
.drawer-group { display: flex; flex-direction: column; gap: 8px; }
.group-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.09em; color: #8a8172; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.09em; color: #8a8172; }
.field input {
  font: inherit; font-size: 14px; padding: 8px 10px;
  border: 1px solid var(--rule); border-radius: 8px; background: #fff; color: var(--ink);
}
.field input:focus { outline: none; border-color: var(--penguin); box-shadow: 0 0 0 2px rgba(234,91,12,0.14); }

/* Cover colour swatches */
.cover-swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.cover-sw {
  width: 30px; height: 30px; border-radius: 7px; padding: 0; cursor: pointer;
  border: 2px solid rgba(0, 0, 0, 0.15);
}
.cover-sw.active { box-shadow: 0 0 0 2px var(--chrome), 0 0 0 4px var(--ink); }
.cover-genre { font-size: 12px; color: #6f6656; min-height: 1em; }

.panel-note { font-size: 12px; line-height: 1.5; color: #6f6656; }
.panel-note em { color: #4a4236; font-style: italic; }
.panel-stats { font-size: 12px; color: #8a8172; font-variant-numeric: tabular-nums; border-top: 1px solid var(--rule); padding-top: 12px; }

/* Buttons (reused inside the drawer) */
.tbtn {
  font: inherit; font-size: 13px; height: 36px; padding: 0 12px;
  border: 1px solid var(--rule); border-radius: 8px;
  background: #fff; color: #4a4236; cursor: pointer; text-align: left;
}
.tbtn:hover { border-color: #c3bbab; }
.tbtn:active { background: #f2eee6; }
.tbtn.style { font-family: var(--serif); }
.tbtn.primary { background: var(--penguin); border-color: var(--penguin); color: #fff; text-align: center; }
.tbtn.primary:hover { filter: brightness(0.95); border-color: var(--penguin); }
.drawer .tbtn { width: 100%; display: block; }

/* ── Contents nav + to-dos ───────────────────────────────────────────────── */
.nav-list, .todo-list { list-style: none; margin: 0; padding: 0; }
.nav-list { display: flex; flex-direction: column; gap: 2px; }
.nav-row { display: flex; align-items: baseline; gap: 8px; padding: 7px 8px; border-radius: 7px; cursor: pointer; font-size: 13px; }
.nav-row:hover { background: rgba(0,0,0,0.05); }
.nav-n { color: #8a7f6d; min-width: 1.3em; font-variant: small-caps; }
.nav-t { color: var(--ink); }
.nav-cover { font-variant: small-caps; letter-spacing: 0.04em; color: #6b6255; }
.todo-list { display: flex; flex-direction: column; gap: 4px; }
.todo-row { display: flex; align-items: center; gap: 8px; }
.todo-row input { accent-color: var(--penguin); width: 15px; height: 15px; flex: 0 0 auto; }
.todo-label { flex: 1; text-align: left; background: none; border: none; font: inherit; font-size: 13px; color: var(--ink); cursor: pointer; padding: 4px 2px; }
.todo-label:hover { color: var(--penguin); }
.todo-empty { font-size: 12px; color: #8a8172; font-style: italic; }

/* ── Stage / sheets ──────────────────────────────────────────────────────── */
.stage {
  overflow: auto;
  display: flex; flex-direction: column; align-items: center;
  gap: var(--gap);
  padding: 40px 24px 96px;
}
.sheet, .page {
  width: var(--pw); height: var(--ph);
  background: var(--paper);
  box-shadow: 0 3px 14px rgba(40, 30, 15, 0.18);
  border-radius: 2px;
  flex: 0 0 auto;
}

/* the editable body: sheets behind, transparent flow of type in front */
.book { position: relative; width: var(--pw); }
.sheets { position: absolute; top: 0; left: 0; width: var(--pw); pointer-events: none; }
.page { position: relative; margin-bottom: var(--gap); }
.run-head {
  position: absolute; left: 0; right: 0; top: 0.95cm; text-align: center;
  font-family: var(--serif); font-variant: small-caps; letter-spacing: 0.06em;
  font-size: 9pt; color: #6b6255;
}
.folio {
  position: absolute; left: 0; right: 0; bottom: 0.95cm; text-align: center;
  font-family: var(--serif); font-size: 10pt; color: #4a4236;
}

.flow {
  position: absolute; top: 0; left: 0; width: var(--pw); z-index: 2;
  padding: var(--mtop) var(--mx) var(--mbot);
  font-family: var(--serif); font-size: 11pt; line-height: 1.42; color: var(--ink);
  text-align: justify; hyphens: auto; -webkit-hyphens: auto;
  outline: none; background: transparent;
}
.flow > * { margin: 0; }
.pbspacer { pointer-events: none; }

.body { text-indent: 1.5em; }
.body.opening { text-indent: 0; }
.cont { text-indent: 0; }        /* a paragraph continued from the previous page */
.caret-mark { display: inline; }  /* zero-width caret anchor during pagination */
.body.opening::first-letter {
  float: left; font-family: var(--serif); font-weight: 600;
  font-size: 3.05em; line-height: 0.7; padding: 0.02em 0.09em 0 0;
}
.quote { font-style: italic; padding: 0.5em 1.5em; text-indent: 0; }

.chapter-title {
  text-align: center; text-indent: 0;
  font-family: var(--serif); font-weight: 500; font-size: 16pt;
  padding: 2.4em 0 1.7em;
}
.chapter-title::before {
  content: attr(data-label);
  display: block;
  font-family: var(--sans); font-weight: 600; font-size: 9pt;
  letter-spacing: 0.22em; text-transform: uppercase; color: var(--cover);
  margin-bottom: 1.1em;
}
sup.cite { font-size: 0.72em; color: var(--penguin); padding-left: 1px; cursor: default; }

/* TO-DO comment marker (like a Word comment; editorial only, never printed) */
.todo-mark {
  display: inline-block; vertical-align: baseline;
  background: var(--penguin); color: #fff;
  font-family: var(--sans); font-size: 8.5pt; font-weight: 600;
  letter-spacing: 0.08em; text-transform: uppercase;
  padding: 0 5px; border-radius: 4px; margin: 0 2px;
  text-indent: 0; cursor: default; user-select: none;
}

/* Flash when you jump to a chapter or to-do from the contents panel */
@keyframes flashbg { 0% { background: rgba(234,91,12,0.22); } 100% { background: transparent; } }
.flash { animation: flashbg 1.2s ease; }
.todo-mark.flash { animation: none; outline: 2px solid #111; outline-offset: 1px; }

/* ── Footnotes at the page foot ──────────────────────────────────────────── */
.footnotes {
  position: absolute; left: var(--mx); right: var(--mx); bottom: var(--mbot);
  font-family: var(--serif);
}
.fn-rule { width: 30%; height: 1px; background: #b8ae9a; margin-bottom: 5px; }
.fn-list { list-style: none; margin: 0; padding: 0; }
.fn-item { font-size: 9pt; line-height: 1.3; padding: 1px 0; text-indent: 0; text-align: left; }
.fn-item sup { color: var(--penguin); font-size: 0.7em; margin-right: 0.3em; }

/* ── Prelim sheets: contents + notes ─────────────────────────────────────── */
.prelim-inner {
  height: 100%; box-sizing: border-box;
  padding: var(--mtop) var(--mx) var(--mbot);
  font-family: var(--serif);
}
.prelim-title {
  text-align: center; font-variant: small-caps; letter-spacing: 0.08em;
  color: #6b6255; font-size: 10pt; margin-bottom: 2.4cm;
}
.prelim-head { text-align: center; font-weight: 500; font-size: 15pt; margin: 0 0 1.5em; }
.toc, .notes-list { list-style: none; padding: 0; margin: 0; font-size: 11pt; }
.toc-row { display: flex; align-items: baseline; gap: 0.4em; margin: 0.55em 0; }
.toc-n { font-variant: small-caps; letter-spacing: 0.04em; min-width: 1.7em; color: #8a7f6d; }
.toc-dots { flex: 1; border-bottom: 1px dotted #b8ae9a; transform: translateY(-0.25em); }
.toc-p { font-variant: small-caps; }
.toc-empty { color: #a99; font-style: italic; }
.notes-list li { margin: 0.5em 0; font-size: 9.5pt; line-height: 1.4; }
.note-n { color: var(--penguin); font-weight: 600; margin-right: 0.3em; }

/* ── Penguin cover ───────────────────────────────────────────────────────── */
.cover-sheet {
  overflow: hidden;
  display: flex; flex-direction: column;
  background: var(--cover);
  color: #111;
}
.pg-band { width: 100%; }
.pg-top { flex: 0 0 29%; }
.pg-mid {
  flex: 0 0 42%;
  background: #f4eee1;
  display: flex; flex-direction: column; justify-content: center;
}
.pg-rule { height: 2px; background: #111; }
.pg-panel {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center;
  padding: 0.4cm 0.8cm;
}
.pg-title {
  font-family: var(--sans); text-transform: uppercase; font-weight: 600;
  letter-spacing: 0.03em; font-size: 17pt; line-height: 1.12;
}
.pg-sub {
  font-family: var(--sans); font-style: italic; font-size: 11pt;
  margin-top: 0.3cm; color: #444;
}
.pg-divider { width: 1.7cm; height: 3px; background: var(--cover); margin: 0.6cm 0; }
.pg-author {
  font-family: var(--sans); text-transform: uppercase;
  letter-spacing: 0.14em; font-size: 12pt;
}
.pg-bot {
  flex: 1 1 auto;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.35cm; color: var(--cover-ink);
}
.pg-oval {
  width: 2.4cm; height: 3.05cm; background: #fff; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; color: #111;
  overflow: hidden;
}
.pg-badger { height: 88%; width: auto; max-width: 84%; object-fit: contain; }
.pg-imprint {
  font-family: var(--sans); text-transform: uppercase;
  letter-spacing: 0.2em; font-size: 8.5pt;
}

@media (max-width: 720px) {
  body { grid-template-columns: 1fr; grid-template-areas: "toolbar" "stage"; }
  .panel { display: none; }
}

/* ── Print / Save-as-PDF: one physical page per .print-page ──────────────── */
#print-root { display: none; }

@media print {
  @page { size: 13cm 19.5cm; margin: 0; }
  html, body { background: #fff; height: auto; display: block; }
  .rail, .drawer, .stage { display: none !important; }
  #print-root { display: block; }

  .print-page {
    position: relative;
    width: 13cm; height: 19.5cm; overflow: hidden;
    background: #fff;
    break-after: page; page-break-after: always;
  }
  .print-page:last-child { break-after: auto; page-break-after: auto; }
  .print-page.cover-sheet { display: flex; flex-direction: column; background: var(--cover); }

  .print-inner { position: absolute; inset: 0; }
  .print-flow {
    position: absolute; top: var(--mtop); left: var(--mx); right: var(--mx); bottom: var(--mbot);
    font-family: var(--serif); font-size: 11pt; line-height: 1.42; color: var(--ink);
    text-align: justify; hyphens: auto; -webkit-hyphens: auto;
  }
  .print-flow > * { margin: 0; }

  /* to-do comments are editorial only — never print them */
  .todo-mark { display: none !important; }

  /* make sure the orange cover + coloured marks actually ink */
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
