@font-face { font-family: "Amiri"; src: url("fonts/amiri.ttf") format("truetype"); font-display: swap; }

:root {
  --bg: #F6EEE3; --bg2: #FBF6EF; --card: #FFFDF9; --ink: #3E2414; --muted: #8A6F5C; --line: #E6D8C6;
  --accent: #B5532E; --accent-ink: #fff; --hi: rgba(181, 83, 46, .16); --hi-line: rgba(181, 83, 46, .55);
  --page-bg: #fff; --shadow: 0 6px 24px rgba(62, 36, 20, .14);
  --bar: rgba(251, 246, 239, .92);
  color-scheme: light;
}
:root[data-theme="night"] {
  --bg: #17130F; --bg2: #1F1A15; --card: #26201A; --ink: #EDE4D8; --muted: #A8998A; --line: #3A3128;
  --accent: #E07A4F; --accent-ink: #1a0f08; --hi: rgba(224, 122, 79, .22); --hi-line: rgba(224, 122, 79, .7);
  --page-bg: #1b1713; --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  --bar: rgba(31, 26, 21, .92);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.view { position: fixed; inset: 0; }

/* ---------- Library ---------- */
#library { overflow-y: auto; padding: calc(env(safe-area-inset-top) + 20px) 16px 40px; }
.lib-head { display: flex; align-items: center; gap: 14px; max-width: 980px; margin: 0 auto 22px; }
.lib-head h1 { margin: 0; font-size: 24px; letter-spacing: -.01em; }
.lib-head p { margin: 2px 0 0; font-size: 14px; }
.lib-head .icon-btn { margin-left: auto; }
.lib-logo { width: 52px; height: 52px; border-radius: 13px; box-shadow: var(--shadow); }
.shelf { max-width: 980px; margin: 0 auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.book { display: flex; gap: 14px; padding: 14px; background: var(--card); border: 1px solid var(--line); border-radius: 16px;
  text-align: left; box-shadow: 0 1px 2px rgba(0,0,0,.04); transition: transform .12s ease, box-shadow .12s ease; }
.book:active { transform: scale(.985); }
@media (hover: hover) { .book:hover { box-shadow: var(--shadow); } }
.book img { width: 84px; height: 118px; object-fit: cover; border-radius: 4px 8px 8px 4px; background: #ddd;
  box-shadow: inset 4px 0 6px rgba(0,0,0,.25), 0 3px 10px rgba(0,0,0,.18); flex: none; }
.book .meta { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.book .t { font-weight: 650; font-size: 16px; line-height: 1.25; }
.book .ar { font-family: "Amiri", serif; font-size: 19px; direction: rtl; text-align: right; line-height: 1.5; }
.book .s, .book .p { font-size: 13px; color: var(--muted); }
.book .badge { align-self: flex-start; margin-top: auto; font-size: 12px; padding: 3px 9px; border-radius: 999px;
  background: var(--hi); color: var(--accent); font-weight: 600; }
.progress { height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--accent); }
.lib-note { max-width: 980px; margin: 22px auto 0; font-size: 13px; line-height: 1.5; }

/* ---------- Bars ---------- */
.bar { position: absolute; left: 0; right: 0; z-index: 5; display: flex; align-items: center; gap: 6px;
  background: var(--bar); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); transition: transform .2s ease, opacity .2s ease; }
.bar.top { top: 0; padding: calc(env(safe-area-inset-top) + 6px) 8px 6px; border-bottom: 1px solid var(--line); }
.bar.bottom { bottom: 0; padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px); border-top: 1px solid var(--line); }
#reader.chrome-off .bar.top { transform: translateY(-110%); opacity: 0; }
#reader.chrome-off .bar.bottom { transform: translateY(110%); opacity: 0; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 18px; flex: none; }
.icon-btn.small { width: 36px; height: 32px; font-size: 14px; font-weight: 600; }
.icon-btn:active { background: var(--hi); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.titlebox { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; }
.r-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.r-page { font-size: 12px; color: var(--muted); padding: 1px 8px; border-radius: 8px; }
.chip { padding: 7px 12px; border-radius: 999px; font-size: 13px; font-weight: 600; border: 1px solid var(--line); }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
#slider { flex: 1; accent-color: var(--accent); min-width: 60px; }
.zoom { display: flex; }

/* ---------- Stage ---------- */
#stage { position: absolute; inset: 0; overflow: auto; -webkit-overflow-scrolling: touch; touch-action: pan-x pan-y; }
#pageWrap { position: relative; margin: 0 auto; background: var(--page-bg); box-shadow: var(--shadow); }
#pageWrap canvas { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }
:root[data-theme="night"] #pageWrap canvas { filter: invert(.9) hue-rotate(180deg) sepia(.12); }
#hiLayer { position: absolute; inset: 0; }
#hiLayer i { position: absolute; background: var(--hi); border-bottom: 2px solid var(--hi-line); border-radius: 3px; mix-blend-mode: multiply; }
:root[data-theme="night"] #hiLayer i { mix-blend-mode: screen; }
#spinner { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; }
#spinner .dot { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Panel ---------- */
#panel { position: absolute; z-index: 8; background: var(--card); color: var(--ink); display: flex; flex-direction: column;
  box-shadow: 0 -8px 30px rgba(0,0,0,.18); }
