/* ============================================
   IMPROVED EPUB READER CSS FOR SOMALI TEXT
   ============================================ */

/* ===== CSS Variables ===== */
:root {
  /* Colors */
  --bg-secondary: #F9E4D4;
  --text-secondary: #6c6f76;
  --border-color: #E6C8B9;

  /* Typography - Fluid scaling */
  --base-font-size: clamp(16px, 2.5vw, 18px);
  --line-height-base: 1.75;
  --letter-spacing-base: 0;

  /* Spacing for Somali text optimization - NO extra spacing */
  --word-spacing-mobile: 0;
  --word-spacing-desktop: 0.015em;

  /* Layout */
  --sidebar-width: 300px;
  --max-content-width: 800px;

  /* Transitions */
  --transition-fast: 0.2s ease;
  --transition-medium: 0.3s ease;
}

/* Night mode variables */
[data-theme="dark"] {
  --bg-secondary: #1a1a1a;
  --text-secondary: #b8b8b8;
  --border-color: #404040;
  --shadow: rgba(0, 0, 0, 0.5);
}

/* ===== Global Styles ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
  -moz-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html,
body {
  background-color: var(--bg-primary);
  margin: 0;
  padding: 0;
  height: 100%;
  overflow-x: hidden;
}

/* Visually hidden utility for accessibility */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ===== Main Content Area ===== */
.reader-main {
  margin-top: var(--header-height);
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 1rem;
  transition: margin-left var(--transition-medium);
  background-color: var(--bg-primary);
  min-height: calc(100vh - var(--header-height));
}

.reader-wrapper.sidebar-open .reader-main {
  margin-left: var(--sidebar-width);
}

.reader-content-area {
  max-width: var(--max-content-width);
  width: 100%;
  padding: 2rem;
  background-color: var(--bg-primary);
}

/* ===== CRITICAL: Somali Text Optimization ===== */
#reader-content {
  font-size: var(--base-font-size);
  line-height: var(--line-height-base);
  color: var(--text-primary);
  background-color: var(--bg-primary);

  /* Smart text rendering for Somali */
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#reader-content p {
  margin-bottom: 1.5em;

  /* SOLUTION FOR LONG SOMALI WORDS */
  /* Disable aggressive hyphenation */
  hyphens: manual;
  -webkit-hyphens: manual;
  -moz-hyphens: manual;
  -ms-hyphens: manual;

  /* DEFAULT: Normal word breaking - prefer whole words */
  word-break: normal;
  overflow-wrap: break-word;
  word-wrap: break-word;

  /* Optimized spacing that scales with font size */
  letter-spacing: var(--letter-spacing-base);
  word-spacing: var(--word-spacing-mobile);

  /* Better text alignment for Somali - LEFT ALIGN ON MOBILE */
  text-align: left;
  text-align-last: left;

  /* Prevent awkward orphans/widows */
  orphans: 3;
  widows: 3;

  /* CRITICAL: Remove justification completely on mobile by default */
  text-justify: none;
}

/* Desktop: Use justify with optimized spacing */
@media (min-width: 768px) {
  #reader-content p {
    text-align: justify;
    text-justify: inter-word;
    word-spacing: 0.015em;
    /* Keep words together on desktop */
    word-break: normal;
    overflow-wrap: break-word;
    hyphens: manual;
  }
}

