:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --page: #ffffff;
  --text: #22201c;
  --muted: #777167;
  --border: #e2ddd3;
  --accent: #b4532a;
  --accent-text: #ffffff;
  --soft: #f0ebe2;
  --doc-font: 19px;
  --ui: "Segoe UI", "Heebo", "Arial Hebrew", system-ui, sans-serif;
  --serif: "Frank Ruhl Libre", "David", "Times New Roman", serif;
}
[data-theme="sepia"] {
  --bg: #efe6d2; --surface: #f8f0de; --page: #f8f0de; --text: #40342a;
  --muted: #86765f; --border: #dccfb4; --soft: #e9dec6;
}
[data-theme="dark"] {
  --bg: #161617; --surface: #202022; --page: #1b1b1d; --text: #dedbd4;
  --muted: #96928a; --border: #333336; --accent: #e08a5f; --accent-text: #1b1b1d; --soft: #2a2a2d;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-family: var(--ui); font-size: 15px; }
.hidden { display: none !important; }
.spacer { flex: 1; }

button, select, input, textarea { font: inherit; color: inherit; }
button {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px; cursor: pointer;
}
button:hover { background: var(--soft); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.icon { padding: 6px 10px; white-space: nowrap; direction: ltr; }
button.icon.small { padding: 0 6px; border: none; background: none; }
button.link { border: none; background: none; color: var(--muted); padding: 2px 4px; font-size: 13px; }
button:disabled { opacity: .5; cursor: default; }
input[type="search"], input[type="password"], dialog input[type="text"], select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- library ---------- */
#library { max-width: 1180px; margin: 0 auto; padding: 24px 20px 60px; min-height: 100%; }
.lib-header { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.lib-header h1 { margin: 0; font-size: 26px; font-family: var(--serif); flex: 1; }
.lib-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.filters { display: flex; gap: 6px; margin: 20px 0 18px; flex-wrap: wrap; }
.filters button { border-radius: 999px; padding: 5px 16px; }
.filters button.active { background: var(--text); color: var(--bg); border-color: var(--text); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 16px; display: flex; flex-direction: column; gap: 10px; cursor: pointer;
  transition: transform .12s, box-shadow .12s;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(0,0,0,.08); }
.card-top { display: flex; align-items: center; gap: 8px; }
.badge {
  font-size: 11px; font-weight: 600; letter-spacing: .04em; padding: 2px 7px; border-radius: 5px;
  background: var(--soft); color: var(--muted);
}
.card select.cat { border: none; background: none; padding: 0; color: var(--muted); font-size: 13px; cursor: pointer; }
.card-title {
  font-family: var(--serif); font-size: 19px; line-height: 1.3; font-weight: 600;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
  min-height: 2.6em; unicode-bidi: plaintext; text-align: start;
}
.bar { height: 5px; background: var(--soft); border-radius: 3px; overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); }
.card-meta { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); }
.card-meta button { border: none; background: none; padding: 2px 5px; color: var(--muted); opacity: 0; }
.card:hover .card-meta button, .card-meta button:focus-visible { opacity: 1; }
.card-meta button:hover { color: var(--accent); }
/* side panel tabs, search results, highlights list */
.side-tabs { display: flex; align-items: center; gap: 2px; padding: 6px 8px 0; border-bottom: 1px solid var(--border); }
.side-tabs button[data-pane] { border: none; background: none; border-radius: 8px 8px 0 0; padding: 7px 12px; color: var(--muted); border-bottom: 2px solid transparent; }
.side-tabs button[data-pane].active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }
.pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.pane[hidden] { display: none; }
.find-box { display: flex; gap: 6px; padding: 12px 12px 6px; }
.find-box input { flex: 1; min-width: 0; unicode-bidi: plaintext; }
.find-all { display: flex; align-items: center; gap: 6px; padding: 2px 14px; font-size: 13px; color: var(--muted); }
.find-all[hidden] { display: none; }
#findStatus, .pane-hint { padding: 4px 14px; margin: 0; }
.results { flex: 1; overflow: auto; padding: 4px 8px 12px; }
.results h4 { margin: 12px 6px 4px; font-size: 13px; color: var(--muted); font-weight: 600; unicode-bidi: plaintext; text-align: start; }
.result { display: block; width: 100%; text-align: start; border: none; background: none; border-radius: 8px; padding: 7px 8px; line-height: 1.5; font-size: 14px; unicode-bidi: plaintext; }
.result:hover { background: var(--soft); }
.result .where { display: block; font-size: 12px; color: var(--muted); }
.result mark { background: color-mix(in srgb, var(--accent) 30%, transparent); color: inherit; border-radius: 2px; }
.mark-row { display: flex; align-items: stretch; gap: 4px; }
/* each highlight in the list leads with a swatch of its own colour */
.mark-row .result { flex: 1; min-width: 0; position: relative; padding-inline-start: 30px; }
.mark-row .result::before {
  content: ""; position: absolute; inset-inline-start: 9px; top: 11px; width: 12px; height: 12px;
  border-radius: 50%; background: var(--mark, #f5d547); border: 1px solid rgba(0,0,0,.2);
}
.mark-row .result.underline { text-decoration: underline; text-decoration-color: #d33; }
.mark-row .result.underline::before { background: none; border: none; border-bottom: 2px solid #d33; border-radius: 0; height: 9px; }
.mark-row > button.icon { align-self: center; }

/* selection bar: marker colours and "ask Claude" for the selected passage */
.sel-bar {
  position: fixed; bottom: 18px; inset-inline-start: 24px; z-index: 30; display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 6px 12px;
  box-shadow: 0 6px 20px rgba(0,0,0,.18);
}
.sel-bar .dot { width: 24px; height: 24px; padding: 0; border-radius: 50%; border: 1px solid rgba(0,0,0,.2); font-size: 13px; font-weight: 700; line-height: 1; }
.dot[data-style="yellow"] { background: #f5d547; }
.dot[data-style="green"] { background: #8fd98f; }
.dot[data-style="pink"] { background: #f5a3c7; }
.dot[data-style="blue"] { background: #8fc4f5; }
.dot[data-style="underline"] { background: var(--surface); color: #d33; text-decoration: underline; }
.sel-bar .divider { width: 1px; height: 20px; background: var(--border); }
.sel-bar .link { color: var(--accent); font-size: 14px; }

/* highlights painted over a PDF page; multiply keeps the print readable underneath */
/* the blend mode has to sit on the layer itself: it is its own stacking context, so blending
   set on the boxes inside would never reach the page canvas */
.hlLayer { position: absolute; inset: 0; z-index: 1; pointer-events: none; mix-blend-mode: multiply; }
.hlLayer div { position: absolute; border-radius: 2px; }
[data-theme="dark"] .hlLayer { mix-blend-mode: screen; opacity: .45; }
.hlLayer div.underline { background: none !important; border-bottom: 2px solid #d33; }
.ocrLayer span.hit, .textLayer span.hit { background: rgba(255, 140, 0, .35); border-radius: 2px; }
::highlight(mark-yellow) { background-color: #f5d54799; }
::highlight(mark-green) { background-color: #8fd98f99; }
::highlight(mark-pink) { background-color: #f5a3c799; }
::highlight(mark-blue) { background-color: #8fc4f599; }
::highlight(mark-underline) { text-decoration: underline 2px #d33; }
::highlight(search-hit) { background-color: rgba(255, 140, 0, .45); }

/* cloud import */
#cloudDlg { width: min(600px, 94vw); }
.cloud-roots { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.cloud-roots button.active { background: var(--text); color: var(--bg); border-color: var(--text); }
#cloudDlg .crumbs { margin: 0 0 8px; }
.cloud-list { height: 44vh; }
/* rows, not the stacked label-over-field layout that dialog labels get by default */
dialog .cloud-list label, dialog label.find-all { flex-direction: row; align-items: center; margin: 0; font-size: inherit; }
dialog label.find-all { padding: 6px 2px; }
.cloud-list label { display: flex; align-items: center; gap: 8px; padding: 8px 12px; cursor: pointer; }
.cloud-list label input, .find-all input { flex: none; }
.cloud-list label:hover { background: var(--soft); }
.cloud-list .note { padding: 16px; color: var(--muted); text-align: center; }
#cloudBookTitle { width: 100%; margin: 6px 0; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; direction: inherit; }
#cloudBookTitle[hidden] { display: none; }

/* folders */
.crumbs { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; margin: -6px 0 16px; }
.crumb { border: none; background: none; padding: 4px 8px; border-radius: 6px; color: var(--muted); }
.crumb:hover:not(:disabled) { background: var(--soft); color: var(--text); }
.crumb.current { color: var(--text); font-weight: 600; opacity: 1; }
.crumbs .sep { color: var(--muted); }
.card.folder { background: var(--soft); min-height: 0; }
.card.folder .card-title { min-height: 0; }
.card.folder .card-title::before { content: "📁 "; }
.drop-target { outline: 2px dashed var(--accent); outline-offset: 2px; }
.card[draggable="true"] { -webkit-user-drag: element; }
.folder-list { list-style: none; margin: 0 0 12px; padding: 0; max-height: 50vh; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.folder-list button { display: block; width: 100%; text-align: start; border: none; border-radius: 0; background: none; padding: 9px 12px; unicode-bidi: plaintext; }
.folder-list button:hover:not(:disabled) { background: var(--soft); }
.folder-list li + li { border-top: 1px solid var(--border); }
.empty { text-align: center; color: var(--muted); margin-top: 80px; line-height: 2; }
.drop-hint {
  position: fixed; inset: 12px; border: 3px dashed var(--accent); border-radius: 18px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  display: flex; align-items: center; justify-content: center; font-size: 22px; z-index: 50; pointer-events: none;
}

/* ---------- reader ---------- */
#reader { height: 100%; display: flex; flex-direction: column; }
.toolbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.doc-title {
  flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  unicode-bidi: plaintext; text-align: start;
}
.position { color: var(--muted); font-size: 13px; white-space: nowrap; }
.tools { display: flex; gap: 6px; }
.progress-line { height: 3px; background: var(--soft); }
#progressFill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.reader-body { flex: 1; display: flex; min-height: 0; }
#stage { flex: 1; min-width: 0; position: relative; background: var(--bg); }

/* text documents */
.doc-scroll { position: absolute; inset: 0; overflow: auto; }
.doc {
  max-width: 46em; margin: 0 auto; padding: 48px 36px 120px; min-height: 100%;
  background: var(--page); font-family: var(--serif); font-size: var(--doc-font); line-height: 1.75;
}
.doc p, .doc li, .doc h1, .doc h2, .doc h3, .doc h4, .doc blockquote, .doc td, .doc th {
  unicode-bidi: plaintext; text-align: start;
}
.doc h1, .doc h2, .doc h3 { line-height: 1.3; }
.doc img { max-width: 100%; height: auto; }
.doc table { border-collapse: collapse; }
.doc td, .doc th { border: 1px solid var(--border); padding: 4px 8px; }
.doc a { color: var(--accent); }
.doc pre { white-space: pre-wrap; }
::selection { background: color-mix(in srgb, var(--accent) 35%, transparent); }

/* pdf */
.pdf-container { position: absolute; inset: 0; overflow: auto; background: var(--bg); }
.pdfViewer .page { margin: 10px auto; border: none; box-shadow: 0 1px 6px rgba(0,0,0,.25); }
[data-theme="dark"] .pdfViewer .page canvas { filter: invert(.88) hue-rotate(180deg); }

/* OCR text layer: invisible text placed over the scanned page so it can be selected */
/* font-size 0 keeps the <br> line breaks from showing as slivers of selection at the page edge; each span sets its own size */
.ocrLayer { position: absolute; inset: 0; overflow: hidden; line-height: 1; font-size: 0; z-index: 2; }
.ocrLayer span {
  position: absolute; color: transparent; white-space: pre; cursor: text;
  transform-origin: 0 0; font-family: sans-serif; unicode-bidi: plaintext; /* never visible: only its box matters */
}
.ocrLayer span::selection { background: color-mix(in srgb, #3b82f6 35%, transparent); color: transparent; }
#ocrDlg { width: min(520px, 94vw); }
#ocrProgress { margin: 4px 0 10px; }
#ocrProgress .bar { margin-bottom: 6px; }

/* epub */
.epub-wrap { position: absolute; inset: 0; background: var(--page); }
.epub-area { position: absolute; inset: 0; }
/* epub.js sizes the chapter before the scrollbar appears; the body padding absorbs the difference. */
.epub-area .epub-container { overflow-x: hidden !important; }
.loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); }

/* ---------- chat ---------- */
#chat {
  width: 400px; max-width: 46vw; display: flex; flex-direction: column;
  background: var(--surface); border-inline-start: 1px solid var(--border);
}
#chat.closed { display: none; }
.chat-head { display: flex; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.messages { flex: 1; overflow: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 100%; line-height: 1.6; unicode-bidi: plaintext; text-align: start; overflow-wrap: anywhere; }
.msg.user { align-self: flex-start; background: var(--soft); border-radius: 12px; padding: 8px 12px; max-width: 92%; }
.msg.user .quote {
  border-inline-start: 3px solid var(--accent); padding-inline-start: 8px; margin-bottom: 6px;
  color: var(--muted); font-size: 13px; max-height: 5.2em; overflow: hidden;
}
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p, .msg.assistant li, .msg.assistant h1, .msg.assistant h2, .msg.assistant h3, .msg.assistant blockquote {
  unicode-bidi: plaintext; text-align: start;
}
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 { font-size: 1.05em; margin: 1em 0 .4em; }
.msg.assistant blockquote { margin: .6em 0; padding-inline-start: 10px; border-inline-start: 3px solid var(--border); color: var(--muted); }
.msg.assistant code { background: var(--soft); border-radius: 4px; padding: 1px 4px; font-size: .9em; }
.msg.assistant pre { background: var(--soft); padding: 10px; border-radius: 8px; overflow: auto; direction: ltr; text-align: left; }
.msg.assistant table { border-collapse: collapse; font-size: .92em; }
.msg.assistant td, .msg.assistant th { border: 1px solid var(--border); padding: 3px 7px; }
.msg.error { color: #b3261e; background: color-mix(in srgb, #b3261e 10%, transparent); border-radius: 8px; padding: 8px 10px; font-size: 14px; }
.msg.thinking { color: var(--muted); font-size: 14px; }
.chat-empty { color: var(--muted); font-size: 14px; line-height: 1.8; margin: auto 0; text-align: center; padding: 0 12px; }

.composer { border-top: 1px solid var(--border); padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.sel-chip { display: flex; align-items: center; gap: 8px; background: var(--soft); border-radius: 8px; padding: 5px 8px; font-size: 13px; }
.sel-label { color: var(--accent); font-weight: 600; white-space: nowrap; }
.sel-text { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); unicode-bidi: plaintext; }
.quick { display: flex; gap: 6px; flex-wrap: wrap; }
.quick button { font-size: 13px; padding: 3px 10px; border-radius: 999px; }
.composer textarea { width: 100%; resize: vertical; min-height: 54px; max-height: 40vh; unicode-bidi: plaintext; }
.composer-row { display: flex; align-items: center; gap: 8px; }
.composer-row select { font-size: 13px; padding: 5px 8px; max-width: 60%; }

/* ---------- listening ---------- */
.listen-bar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  background: var(--surface); border-top: 1px solid var(--border);
}
/* the settings that open above the player bar */
.listen-panel {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px 18px; align-items: start;
  padding: 12px 14px 4px; background: var(--surface); border-top: 1px solid var(--border);
  max-height: 46vh; overflow: auto; font-size: 14px;
}
.listen-panel .field { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 13px; }
.listen-panel .field select { color: var(--text); font-size: 14px; }
.listen-panel .row { display: flex; align-items: center; gap: 8px; }
.listen-panel input[type="range"] { flex: 1; min-width: 0; accent-color: var(--accent); direction: ltr; }
.listen-panel output { min-width: 3.2em; color: var(--text); direction: ltr; text-align: end; }
.listen-panel fieldset { grid-column: 1 / -1; border: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 4px 18px; }
.listen-panel legend { padding: 0; margin-bottom: 4px; color: var(--muted); font-size: 13px; }
.listen-panel fieldset label { display: flex; align-items: center; gap: 6px; padding: 3px 0; cursor: pointer; }
.listen-panel fieldset input { accent-color: var(--accent); }
.listen-panel .hint { grid-column: 1 / -1; margin: 0 0 8px; }
/* the sentence being read, over a PDF page (see .hlLayer for why the blend sits on the layer) */
.spokenLayer { position: absolute; inset: 0; z-index: 1; pointer-events: none; mix-blend-mode: multiply; }
.spokenLayer div { position: absolute; border-radius: 3px; background: #f2c89b; }
[data-theme="dark"] .spokenLayer { mix-blend-mode: screen; opacity: .4; }
#reader.listening .pdfViewer .page, #reader.listening .doc { cursor: pointer; }
.listen-bar .transport { display: flex; gap: 6px; } /* stays left-to-right, like on any player */
.listen-bar #listenPlay { min-width: 46px; font-size: 16px; }
.listen-bar #listenPlay.busy { opacity: .6; }
.listen-status {
  flex: 1; min-width: 0; color: var(--muted); font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; unicode-bidi: plaintext; text-align: start;
}
.listen-status.error { color: #b3261e; white-space: normal; }
#listenBtn.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
#reader.listening .sel-bar { bottom: 74px; }
::highlight(spoken) { background-color: color-mix(in srgb, var(--accent) 22%, transparent); }
#ttsSampleStatus:empty { display: none; }
#ttsSampleStatus.error { color: #b3261e; }
dialog label .row { display: flex; gap: 8px; }
dialog label .row select { flex: 1; min-width: 0; }
#settingsDlg { max-height: 92vh; overflow: auto; }

/* ---------- dialog / toast ---------- */
dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 22px; width: min(440px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog h2 { margin: 0 0 14px; font-size: 19px; }
dialog label { display: flex; flex-direction: column; gap: 5px; margin-bottom: 10px; font-size: 14px; }
dialog input, dialog select { direction: ltr; }
.hint { color: var(--muted); font-size: 12.5px; margin: 0 0 12px; line-height: 1.6; }
#bookDlg { width: min(560px, 94vw); }
#bookDlg input[type="text"] { direction: inherit; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; }
#bookDlg select { direction: rtl; }
.chapters-head { display: flex; align-items: center; gap: 8px; margin: 14px 0 8px; }
.chapter-list { list-style: none; margin: 0 0 10px; padding: 0; max-height: 44vh; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.chapter-list:empty::after { content: "עדיין אין פרקים. הוסף קבצים."; display: block; padding: 16px; color: var(--muted); text-align: center; font-size: 14px; }
.chapter-list li { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--border); }
.chapter-list li:last-child { border-bottom: none; }
.chapter-list .num { color: var(--muted); font-size: 13px; min-width: 1.6em; }
.chapter-list .name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; unicode-bidi: plaintext; text-align: start; }
.chapter-list button { padding: 2px 8px; }
#chapterSelect { max-width: 30vw; font-size: 14px; padding: 5px 8px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 100;
  background: var(--text); color: var(--bg); padding: 9px 18px; border-radius: 10px; max-width: 90vw; font-size: 14px;
}

@media (max-width: 820px) {
  #chat { position: absolute; inset: 0; width: auto; max-width: none; z-index: 20; border: none; }
  .reader-body { position: relative; }
  .position { display: none; }
  .doc { padding: 28px 18px 100px; }
  /* two rows: title and chapter on top, the tools in a strip that scrolls sideways if needed */
  .toolbar { gap: 6px; padding: 6px 8px; flex-wrap: wrap; }
  .doc-title { flex: 1 1 40%; }
  #chapterSelect { max-width: 46vw; }
  .tools { gap: 4px; width: 100%; overflow-x: auto; justify-content: space-between; scrollbar-width: none; }
  .nav-btn { display: none; }
  /* 16px keeps iOS from zooming the page when a field is focused */
  input, select, textarea { font-size: 16px; }
  .sel-bar { inset-inline: 12px; bottom: 12px; justify-content: center; }
}
