:root {
  --color-page-bg: #214d36;
  --color-surface-primary: #f8f1e3;
  --color-surface-secondary: #efe3cf;
  --color-surface-tertiary: #fffaf1;
  --color-text-main: #16120d;
  --color-text-muted: #5d5345;
  --color-text-title: #214d36;
  --color-text-accent: #732b31;
  --color-border: #c9b79e;
  --color-button-text: #fdf9f2;
  --color-shadow: rgba(11, 21, 16, 0.18);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  background: var(--color-page-bg);
  color: var(--color-text-main);
}
.layout { max-width: 1100px; margin: 0 auto; padding: 24px; display: grid; gap: 16px; }

h1, h2, h3, h4, .chapter-title {
  color: var(--color-text-title);
}

.topbar { display: grid; grid-template-columns: 1fr minmax(340px, 2fr) 1fr; align-items: center; gap: 10px; }
.topbar-zone { display: flex; align-items: center; gap: 6px; }
.topbar-left { justify-content: flex-start; }
.topbar-center { justify-content: center; }
.topbar-right { justify-content: flex-end; }

.topbar-action {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 5px 9px;
  background: var(--color-surface-primary);
  color: var(--color-text-title);
  font-size: 0.9rem;
  cursor: pointer;
  width: auto;
  margin-top: 0;
  align-self: center;
  white-space: nowrap;
}
.topbar-action:hover { border-color: var(--color-text-accent); color: var(--color-text-accent); }
.topbar-action svg { width: 16px; height: 16px; display: block; }

.topbar-expr {
  min-width: 260px;
  max-width: 420px;
  width: 45vw;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 6px 10px;
  background: var(--color-surface-primary);
  color: var(--color-text-main);
  margin-top: 0;
}


.panel { background: var(--color-surface-primary); border: 1px solid var(--color-border); border-radius: 12px; padding: 16px; box-shadow: 0 10px 24px var(--color-shadow); }
.panel.plain-panel { background: transparent; border: 0; border-radius: 0; padding: 0; }
.muted, small { color: var(--color-text-muted); }
.hidden { display: none !important; }

.grid { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); margin-bottom: 12px; }
input, select, button { width: 100%; margin-top: 4px; padding: 8px; border-radius: 8px; border: 1px solid var(--color-border); }
input, select { background: var(--color-surface-tertiary); color: var(--color-text-main); }
button { background: var(--color-text-title); color: var(--color-button-text); border: none; cursor: pointer; align-self: end; }
button:hover { background: var(--color-text-accent); }

