*{box-sizing:border-box}
:root{
  color-scheme:dark;
  --bg:#06172a;
  --panel:#0b223a;
  --line:rgba(185,203,216,.22);
  --text:#f5f1e8;
  --muted:#afc0cc;
  --cyan:#23d7de;
  --gold:#d2b46b;
}
html,body{margin:0;min-height:100%;background:
radial-gradient(circle at 50% -15%,rgba(27,102,130,.32),transparent 42%),
var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Segoe UI",Tahoma,sans-serif}
body{padding-bottom:max(24px,env(safe-area-inset-bottom))}
button,a,input{font:inherit}
button,a{touch-action:manipulation}
.topbar{
  min-height:72px;
  display:grid;
  grid-template-columns:1fr auto 1fr;
  align-items:center;
  gap:12px;
  padding:10px max(14px,env(safe-area-inset-right)) 10px max(14px,env(safe-area-inset-left));
  border-bottom:1px solid var(--line);
  background:rgba(5,22,40,.92);
  backdrop-filter:blur(14px);
  position:sticky;
  top:0;
  z-index:20
}
.back{
  justify-self:start;
  color:var(--text);
  text-decoration:none;
  padding:9px 12px;
  border:1px solid rgba(210,180,107,.38);
  border-radius:12px;
  background:rgba(210,180,107,.08);
  font-weight:700
}
.title{display:flex;align-items:center;gap:9px;text-align:center}
.title>span{color:var(--gold);font-size:1.55rem}
.title h1{font-size:1.1rem;margin:0}
.title p{font-size:.72rem;color:var(--muted);margin:2px 0 0}
.icon-btn{
  justify-self:end;
  width:42px;height:42px;border-radius:12px;
  border:1px solid var(--line);
  background:rgba(255,255,255,.045);
  color:var(--text);font-size:1.25rem
}
main{width:min(100%,1040px);margin:auto;padding:14px 10px 24px}
.reader-controls{
  display:grid;
  grid-template-columns:auto 1fr auto;
  align-items:center;
  gap:10px;
  margin-bottom:12px
}
.reader-controls>button{
  min-width:78px;
  min-height:43px;
  border-radius:12px;
  border:1px solid rgba(35,215,222,.32);
  background:rgba(35,215,222,.07);
  color:var(--text);
  font-weight:700
}
.reader-controls>button:disabled{opacity:.35}
#pageForm{
  display:flex;
  justify-content:center;
  align-items:center;
  gap:7px;
  color:var(--muted);
  font-size:.82rem
}
#pageInput{
  width:68px;
  height:42px;
  text-align:center;
  border:1px solid var(--line);
  border-radius:10px;
  background:rgba(255,255,255,.06);
  color:var(--text);
  font-weight:800
}
.mushaf-stage{
  position:relative;
  overflow:auto;
  width:100%;
  min-height:65vh;
  display:flex;
  align-items:flex-start;
  justify-content:center;
  border:1px solid var(--line);
  border-radius:18px;
  background:#f3eedf;
  box-shadow:0 18px 45px rgba(0,0,0,.28);
  overscroll-behavior:contain
}
#mushafPage{
  display:block;
  width:auto;
  max-width:100%;
  height:auto;
  margin:auto;
  transform-origin:top center;
  user-select:none;
  -webkit-user-select:none
}
.loader{
  position:absolute;
  inset:0;
  display:grid;
  place-items:center;
  color:#344;
  background:#f3eedf;
  z-index:2;
  font-weight:700
}
.loader.hidden{display:none}
.zoom-controls{
  display:flex;
  justify-content:center;
  gap:8px;
  margin-top:12px
}
.zoom-controls button{
  min-width:48px;
  height:40px;
  border:1px solid var(--line);
  border-radius:11px;
  background:rgba(255,255,255,.05);
  color:var(--text);
  font-weight:700
}
#zoomResetBtn{min-width:70px}
.hint{text-align:center;color:var(--muted);font-size:.73rem;line-height:1.7;margin:12px 8px 0}

@media(max-width:600px){
  .topbar{grid-template-columns:auto 1fr auto}
  .back{font-size:.76rem;padding:8px 9px}
  .title h1{font-size:.98rem}
  .title p{font-size:.62rem}
  main{padding:10px 6px 22px}
  .reader-controls{gap:6px}
  .reader-controls>button{min-width:65px;font-size:.78rem}
  #pageForm{gap:4px;font-size:.7rem}
  #pageInput{width:58px}
  .mushaf-stage{border-radius:12px;min-height:68vh}
}