/* phone / portrait: bottom sheet */
#panel { left: 0; right: 0; bottom: 0; height: min(58vh, 560px); border-radius: 18px 18px 0 0; padding-bottom: env(safe-area-inset-bottom); }
.grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 2px; flex: none; }
/* wide: side panel */
@media (min-width: 900px) and (min-aspect-ratio: 1/1) {
  #panel { left: auto; top: 0; bottom: 0; right: 0; width: 45vw; height: auto; border-radius: 0;
    border-left: 1px solid var(--line); box-shadow: -8px 0 30px rgba(0,0,0,.12); padding-top: env(safe-area-inset-top); }
  .grab { display: none; }
  #reader.panel-open #stage, #reader.panel-open .bar { right: 45vw; }
}
.p-head { display: flex; align-items: center; gap: 8px; padding: 6px 10px 8px 16px; border-bottom: 1px solid var(--line); flex: none; }
.p-info { flex: 1; font-size: 13px; color: var(--muted); min-width: 0; }
.seg { display: inline-flex; background: var(--bg); border-radius: 12px; padding: 3px; gap: 2px; flex: none; }
.seg button { padding: 6px 12px; border-radius: 9px; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.p-body { flex: 1; overflow-y: auto; padding: 14px 18px 18px; -webkit-overflow-scrolling: touch; }
.p-ar { font-family: "Amiri", serif; font-size: calc(var(--fs, 18px) * 1.35); line-height: 2.05; text-align: right; margin-bottom: 14px;
  padding-bottom: 12px; border-bottom: 1px dashed var(--line); }
.p-langrow { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.fs { display: flex; }
.p-tr { font-size: var(--fs, 18px); line-height: 1.65; white-space: pre-wrap; }
.p-tr .none { color: var(--muted); font-style: italic; }
.p-foot { display: flex; gap: 10px; padding: 10px 14px; border-top: 1px solid var(--line); flex: none; }
.p-foot .btn { flex: 1; }
.btn { padding: 10px 14px; border-radius: 12px; background: var(--accent); color: var(--accent-ink); font-weight: 600; font-size: 14px; }
.btn.ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.btn:disabled { opacity: .4; }

#toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 80px); transform: translateX(-50%); z-index: 20;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100vw - 32px); text-align: center; }
dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--card); color: var(--ink); padding: 18px; }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog label { display: flex; flex-direction: column; gap: 8px; font-size: 14px; }
dialog input { font: inherit; font-size: 18px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg2); color: var(--ink); width: 200px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 14px 0 0; }
