/* ScriptureStudio — Sermon Notes
 *
 * Rich-text editor for sermon outlines, with scripture-reference auto-detection,
 * inline verse unpack blocks, and the same right-sidebar (commentary + lexicon)
 * as the Reader.
 *
 * Design goals
 *   - single contentEditable surface; editor IS the preview
 *   - large serif body, high contrast (designed to be printed for preaching)
 *   - 4-depth outline with standard numerals: I/II → A/B → 1/2 → a/b
 *   - scripture blocks visibly distinct; primary (preaching) blocks heavier
 *   - print media query strips chrome, bumps size, respects page breaks
 */

:root {
  --sp-bg: #fdfaf2;
  --sp-paper: #ffffff;
  --sp-ink: #1c1917;
  --sp-ink-soft: #4b4a47;
  --sp-rule: #d9cfb9;
  --sp-rule-soft: #ebe4d2;
  --sp-accent: #8b6f3e;
  --sp-accent-soft: #c9b58a;
  --sp-scripture-bg: #fcf8ed;
  --sp-primary-bg: #fff7de;
  --sp-summary-bg: #eef3f0;
  --sp-summary-rule: #b8cec3;
  --sp-hover: #f0e6c8;
}

* { box-sizing: border-box; }

/* Critical: author-defined `display: flex` on .sp-gate and .sp-app overrides
 * the UA default `[hidden] { display: none }`. Make the HTML `hidden`
 * attribute authoritative regardless of other display rules. Without this
 * the gate never hides on bootApp(). */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  background: var(--sp-bg);
  color: var(--sp-ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ───── Auth gate ───── */

.sp-gate {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--sp-bg);
  z-index: 10;
}
.sp-gate-card {
  background: var(--sp-paper);
  border: 1px solid var(--sp-rule);
  border-radius: 8px;
  padding: 2em 2.5em;
  max-width: 420px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0,0,0,0.05);
}
.sp-gate-card h2 { margin: 0 0 0.5em; font-weight: 600; }
.sp-gate-note { font-size: 0.85em; color: var(--sp-ink-soft); margin-top: 1em; line-height: 1.4; }
.sp-btn {
  display: inline-block;
  padding: 0.6em 1.4em;
  background: var(--sp-accent);
  color: #fff;
  text-decoration: none;
  border-radius: 4px;
  font-weight: 500;
  margin-top: 0.5em;
}
.sp-btn:hover { background: #6f5730; }

/* ───── App layout ───── */

.sp-app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.sp-header {
  position: sticky;
  top: 0;
  background: var(--sp-paper);
  border-bottom: 1px solid var(--sp-rule);
  z-index: 5;
}

.sp-header-row {
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.5em 1em;
  border-bottom: 1px solid var(--sp-rule-soft);
}

.sp-home {
  color: var(--sp-ink-soft);
  text-decoration: none;
  font-size: 1.2em;
  padding: 0.2em 0.5em;
}
.sp-home:hover { color: var(--sp-ink); }

.sp-title-input {
  flex: 1;
  font-size: 1.1em;
  font-weight: 500;
  border: none;
  background: transparent;
  padding: 0.3em 0.4em;
  color: var(--sp-ink);
  outline: none;
}
.sp-title-input:focus { background: var(--sp-rule-soft); border-radius: 3px; }

.sp-save-status {
  font-size: 0.8em;
  color: var(--sp-ink-soft);
  white-space: nowrap;
  padding: 0.3em 0.5em;
}
.sp-save-status.saving { color: #a06b00; }
.sp-save-status.saved { color: #2f7d32; }
.sp-save-status.error { color: #b42318; }

/* ───── Toolbar ───── */

.sp-toolbar {
  display: flex;
  gap: 0.3em;
  padding: 0.4em 0.8em;
  flex-wrap: wrap;
  align-items: center;
}
.sp-tools-group {
  display: flex;
  gap: 2px;
  padding-right: 0.5em;
  border-right: 1px solid var(--sp-rule-soft);
}
.sp-tools-group:last-child { border-right: none; }
.sp-tools-right { margin-left: auto; border-right: none; }

.sp-tool {
  background: transparent;
  border: 1px solid transparent;
  padding: 0.3em 0.6em;
  cursor: pointer;
  color: var(--sp-ink);
  font-size: 0.95em;
  border-radius: 3px;
  min-width: 32px;
  user-select: none;
}
.sp-tool:hover { background: var(--sp-rule-soft); }
.sp-tool:active, .sp-tool.active { background: var(--sp-accent-soft); border-color: var(--sp-accent); }

/* ───── Main area ───── */

.sp-main {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.sp-editor-wrap {
  flex: 1;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  padding: 2em 1em;
  background: var(--sp-bg);
}

.sp-sidebar {
  width: 420px;
  max-width: 40vw;
  border-left: 1px solid var(--sp-rule);
  background: var(--sp-paper);
  overflow-y: auto;
  flex-shrink: 0;
}

/* ───── Editor surface ───── */

.sp-editor {
  width: 100%;
  max-width: 8.5in;
  min-height: 11in;
  padding: 1in;
  background: var(--sp-paper);
  color: var(--sp-ink);
  font-family: Georgia, "Iowan Old Style", "Cambria", serif;
  /* Screen: readable without being huge. Print rule below overrides. */
  font-size: 12pt;
  line-height: 1.5;
  outline: none;
  box-shadow: 0 2px 12px rgba(0,0,0,0.04);
  border: 1px solid var(--sp-rule-soft);
}

.sp-editor:empty::before {
  content: 'Start typing your sermon outline. Use Tab to indent. Scripture references (e.g. John 3:16) are auto-bolded — right-click one to unpack it.';
  color: var(--sp-ink-soft);
  font-style: italic;
}

.sp-editor p { margin: 0.5em 0; }

/* ───── Outline lists (4 depths) ───── */

.sp-editor ol {
  margin: 0.4em 0 0.4em 1.5em;
  padding-left: 1em;
}
.sp-editor ol.depth-1 { list-style-type: upper-roman; font-weight: 600; font-size: 1.05em; }
.sp-editor ol.depth-2 { list-style-type: upper-alpha; font-weight: 500; font-size: 1em; }
.sp-editor ol.depth-3 { list-style-type: decimal; font-weight: 400; font-size: 0.98em; }
.sp-editor ol.depth-4 { list-style-type: lower-alpha; font-weight: 400; font-size: 0.96em; }
.sp-editor ol ol { margin-top: 0.2em; margin-bottom: 0.2em; }

.sp-editor li {
  margin: 0.25em 0;
  padding-left: 0.3em;
}
.sp-editor li::marker { color: var(--sp-accent); font-weight: 600; }

/* ───── Legacy-import class aliases ───── */
/* The legacy importer emits .sp-note-scripture (empty div with data-ref) and
 * .sp-note-list. Alias them onto the canonical editor classes so imported
 * notes render the same as native ones. */
.sp-editor .sp-note-list { margin: 0.4em 0 0.4em 1.5em; padding-left: 1em; }
.sp-editor .sp-note-list.depth-1 { list-style-type: upper-roman; font-weight: 600; font-size: 1.05em; }
.sp-editor .sp-note-list.depth-2 { list-style-type: upper-alpha; font-weight: 500; font-size: 1em; }
.sp-editor .sp-note-list.depth-3 { list-style-type: decimal; font-weight: 400; font-size: 0.98em; }
.sp-editor .sp-note-list.depth-4 { list-style-type: lower-alpha; font-weight: 400; font-size: 0.96em; }

/* Legacy `.sp-note-scripture` blocks are migrated to inline `.sp-ref` spans
 * on every note load (see sermon-notes.js migrateLegacyRefs). If any slip
 * through before migration runs, keep them visually unobtrusive as inline
 * text rather than full-width blocks. */
.sp-note-scripture {
  display: inline;
  font-weight: 700;
  cursor: pointer;
  color: var(--sp-accent);
}
.sp-note-scripture::before { content: attr(data-ref); }

.sp-note-break { border: none; border-top: 1px solid var(--sp-rule); margin: 1em 0; }
.sp-note-break-major { border-top: 3px double var(--sp-accent-soft); margin: 1.4em 0; }

.sp-note-quote {
  margin: 0.8em 0;
  padding: 0.6em 1em;
  border-left: 3px solid var(--sp-rule);
  background: #f5f2ea;
  font-style: italic;
  color: var(--sp-ink-soft);
}

/* ───── Scripture references (inline, bolded) ───── */

.sp-ref {
  font-weight: 700;
  cursor: pointer;
  border-bottom: 1px dotted transparent;
  padding: 0 1px;
  white-space: nowrap;
}
.sp-ref:hover {
  background: var(--sp-hover);
  border-bottom-color: var(--sp-accent);
}
.sp-ref.sp-ref-unpacked { color: var(--sp-accent); }
.sp-ref.sp-ref-primary { font-weight: 800; font-size: 1.08em; color: var(--sp-accent); }
.sp-scripture-block.sp-scripture-primary { background: var(--sp-primary-bg); border-left-color: var(--sp-accent); border-left-width: 6px; font-weight: 600; font-size: 1.1em; }

/* ───── Unpacked scripture blocks ───── */

.sp-scripture-block {
  display: block;
  margin: 0.8em 0;
  padding: 0.8em 1em;
  background: var(--sp-scripture-bg);
  border-left: 4px solid var(--sp-accent-soft);
  border-radius: 3px;
  font-size: 1em;
  line-height: 1.5;
  position: relative;
}
.sp-scripture-block[data-primary="1"] {
  background: var(--sp-primary-bg);
  border-left-color: var(--sp-accent);
  border-left-width: 6px;
  font-weight: 600;
  font-size: 1.12em;
}
/* Block's internal header is hidden when there's a preceding inline .sp-ref
 * (which already labels the passage). Keep the DOM attribute for a11y /
 * screen readers but render it collapsed. */
.sp-scripture-block .sp-sb-head {
  display: none;
}
.sp-scripture-block[data-primary="1"] .sp-sb-head {
  color: #5f4820;
}
.sp-scripture-block .sp-sb-verse {
  display: block;
  margin: 0.2em 0;
}
.sp-scripture-block .sp-sb-verse-num {
  display: inline-block;
  min-width: 1.6em;
  color: var(--sp-accent);
  font-weight: 600;
  font-size: 0.85em;
  vertical-align: baseline;
}
.sp-scripture-block .word-button {
  display: inline;
  background: transparent;
  border: none;
  font: inherit;
  color: inherit;
  padding: 0 1px;
  cursor: pointer;
  border-bottom: 1px solid transparent;
}
.sp-scripture-block .word-button:hover {
  background: var(--sp-hover);
  border-bottom-color: var(--sp-accent);
}

/* ───── Summary blocks (compact mini-outline) ───── */

.sp-summary-block {
  display: block;
  margin: 0.7em 0 0.7em 0.2em;
  padding: 0.5em 0.9em;
  background: var(--sp-summary-bg);
  border-left: 3px solid var(--sp-summary-rule);
  border-radius: 3px;
  font-size: 0.85em;
  font-style: italic;
  color: var(--sp-ink-soft);
}
.sp-summary-block::before {
  content: 'Summary';
  display: block;
  font-size: 0.75em;
  font-style: normal;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--sp-summary-rule);
  margin-bottom: 0.3em;
}
.sp-summary-block ol {
  margin: 0.1em 0 0.1em 1.3em;
  font-size: 0.95em;
}
.sp-summary-block ol.depth-1 { font-size: 0.95em; font-weight: 500; }
.sp-summary-block ol.depth-2 { font-size: 0.92em; }
.sp-summary-block ol.depth-3 { font-size: 0.9em; }
.sp-summary-block li { margin: 0.1em 0; }

/* ───── Horizontal rules (section breaks) ───── */

.sp-editor hr {
  border: none;
  border-top: 1px solid var(--sp-rule);
  margin: 1em 0;
}

/* ───── Blockquotes (converted from legacy <h2>/<h3> abuse) ───── */

.sp-editor blockquote,
.sp-editor .sp-note-quote {
  margin: 0.8em 0;
  padding: 0.5em 1em;
  border-left: 3px solid var(--sp-rule);
  background: #f5f2ea;
  font-style: italic;
  color: var(--sp-ink-soft);
}

/* ───── Context menu ───── */

.sp-ctx-menu {
  position: fixed;
  z-index: 20;
  background: var(--sp-paper);
  border: 1px solid var(--sp-rule);
  border-radius: 5px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  min-width: 180px;
  padding: 0.3em 0;
}
.sp-ctx-menu[hidden] { display: none; }
.sp-ctx-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  padding: 0.5em 1em;
  cursor: pointer;
  font-size: 0.95em;
  color: var(--sp-ink);
}
.sp-ctx-item:hover { background: var(--sp-rule-soft); }
.sp-ctx-item:disabled { color: #a8a295; cursor: default; }
.sp-ctx-item:disabled:hover { background: transparent; }
.sp-ctx-sep { border-top: 1px solid var(--sp-rule-soft); margin: 0.3em 0; }

/* ───── Picker modal ───── */

.sp-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 12vh;
  z-index: 30;
}
.sp-modal[hidden] { display: none; }
.sp-modal-card {
  background: var(--sp-paper);
  border-radius: 8px;
  width: min(540px, 92vw);
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0,0,0,0.25);
}
.sp-modal-head {
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.7em 1em;
  border-bottom: 1px solid var(--sp-rule-soft);
}
.sp-modal-head h3 { margin: 0; font-size: 1em; flex-shrink: 0; }
.sp-modal-head input[type="search"] {
  flex: 1;
  padding: 0.3em 0.5em;
  border: 1px solid var(--sp-rule);
  border-radius: 3px;
  font-size: 0.95em;
  outline: none;
}
.sp-modal-close {
  background: transparent;
  border: none;
  font-size: 1.2em;
  cursor: pointer;
  padding: 0 0.4em;
  color: var(--sp-ink-soft);
}
.sp-modal-close:hover { color: var(--sp-ink); }

.sp-picker-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}
.sp-picker-item {
  padding: 0.7em 1em;
  cursor: pointer;
  border-bottom: 1px solid var(--sp-rule-soft);
}
.sp-picker-item:hover { background: var(--sp-rule-soft); }
.sp-picker-item.active { background: var(--sp-primary-bg); }
.sp-picker-title { font-weight: 500; }
.sp-picker-meta { font-size: 0.8em; color: var(--sp-ink-soft); margin-top: 0.2em; }

/* ───── Print CSS ───── */

@media print {
  /* Reasonable print defaults: a pastor should get ~450-650 words per page,
   * which is roughly 11pt serif at 1.35 line-height on US Letter with
   * 0.6in margins. Keeps sermon notes to 2-4 pages rather than 6-8. */
  @page { size: letter; margin: 0.55in 0.65in; }
  html, body { background: #fff; }
  .sp-header, .sp-sidebar, .sp-ctx-menu, .sp-modal, .sp-gate { display: none !important; }
  .sp-main { display: block; overflow: visible; }
  .sp-editor-wrap { padding: 0; overflow: visible; justify-content: flex-start; background: #fff; }
  .sp-editor {
    border: none;
    box-shadow: none;
    max-width: none;
    width: 100%;
    padding: 0;
    font-size: 11pt;
    line-height: 1.35;
    min-height: auto;
    color: #000;
  }
  .sp-editor p { margin: 0.35em 0; }
  .sp-editor h1, .sp-editor h2, .sp-editor h3 {
    page-break-after: avoid;
    margin: 0.6em 0 0.3em;
  }
  .sp-editor h1 { font-size: 14pt; }
  .sp-editor h2 { font-size: 12.5pt; }
  .sp-editor h3 { font-size: 11.5pt; }
  .sp-editor ol.depth-1 { font-size: 1em; }
  .sp-editor ol { margin: 0.25em 0 0.35em 1.2em; padding-left: 0.8em; }
  .sp-editor li { margin: 0.15em 0; }
  .sp-scripture-block {
    background: #fff;
    border-left-width: 3px;
    border-left-color: #555;
    font-size: 10.5pt;
    margin: 0.35em 0;
    padding: 0.25em 0.5em;
    page-break-inside: avoid;
  }
  .sp-scripture-block[data-primary="1"],
  .sp-scripture-block.sp-scripture-primary {
    background: #fff;
    font-size: 11pt;
    font-weight: 600;
    border-left-width: 4px;
    border-left-color: #000;
  }
  .sp-scripture-block .word-button {
    cursor: default;
    border-bottom: none;
    background: transparent;
    padding: 0;
    color: inherit;
    font: inherit;
  }
  .sp-scripture-block .sp-sb-head { font-weight: 700; }
  .sp-scripture-block .sp-sb-verse-num {
    font-weight: 700;
    font-size: 0.85em;
    vertical-align: super;
    margin-right: 0.15em;
  }
  .sp-summary-block {
    background: #fff;
    border-left-color: #999;
    page-break-inside: avoid;
  }
  .sp-ref {
    color: inherit;
    font-weight: 600;
    background: transparent;
  }
  .sp-ref.sp-ref-primary { font-size: 1em; }
  .sp-editor hr { border-top-color: #999; }
  /* Hide the "start typing…" placeholder if the doc is empty on print. */
  .sp-editor:empty::before { display: none; }
}
