:root {
  --bg-color: #0f172a;
  --sidebar-bg: #1e293b;
  --card-bg: rgba(30, 41, 59, 0.7);
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --accent-primary: #6366f1; /* Indigo 500 */
  --accent-hover: #4f46e5;
  --accent-glow: rgba(99, 102, 241, 0.5);
  --success: #22c55e;
  --error: #ef4444;
  --glass-border: rgba(255, 255, 255, 0.1);
  --option-bg: #334155;
  --option-hover: #475569;
  --progress-bg: rgba(255, 255, 255, 0.1);
  --nav-hover-bg: rgba(255, 255, 255, 0.05);
  --feedback-bg: rgba(255, 255, 255, 0.03);
  --main-gradient: radial-gradient(
      circle at top right,
      #312e81 0%,
      transparent 40%
    ),
    radial-gradient(circle at bottom left, #1e1b4b 0%, transparent 40%);
}

[data-theme="light"] {
  --bg-color: #f8fafc;
  --sidebar-bg: #ffffff;
  --card-bg: rgba(255, 255, 255, 0.85);
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --accent-primary: #4f46e5;
  --accent-hover: #4338ca;
  --accent-glow: rgba(79, 70, 229, 0.25);
  --success: #16a34a;
  --error: #dc2626;
  --glass-border: rgba(0, 0, 0, 0.1);
  --option-bg: #f1f5f9;
  --option-hover: #e2e8f0;

  --progress-bg: rgba(0, 0, 0, 0.1);
  --nav-hover-bg: rgba(0, 0, 0, 0.05);
  --feedback-bg: rgba(0, 0, 0, 0.03);
  --main-gradient: radial-gradient(
      circle at top right,
      #e0e7ff 0%,
      transparent 40%
    ),
    radial-gradient(circle at bottom left, #f1f5f9 0%, transparent 40%);
}

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

body {
  font-family: "Inter", sans-serif;
  background-color: var(--bg-color);
  color: var(--text-primary);
  min-height: 100vh;
  overflow-x: hidden;
}

/* Animations */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in {
  animation: fadeIn 0.4s ease-out forwards;
}

/* Layout */
.app-container {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 260px;
  background-color: var(--sidebar-bg);
  padding: 2rem;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--glass-border);
  position: fixed;
  height: 100vh;
  z-index: 10;
}

.main-content {
  flex: 1;
  margin-left: 260px;
  padding: 2rem 4rem;
  position: relative;
  background: var(--main-gradient);
}

/* Sidebar Components */
.logo {
  display: flex;
  align-items: center;
  gap: 1rem;
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 3rem;
  color: var(--text-primary);
}

.logo-icon {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, var(--accent-primary), #818cf8);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px var(--accent-glow);
}

.nav-links {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  transition: all 0.2s;
  font-size: 1rem;
  font-weight: 500;
}

.nav-item:hover {
  background: var(--nav-hover-bg);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--accent-primary);
  color: #fff;
  box-shadow: 0 4px 12px var(--accent-glow);
}

/* Stats Card */
.stats-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 1.25rem;
  margin-top: auto;
  border: 1px solid var(--glass-border);
}

.stat-group {
  margin-bottom: 1.5rem;
}

.stat-group:last-child {
  margin-bottom: 0;
}

.stat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 0.75rem; /* Increased */
  gap: 1rem; /* Ensure space */
}

.stat-value {
  color: var(--text-primary);
  font-weight: 600;
}

.progress-bar-bg {
  width: 100%;
  height: 6px;
  background: var(--progress-bg);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-primary), #a5b4fc);
  border-radius: 4px;
  transition: width 0.3s ease-out;
}

