/* ============ متغیرها و تم ============ */
:root {
  --pw: 550px;
  --ph: 733px;
  --accent: #6366f1;
  --accent-2: #ec4899;
  --bar-h: 58px;
  --top-h: 52px;
  --radius: 12px;
  --font: "Vazirmatn", Tahoma, "Segoe UI", system-ui, sans-serif;
}
body[data-theme="dark"] {
  --bg: radial-gradient(1200px 700px at 50% 20%, #1e293b 0%, #0b1220 70%);
  --bar: rgba(15, 23, 42, .88);
  --bar-fg: #e2e8f0;
  --bar-hover: rgba(255, 255, 255, .1);
  --panel: #111827;
  --panel-fg: #e5e7eb;
  --line: rgba(255, 255, 255, .1);
  --card: #1f2937;
}
body[data-theme="light"] {
  --bg: radial-gradient(1200px 700px at 50% 20%, #ffffff 0%, #dbe3ee 80%);
  --bar: rgba(255, 255, 255, .92);
  --bar-fg: #1e293b;
  --bar-hover: rgba(0, 0, 0, .06);
  --panel: #ffffff;
  --panel-fg: #1f2937;
  --line: rgba(0, 0, 0, .1);
  --card: #f1f5f9;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; font-family: var(--font); }
body { background: var(--bg); color: var(--bar-fg); overscroll-behavior: none; }
button, input { font-family: inherit; }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* ============ نوار بالا ============ */
.topbar {
  height: var(--top-h); flex: none; display: flex; align-items: center; gap: 12px;
  padding: 0 14px; padding-top: env(safe-area-inset-top);
  background: var(--bar); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); z-index: 20;
}
.brand { font-weight: 800; font-size: 18px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.title { flex: 1; text-align: center; font-size: 14px; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 4px; }

/* ============ دکمه‌ها ============ */
.ib {
  width: 40px; height: 40px; border-radius: 10px; border: 0; background: transparent; color: var(--bar-fg);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer; transition: background .15s, transform .1s;
  text-decoration: none; flex: none;
}
.ib:hover { background: var(--bar-hover); }
.ib:active { transform: scale(.93); }
.ib.active { background: var(--accent); color: #fff; }
.ib[disabled] { opacity: .3; pointer-events: none; }
.ib svg { width: 22px; height: 22px; }
.btn {
  border: 1px solid var(--line); background: var(--card); color: var(--panel-fg); border-radius: 10px; padding: 9px 14px;
  cursor: pointer; font-size: 14px; text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
}
.btn.primary { background: linear-gradient(90deg, var(--accent), var(--accent-2)); color: #fff; border-color: transparent; }

/* ============ استیج ============ */
.stage { position: relative; flex: 1; min-height: 0; overflow: hidden; user-select: none; -webkit-user-select: none; touch-action: none; }
.zoom-layer {
  position: absolute; inset: 14px 54px; display: flex; align-items: center; justify-content: center;
  /* Allow rerasterization at the new zoom scale; do not lock a low-resolution layer. */
  transform-origin: center center;
}
.book-wrap { width: 100%; height: 100%; position: relative; }
#book { position: relative; width: 100%; height: 100%; }
.pan-layer { position: absolute; inset: 0; z-index: 5; cursor: grab; }
.pan-layer:active { cursor: grabbing; }

.side-nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 6; width: 40px; height: 72px; border: 0; border-radius: 10px;
  background: var(--bar); color: var(--bar-fg); cursor: pointer; display: flex; align-items: center; justify-content: center; opacity: .55; transition: opacity .2s;
}
.side-nav:hover { opacity: 1; }
.side-nav svg { width: 24px; height: 24px; }
.side-prev { left: 6px; }   /* کتاب به‌صورت فیزیکی چپ‌به‌راست ورق می‌خورد */
.side-next { right: 6px; }

/* ============ صفحه‌ها ============ */
.page { background: #fdfcf8; overflow: hidden; }
.page-inner {
  width: var(--pw); height: var(--ph); transform-origin: 0 0; transform: scale(var(--s, 1));
  position: absolute; top: 0; left: 0; direction: rtl; color: #1e293b; font-size: 17px; line-height: 2;
}
.page-inner > img { width: 100%; height: 100%; object-fit: contain; display: block; -webkit-user-drag: none; }
.page::after { /* سایهٔ فرورفتگی وسط کتاب */
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(to right, rgba(0, 0, 0, .0) 90%, rgba(0, 0, 0, .06));
}
.page.--right::after { background: linear-gradient(to left, rgba(0, 0, 0, 0) 90%, rgba(0, 0, 0, .06)); }
.page .pnum { position: absolute; bottom: 18px; left: 0; right: 0; text-align: center; font-size: 13px; color: #94a3b8; z-index: 2; }

.pg { padding: 56px 48px; height: 100%; }
.pg-soft { background: linear-gradient(160deg, #fdf2f8, #eef2ff); }
.pg h2 { font-size: 30px; line-height: 1.5; margin: 6px 0 14px; font-weight: 800; }
.pg h3 { font-size: 24px; margin: 0 0 14px; font-weight: 800; }
.pg p { margin: 0 0 14px; }
.pg .lead { font-size: 20px; font-weight: 600; color: #4338ca; }
.pg .muted { color: #64748b; font-size: 14px; }
.pg a { color: var(--accent); }
.kicker { display: inline-block; font-size: 13px; font-weight: 600; letter-spacing: 0; color: #fff; background: linear-gradient(90deg, var(--accent), var(--accent-2)); padding: 2px 12px; border-radius: 99px; line-height: 1.9; }
blockquote { margin: 18px 0; padding: 10px 18px; border-right: 4px solid var(--accent-2); background: rgba(99, 102, 241, .07); border-radius: 8px; font-weight: 600; }
.btn-link { display: inline-block; margin-top: 8px; padding: 8px 18px; border-radius: 99px; background: var(--accent); color: #fff !important; text-decoration: none; cursor: pointer; font-size: 15px; }
.tips { padding-right: 20px; }

.cover-front, .cover-back {
  width: 100%; height: 100%; background-size: cover; background-position: center; color: #fff; padding: 60px 44px; position: relative;
  display: flex; flex-direction: column; justify-content: flex-end; text-shadow: 0 2px 20px rgba(0, 0, 0, .35);
}
.cover-front h1 { font-size: 78px; line-height: 1.15; margin: 0 0 14px; font-weight: 800; }
.cover-front p, .cover-back p { font-size: 20px; margin: 0; }
.cover-badge { position: absolute; top: 44px; right: 44px; background: rgba(255, 255, 255, .22); backdrop-filter: blur(6px); padding: 4px 16px; border-radius: 99px; font-size: 15px; }
.cover-back { justify-content: center; align-items: center; text-align: center; }
.cover-back h2 { font-size: 54px; margin: 0 0 8px; }

.gal { display: grid; gap: 18px; margin-top: 18px; }
.gal figure { margin: 0; }
.gal img { width: 100%; height: 235px; object-fit: cover; border-radius: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, .18); display: block; }
.gal figcaption { text-align: center; font-size: 14px; color: #64748b; margin-top: 4px; }

.pg-hero { position: relative; padding-top: 340px; }
.pg-hero .hero-img { position: absolute; top: 0; left: 0; right: 0; height: 320px; background: var(--hero) center/cover; border-bottom-left-radius: 40px; border-bottom-right-radius: 40px; }
.pg-hero .kicker { margin-top: 8px; }

.steps { list-style: none; counter-reset: s; padding: 0; margin: 0 0 14px; }
.steps li { counter-increment: s; position: relative; padding: 4px 56px 4px 0; margin-bottom: 14px; }
.steps li::before { content: counter(s, persian); position: absolute; right: 0; top: 6px; width: 40px; height: 40px; border-radius: 50%; background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 800; }
.steps b { display: block; }
.steps span { color: #64748b; font-size: 15px; line-height: 1.7; display: block; }

.video-card { height: 250px; border-radius: 16px; background-size: cover; background-position: center; display: flex; align-items: center; justify-content: center; cursor: pointer; margin: 18px 0 8px; box-shadow: 0 8px 24px rgba(0, 0, 0, .25); }
.play { width: 76px; height: 76px; border-radius: 50%; border: 0; background: rgba(255, 255, 255, .92); color: var(--accent); cursor: pointer; display: flex; align-items: center; justify-content: center; box-shadow: 0 0 0 10px rgba(255, 255, 255, .25); }

.bars { display: flex; align-items: flex-end; gap: 12px; height: 330px; margin: 26px 0 8px; padding: 0 4px 30px; border-bottom: 2px solid #e2e8f0; }
.bars div { flex: 1; height: var(--h); background: linear-gradient(to top, var(--accent), var(--accent-2)); border-radius: 8px 8px 0 0; position: relative; }
.bars i { position: absolute; top: -26px; left: 0; right: 0; text-align: center; font-style: normal; font-size: 14px; font-weight: 600; line-height: 1.5; }
.bars span { position: absolute; bottom: -30px; left: 0; right: 0; text-align: center; font-size: 12px; color: #64748b; line-height: 1.5; }

.tbl { width: 100%; border-collapse: collapse; margin: 6px 0 18px; font-size: 16px; }
.tbl th, .tbl td { padding: 8px 10px; text-align: right; border-bottom: 1px solid #e2e8f0; line-height: 1.7; }
.tbl th { background: #eef2ff; color: #3730a3; }

mark.hl { background: #fde047; color: #111; border-radius: 3px; padding: 0 2px; }

/* ============ نوار ابزار پایین ============ */
.toolbar {
  height: calc(var(--bar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); flex: none;
  display: flex; align-items: center; justify-content: space-between; gap: 6px; padding-inline: 10px;
  background: var(--bar); backdrop-filter: blur(10px); border-top: 1px solid var(--line); z-index: 20;
}
.tb-group { display: flex; align-items: center; gap: 2px; }
.tb-group.nav { gap: 4px; direction: ltr; }
.pager { display: flex; align-items: center; gap: 6px; font-size: 14px; padding: 0 6px; direction: ltr; }
.pager input {
  width: 66px; height: 34px; text-align: center; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: inherit; font-size: 14px; outline: none;
}
.pager input:focus { border-color: var(--accent); }
.ib.more { display: none; }

/* ============ پنل‌های کناری ============ */
.panel {
  position: fixed; top: 0; bottom: 0; right: 0; width: min(340px, 88vw); background: var(--panel); color: var(--panel-fg); z-index: 40;
  transform: translateX(105%); transition: transform .28s ease; display: flex; flex-direction: column; box-shadow: -8px 0 30px rgba(0, 0, 0, .35);
  padding-top: env(safe-area-inset-top);
}
.panel.open { transform: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.panel-head h3 { margin: 0; font-size: 16px; }
.panel-head .ib { color: var(--panel-fg); }
.panel-body { flex: 1; overflow: auto; padding: 12px; -webkit-overflow-scrolling: touch; }
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 30; }

.thumbs { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; align-content: start; }
.thumb { cursor: pointer; text-align: center; font-size: 12px; }
.thumb:hover .tp { box-shadow: 0 0 0 2px var(--accent-2); }
.thumb .tp { width: 100%; aspect-ratio: var(--pw-n) / var(--ph-n); position: relative; overflow: hidden; border-radius: 6px; border: 2px solid transparent; background: #fff; box-shadow: 0 2px 8px rgba(0, 0, 0, .3); }
.thumb.current .tp { border-color: var(--accent); }
.thumb .tp > div { position: absolute; top: 0; left: 0; width: var(--pw); height: var(--ph); transform-origin: 0 0; pointer-events: none; }
.thumb .tp img { width: 100%; height: 100%; object-fit: contain; }
.thumb span { display: block; margin-top: 4px; }

.toc-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; background: transparent; border: 0; border-bottom: 1px dashed var(--line); color: inherit; padding: 12px 6px; cursor: pointer; font-size: 15px; text-align: right; }
.toc-item:hover { background: var(--bar-hover); }
.toc-item em { font-style: normal; opacity: .6; font-size: 13px; direction: ltr; }

.search-box { padding: 12px 12px 0; }
.search-box input { width: 100%; height: 42px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: inherit; padding: 0 12px; font-size: 15px; outline: none; }
.search-box input:focus { border-color: var(--accent); }
.res { padding: 10px; border-radius: 10px; background: var(--card); margin-bottom: 8px; cursor: pointer; font-size: 14px; line-height: 1.8; }
.res b { color: var(--accent-2); display: block; font-size: 12px; }
.res mark { background: #fde047; color: #111; border-radius: 3px; }
.empty { opacity: .6; text-align: center; padding: 30px 10px; font-size: 14px; }

/* ============ مودال، توست ============ */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .6); z-index: 60; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-card { background: var(--panel); color: var(--panel-fg); width: min(520px, 100%); border-radius: 16px; padding: 14px 18px 20px; position: relative; }
.modal-card label { display: block; font-size: 13px; opacity: .7; margin: 14px 0 6px; }
.modal-card .panel-head { padding: 4px 0 10px; margin-bottom: 4px; }
.copy-row { display: flex; gap: 8px; }
.copy-row input { flex: 1; min-width: 0; direction: ltr; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: inherit; padding: 0 10px; font-size: 13px; }
.share-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; }
.share-btns .btn { flex: 1 1 120px; }
.modal-card.video { width: min(900px, 100%); padding: 0; background: #000; overflow: hidden; }
.modal-card.video iframe, .modal-card.video video { width: 100%; aspect-ratio: 16 / 9; border: 0; display: block; }
.close-float { position: absolute; top: 6px; left: 6px; z-index: 2; background: rgba(0, 0, 0, .6); color: #fff; }

.toast { position: fixed; bottom: calc(var(--bar-h) + 26px); left: 50%; transform: translateX(-50%); background: #111827; color: #fff; padding: 9px 18px; border-radius: 99px; font-size: 14px; z-index: 80; box-shadow: 0 6px 20px rgba(0, 0, 0, .4); }

.loader { position: absolute; inset: 0; display: flex; flex-direction: column; gap: 12px; align-items: center; justify-content: center; z-index: 10; background: transparent; pointer-events: none; font-size: 14px; }
.spin { width: 40px; height: 40px; border-radius: 50%; border: 4px solid rgba(255, 255, 255, .2); border-top-color: var(--accent); animation: sp .8s linear infinite; }
@keyframes sp { to { transform: rotate(360deg); } }

.more-menu {
  position: fixed; bottom: calc(var(--bar-h) + 8px + env(safe-area-inset-bottom)); left: 8px; right: 8px; z-index: 35; background: var(--bar); backdrop-filter: blur(12px);
  border: 1px solid var(--line); border-radius: 16px; padding: 8px; display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
}

/* ============ موبایل ============ */
@media (max-width: 760px) {
  :root { --bar-h: 54px; --top-h: 46px; }
  .title { display: none; }
  .brand { flex: 1; }
  .toolbar .sec { display: none; }
  .ib.more { display: inline-flex; }
  .zoom-layer { inset: 8px 8px; }
  .side-nav { display: none; }
  .pager input { width: 52px; }
  .ib { width: 38px; height: 38px; }
}
@media (max-width: 760px) and (orientation: landscape) {
  :root { --bar-h: 46px; --top-h: 38px; }
  .topbar { padding-top: 0; }
}
.more-menu .sec { display: inline-flex !important; }

/* حالت تمام‌صفحهٔ جایگزین (iOS) */
body.immersive .topbar { display: none; }

/* Coordinates are percentages of the original page, and scale with it. */
.page-overlay { position: absolute; z-index: 3; display: flex; align-items: center;
  justify-content: center; min-width: 40px; min-height: 40px; padding: 6px 12px;
  background: #4f46e5; border: 2px solid white; border-radius: 12px;
  color: white; font: bold 16px var(--font); text-decoration: none; cursor: pointer; }
.page-overlay:focus-visible { outline: 4px solid #fbbf24; }
.page-overlay.inline-video { padding: 0; background: #000; object-fit: contain; }

/* Flat high-resolution image reading, outside StPageFlip's 3D layer. */
.zoom-reader { position: absolute; z-index: 4; display: flex; direction: ltr; cursor: grab; }
.zoom-reader:active { cursor: grabbing; }
.zoom-reader-page { position: relative; flex: none; overflow: hidden; background: #fdfcf8; }
.zoom-reader-page > img { display: block; width: 100%; height: 100%; object-fit: contain;
  -webkit-user-drag: none; pointer-events: none; }
.zoom-reader-page > .page-overlay { font-size: calc(16px * var(--reader-scale, 1)); }

/* Image pages use physical page dimensions, without a second scaling transform. */
.image-page > .page-inner { width: 100%; height: 100%; transform: none; }
.image-page > .page-inner > .page-overlay { font-size: calc(16px * var(--s, 1)); }

/* Header logo: keep the uploaded SVG vector at every display density. */
.brand.has-logo-image { display: flex; align-items: center; background: none; color: inherit; }
.brand.has-logo-image #logoText { display: inline-flex; align-items: center; }
.brand-logo { display: block; width: 36px; height: 36px; object-fit: contain; }
body[data-theme="dark"] .brand-logo { filter: invert(1); }
@media (max-width: 760px) { .brand-logo { width: 30px; height: 30px; } }

/* Physical RTL binding: mirror the engine, then unmirror each page's content. */
body[data-book-direction="rtl"] .book-wrap { transform: scaleX(-1); }
body[data-book-direction="rtl"] .page-inner {
  transform: scale(var(--s, 1)) translateX(100%) scaleX(-1);
  transform-origin: 0 0;
}
body[data-book-direction="rtl"] .image-page > .page-inner {
  transform: scaleX(-1); transform-origin: center center;
}
body[data-book-direction="rtl"] .page .pnum { transform: scaleX(-1); }
body[data-book-direction="rtl"] .zoom-reader { direction: rtl; }
body[data-book-direction="rtl"] .tb-group.nav { direction: rtl; }
body[data-book-direction="rtl"] .tb-group.nav > button svg,
body[data-book-direction="rtl"] .side-nav svg { transform: scaleX(-1); }
body[data-book-direction="rtl"] .side-prev { left: auto; right: 6px; }
body[data-book-direction="rtl"] .side-next { right: auto; left: 6px; }
