/* ============================================================
   NEM Lectionary Enhancement — TOC, Search, Day/Night Toggle
   © 2026 New Every Morning Anglican Fellowship
   Drop-in, progressive-enhancement stylesheet
   ============================================================ */

:root {
  --nem-cream: #faf7f0;
  --nem-cream-soft: #f3ecd9;
  --nem-gold: #b8973a;
  --nem-gold-soft: #d4b55a;
  --nem-ink: #2a2118;
  --nem-ink-soft: #4a3e30;
  --nem-sepia: #6b4f2c;
  --nem-rule: rgba(184, 151, 58, 0.35);
  --nem-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
  --nem-font-display: 'Cinzel', 'Trajan Pro', Georgia, serif;
  --nem-font-body: 'Crimson Text', 'EB Garamond', Georgia, serif;
  --nem-font-italic: 'IM Fell English', 'EB Garamond', Georgia, serif;

  /* Theme tokens — day mode by default on :root */
  --bg: var(--nem-cream);
  --bg-elev: #ffffff;
  --bg-soft: var(--nem-cream-soft);
  --fg: var(--nem-ink);
  --fg-soft: var(--nem-ink-soft);
  --accent: var(--nem-gold);
  --accent-soft: var(--nem-gold-soft);
  --rule: var(--nem-rule);
  --shadow: var(--nem-shadow);
}

/* Dark (night) mode — default per NEM system */
html[data-nem-theme="night"],
html:not([data-nem-theme]) {
  --bg: #1a1410;
  --bg-elev: #241c15;
  --bg-soft: #2d241b;
  --fg: #f0e6d2;
  --fg-soft: #c9bba0;
  --accent: #d4b55a;
  --accent-soft: #b8973a;
  --rule: rgba(212, 181, 90, 0.3);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.5);
}

html[data-nem-theme="day"] {
  --bg: var(--nem-cream);
  --bg-elev: #ffffff;
  --bg-soft: var(--nem-cream-soft);
  --fg: var(--nem-ink);
  --fg-soft: var(--nem-ink-soft);
  --accent: var(--nem-gold);
  --accent-soft: var(--nem-gold-soft);
  --rule: var(--nem-rule);
  --shadow: var(--nem-shadow);
}

/* ---------- Control cluster (top-right) ---------- */
.nem-controls {
  position: fixed;
  top: 1rem;
  right: 1rem;
  display: flex;
  gap: 0.5rem;
  z-index: 9998;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.nem-btn {
  background: var(--bg-elev);
  color: var(--fg);
  border: 1.5px solid var(--rule);
  border-radius: 999px;
  padding: 0.5rem 0.95rem;
  font-family: var(--nem-font-display);
  font-size: 0.78rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.18s ease, background 0.18s ease, color 0.18s ease, border-color 0.18s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  min-height: 44px; /* touch target */
}

.nem-btn:hover,
.nem-btn:focus-visible {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  transform: translateY(-1px);
}

.nem-btn:focus-visible {
  outline: 3px solid var(--accent-soft);
  outline-offset: 2px;
}

.nem-btn__icon {
  font-size: 1rem;
  line-height: 1;
}

/* ---------- Search modal ---------- */
.nem-search-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 9999;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 4rem 1rem 1rem;
  overflow-y: auto;
}

.nem-search-backdrop[data-open="true"] {
  display: flex;
}

.nem-search-modal {
  background: var(--bg-elev);
  color: var(--fg);
  border: 1.5px solid var(--rule);
  border-radius: 12px;
  width: 100%;
  max-width: 640px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 5rem);
  overflow: hidden;
}

.nem-search-header {
  padding: 1.25rem 1.5rem 0.75rem;
  border-bottom: 1px solid var(--rule);
  flex-shrink: 0;
}

.nem-search-title {
  font-family: var(--nem-font-display);
  font-size: 0.9rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 0.75rem;
  font-weight: 500;
}

.nem-search-input {
  width: 100%;
  font-family: var(--nem-font-body);
  font-size: 1.15rem;
  padding: 0.65rem 0.85rem;
  border: 1.5px solid var(--rule);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  box-sizing: border-box;
}

.nem-search-input:focus-visible {
  outline: 3px solid var(--accent-soft);
  outline-offset: 1px;
  border-color: var(--accent);
}

.nem-search-hint {
  font-family: var(--nem-font-italic);
  font-style: italic;
  color: var(--fg-soft);
  font-size: 0.85rem;
  margin: 0.5rem 0 0;
}

.nem-search-results {
  overflow-y: auto;
  flex: 1;
  padding: 0.5rem 0;
}

.nem-result,
.nem-toc-link {
  display: block;
  padding: 0.65rem 1.5rem;
  text-decoration: none;
  color: var(--fg);
  border-left: 3px solid transparent;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  font-family: var(--nem-font-body);
  line-height: 1.4;
}

