:root {
  --bg: #ffffff;
  --panel: #f5f5f3;
  --fg: #1d1d1b;
  --muted: #6b6a66;
  --line: #dddcd7;
  --accent: #9a3b1b;
  --hl: #fff1a8;
  --hl-edge: #e0b400;
  --ui-font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --script-font: "Atkinson Hyperlegible", system-ui, sans-serif;
  --script-size: 19px;
  --who-w: 7em;
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #f6eedb;
  --panel: #ede2c8;
  --fg: #2e261b;
  --muted: #7a6a52;
  --line: #d9caa8;
  --accent: #8a3214;
  --hl: #f5d77a;
  --hl-edge: #b98a00;
}
:root[data-theme="dark"] {
  --bg: #17171a;
  --panel: #202024;
  --fg: #e8e6e1;
  --muted: #9c9a94;
  --line: #34343a;
  --accent: #f0a37f;
  --hl: #4a3f12;
  --hl-edge: #d6b13a;
  color-scheme: dark;
}
:root[data-mono="1"] { --who-w: 9ch; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--ui-font);
  font-size: 16px;
  line-height: 1.45;
}
button, select, input { font: inherit; color: inherit; }

/* ---- layout ---- */
.layout {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  min-height: 100vh;
}
.panel {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 20px 20px 32px;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.panel h1 { font-size: 20px; margin: 0 0 16px; }
.play-note { color: var(--muted); font-size: 13.5px; margin: -6px 0 16px; }
.field { display: block; border: 0; margin: 0 0 18px; padding: 0; min-width: 0; }
.field > span, .field > legend { display: block; font-weight: 600; font-size: 14px; margin-bottom: 6px; padding: 0; }
.field output { font-weight: 400; color: var(--muted); }
select, input[type="range"] { width: 100%; }
select {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
}
input[type="range"] { accent-color: var(--accent); }
.radio { display: block; padding: 3px 0; }
.radio small { color: var(--muted); display: block; margin-left: 1.6em; font-size: 12.5px; }
input[type="radio"] { accent-color: var(--accent); }

.fonts { display: grid; gap: 6px; }
.font-card {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  cursor: pointer;
}
.font-card:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.font-card input { grid-row: span 2; align-self: center; }
.font-name { font-weight: 700; font-size: 15px; }
.font-sample { font-size: 13.5px; color: var(--muted); }

.seg { display: flex; gap: 6px; }
.seg label {
  flex: 1;
  text-align: center;
  padding: 6px 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  cursor: pointer;
  font-size: 14px;
}
.seg label:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

.actions { display: flex; gap: 8px; margin-top: 4px; }
.actions button {
  flex: 1;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  cursor: pointer;
  font-weight: 600;
}
.actions .primary { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="dark"] .actions .primary { color: #17171a; }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 10px; }

/* ---- script ---- */
.script {
  padding: 24px 40px 120px;
  font-family: var(--script-font);
  font-size: var(--script-size);
  line-height: 1.5;
  outline: none;
}
.script > * { max-width: 46em; margin-inline: auto; }
:root[data-mono="1"] .script > * { max-width: 72ch; }
.loading, .error { color: var(--muted); font-family: var(--ui-font); }
.error { color: var(--accent); }

.page { padding: 8px 0 28px; border-bottom: 1px dashed var(--line); }
.pnum { text-align: center; color: var(--muted); font-size: 0.8em; margin: 8px 0 12px; }
.missing-note { text-align: center; color: var(--muted); font-style: italic; padding: 2em 0; }

.speech {
  display: grid;
  grid-template-columns: var(--who-w) minmax(0, 1fr);
  gap: 0 1em;
  margin: 0.75em 0;
  border-radius: 6px;
}
.speech.cont { margin-top: 0.2em; }
.who { font-weight: 700; overflow-wrap: anywhere; }
.say p { margin: 0; }
.say p + p { margin-top: 0.2em; }
.stage { color: var(--muted); padding-left: 1.5em; }
.verse { padding-left: 2.5em; margin-block: 0.35em !important; }
.scene { margin: 1em 0; }
.center { text-align: center; margin: 1em 0; letter-spacing: 0.03em; }
.gap { height: 0.4em; }

:root[data-layout="flow"] .stage { font-style: italic; }
:root[data-layout="original"] .say p,
:root[data-layout="original"] .scene { white-space: pre-wrap; }
:root[data-layout="original"] .stage { padding-left: 2.5em; }

.speech.mine .say {
  background: var(--hl);
  border-left: 3px solid var(--hl-edge);
  border-radius: 3px;
  padding: 0.1em 0.5em;
  margin-left: calc(-0.5em - 3px);
}
.speech.mine .who { color: var(--accent); }
.speech.mine .stage { color: inherit; opacity: 0.75; }
.speech.current { outline: 2px solid var(--hl-edge); outline-offset: 6px; }

/* ---- floating navigation for the chosen role / reading mode ---- */
.lines-nav {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  font-family: var(--ui-font);
}
.lines-nav[hidden] { display: none; }
.lines-nav button {
  min-width: 40px;
  height: 40px;
  border-radius: 999px;
  border: 0;
  background: transparent;
  cursor: pointer;
  font-size: 15px;
}
.lines-nav button:hover { background: var(--line); }
.lines-nav span { min-width: 4.5em; text-align: center; font-size: 14px; color: var(--muted); }
.lines-nav .exit, .lines-nav .read-only { display: none; }
.lines-nav.no-role #prev, .lines-nav.no-role #next, .lines-nav.no-role span { display: none; }

body.reading .layout { grid-template-columns: minmax(0, 1fr); }
body.reading .panel { display: none; }
body.reading .script { padding-top: 32px; }
body.reading .lines-nav .exit,
body.reading .lines-nav .read-only { display: inline-block; }

/* ---- narrow screens ---- */
@media (max-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .panel { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .script { padding: 16px 16px 120px; }
}
@media (max-width: 560px) {
  .speech { display: block; }
  .who { font-size: 0.85em; letter-spacing: 0.02em; }
  .stage { padding-left: 1em; }
  :root[data-layout="original"] .stage { padding-left: 1.5em; }
}

/* ---- shared bits for the library, upload and editor pages ---- */
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--accent); font-weight: 600; }
.crumbs { display: flex; gap: 16px; flex-wrap: wrap; font-size: 14px; margin-bottom: 10px; }
.crumbs a { text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }
input:not([type]), input[type="text"], input[type="password"], textarea {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
}
button, .button {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  cursor: pointer;
  font-weight: 600;
  font-size: 15px;
  text-decoration: none;
  color: var(--fg);
}
button:disabled { opacity: 0.5; cursor: default; }
.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="dark"] .primary { color: #17171a; }
button.link { border: 0; background: none; padding: 0; font: inherit; color: var(--accent); text-align: left; cursor: pointer; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 16px;
  padding: 14px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 20px; margin: 0; }
.topbar .crumbs { margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.wrap { max-width: 760px; margin: 0 auto; padding: 20px 16px 60px; }
.wrap.wide { max-width: 1400px; }
.stack > * + * { margin-top: 12px; }

.play-list { list-style: none; padding: 0; margin: 16px 0; display: grid; gap: 10px; }
.play-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
}
.play-main { flex: 1; min-width: 0; text-decoration: none; color: inherit; display: grid; gap: 2px; }
.play-main:hover .play-title { color: var(--accent); }
.play-title { font-weight: 700; font-size: 17px; }
.play-card .play-note { margin: 0; }
.play-meta { font-size: 12.5px; color: var(--muted); }
.badge { font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--hl); color: var(--fg); vertical-align: 2px; }

