/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #faf6ef;
  --bg-page: #fffdf8;
  --text: #2c1810;
  --text-muted: #6b5a4e;
  --accent: #8b5e3c;
  --accent-light: #c49a6c;
  --border: #e0d5c5;
  --sidebar-bg: #f5efe5;
  --sidebar-w: 300px;
  --heading-color: #5a3520;
  --nav-bg: #fffdf8;
  --shadow: 0 2px 12px rgba(44, 24, 16, 0.08);
  --radius: 8px;
}

html {
  font-size: 18px;
  scroll-behavior: smooth;
}

body {
  font-family: 'Amiri', 'Traditional Arabic', 'Scheherazade New', serif;
  background: var(--bg);
  color: var(--text);
  line-height: 2;
  min-height: 100vh;
  display: flex;
}

/* === Sidebar === */
.sidebar {
  position: fixed;
  top: 0;
  right: 0;
  width: var(--sidebar-w);
  height: 100vh;
  background: var(--sidebar-bg);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.3s ease;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
  font-size: 1.1rem;
  color: var(--accent);
  font-weight: 700;
}

.sidebar-close {
  display: none;
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
}

.sidebar-search {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-search input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-page);
  font-family: inherit;
  font-size: 0.85rem;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}

.sidebar-search input:focus {
  border-color: var(--accent-light);
}

.toc-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0;
}

.toc-item {
  display: block;
  width: 100%;
  text-align: right;
  padding: 0.4rem 1.25rem;
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.85rem;
  color: var(--text);
  cursor: pointer;
  line-height: 1.7;
  transition: background 0.15s, color 0.15s;
  border-right: 3px solid transparent;
}

.toc-item:hover {
  background: rgba(139, 94, 60, 0.08);
}

.toc-item.active {
  background: rgba(139, 94, 60, 0.12);
  color: var(--accent);
  border-right-color: var(--accent);
  font-weight: 700;
}

.toc-item .toc-page {
  float: left;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 400;
}

.toc-vol-header {
  padding: 0.6rem 1.25rem 0.3rem;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.02em;
}

/* === Main Content === */
.main {
  flex: 1;
  margin-right: var(--sidebar-w);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.book-header {
  text-align: center;
  padding: 2rem 1.5rem 1rem;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, var(--bg-page) 0%, var(--bg) 100%);
}

.book-header h1 {
  font-size: 1.5rem;
  color: var(--heading-color);
  font-weight: 700;
  line-height: 1.6;
}