/* Pie Chart */
.pie-container {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.pie-chart {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: conic-gradient(var(--option-bg) 0% 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  /* Ensure pie rotation starts at top */
  transform: rotate(0deg);
}

.pie-inner {
  width: 44px;
  height: 44px;
  background: var(--sidebar-bg);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-primary);
}

.pie-legend {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.85rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-secondary);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.dot.correct {
  background: var(--success);
}
.dot.incorrect {
  background: var(--error);
}

/* Main Content Components */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

h2 {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.secondary-btn {
  background: transparent;
  border: 1px solid var(--glass-border);
  color: var(--text-secondary);
  padding: 0.5rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}

.secondary-btn:hover {
  border-color: var(--text-primary);
  color: var(--text-primary);
}

/* Quiz Card */
.glass-panel {
  background: var(--card-bg);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border: 1px solid var(--glass-border);
  border-radius: 24px;
  border-radius: 24px;
  padding: 2.5rem;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  max-width: 900px;
  margin: 0 auto;
  transition: transform 0.3s ease, border-color 0.3s ease;
  overflow: hidden; /* For header bg clip */
}

.question-card.marked .question-header {
  background: linear-gradient(to bottom, rgba(253, 224, 71, 0.15), transparent);
  margin: -2.5rem -2.5rem 2rem -2.5rem;
  padding: 2rem 2.5rem 0.5rem 2.5rem;
  border-bottom: 1px solid rgba(253, 224, 71, 0.1);
}

.question-header {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 2rem;
}

.q-badge {
  background: rgba(99, 102, 241, 0.1);
  color: #818cf8;
  padding: 0.25rem 0.75rem;
  border-radius: 99px;
  font-size: 0.875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.q-number {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-left: auto;
}

.question-text {
  font-size: 1.25rem;
  line-height: 1.6;
  margin-bottom: 2rem;
  font-weight: 400;
}

.question-text p {
  margin-bottom: 1rem;
}

.question-image-container {
  margin-bottom: 2rem;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--glass-border);
}

.question-image-container img {
  width: 100%;
  height: auto;
  display: block;
}

/* Options */
.options-grid {
  display: grid;
  gap: 1rem;
  margin-bottom: 2.5rem;
}

.option-card {
  background: var(--option-bg);
  padding: 1.25rem 1.5rem;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s;
  border: 2px solid transparent;
  display: flex;
  align-items: start;
  gap: 1rem;
  font-size: 1.1rem;
}

.option-card:hover:not(.disabled) {
  background: var(--option-hover);
  transform: translateY(-2px);
}

.option-card.selected {
  background: var(--option-hover);
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

.option-card.correct {
  background: rgba(34, 197, 94, 0.1);
  border-color: var(--success);
}
.option-card.incorrect {
  background: rgba(239, 68, 68, 0.1);
  border-color: var(--error);
}

.option-marker {
  width: 28px;
  height: 28px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  font-weight: 600;
  flex-shrink: 0;
}

.selected .option-marker {
  background: var(--accent-primary);
  color: white;
}

.correct .option-marker {
  background: var(--success);
}

.incorrect .option-marker {
  background: var(--error);
}

.action-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 1.5rem;
  border-top: 1px solid var(--glass-border);
}

.center-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
}

/* Make secondary btn in action bar match height nicely */
.action-bar .secondary-btn {
  padding: 0.75rem 1rem;
  font-size: 1rem;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: transparent;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  opacity: 0.7;
  transition: 0.2s;
}

.nav-btn:hover:not(:disabled) {
  opacity: 1;
  transform: translateX(4px);
}
.nav-btn:first-child:hover:not(:disabled) {
  transform: translateX(-4px);
}

.nav-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.primary-btn {
  background: var(--accent-primary);
  color: white;
  border: none;
  padding: 0.75rem 2rem;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px var(--accent-glow);
  transition: 0.2s;
}

.primary-btn:hover {
  background: var(--accent-hover);
  transform: translateY(-2px);
  box-shadow: 0 8px 16px var(--accent-glow);
}

.primary-btn:disabled {
  background: var(--glass-border);
  color: var(--text-secondary);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
  opacity: 0.6;
}

/* Feedback */
.feedback-section {
  margin-bottom: 2rem;
  padding: 1.5rem;
  border-radius: 12px;
  background: var(--feedback-bg);
  border: 1px solid var(--glass-border);
}

.feedback-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
  font-weight: 700;
  font-size: 1.1rem;
}

.feedback-success {
  color: var(--success);
}
.feedback-error {
  color: var(--error);
}

.explanation-box {
  color: var(--text-secondary);
  line-height: 1.6;
  white-space: pre-wrap; /* Preserve whitespace and breaks */
}

/* Utilities */
.hidden {
  display: none;
}

/* MathJax/Katex styles overrides */
.katex {
  font-size: 1.1em;
}

/* Latex-specific adjustment (quill-math) */
.quill-math {
  display: inline-block;
}

#mark-btn.active {
  background: rgba(253, 224, 71, 0.2);
  color: #fde047;
  border-color: #fde047;
}

/* Modal */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
}

.modal.hidden {
  display: none !important;
}

/* Global hidden utility */
.hidden {
  display: none !important;
}

/* Lightbox Specific */
#lightbox-modal {
  background: rgba(0, 0, 0, 0.95);
  cursor: zoom-out;
}

#lightbox-image {
  max-width: 95%;
  max-height: 95vh;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

.modal-content {
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  padding: 2rem;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 1.5rem;
  cursor: pointer;
}

.grid-item:hover {
  background: var(--option-hover);
}

.grid-item.current {
  border-color: var(--accent-primary);
  background: rgba(99, 102, 241, 0.1);
  color: var(--accent-primary);
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(50px, 1fr));
  gap: 0.75rem;
  overflow-y: auto;
  padding-right: 0.5rem;
}