/* Prevent excessive gaps with large fonts on mobile */
@media (max-width: 767px) {
  #reader-content {
    /* DEFAULT: Normal word breaking - don't be aggressive */
    word-break: normal;
    overflow-wrap: break-word;
  }

  /* DEFAULT: All paragraphs start with left align and normal breaking */
  #reader-content p {
    text-align: left;
    text-justify: none;
    word-spacing: 0;
    letter-spacing: 0;
    /* Normal word breaking - keep words together unless necessary */
    word-break: normal;
    overflow-wrap: break-word;
  }

  /* ====================================================================
     Small fonts (12px-18px): JUSTIFIED - Safe on mobile, words stay intact
     ==================================================================== */
  body.font-size-12 #reader-content p,
  .reader-wrapper.font-size-12 #reader-content p,
  body.font-size-14 #reader-content p,
  .reader-wrapper.font-size-14 #reader-content p,
  body.font-size-16 #reader-content p,
  .reader-wrapper.font-size-16 #reader-content p,
  body.font-size-18 #reader-content p,
  .reader-wrapper.font-size-18 #reader-content p {
    text-align: justify !important;
    text-justify: inter-word !important;
    word-spacing: 0.015em !important;
    letter-spacing: 0.005em !important;
    /* Keep words intact - only break if absolutely necessary */
    word-break: normal !important;
    overflow-wrap: break-word !important;
    hyphens: manual !important;
    -webkit-hyphens: manual !important;
  }

  /* ====================================================================
     Medium & Large fonts (20px-32px): LEFT ALIGNED - NO word breaking
     preventing "rivers" of white space common in Somali text at these sizes
     ==================================================================== */
  body.font-size-20 #reader-content p,
  .reader-wrapper.font-size-20 #reader-content p,
  body.font-size-22 #reader-content p,
  .reader-wrapper.font-size-22 #reader-content p,
  body.font-size-24 #reader-content p,
  .reader-wrapper.font-size-24 #reader-content p,
  body.font-size-26 #reader-content p,
  .reader-wrapper.font-size-26 #reader-content p,
  body.font-size-28 #reader-content p,
  .reader-wrapper.font-size-28 #reader-content p,
  body.font-size-30 #reader-content p,
  .reader-wrapper.font-size-30 #reader-content p,
  body.font-size-32 #reader-content p,
  .reader-wrapper.font-size-32 #reader-content p {
    text-align: left !important;
    text-justify: none !important;
    word-spacing: 0 !important;
    letter-spacing: 0 !important;
    /* CRITICAL: Keep words together - only break if word is longer than line */
    word-break: normal !important;
    overflow-wrap: break-word !important;
    /* Allow long words to overflow slightly if needed rather than breaking */
    white-space: normal !important;
  }
}

/* Heading styles */
#reader-content h1,
#reader-content h2,
#reader-content h3,
#reader-content h4,
#reader-content h5,
#reader-content h6 {
  margin-top: 2rem;
  margin-bottom: 1rem;
  color: var(--text-primary);
  line-height: 1.3;
  font-weight: 700;
  word-break: break-word;
  hyphens: auto;
}

#reader-content h1 {
  font-size: clamp(1.75rem, 4vw, 2.25rem);
}

#reader-content h2 {
  font-size: clamp(1.5rem, 3.5vw, 2rem);
}

#reader-content h3 {
  font-size: clamp(1.25rem, 3vw, 1.75rem);
}

#reader-content h4 {
  font-size: clamp(1.125rem, 2.5vw, 1.5rem);
}

#reader-content h5 {
  font-size: clamp(1rem, 2vw, 1.25rem);
}

#reader-content h6 {
  font-size: clamp(0.95rem, 1.8vw, 1.1rem);
}

.chapter-title {
  font-size: clamp(1.5rem, 3.5vw, 2rem);
  font-weight: 700;
  margin-bottom: 1.5rem;
  color: var(--accent);
  word-break: break-word;
  hyphens: auto;
}

#reader-content img {
  max-width: 100%;
  height: auto;
  margin: 1.5rem 0;
  border-radius: 8px;
  box-shadow: 0 2px 8px var(--shadow);
  display: block;
}



#reader-content ul,
#reader-content ol {
  margin: 1rem 0;
  padding-left: 2rem;
}

#reader-content li {
  margin-bottom: 0.5rem;
}

/* ===== Sidebar Styles ===== */
.reader-sidebar {
  width: var(--sidebar-width);
  background-color: var(--bg-primary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  position: fixed;
  left: calc(-1 * var(--sidebar-width));
  top: var(--header-height);
  bottom: 0;
  z-index: 999;
  transition: left var(--transition-medium);
  box-shadow: 2px 0 10px var(--shadow);
  overflow: hidden;
}

.reader-sidebar.open {
  left: 0;
}

.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-color);
  background-color: var(--bg-primary);
  flex-shrink: 0;
}