.nem-result:hover,
.nem-result:focus-visible,
.nem-result[data-active="true"],
.nem-toc-link:hover,
.nem-toc-link:focus-visible {
  background: var(--bg-soft);
  border-left-color: var(--accent);
  color: var(--fg);
  outline: none;
}

.nem-result__title {
  font-weight: 600;
  font-size: 1.05rem;
}

.nem-result__context {
  font-family: var(--nem-font-italic);
  font-size: 0.85rem;
  color: var(--fg-soft);
  margin-top: 0.2rem;
  display: block;
}

.nem-result__snippet {
  font-size: 0.9rem;
  color: var(--fg-soft);
  margin-top: 0.25rem;
  font-style: italic;
}

.nem-result mark,
.nem-search-results mark {
  background: var(--accent);
  color: var(--bg);
  padding: 0 2px;
  border-radius: 2px;
  font-style: normal;
}

.nem-empty {
  padding: 1.5rem;
  text-align: center;
  color: var(--fg-soft);
  font-family: var(--nem-font-italic);
  font-style: italic;
}

/* ---------- TOC grouping inside modal ---------- */
.nem-toc-group {
  padding: 0.5rem 0 0.25rem;
}

.nem-toc-group__heading {
  font-family: var(--nem-font-display);
  font-size: 0.72rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--accent);
  padding: 0.6rem 1.5rem 0.35rem;
  margin: 0;
  border-top: 1px solid var(--rule);
  font-weight: 500;
}

.nem-toc-group:first-child .nem-toc-group__heading {
  border-top: none;
}

.nem-toc-link {
  padding: 0.4rem 1.5rem 0.4rem 2rem;
  font-size: 0.95rem;
}

/* ---------- Close button ---------- */
.nem-search-close {
  position: absolute;
  top: 0.75rem;
  right: 0.9rem;
  background: transparent;
  border: none;
  color: var(--fg-soft);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  padding: 0.35rem 0.55rem;
  border-radius: 999px;
  min-width: 44px;
  min-height: 44px;
}

.nem-search-close:hover,
.nem-search-close:focus-visible {
  background: var(--bg-soft);
  color: var(--fg);
  outline: none;
}

/* ---------- Anchored heading target highlight ---------- */
:target {
  animation: nem-flash 1.8s ease-out;
  scroll-margin-top: 1.5rem;
}

@keyframes nem-flash {
  0%   { background: var(--accent); color: var(--bg); }
  60%  { background: var(--accent-soft); color: var(--bg); }
  100% { background: transparent; color: inherit; }
}

/* ---------- Back-to-top ---------- */
.nem-top {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  width: 48px;
  height: 48px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--bg);
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  z-index: 9997;
  display: none;
  align-items: center;
  justify-content: center;
  transition: transform 0.18s ease, background 0.18s ease;
}

.nem-top[data-visible="true"] {
  display: inline-flex;
}

.nem-top:hover,
.nem-top:focus-visible {
  background: var(--accent-soft);
  transform: translateY(-2px);
  outline: none;
}

.nem-top:focus-visible {
  outline: 3px solid var(--accent-soft);
  outline-offset: 3px;
}

/* ---------- Screen reader only ---------- */
.nem-sr {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}

/* ---------- Anchor pilcrow on each heading ---------- */
.nem-anchor {
  color: var(--accent);
  text-decoration: none;
  opacity: 0;
  margin-left: 0.4rem;
  font-size: 0.8em;
  transition: opacity 0.2s ease;
  font-family: var(--nem-font-display);
}

*:hover > .nem-anchor,
.nem-anchor:focus-visible {
  opacity: 1;
  outline: none;
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .nem-btn, .nem-result, .nem-toc-link, .nem-top {
    transition: none;
  }
  :target { animation: none; }
}

/* ---------- Mobile tuning ---------- */
@media (max-width: 640px) {
  .nem-controls { top: 0.5rem; right: 0.5rem; }
  .nem-btn { font-size: 0.72rem; padding: 0.45rem 0.75rem; }
  .nem-btn__label { display: none; }
  .nem-btn__icon { font-size: 1.15rem; }
  .nem-search-backdrop { padding: 3rem 0.5rem 0.5rem; }
  .nem-top { bottom: 0.75rem; right: 0.75rem; width: 44px; height: 44px; }
}

/* ---------- Print: hide enhancement UI ---------- */
@media print {
  .nem-controls, .nem-search-backdrop, .nem-top, .nem-anchor {
    display: none !important;
  }
}

/* ============================================================
   Night-mode page overrides
   When toggled to night, override the host page's own colors.
   Scoped to html[data-nem-theme="night"] so day mode leaves the
   page untouched.
   ============================================================ */
