/* ====================================================================
   MOBILE RESPONSIVE FIX FOR ML BOOKS
   Purpose: Fix overflow and responsive issues on mobile devices
   Applies to: ML Mathematics & ML Algorithms books
   ==================================================================== */

/* Fix #1: Content wrapper max-width override */
.content-wrapper {
  max-width: 900px !important;  /* Override 2000px from custom-styles.css */
}

/* Fix #2: Responsive padding - reduce on smaller screens */
@media (max-width: 768px) {
  .content-wrapper {
    padding: 20px !important;  /* 40px → 20px */
  }
}

@media (max-width: 480px) {
  .content-wrapper {
    padding: 15px !important;  /* 20px → 15px for small phones */
  }
}

/* Fix #3: Table responsive behavior */
@media (max-width: 768px) {
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  td, th {
    padding: 8px !important;  /* 15px → 8px */
    font-size: 0.9em !important;
  }
}

/* Fix #4: Formula and code block sizes */
@media (max-width: 768px) {
  .math-formula, code {
    font-size: 1.2em !important;  /* 2em → 1.2em */
    padding: 10px !important;  /* 20px → 10px */
  }

  .math-display {
    font-size: 1.3em !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

@media (max-width: 480px) {
  .math-formula, code {
    font-size: 1em !important;
    padding: 8px !important;
  }
}

/* Fix #5: Grid layouts - override inline styles to stack on mobile */
@media (max-width: 768px) {
  [style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
    gap: 15px !important;
  }
}

/* Fix #6: Prevent horizontal overflow globally */
body, html {
  overflow-x: hidden !important;
  max-width: 100vw;
}

.content-wrapper * {
  max-width: 100%;
  overflow-wrap: break-word;
  word-wrap: break-word;
}

/* Fix #7: Font size scaling for readability */
@media (max-width: 480px) {
  body {
    font-size: 16px !important;  /* 18px → 16px */
  }

  h1 {
    font-size: 1.8em !important;  /* 2.2em → 1.8em */
  }

  h2 {
    font-size: 1.5em !important;
  }

  h3 {
    font-size: 1.3em !important;
  }
}

/* Fix #8: Collapsible sections on mobile */
@media (max-width: 768px) {
  .collapsible {
    padding: 12px !important;
    font-size: 1em !important;
  }

  .collapsible-content {
    padding: 12px !important;
  }
}

/* Fix #9: Example boxes and practice problems */
@media (max-width: 768px) {
  [style*="background-color: #e8f5e9"],
  [style*="background-color: #f8f9fa"],
  [style*="background-color: #fff3cd"] {
    padding: 15px !important;
    margin: 15px 0 !important;
  }
}

/* Fix #10: Parts grid on index pages */
@media (max-width: 768px) {
  .parts-grid {
    grid-template-columns: 1fr !important;
    gap: 20px !important;
  }

  .part-card {
    padding: 20px !important;
  }
}

@media (max-width: 480px) {
  .part-card {
    padding: 15px !important;
  }
}

/* Fix #11: Ensure images don't overflow */
img {
  max-width: 100% !important;
  height: auto !important;
}

/* Fix #12: Pre and code blocks */
@media (max-width: 768px) {
  pre {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 10px !important;
  }
}

/* Fix #13: Header title responsive - CRITICAL for mobile */
.header h1 {
  word-wrap: break-word !important;
  overflow-wrap: break-word !important;
  hyphens: auto !important;
}

@media (max-width: 768px) {
  .header {
    padding: 15px 20px !important;
  }

  .header h1 {
    font-size: 1.3em !important;  /* Scale down from 2.5em */
    line-height: 1.3 !important;
  }

  .header p {
    font-size: 0.9em !important;  /* Subtitle smaller */
  }
}

@media (max-width: 480px) {
  .header {
    padding: 12px 15px !important;
  }

  .header h1 {
    font-size: 1em !important;  /* Even smaller for phones */
    line-height: 1.4 !important;
  }

  .header p {
    font-size: 0.75em !important;
  }
}

/* Fix #14: Sidebar width adjustment on mobile */
@media (max-width: 768px) {
  .sidebar {
    width: 260px !important;  /* Slightly narrower */
  }
}

@media (max-width: 480px) {
  .sidebar {
    width: 240px !important;  /* Even narrower for small phones */
  }
}

/* Fix #15: Bottom navigation buttons responsive */
@media (max-width: 768px) {
  .nav-buttons,
  .chapter-navigation,
  [style*="display: flex"][style*="justify-content: space-between"] {
    flex-wrap: wrap !important;
    gap: 10px !important;
  }

  .nav-buttons a,
  .chapter-navigation a,
  .prev-chapter,
  .next-chapter,
  .home-link {
    font-size: 0.9em !important;
    padding: 8px 12px !important;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 45% !important;
  }
}

@media (max-width: 480px) {
  .nav-buttons a,
  .chapter-navigation a,
  .prev-chapter,
  .next-chapter,
  .home-link {
    font-size: 0.8em !important;
    padding: 6px 10px !important;
    max-width: 48% !important;
  }

  /* Stack navigation vertically on very small screens */
  .nav-buttons,
  .chapter-navigation {
    flex-direction: column !important;
  }

  .nav-buttons a,
  .chapter-navigation a {
    max-width: 100% !important;
    text-align: center !important;
  }
}
