:root {
  --bg: #faf7f0;
  --panel: #fffdf8;
  --ink: #1c1a16;
  --muted: #8a8172;
  --line: #e7e0d2;
  --accent: #6b4f2a;      /* warm classical brown */
  --accent-soft: #efe6d3;
  --hit: #fff6e6;
  --serif: "Amiri", "Scheherazade New", "Noto Naskh Arabic", Georgia, serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17150f; --panel: #201d16; --ink: #ece6d8; --muted: #9a9080;
    --line: #342f24; --accent: #d8b887; --accent-soft: #2b2619; --hit: #241f14;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font-family: var(--serif); font-size: 18px; line-height: 1.9;
}
button { font-family: inherit; cursor: pointer; color: inherit; }
.muted { color: var(--muted); font-size: 0.82em; }

/* first-run gate */
.gate { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 10; }
.gate-card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 40px; width: min(520px, 90vw); text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.08); }
.brand { font-size: 64px; margin: 0; color: var(--accent); }
.gate-sub { color: var(--muted); margin: 4px 0 24px; }
.progress { height: 10px; background: var(--accent-soft); border-radius: 6px; overflow: hidden; margin: 18px 0 8px; }
.bar { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.dl-status { min-height: 1.4em; color: var(--muted); font-size: 0.85em; }

/* layout */
.app { display: grid; grid-template-columns: 300px 1fr; height: 100vh; }
.sidebar { background: var(--panel); border-inline-start: 1px solid var(--line);
  display: flex; flex-direction: column; overflow: hidden; }
.logo { font-size: 34px; color: var(--accent); text-align: center; padding: 18px 0 8px; }
.search { padding: 0 16px 12px; }
.search input { width: 100%; padding: 11px 14px; border: 1px solid var(--line);
  border-radius: 10px; background: var(--bg); color: var(--ink); font-size: 16px; }
.cats { overflow-y: auto; flex: 1; padding: 4px 8px; }
.cat { display: flex; justify-content: space-between; gap: 8px; width: 100%;
  background: none; border: 0; padding: 8px 10px; border-radius: 8px; font-size: 15px; text-align: start; }
.cat:hover { background: var(--accent-soft); }
.cat.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.cat .count { color: var(--muted); font-size: 0.78em; }
.corpus { padding: 12px 16px; border-top: 1px solid var(--line); color: var(--muted); font-size: 0.8em; }

.main { overflow-y: auto; height: 100vh; }
.list-head { padding: 22px 28px 8px; }
.list-head h2 { margin: 0; font-size: 24px; }
.list { padding: 10px 28px 40px; display: flex; flex-direction: column; gap: 10px; }
.search-count { padding: 4px 2px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px 18px; text-align: start; width: 100%; transition: border-color .15s, transform .05s; }
.card:hover { border-color: var(--accent); }
.card:active { transform: translateY(1px); }
.card-title { font-size: 19px; color: var(--accent); margin-bottom: 4px; }
.card-meta { display: flex; justify-content: space-between; gap: 12px; }
.card.unavailable { opacity: .5; }
.card.hit .snippet { font-size: 16px; color: var(--ink); margin: 6px 0; }
.hit { background: var(--hit); }
.more { text-align: center; padding: 10px 0 40px; }
.more button, .primary, .ghost { border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 20px; background: var(--panel); }
.primary { background: var(--accent); color: #fff; border: 0; font-size: 17px; padding: 12px 28px; }
.ghost:hover, .more button:hover { border-color: var(--accent); }
.ghost:disabled { opacity: .4; cursor: default; }

/* reader */
.reader { display: flex; flex-direction: column; height: 100vh; position: relative; }
.reader-head, .reader-foot { display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 12px 24px; background: var(--panel); border-bottom: 1px solid var(--line); }
.reader-foot { border-top: 1px solid var(--line); border-bottom: 0; }
.reader-title { text-align: center; flex: 1; }
.reader-title #r-title { font-size: 18px; color: var(--accent); display: block; }
.page { flex: 1; overflow-y: auto; padding: 36px clamp(24px, 8vw, 120px);
  font-size: 21px; line-height: 2.2; text-align: justify; }
.page p { margin: 0 0 1.1em; text-indent: 0; }
.toc-panel { position: absolute; inset-block: 0; inset-inline-start: 0; width: min(380px, 80%);
  background: var(--panel); border-inline-end: 1px solid var(--line); overflow-y: auto; padding: 16px; box-shadow: 4px 0 24px rgba(0,0,0,.12); }
.toc-item { display: block; width: 100%; text-align: start; background: none; border: 0;
  padding: 7px 8px; border-radius: 6px; font-size: 15px; }
.toc-item:hover { background: var(--accent-soft); }
.toc-item.lvl2 { padding-inline-start: 22px; } .toc-item.lvl3 { padding-inline-start: 36px; }
.toc-item.lvl4 { padding-inline-start: 50px; color: var(--muted); }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-end: 0; width: 82%; z-index: 5;
    transform: translateX(100%); transition: transform .2s; }
}