.result { border-top: 1px solid var(--color-border); margin-top: 8px; padding-top: 8px; max-height: 420px; overflow: auto; }
.result.no-scroll { max-height: none; overflow: visible; }
.result.no-scroll { border-top: 0; margin-top: 0; padding-top: 0; background: transparent; }
.summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 10px; }
.summary-card { border: 1px solid var(--color-border); border-radius: 10px; padding: 10px; background: var(--color-surface-secondary); }
.summary-card h3 { margin: 0 0 6px; }
.edition-card-link { display: block; color: inherit; text-decoration: none; cursor: pointer; }
.edition-card-link:hover { border-color: var(--color-text-accent); }
.books-list { word-break: break-word; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.badge { border: 1px solid var(--color-border); border-radius: 999px; padding: 2px 8px; background: var(--color-surface-tertiary); color: var(--color-text-accent); font-size: 0.9rem; }
.edition-link { color: var(--color-text-accent); text-decoration: none; }
.edition-link:hover { text-decoration: underline; }

.book-group { margin: 10px 0; }
.book-group h4 { margin: 0 0 6px; color: var(--color-text-accent); }
.book-links { display: flex; flex-wrap: wrap; gap: 8px; }
.edition-divider { border: 0; border-top: 1px solid var(--color-border); margin: 18px 0 12px; }
.edition-meta { color: var(--color-text-muted); display: grid; gap: 10px; }
.edition-meta p { margin: 0; }
.edition-source-link { color: var(--color-text-accent); text-decoration: none; word-break: break-word; }
.edition-source-link:hover { text-decoration: underline; }
.editions-summary-label {
  color: var(--color-text-accent);
  font-size: 1.15rem;
  font-weight: 700;
}
.book-link-wrap { position: relative; display: inline-flex; z-index: 0; }
.book-link-trigger {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 4px 10px;
  color: var(--color-text-accent);
  background: var(--color-surface-tertiary);
  cursor: pointer;
}
.book-link-wrap.is-open { z-index: 20; }
.book-link-wrap.is-open .book-chapter-menu { display: block; }

.verse-line { display: flex; align-items: flex-start; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--color-border); }
.verse-ref {
  flex: 0 0 4ch;
  min-width: 4ch;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--color-text-accent);
}
.verse-words { display: inline-flex; flex-wrap: wrap; align-items: flex-start; gap: 6px; }
.verse-line { font-family: "Noto Sans", Georgia, "Times New Roman", serif; }
.word-unit {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  vertical-align: top;
}
.word-chip { display: inline-block; padding: 4px 8px; border: none; border-radius: 999px; background: transparent; color: var(--color-text-main); cursor: default; }
.word-chip-link { color: inherit; text-decoration: none; }
.word-gloss {
  display: block;
  max-width: 14ch;
  color: var(--color-text-muted);
  font-size: 0.72rem;
  line-height: 1.2;
  text-align: center;
  text-wrap: balance;
  direction: ltr;
  unicode-bidi: isolate;
}
.word-chip.has-tooltip {
  color: var(--color-text-accent);
  cursor: pointer;
  text-decoration: none;
}
.word-chip.has-tooltip:hover {
  text-decoration: underline;
  text-underline-offset: 0.18em;
}
.word-chip.has-tooltip:focus-visible {
  outline: 1px solid var(--color-text-accent);
  outline-offset: 2px;
}
.verse-line.rtl { direction: rtl; }
.verse-line.rtl,
.verse-line.rtl .verse-words,
.verse-line.rtl .word-chip {
  font-family: "Noto Sans Hebrew", "Noto Sans", Georgia, "Times New Roman", serif;
}
.verse-line.rtl .verse-ref { min-width: 3.5ch; }

.word-tooltip {
  position: absolute;
  z-index: 40;
  width: min(360px, calc(100vw - 24px));
  display: grid;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: 12px;
  background: var(--color-surface-primary);
  color: var(--color-text-main);
  box-shadow: 0 10px 24px var(--color-shadow);
  pointer-events: auto;
  font-family: "Noto Sans", "Noto Sans Hebrew", Georgia, "Times New Roman", serif;
  font-size: 1rem;
}
.word-tooltip::after {
  content: "";
  position: absolute;
  left: 50%;
  width: 12px;
  height: 12px;
  background: var(--color-surface-primary);
  border-left: 1px solid var(--color-border);
  border-top: 1px solid var(--color-border);
  transform: translateX(-50%) rotate(45deg);
}
.word-tooltip[data-placement="top"]::after {
  bottom: -7px;
  transform: translateX(-50%) rotate(225deg);
}
.word-tooltip[data-placement="bottom"]::after {
  top: -7px;
  transform: translateX(-50%) rotate(45deg);
}
.word-tooltip-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.word-tooltip p,
.sheet-markup p {
  margin: 0;
  line-height: 1.5;
}
.word-tooltip a,
.sheet-markup a {
  color: var(--color-text-accent);
}
.word-tooltip a:hover,
.sheet-markup a:hover {
  text-decoration: underline;
}
.word-tooltip-label {
  flex: 0 0 auto;
  margin-top: 1px;
}
.word-tooltip-value {
  min-width: 0;
  font-size: 0.9rem;
  line-height: 1.4;
  word-break: break-word;
}

