@font-face {
  font-family: "Skolar";
  src: url("/assets/fonts/fonnts.com-Skolar-Latin-.otf") format("opentype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Skolar";
  src: url("/assets/fonts/fonnts.com-Skolar-Latin-Medium.otf") format("opentype");
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Skolar";
  src: url("/assets/fonts/fonnts.com-Skolar-Latin-Semibold-.otf") format("opentype");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #f7f3ee;
  --bg-soft: rgba(247,243,238,0.9);
  --ink: #141210;
  --muted: #6e655d;
  --accent: #b44c2f;
  --card: #fffaf4;
  --border: #e5dbd1;
  --font-size: 19px;
  --measure-ch: 76;
}

:root[data-theme="dark"],
body[data-theme="dark"] {
  --bg: #141210;
  --bg-soft: rgba(20,18,16,0.92);
  --ink: #f3ece2;
  --muted: #b2a79b;
  --accent: #e07a5f;
  --card: #1b1916;
  --border: #2f2924;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Skolar", "Georgia", serif;
  font-size: var(--font-size);
  line-height: 1.65;
  font-style: normal;
}

/* ===========================================
   COLLAPSIBLE TOPBAR
   =========================================== */

.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow: visible;
}

/* Mini bar - always visible */
.topbar-mini {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  min-height: 52px;
}

.mini-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
}

.mini-app {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  flex-shrink: 0;
}

.mini-book {
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Menu toggle button */
.menu-toggle {
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.menu-toggle:hover {
  background: var(--border);
}

/* Hamburger icon - 3 lines */
.menu-icon {
  width: 20px;
  height: 16px;
  position: relative;
}

.menu-icon::before,
.menu-icon span,
.menu-icon::after {
  content: "";
  position: absolute;
  left: 0;
  width: 100%;
  height: 2px;
  background: var(--ink);
  border-radius: 1px;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.menu-icon::before {
  top: 0;
}

.menu-icon span {
  top: 50%;
  transform: translateY(-50%);
}

.menu-icon::after {
  bottom: 0;
}

/* X icon when open */
.topbar.open .menu-icon::before {
  transform: rotate(45deg);
  top: 7px;
}

.topbar.open .menu-icon span {
  opacity: 0;
}

.topbar.open .menu-icon::after {
  transform: rotate(-45deg);
  bottom: 7px;
}

/* Collapsible panel - starts open */
.topbar-panel {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out, box-shadow 0.2s ease;
  background: var(--bg);
  border-top: 1px solid var(--border);
  pointer-events: none;
}

/* When closed (no .open class) */
.topbar:not(.open) .topbar-panel {
  max-height: 0;
  border-top-color: transparent;
}

/* When open */
.topbar.open .topbar-panel {
  max-height: min(70vh, 520px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  pointer-events: auto;
  box-shadow: 0 12px 32px rgba(0,0,0,0.12);
}

/* Controls grid inside panel */
.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.toggle-row {
  display: flex;
  gap: 6px;
}

/* Panel sections (search, interpret) */
.panel-section {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.section-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 10px;
}

.section-title {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink);
}

.section-hint {
  font-size: 12px;
  color: var(--muted);
}

.search-form,
.interpret-form {
  display: flex;
  gap: 8px;
}

.search-form input,
.interpret-form textarea,
#search-form input,
#interpret-input {
  flex: 1;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 15px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  border-radius: 8px;
}

.interpret-form textarea {
  min-height: 60px;
  resize: vertical;
}

.search-form button,
.interpret-form button,
#search-form button,
#interpret-submit {
  padding: 10px 16px;
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  flex-shrink: 0;
}

.search-form button:hover,
.interpret-form button:hover {
  opacity: 0.9;
}

.interpret-output {
  margin-top: 12px;
  padding: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  line-height: 1.6;
  min-height: 20px;
}

.interpret-output:empty {
  display: none;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}
.control-group select,
.control-group button {
  font-family: inherit;
  font-size: 14px;
}
.chapter-controls {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}
.pill.small {
  padding: 4px 8px;
  line-height: 1;
}
.pill:disabled,
.pill.small:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.pill:disabled:hover {
  border-color: var(--border);
  background: var(--card);
  color: var(--ink);
}
.control-group select {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  padding: 6px 28px 6px 10px;
  border-radius: 999px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.02);
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 14px) 50%, calc(100% - 8px) 50%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  cursor: pointer;
}
.control-group select option {
  background: var(--card);
  color: var(--ink);
}

.toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
}
.toggle button {
  padding: 6px 12px;
  border: none;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, color 0.15s;
  min-width: 44px;
  text-align: center;
}
.toggle button:hover {
  background: var(--border);
}
.toggle button.active {
  background: var(--ink);
  color: var(--bg);
}
.toggle button.active:hover {
  background: var(--ink);
}

