:root {
  --bg: #0d1420;
  --bg2: #111c2c;
  --surface: #16233a;
  --surface2: #1c2c47;
  --border: #2a3d5c;
  --border-soft: #22334e;
  --text: #e8eef7;
  --muted: #8ea3bf;
  --accent: #d8a24a;      /* warm gold - the Hebrew highlight */
  --accent-soft: #f0c880;
  --indigo: #7e8ff5;
  --green: #3ddc97;
  --red: #f87171;
  --radius: 10px;
  --shadow: 0 6px 22px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0 0 .4em; line-height: 1.25; font-weight: 650; }
h1 { font-size: 1.15rem; letter-spacing: .01em; }
h2 { font-size: 1.02rem; }
h3 { font-size: .95rem; }
p { margin: 0 0 .8em; }
a { color: var(--accent-soft); }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em;
       background: var(--surface2); padding: 1px 5px; border-radius: 4px; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; flex-wrap: wrap; gap: 14px 24px; align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  background: linear-gradient(180deg, #14213a 0%, var(--bg2) 100%);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 40;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand .mark {
  font-size: 30px; line-height: 1; color: var(--accent);
  background: rgba(216, 162, 74, .1);
  border: 1px solid rgba(216, 162, 74, .35);
  border-radius: 12px; width: 48px; height: 48px;
  display: grid; place-items: center;
}
.brand p { margin: 0; color: var(--muted); font-size: .82rem; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab {
  background: transparent; border: 1px solid transparent; color: var(--muted);
  padding: 9px 15px; border-radius: 8px; cursor: pointer; font-size: .88rem;
  font-family: inherit; transition: color .18s, background .18s, border-color .18s;
  min-height: 40px;
}
.tab:hover { color: var(--text); background: var(--surface); }
.tab.active { color: #17202f; background: var(--accent); border-color: var(--accent); font-weight: 600; }
a.tab.signout { text-decoration: none; display: inline-flex; align-items: center;
                margin-left: 8px; border-color: var(--border-soft); font-size: .8rem; }
a.tab.signout:hover { color: var(--red); border-color: var(--red); background: transparent; }
.tab:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 2px solid var(--indigo); outline-offset: 2px;
}

main { padding: 20px 22px 60px; max-width: 1500px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 22px; align-items: start; }
@media (max-width: 1000px) { .layout { grid-template-columns: 1fr; } }

.sidebar {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 6px 16px 18px; position: sticky; top: 96px; max-height: calc(100vh - 118px);
  overflow-y: auto;
}
@media (max-width: 1000px) { .sidebar { position: static; max-height: none; } }

fieldset { border: 0; border-top: 1px solid var(--border-soft); margin: 0; padding: 14px 0 4px; }
fieldset:first-of-type { border-top: 0; }
legend { font-size: .72rem; text-transform: uppercase; letter-spacing: .09em; color: var(--accent);
         font-weight: 700; padding: 0; }

label { display: block; font-size: .78rem; color: var(--muted); margin: 10px 0 4px; }
input[type=text], input[type=number], input[type=password], select {
  width: 100%; background: var(--bg2); color: var(--text);
  border: 1px solid var(--border); border-radius: 7px; padding: 9px 10px;
  font: inherit; font-size: .9rem; min-height: 40px;
}
input::placeholder { color: #5d7391; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { font-size: .74rem; color: var(--muted); margin: 6px 0 0; line-height: 1.45; }

.preset-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 8px; }
.chip {
  background: var(--surface2); border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 6px 12px; font-size: .78rem; cursor: pointer;
  font-family: inherit; transition: border-color .18s, color .18s;
}
.chip:hover { border-color: var(--accent); color: var(--accent-soft); }
.chip.ghost { color: var(--muted); }

.book-list {
  max-height: 210px; overflow-y: auto; border: 1px solid var(--border-soft);
  border-radius: 8px; padding: 6px; background: var(--bg2);
}
.book-list .sec { font-size: .68rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted); padding: 7px 6px 3px; position: sticky; top: -6px; background: var(--bg2); }
.book-row { display: flex; align-items: center; gap: 8px; padding: 4px 6px; border-radius: 6px; }
.book-row:hover { background: var(--surface2); }
.book-row label { margin: 0; color: var(--text); font-size: .82rem; cursor: pointer; flex: 1;
                  display: flex; justify-content: space-between; gap: 10px; }