.sidebar-tab {
  flex: 1;
  padding: 1.25rem 0.5rem;
  background: none;
  border: none;
  cursor: pointer;
  font-weight: 500;
  color: var(--text-secondary);
  transition: all var(--transition-fast);
  border-bottom: 2px solid transparent;
  font-size: 0.9rem;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sidebar-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  background-color: rgba(112, 25, 61, 0.05);
}

.sidebar-tab:hover,
.sidebar-tab:focus {
  background-color: var(--border-color);
}

.sidebar-content {
  flex: 1;
  min-height: 0;
  /* Fix for flex scrolling */
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1rem;
  background-color: var(--bg-primary);
  /* Enable native vertical scrolling while allowing custom horizontal handling */
  touch-action: pan-y;
  overscroll-behavior: contain;
  height: 100%;
}

/* Custom scrollbar */
.sidebar-content::-webkit-scrollbar {
  width: 8px;
}

.sidebar-content::-webkit-scrollbar-track {
  background: var(--bg-secondary);
}

.sidebar-content::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

.sidebar-content::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
}

/* Tab Content */
.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

/* TOC Styles */
.toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 100%;
  overflow-y: auto;
}

.toc-item {
  margin-bottom: 0.25rem;
}

.toc-link {
  display: block;
  padding: 0.75rem;
  color: var(--text-primary);
  text-decoration: none;
  border-radius: 6px;
  transition: all var(--transition-fast);
  font-size: 0.9rem;
  line-height: 1.4;
  border: 1px solid transparent;
  word-break: break-word;
}

.toc-link:hover,
.toc-link:focus {
  background-color: var(--accent);
  color: white;
  border-color: var(--accent);
}

.toc-link.active {
  background-color: var(--accent);
  color: white;
  border-color: var(--accent);
  font-weight: 600;
}

/* Bookmarks Styles */
.bookmarks-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.bookmark-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  cursor: pointer;
  transition: all var(--transition-fast);
  min-height: 44px;
}

.bookmark-item:hover,
.bookmark-item:focus {
  background-color: var(--accent);
  color: white;
  border-color: var(--accent);
}

.bookmark-text {
  font-size: 0.9rem;
  line-height: 1.4;
  word-break: break-word;
}

.bookmark-delete {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 4px;
  transition: all var(--transition-fast);
  flex-shrink: 0;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bookmark-delete:hover,
.bookmark-delete:focus {
  background-color: rgba(220, 53, 69, 0.1);
  color: #dc3545;
}

/* Search Styles */
.search-container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.search-input {
  width: 100%;
  padding: 1rem;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.9rem;
  transition: border-color var(--transition-fast);
  min-height: 44px;
}

.search-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(112, 25, 61, 0.1);
}

.search-results {
  max-height: 400px;
  overflow-y: auto;
}

.search-result-item {
  display: block;
  padding: 1rem;
  color: var(--text-primary);
  text-decoration: none;
  border-radius: 6px;
  transition: all var(--transition-fast);
  font-size: 0.9rem;
  line-height: 1.4;
  border: 1px solid transparent;
  margin-bottom: 0.25rem;
  word-break: break-word;
  min-height: 44px;
}

.search-result-item:hover,
.search-result-item:focus {
  background-color: var(--accent);
  color: white;
  border-color: var(--accent);
}

.empty-text {
  text-align: center;
  color: var(--text-secondary);
  padding: 2rem 1rem;
  font-style: italic;
}

/* Settings Styles */
.settings-group {
  margin-bottom: 1.5rem;
}

.settings-group h4 {
  margin: 0 0 1rem 0;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
}

.setting-item {
  margin-bottom: 1rem;
}

.setting-item label {
  display: block;
  margin-bottom: 0.5rem;
  color: var(--text-primary);
  font-size: 0.9rem;
}

.font-size-slider {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: var(--border-color);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.font-size-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  transition: transform var(--transition-fast);
}

