:root {
  --ink: #232120;
  --ink-soft: #55504c;
  --paper: #ece7dd;
  --paper-raised: #f7f4ec;
  --rule: #c9c0b1;
  --brass: #8a6a3f;
  --brass-dark: #6d5330;
  --moss: #4c6650;
  --error: #a3402f;
  --warn: #a3402f;

  --serif: "Source Serif 4", Georgia, serif;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
  --mono: "IBM Plex Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  overflow: hidden; /* the page itself never scrolls — panes below do, independently */
}

.masthead {
  flex-shrink: 0;
  padding: 2.75rem 2rem 1.75rem;
  border-bottom: 1px solid var(--rule);
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

.masthead-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.masthead h1 {
  font-family: var(--serif);
  font-weight: 700;
  font-size: 2.1rem;
  margin: 0 0 0.4rem;
  letter-spacing: -0.01em;
}

.page-nav {
  display: flex;
  gap: 0.4rem;
}

.page-nav-link {
  font-family: var(--sans);
  font-weight: 600;
  font-size: 0.88rem;
  text-decoration: none;
  color: var(--ink-soft);
  padding: 0.4rem 0.85rem;
  border-radius: 3px;
  border: 1px solid transparent;
}

.page-nav-link:hover {
  color: var(--ink);
  border-color: var(--rule);
}

.page-nav-link.is-active {
  color: var(--brass-dark);
  border-color: var(--brass);
  background: var(--paper-raised);
}

.tagline {
  color: var(--ink-soft);
  margin: 0;
  max-width: 46em;
  font-size: 0.98rem;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  grid-template-rows: 1fr;
  gap: 0;
  max-width: 1320px;
  width: 100%;
  margin: 0 auto;
  align-items: stretch;
  flex: 1 1 auto;
  min-height: 0;
}

.pane {
  padding: 2rem;
  overflow-y: auto;
  min-height: 0;
}

.pane--library {
  border-right: 1px solid var(--rule);
}

.block + .block {
  margin-top: 2.25rem;
  padding-top: 2rem;
  border-top: 1px dashed var(--rule);
}

h2 {
  font-family: var(--serif);
  font-size: 1.3rem;
  font-weight: 600;
  margin: 0 0 0.35rem;
}

h3 {
  font-family: var(--serif);
  font-size: 1.05rem;
  font-weight: 600;
  margin: 0 0 0.5rem;
}

.hint {
  color: var(--ink-soft);
  font-size: 0.87rem;
  margin: 0 0 0.75rem;
}

textarea, input[type="text"], select {
  width: 100%;
  font-family: var(--sans);
  font-size: 0.95rem;
  color: var(--ink);
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: 3px;
  padding: 0.6rem 0.7rem;
  resize: vertical;
}

textarea:focus, input:focus, select:focus {
  outline: 2px solid var(--brass);
  outline-offset: 1px;
}

.btn {
  display: inline-block;
  font-family: var(--sans);
  font-weight: 600;
  font-size: 0.88rem;
  padding: 0.55rem 1.1rem;
  border-radius: 3px;
  border: 1px solid var(--brass-dark);
  background: var(--brass);
  color: #fff8ee;
  cursor: pointer;
  margin-top: 0.6rem;
}

.btn:hover { background: var(--brass-dark); }

.btn--quiet {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--rule);
}

.btn--quiet:hover {
  background: var(--paper-raised);
  border-color: var(--ink-soft);
}

.btn--primary {
  font-size: 0.95rem;
  padding: 0.7rem 1.4rem;
  background: var(--moss);
  border-color: #33452f;
}

.btn--primary:hover { background: #3a4f3d; }

.status {
  display: inline-block;
  margin-left: 0.6rem;
  font-size: 0.85rem;
  color: var(--moss);
}

.status.error { color: var(--error); }

.file-btn { position: relative; }

.import-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.4rem;
}

.import-order-row {
  margin: 0.6rem 0;
}

.import-order-row label {
  display: block;
  font-size: 0.8rem;
  color: var(--ink-soft);
  font-weight: 600;
  margin-bottom: 0.3rem;
}

.import-order-row select {
  margin-bottom: 0.3rem;
}

/* Chapter pool list */
.chapter-list {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
  border-top: 1px solid var(--rule);
}

