:root {
  --bg: #F7F3EC;
  --surface: #FFFFFF;
  --surface-2: #FBF9F5;
  --desk: #EDE6D8;
  --line: #E4DCCD;
  --line-strong: #D6CCBA;
  --ink: #1F1D1A;
  --ink-2: #3A362F;
  --muted: #5C574E;
  --accent: #0F5E5A;
  --accent-ink: #FFFFFF;
  --accent-soft: #E9F1EF;
  --danger: #A23B2A;
  --shadow: 0 6px 20px rgba(60, 45, 20, 0.12);
  --page-bg: #FFFFFF;
  --page-filter: none;
  --radius: 12px;
  --ui: 'IBM Plex Sans Arabic', system-ui, sans-serif;
  --serif: 'Amiri', 'Times New Roman', serif;
  --hl-yellow: rgba(246, 214, 70, 0.45);
  --hl-green: rgba(110, 196, 170, 0.42);
  --hl-pink: rgba(236, 140, 160, 0.40);
  --hl-blue: rgba(120, 165, 235, 0.40);
  color-scheme: light;
}
body.theme-sepia {
  --bg: #F1E4C8;
  --surface: #F8EEDB;
  --surface-2: #F4E8CF;
  --desk: #E6D5B2;
  --line: #DCCBA8;
  --line-strong: #CDB98F;
  --ink: #3A2E1A;
  --ink-2: #4A3A20;
  --muted: #6A5636;
  --page-filter: sepia(0.35) saturate(0.9) brightness(0.97);
}
body.theme-night {
  --bg: #121416;
  --surface: #1B1E21;
  --surface-2: #171A1D;
  --desk: #0E1012;
  --line: #2A2E33;
  --line-strong: #3A4046;
  --ink: #E2DED6;
  --ink-2: #C9C4BA;
  --muted: #A9A49A;
  --accent: #7CC8BE;
  --accent-ink: #0E1A19;
  --accent-soft: #24413E;
  --danger: #E58A7A;
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
  --page-filter: invert(0.9) hue-rotate(180deg) brightness(0.95) contrast(0.92);
  --hl-yellow: rgba(242, 212, 107, 0.38);
  --hl-green: rgba(124, 200, 190, 0.35);
  --hl-pink: rgba(236, 140, 160, 0.35);
  --hl-blue: rgba(120, 165, 235, 0.38);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--ui);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 44px; padding: 0 16px; border-radius: var(--radius);
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  font-size: 15px; white-space: nowrap; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn {
  width: 44px; height: 44px; flex: 0 0 auto; border-radius: 10px; border: none; background: transparent;
  display: inline-flex; align-items: center; justify-content: center; color: var(--ink-2);
}
.icon-btn:hover { background: var(--accent-soft); }
.icon-btn.on { background: var(--accent-soft); color: var(--accent); }
.icon-btn svg, .btn svg { width: 20px; height: 20px; flex: 0 0 auto; }
.chip {
  height: 38px; padding: 0 14px; border-radius: 19px; border: 1px solid var(--line-strong);
  background: var(--surface); font-size: 14px; color: var(--ink-2); white-space: nowrap;
}
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.input {
  height: 46px; border: 1px solid var(--line-strong); border-radius: var(--radius); padding: 0 14px;
  background: var(--surface-2); font-size: 16px; width: 100%;
}
textarea.input { height: auto; min-height: 90px; padding: 10px 14px; resize: vertical; line-height: 1.7; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 14px; font-weight: 600; }
.error { color: var(--danger); font-size: 14px; }
.muted { color: var(--muted); }

#toast {
  position: fixed; inset-inline: 0; bottom: 24px; margin: 0 auto; width: fit-content; max-width: calc(100% - 32px);
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 12px; font-size: 15px;
  box-shadow: var(--shadow); opacity: 0; transform: translateY(10px); transition: 0.2s; pointer-events: none; z-index: 100;
}
#toast.show { opacity: 1; transform: none; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 32px 16px; }
.login-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
  padding: 40px 28px; display: flex; flex-direction: column; gap: 18px;
}
.brand { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; }
.brand h1 { margin: 0; font-family: var(--serif); font-size: 36px; line-height: 1.2; }
.brand p { margin: 0; color: var(--muted); font-size: 15px; }
.brand svg { width: 48px; height: 48px; color: var(--accent); }