.font-size-slider::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

.font-size-slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
  transition: transform var(--transition-fast);
}

.font-size-slider::-moz-range-thumb:hover {
  transform: scale(1.2);
}

#font-size-value {
  display: inline-block;
  font-weight: 600;
  color: var(--accent);
}

.font-select {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.9rem;
  transition: border-color var(--transition-fast);
  cursor: pointer;
}

.font-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(112, 25, 61, 0.1);
}

.toggle-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text-primary);
  padding: 0.5rem 0;
}

.toggle-label input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: relative;
  width: 44px;
  height: 24px;
  background-color: var(--border-color);
  border-radius: 24px;
  transition: background-color var(--transition-medium);
  flex-shrink: 0;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background-color: white;
  border-radius: 50%;
  transition: transform var(--transition-medium);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.toggle-label input:checked+.toggle-slider {
  background-color: var(--accent);
}

.toggle-label input:checked+.toggle-slider::before {
  transform: translateX(20px);
}

.toggle-label input:focus+.toggle-slider {
  box-shadow: 0 0 0 3px rgba(112, 25, 61, 0.2);
}

.reset-btn {
  width: 100%;
  padding: 0.75rem;
  background-color: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  font-size: 0.9rem;
  transition: background-color var(--transition-fast);
}

.reset-btn:hover,
.reset-btn:focus {
  background-color: #5a1431;
}

/* ===== Sidebar Overlay ===== */
.sidebar-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 998;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition-medium), visibility var(--transition-medium);
  backdrop-filter: blur(2px);
}

.sidebar-overlay.active {
  opacity: 1;
  visibility: visible;
}

/* ===== Content Navigation ===== */
.content-navigation {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 2px solid var(--border-color);
  gap: 1rem;
}

.chapter-indicator {
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-weight: 500;
  text-align: center;
}

.nav-btn {
  background: var(--accent);
  border: none;
  color: white;
  padding: 0.75rem 1.5rem;
  cursor: pointer;
  font-weight: 500;
  font-size: 0.9rem;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border-radius: 6px;
  text-decoration: none;
  white-space: nowrap;
}

.nav-btn:hover:not(:disabled),
.nav-btn:focus:not(:disabled) {
  background-color: #5a1431;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px var(--shadow);
}

.nav-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ===== Skip Link for Accessibility ===== */
.skip-link {
  position: absolute;
  top: -100px;
  left: 6px;
  background: var(--accent);
  color: white;
  padding: 0.75rem 1rem;
  text-decoration: none;
  border-radius: 6px;
  z-index: 1001;
  transition: top var(--transition-fast);
  font-weight: 600;
}

.skip-link:focus {
  top: 6px;
}

/* ===== Focus Styles for Accessibility ===== */
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ===== Font Size Classes ===== */
body.font-size-12 #reader-content,
.reader-wrapper.font-size-12 #reader-content {
  font-size: 12px;
}

body.font-size-14 #reader-content,
.reader-wrapper.font-size-14 #reader-content {
  font-size: 14px;
}

body.font-size-16 #reader-content,
.reader-wrapper.font-size-16 #reader-content {
  font-size: 16px;
}

body.font-size-18 #reader-content,
.reader-wrapper.font-size-18 #reader-content {
  font-size: 18px;
}

body.font-size-20 #reader-content,
.reader-wrapper.font-size-20 #reader-content {
  font-size: 20px;
}

body.font-size-22 #reader-content,
.reader-wrapper.font-size-22 #reader-content {
  font-size: 22px;
}

body.font-size-24 #reader-content,
.reader-wrapper.font-size-24 #reader-content {
  font-size: 24px;
}

body.font-size-26 #reader-content,
.reader-wrapper.font-size-26 #reader-content {
  font-size: 26px;
}

body.font-size-28 #reader-content,
.reader-wrapper.font-size-28 #reader-content {
  font-size: 28px;
}

body.font-size-30 #reader-content,
.reader-wrapper.font-size-30 #reader-content {
  font-size: 30px;
}