/* ---- landing hero (website only) ---- */
.hero {
  text-align: center;
  padding: clamp(48px, 10vh, 120px) 24px 44px;
  border-bottom: 1px solid var(--line);
  background:
    radial-gradient(120% 90% at 50% -10%, var(--accent-soft) 0%, transparent 60%);
}
.hero-brand {
  font-size: clamp(56px, 11vw, 104px);
  line-height: 1; margin: 0 0 10px; color: var(--accent);
  letter-spacing: -0.01em;
}
.hero-tagline { font-size: clamp(17px, 2.4vw, 22px); margin: 0 0 4px; }
.hero-tagline-en { color: var(--muted); font-size: 0.86em; margin: 0 0 28px; font-family: Georgia, serif; }
.hero-search {
  display: flex; gap: 8px; max-width: 620px; margin: 0 auto;
}
.hero-search input {
  flex: 1; padding: 15px 20px; font-size: 19px; font-family: var(--serif);
  border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--ink);
  box-shadow: 0 6px 24px rgba(0,0,0,.06);
}
.hero-search input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.hero-search button {
  padding: 0 26px; font-size: 18px; border: 0; border-radius: 14px;
  background: var(--accent); color: #fff; font-family: var(--serif);
}
.hero-stats { color: var(--muted); font-size: 0.85em; margin: 22px 0 0; }

[hidden]{display:none !important}

/* v2 layout: categories left, 3-col table */
.app { direction: ltr; }
.sidebar { border-inline-start: 0; border-inline-end: 1px solid var(--line); }
.sidebar, .main { direction: rtl; }
.cat { font-size: 15px; }
.cat .count { color: var(--accent); font-size: 0.8em; font-variant-numeric: tabular-nums; background: var(--accent-soft); border-radius: 20px; padding: 1px 9px; min-width: 2.2em; text-align: center; }
.list { gap: 0; padding: 0 28px 40px; }
.book-row { display: grid; grid-template-columns: minmax(0, 2.4fr) minmax(0, 1.7fr) 120px; gap: 18px; align-items: baseline; width: 100%; text-align: start; background: none; border: 0; border-bottom: 1px solid var(--line); padding: 13px 6px; transition: background .12s; }
.book-row:hover { background: var(--accent-soft); }
.book-row.head { position: sticky; top: 0; z-index: 1; background: var(--bg); font-size: 0.8em; font-weight: 700; color: var(--muted); border-bottom: 2px solid var(--line); cursor: default; }
.book-row.head:hover { background: var(--bg); }
.col-title { color: var(--accent); font-size: 18px; line-height: 1.5; }
.book-row.head .col-title { color: var(--muted); }
.col-author { color: var(--ink); font-size: 15px; line-height: 1.5; }
.book-row.head .col-author { color: var(--muted); }
.col-pages { color: var(--muted); font-size: 14px; white-space: nowrap; text-align: end; font-variant-numeric: tabular-nums; }
.book-row.unavailable { opacity: .5; }
.book-row.unavailable:hover { background: none; }
@media (max-width: 760px) { .sidebar { inset-inline-end: auto; inset-inline-start: 0; transform: translateX(-100%); } .book-row { grid-template-columns: minmax(0, 2fr) minmax(0, 1.3fr) 76px; gap: 10px; } .col-title { font-size: 16px; } .col-author { font-size: 13px; } .col-pages { font-size: 12px; } }

/* revert: categories on the right */
.app { direction: rtl; }
.sidebar { border-inline-end: 0; border-inline-start: 1px solid var(--line); }
@media (max-width: 760px) { .sidebar { inset-inline-start: auto; inset-inline-end: 0; transform: translateX(100%); } }

/* reader: persistent clickable TOC side column */
.reader { display: grid; grid-template-columns: minmax(240px, 320px) 1fr; grid-template-rows: auto 1fr auto; grid-template-areas: "toc head" "toc page" "toc foot"; }
.reader-head { grid-area: head; } .page { grid-area: page; } .reader-foot { grid-area: foot; }
.toc-panel { grid-area: toc; position: static; inset: auto; width: auto; max-height: none; box-shadow: none; border-inline-start: 0; border-inline-end: 1px solid var(--line); }
.toc-panel .muted { padding: 16px; display: block; }
.reader.toc-collapsed { grid-template-columns: 1fr; grid-template-areas: "head" "page" "foot"; }
.reader.toc-collapsed .toc-panel { display: none; }
@media (max-width: 760px) { .reader { grid-template-columns: 1fr; grid-template-areas: "head" "toc" "page" "foot"; } .toc-panel { max-height: 34vh; } }