/* ---------- library ---------- */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding: 12px max(16px, 5%);
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.logo { display: flex; align-items: center; gap: 10px; flex: 1 1 auto; font-family: var(--serif); font-size: 26px; font-weight: 700; }
.logo svg { width: 28px; height: 28px; color: var(--accent); }
.topbar .search { flex: 0 1 320px; border-radius: 22px; height: 44px; }
.lib { max-width: 1160px; margin: 0 auto; padding: 28px max(16px, 5%) 64px; display: flex; flex-direction: column; gap: 28px; }
.continue {
  display: flex; flex-wrap: wrap; gap: 20px; align-items: center; background: var(--surface);
  border: 1px solid var(--line); border-radius: 20px; padding: 20px;
}
.continue .cover { width: 100px; flex: 0 0 auto; }
.continue .info { flex: 1 1 260px; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.continue .kicker { font-size: 14px; color: var(--accent); font-weight: 600; }
.continue .title { font-size: 22px; font-weight: 700; overflow-wrap: anywhere; }
.progress { height: 8px; background: var(--desk); border-radius: 4px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); border-radius: 4px; }
.progress.thin { height: 4px; }
.section-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.section-head h2 { margin: 0; font-size: 22px; flex: 1 1 auto; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 24px 20px; }
.book { display: flex; flex-direction: column; gap: 8px; position: relative; min-width: 0; }
.book .open { all: unset; cursor: pointer; display: flex; flex-direction: column; gap: 8px; }
.book .open:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 10px; }
.cover {
  aspect-ratio: 3 / 4; border-radius: 10px; overflow: hidden; background: var(--accent); color: #F7F3EC;
  display: flex; align-items: center; justify-content: center; text-align: center; padding: 12px;
  font-family: var(--serif); font-size: 20px; line-height: 1.4; box-shadow: 0 2px 10px rgba(60, 45, 20, 0.15);
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover.has-img { padding: 0; background: var(--surface-2); }
.book .name { font-size: 15px; font-weight: 600; line-height: 1.4; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book .meta { font-size: 13px; color: var(--muted); }
.book .menu-btn { position: absolute; top: 6px; inset-inline-start: 6px; background: rgba(255,255,255,0.88); color: #3A362F; width: 36px; height: 36px; border-radius: 18px; }
.upload-tile {
  aspect-ratio: 3 / 4; border-radius: 10px; border: 2px dashed var(--line-strong); background: var(--surface-2);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; text-align: center;
  padding: 12px; color: var(--muted); font-size: 13px; cursor: pointer; width: 100%;
}
.upload-tile strong { color: var(--accent); font-size: 15px; }
.upload-tile svg { width: 32px; height: 32px; color: var(--accent); }
.drop-overlay {
  position: fixed; inset: 0; background: rgba(15, 94, 90, 0.12); border: 4px dashed var(--accent); z-index: 50;
  display: flex; align-items: center; justify-content: center; font-size: 24px; font-weight: 700; color: var(--accent); pointer-events: none;
}
.empty { text-align: center; color: var(--muted); padding: 32px 0; }

/* menus & dialogs */
.menu {
  position: fixed; z-index: 60; min-width: 200px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: var(--shadow); padding: 6px; display: flex; flex-direction: column;
}
.menu button { all: unset; cursor: pointer; padding: 10px 14px; border-radius: 10px; font-size: 15px; display: flex; gap: 10px; align-items: center; }
.menu button:hover, .menu button:focus-visible { background: var(--accent-soft); }
.menu button.danger { color: var(--danger); }
.menu hr { border: none; border-top: 1px solid var(--line); margin: 4px 0; }
.menu .label { font-size: 12px; color: var(--muted); padding: 6px 14px 2px; }
dialog {
  border: 1px solid var(--line); border-radius: 18px; background: var(--surface); color: var(--ink);
  padding: 24px; width: min(440px, calc(100% - 32px)); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(20, 16, 10, 0.35); }
dialog h2 { margin: 0 0 16px; font-size: 20px; }
dialog form { display: flex; flex-direction: column; gap: 16px; }
dialog .actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.seg { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- reader ---------- */
.reader { position: fixed; inset: 0; display: flex; flex-direction: column; background: var(--desk); }
.r-top {
  display: flex; align-items: center; gap: 4px; padding: 6px 10px; padding-top: max(6px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line); min-height: 58px; flex-wrap: nowrap;
}
.r-title { flex: 1 1 auto; min-width: 0; font-weight: 600; font-size: 15px; padding: 0 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.zoom { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; height: 44px; }
.zoom .icon-btn { width: 38px; height: 42px; }
.zoom select { border: none; background: transparent; height: 42px; font-size: 14px; padding: 0 2px; max-width: 110px; }
.ask-btn { height: 44px; }
.findbar {
  display: flex; align-items: center; gap: 6px; padding: 6px 10px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.findbar .input { height: 42px; flex: 1 1 auto; max-width: 480px; }
.findbar .count { font-size: 13px; color: var(--muted); white-space: nowrap; min-width: 70px; }
.r-body { flex: 1 1 auto; display: flex; min-height: 0; position: relative; }
.r-side {
  width: 300px; flex: 0 0 auto; background: var(--surface-2); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); flex: 0 0 auto; }
.tabs button {
  flex: 1; height: 48px; border: none; background: transparent; border-bottom: 3px solid transparent; font-size: 15px; color: var(--muted);
}
.tabs button.on { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }
.tab-body { flex: 1 1 auto; overflow: auto; padding: 10px; }
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc ul ul { padding-inline-start: 16px; }
.toc a { display: block; padding: 8px 10px; border-radius: 8px; color: var(--ink-2); text-decoration: none; font-size: 15px; line-height: 1.5; }
.toc a:hover { background: var(--accent-soft); }
.toc a.current { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.thumbs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.thumb { all: unset; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.thumb .ph { width: 100%; aspect-ratio: 3 / 4; background: var(--page-bg); border: 2px solid var(--line); border-radius: 4px; overflow: hidden; display: flex; }
.thumb canvas { width: 100%; height: 100%; object-fit: contain; filter: var(--page-filter); }
.thumb.current .ph { border-color: var(--accent); }
.thumb:focus-visible .ph { outline: 2px solid var(--accent); }
.notes { display: flex; flex-direction: column; gap: 10px; }
.notes .group-title { font-size: 13px; color: var(--muted); font-weight: 600; margin-top: 6px; }
.note-card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }
.note-card .quote { font-family: var(--serif); font-size: 16px; line-height: 1.8; border-inline-start: 4px solid var(--c, var(--hl-yellow)); padding-inline-start: 8px; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.note-card .note { font-size: 14px; white-space: pre-wrap; color: var(--ink-2); max-height: 9em; overflow: auto; }
.note-card .row { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.note-card .row .sp { flex: 1; }
.note-card .row .icon-btn { width: 36px; height: 36px; }
.bm-row { display: flex; align-items: center; gap: 6px; }
.bm-row a { flex: 1; padding: 8px 10px; border-radius: 8px; text-decoration: none; color: var(--ink-2); }
.bm-row a:hover { background: var(--accent-soft); }

.stage { flex: 1 1 auto; position: relative; min-width: 0; }
#viewerContainer { position: absolute; inset: 0; overflow: auto; direction: ltr; }
.pdfViewer { padding: 16px 0 48px; }
.pdfViewer .page { box-shadow: 0 2px 12px rgba(60, 45, 20, 0.14); border: none !important; margin: 12px auto !important; background-color: var(--page-bg); }
.pdfViewer .page canvas { filter: var(--page-filter); }
.pdfViewer.scrollPage .page { margin: 0 auto !important; }
body.theme-night .pdfViewer .page { box-shadow: 0 2px 16px rgba(0,0,0,0.6); }
.hl-layer { position: absolute; inset: 0; pointer-events: none; z-index: 1; mix-blend-mode: multiply; }
body.theme-night .hl-layer { mix-blend-mode: screen; }
.hl-layer div { position: absolute; border-radius: 2px; }
.hl-layer .yellow { background: var(--hl-yellow); }
.hl-layer .green { background: var(--hl-green); }
.hl-layer .pink { background: var(--hl-pink); }
.hl-layer .blue { background: var(--hl-blue); }
.hl-layer .has-note::after { content: ''; position: absolute; top: -5px; inset-inline-end: -5px; width: 10px; height: 10px; border-radius: 5px; background: #C9A227; }
.hl-layer .flash { outline: 3px solid var(--accent); }
.pdfViewer .textLayer { z-index: 2; }
.pdfViewer .textLayer ::selection { background: rgba(60, 120, 220, 0.35); }
.pdfViewer .page .bm-flag { position: absolute; top: 0; right: 28px; width: 22px; height: 32px; z-index: 3; color: var(--accent); pointer-events: none; }
.r-bottom {
  display: flex; align-items: center; justify-content: center; gap: 10px; padding: 6px 12px;
  padding-bottom: max(6px, env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--line); font-size: 14px;
}
.r-bottom input { width: 64px; height: 38px; text-align: center; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface-2); }
.r-bottom .slider { flex: 0 1 320px; accent-color: var(--accent); direction: ltr; transform: scaleX(-1); }
.loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 16px; }

/* selection toolbar */
.sel-bar {
  position: fixed; z-index: 40; display: flex; gap: 2px; background: #1F1D1A; border-radius: 12px; padding: 4px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.28); direction: rtl;
}
.sel-bar button { height: 40px; padding: 0 12px; border: none; border-radius: 8px; background: transparent; color: #F7F3EC; font-size: 14px; white-space: nowrap; }
.sel-bar button:hover { background: rgba(255,255,255,0.12); }
.sel-bar button.main { background: #0F5E5A; color: #fff; font-weight: 600; }
.sel-bar .sw { width: 26px; height: 26px; padding: 0; border-radius: 13px; margin: 7px 3px; border: 2px solid transparent; }
.sel-bar .sw:hover { border-color: #fff; }
.sw.yellow { background: #F6D646; } .sw.green { background: #6EC4AA; } .sw.pink { background: #EC8CA0; } .sw.blue { background: #78A5EB; }

.popover {
  position: fixed; z-index: 45; width: min(360px, calc(100vw - 24px)); background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px;
}
.popover .quote { font-family: var(--serif); font-size: 15px; color: var(--muted); max-height: 5.4em; overflow: hidden; }
.popover .swatches { display: flex; gap: 8px; align-items: center; }
.popover .swatches .sw { width: 30px; height: 30px; border-radius: 15px; border: 2px solid transparent; }
.popover .swatches .sw.on { border-color: var(--ink); }
.popover .row { display: flex; gap: 8px; flex-wrap: wrap; }

/* AI panel */
.ai {
  width: 420px; flex: 0 0 auto; background: var(--surface); border-inline-start: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.ai-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px 8px 8px; border-bottom: 1px solid var(--line); }
.ai-head h2 { margin: 0; font-size: 17px; flex: 1; display: flex; align-items: center; gap: 8px; }
.ai-head h2 svg { width: 20px; height: 20px; color: var(--accent); }
.ai-head .model { font-size: 12px; color: var(--muted); font-weight: 400; }
.ai-body { flex: 1 1 auto; overflow: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 14px; }
.ai blockquote {
  margin: 0; padding: 10px 14px; background: var(--accent-soft); border-radius: 12px; font-family: var(--serif);
  font-size: 16px; line-height: 1.9; color: var(--ink-2); max-height: 8.5em; overflow: auto;
}
.msg { font-size: 15.5px; line-height: 1.9; overflow-wrap: anywhere; }
.msg.user { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--line); border-radius: 14px; padding: 8px 14px; max-width: 90%; }
.msg p { margin: 0 0 10px; } .msg p:last-child { margin-bottom: 0; }
.msg h3, .msg h4 { margin: 12px 0 6px; font-size: 16px; }
.msg ul, .msg ol { margin: 0 0 10px; padding-inline-start: 22px; }
.msg code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 0.92em; }
.msg.err { color: var(--danger); }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 4px; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: 0.2s; } .typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }
.ai-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.ai-form { padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); display: flex; gap: 8px; align-items: flex-end; }
.ai-form textarea { flex: 1; min-height: 46px; max-height: 140px; border-radius: 23px; padding: 11px 16px; resize: none; }
.ai-form .send { width: 46px; height: 46px; border-radius: 23px; border: none; background: var(--accent); color: var(--accent-ink); display: flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.ai-form .send svg { width: 20px; height: 20px; }
.ai-empty { color: var(--muted); font-size: 15px; line-height: 1.9; }

/* ---------- small screens ---------- */
@media (max-width: 1100px) {
  .ai { position: absolute; inset-block: 0; inset-inline-end: 0; z-index: 20; box-shadow: var(--shadow); width: min(420px, 92vw); }
}
@media (max-width: 860px) {
  .r-side { position: absolute; inset-block: 0; inset-inline-start: 0; z-index: 25; width: min(320px, 86vw); box-shadow: var(--shadow); }
  .hide-sm { display: none !important; }
}
@media (max-width: 640px) {
  .r-top { gap: 0; padding-inline: 4px; }
  .zoom select { display: none; }
  .ask-btn .lbl { display: none; }
  .ask-btn { width: 44px; padding: 0; }
  .ai {
    inset-block: auto 0; inset-inline: 0; width: 100%; height: min(70vh, 560px);
    border-radius: 22px 22px 0 0; border: none; box-shadow: 0 -8px 30px rgba(60,45,20,0.2);
  }
  .ai-head::before { content: ''; position: absolute; top: 6px; left: 50%; width: 40px; height: 5px; margin-left: -20px; border-radius: 3px; background: var(--line-strong); }
  .ai-head { position: relative; padding-top: 16px; }
  .r-bottom .slider { display: none; }
  .continue .cover { width: 80px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 18px 14px; }
}
