/* ==========================================================================
   MADINA MUSHAF 15-LINE LAYOUT ENGINE - STYLESHEET
   Conforms to the 1405 AH Old Madina Mushaf while adhering strictly to the
   design system, color tokens, and theme of the application.
   ========================================================================== */

:root {
  --quran-font-family: 'Hafs', 'me_quran', 'Amiri_Quran', 'Uthman', serif;
  --quran-font-size: 16px;
  --target-line-width: 270px;
  --mushaf-line-height-factor: 2.15;

  /* Theme tokens for Mushaf reader - Light mode (default) */
  --mushaf-modal-bg: var(--bg);
  --mushaf-page-bg: #fffefb;
  --mushaf-page-border: var(--border-color);
  --mushaf-inner-border: hsl(var(--border) / 0.8);
  --mushaf-text-color: var(--fg);
  --mushaf-accent-gold: #966e2d;
  --mushaf-gold-glow: rgba(150, 110, 45, 0.28);
  --mushaf-surah-bg: hsl(var(--secondary) / 0.7);
  --mushaf-surah-border: hsl(var(--border));
  --mushaf-hover-bg: hsl(var(--accent) / 0.8);
  --mushaf-active-bg: hsl(var(--primary) / 0.12);
  --mushaf-playing-bg: hsl(var(--primary) / 0.12);
  --mushaf-active-word-bg: var(--primary-bg);
  --mushaf-active-word-fg: var(--primary-fg);
  --mushaf-shadow: 0 4px 20px -2px rgba(0, 0, 0, 0.08), 0 0 0 1px var(--border-color);
}

/* Dark mode theme adaptations */
[data-theme="dark"] {
  --mushaf-modal-bg: var(--bg);
  --mushaf-page-bg: hsl(192 24% 9.5%);
  --mushaf-page-border: var(--border-color);
  --mushaf-inner-border: hsl(190 18% 22%);
  --mushaf-text-color: var(--fg);
  --mushaf-accent-gold: #e5c07b;
  --mushaf-gold-glow: rgba(229, 192, 123, 0.35);
  --mushaf-surah-bg: hsl(var(--secondary) / 0.5);
  --mushaf-surah-border: hsl(var(--border));
  --mushaf-hover-bg: hsl(var(--accent) / 0.5);
  --mushaf-active-bg: hsl(var(--primary) / 0.22);
  --mushaf-playing-bg: rgba(56, 189, 248, 0.18);
  --mushaf-active-word-bg: #e5c07b;
  --mushaf-active-word-fg: #020817;
  --mushaf-shadow: 0 10px 30px -5px rgba(0, 0, 0, 0.55), 0 0 0 1px var(--border-color);
}

/* Warm sepia theme adaptations */
[data-theme="sepia"] {
  --mushaf-modal-bg: var(--bg);
  --mushaf-page-bg: #f8f2e4;
  --mushaf-page-border: var(--border-color);
  --mushaf-inner-border: hsl(35 20% 74%);
  --mushaf-text-color: var(--fg);
  --mushaf-accent-gold: #8c5e1e;
  --mushaf-gold-glow: rgba(140, 94, 30, 0.3);
  --mushaf-surah-bg: hsl(var(--secondary) / 0.7);
  --mushaf-surah-border: hsl(var(--border));
  --mushaf-hover-bg: hsl(var(--accent) / 0.8);
  --mushaf-active-bg: hsl(var(--primary) / 0.18);
  --mushaf-playing-bg: rgba(14, 116, 144, 0.16);
  --mushaf-active-word-bg: #8c5e1e;
  --mushaf-active-word-fg: #fcf9f2;
  --mushaf-shadow: 0 6px 24px -4px rgba(80, 60, 40, 0.15), 0 0 0 1px var(--border-color);
}

/* Fullscreen Immersive Reading Modal */
.mushaf-fullscreen-modal {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  background-color: var(--mushaf-modal-bg);
  color: var(--fg);
  font-family: 'IBM Plex Sans Arabic', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  z-index: 3000;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: text;
  direction: rtl;
  box-sizing: border-box;
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* Minimal Top Bar — Borderless & Unobtrusive */
.mushaf-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.375rem;
  padding: 0.5rem 0.75rem;
  background-color: transparent;
  border-bottom: none;
  min-height: 48px;
  z-index: 10;
  color: var(--fg);
  box-sizing: border-box;
}

.mushaf-topbar-section {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  min-width: 0;
}

.mushaf-topbar-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.1rem;
  min-width: 0;
}