body.font-size-32 #reader-content,
.reader-wrapper.font-size-32 #reader-content {
  font-size: 32px;
}

/* ===== Font Family Classes ===== */
#reader-content.font-serif {
  font-family: 'Georgia', 'Times New Roman', serif;
}

#reader-content.font-sans {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
}

#reader-content.font-mono {
  font-family: 'Courier New', 'Courier', monospace;
}

#reader-content.font-dyslexic {
  font-family: 'OpenDyslexic', 'Comic Sans MS', cursive;
  letter-spacing: 0.05em;
  word-spacing: 0.1em;
}

/* Bold Text Class */
.bold-text #reader-content {
  font-weight: 600;
}

/* ===== High Contrast Mode Support ===== */
@media (prefers-contrast: high) {
  :root {
    --text-primary: #000000;
    --text-secondary: #000000;
    --bg-primary: #ffffff;
    --bg-secondary: #ffffff;
    --border-color: #000000;
  }

  [data-theme="dark"] {
    --text-primary: #ffffff;
    --text-secondary: #ffffff;
    --bg-primary: #000000;
    --bg-secondary: #000000;
    --border-color: #ffffff;
  }
}

/* ===== Reduced Motion Support ===== */
@media (prefers-reduced-motion: reduce) {

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ========================================
   RESPONSIVE DESIGN
   ======================================== */

/* Desktop (1024px+) */
@media (min-width: 1024px) {
  .reader-content-area {
    padding: 3rem;
  }

  #reader-content {
    font-size: clamp(17px, 1.8vw, 19px);
    line-height: 1.8;
  }
}

/* Tablet (768px - 1023px) */
@media (max-width: 1023px) {
  :root {
    --sidebar-width: 280px;
  }

  .reader-content-area {
    padding: 2rem;
  }

  .book-title {
    font-size: 1rem;
  }
}