/* AFAQ MUSHAF INDEX */
.top-actions{
  justify-self:end;
  display:flex;
  align-items:center;
  gap:7px
}

.index-btn{
  min-height:42px;
  padding:0 11px;
  border-radius:12px;
  border:1px solid rgba(210,180,107,.38);
  background:rgba(210,180,107,.08);
  color:var(--text);
  font-weight:700
}

.index-sheet{
  position:fixed;
  inset:0;
  z-index:100;
  display:flex;
  align-items:flex-end;
  justify-content:center
}

.index-sheet.hidden{display:none}

.index-backdrop{
  position:absolute;
  inset:0;
  background:rgba(0,0,0,.65);
  backdrop-filter:blur(3px)
}

.index-panel{
  position:relative;
  z-index:1;
  width:min(760px,100%);
  max-height:82vh;
  display:flex;
  flex-direction:column;
  border:1px solid rgba(190,207,219,.22);
  border-radius:22px 22px 0 0;
  background:#081d32;
  box-shadow:0 -20px 55px rgba(0,0,0,.38);
  overflow:hidden
}

.index-head{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
  padding:16px;
  border-bottom:1px solid var(--line)
}

.index-head h2{
  margin:0;
  font-size:1.2rem
}

.index-head p{
  margin:3px 0 0;
  color:var(--muted);
  font-size:.73rem
}

.index-close{
  width:42px;
  height:42px;
  flex:0 0 auto;
  border-radius:12px;
  border:1px solid var(--line);
  background:rgba(255,255,255,.05);
  color:var(--text)
}

.index-tabs{
  display:grid;
  grid-template-columns:repeat(3,1fr);
  gap:7px;
  padding:10px 12px;
  border-bottom:1px solid var(--line)
}

.index-tab{
  min-height:42px;
  border:1px solid var(--line);
  border-radius:11px;
  background:rgba(255,255,255,.035);
  color:var(--muted);
  font-weight:700
}

.index-tab.active{
  color:#06172a;
  background:var(--cyan);
  border-color:var(--cyan)
}

.index-body{
  overflow:auto;
  padding:12px;
  overscroll-behavior:contain
}

.index-loading{
  text-align:center;
  color:var(--muted);
  padding:30px 10px
}

.surah-list{
  display:grid;
  grid-template-columns:repeat(2,minmax(0,1fr));
  gap:8px
}

.surah-item{
  min-height:62px;
  display:grid;
  grid-template-columns:34px 1fr auto;
  align-items:center;
  gap:9px;
  padding:9px 11px;
  text-align:right;
  border:1px solid var(--line);
  border-radius:13px;
  background:rgba(255,255,255,.035);
  color:var(--text)
}

.surah-item:hover,
.surah-item:focus-visible{
  border-color:rgba(35,215,222,.65);
  outline:none
}

.surah-number{
  width:32px;
  height:32px;
  display:grid;
  place-items:center;
  border-radius:50%;
  color:var(--gold);
  border:1px solid rgba(210,180,107,.32);
  font-size:.72rem
}

.surah-name{
  font-weight:800
}

.surah-page{
  color:var(--muted);
  font-size:.68rem;
  white-space:nowrap
}

.juz-grid,
.pages-grid{
  display:grid;
  grid-template-columns:repeat(5,minmax(0,1fr));
  gap:8px
}

.juz-item,
.page-item{
  min-height:48px;
  border:1px solid var(--line);
  border-radius:11px;
  background:rgba(255,255,255,.035);
  color:var(--text);
  font-weight:700
}

.juz-item{
  min-height:65px;
  line-height:1.5
}

.juz-item small{
  display:block;
  color:var(--muted);
  font-size:.62rem
}

.page-item.current{
  border-color:var(--cyan);
  color:var(--cyan);
  background:rgba(35,215,222,.08)
}

@media(max-width:600px){
  .index-btn span{display:none}
  .index-btn{width:42px;padding:0}

  .surah-list{
    grid-template-columns:1fr
  }

  .juz-grid,
  .pages-grid{
    grid-template-columns:repeat(4,minmax(0,1fr))
  }

  .index-panel{
    max-height:86vh
  }
}

/* AFAQ MUSHAF — zoomed page one-finger pan fix */
.mushaf-stage {
  display: block;
  direction: ltr;
  overflow: auto;
  overscroll-behavior: auto;
  touch-action: pan-x pan-y pinch-zoom;
  -webkit-overflow-scrolling: touch;
}

#mushafPage {
  display: block;
  margin: 0 auto;
}

.loader {
  direction: rtl;
}

/* =======================================================
   AFAQ MUSHAF — FINAL READER EXPERIENCE
   ======================================================= */