dialog.login { border: 1px solid var(--line); border-radius: 12px; background: var(--bg); color: var(--fg); padding: 20px; width: min(400px, calc(100vw - 32px)); }
dialog.login h2 { margin: 0 0 8px; font-size: 18px; }
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }

.dropzone {
  display: block;
  padding: 28px 16px;
  border: 2px dashed var(--line);
  border-radius: 10px;
  text-align: center;
  cursor: pointer;
  background: var(--panel);
}
.dropzone.over { border-color: var(--accent); }
.dropzone input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.dropzone:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.thumbs { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.thumb { position: relative; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--panel); font-size: 12px; }
.thumb img { display: block; width: 100%; aspect-ratio: 3 / 4; object-fit: cover; }
.thumb-num { position: absolute; top: 4px; left: 4px; background: var(--accent); color: #fff; border-radius: 999px; padding: 0 7px; font-weight: 700; }
.thumb-name { display: block; padding: 4px 6px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.thumb-tools { display: flex; justify-content: space-between; padding: 4px; }
.thumb-tools button { padding: 2px 8px; font-size: 12px; }
.progress progress { width: 100%; accent-color: var(--accent); }
form.busy .dropzone, form.busy .thumb-tools { pointer-events: none; opacity: 0.6; }

.meta-row { display: flex; gap: 12px; align-items: end; flex-wrap: wrap; }
.meta-row .grow { flex: 1 1 260px; }
.field.toggle { display: flex; align-items: center; gap: 8px; padding-bottom: 8px; }
.field.toggle span { margin: 0; }
.field.toggle input { accent-color: var(--accent); }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
.notice { background: var(--hl); border-radius: 8px; padding: 8px 12px; font-size: 14px; }
button.quiet { border-color: transparent; background: transparent; color: var(--muted); }
button.quiet:hover { color: var(--accent); }

/* login dialog */
dialog.login .field { margin-bottom: 12px; }
.pw-field { position: relative; display: block; }
.pw-field input { padding-right: 44px; }
.pw-field input[aria-invalid] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.pw-field .eye {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  display: grid; place-items: center; width: 36px; height: 32px; padding: 0;
  border: 0; background: transparent; color: var(--muted);
}
.pw-field .eye:hover, .pw-field .eye[aria-pressed="true"] { color: var(--accent); }
dialog.login .check { margin: 2px 0 4px; font-size: 14.5px; }
dialog.login .error { margin: 10px 0 0; font-size: 14px; }
dialog.login .actions { margin-top: 16px; }

/* editor */
.review-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  margin: 4px 0 12px; padding: 10px 14px; border-radius: 10px;
  background: var(--hl); border: 1px solid var(--hl-edge);
}
.review-bar span { flex: 1; font-weight: 600; }
details.checks { margin: 0 0 14px; font-size: 14px; }
details.checks summary { cursor: pointer; }
details.checks ul { margin: 6px 0 10px; padding-left: 18px; max-height: 220px; overflow-y: auto; }

.editor {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr) minmax(0, 1.1fr);
  gap: 16px;
  align-items: start;
}
.editor.no-photos { grid-template-columns: 88px minmax(0, 1fr); }
.editor.busy { opacity: 0.5; pointer-events: none; }
.page-list { position: sticky; top: 12px; display: grid; gap: 8px; }
.page-list ol {
  list-style: none; margin: 0; padding: 0;
  display: grid; gap: 4px; max-height: calc(100vh - 140px); overflow-y: auto;
}
.page-list ol button {
  position: relative; width: 100%; padding: 6px 4px; font-size: 14px; text-align: center;
  display: flex; justify-content: center; align-items: center; gap: 4px;
}
.page-list ol button.current { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); color: var(--accent); }
.page-list ol button.missing { border-style: dashed; color: var(--muted); font-weight: 400; }
.page-list ol button.failed { border-color: var(--accent); }
.page-list .count {
  min-width: 18px; padding: 0 5px; border-radius: 999px; font-size: 11px; line-height: 18px;
  background: var(--hl); color: var(--fg); border: 1px solid var(--hl-edge);
}
.page-list .count.bad { background: var(--accent); border-color: var(--accent); color: #fff; }
.page-list-tools { display: grid; gap: 4px; }
.page-list-tools > * { padding: 6px 4px; font-size: 13px; text-align: center; }
.page-list-tools .busy { opacity: 0.6; pointer-events: none; }

.page-photo { position: sticky; top: 12px; }
.photo-frame {
  max-height: calc(100vh - 80px); overflow: auto; cursor: zoom-in;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
}
.photo-frame img { display: block; width: 100%; height: auto; }
.photo-frame.zoomed { cursor: zoom-out; }
.photo-frame.zoomed img { width: 220%; max-width: none; }
.photo-empty { padding: 48px 16px; text-align: center; margin: 0; }
.photo-pick { display: inline-flex; align-items: center; gap: 6px; margin-top: 6px; }
.photo-pick select { width: auto; padding: 2px 6px; }

.page-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.page-head .grow { flex: 1; }
.page-head > button { padding: 6px 10px; font-size: 14px; }
.page-num { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; }
.page-num input { width: 5.5em; padding: 5px 8px; font-weight: 700; text-align: center; }
.page-head .check { font-size: 14px; color: var(--muted); }
.dup-note { margin-bottom: 8px; padding: 8px 12px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); font-size: 14px; }
.missing-hint { padding: 24px; border: 1px dashed var(--line); border-radius: 8px; text-align: center; }

