:root {
  --primary: #4f46e5;
  --primary-light: #eef2ff;
  --secondary: #64748b;
  --accent: #f59e0b;
  --bg-main: #ffffff;
  --bg-sidebar: #f1f5f9; /* Darkened for better contrast */
  --bg-card: #ffffff;
  --text-main: #0f172a;
  --text-muted: #475569;
  --border-light: #e2e8f0;
  --code-bg: #f8fafc;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --radius: 12px;
  --spacing: clamp(1.25rem, 4vw, 3rem); /* Reduced for tighter layout */
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

img, video, canvas, svg {
  max-width: 100%;
  height: auto;
}

html, body {
  width: 100%;
  max-width: 100%;
  overflow-x: clip; /* Modern way to prevent horizontal scroll */
  position: relative;
}

body {
  font-family: 'Outfit', 'Inter', system-ui, sans-serif;
  background: linear-gradient(to right, var(--bg-sidebar) 280px, var(--bg-main) 280px);
  background-attachment: fixed; /* Keep sidebar background fixed infinitely */
  color: var(--text-main);
  line-height: 1.6;
  scroll-behavior: smooth;
  overflow-wrap: break-word;
  word-wrap: break-word;
  word-break: break-word;
}

/* Custom Scrollbar */
::-webkit-scrollbar {
  width: 8px;
}
::-webkit-scrollbar-track {
  background: var(--bg-sidebar);
}
::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--secondary);
}

/* Layout */
.app-container {
  min-height: 100vh;
  padding-left: 280px; /* Space for the fixed sidebar */
  width: 100%;
}

/* Sidebar */
.sidebar {
  border-right: 1px solid var(--border-light);
  padding: 2rem 1.5rem;
  position: fixed;
  top: 0;
  left: 0;
  width: 280px;
  height: 100vh;
  overflow-y: auto;
  z-index: 1000; /* Increased to ensure it's on top */
  background: var(--bg-sidebar);
}

#sidebar-nav {
  padding-right: 0.5rem;
}

/* Custom scrollbar for sidebar nav */
#sidebar-nav::-webkit-scrollbar {
  width: 4px;
}
#sidebar-nav::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 10px;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--primary);
  margin-bottom: 2.5rem;
  text-decoration: none;
}

.nav-section-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--secondary);
  font-weight: 600;
  margin-bottom: 0.75rem;
  margin-top: 1.5rem;
}

.nav-link {
  display: block;
  padding: 0.8rem 1rem; /* Increased for better touch target */
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.95rem;
  font-weight: 500;
  border-radius: var(--radius);
  transition: all 0.2s;
  margin-bottom: 0.4rem;
}

.nav-link:hover {
  background-color: var(--primary-light);
  color: var(--primary);
}

.nav-link.active {
  background-color: var(--primary);
  color: white;
}

/* Q&A Styling */
details {
  border: 1px solid var(--border-light);
  border-radius: 8px;
  margin-bottom: 0.5rem;
  transition: all 0.3s ease;
  background: white;
}

details[open] {
  background-color: var(--bg-sidebar);
  border-color: var(--primary-light);
  box-shadow: var(--shadow-sm);
}

summary {
  padding: 1rem;
  cursor: pointer;
  font-weight: 600;
  color: var(--text-main);
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

summary::after {
  content: '+';
  font-size: 1.2rem;
  color: var(--primary);
  transition: transform 0.3s;
}

details[open] summary::after {
  content: '−';
  transform: rotate(180deg);
}

details p {
  padding: 0 1rem 1rem 1rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border-light);
  margin-top: 0.5rem;
  padding-top: 1rem;
}

