:root {
  --bg: #e9ecef; --bar: #ffffff; --ink: #2d3339; --muted: #6b7680; --line: #d5dbe0;
  --accent: #4a5560; --paper-shadow: 0 2px 14px rgba(0,0,0,.12);
}
body.night { --bg: #15181b; --bar: #1f2328; --ink: #dfe3e6; --muted: #98a2ab; --line: #30363c; --accent: #aab4bd; --paper-shadow: none; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font-family: "Segoe UI", Tahoma, "Noto Naskh Arabic", "Geeza Pro", Arial, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; overflow: hidden; }
body { display: flex; flex-direction: column; height: 100dvh; }

header, footer { background: var(--bar); border-color: var(--line); display: flex; align-items: center; gap: 6px; padding: 6px 8px; z-index: 5; }
header { border-bottom: 1px solid var(--line); padding-top: max(6px, env(safe-area-inset-top)); }
footer { border-top: 1px solid var(--line); padding-bottom: max(6px, env(safe-area-inset-bottom)); }
.title { flex: 1; font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title span { color: var(--muted); font-weight: 400; margin-inline-start: 4px; }
.ib { width: 40px; height: 40px; border: 0; border-radius: 10px; background: transparent; color: var(--ink); font-size: 20px; cursor: pointer; }
.ib:hover, .ib.on { background: rgba(127,127,127,.15); }

#stage { flex: 1; position: relative; overflow: hidden; touch-action: pan-y; }
#scroller { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 10px; }
#page { max-width: 100%; max-height: 100%; background: #fff; box-shadow: var(--paper-shadow); pointer-events: none; opacity: 0; transition: opacity .15s; }
#page.ready { opacity: 1; }
#shield { position: absolute; inset: 0; }
body.night #page.inv { filter: invert(.88) hue-rotate(180deg); }
body.night #page:not(.inv) { filter: brightness(.85); }

/* zoom: fit to width and scroll */
body.zoom #stage { touch-action: pan-x pan-y pinch-zoom; }
body.zoom #scroller { overflow: auto; display: block; padding: 0; }
body.zoom #page { max-width: none; max-height: none; width: 100%; display: block; }
body.zoom #shield { position: absolute; inset: 0 0 auto 0; height: var(--zh, 100%); }
@media (min-width: 900px) { body.zoom #page { width: 900px; margin: 0 auto; } }

#loading, #msg { position: absolute; inset: auto 0 18px 0; margin: auto; width: max-content; max-width: 90%; padding: 8px 14px; border-radius: 20px; background: rgba(40,45,50,.85); color: #fff; font-size: 14px; }
#loading { display: none; }
#loading.show { display: block; }

.nav { border: 1px solid var(--line); background: transparent; color: var(--ink); border-radius: 10px; height: 40px; padding: 0 12px; font: inherit; font-size: 14px; cursor: pointer; white-space: nowrap; }
.nav:disabled { opacity: .35; }
.mid { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
#slider { flex: 1; min-width: 0; accent-color: var(--accent); direction: rtl; }
.pg { border: 0; background: rgba(127,127,127,.12); color: var(--ink); border-radius: 8px; padding: 6px 10px; font: inherit; font-size: 13px; cursor: pointer; white-space: nowrap; font-variant-numeric: tabular-nums; }

#tocWrap { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 20; }
#toc { position: absolute; top: 0; bottom: 0; right: 0; width: min(340px, 88vw); background: var(--bar); overflow-y: auto; padding: max(10px, env(safe-area-inset-top)) 0 20px; box-shadow: -4px 0 20px rgba(0,0,0,.2); }
.tochead { display: flex; justify-content: space-between; align-items: center; padding: 4px 16px 10px; font-weight: 700; font-size: 17px; border-bottom: 1px solid var(--line); }
#tocList { list-style: none; margin: 0; padding: 6px 0; }
#tocList li { display: flex; justify-content: space-between; gap: 10px; padding: 11px 18px; cursor: pointer; font-size: 15px; }
#tocList li:hover, #tocList li.here { background: rgba(127,127,127,.12); }
#tocList li small { color: var(--muted); font-variant-numeric: tabular-nums; }
#tocList li.ch { font-weight: 600; }
#feedbackLink a, #endFeedback { display: block; margin: 10px 16px; padding: 11px; text-align: center; border-radius: 10px; background: var(--accent); color: #fff; text-decoration: none; font-weight: 600; }
.rights { color: var(--muted); font-size: 12px; text-align: center; padding: 0 16px; line-height: 1.7; }

#endCard { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 30; display: flex; align-items: center; justify-content: center; }
#endCard[hidden] { display: none; }
.card { background: var(--bar); color: var(--ink); border-radius: 16px; padding: 22px; width: min(360px, 90vw); text-align: center; }
.card h2 { margin: 0 0 8px; font-size: 19px; }
.card p { color: var(--muted); margin: 0 0 14px; }
#endClose { margin-top: 6px; border: 0; background: transparent; color: var(--muted); font: inherit; cursor: pointer; padding: 8px; }

@media (max-width: 420px) { .nav { padding: 0 9px; } .title { font-size: 13px; } }
@media print { body { display: none !important; } }