.mushaf-topbar-title {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--fg);
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mushaf-topbar-sub {
  font-size: 0.75rem;
  color: var(--muted-fg);
  line-height: 1.2;
  white-space: nowrap;
}

/* Reader Viewport Area — Edge-to-Edge Mobile First */
.mushaf-viewport {
  position: relative;
  flex: 1;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  padding: 0.25rem 0;
  box-sizing: border-box;
}

.mushaf-page-wrapper {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  width: 100%;
  margin: auto 0;
  overflow: visible;
}

/* Floating Screen-Edge Navigation Chevrons (Desktop/Tablet) */
.mushaf-nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background-color: hsl(var(--card) / 0.92);
  color: var(--fg);
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.35rem;
  cursor: pointer;
  z-index: 20;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  user-select: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.mushaf-nav-btn:hover:not(:disabled) {
  background-color: var(--accent-bg);
  color: var(--accent-fg);
  border-color: var(--primary-bg);
  transform: translateY(-50%) scale(1.08);
}

.mushaf-nav-btn:active:not(:disabled) {
  transform: translateY(-50%) scale(0.96);
}

.mushaf-nav-btn:disabled {
  opacity: 0.25;
  cursor: not-allowed;
  pointer-events: none;
}

/* In RTL: Next Page is to the left (advance forward in book) */
.mushaf-nav-btn.next-btn {
  left: 1rem;
}

/* In RTL: Previous Page is to the right (go back) */
.mushaf-nav-btn.prev-btn {
  right: 1rem;
}

@media (max-width: 640px) {
  /* Hide floating chevrons on mobile so they never overlap Quran text */
  .mushaf-nav-btn {
    display: none;
  }
}

/* The 15-Line Madina Mushaf Page — Borderless, Edge-to-Edge Reading Surface */
.mushaf-page {
  position: relative;
  flex: none;
  background-color: transparent;
  border: none;
  border-radius: 0;
  box-shadow: none;
  padding: 0 4px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  user-select: text;
  direction: rtl;
  margin: 0 auto;
  max-width: none;
  box-sizing: border-box;
}

/* Page Header & Footer hidden inside fullscreen reader (info lives cleanly in topbar) */
.mushaf-fullscreen-modal .mushaf-page-header,
.mushaf-fullscreen-modal .mushaf-page-footer {
  display: none;
}

.mushaf-page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  margin-bottom: 6px;
  padding: 0 2px 4px 2px;
  border-bottom: none;
  font-size: 11px;
  font-weight: 500;
  color: var(--muted-fg);
}

.mushaf-header-surah {
  font-family: var(--quran-font-family);
  font-size: 13px;
  color: var(--fg);
}

.mushaf-header-juz {
  font-size: 11px;
  color: var(--muted-fg);
}

/* 15 Lines Column Container */
.mushaf-lines-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 0;
}

/* Individual Quran Line (1 to 15) */
.quran-line {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  height: calc(var(--quran-font-size) * var(--mushaf-line-height-factor));
  direction: rtl;
  overflow: visible;
}

/* Line Content Wrapper (intrinsic width, never flex-shrunk, so scaleX justifies every line identically) */
.line-content {
  display: inline-block;
  direction: rtl;
  width: max-content;
  min-width: 0;
  flex-shrink: 0;
  height: 100%;
  text-align: right;
  white-space: pre;
  transform-origin: 100% 0%; /* Right Top origin for RTL justification */
  will-change: transform;
}

/* Unjustified Centered Lines (Stretch = -1, e.g. Page 1, Short Lines, Basmalah) */
.line-content.centered {
  margin-inline: auto;
  text-align: center;
  transform-origin: 50% 0%;
}

/* Hidden measurer for original Hafs marker advance widths (1, 2, 3 digits) */
.ayah-marker-measurer {
  position: absolute;
  top: 0;
  left: 0;
  visibility: hidden;
  pointer-events: none;
  white-space: pre;
  font-family: var(--quran-font-family);
  font-size: var(--quran-font-size);
}

/* Quranpedia Ayah Marker (015-regular rosette, crisp print palette across all themes) */
.ayah-marker-defs {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
  pointer-events: none;
}

.ayah-mark-svg {
  --fill-base: #fff8e7;
  --fill-1: #f4e9bc;
  --ink-base: #083a3a;
  position: relative;
  z-index: 1;
  display: inline-block;
  vertical-align: -0.22em;
  margin-inline: 0.08em;
  line-height: 0;
}

.ayah-mark-svg svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.ayah-mark-num {
  font-family: 'IBM Plex Sans Arabic', var(--quran-font-family), sans-serif;
  font-weight: 700;
  fill: var(--ink-base);
}