/* Mobile Menu Button */
.mobile-menu-btn {
  display: none;
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 3.5rem;
  height: 3.5rem;
  background-color: var(--primary);
  color: white;
  border: none;
  border-radius: 50%;
  box-shadow: var(--shadow-lg);
  z-index: 2000;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.mobile-menu-btn:active {
  transform: scale(0.9);
}

/* Main Content */
.main-content {
  padding: 0;
  max-width: 100%;
  width: 100%;
  overflow-x: hidden;
  min-width: 0; /* Crucial for grid item overflow */
}

.header {
  padding: var(--spacing) 2rem;
  background: linear-gradient(to bottom, var(--primary-light), transparent);
  text-align: center;
  margin-bottom: 2rem;
}

.header h1 {
  font-size: clamp(2rem, 8vw, 3.5rem); /* Fluid Typography */
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-bottom: 1rem;
  background: linear-gradient(to right, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  line-height: 1.1;
  word-break: break-word;
}

.header p {
  font-size: 1.25rem;
  color: var(--text-muted);
  max-width: 700px;
  margin: 0 auto;
}

.author-watermark {
  margin-top: 1.5rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  opacity: 0.8;
}

.author-watermark i {
  color: var(--primary);
}

.author-watermark .fw-bold {
  color: var(--text-main);
  font-weight: 700;
}

.content-section {
  padding: var(--spacing) 3rem;
  border-bottom: 1px solid var(--border-light);
  scroll-margin-top: 2rem;
  min-height: 200px; /* Ensure it's not collapsed */
  transition: opacity 0.8s ease-out, transform 0.8s ease-out;
}

.section-tag {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  background-color: var(--primary-light);
  color: var(--primary);
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  margin-bottom: 1rem;
}

h2 {
  font-size: 2.25rem;
  font-weight: 800;
  margin-bottom: 1.5rem;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 2rem;
  margin-bottom: 2rem;
  box-shadow: var(--shadow-sm);
  width: 100%;
  max-width: 100%;
}

.grid-cols-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
}

/* Code Blocks */
pre {
  background-color: var(--code-bg);
  color: var(--text-main);
  padding: 1.5rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-light);
  overflow-x: auto;
  position: relative;
  margin: 1rem 0;
  font-family: 'Fira Code', monospace;
  font-size: 0.9rem;
  width: 100%;
  max-width: 100%;
  white-space: pre; /* Ensure it doesn't wrap but scrolls */
}

pre::before {
  content: 'HTML';
  position: absolute;
  top: 0;
  right: 1.5rem;
  font-size: 0.7rem;
  background: var(--border-light);
  padding: 0.25rem 0.75rem;
  border-radius: 0 0 6px 6px;
  color: var(--text-muted);
  font-weight: 700;
}

code {
  font-family: 'Fira Code', monospace;
  font-size: 0.9em;
  color: var(--primary);
}

/* Inline code stays highlighted, but code in pre doesn't */
:not(pre) > code {
  background-color: var(--primary-light);
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
}

/* Tables */
.table-container {
  width: 100%;
  overflow-x: auto;
  margin: 1.5rem 0;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95rem;
}

th {
  text-align: left;
  background-color: var(--bg-sidebar);
  padding: 1rem;
  border-bottom: 2px solid var(--border-light);
  font-weight: 700;
}

td {
  padding: 1rem;
  border-bottom: 1px solid var(--border-light);
}

/* Progress Bar */
#scroll-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 4px;
  background: var(--primary);
  z-index: 1000;
  transition: width 0.1s;
}

/* Responsive */
@media (max-width: 850px) {
  body {
    background: var(--bg-main); /* Reset background on mobile */
  }
  .app-container {
    padding-left: 0; /* Remove padding on mobile */
  }
  
  .sidebar {
    position: fixed;
    left: -280px;
    top: 0;
    z-index: 1500;
    transition: left 0.3s ease;
    box-shadow: var(--shadow-lg);
  }

  .sidebar.open {
    left: 0;
  }

  .mobile-menu-btn {
    display: flex;
  }

  .header h1 {
    font-size: 2.2rem;
    padding: 0 1rem;
    word-break: break-word;
  }
  
  .header p {
    font-size: 1rem;
    padding: 0 1rem;
  }

  .content-section {
    padding: 2.5rem 1rem; /* Even tighter spacing for mobile */
  }
  
  .grid-cols-2 {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .card {
    padding: 1.25rem;
  }

  pre {
    padding: 1rem;
    font-size: 0.85rem;
  }
}

/* Fix for small phones */
@media (max-width: 480px) {
  .header h1 {
    font-size: 1.8rem;
  }
  
  .header {
    padding: 3rem 1rem;
  }

  .back-to-top {
    right: 1.5rem;
    bottom: 6.5rem; /* Positioned above the mobile menu button */
  }
}

@media (max-width: 850px) {
  /* Disable custom scrollbar on mobile to prevent layout shift */
  ::-webkit-scrollbar {
    display: none;
  }
}

/* Back to Top Button */
.back-to-top {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 3.5rem;
  height: 3.5rem;
  background-color: var(--primary);
  color: white;
  border: none;
  border-radius: 50%;
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  opacity: 0;
  visibility: hidden;
  transform: translateY(20px);
  transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.back-to-top.show {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.back-to-top:hover {
  background-color: var(--text-main);
  transform: translateY(-5px);
  box-shadow: 0 15px 30px -5px rgba(0, 0, 0, 0.2);
}

.back-to-top:active {
  transform: scale(0.9) translateY(-5px);
}

/* Animations - Improved reliability */
.fade-in {
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.8s ease-out, transform 0.8s ease-out;
}

/* Only hide if JS is working and section is not yet visible */
.js-enabled .fade-in:not(.visible) {
  opacity: 0;
  transform: translateY(30px);
}

.fade-in.visible {
  opacity: 1;
  transform: translateY(0);
}