html[data-nem-theme="night"] {
  background: #1a1410 !important;
  color: #f0e6d2 !important;
  color-scheme: dark;
}
html[data-nem-theme="night"] body {
  background: #1a1410 !important;
  color: #f0e6d2 !important;
}

/* Top-level containers — transparent so body bg shows */
html[data-nem-theme="night"] body > *,
html[data-nem-theme="night"] main,
html[data-nem-theme="night"] article,
html[data-nem-theme="night"] section,
html[data-nem-theme="night"] header,
html[data-nem-theme="night"] footer,
html[data-nem-theme="night"] nav,
html[data-nem-theme="night"] aside {
  background-color: transparent !important;
  color: inherit;
}

/* Common card / wrapper class-name patterns */
html[data-nem-theme="night"] [class*="container"],
html[data-nem-theme="night"] [class*="wrapper"],
html[data-nem-theme="night"] [class*="content"],
html[data-nem-theme="night"] [class*="page"],
html[data-nem-theme="night"] [class*="card"],
html[data-nem-theme="night"] [class*="panel"],
html[data-nem-theme="night"] [class*="box"],
html[data-nem-theme="night"] [class*="inner"],
html[data-nem-theme="night"] [class*="outer"],
html[data-nem-theme="night"] [class*="main"] {
  background-color: transparent !important;
}

/* Headings — gold accent */
html[data-nem-theme="night"] h1,
html[data-nem-theme="night"] h2,
html[data-nem-theme="night"] h3,
html[data-nem-theme="night"] h4,
html[data-nem-theme="night"] h5,
html[data-nem-theme="night"] h6 {
  color: #d4b55a !important;
}

/* Body text */
html[data-nem-theme="night"] p,
html[data-nem-theme="night"] li,
html[data-nem-theme="night"] blockquote,
html[data-nem-theme="night"] dd,
html[data-nem-theme="night"] dt,
html[data-nem-theme="night"] td,
html[data-nem-theme="night"] th,
html[data-nem-theme="night"] figcaption,
html[data-nem-theme="night"] caption,
html[data-nem-theme="night"] span {
  color: #f0e6d2 !important;
}
html[data-nem-theme="night"] strong,
html[data-nem-theme="night"] b {
  color: #f5ecd6 !important;
}
html[data-nem-theme="night"] em,
html[data-nem-theme="night"] i {
  color: #c9bba0 !important;
}

/* Links */
html[data-nem-theme="night"] a {
  color: #d4b55a !important;
}
html[data-nem-theme="night"] a:hover,
html[data-nem-theme="night"] a:focus {
  color: #e5c877 !important;
}
html[data-nem-theme="night"] a:visited {
  color: #c8a55a !important;
}

/* Rules, borders, blockquote */
html[data-nem-theme="night"] hr {
  border-color: rgba(212, 181, 90, 0.3) !important;
  background-color: rgba(212, 181, 90, 0.3) !important;
}
html[data-nem-theme="night"] blockquote {
  border-color: #b8973a !important;
  color: #d4c7a6 !important;
}

/* Tables */
html[data-nem-theme="night"] table,
html[data-nem-theme="night"] tr,
html[data-nem-theme="night"] td,
html[data-nem-theme="night"] th {
  background-color: transparent !important;
  border-color: rgba(212, 181, 90, 0.3) !important;
}
html[data-nem-theme="night"] thead {
  background-color: rgba(212, 181, 90, 0.08) !important;
}

/* Code blocks */
html[data-nem-theme="night"] pre,
html[data-nem-theme="night"] code {
  background-color: rgba(0, 0, 0, 0.35) !important;
  color: #e5d4a0 !important;
}

/* Form inputs (excluding my own search input) */
html[data-nem-theme="night"] input:not(.nem-search-input),
html[data-nem-theme="night"] select,
html[data-nem-theme="night"] textarea {
  background-color: #241c15 !important;
  color: #f0e6d2 !important;
  border-color: rgba(212, 181, 90, 0.3) !important;
}

/* Text selection */
html[data-nem-theme="night"] ::selection {
  background: #d4b55a;
  color: #1a1410;
}

/* Non-NEM buttons */
html[data-nem-theme="night"] button:not(.nem-btn):not(.nem-search-close):not(.nem-top) {
  background-color: rgba(212, 181, 90, 0.12) !important;
  color: #f0e6d2 !important;
  border-color: rgba(212, 181, 90, 0.3) !important;
}

/* Preserve non-NEM <mark>s (e.g. rubric highlights) */
html[data-nem-theme="night"] mark:not(.nem-search-results mark) {
  background: #d4b55a !important;
  color: #1a1410 !important;
}