/* Mobile (max-width: 767px) */
@media (max-width: 767px) {
  :root {
    --header-height: 56px;
    --sidebar-width: 260px;
  }

  .reader-header {
    padding: 0.5rem 0.75rem;
  }

  .header-left {
    gap: 0.75rem;
  }

  .back-btn {
    font-size: 0.9rem;
    padding: 0.4rem 0.6rem;
  }

  .sidebar-toggle {
    font-size: 1.3rem;
    padding: 0.4rem;
  }

  .header-center {
    margin: 0 0.75rem;
  }

  .book-title {
    font-size: 0.95rem;
  }

  .header-right {
    gap: 0.75rem;
  }

  .font-btn {
    width: 44px;
    height: 44px;
    font-weight: bold;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .bookmark-btn {
    width: 44px;
    height: 44px;
    font-size: 1.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .night-mode-btn,
  #modeToggle,
  #sidebarToggle {
    font-size: 1.75rem;
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .reader-main {
    padding: 0.5rem;
  }

  .reader-content-area {
    padding: 1.5rem 1rem;
  }

  .sidebar-tab {
    padding: 1rem 0.25rem;
    font-size: 0.9rem;
  }

  .sidebar-content {
    padding: 0.75rem;
  }

  .toc-link,
  .bookmark-item,
  .search-result-item {
    padding: 1rem;
    font-size: 0.95rem;
  }

  .search-input {
    padding: 1rem;
    font-size: 1rem;
  }

  .settings-group h4 {
    font-size: 0.95rem;
  }

  .toggle-label {
    font-size: 0.85rem;
  }

  .reset-btn {
    padding: 0.75rem;
    font-size: 1rem;
    min-height: 44px;
  }

  #reader-content {
    font-size: 17px;
    line-height: 1.7;
  }

  .chapter-title {
    font-size: 1.4rem;
    margin-bottom: 1.5rem;
  }

  .content-navigation {
    margin-top: 2rem;
    padding-top: 1rem;
    flex-wrap: wrap;
  }

  .chapter-indicator {
    order: 3;
    width: 100%;
    margin-top: 0.75rem;
  }

  .nav-btn {
    padding: 0.625rem 1.25rem;
    font-size: 0.85rem;
  }

  /* Touch-friendly targets */
  button,
  .sidebar-tab,
  .toc-link,
  .bookmark-item,
  .search-result-item,
  .nav-btn {
    min-height: 44px;
    touch-action: manipulation;
  }
}

/* Small tablets and large phones (480px - 767px) */
@media (max-width: 767px) and (min-width: 480px) {
  .content-navigation {
    flex-direction: row;
    justify-content: space-between;
  }

  .chapter-indicator {
    order: 2;
    width: auto;
    margin-top: 0;
  }
}

/* Phones (max-width: 479px) */
@media (max-width: 479px) {
  :root {
    --header-height: 52px;
    --sidebar-width: 240px;
  }

  .reader-header {
    padding: 0.4rem 0.6rem;
  }

  .header-left {
    gap: 0.6rem;
  }

  .back-btn {
    font-size: 0.85rem;
    padding: 0.3rem 0.5rem;
  }

  .sidebar-toggle {
    font-size: 1.2rem;
    padding: 0.3rem;
  }

  .book-title {
    font-size: 0.875rem;
  }

  .header-right {
    gap: 0.4rem;
  }

  .font-btn {
    width: 26px;
    height: 26px;
    font-size: 0.75rem;
  }

  .bookmark-btn {
    width: 26px;
    height: 26px;
    font-size: 0.8rem;
  }

  .night-mode-btn {
    font-size: 1.2rem;
  }

  .reader-content-area {
    padding: 1.25rem 0.875rem;
  }

  .sidebar-tab {
    padding: 0.75rem 0.2rem;
    font-size: 0.8rem;
  }

  .sidebar-content {
    padding: 0.625rem;
  }

  .toc-link,
  .bookmark-item,
  .search-result-item {
    padding: 0.5rem;
    font-size: 0.8rem;
  }

  .search-input {
    padding: 0.5rem;
    font-size: 0.8rem;
  }

  .settings-group {
    margin-bottom: 1.25rem;
  }

  .settings-group h4 {
    font-size: 0.9rem;
    margin-bottom: 0.75rem;
  }

  .setting-item {
    margin-bottom: 0.875rem;
  }

  .toggle-label {
    font-size: 0.8rem;
    gap: 0.625rem;
  }

  .reset-btn {
    padding: 0.5rem;
    font-size: 0.85rem;
  }

  #reader-content {
    font-size: 16px;
    line-height: 1.65;
  }

  #reader-content h1,
  #reader-content h2,
  #reader-content h3,
  #reader-content h4,
  #reader-content h5,
  #reader-content h6 {
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
  }

  #reader-content p {
    margin-bottom: 1.25rem;
  }

  #reader-content img {
    margin: 1rem 0;
    border-radius: 6px;
  }



  .chapter-title {
    font-size: 1.3rem;
    margin-bottom: 1.25rem;
  }

  .content-navigation {
    flex-direction: column;
    gap: 0.75rem;
  }

  .chapter-indicator {
    order: 2;
    width: 100%;
  }

  .nav-btn {
    padding: 0.75rem 1.25rem;
    font-size: 1rem;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .nav-prev {
    order: 1;
  }

  .nav-next {
    order: 3;
  }
}