.chapter-list li {
  border-bottom: 1px solid var(--rule);
  padding: 0.75rem 0.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.chapter-list .chapter-text {
  display: block;
}

.chapter-list .chapter-text strong {
  font-family: var(--serif);
  font-size: 1rem;
  display: block;
}

.chapter-list .chapter-text span {
  color: var(--ink-soft);
  font-size: 0.85rem;
}

.ref-id-tag {
  display: inline-block;
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--brass-dark);
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: 3px;
  padding: 0.1rem 0.4rem;
  margin: 0.2rem 0;
}

.chapter-list .chapter-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-top: 0.4rem;
  border-top: 1px dashed var(--rule);
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--ink-soft);
  font-family: var(--mono);
  font-size: 0.8rem;
  padding: 0.15rem 0.35rem;
}

.icon-btn:hover { color: var(--error); }

.chapter-list--scrollable {
  max-height: 58vh;
  overflow-y: auto;
  border: 1px solid var(--rule);
  border-radius: 3px;
  padding: 0 0.7rem;
}

.filter-row {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  margin: 0.75rem 0 0.75rem;
}

.filter-btn {
  font-family: var(--sans);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--rule);
  background: #fff;
  color: var(--ink-soft);
  cursor: pointer;
}

.filter-btn:hover {
  border-color: var(--ink-soft);
  color: var(--ink);
}

.filter-btn.is-active {
  background: var(--brass);
  border-color: var(--brass-dark);
  color: #fff8ee;
}

.add-chapter { margin-top: 1.1rem; }

.add-chapter summary,
.new-order-form summary {
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--brass-dark);
  font-weight: 600;
}

.add-chapter-form {
  margin-top: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* Order header */
.order-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 1rem;
  flex-wrap: wrap;
}

.order-picker { flex: 1; min-width: 220px; }

.order-picker label {
  display: block;
  font-size: 0.8rem;
  color: var(--ink-soft);
  margin-bottom: 0.3rem;
  font-weight: 600;
}

.order-actions {
  display: flex;
  gap: 0.5rem;
}

.order-actions .btn { margin-top: 0; }

.new-order-form { margin: 1rem 0 1.5rem; }

.new-order-inline {
  margin-top: 0.6rem;
  display: flex;
  gap: 0.6rem;
}

.new-order-inline input { flex: 1; }

.copy-settings-row {
  margin-top: 0.75rem;
}

.copy-settings-row label {
  display: block;
  font-size: 0.8rem;
  color: var(--ink-soft);
  font-weight: 600;
  margin-bottom: 0.3rem;
}

.copy-settings-row select {
  margin-bottom: 0.4rem;
}