/* textarea over a layer that paints the {uncertain} words; both must wrap identically */
.hl-wrap { position: relative; }
.hl-back, .hl-wrap textarea {
  margin: 0; padding: 12px 14px; border: 1px solid var(--line); border-radius: 8px;
  font-family: "Courier Prime", ui-monospace, monospace; font-size: 15px; line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: break-word; word-break: normal; tab-size: 4; letter-spacing: 0;
}
.hl-back { position: absolute; inset: 0; color: transparent; border-color: transparent; pointer-events: none; overflow: hidden; }
.hl-back mark { color: transparent; background: var(--hl); border-radius: 3px; box-shadow: 0 0 0 1px var(--hl-edge); }
.hl-wrap textarea {
  position: relative; display: block; width: 100%; min-height: 320px;
  background: transparent; color: var(--fg); resize: none; overflow: hidden;
}
.hl-wrap { background: var(--bg); border-radius: 8px; }
.help { font-size: 14px; margin-top: 8px; }
.help td { padding: 2px 10px 2px 0; vertical-align: top; }
.danger { margin-top: 32px; }
.danger button { color: var(--accent); border-color: var(--accent); background: transparent; }

/* review of uncertain words */
dialog.review {
  width: min(1100px, calc(100vw - 24px)); max-height: calc(100vh - 24px); padding: 0;
  border: 1px solid var(--line); border-radius: 12px; background: var(--bg); color: var(--fg);
}
.review-grid { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(300px, 1fr); min-height: min(640px, calc(100vh - 26px)); }
.review-photo { position: relative; overflow: auto; background: var(--panel); border-right: 1px solid var(--line); max-height: calc(100vh - 26px); }
.review-photo img { display: block; width: 180%; max-width: none; }
.review-band { position: absolute; left: 0; width: 180%; background: rgba(224, 180, 0, 0.18); border-block: 2px solid var(--hl-edge); pointer-events: none; }
.review-card { padding: 18px 20px; display: flex; flex-direction: column; gap: 10px; }
.review-top { display: flex; align-items: center; justify-content: space-between; }
.review-top h2 { margin: 0; font-size: 18px; }
.review-context {
  font-family: "Courier Prime", ui-monospace, monospace; font-size: 15px; line-height: 1.6;
  padding: 10px 12px; border-radius: 8px; background: var(--panel); overflow-wrap: anywhere;
}
.ctx-line { color: var(--muted); }
.ctx-line.main { color: var(--fg); }
.review-context mark { background: var(--hl); color: var(--fg); padding: 0 2px; border-radius: 3px; box-shadow: 0 0 0 2px var(--hl-edge); }
#review-fix { font-size: 18px; font-family: "Courier Prime", ui-monospace, monospace; }
.review-alts { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip { padding: 4px 10px; border-radius: 999px; font-family: "Courier Prime", ui-monospace, monospace; font-weight: 400; }
.review-card .actions { margin-top: 6px; }
#review-item, #review-done { display: flex; flex-direction: column; gap: 10px; }
#review-item[hidden], #review-done[hidden] { display: none; }

@media (max-width: 1000px) {
  .editor, .editor.no-photos { grid-template-columns: minmax(0, 1fr); }
  .page-list { position: static; }
  .page-list ol { display: flex; overflow-x: auto; max-height: none; padding-bottom: 4px; }
  .page-list ol button { min-width: 48px; }
  .page-list-tools { display: flex; }
  .page-photo { position: static; }
  .photo-frame { max-height: 60vh; }
  .review-grid { grid-template-columns: minmax(0, 1fr); }
  .review-photo { max-height: 42vh; border-right: 0; border-bottom: 1px solid var(--line); }
}

/* ---- PDF export (print) ----
   The text runs on from one original page to the next so every sheet is full,
   at the size chosen on screen (1 CSS px = 0.75 pt, so the default 19 px is ~14 pt). */
@page {
  size: A4;
  margin: 14mm 15mm 16mm;
  @bottom-center { content: counter(page) " / " counter(pages); font-family: system-ui, sans-serif; font-size: 9pt; color: #666; }
}
@media print {
  :root, :root[data-theme] {
    --bg: #fff; --fg: #000; --muted: #444; --line: #ccc; --accent: #000;
    --hl: #fff1a8; --hl-edge: #c99a00;
  }
  body { background: #fff; }
  .layout { display: block; }
  .panel, .lines-nav { display: none !important; }
  .script { padding: 0; font-size: var(--script-size); line-height: 1.4; orphans: 2; widows: 2; }
  .script > * { max-width: none; }
  .page { border: 0; padding: 0; }
  /* original page numbers stay as small markers in the margin of the flow */
  .pnum { font-size: 0.65em; margin: 0.6em 0 0.2em; text-align: right; }
  .missing-note { padding: 0.4em 0; }
  .speech { margin: 0.5em 0; }
  .speech.current { outline: none; }
  .speech.mine .say { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .speech.mine .who { text-decoration: underline; }
}