/* Continuous text/symbol chunks inside a part (spaces, waqf marks, rubu_al_hizb, sajdah) */
.quran-chunk {
  display: inline;
  white-space: pre;
}

/* Clean Surah Title Banner (no double ornamental box) */
.surah-title-frame {
  position: absolute;
  top: 2px;
  left: 0;
  right: 0;
  bottom: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: hsl(var(--muted) / 0.6);
  border-radius: var(--radius);
  z-index: 2;
}

.surah-title-text {
  font-family: var(--quran-font-family);
  font-size: calc(var(--quran-font-size) * 1.15);
  font-weight: bold;
  color: var(--fg);
  z-index: 3;
}

/* Basmalah Ligature Line */
.basmalah-ligature {
  font-family: var(--quran-font-family);
  font-size: calc(var(--quran-font-size) * 1.2);
  color: var(--fg);
  opacity: 0.95;
  text-align: center;
  width: 100%;
  display: block;
}

/* Quran Part Span (Fragment of Ayah) */
.quran-part {
  display: inline;
  font-family: var(--quran-font-family);
  font-size: var(--quran-font-size);
  color: var(--mushaf-text-color);
  line-height: var(--mushaf-line-height-factor);
  white-space: pre;
  cursor: pointer;
  border-radius: 3px;
  padding: 1px 0;
  transition: background-color 0.12s ease;
}

/* Individual Words inside Ayah Fragments (for real-time word tracking) */
.quran-word {
  display: inline;
  cursor: pointer;
  border-radius: 3px;
  padding: 0;
  transition: background-color 0.1s ease, color 0.1s ease;
}

.quran-word:hover {
  background-color: var(--mushaf-hover-bg);
}

/* Active Reading Word Highlighting */
.quran-word.reading-word-active {
  background-color: var(--mushaf-active-word-bg) !important;
  color: var(--mushaf-active-word-fg) !important;
  border-radius: 3px;
  box-shadow: 0 0 10px var(--mushaf-gold-glow);
}

/* Synchronized Hover Highlighting */
.quran-part.is-hovered,
.quran-word.is-hovered {
  background-color: var(--mushaf-hover-bg);
}

/* Audio Playing Ayah Highlighting */
.quran-part.playing-ayah {
  background-color: var(--mushaf-playing-bg) !important;
  border-radius: 3px;
}

/* Page Footer (no border) */
.mushaf-page-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  margin-top: 4px;
  padding-top: 4px;
  border-top: none;
  font-size: 11px;
  color: var(--muted-fg);
}

/* Minimal Bottom Bar */
.mushaf-bottombar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem calc(0.5rem + env(safe-area-inset-bottom, 0px)) 0.75rem;
  background-color: hsl(var(--card) / 0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--border-color);
  min-height: 52px;
  z-index: 10;
  color: var(--fg);
  box-sizing: border-box;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.mushaf-bottom-nav-group {
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.mushaf-bottom-nav-btn {
  height: 2rem;
  padding: 0 0.625rem;
  font-size: 0.75rem;
  font-weight: 500;
  border-radius: calc(var(--radius) - 2px);
  border: 1px solid var(--border-color);
  background-color: var(--card-bg);
  color: var(--fg);
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.mushaf-bottom-nav-btn:hover:not(:disabled) {
  background-color: var(--accent-bg);
  color: var(--accent-fg);
}

.mushaf-bottom-nav-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.mushaf-section-badge {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary-fg);
  background-color: var(--primary-bg);
  border-radius: 9999px;
  padding: 0.25rem 0.75rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  white-space: nowrap;
}

.mushaf-tip-text {
  font-size: 0.75rem;
  color: var(--muted-fg);
}

@media (max-width: 640px) {
  .mushaf-tip-text {
    display: none;
  }
}

/* Self-Test Blur Mode (blur individual words so tapping any word reveals it cleanly) */
.mushaf-blur-mode .mushaf-word,
.mushaf-blur-mode .quran-word,
.mushaf-blur-mode .word {
  filter: blur(6px);
  cursor: pointer;
  transition: filter 0.2s ease;
  user-select: none;
  -webkit-user-select: none;
}

.mushaf-blur-mode .revealed {
  filter: none !important;
}

.mushaf-selftest-btn.active,
.reading-audio-btn.active {
  background-color: var(--primary-bg);
  color: var(--primary-fg);
}

.mushaf-bottombar {
  background-color: transparent !important;
  border-top: none !important;
  padding: 0.5rem 0.875rem calc(0.5rem + env(safe-area-inset-bottom, 0px)) !important;
}
