:root {
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --topbar-h: 52px;
  --toolbar-h: 46px;
  --status-h: 30px;
  --radius: 10px;
  --accent: #7c5cff;
}
body[data-theme="paper"] {
  --bg: #fbf7ef; --surface: #ffffff; --surface-2: #f3ede1; --text: #2a2521; --muted: #7d7266;
  --border: #e4dbcb; --link: #8a5a1e; --shadow: 0 8px 28px rgba(80, 60, 30, .12);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font-ui); font-size: 15px; line-height: 1.5;
  display: flex; flex-direction: column;
}
a { color: var(--link); }
button, input, select, textarea { font: inherit; color: inherit; }
.skip-link { position: absolute; left: -999px; }
.skip-link:focus { left: 8px; top: 8px; z-index: 99; background: var(--surface); padding: 6px 10px; border-radius: 6px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--topbar-h);
  display: flex; align-items: center; gap: 14px; padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .3px; }
.brand-mark { color: var(--accent); }
.mainnav { display: flex; gap: 2px; overflow-x: auto; }
.nav-btn {
  background: none; border: 0; border-radius: 8px; padding: 7px 11px; cursor: pointer;
  color: var(--muted); font-weight: 600; white-space: nowrap;
}
.nav-btn:hover { background: var(--surface-2); color: var(--text); }
.nav-btn.active { background: var(--accent); color: #fff; }
.quickref { margin-left: auto; position: relative; display: flex; }
.quickref input {
  width: 210px; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px 0 0 8px;
  background: var(--surface);
}
.quickref button {
  border: 1px solid var(--border); border-left: 0; border-radius: 0 8px 8px 0;
  background: var(--surface-2); cursor: pointer; padding: 0 11px;
}
.quickref-suggest {
  position: absolute; top: 110%; left: 0; right: 0; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); overflow: hidden;
}
.quickref-suggest div { padding: 7px 10px; cursor: pointer; }
.quickref-suggest div:hover, .quickref-suggest .sel { background: var(--surface-2); }
.quickref-suggest small { color: var(--muted); }