/* Small phones (max-width: 360px) */
@media (max-width: 360px) {
  :root {
    --header-height: 50px;
    --sidebar-width: 220px;
  }

  .reader-header {
    padding: 0.35rem 0.5rem;
  }

  .header-left {
    gap: 0.5rem;
  }

  .back-btn {
    font-size: 0.8rem;
    padding: 0.25rem 0.45rem;
  }

  .sidebar-toggle {
    font-size: 1.15rem;
    padding: 0.25rem;
  }

  .book-title {
    font-size: 0.825rem;
  }

  .header-right {
    gap: 0.3rem;
  }

  .font-btn {
    width: 24px;
    height: 24px;
    font-size: 0.7rem;
  }

  .bookmark-btn {
    width: 24px;
    height: 24px;
    font-size: 0.75rem;
  }

  .night-mode-btn {
    font-size: 1.15rem;
  }

  .reader-content-area {
    padding: 1rem 0.75rem;
  }

  .sidebar-tab {
    padding: 0.7rem 0.15rem;
    font-size: 0.75rem;
  }

  .sidebar-content {
    padding: 0.5rem;
  }

  .toc-link,
  .bookmark-item,
  .search-result-item {
    padding: 0.45rem;
    font-size: 0.75rem;
  }

  .search-input {
    padding: 0.45rem;
    font-size: 0.75rem;
  }

  .settings-group h4 {
    font-size: 0.85rem;
    margin-bottom: 0.625rem;
  }

  .setting-item {
    margin-bottom: 0.75rem;
  }

  .toggle-label {
    font-size: 0.75rem;
    gap: 0.5rem;
  }

  .toggle-slider {
    width: 40px;
    height: 22px;
  }

  .toggle-slider::before {
    width: 18px;
    height: 18px;
  }

  .toggle-label input:checked+.toggle-slider::before {
    transform: translateX(18px);
  }

  .reset-btn {
    padding: 0.45rem;
    font-size: 0.8rem;
  }

  #reader-content {
    font-size: 15px;
    line-height: 1.6;
  }

  #reader-content h1,
  #reader-content h2,
  #reader-content h3,
  #reader-content h4,
  #reader-content h5,
  #reader-content h6 {
    margin-top: 1.25rem;
    margin-bottom: 0.625rem;
  }

  #reader-content p {
    margin-bottom: 1rem;
  }

  #reader-content img {
    margin: 0.75rem 0;
    border-radius: 5px;
  }



  .chapter-title {
    font-size: 1.2rem;
    margin-bottom: 1rem;
  }

  .content-navigation {
    margin-top: 1.5rem;
    padding-top: 0.75rem;
  }

  .nav-btn {
    padding: 0.45rem 0.9rem;
    font-size: 0.75rem;
  }

  /* Hide font controls only on very small screens */
  @media (max-width: 400px) {
    .font-controls {
      display: none;
    }
  }
}

/* Extra small phones (max-width: 320px) */
@media (max-width: 320px) {
  :root {
    --header-height: 48px;
    --sidebar-width: 200px;
  }

  .reader-header {
    padding: 0.3rem 0.4rem;
  }

  .header-left {
    gap: 0.4rem;
  }

  .back-btn {
    font-size: 0.75rem;
    padding: 0.2rem 0.35rem;
  }

  .sidebar-toggle {
    font-size: 1.1rem;
    padding: 0.2rem;
  }

  .book-title {
    font-size: 0.8rem;
  }

  .header-right {
    gap: 0.25rem;
  }

  .bookmark-btn,
  .font-controls {
    display: none;
  }

  .night-mode-btn {
    font-size: 1.1rem;
  }

  .reader-content-area {
    padding: 0.875rem 0.625rem;
  }

  .sidebar-tab {
    padding: 0.65rem 0.125rem;
    font-size: 0.7rem;
  }

  .sidebar-content {
    padding: 0.45rem;
  }

  .toc-link,
  .bookmark-item,
  .search-result-item {
    padding: 0.4rem;
    font-size: 0.7rem;
  }

  .search-input {
    padding: 0.4rem;
    font-size: 0.7rem;
  }

  .settings-group h4 {
    font-size: 0.8rem;
    margin-bottom: 0.6rem;
  }

  .setting-item {
    margin-bottom: 0.7rem;
  }

  .toggle-label {
    font-size: 0.7rem;
    gap: 0.45rem;
  }

  .toggle-slider {
    width: 38px;
    height: 20px;
  }

  .toggle-slider::before {
    width: 16px;
    height: 16px;
  }

  .toggle-label input:checked+.toggle-slider::before {
    transform: translateX(16px);
  }

  .reset-btn {
    padding: 0.4rem;
    font-size: 0.75rem;
  }

  #reader-content {
    font-size: 14px;
    line-height: 1.55;
  }

  #reader-content h1,
  #reader-content h2,
  #reader-content h3,
  #reader-content h4,
  #reader-content h5,
  #reader-content h6 {
    margin-top: 1.1rem;
    margin-bottom: 0.55rem;
  }

  #reader-content p {
    margin-bottom: 0.9rem;
  }

  #reader-content img {
    margin: 0.65rem 0;
    border-radius: 4px;
  }

  #reader-content blockquote {
    padding-left: 0.5rem;
    margin: 0.9rem 0;
    font-size: 0.9em;
  }

  .chapter-title {
    font-size: 1.15rem;
    margin-bottom: 0.9rem;
  }

  .content-navigation {
    margin-top: 1.25rem;
    padding-top: 0.65rem;
  }

  .nav-btn {
    padding: 0.4rem 0.75rem;
    font-size: 0.7rem;
  }
}