.mushaf-stage {
  touch-action: pan-y;
}

#mushafPage {
  transform-origin: center center !important;
  will-change: transform;
  transition: none;
}

.mushaf-stage.is-zoomed {
  touch-action: none !important;
  overflow: hidden !important;
}

/* زر التعليمات */
.reader-help-btn {
  width: 42px;
  height: 42px;
  border: 1px solid rgba(190,207,219,.28);
  border-radius: 12px;
  background: rgba(255,255,255,.05);
  color: #d8e5ed;
  font-size: 22px;
  font-weight: 900;
  cursor: pointer;
}

/* نافذة التعليمات */
.reader-help-overlay {
  position: fixed;
  inset: 0;
  z-index: 120000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(0,0,0,.58);
  backdrop-filter: blur(5px);
}

.reader-help-overlay.hidden {
  display: none;
}

.reader-help-card {
  position: relative;
  width: min(92vw, 430px);
  padding: 24px 20px 18px;
  border: 1px solid rgba(190,207,219,.24);
  border-radius: 20px;
  background: #081d32;
  color: #edf5f8;
  box-shadow: 0 22px 70px rgba(0,0,0,.5);
  direction: rtl;
}

.reader-help-card h2 {
  margin: 0 0 16px;
  text-align: center;
  font-size: 1.15rem;
}

.reader-help-card p {
  margin: 9px 0;
  line-height: 1.8;
  font-size: .9rem;
  color: #d2e0e8;
}

.reader-help-close {
  position: absolute;
  top: 8px;
  left: 10px;
  width: 36px;
  height: 36px;
  border: 0;
  background: transparent;
  color: #fff;
  font-size: 28px;
}

/* إشعار استكمال القراءة */
.reader-resume-toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 110000;
  max-width: calc(100vw - 32px);
  padding: 11px 17px;
  border: 1px solid rgba(190,207,219,.22);
  border-radius: 999px;
  background: rgba(8,29,50,.95);
  color: #edf5f8;
  box-shadow: 0 8px 28px rgba(0,0,0,.35);
  font-size: .82rem;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 18px);
  transition: opacity .2s ease, transform .2s ease;
}

.reader-resume-toast.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translate(-50%, 0);
}

/* صور الصفحات داخل الفهرس */
.pages-grid {
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)) !important;
  gap: 9px !important;
}

.page-item.page-thumb {
  height: auto !important;
  padding: 5px !important;
  overflow: hidden;
}

.page-thumb img {
  display: block;
  width: 100%;
  aspect-ratio: 790 / 1268;
  object-fit: cover;
  border-radius: 7px;
  background: #f3eedf;
}

.page-thumb span {
  display: block;
  padding: 5px 2px 2px;
  font-size: .75rem;
  font-weight: 800;
}

/* وضع القراءة المغمور */
body.reader-focus-open {
  overflow: hidden !important;
}

body.reader-focus-open .topbar,
body.reader-focus-open header,
body.reader-focus-open main > *:not(#mushafStage) {
  display: none !important;
}

body.reader-focus-open #mushafStage {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  height: 100dvh !important;
  min-height: 100dvh !important;
  max-height: 100dvh !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  border-radius: 0 !important;
  background: #000 !important;
  z-index: 100000 !important;
  overflow: hidden !important;
  overscroll-behavior: none !important;
  touch-action: none !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}

body.reader-focus-open #mushafPage {
  display: block !important;
  width: auto !important;
  height: auto !important;
  max-width: 100vw !important;
  max-height: 100dvh !important;
  margin: auto !important;
  object-fit: contain !important;
}

/* × سوداء — منتصف أعلى الشاشة */
.reader-fullscreen-exit-fab {
  display: none;
}

body.reader-focus-open .reader-fullscreen-exit-fab {
  display: grid !important;
  position: fixed !important;
  top: max(9px, env(safe-area-inset-top)) !important;
  left: 50% !important;
  right: auto !important;
  transform: translateX(-50%) !important;
  width: 42px !important;
  height: 42px !important;
  place-items: center !important;
  border: 1px solid rgba(0,0,0,.18) !important;
  border-radius: 50% !important;
  background: rgba(255,255,255,.82) !important;
  color: #111 !important;
  box-shadow: 0 3px 14px rgba(0,0,0,.18) !important;
  font-family: Arial, sans-serif !important;
  font-size: 30px !important;
  font-weight: 500 !important;
  line-height: 1 !important;
  z-index: 100003 !important;
}

body.reader-focus-open .loader {
  z-index: 100002 !important;
}