.book-header .author {
  font-size: 0.95rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.page-content {
  flex: 1;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  padding: 2rem 2.5rem 3rem;
}

.page-content .loading {
  text-align: center;
  color: var(--text-muted);
  padding: 4rem 0;
  font-size: 1.1rem;
}

/* Page text styling */
.page-text {
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.page-text p {
  margin-bottom: 0.75rem;
  text-align: justify;
  text-justify: inter-word;
}

.page-text .page-heading {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--heading-color);
  text-align: center;
  margin: 1.5rem 0 1rem;
  padding: 0.5rem 0;
  border-bottom: 2px solid var(--border);
  border-top: 2px solid var(--border);
  line-height: 1.8;
}

.page-text .page-heading:first-child {
  margin-top: 0;
}

.page-text .footnote-sep {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.5rem 30% 1rem 0;
}

.page-text .footnote {
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.8;
}

/* === Search === */
.search-bar {
  padding: 0.75rem 1.5rem;
  background: var(--bg-page);
  border-bottom: 1px solid var(--border);
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

.search-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  right: 0.75rem;
  color: var(--text-muted);
  pointer-events: none;
}

.search-bar input {
  width: 100%;
  padding: 0.6rem 2.5rem 0.6rem 2.2rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.search-bar input:focus {
  border-color: var(--accent-light);
  box-shadow: 0 0 0 3px rgba(139, 94, 60, 0.1);
}

.search-clear {
  position: absolute;
  left: 0.5rem;
  background: none;
  border: none;
  font-size: 1.3rem;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
  padding: 0.2rem;
  display: none;
}

.search-clear.visible {
  display: block;
}

.search-results-info {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.4rem;
  min-height: 1.2em;
}

.search-results {
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  display: none;
}

.search-results.active {
  display: block;
}

.search-result-item {
  display: block;
  width: 100%;
  text-align: right;
  padding: 0.8rem 1.5rem;
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  cursor: pointer;
  line-height: 1.8;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
}

.search-result-item:hover {
  background: rgba(139, 94, 60, 0.06);
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-result-page {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--accent);
  background: rgba(139, 94, 60, 0.1);
  padding: 0.1rem 0.5rem;
  border-radius: 4px;
  margin-left: 0.5rem;
}

.search-result-snippet {
  color: var(--text);
  display: block;
  margin-top: 0.2rem;
}

.search-result-snippet mark {
  background: rgba(196, 154, 108, 0.35);
  color: var(--heading-color);
  padding: 0 0.1rem;
  border-radius: 2px;
  font-weight: 700;
}

.search-more-btn {
  display: block;
  width: 100%;
  padding: 0.7rem;
  border: none;
  background: var(--sidebar-bg);
  font-family: inherit;
  font-size: 0.85rem;
  color: var(--accent);
  cursor: pointer;
  font-weight: 700;
  transition: background 0.15s;
}

.search-more-btn:hover {
  background: rgba(139, 94, 60, 0.12);
}

/* Quran reference badges */
.quran-ref {
  position: relative;
  cursor: pointer;
  user-select: text;
}

.quran-ref-badge {
  display: inline-block;
  font-size: 0.55rem;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
  margin: 0 0.15rem;
  vertical-align: super;
  line-height: 1.3;
  letter-spacing: 0.02em;
  font-family: system-ui, sans-serif;
  direction: ltr;
  unicode-bidi: isolate;
  cursor: pointer;
  transition: background 0.15s;
}

.quran-ref-badge:hover {
  background: var(--heading-color);
}

.quran-ref-wnum {
  opacity: 0.7;
  font-weight: 400;
}

/* Popup on click */
.quran-ref-popup {
  position: fixed;
  z-index: 300;
  background: var(--bg-page);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 20px rgba(44, 24, 16, 0.15);
  padding: 0.75rem 1rem;
  font-size: 0.85rem;
  direction: rtl;
  max-width: 320px;
  line-height: 1.8;
}

.quran-ref-popup .popup-orig {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--heading-color);
  text-align: center;
  margin-bottom: 0.4rem;
  line-height: 1.6;
}

.quran-ref-popup .popup-details {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: center;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.quran-ref-popup .popup-tag {
  background: rgba(139, 94, 60, 0.1);
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  font-family: system-ui, sans-serif;
  direction: ltr;
}

.quran-ref-popup .popup-copy {
  display: block;
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.3rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  font-family: inherit;
  font-size: 0.75rem;
  color: var(--accent);
  cursor: pointer;
  text-align: center;
  transition: background 0.15s;
}

.quran-ref-popup .popup-copy:hover {
  background: rgba(139, 94, 60, 0.1);
}

/* highlight in page */
.page-text mark.search-highlight {
  background: rgba(196, 154, 108, 0.4);
  color: inherit;
  padding: 0 0.1rem;
  border-radius: 2px;
}

/* === Bottom Navigation === */
.page-nav {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
  background: var(--nav-bg);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 8px rgba(44, 24, 16, 0.06);
  z-index: 50;
}

.nav-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg-page);
  color: var(--accent);
  cursor: pointer;
  transition: all 0.2s;
}

.nav-btn:hover {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.nav-btn:disabled {
  opacity: 0.3;
  cursor: default;
  background: var(--bg-page);
  color: var(--text-muted);
}

.page-indicator {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.vol-label {
  font-weight: 700;
  color: var(--accent);
  margin-left: 0.5rem;
}

.page-input {
  width: 3.5rem;
  text-align: center;
  padding: 0.25rem 0.3rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--bg-page);
  outline: none;
  -moz-appearance: textfield;
}

.page-input::-webkit-inner-spin-button,
.page-input::-webkit-outer-spin-button { -webkit-appearance: none; }

.page-input:focus {
  border-color: var(--accent-light);
}

/* === Sidebar Toggle (mobile) === */
.sidebar-toggle {
  display: none;
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 200;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-page);
  color: var(--accent);
  cursor: pointer;
  box-shadow: var(--shadow);
  align-items: center;
  justify-content: center;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 90;
}

/* === Responsive === */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(100%);
    width: 85vw;
    max-width: 360px;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-close {
    display: block;
  }

  .sidebar-overlay.open {
    display: block;
  }

  .sidebar-toggle {
    display: flex;
  }

  .main {
    margin-right: 0;
  }

  .book-header h1 {
    font-size: 1.2rem;
  }

  .page-content {
    padding: 1.5rem 1.25rem 2.5rem;
  }

  .page-nav {
    gap: 0.5rem;
    padding: 0.6rem 1rem;
  }

  html {
    font-size: 16px;
  }
}

@media (max-width: 480px) {
  .page-content {
    padding: 1rem 1rem 2rem;
  }

  .vol-label {
    display: none;
  }
}

/* === Scrollbar === */
.toc-list::-webkit-scrollbar {
  width: 6px;
}

.toc-list::-webkit-scrollbar-track {
  background: transparent;
}

.toc-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.toc-list::-webkit-scrollbar-thumb:hover {
  background: var(--accent-light);
}

/* === Print === */
@media print {
  .sidebar, .sidebar-toggle, .page-nav, .sidebar-overlay { display: none !important; }
  .main { margin-right: 0; }
  .page-content { max-width: none; padding: 0; }
}

.quran-ref-popup .popup-copy-short {
  font-weight: 700;
  color: var(--heading-color);
}

.quran-ref-popup .popup-copy-full {
  font-size: 0.7rem;
  color: var(--text-muted);
  border-style: dashed;
  margin-top: 0.3rem;
}