/* Sequence list */
.add-to-order-row {
  display: flex;
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.add-to-order-row select { flex: 1; }
.add-to-order-row .btn { margin-top: 0; }

.sequence-list {
  list-style: none;
  margin: 0;
  padding: 0;
  counter-reset: none;
}

.sequence-item {
  border: 1px solid var(--rule);
  border-radius: 3px;
  margin-bottom: 1rem;
  background: var(--paper-raised);
  overflow: hidden;
}

.sequence-item-head {
  display: flex;
  align-items: flex-start;
  gap: 0.85rem;
  padding: 0.85rem 1rem;
}

.sequence-position {
  font-family: var(--mono);
  font-size: 0.85rem;
  color: var(--brass-dark);
  background: #fff;
  border: 1px solid var(--rule);
  border-radius: 50%;
  width: 1.7rem;
  height: 1.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.sequence-title { flex: 1; }

.sequence-headline {
  font-family: var(--serif);
  font-size: 1.05rem;
  display: block;
}

.sequence-summary {
  color: var(--ink-soft);
  font-size: 0.85rem;
}

.sequence-controls {
  display: flex;
  gap: 0.3rem;
  flex-shrink: 0;
}

.sequence-controls .icon-btn { color: var(--ink-soft); }
.sequence-controls .icon-btn:hover { color: var(--brass-dark); }
.sequence-controls .icon-btn[data-action="remove"]:hover { color: var(--error); }

.stale-badge {
  background: #f4e3d8;
  color: var(--warn);
  font-size: 0.8rem;
  padding: 0.5rem 1rem;
  border-top: 1px dashed var(--rule);
}

.sequence-body {
  padding: 0 1rem 1rem;
  border-top: 1px solid var(--rule);
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.field-row { margin-top: 0.7rem; }

.field-row label {
  display: block;
  font-size: 0.78rem;
  color: var(--ink-soft);
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.field-row textarea {
  font-family: var(--serif);
  font-size: 0.95rem;
  background: #fff;
}

.meta-line {
  font-size: 0.78rem;
  color: var(--ink-soft);
  display: flex;
  gap: 0.9rem;
  flex-wrap: wrap;
}

.meta-line .meta-label {
  font-weight: 600;
  color: var(--brass-dark);
}

.sequence-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.sequence-actions .btn { margin-top: 0.3rem; font-size: 0.82rem; padding: 0.4rem 0.8rem; }

.prompt-inline, .history-inline {
  margin-top: 0.4rem;
  border: 1px dashed var(--rule);
  border-radius: 3px;
  padding: 0.75rem;
  background: #fff;
}

.prompt-inline label {
  font-size: 0.75rem;
  color: var(--ink-soft);
  font-weight: 600;
  display: block;
  margin-top: 0.5rem;
}

.prompt-inline textarea, .history-inline textarea {
  font-family: var(--mono);
  font-size: 0.8rem;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.history-list li {
  border-bottom: 1px dashed var(--rule);
  padding-bottom: 0.6rem;
  font-size: 0.85rem;
}

.history-list .history-meta {
  color: var(--ink-soft);
  font-size: 0.78rem;
  margin-bottom: 0.3rem;
}

.history-list .history-text {
  white-space: pre-wrap;
}

/* Generation controls */
.gen-row {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.gen-field { flex: 1; min-width: 200px; margin-bottom: 0.7rem; }

.gen-field label {
  display: block;
  font-size: 0.8rem;
  color: var(--ink-soft);
  margin-bottom: 0.3rem;
  font-weight: 600;
}

/* Write page */
.write-chapter-list li {
  cursor: pointer;
  align-items: center;
}

.write-chapter-list li:hover {
  background: var(--paper-raised);
}

.bulk-generate-row {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px dashed var(--rule);
}

.bulk-generate-row .checkbox-label {
  margin-bottom: 0.5rem;
}

.write-chapter-list li.is-selected {
  background: var(--paper-raised);
  border-left: 3px solid var(--brass);
  padding-left: calc(0.1rem - 3px + 3px);
}

.draft-badge {
  font-family: var(--mono);
  font-size: 0.7rem;
  padding: 0.15rem 0.4rem;
  border-radius: 3px;
  flex-shrink: 0;
  align-self: center;
}

.draft-badge.has-draft {
  color: var(--moss);
  border: 1px solid var(--moss);
}

.draft-badge.no-draft {
  color: var(--ink-soft);
  border: 1px solid var(--rule);
}

.chapter-editor-head {
  margin-bottom: 1.5rem;
}

.chapter-editor-head h2 {
  font-size: 1.6rem;
}

#editor-reference-info {
  margin-top: 0.5rem;
}

.extra-fields-list {
  margin: 0.5rem 0 0;
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.15rem 0.75rem;
  font-size: 0.85rem;
}

.extra-fields-list dt {
  color: var(--ink-soft);
  font-weight: 600;
}

.extra-fields-list dd {
  margin: 0;
}

.section-head-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

#content-editor, #adapted-content-editor {
  font-family: var(--serif);
  font-size: 0.98rem;
  line-height: 1.6;
  background: #fff;
}

.sections-editor {
  list-style: none;
  margin: 0 0 0.75rem;
  padding: 0;
}

.section-card {
  border: 1px solid var(--rule);
  border-radius: 3px;
  background: var(--paper-raised);
  padding: 0.9rem 1rem 1.1rem;
  margin-bottom: 1rem;
}

.section-card-head {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  margin-bottom: 0.7rem;
}

.section-position {
  font-family: var(--mono);
  font-size: 0.85rem;
  color: var(--brass-dark);
  background: #fff;
  border: 1px solid var(--rule);
  border-radius: 50%;
  width: 1.7rem;
  height: 1.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.section-title-input {
  flex: 1;
  font-family: var(--serif);
  font-size: 1.05rem;
  font-weight: 600;
  background: #fff;
}

.section-controls {
  display: flex;
  gap: 0.2rem;
  flex-shrink: 0;
}

.section-card label {
  display: block;
  font-size: 0.78rem;
  color: var(--ink-soft);
  font-weight: 600;
  margin: 0.6rem 0 0.25rem;
}

.optional-tag {
  font-weight: 400;
  color: var(--ink-soft);
  font-style: italic;
}

.section-card textarea, .section-card input[type="text"] {
  background: #fff;
  font-family: var(--serif);
  font-size: 0.95rem;
}

.quiz-toggle-row { margin-top: 0.7rem; }

.checkbox-label {
  display: flex !important;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem !important;
  font-weight: 600 !important;
  color: var(--ink) !important;
  cursor: pointer;
}

.quiz-block {
  margin-top: 0.6rem;
  padding: 0.75rem 0.85rem;
  border: 1px dashed var(--rule);
  border-radius: 3px;
  background: #fff;
}

.quiz-options-list {
  list-style: none;
  margin: 0 0 0.5rem;
  padding: 0;
}

.quiz-option-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}

.quiz-option-row .option-text-input {
  flex: 1;
  font-family: var(--sans);
  font-size: 0.9rem;
  margin: 0;
}

.quiz-option-row .option-correct-radio {
  flex-shrink: 0;
  accent-color: var(--moss);
}

.add-option-btn {
  font-size: 0.8rem;
  padding: 0.35rem 0.7rem;
}

.editor-actions {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 0.6rem;
}

.editor-actions .btn { margin-top: 0; }

.refine-row {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.9rem;
}

.refine-row input { flex: 1; }
.refine-row .btn { margin-top: 0; }

.prompt-panel {
  margin-top: 1rem;
  border: 1px dashed var(--rule);
  border-radius: 3px;
  padding: 0.85rem 1rem;
  background: var(--paper-raised);
}

.prompt-panel label {
  font-size: 0.78rem;
  color: var(--ink-soft);
  font-weight: 600;
  display: block;
  margin-top: 0.5rem;
}

.prompt-panel textarea {
  font-family: var(--mono);
  font-size: 0.8rem;
  background: #fff;
}

#adapted-panel {
  margin-top: 1.2rem;
}

.layout--single {
  grid-template-columns: 1fr;
  max-width: 760px;
}

.pane--settings label {
  display: block;
  font-size: 0.8rem;
  color: var(--ink-soft);
  font-weight: 600;
  margin: 0.9rem 0 0.3rem;
}

.pane--settings label:first-of-type {
  margin-top: 0;
}

.pane--settings {
  border-right: none;
}

.project-current {
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
}

.project-current strong {
  font-family: var(--serif);
  color: var(--brass-dark);
}

.quick-ref-block {
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: 3px;
  padding: 1.25rem 1.5rem;
}

.quick-ref-table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  font-size: 0.88rem;
}

.quick-ref-table th, .quick-ref-table td {
  border: 1px solid var(--rule);
  padding: 0.55rem 0.7rem;
  text-align: left;
  vertical-align: top;
}

.quick-ref-table th {
  background: var(--paper-raised);
  font-family: var(--sans);
  font-weight: 600;
  color: var(--brass-dark);
}

.quick-ref-table td {
  background: #fff;
}

.hint--example {
  font-family: var(--serif);
  font-style: italic;
  background: var(--paper-raised);
  border-left: 3px solid var(--brass);
  padding: 0.75rem 1rem;
  margin: 0.5rem 0 1rem;
}

@media (max-width: 900px) {
  /* Side-by-side independent scroll only makes sense with two columns.
     Below this width panes stack vertically, so fall back to one normal
     scrolling page instead of fighting over a fixed height budget. */
  body { overflow: auto; height: auto; }
  .layout { flex: none; height: auto; min-height: 0; grid-template-rows: none; grid-template-columns: 1fr; }
  .pane { overflow-y: visible; height: auto; }
  .pane--library { border-right: none; border-bottom: 1px solid var(--rule); }
  .chapter-list--scrollable { max-height: 40vh; }
}