.chapter-section {
  margin: 10px 0 18px;
  border: 1px solid var(--color-border);
  border-radius: 10px;
  background: var(--color-surface-secondary);
  padding: 10px;
}
.sheet-card {
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--color-border);
  border-radius: 12px;
  background: var(--color-surface-primary);
  box-shadow: 0 10px 24px var(--color-shadow);
}
.sheet-head h3 {
  margin: 0;
  color: var(--color-text-accent);
}
.sheet-markup {
  display: grid;
  gap: 10px;
  font-family: "Noto Sans", "Noto Sans Hebrew", Georgia, "Times New Roman", serif;
  font-size: 1rem;
}
.sheet-block {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--color-border);
  border-radius: 10px;
  background: var(--color-surface-secondary);
}
.sheet-rich-markup {
  display: block;
  margin-top: 6px;
  padding-top: 10px;
  border-top: 1px solid color-mix(in srgb, var(--color-border) 75%, transparent);
  line-height: 1.65;
  overflow-wrap: anywhere;
}
.sheet-rich-markup entry,
.sheet-rich-markup sense,
.sheet-rich-markup lex\:entry,
.sheet-rich-markup lex\:sense {
  display: block;
}
.sheet-rich-markup entry,
.sheet-rich-markup lex\:entry {
  color: var(--color-text-main);
}
.sheet-rich-markup sense,
.sheet-rich-markup lex\:sense {
  margin-top: 0.8rem;
  margin-left: calc(var(--sense-depth, 0) * 0.5rem);
  padding-left: 0.9rem;
  border-left: 2px solid color-mix(in srgb, var(--color-border) 70%, transparent);
}
.sheet-rich-markup entry > sense:first-of-type,
.sheet-rich-markup entry > lex\:sense:first-of-type,
.sheet-rich-markup lex\:entry > lex\:sense:first-of-type,
.sheet-rich-markup lex\:entry > sense:first-of-type,
.sheet-rich-markup sense > sense:first-of-type,
.sheet-rich-markup sense > lex\:sense:first-of-type,
.sheet-rich-markup lex\:sense > lex\:sense:first-of-type,
.sheet-rich-markup lex\:sense > sense:first-of-type {
  margin-top: 0.5rem;
}
.sheet-rich-markup w,
.sheet-rich-markup lex\:w {
  font-weight: 700;
  color: var(--color-text-title);
  font-family: "Noto Sans Hebrew", "Noto Sans", Georgia, "Times New Roman", serif;
}
.sheet-rich-markup def,
.sheet-rich-markup lex\:def {
  font-weight: 700;
  color: var(--color-text-accent);
}
.sheet-rich-markup pos,
.sheet-rich-markup status,
.sheet-rich-markup lex\:pos,
.sheet-rich-markup lex\:status {
  display: none;
}
.sheet-rich-markup ref,
.sheet-rich-markup lex\:ref {
  color: var(--color-text-accent);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.sheet-rich-markup foreign,
.sheet-rich-markup em,
.sheet-rich-markup lex\:foreign,
.sheet-rich-markup lex\:em {
  font-style: italic;
}
.sheet-rich-markup[dir="rtl"] {
  text-align: right;
}
.chapter-title { margin: 0 0 8px; padding-bottom: 4px; border-bottom: 1px solid var(--color-border); }
.chapter-title-book,
.chapter-title-number {
  color: var(--color-text-accent);
}

.settings-wrap { position: relative; }
.settings-menu { position: absolute; top: 110%; right: 0; min-width: 320px; max-width: min(320px, calc(100vw - 32px)); background: var(--color-surface-primary); border: 1px solid var(--color-border); border-radius: 10px; padding: 10px; box-shadow: 0 10px 20px var(--color-shadow); z-index: 10; }
.settings-font { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.font-size-controls { display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; }
.font-size-btn { width: auto; min-width: 2.5rem; margin-top: 0; padding: 4px 8px; border-radius: 999px; }
.font-size-value { min-width: 4.5rem; text-align: center; font-variant-numeric: tabular-nums; color: var(--color-text-accent); }
.settings-lang { display: flex; gap: 6px; }
.settings-lang-top { justify-content: flex-end; margin-bottom: 8px; }
.settings-switches { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; }
.settings-switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; white-space: nowrap; width: 100%; }
.settings-switch input[type="checkbox"] { margin: 0; width: auto; flex: 0 0 auto; }
.settings-switch span { white-space: nowrap; line-height: 1.2; }
.settings-flag { border: 1px solid var(--color-border); background: var(--color-surface-tertiary); border-radius: 999px; padding: 3px 7px; cursor: pointer; color: var(--color-text-title); filter: grayscale(0.9) saturate(0.25) opacity(0.7); transition: filter 160ms ease, opacity 160ms ease, border-color 160ms ease; }
.settings-flag.active { filter: none; opacity: 1; border-color: var(--color-text-title); }
.icon-only { padding: 5px 8px; }

.expr-group { display: inline-flex; align-items: center; border: 1px solid var(--color-border); border-radius: 999px; background: var(--color-surface-primary); padding: 2px; min-width: 320px; max-width: 620px; width: 100%; }
.expr-group .topbar-expr { border: none; background: transparent; width: 100%; min-width: 0; padding: 6px 10px; }
.expr-group .topbar-expr:focus { outline: none; }
.expr-group .topbar-action { border: none; background: transparent; margin-left: auto; }

.chapter-head { position: relative; display: inline-block; }
.chapter-title { cursor: default; }
.chapter-menu { display: none; position: absolute; top: calc(100% + 4px); left: 0; min-width: 320px; max-width: min(560px, calc(100vw - 32px)); background: var(--color-surface-primary); border: 1px solid var(--color-border); border-radius: 10px; padding: 8px; box-shadow: 0 10px 20px var(--color-shadow); z-index: 5; }
.chapter-head:hover .chapter-menu { display: block; }
.chapter-menu-nav { display: flex; gap: 6px; margin-bottom: 8px; }
.chapter-menu-nav .chapter-menu-item {
  padding: 0 10px;
  min-height: 30px;
  background: var(--color-surface-tertiary);
}
.chapter-menu-list,
.book-chapter-menu-list {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 4px;
}
.chapter-menu-item,
.book-chapter-item {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  text-decoration: none;
  color: var(--color-text-accent);
  font-variant-numeric: tabular-nums;
  border-radius: 4px;
}
.chapter-menu-item:hover,
.book-chapter-item:hover {
  background: var(--color-surface-tertiary);
  color: var(--color-text-accent);
}
.chapter-menu-item.is-disabled { opacity: 0.45; pointer-events: none; }

.book-chapter-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  width: min(360px, calc(100vw - 32px));
  min-width: 0;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 3rem);
  overflow: auto;
  z-index: 30;
}