/* ---------- toolbar ---------- */
.toolbar {
  /* -1px overlap: with fractional zoom/DPR the 52px boundary can round to
     52.5, leaving a sub-pixel slit where scrolling text shows through */
  position: sticky; top: calc(var(--topbar-h) - 1px); z-index: 25; min-height: var(--toolbar-h);
  display: flex; align-items: center; gap: 12px; padding: 6px 14px; flex-wrap: wrap;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.toolbar select {
  padding: 5px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2);
}
.toolbar-nav { display: flex; align-items: center; gap: 8px; }
.toolbar-nav button {
  width: 30px; height: 30px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface-2); cursor: pointer; line-height: 1;
}
.toolbar-nav button:hover { background: var(--accent); color: #fff; }
.chapter-title { font-weight: 700; min-width: 130px; text-align: center; }
.toolbar-actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.ghost-btn {
  border: 1px solid var(--border); background: none; border-radius: 8px; padding: 5px 9px;
  cursor: pointer; color: var(--muted); font-weight: 600;
}
.ghost-btn:hover { color: var(--text); background: var(--surface-2); }
.ghost-btn.on { color: var(--text); border-color: var(--accent); }
body[data-view="home"] .toolbar, body:not([data-view="reader"]) .toolbar { display: none; }

/* ---------- main ---------- */
main { flex: 1; padding: 22px 18px 90px; }
.view { max-width: 860px; margin: 0 auto; }
.wide { max-width: 1180px; }

/* ---------- reader ---------- */
.reader { max-width: var(--max-width, 760px); margin: 0 auto; }
.reader-intro {
  font-size: 12.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 1.4px;
  margin-bottom: 18px; text-align: center;
}
.reader-text { font-family: var(--font-serif); font-size: var(--font-size, 19px); line-height: var(--line-height, 1.9); }
.reader-text .para { margin: 0; }
.reader-text .para + .para { margin-top: 1em; }
.verse { position: relative; padding: 1px 4px; border-radius: 5px; cursor: default; }
.verse .vnum {
  font-family: var(--font-ui); font-size: .58em; vertical-align: .55em; color: var(--accent);
  margin: 0 .28em 0 0; font-weight: 700; cursor: pointer; user-select: none;
}
.verse.selected { box-shadow: inset 0 0 0 2px var(--accent); }
.red-letter { color: #a3372c; }
.xref-inline {
  font-family: var(--font-ui); font-size: .62em; vertical-align: .34em; color: var(--accent);
  cursor: pointer; padding: 0 1px; text-decoration: none;
}
.chapter-nav { display: flex; justify-content: space-between; margin-top: 34px; gap: 10px; }
.chapter-nav button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 8px 16px; cursor: pointer; font-weight: 600;
}
.chapter-nav button:hover { border-color: var(--accent); }

.notice {
  background: var(--surface-2); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  padding: 9px 12px; border-radius: 8px; margin: 0 0 18px; font-size: 13.5px;
}
.notice a { cursor: pointer; }

/* ---------- generic cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.grid { display: grid; gap: 14px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
h1.page-title { font-size: 21px; margin: 0 0 16px; }
h2.section { font-size: 16px; margin: 26px 0 10px; letter-spacing: .2px; }
.muted { color: var(--muted); }
.stat { text-align: center; padding: 14px 8px; }
.stat .num { font-size: 26px; font-weight: 800; color: var(--accent); display: block; }
.stat .lbl { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.chip {
  display: inline-block; border-radius: 999px; padding: 2px 9px; font-size: 12px;
  background: var(--surface-2); border: 1px solid var(--border); cursor: pointer; margin: 0 4px 4px 0;
}
.chip.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.pill { border-radius: 999px; padding: 1px 8px; font-size: 11px; background: var(--surface-2); border: 1px solid var(--border); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 8px;
  padding: 7px 13px; cursor: pointer; font-weight: 600;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.danger { color: #c0392b; }
.btn.small { padding: 3px 9px; font-size: 13px; }
input.field, textarea.field, select.field {
  width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface);
}
textarea.field { min-height: 110px; font-family: var(--font-serif); line-height: 1.6; }
label.lbl { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 12px 0 5px; }

/* ---------- search ---------- */
.search-bar { display: flex; gap: 8px; flex-wrap: wrap; }
.search-bar input { flex: 1; min-width: 220px; }
.filters { margin: 12px 0; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.result { border-left: 3px solid transparent; padding: 9px 12px; border-radius: 6px; }
.result:hover { background: var(--surface-2); border-left-color: var(--accent); }
.result .ref { font-weight: 700; cursor: pointer; color: var(--link); }
.result .snippet { font-family: var(--font-serif); font-size: 15.5px; margin-top: 2px; }
.result mark { background: color-mix(in srgb, var(--accent) 30%, transparent); color: inherit; border-radius: 3px; padding: 0 2px; }

/* ---------- statusbar ---------- */
.statusbar {
  position: fixed; bottom: 0; left: 0; right: 0; height: var(--status-h); z-index: 28;
  display: flex; align-items: center; gap: 10px; padding: 0 14px;
  background: var(--surface); border-top: 1px solid var(--border); font-size: 12.5px; color: var(--muted);
}
.statusbar .spacer { flex: 1; }
.badge { border-radius: 999px; padding: 2px 9px; background: var(--surface-2); border: 1px solid var(--border); }
.badge.warn { border-color: #d9a441; color: #9a6b13; }

/* ---------- verse menu ---------- */
.verse-menu {
  position: fixed; z-index: 60; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; min-width: 210px;
}
.verse-menu button {
  display: block; width: 100%; text-align: left; background: none; border: 0;
  padding: 6px 8px; border-radius: 6px; cursor: pointer;
}
.verse-menu button:hover { background: var(--surface-2); }

/* ---------- drawer ---------- */
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(430px, 96vw); z-index: 55;
  background: var(--surface); border-left: 1px solid var(--border); box-shadow: var(--shadow);
  display: flex; flex-direction: column;
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.drawer-head h2 { margin: 0; font-size: 15px; }
.drawer-head button { background: none; border: 0; cursor: pointer; font-size: 16px; color: var(--muted); }
.drawer-body { padding: 14px; overflow: auto; }
.drawer-body .xref { padding: 8px 0; border-bottom: 1px dashed var(--border); }
.drawer-body .xref b { color: var(--link); cursor: pointer; }
.xref-score { margin-left: 7px; font-size: 11px; color: var(--muted); font-family: var(--font-ui); }
.drawer-body .xref p { margin: 3px 0 0; font-family: var(--font-serif); font-size: 14.5px; color: var(--muted); }

/* ---------- heatmap ---------- */
.heat { display: grid; grid-template-columns: repeat(auto-fill, minmax(13px, 1fr)); gap: 3px; }
.heat i { aspect-ratio: 1; border-radius: 3px; background: var(--surface-2); display: block; }
.heat i.l1 { background: color-mix(in srgb, var(--accent) 30%, var(--surface-2)); }
.heat i.l2 { background: color-mix(in srgb, var(--accent) 60%, var(--surface-2)); }
.heat i.l3 { background: var(--accent); }

.book-tabs { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 10px; gap: 8px; position: static; }
  .quickref { margin-left: 0; width: 100%; }
  .quickref input { flex: 1; }
  .toolbar { position: static; }
  main { padding: 16px 12px 80px; }
  .chapter-title { min-width: 90px; }
  .toolbar-actions { margin-left: 0; }
}

/* ------------------------------------------- clickable word annotations --- */
.w-mark {
  cursor: pointer; border-bottom: 1.5px dotted var(--accent);
  border-radius: 3px; padding: 0 1px;
}
.w-mark:hover, .w-mark:focus-visible { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.w-mark.word { border-bottom-color: var(--muted); }

.word-popup {
  position: fixed; z-index: 60; max-width: 360px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 14px;
  font-family: var(--font-ui); font-size: 13.5px; line-height: 1.45;
}
.wp-card { display: block; }
.wp-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.wp-lemma { font-family: var(--font-serif); font-size: 19px; }
.wp-key { color: var(--muted); font-size: 11.5px; letter-spacing: .4px; }
.wp-gloss { margin: 2px 0 6px; font-style: italic; }
.wp-body { margin: 2px 0 6px; color: var(--text); }
.wp-parse { margin: 2px 0 6px; display: flex; flex-wrap: wrap; gap: 6px;
  align-items: baseline; font-size: 12.5px; color: var(--text); }
.wp-parse code, .wp-code {
  font-family: ui-monospace, monospace; font-size: 11px; color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 4px; padding: 0 4px;
}
.wp-paradigm { margin: 6px 0; border: 1px solid var(--border);
  border-radius: 6px; padding: 2px 8px; background: var(--surface-2); }
.wp-paradigm summary { cursor: pointer; font-size: 12px; color: var(--muted);
  padding: 3px 0; }
.wp-grid { border-collapse: collapse; width: 100%; font-size: 12px;
  margin: 4px 0; }
.wp-grid td { padding: 2px 6px; border-top: 1px solid var(--border);
  vertical-align: baseline; }
.wp-grid-code { font-family: ui-monospace, monospace; font-size: 10.5px;
  color: var(--muted); white-space: nowrap; }
.wp-grid-label { color: var(--text); }
.wp-grid-forms { font-family: var(--font-serif); text-align: right; }
.wp-grid-here td { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.wp-grid-here .wp-grid-code { color: var(--accent); font-weight: 600; }
.wp-links, .wp-where { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 5px; align-items: center; }
.wp-chip, .wp-ref {
  font: inherit; font-size: 12px; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; color: inherit;
}
.wp-chip:hover, .wp-ref:hover { border-color: var(--accent); }
.wp-where .muted { font-size: 12px; }
.reader-text.rtl { direction: rtl; text-align: right; }
.reader-text[lang="he"] { font-size: calc(var(--font-size, 19px) * 1.08); }