/* ===== Print Styles ===== */
@media print {

  .reader-header,
  .reader-sidebar,
  .sidebar-overlay,
  .content-navigation,
  .skip-link {
    display: none !important;
  }

  .reader-main {
    margin-top: 0;
    padding: 0;
  }

  .reader-content-area {
    max-width: 100%;
    padding: 0;
  }

  #reader-content {
    font-size: 12pt;
    line-height: 1.5;
    color: black;
  }

  #reader-content p {
    orphans: 3;
    widows: 3;
  }

  #reader-content h1,
  #reader-content h2,
  #reader-content h3,
  #reader-content h4,
  #reader-content h5,
  #reader-content h6 {
    page-break-after: avoid;
  }
}

/* ===== Dark Mode Enhancements ===== */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-primary: #1a1a1a;
    --bg-secondary: #1a1a1a;
    --text-primary: #e8e8e8;
    --text-secondary: #b8b8b8;
    --accent: #8a2350;
    --border-color: #404040;
    --shadow: rgba(0, 0, 0, 0.5);
  }
}

/* ===== Performance Optimizations ===== */
.reader-wrapper,
.reader-sidebar,
.sidebar-overlay,
.reader-main {
  will-change: auto;
}

/* Optimize animations */
@media (hover: hover) and (pointer: fine) {
  .nav-btn:hover:not(:disabled) {
    transform: translateY(-2px);
  }
}

/* Touch-specific enhancements */
@media (hover: none) and (pointer: coarse) {

  /* Visual feedback for touch interactions */
  .toc-link:active,
  .bookmark-item:active,
  .search-result-item:active {
    background-color: var(--accent) !important;
    color: white !important;
    border-color: var(--accent) !important;
    opacity: 0.9;
  }

  .sidebar-tab:active {
    background-color: var(--accent);
    color: white;
    opacity: 0.9;
  }

  /* Reduce motion for better performance on mobile */
  .reader-sidebar {
    transition: left 0.2s ease-out;
  }

  .reader-wrapper.sidebar-open .reader-main {
    transition: margin-left 0.2s ease-out;
  }

  /* Larger touch targets on touch devices */
  .toc-link,
  .bookmark-item,
  .search-result-item {
    padding: 1.25rem;
  }

  .sidebar-tab {
    padding: 1.25rem 0.5rem;
  }
}

/* ===== Utility Classes ===== */
.text-center {
  text-align: center;
}

.text-left {
  text-align: left;
}

.text-right {
  text-align: right;
}

.mt-0 {
  margin-top: 0;
}

.mt-1 {
  margin-top: 0.5rem;
}

.mt-2 {
  margin-top: 1rem;
}

.mt-3 {
  margin-top: 1.5rem;
}

.mb-0 {
  margin-bottom: 0;
}

.mb-1 {
  margin-bottom: 0.5rem;
}

.mb-2 {
  margin-bottom: 1rem;
}

.mb-3 {
  margin-bottom: 1.5rem;
}

.p-0 {
  padding: 0;
}

.p-1 {
  padding: 0.5rem;
}

.p-2 {
  padding: 1rem;
}

.p-3 {
  padding: 1.5rem;
}