.pill {
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.pill:hover {
  border-color: var(--muted);
}
.pill.active,
.pill[aria-pressed="true"] {
  background: var(--ink);
  color: var(--bg);
  border-color: var(--ink);
}
.pill.active:hover,
.pill[aria-pressed="true"]:hover {
  background: var(--muted);
  border-color: var(--muted);
}

.status {
  font-size: 12px;
  color: var(--muted);
}
.results {
  margin-top: 12px;
  display: grid;
  gap: 8px;
  max-height: 35vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  padding: 4px 0;
}
.results a {
  color: var(--accent);
  text-decoration: none;
  cursor: pointer;
  padding: 8px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: block;
  line-height: 1.4;
}
.results a:hover {
  background: var(--border);
}
.results a:active {
  background: var(--ink);
  color: var(--bg);
}
.search-clear {
  justify-self: start;
  padding: 8px 16px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  margin-bottom: 8px;
  font-weight: 500;
}
.search-clear:hover {
  background: var(--border);
}
.search-clear:active {
  background: var(--ink);
  color: var(--bg);
}

.reader {
  padding: 12px 20px 40px;
}
.reader-content {
  max-width: calc(var(--measure-ch) * 1ch);
  margin: 0 auto;
  padding-bottom: 60px;
  position: relative;
  font-style: normal !important;
  font-weight: 400;
  font-synthesis: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.reader-content.scroll-fade::before {
  content: "";
  position: sticky;
  top: 0;
  display: block;
  height: 24px;
  background: linear-gradient(180deg, var(--bg) 0%, transparent 100%);
  pointer-events: none;
  z-index: 2;
}
.verse {
  padding: 2px 0;
  font-style: normal !important;
  font-weight: 400;
}
.verse.study-aligned {
  line-height: 1.9;
}
.reading .verse {
  display: inline;
  padding: 0;
}
.reading .verse::after {
  content: " ";
}
.verse-num {
  font-size: 0.65em;
  vertical-align: super;
  color: var(--muted);
  margin-right: 6px;
}
.reading .verse-num {
  display: none;
}
.chapter-break {
  margin: 18px 0 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--muted);
}

.page-controls {
  max-width: calc(var(--measure-ch) * 1ch);
  margin: 0 auto 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.page-controls button {
  padding: 10px 20px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}
.page-controls button:hover {
  background: var(--ink);
  color: var(--bg);
}
.page-controls button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.page-controls button:disabled:hover {
  background: var(--card);
  color: var(--ink);
}
.chapter-label {
  font-size: 14px;
  color: var(--muted);
}
.hidden { display: none !important; }

.selection-popover {
  position: absolute;
  z-index: 200;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--border);
  padding: 10px;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.15);
  display: grid;
  gap: 6px;
}
.selection-popover select,
.selection-popover button {
  font-family: inherit;
  font-size: 14px;
}
.selection-popover select {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--ink);
  padding: 6px 10px;
  border-radius: 6px;
}
.popover-title {
  font-size: 12px;
  color: var(--muted);
}

.site-footer {
  border-top: 1px solid var(--border);
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 24px;
  font-size: 12px;
  color: var(--muted);
}
.site-footer a {
  color: var(--accent);
  text-decoration: none;
}
.site-footer a:hover {
  text-decoration: underline;
}

/* ===========================================
   STUDY MODE
   =========================================== */

/* Footnote markers */
.fn-marker {
  display: inline-block;
  font-size: 0.6em;
  color: var(--accent);
  cursor: pointer;
  margin-left: 2px;
  font-weight: 600;
  vertical-align: super;
  line-height: 1;
  min-width: 16px;
  min-height: 16px;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
}
.fn-marker:hover {
  text-decoration: underline;
}
.study-footnotes {
  display: inline;
}

.align-token {
  border-radius: 6px;
  padding: 1px 2px;
  transition: background 0.15s ease, color 0.15s ease;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.align-token.active {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
}
.align-token.punct {
  cursor: default;
}

.study-source-units {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 8px 0 10px;
  padding-left: 24px;
}
.study-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 4px 0 10px;
  padding-left: 24px;
}
.study-action {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--accent);
  border-radius: 999px;
  padding: 6px 12px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.explorer-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 8px;
}
.explorer-controls select {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
}
.explorer-results {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}
.explorer-result {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  padding: 10px 12px;
}
.explorer-result-title {
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
}
.explorer-result-meta {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}
.explorer-renderings {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.explorer-rendering-chip {
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--ink);
  font-size: 12px;
  padding: 3px 8px;
}
.source-chip {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  border-radius: 999px;
  padding: 4px 10px;
  font: inherit;
  font-size: 12px;
  line-height: 1.4;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.source-chip.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--card));
  color: var(--accent);
}