.book-row .he { color: var(--muted); font-size: .85rem; }
.book-row.disabled { opacity: .38; }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }

.term-row { display: flex; gap: 6px; align-items: center; margin-bottom: 7px; }
.term-row input {
  direction: rtl; text-align: right; font-size: 1.15rem;
  font-family: "Times New Roman", "Frank Ruehl", "David", serif; letter-spacing: .02em;
}
.term-row .kill {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  border-radius: 7px; width: 40px; min-height: 40px; cursor: pointer; font-size: 1rem;
}
.term-row .kill:hover { border-color: var(--red); color: var(--red); }
.term-row input.active-kb { border-color: var(--accent); }

/* ---------- multi-word split hint ---------- */
.split-hint {
  background: rgba(216,162,74,.09); border-left: 3px solid var(--accent);
  border-radius: 0 7px 7px 0; padding: 9px 11px; margin: 4px 0 8px; font-size: .76rem;
  color: var(--muted); line-height: 1.5;
}
.split-hint .he { font-family: "Times New Roman", serif; font-size: 1rem; color: var(--accent-soft); }
.split-hint button { margin-top: 7px; }

/* ---------- on-screen Hebrew keyboard ---------- */
.kb {
  margin-top: 12px; background: var(--bg2); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px;
}
.kb[hidden] { display: none; }
.kb-head {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center; justify-content: space-between;
  font-size: .68rem; color: var(--muted); margin-bottom: 8px;
}
.kb-modes { display: flex; gap: 4px; }
.kb-mode {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  border-radius: 999px; padding: 3px 9px; font-size: .66rem; cursor: pointer; font-family: inherit;
}
.kb-mode.active { background: var(--accent); border-color: var(--accent); color: #17202f; font-weight: 600; }

.kb-keys { display: grid; grid-template-columns: repeat(6, 1fr); gap: 5px; direction: rtl; }
.kb-key {
  background: var(--surface2); border: 1px solid var(--border); border-radius: 7px;
  color: var(--text); cursor: pointer; font-family: inherit;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 6px 2px; min-height: 44px; transition: background .15s, border-color .15s;
}
.kb-key:hover { border-color: var(--accent); background: var(--surface); }
.kb-key:active { background: var(--accent); color: #17202f; }
.kb-key .l { font-family: "Times New Roman", "Frank Ruehl", serif; font-size: 1.3rem; line-height: 1; color: var(--accent); }
.kb-key .n { font-size: .58rem; color: #6d84a3; margin-top: 2px; letter-spacing: .02em; }
.kb-key:hover .l { color: var(--accent-soft); }

.kb-finals { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border-soft); }
.kb-finals-label { display: block; font-size: .62rem; color: #6d84a3; margin-bottom: 5px; }
.kb-finals #kb-final-keys { display: flex; gap: 5px; direction: rtl; }
.kb-finals .kb-key { flex: 1; min-height: 34px; opacity: .75; }
.kb-finals .kb-key .l { font-size: 1.05rem; }

.kb-actions { display: flex; gap: 6px; margin-top: 9px; }
.kb-actions .btn { flex: 1; padding: 6px 4px; font-size: .72rem; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border-radius: 8px; padding: 10px 16px; font: inherit; font-size: .87rem;
  cursor: pointer; border: 1px solid var(--border); background: var(--surface2);
  color: var(--text); text-decoration: none; min-height: 40px;
  transition: background .18s, border-color .18s, color .18s, opacity .18s;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #17202f; font-weight: 650; }
.btn.primary:hover { background: var(--accent-soft); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--accent-soft); }
.btn.small { padding: 6px 11px; font-size: .78rem; min-height: 34px; }
.btn.block { width: 100%; margin-top: 16px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

.status { font-size: .8rem; color: var(--muted); margin-top: 10px; min-height: 1.2em; }
.status.error { color: var(--red); }

/* ---------- results ---------- */
.content { min-width: 0; }
.summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 13px 15px;
}
.stat .k { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.stat .v { font-size: 1.35rem; font-weight: 650; margin-top: 3px; }
.stat .v.he { font-family: "Times New Roman", serif; font-size: 1.6rem; color: var(--accent); direction: rtl; }
.stat .sub { font-size: .74rem; color: var(--muted); margin-top: 2px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px; margin-bottom: 18px;
}
.card.wide { max-width: 960px; }
.card-head { display: flex; flex-wrap: wrap; gap: 10px; align-items: baseline; justify-content: space-between; margin-bottom: 12px; }
.card-head .he { font-family: "Times New Roman", serif; font-size: 1.5rem; color: var(--accent); direction: rtl; }
.card-head .meta { font-size: .78rem; color: var(--muted); }

.tbl-wrap {
  overflow: auto; border: 1px solid var(--border-soft); border-radius: 8px;
  max-height: 560px;   /* keep long result sets scrollable instead of endless */
}
table { border-collapse: collapse; width: 100%; font-size: .84rem; }
th, td { text-align: left; padding: 8px 11px; border-bottom: 1px solid var(--border-soft); white-space: nowrap; }
th { background: var(--bg2); color: var(--muted); font-size: .7rem; text-transform: uppercase;
     letter-spacing: .07em; font-weight: 600; position: sticky; top: 0; }
tbody tr:hover { background: var(--surface2); }
td.he { font-family: "Times New Roman", serif; font-size: 1.15rem; color: var(--accent-soft); direction: rtl; text-align: right; }
td.verse { white-space: normal; min-width: 260px; max-width: 460px; direction: rtl; text-align: right;
           font-family: "Times New Roman", serif; font-size: 1rem; color: var(--muted); }
td.num { font-variant-numeric: tabular-nums; }
td.he.glyph { text-align: center; width: 70px; font-size: 1.45rem; color: var(--accent); }

.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .7rem; font-weight: 600; }
.tag.fwd { background: rgba(61, 220, 151, .14); color: var(--green); }
.tag.rev { background: rgba(126, 143, 245, .16); color: var(--indigo); }

.pager { display: flex; gap: 8px; align-items: center; margin-top: 10px; font-size: .8rem; color: var(--muted); }
.empty { color: var(--muted); font-size: .88rem; padding: 26px 4px; }
.banner {
  border-left: 3px solid var(--accent); background: rgba(216, 162, 74, .07);
  padding: 10px 14px; border-radius: 0 8px 8px 0; font-size: .82rem; color: var(--muted); margin-bottom: 14px;
}

/* ---------- matrix ---------- */
.matrix-toolbar {
  display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 16px; margin-bottom: 14px;
}
.matrix-toolbar > div { min-width: 0; }
.matrix-toolbar input { width: 100px; }
.matrix-toolbar label { margin-top: 0; }
.grow { flex: 1; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; font-size: .8rem; }
.legend .item { display: flex; align-items: center; gap: 6px; }
.legend .sw { width: 14px; height: 14px; border-radius: 4px; }
.legend .he { font-family: "Times New Roman", serif; font-size: 1.15rem; direction: rtl; }

.matrix-meta { font-size: .8rem; color: var(--muted); margin-bottom: 10px; }
.matrix-host {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; overflow: auto;
}
.grid { direction: rtl; display: inline-block; font-family: "Times New Roman", "Frank Ruehl", serif; }
.grid .r { display: flex; }
.grid .c {
  width: 1.5em; height: 1.5em; display: grid; place-items: center;
  font-size: 1.02rem; color: #96a9c4; border-radius: 3px; cursor: default;
  transition: background .12s;
}
.grid .c.on { cursor: pointer; }
.grid .c:hover { background: var(--surface2); }
.grid .c.h0 { background: rgba(216,162,74,.9);  color: #14203a; font-weight: 700; }
.grid .c.h1 { background: rgba(61,220,151,.85); color: #0d2019; font-weight: 700; }
.grid .c.h2 { background: rgba(126,143,245,.85); color: #101433; font-weight: 700; }
.grid .c.h3 { background: rgba(248,113,113,.85); color: #2a0f0f; font-weight: 700; }
.grid .c.h4 { background: rgba(236,124,214,.85); color: #2d0f27; font-weight: 700; }
.grid .c.h5 { background: rgba(94,208,235,.85);  color: #06222b; font-weight: 700; }

.verse-popup {
  position: fixed; bottom: 18px; right: 18px; max-width: 460px; z-index: 60;
  background: var(--surface2); border: 1px solid var(--accent); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); font-size: .86rem;
}
.verse-popup .close { float: left; background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1.1rem; }
.verse-popup .v { direction: rtl; text-align: right; font-family: "Times New Roman", serif;
                  font-size: 1.05rem; margin: 6px 0; color: var(--muted); }
.verse-popup .v.cur { color: var(--accent-soft); }
.verse-popup .ref { font-size: .74rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }

/* ---------- clusters ---------- */
.cluster-scroll { max-height: 560px; overflow-y: auto; padding-right: 4px; }
.cluster {
  background: var(--surface2); border: 1px solid var(--border); border-left: 3px solid var(--green);
  border-radius: 0 var(--radius) var(--radius) 0; padding: 12px 16px; margin-bottom: 10px;
}
.cluster .top { display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; align-items: center; }
.cluster .terms { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; font-size: .8rem; color: var(--muted); }
.cluster .terms .he { font-family: "Times New Roman", serif; font-size: 1.1rem; color: var(--accent-soft); direction: rtl; }

/* ---------- gematria + stats ---------- */
.hebrew-input { direction: rtl; text-align: right; font-size: 1.5rem;
                font-family: "Times New Roman", "Frank Ruehl", serif; padding: 12px; }
.gem-out { margin-top: 14px; }
.gem-word { border-top: 1px solid var(--border-soft); padding: 10px 0; display: flex;
            flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; }
.gem-word .w { font-family: "Times New Roman", serif; font-size: 1.5rem; color: var(--accent); direction: rtl; }
.gem-word .parts { display: flex; flex-direction: row-reverse; gap: 6px; flex-wrap: wrap; }
.gem-word .pair {
  display: flex; flex-direction: column; align-items: center; min-width: 30px;
  background: var(--bg2); border: 1px solid var(--border-soft); border-radius: 6px; padding: 3px 6px;
}
.gem-word .pair b { font-family: "Times New Roman", serif; font-size: 1.05rem; color: var(--accent-soft); font-weight: 400; }
.gem-word .pair i { font-style: normal; font-size: .68rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.gem-word .val { font-size: 1.3rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.gem-total { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border);
             display: flex; justify-content: space-between; font-size: 1rem; }

.letter-table { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 8px; }
.letter-table .lt {
  background: var(--bg2); border: 1px solid var(--border-soft); border-radius: 8px;
  padding: 9px; text-align: center;
}
.letter-table .lt .l { font-family: "Times New Roman", serif; font-size: 1.5rem; color: var(--accent); }
.letter-table .lt .v { font-size: .78rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.letter-table .lt .n { font-size: .68rem; color: #6d84a3; }

.export-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin-top: 10px; }
.export-row .inline { display: flex; gap: 8px; align-items: center; }
.export-row .inline label { margin: 0; white-space: nowrap; }
.export-row .inline input { width: 90px; }

.prose { line-height: 1.6; }
.prose ul { padding-left: 20px; color: var(--muted); }
.prose li { margin-bottom: 6px; }
.prose .credit { font-size: .8rem; color: var(--muted); border-top: 1px solid var(--border-soft); padding-top: 12px; margin-top: 18px; }

.spin {
  display: inline-block; width: 13px; height: 13px; border: 2px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: sp .7s linear infinite;
  vertical-align: -2px; margin-right: 7px;
}
@keyframes sp { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
