@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%; overflow: hidden; background: var(--bg); color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; touch-action: pan-x pan-y; }
#reader { touch-action: none; }
#reader .bar, #reader #panel .p-head, #reader #panel .p-foot { touch-action: none; }
#reader #panel .p-body { touch-action: pan-y; }
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; overscroll-behavior: contain; }
#pageWrap { position: relative; margin: 0 auto; background: var(--page-bg); box-shadow: var(--shadow); perspective: 2400px; }
/* page turn */
#pageWrap .leaf { position: absolute; inset: 0; z-index: 3; transform-style: preserve-3d; will-change: transform; pointer-events: none; }
#pageWrap .leaf canvas { position: absolute; inset: 0; width: 100%; height: 100%; backface-visibility: hidden; }
#pageWrap .leaf .shade { position: absolute; inset: 0; pointer-events: none; }
#pageWrap .under-shade { position: absolute; inset: 0; z-index: 2; pointer-events: none; background: linear-gradient(to right, rgba(0,0,0,.28), rgba(0,0,0,0) 50%); }
#pageWrap .leaf .back { position: absolute; inset: 0; transform: rotateY(180deg); backface-visibility: hidden;
  background: linear-gradient(to left, #e6e0d6, #f6f2eb 55%, #fff); box-shadow: inset -12px 0 18px rgba(0,0,0,.08); }
:root[data-theme="night"] #pageWrap .leaf .back { background: linear-gradient(to left, #2a241e, #221d18 55%, #1d1915); }
#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); visibility: hidden;
  transition: transform .32s cubic-bezier(.2, .8, .2, 1), visibility 0s linear .32s; }
#panel.open { visibility: visible; transform: none !important; transition: transform .32s cubic-bezier(.2, .8, .2, 1), visibility 0s; }
/* 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);
  transform: translateY(105%); }
#stage, .bar { transition: right .32s cubic-bezier(.2, .8, .2, 1); }
@media (prefers-reduced-motion: reduce) { #panel, #stage, .bar { transition: none !important; } }
.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);
    transform: translateX(105%); }
  .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; }
.p-foot .btn.play, .p-foot .btn.auto { flex: none; }
.btn.play { width: 48px; padding: 8px; display: grid; place-items: center; }
.btn.play svg { width: 22px; height: 22px; fill: currentColor; }
.btn.play .i-pause, .btn.play.on .i-play { display: none; }
.btn.play.on .i-pause { display: inline; }
.btn.play.loading { opacity: .6; }
.btn.auto { font-size: 13px; padding: 10px 10px; }
.btn.auto[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
#hiLayer i.speak { background: rgba(255, 196, 0, .38); border-bottom-color: rgba(214, 150, 0, .9); }
.p-ar .k-sent { background: rgba(255, 196, 0, .16); border-radius: 6px; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
.p-ar .k-sent span { border-radius: 4px; transition: background-color .12s; }
.p-ar .k-sent .k-now { background: rgba(255, 196, 0, .55); }
.p-ar .k-sent .k-done { color: var(--muted); }

#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; }

#upd, #moved { position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 12px); transform: translateX(-50%); z-index: 30;
  display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 16px; border-radius: 14px; background: var(--card); color: var(--ink);
  border: 1px solid var(--line); box-shadow: 0 8px 28px rgba(0,0,0,.18); font-size: 14px; font-weight: 600; max-width: calc(100vw - 32px); }
#upd[hidden], #moved[hidden] { display: none; }
#moved { top: calc(env(safe-area-inset-top) + 70px); }
#moved a.btn { text-decoration: none; }
#upd .btn, #moved .btn { padding: 8px 12px; font-size: 13px; }

#stgBadge { position: fixed; left: 0; bottom: 0; z-index: 40; padding: 3px 10px; border-top-right-radius: 8px; background: #c0392b; color: #fff;
  font: 700 11px/1.4 system-ui, sans-serif; letter-spacing: .08em; pointer-events: none; padding-bottom: calc(3px + env(safe-area-inset-bottom)); }