.grid-item {
  aspect-ratio: 1;
  background: var(--option-bg);
  border-radius: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.grid-item:hover {
  background: var(--option-hover);
}

.grid-item.current {
  border-color: var(--accent-primary);
  background: rgba(99, 102, 241, 0.1);
  color: var(--accent-primary);
}

.grid-item.answered {
  /* Default answered style (maybe neutral or specific if correct/incorrect not known yet, but logic sets inline styles) */
  /* This class is mainly a hook in JS */
}

.grid-item.correct {
  background: rgba(34, 197, 94, 0.1);
  color: var(--success);
  border-color: rgba(34, 197, 94, 0.3);
}

.grid-item.incorrect {
  background: rgba(239, 68, 68, 0.1);
  color: var(--error);
  border-color: rgba(239, 68, 68, 0.3);
}

.grid-item.marked {
  position: relative;
  /* Ensure text is above markers */
  z-index: 1;
}

.grid-item.marked::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 70%;
  height: 70%;
  background: rgba(253, 224, 71, 0.2);
  border-radius: 50%;
  z-index: -1;
}

/* Ensure marked status is visible even if answered */
.grid-item.marked.correct::before {
  background: rgba(253, 224, 71, 0.3); /* Slightly brighter on colored bg */
}
.grid-item.marked.incorrect::before {
  background: rgba(253, 224, 71, 0.3);
}

/* Default Hidden State for Mobile Stats */
.mobile-only-stats {
  display: none;
}

/* Mobile Responsive */
@media (max-width: 768px) {
  .app-container {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    max-width: 100vw;
    box-sizing: border-box;
    height: auto;
    position: relative;
    padding: 0.75rem 1rem;
    flex-direction: row;
    justify-content: flex-start;
    gap: 1rem;
    align-items: center;
    border-right: none;
    border-bottom: 1px solid var(--glass-border);
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch; /* smooth scrolling */
  }

  /* Hide scrollbar for cleaner look */
  .sidebar::-webkit-scrollbar {
    display: none;
  }
  .sidebar {
    -ms-overflow-style: none; /* IE and Edge */
    scrollbar-width: none; /* Firefox */
  }

  .logo {
    margin-bottom: 0;
    font-size: 1.2rem;
    flex-shrink: 0;
    margin-right: auto; /* Push nav to right slightly if space exists, or just gap */
  }

  .logo span {
    display: block; /* Show title on mobile header */
  }

  .nav-links {
    flex-direction: row;
    flex: 0 0 auto;
  }

  .nav-item span:not(.icon) {
    display: none;
  }

  .nav-item {
    padding: 0.5rem;
  }

  .stats-card {
    display: none; /* Hide sidebar stats on mobile */
  }

  /* Show our new mobile stats container */
  .mobile-only-stats {
    display: block;
    margin-top: 0; /* Attach to top bar */
    margin-bottom: 1rem;
    padding: 0.75rem 1rem;
    background: var(--card-bg);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    font-size: 0.85rem;
  }

  .main-content {
    margin-left: 0;
    padding: 1rem;
  }

  /* Mobile Stats Styling */
  .stat-row.compact-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.4rem;
  }

  .stat-group-mobile {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .stat-label-inline {
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-transform: uppercase;
  }

  .count-item {
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
    font-weight: 600;
  }

  .count-item.correct {
    color: var(--success);
    margin-right: 0.5rem;
  }
  .count-item.incorrect {
    color: var(--error);
  }

  .count-item .icon {
    font-size: 0.9em;
  }

  /* Previous generic classes can be overridden or ignored */

  .accuracy-bar-container {
    width: 100%;
    height: 6px;
    background: var(--progress-bg);
    border-radius: 3px;
    display: flex;
    overflow: hidden;
  }

  .acc-bar-segment {
    height: 100%;
    transition: width 0.3s ease;
  }

  .acc-bar-segment.correct {
    background: var(--success);
  }

  .acc-bar-segment.incorrect {
    background: var(--error);
  }

  .top-bar {
    flex-direction: row; /* Keep inline on mobile */
    gap: 0.5rem;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  h2 {
    font-size: 1.25rem; /* Smaller title on mobile */
    margin: 0;
  }

  .controls {
    width: auto; /* Shrink to fit content */
    display: flex;
    gap: 0.5rem;
  }

  .secondary-btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.85rem;
  }

  .glass-panel {
    padding: 1.5rem;
  }

  .question-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .action-bar {
    flex-wrap: wrap;
    gap: 1rem;
  }

  /* Unwrap the group on mobile so elements flow naturally */
  .center-actions {
    display: contents;
  }

  .primary-btn {
    width: 100%;
    order: -1;
  }

  .nav-btn {
    flex: 1;
    justify-content: center;
  }
}

.info-footer {
  text-align: center;
  margin-top: 3rem;
  padding: 1rem;
  color: var(--text-secondary);
  font-size: 0.85rem;
  opacity: 0.7;
}

/* Loading Spinner */
.loading-spinner {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.25rem;
  color: var(--accent-primary);
  font-weight: 500;
  padding: 3rem;
  background: rgba(30, 41, 59, 0.4);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border-radius: 12px;
  border: 1px solid var(--glass-border);
}

.error-msg {
  color: var(--error);
  background: rgba(239, 68, 68, 0.1);
  padding: 1.5rem;
  border-radius: 8px;
  border: 1px solid rgba(239, 68, 68, 0.3);
  text-align: center;
}
