:root {
  --bg: #ecebe6;
  --panel: #ffffff;
  --field: #f6f5f2;
  --ink: #1e1b18;
  --muted: #6a635b;
  --rule: #ddd8cf;
  --rule-strong: #c9c2b6;
  --accent: #8b4513;        /* the slide counter brown */
  --accent-strong: #6f360f;
  --accent-ink: #ffffff;
  --danger: #a3271b;
  --shadow: 0 1px 2px rgb(30 27 24 / 0.08), 0 6px 18px rgb(30 27 24 / 0.08);

  /* the hymn board stays dark wood in both themes */
  --wood: #2a1e17;
  --wood-ink: #f3eee7;
  --wood-muted: #cdbfb0;
  --board: #1b130e;
  --board-edge: #4a3a2e;
  --slot: #0f0b08;
  --slot-edge: #3a2d23;
  --slot-ink: #f7f3ec;
  --slot-label: #a8988a;

  --slide: #ffffff;
  --slide-ink: #000000;
  --slide-link: #0000ff;  /* the default theme's hyperlink colour */

  --display: "Spectral", Georgia, serif;
  --body: "Public Sans", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151311;
    --panel: #1f1c19;
    --field: #171513;
    --ink: #ece7e0;
    --muted: #a39a8f;
    --rule: #3a352f;
    --rule-strong: #514a42;
    --accent: #d68b55;
    --accent-strong: #e8a575;
    --accent-ink: #1a0f07;
    --danger: #f08a7c;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 6px 18px rgb(0 0 0 / 0.35);
    --wood: #241912;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.5 var(--body);
  -webkit-font-smoothing: antialiased;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
code { font-family: var(--mono); font-size: 0.9em; }

/* --- hymn board header ---------------------------------------------- */

.masthead { background: var(--wood); color: var(--wood-ink); }
.masthead-inner {
  max-width: 1392px; margin: 0 auto;
  padding: 32px 24px 28px;
  display: flex; align-items: flex-end; justify-content: space-between; gap: 24px 32px;
}
.brand { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.brand h1 { margin: 0; font: 600 40px/1.05 var(--display); letter-spacing: -0.01em; }
.lede { margin: 0; color: var(--wood-muted); }
.engine { display: flex; align-items: center; gap: 8px; margin: 10px 0 0; font-size: 13px; color: var(--wood-muted); }
.engine-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
  background: var(--wood-muted);
  animation: pulse 1.2s ease-in-out infinite;
}
.engine[data-state="ready"] .engine-dot { background: #7cc287; animation: none; }
.engine[data-state="error"] .engine-dot { background: #f08a7c; animation: none; }
@keyframes pulse { 50% { opacity: 0.25; } }

.hymn-board {
  display: flex; gap: 14px; flex-shrink: 0;
  padding: 12px;
  background: var(--board);
  border: 1px solid var(--board-edge); border-radius: 6px;
}
.hymn-slot { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.hymn-number {
  min-width: 76px; padding: 4px 12px;
  background: var(--slot); border: 1px solid var(--slot-edge); border-radius: 3px;
  text-align: center;
  font: 600 34px/1.15 var(--display);
  color: var(--slot-ink);
  font-variant-numeric: tabular-nums;
}
.hymn-label { font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--slot-label); }

/* --- workspace ------------------------------------------------------- */

.workspace {
  max-width: 1392px; margin: 0 auto;
  padding: 28px 24px 48px;
  display: grid; grid-template-columns: minmax(400px, 5fr) minmax(0, 6fr);
  gap: 32px; align-items: start;
}

.controls {
  position: sticky; top: 16px;
  max-height: calc(100vh - 32px); overflow: auto;
  display: flex; flex-direction: column; gap: 22px;
  padding: 22px;
  background: var(--panel);
  border: 1px solid var(--rule); border-radius: 10px;
}

.field { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
/* the song text takes all the height the card has left */
.controls > * { flex-shrink: 0; }
.controls > .songs-field { flex: 1 1 auto; min-height: 300px; }
.songs-field .editor { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }
.field-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px 12px; }
.field-actions { display: flex; gap: 16px; }
.label { display: block; padding: 0; font-weight: 600; font-size: 13px; letter-spacing: 0.02em; }
.hint { margin: 0; font-size: 13px; color: var(--muted); }
.undo {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin: 0; padding: 8px 12px;
  font-size: 13px;
  background: color-mix(in srgb, var(--accent) 9%, var(--panel));
  border: 1px solid color-mix(in srgb, var(--accent) 30%, var(--rule));
  border-radius: 6px;
}
.undo .link-button { flex-shrink: 0; min-height: 28px; }
.title-code { color: var(--accent); font-weight: 500; }

/* Editor: a transparent textarea over a backdrop that paints the same text,
   so "# Title" lines can be coloured. Both must share every text metric. */
.editor { position: relative; }
.editor > textarea, .editor-backdrop {
  margin: 0; padding: 14px;
  font: 400 13px/1.6 var(--mono);
  letter-spacing: 0; tab-size: 2;
  white-space: pre-wrap; overflow-wrap: break-word; word-break: normal;
  border: 1px solid var(--rule); border-radius: 6px;
  scrollbar-gutter: stable;
}
/* the textarea sets the size; the backdrop fills the same box behind it */
.editor-backdrop {
  position: absolute; inset: 0;
  overflow: hidden;
  background: var(--field); color: var(--ink);
  pointer-events: none;
}
.editor-backdrop .title-line { color: var(--accent); font-weight: 500; }
/* search matches; no padding, so the text keeps its exact position under the textarea */
.editor-backdrop mark { padding: 0; color: inherit; background: rgb(242 193 78 / 0.55); border-radius: 2px; }
.editor-backdrop mark.current { color: var(--accent-ink); background: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

.search {
  display: flex; align-items: center; gap: 2px;
  flex: 1 1 180px; min-width: 0;
  padding: 0 4px 0 10px;
  color: var(--muted);
  background: var(--field);
  border: 1px solid var(--rule); border-radius: 999px;
}
.search:focus-within { border-color: var(--accent); }
.search input {
  flex: 1 1 auto; min-width: 0;
  padding: 6px 4px; border: 0; outline: 0;
  font: inherit; font-size: 13px; color: var(--ink); background: transparent;
}
.search input::-webkit-search-cancel-button { display: none; }
.search-count { font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.search-count.none { color: var(--danger); }
.search-step {
  display: grid; place-items: center; width: 26px; height: 26px; padding: 0;
  color: var(--muted); background: none; border: 0; border-radius: 50%; cursor: pointer;
}
.search-step:hover:not(:disabled) { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.search-step:disabled { opacity: 0.35; cursor: default; }
.editor > textarea {
  position: relative; display: block;
  flex: 1 1 auto; width: 100%; min-height: 0;
  resize: none; overflow: auto;
  background: transparent; color: transparent;
  caret-color: var(--ink);
}
.editor > textarea::placeholder { color: var(--muted); opacity: 0.75; }
.editor > textarea::selection { color: transparent; background: color-mix(in srgb, var(--accent) 28%, transparent); }
.editor > textarea:focus-visible { outline-offset: 0; }
.drop-hint {
  position: absolute; inset: 0; z-index: 1;
  display: none; place-items: center;
  border: 2px dashed var(--accent); border-radius: 6px;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  color: var(--accent); font-weight: 600;
}
.editor.dragging .drop-hint { display: grid; }

.template-card {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px;
  border: 1px dashed var(--rule-strong); border-radius: 6px;
  background: var(--field);
}
.template-card.chosen { border-style: solid; }
.template-card:hover { border-color: var(--accent); }
.template-pick { display: flex; align-items: center; gap: 12px; flex-grow: 1; min-width: 0; min-height: 40px; cursor: pointer; }
.template-icon { color: var(--accent); flex-shrink: 0; }
.template-text { display: flex; flex-direction: column; min-width: 0; }
.template-name { font-size: 14px; font-weight: 500; overflow-wrap: anywhere; }
.template-sub { font-size: 12px; color: var(--muted); }

.check { display: flex; align-items: flex-start; gap: 12px; cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 2px 0 0; flex-shrink: 0; accent-color: var(--accent); }
.check-text { display: flex; flex-direction: column; font-weight: 500; }
.check-text .hint { font-weight: 400; }

.more summary {
  width: fit-content; cursor: pointer;
  font-weight: 600; font-size: 13px; letter-spacing: 0.02em;
}
.more-body { display: flex; flex-direction: column; gap: 16px; margin-top: 12px; }
.type-settings { margin: 0; padding: 0; border: 0; min-width: 0; display: grid; grid-template-columns: 1.6fr 1fr 1fr; gap: 10px; }
.type-settings legend { padding: 0; margin-bottom: 8px; }
.mini span { display: block; margin-bottom: 4px; font-size: 12px; color: var(--muted); }
.more-body .label { margin-bottom: 6px; }

input[type="text"], input[type="number"], select {
  width: 100%;
  padding: 9px 10px;
  font: inherit; font-size: 14px; color: var(--ink);
  background: var(--field);
  border: 1px solid var(--rule); border-radius: 6px;
}

.link-button {
  padding: 0; border: 0; background: none;
  font: inherit; font-size: 13px; font-weight: 500;
  color: var(--accent); cursor: pointer;
  text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1px;
}
.link-button:hover { color: var(--accent-strong); }

/* pinned to the bottom of the scrolling controls card, so it is always in reach */
.download {
  position: sticky; bottom: -22px; z-index: 1;
  display: flex; flex-direction: column; gap: 10px;
  margin: 0 -22px -22px; padding: 18px 22px 22px;
  background: var(--panel);
  border-top: 1px solid var(--rule);
  border-radius: 0 0 10px 10px;
}
.button-primary {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; min-height: 50px; padding: 0 18px;
  font: 600 16px var(--body);
  color: var(--accent-ink); background: var(--accent);
  border: 0; border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}
.button-primary:hover:not(:disabled) { background: var(--accent-strong); }
.button-primary:disabled { opacity: 0.55; cursor: progress; }
.message { margin: 0; font-size: 13px; }
.message:empty { display: none; }
.message.error { color: var(--danger); }
.message.done { color: var(--muted); }
.privacy { margin: 0; font-size: 12px; color: var(--muted); }

/* --- preview --------------------------------------------------------- */

.preview { min-width: 0; }
.preview-head {
  position: sticky; top: 0; z-index: 2;
  display: flex; flex-direction: column; gap: 12px;
  margin-bottom: 20px; padding: 12px 0 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--rule);
}
.preview-head h2 { margin: 0; font: 600 24px var(--display); }
.jump {
  display: flex; gap: 8px;
  overflow-x: auto; scrollbar-width: thin;
  padding-bottom: 2px;
}
.jump a {
  flex-shrink: 0;
  padding: 6px 12px;
  font-size: 13px; font-weight: 500; color: var(--ink); text-decoration: none; white-space: nowrap;
  background: var(--panel);
  border: 1px solid var(--rule); border-radius: 999px;
}
.jump a:hover { border-color: var(--accent); color: var(--accent); }

.empty {
  display: grid; place-content: center; text-align: center;
  min-height: 320px;
  border: 1px dashed var(--rule-strong); border-radius: 10px;
  color: var(--muted);
}
.empty p { margin: 2px 0; }
.empty p:first-child { font: 500 18px var(--display); color: var(--ink); }

.notice {
  margin: 0 0 18px; padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--rule); border-radius: 6px;
  font-size: 13px;
}

.song { margin-bottom: 30px; scroll-margin-top: 120px; content-visibility: auto; contain-intrinsic-size: auto 280px; }
.song-head { display: flex; align-items: baseline; gap: 10px; margin: 0 0 12px; }
.song-head h3 { margin: 0; font: 500 19px/1.3 var(--display); }
.song-head span { font-size: 12px; color: var(--muted); white-space: nowrap; }

.slides { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 16px; }
.slide-figure { margin: 0; display: flex; flex-direction: column; gap: 6px; scroll-margin-top: 140px; }
/* song slides open their verse in the editor; the one being edited is outlined */
.slide-figure[data-start] .slide { cursor: pointer; transition: box-shadow 0.15s; }
.slide-figure[data-start] .slide:hover { box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 45%, transparent), var(--shadow); }
.slide-figure.current .slide { box-shadow: 0 0 0 3px var(--accent), var(--shadow); }
.slide:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

.preview { --font: Calibri, Carlito, sans-serif; --slide-w: 10; --slide-ratio: 4 / 3; }

/* A slide drawn at its real proportions: --in is one inch of the slide (its width is
   --slide-w inches = 100cqw), so positions and pt sizes match songslides.py exactly. */
.slide {
  --in: calc(100cqw / var(--slide-w));
  container-type: inline-size;
  position: relative;
  aspect-ratio: var(--slide-ratio);
  overflow: hidden;
  background: var(--slide);
  color: var(--slide-ink);
  border-radius: 3px;
  box-shadow: var(--shadow);
  font-family: var(--font);
}
.slide-art { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }
.slide-title {
  position: absolute; left: calc(0.7 * var(--in)); right: calc(1.5 * var(--in)); top: calc(0.65 * var(--in));
  margin: 0;
  font-size: calc(var(--pt) * var(--in) / 72);
  font-weight: 700; line-height: 1.2;
  white-space: nowrap; overflow: hidden;
}
.slide.toc .slide-title { right: calc(0.7 * var(--in)); }
.slide-counter {
  position: absolute; right: calc(0.4 * var(--in)); top: calc(0.65 * var(--in));
  font-size: calc(24 * var(--in) / 72);
  font-weight: 700; line-height: 1.2;
  color: #8b4513;
}
.slide-lyrics {
  position: absolute; left: calc(0.7 * var(--in)); right: calc(0.7 * var(--in)); top: calc(1.45 * var(--in));
  margin: 0; padding: 0; list-style: none;
  font-size: calc(var(--pt) * var(--in) / 72);
  line-height: 1.2;
}
.slide-lyrics li { margin-bottom: calc(var(--gap) * var(--in) / 72); white-space: pre-wrap; overflow-wrap: anywhere; }
.slide-toc {
  position: absolute; left: calc(0.7 * var(--in)); right: calc(0.5 * var(--in)); top: calc(1.65 * var(--in));
  display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: repeat(10, auto);
  grid-auto-flow: column; column-gap: calc(0.4 * var(--in)); align-content: start;
  margin: 0; padding: 0; list-style: none;
  font-size: calc(var(--pt) * var(--in) / 72);
  line-height: 1.2;
  color: var(--slide-link);
}
.slide-toc li { font-size: calc(var(--pt) * var(--in) / 72); margin-bottom: calc(var(--gap) * var(--in) / 72); text-decoration: underline; white-space: pre; overflow: hidden; text-overflow: ellipsis; }
.slide-toc a { color: inherit; text-decoration: inherit; }
.slide-toc a:hover { color: #3a3ad0; }
.slide-note { margin: 0; font-size: 12px; color: var(--accent); }

/* --- wide screens: an editor layout, text and preview scroll separately -- */

@media (min-width: 861px) {
  body { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
  .masthead { flex: none; }
  .masthead-inner { padding: 14px 24px; align-items: center; }
  .brand { gap: 2px; }
  .brand h1 { font-size: 30px; }
  .lede { font-size: 14px; }
  .engine { margin-top: 2px; }
  .hymn-board { gap: 10px; padding: 8px; }
  .hymn-number { min-width: 64px; padding: 2px 10px; font-size: 28px; }
  .hymn-label { font-size: 10px; }

  .workspace {
    flex: 1 1 auto; min-height: 0; width: 100%;
    grid-template-rows: minmax(0, 1fr); align-items: stretch;
    padding: 20px 24px;
  }
  .controls { position: static; align-self: stretch; max-height: none; min-height: 0; overflow: auto; }
  /* give the song text every pixel: compact settings below it */
  .controls { gap: 16px; padding: 18px 20px 20px; }
  .template-card { padding: 6px 12px; }
  .template-card.chosen .template-sub, .toc-hint { display: none; }
  .download { bottom: -20px; margin: 0 -20px -20px; padding: 14px 20px 18px; gap: 8px; }
  .button-primary { min-height: 46px; }
  .preview { min-height: 0; overflow: auto; padding: 0 12px 24px 4px; scrollbar-gutter: stable; }
}

/* --- small screens --------------------------------------------------- */

@media (max-width: 860px) {
  .masthead-inner { padding: 22px 16px 18px; align-items: flex-start; }
  .brand h1 { font-size: 30px; }
  .lede { font-size: 14px; }
  .hymn-board { gap: 8px; padding: 8px; border-radius: 5px; }
  .hymn-number { min-width: 44px; padding: 2px 8px; font-size: 22px; line-height: 1.2; }
  .hymn-label { font-size: 9px; }

  .workspace { grid-template-columns: minmax(0, 1fr); gap: 24px; padding: 16px 16px 140px; }
  .controls { position: static; max-height: none; overflow: visible; min-width: 0; padding: 16px; gap: 18px; }
  .controls > .songs-field { min-height: 0; }
  .songs-field .editor { flex: none; height: 55vh; min-height: 260px; }
  .field-head { flex-wrap: wrap; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .more summary { min-height: 44px; display: flex; align-items: center; }

  /* the download bar stays at the bottom of the screen */
  .download {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    gap: 6px; margin: 0; border-radius: 0;
    padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
    background: var(--panel);
    border-top: 1px solid var(--rule);
    box-shadow: 0 -6px 18px rgb(30 27 24 / 0.1);
  }
  .privacy { text-align: center; }
  .message { text-align: center; }

  .preview-head { padding-top: 8px; }
  .preview-head h2 { font-size: 21px; }
  .jump a { padding: 8px 12px; }
  .slides { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .song { scroll-margin-top: 110px; }
}

@media (max-width: 380px) {
  .masthead-inner { flex-direction: column; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
