* {
  box-sizing: border-box;
}

:root {
  --bg-color: #f0f2f5;
  --sidebar-bg: #ffffff;
  --card-bg: #ffffff;
  --text-color: #1a1a1a;
  --accent-color: #3b82f6;
  --border-color: #e5e7eb;
  --hover-color: #f3f4f6;
  --heatmap-base: #e0f2fe;
  --heatmap-dark: #0c4a6e;
  --glass-bg: rgba(255, 255, 255, 0.8);
  --glass-border: rgba(255, 255, 255, 0.3);
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --transition-speed: 0.3s;
}

/* Dark Theme */
[data-theme="dark"] {
  --bg-color: #0f172a;
  --sidebar-bg: #1e293b;
  --card-bg: #1e293b;
  --text-color: #f1f5f9;
  --accent-color: #60a5fa;
  --border-color: #334155;
  --hover-color: #334155;
  --heatmap-base: #1e3a5f;
  --heatmap-dark: #60a5fa;
  --glass-bg: rgba(30, 41, 59, 0.9);
  --glass-border: rgba(51, 65, 85, 0.5);
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -1px rgba(0, 0, 0, 0.2);
}

/* Logo in Dark Mode */
[data-theme="dark"] .logo-img {
  filter: brightness(0) invert(1);
  mix-blend-mode: screen;
}

/* Smooth transitions for theme switch */
body,
#sidebar,
#main-content,
.header-container,
#heatmap-container,
.site-footer,
.heatmap-wrapper,
.verse-cell {
  transition: background-color var(--transition-speed) ease,
    color var(--transition-speed) ease,
    border-color var(--transition-speed) ease;
}

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100vh - 85px);
  /* Viewport minus footer height */
}

#sidebar {
  width: 300px;
  background-color: var(--sidebar-bg);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  height: 100%;
  box-shadow: var(--shadow);
  z-index: 10;
}

.logo-area {
  padding: 24px;
  border-bottom: 1px solid var(--border-color);
}

.logo-link {
  display: block;
  text-decoration: none;
}

.logo-img {
  width: 100%;
  height: auto;
  display: block;
  margin: -20px 0;
  /* Crop top/bottom whitespace from image */
}

/* Search Container */
.search-container {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
}

.parasha-search {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-size: 0.9rem;
  font-family: inherit;
  background: var(--bg-color);
  color: var(--text-color);
  transition: all 0.2s ease;
  box-sizing: border-box;
}

.parasha-search:focus {
  outline: none;
  border-color: var(--accent-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.parasha-search::placeholder {
  color: #9ca3af;
}

.nav-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.book-section {
  margin-bottom: 24px;
}

.book-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  cursor: pointer;
  margin-bottom: 4px;
  border-radius: 8px;
  transition: background-color 0.2s ease;
}

.book-header:hover {
  background-color: var(--hover-color);
}

.book-title {
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6b7280;
  font-weight: 600;
  transition: color 0.2s ease;
}

.book-header:hover .book-title {
  color: var(--accent-color);
}

.chevron {
  font-size: 0.8rem;
  color: #9ca3af;
  transition: transform 0.3s ease;
}

.book-section.expanded .chevron {
  transform: rotate(180deg);
}

.book-section.expanded .book-title {
  color: var(--accent-color);
}

.parasha-list {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
  opacity: 0;
}

.book-section.expanded .parasha-list {
  max-height: 1000px;
  /* Arbitrary large height */
  opacity: 1;
  transition: max-height 0.5s ease-in, opacity 0.3s ease-in;
}

.parasha-item {
  padding: 10px 12px;
  margin-bottom: 4px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 0.95rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.parasha-item:hover {
  background-color: var(--hover-color);
  transform: translateX(4px);
}

.parasha-item.active {
  background-color: var(--hover-color);
  color: var(--accent-color);
  font-weight: 600;
  border-left: 3px solid var(--accent-color);
}

#main-content {
  flex: 1;
  padding: 40px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.header-container {
  width: 100%;
  max-width: 1000px;
  margin-bottom: 32px;
  background: var(--glass-bg);
  backdrop-filter: blur(12px);
  padding: 24px;
  border-radius: 16px;
  border: 1px solid var(--glass-border);
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 48px;
}

.header-content h2 {
  margin: 0 0 8px 0;
  font-size: 2rem;
  font-weight: 800;
}

.header-content p {
  margin: 0;
  color: #6b7280;
}

.ref-badge {
  background-color: #eff6ff;
  color: var(--accent-color);
  padding: 6px 12px;
  border-radius: 9999px;
  font-size: 0.875rem;
  font-weight: 500;
}

/* Commentary Filter */
.filter-container {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  width: 100%;
  max-width: 1000px;
  background: var(--glass-bg);
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid var(--glass-border);
}

.filter-container label {
  font-weight: 500;
  color: var(--text-color);
  white-space: nowrap;
}

.category-filter {
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--bg-color);
  color: var(--text-color);
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  min-width: 150px;
}

.category-filter:focus {
  outline: none;
  border-color: var(--accent-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

#heatmap-container {
  width: 100%;
  max-width: 1000px;
  background: var(--sidebar-bg);
  padding: 32px;
  border-radius: 16px;
  box-shadow: var(--shadow);
  flex-grow: 1;
  /* Grow to fill available space */
}



.chapter-section {
  margin-bottom: 40px;
}

.chapter-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text-color);
  border-bottom: 2px solid var(--border-color);
  padding-bottom: 8px;
  display: inline-block;
}