@media (max-width: 720px) {
  .layout {
    padding: 12px;
  }

  .topbar {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "left right"
      "center center";
    align-items: start;
  }

  .topbar-left { grid-area: left; }
  .topbar-center { grid-area: center; justify-content: stretch; }
  .topbar-right { grid-area: right; }
  .topbar-zone { min-width: 0; }

  .topbar-expr {
    min-width: 0;
    max-width: none;
    width: 100%;
  }

  .expr-group {
    min-width: 0;
    max-width: none;
    width: 100%;
  }

  .grid,
  .summary-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .settings-menu {
    min-width: 0;
    width: min(320px, calc(100vw - 24px));
  }

  .settings-font {
    align-items: flex-start;
    flex-direction: column;
  }

  .settings-switch,
  .settings-switch span {
    white-space: normal;
  }

  .chapter-menu {
    min-width: 0;
    width: min(560px, calc(100vw - 24px));
    max-width: calc(100vw - 24px);
  }

  .chapter-menu-list,
  .book-chapter-menu-list {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }
}

body.dark-mode {
  --color-page-bg: #102218;
  --color-surface-primary: #1b241e;
  --color-surface-secondary: #222d26;
  --color-surface-tertiary: #2a362f;
  --color-text-main: #f0eadf;
  --color-text-muted: #b6b0a4;
  --color-text-title: #8fb79f;
  --color-text-accent: #c7868d;
  --color-border: #47564b;
  --color-button-text: #0f1712;
  --color-shadow: rgba(0, 0, 0, 0.3);
}
body.dark-mode .topbar-expr::placeholder { color: var(--color-text-muted); opacity: 1; }