/* Scholar cards (inline, collapsible) */
.study-scholars {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 4px 0 10px;
  padding-left: 24px;
}
.scholar-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  line-height: 1.45;
  max-width: 420px;
  overflow: hidden;
}
.scholar-name {
  font-weight: 600;
  color: var(--accent);
  display: block;
  font-size: 12px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.scholar-name::before {
  content: "\25B8 ";
  font-size: 10px;
}
.scholar-card.open .scholar-name::before {
  content: "\25BE ";
}
.scholar-view {
  color: var(--ink);
  display: block;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease;
  margin-top: 0;
}
.scholar-card.open .scholar-view {
  max-height: 500px;
  margin-top: 4px;
}
.scholar-entry {
  margin-bottom: 8px;
  font-size: 13px;
  line-height: 1.5;
}
.scholar-entry:last-child {
  margin-bottom: 0;
}
.scholar-entry strong {
  color: var(--accent);
}

/* Lexeme chips */
.study-lexemes {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 4px 0 8px;
  padding-left: 24px;
}
.lexeme-chip {
  font-size: 14px;
  padding: 4px 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  color: var(--accent);
  font-family: inherit;
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
}
.lexeme-chip:hover {
  background: var(--border);
}

/* Lexeme popover */
.lexeme-popover {
  position: absolute;
  z-index: 300;
  max-width: 280px;
  min-width: 180px;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  box-shadow: 0 8px 28px rgba(0,0,0,0.18);
  font-size: 14px;
  line-height: 1.5;
}
.alignment-popover {
  position: absolute;
  z-index: 300;
  max-width: 280px;
  min-width: 200px;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  box-shadow: 0 8px 28px rgba(0,0,0,0.18);
}
.alignment-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 4px;
}
.alignment-kind {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 6px;
}
.alignment-meta,
.alignment-rendering,
.alignment-handle {
  font-size: 13px;
  line-height: 1.5;
  color: var(--ink);
}
.alignment-same-source {
  margin-top: 10px;
  display: grid;
  gap: 6px;
}
.alignment-same-source-row {
  font-size: 12px;
  color: var(--muted);
}
.alignment-handle {
  margin-top: 8px;
  color: var(--accent);
}
.lex-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 6px;
}
.lex-lang {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 4px;
}
.lex-lemma {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}
.lex-gloss {
  font-size: 14px;
  line-height: 1.5;
}

/* Bottom sheet (footnotes, mobile-first) */
.study-sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 400;
}
.study-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 401;
  background: var(--card);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -8px 32px rgba(0,0,0,0.15);
  transform: translateY(100%);
  transition: transform 0.25s ease;
  max-height: 50vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.study-sheet.visible {
  transform: translateY(0);
}
.study-sheet-handle {
  display: flex;
  justify-content: center;
  padding: 10px 0 6px;
  cursor: grab;
  flex-shrink: 0;
}
.study-sheet-handle span {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}
.study-sheet-content {
  padding: 0 20px 24px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
}
.study-sheet-title {
  font-weight: 600;
  font-size: 15px;
  color: var(--accent);
  margin-bottom: 8px;
}
.study-sheet-body {
  font-size: 15px;
  line-height: 1.6;
  color: var(--ink);
}
.compare-stream {
  border-top: 1px solid var(--border);
  padding-top: 12px;
  margin-top: 12px;
}
.compare-stream:first-child {
  border-top: 0;
  padding-top: 0;
  margin-top: 0;
}
.compare-stream-title {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin-bottom: 6px;
}
.compare-stream-text {
  font-size: 14px;
  line-height: 1.6;
}

/* Hide study elements when study mode is off */
body:not(.study-mode) .fn-marker,
body:not(.study-mode) .study-footnotes,
body:not(.study-mode) .study-source-units,
body:not(.study-mode) .study-actions,
body:not(.study-mode) .study-scholars,
body:not(.study-mode) .study-lexemes {
  display: none;
}

/* In reading mode, hide study annotations to keep clean flow */
.reading .study-scholars,
.reading .study-source-units,
.reading .study-actions,
.reading .study-lexemes {
  display: none;
}

@media (max-width: 900px) {
  .controls { gap: 10px; grid-template-columns: 1fr; }
  .reader { padding: 8px 16px 24px; }
  .topbar.open .topbar-panel {
    max-height: calc(100vh - 56px);
  }
  .search-form,
  .interpret-form {
    flex-direction: column;
  }
  .results {
    max-height: none;
  }
  .study-scholars { padding-left: 0; }
  .study-source-units { padding-left: 0; }
  .study-actions { padding-left: 0; }
  .scholar-card { max-width: 100%; }
  .study-lexemes { padding-left: 0; }
  .lexeme-popover { max-width: calc(100vw - 24px); left: 12px !important; }
  .alignment-popover { max-width: calc(100vw - 24px); left: 12px !important; }
  .study-sheet { max-height: 55vh; }
}