.heatmap-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 8px;
}

.verse-cell {
  aspect-ratio: 1;
  background-color: var(--heatmap-base);
  border-radius: 6px;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.verse-number {
  font-size: 0.7rem;
  font-weight: 600;
  pointer-events: none;
  user-select: none;
}

.verse-cell:hover {
  transform: scale(1.15);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 2;
  border: 2px solid white;
}

.verse-cell:focus {
  outline: 3px solid var(--accent-color);
  outline-offset: 2px;
  z-index: 3;
}

.loading-state,
.empty-state {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 400px;
  color: #6b7280;
  font-size: 1.1rem;
}

/* Enhanced Loading Container */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  gap: 20px;
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border-color);
  border-top-color: var(--accent-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-text {
  font-size: 1rem;
  color: #6b7280;
  font-weight: 500;
}

.loading-progress {
  width: 200px;
  height: 6px;
  background: var(--border-color);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent-color), #2563eb);
  border-radius: 3px;
  transition: width 0.3s ease;
}

/* Skeleton Animation */
.skeleton-grid {
  display: grid;
  grid-template-columns: repeat(6, 40px);
  gap: 8px;
  margin-top: 20px;
}

.skeleton-cell {
  aspect-ratio: 1;
  background: linear-gradient(90deg, #e5e7eb 25%, #f3f4f6 50%, #e5e7eb 75%);
  background-size: 200% 100%;
  border-radius: 6px;
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0% {
    background-position: 200% 0;
  }

  100% {
    background-position: -200% 0;
  }
}

/* Tooltip Customisation */
[data-tippy-root] {
  font-family: 'Inter', sans-serif;
}

.tooltip-content {
  text-align: center;
}

.tooltip-ref {
  font-weight: 700;
  margin-bottom: 4px;
  display: block;
}

.tooltip-count {
  font-size: 0.9em;
  opacity: 0.9;
}

.tooltip-hebrew {
  font-size: 0.95em;
  margin: 6px 0;
  line-height: 1.5;
  color: #e0e0e0;
  direction: rtl;
  text-align: right;
  font-family: 'SBL Hebrew', 'Ezra SIL', 'Times New Roman', serif;
}

/* Header Actions */
.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

/* Dark Mode Toggle */
.dark-mode-toggle {
  background: var(--glass-bg);
  border: 1px solid var(--border-color);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  transition: all 0.2s ease;
  box-shadow: var(--shadow);
}

/* Legend Bar */
.legend-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: white;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  flex-wrap: wrap;
}

/* Reduced whitespace for heatmap wrappers */
.heatmap-wrapper {
  background: var(--card-bg);
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  border: 1px solid var(--border-color);
  margin-top: 10px;
  /* Reduced from 20px */
  width: 100%;
}

.dark-mode-toggle:hover {
  transform: scale(1.05);
}

/* Show/hide icons based on theme */
.dark-mode-toggle .light-icon {
  display: none;
}

[data-theme="dark"] .dark-mode-toggle .dark-icon {
  display: none;
}

[data-theme="dark"] .dark-mode-toggle .light-icon {
  display: block;
}

/* This Week Container in Sidebar */
.this-week-container {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
}

/* This Week Button */
.this-week-btn {
  background: linear-gradient(135deg, var(--accent-color), #2563eb);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
  transition: all 0.2s ease;
}

.this-week-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}

/* Info Button */


/* Legend Bar */
.legend-bar {
  width: 100%;
  max-width: 1000px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: var(--sidebar-bg);
  border-radius: 12px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 5;
}

.legend-gradient {
  flex: 1;
  height: 16px;
  border-radius: 8px;
  background: linear-gradient(to right,
      hsl(210, 85%, 95%),
      hsl(210, 85%, 75%),
      hsl(210, 85%, 55%),
      hsl(210, 85%, 35%));
  border: 1px solid var(--border-color);
}

.legend-label {
  font-size: 0.8rem;
  color: #6b7280;
  white-space: nowrap;
}

/* Intro Content in Empty State */
.intro-content {
  max-width: 600px;
  text-align: center;
  padding: 20px 20px 10px 20px;
}

.intro-content h3 {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0 0 16px 0;
  color: var(--accent-color);
}

.intro-content p {
  margin: 0 0 16px 0;
  line-height: 1.7;
  color: #4b5563;
}

.intro-content .intro-cta {
  font-weight: 600;
  color: var(--accent-color);
  margin-top: 24px;
}

/* Empty state adjustment for intro */
.empty-state {
  height: auto;
  min-height: 400px;
}

@media screen and (max-width: 768px) {
  html {
    height: auto;
    overflow-x: hidden;
  }

  body {
    display: block;
    /* Disable flex on mobile */
    height: auto;
    min-height: 100vh;
    overflow-y: visible;
    /* Let html handle scroll or body grow */
    overflow-x: hidden;
  }

  .app-container {
    display: block;
    flex: none;
    height: auto;
    overflow: visible;
  }

  #sidebar {
    width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    /* Pin to top */
    top: 0;
    z-index: 200;
  }

  .logo-area {
    padding: 8px 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--sidebar-bg);
  }

  /* Smaller logo on mobile */
  .logo-img {
    width: auto;
    height: 36px;
    margin: 0;
  }

  /* Hide This Week and Search in header on mobile - they're in hamburger menu */
  .this-week-container,
  .search-container {
    display: none;
  }

  /* Show them when nav-scroll is open */
  .nav-scroll.open~.this-week-container,
  .nav-scroll.open~.search-container {
    display: block;
  }

  #menu-toggle {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-color);
    padding: 4px;
    display: flex;
  }

  /* Hidden by default on mobile */
  .nav-scroll {
    display: none;
    padding: 8px 16px;
    border-top: 1px solid var(--border-color);
    position: fixed;
    /* Fixed overlay */
    top: 60px;
    bottom: 0;
    left: 0;
    right: 0;
    height: auto;
    background: var(--sidebar-bg);
    z-index: 100;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    overflow-y: auto;
    /* Allow menu to scroll internally */
  }

  /* Show when open */
  .nav-scroll.open {
    display: block;
  }

  /* Main content adjustment */
  #main-content {
    display: block;
    /* Disable flex on mobile */
    padding: 16px;
    width: 100%;
    box-sizing: border-box;
    overflow: visible;
    height: auto;
  }

  /* Indentation for nesting */
  .parasha-item {
    margin-left: 12px;
    border-left: 2px solid var(--border-color);
    padding: 12px;
  }

  .header-container {
    flex-direction: column;
    align-items: flex-start;
    padding: 16px;
    gap: 12px;
  }

  .header-content h2 {
    font-size: 1.5rem;
  }

  #heatmap-container {
    padding: 16px;
  }

  .heatmap-grid {
    grid-template-columns: repeat(auto-fill, minmax(32px, 1fr));
    gap: 4px;
  }

  /* Improve touch targets for parasha items */
  .parasha-item {
    padding: 12px;
  }
}

/* Hide toggle on desktop */
@media screen and (min-width: 769px) {
  #menu-toggle {
    display: none;
  }
}

/* Footer Styles */
.site-footer {
  padding: 24px 40px;
  text-align: center;
  color: #6b7280;
  font-size: 0.9rem;
  border-top: 1px solid var(--border-color);
  background: var(--sidebar-bg);
  flex-shrink: 0;
}

.footer-content {
  max-width: 800px;
  margin: 0 auto;
}

.footer-about {
  margin: 0 0 12px 0;
  line-height: 1.5;
}

.footer-links {
  margin: 0;
  color: #9ca3af;
}

.footer-links strong {
  color: var(--text-color);
}

.site-footer a {
  color: var(--accent-color);
  text-decoration: none;
}

.site-footer a:hover {
  text-decoration: underline;
}

/* Global Heatmap */
.heatmap-wrapper {
  background: var(--card-bg);
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  border: 1px solid var(--border-color);
  margin-top: 20px;
  width: 100%;
}

.global-book-row {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  gap: 16px;
}

.global-book-label {
  width: 100px;
  font-size: 0.9rem;
  font-weight: 600;
  color: #4b5563;
  flex-shrink: 0;
}

.global-chapters-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  flex-grow: 1;
}

.global-chapter-cell {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 500;
  border-radius: 2px;
  cursor: pointer;
  transition: transform 0.1s ease, z-index 0s;
}

.global-chapter-cell:hover {
  transform: scale(1.4);
  z-index: 10;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
}

/* Responsive adjustments */
@media (max-width: 768px) {
  .global-book-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    margin-bottom: 16px;
  }

  .global-book-label {
    width: 100%;
    margin-bottom: 4px;
  }

  .global-chapter-cell {
    width: 20px;
    height: 20px;
    font-size: 0.6rem;
  }
}

/* Switch Toggle */
.scale-toggle-container {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
}

input:checked+.slider {
  background-color: var(--accent-color);
}

input:focus+.slider {
  box-shadow: 0 0 1px var(--accent-color);
}

input:checked+.slider:before {
  transform: translateX(16px);
}

.slider.round {
  border-radius: 24px;
}

.slider.round:before {
  border-radius: 50%;
}