/* --- Variables & Themes --- */
:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Inter", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  /* Dark Theme (Default) */
  --bg-body: #0a0e17;
  --bg-card: #111827;
  --bg-card-subtle: #172033;
  --bg-choice: #161f30;
  --bg-choice-hover: #1e293b;
  --border-subtle: rgba(255, 255, 255, 0.08);
  --border-hover: rgba(255, 255, 255, 0.16);

  --text-main: #f8fafc;
  --text-body: #cbd5e1;
  --text-muted: #64748b;

  --accent-primary: #3b82f6;
  --accent-primary-hover: #2563eb;
  --accent-green: #10b981;
  --accent-green-bg: rgba(16, 185, 129, 0.12);
  --accent-red: #ef4444;
  --accent-red-bg: rgba(239, 68, 68, 0.12);
  --accent-gold: #f59e0b;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 18px;
  --radius-full: 9999px;
  --shadow-card: 0 10px 30px -10px rgba(0, 0, 0, 0.5), 0 0 1px 1px rgba(255, 255, 255, 0.05);
}

body.theme-light {
  --bg-body: #f8fafc;
  --bg-card: #ffffff;
  --bg-card-subtle: #f1f5f9;
  --bg-choice: #f8fafc;
  --bg-choice-hover: #edf2f7;
  --border-subtle: rgba(0, 0, 0, 0.08);
  --border-hover: rgba(0, 0, 0, 0.16);

  --text-main: #0f172a;
  --text-body: #334155;
  --text-muted: #94a3b8;

  --accent-primary: #2563eb;
  --accent-primary-hover: #1d4ed8;
  --accent-green-bg: rgba(16, 185, 129, 0.08);
  --accent-red-bg: rgba(239, 68, 68, 0.08);
  --shadow-card: 0 10px 30px -10px rgba(0, 0, 0, 0.08), 0 0 1px 1px rgba(0, 0, 0, 0.05);
}

/* --- Reset & Layout --- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  height: 100%;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-body);
  color: var(--text-body);
  line-height: 1.5;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* Top Progress Track */
.top-progress-track {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: transparent;
  z-index: 100;
}

.top-progress-fill {
  height: 100%;
  background: var(--accent-green);
  transition: width 0.3s ease;
  box-shadow: 0 0 8px var(--accent-green);
}

.app-container {
  max-width: 820px;
  width: 100%;
  height: 100%;
  height: 100dvh;
  margin: 0 auto;
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
}

/* --- Fixed Clean Header --- */
.app-header {
  flex-shrink: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: max(0.5rem, env(safe-area-inset-top));
  padding-bottom: 0.65rem;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-body);
  z-index: 50;
  width: 100%;
  gap: 0.75rem;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.logo {
  font-size: 1.35rem;
}

.clean-select {
  background: var(--bg-card);
  color: var(--text-main);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-full);
  padding: 0.35rem 0.85rem;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}

.clean-select:focus {
  border-color: var(--accent-primary);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

/* Segmented Control */
.segmented-control {
  display: inline-flex;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-full);
  padding: 2px;
}

.seg-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.35rem 0.85rem;
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: all 0.2s ease;
}

.seg-btn.active {
  background: var(--accent-primary);
  color: #ffffff;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s;
}

.icon-btn:hover {
  color: var(--text-main);
  background: var(--bg-card);
  border-color: var(--border-subtle);
}

.icon-btn kbd {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  font-weight: 700;
}

/* --- Collapsible Drawer --- */
.drawer-panel {
  position: absolute;
  top: calc(max(0.5rem, env(safe-area-inset-top)) + 52px);
  left: max(1rem, env(safe-area-inset-left));
  right: max(1rem, env(safe-area-inset-right));
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.6);
  padding: 1rem 1.25rem;
  z-index: 45;
  animation: slideDown 0.15s ease-out;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

.drawer-grid {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
}

.drawer-field {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.drawer-field label {
  font-size: 0.775rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-muted);
}

.form-select {
  background: var(--bg-body);
  color: var(--text-main);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
  outline: none;
}

.drawer-buttons {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
}

.btn-subtle {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: transparent;
  border: 1px solid var(--border-subtle);
  color: var(--text-body);
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-subtle:hover {
  background: var(--bg-card-subtle);
  border-color: var(--border-hover);
}

.btn-subtle.text-muted {
  color: var(--text-muted);
  border-color: transparent;
}

/* --- Main Question Hero Stage --- */
.main-content {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.5rem 0;
}

.card-top-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0.25rem 0.45rem;
  font-size: 0.825rem;
  color: var(--text-muted);
  width: 100%;
}

.info-breadcrumbs {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.info-id {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--text-main);
}

.info-citation {
  background: rgba(59, 130, 246, 0.12);
  color: var(--accent-primary);
  padding: 0.1rem 0.4rem;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 0.75rem;
}

.info-stats {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.info-counter strong {
  color: var(--text-main);
}

.star-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px;
  transition: all 0.2s;
}

.star-btn:hover {
  color: var(--accent-gold);
  transform: scale(1.15);
}

.star-btn.active {
  color: var(--accent-gold);
}

.star-btn.active svg {
  fill: var(--accent-gold);
}

/* Hero Card */
.card-wrapper {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  width: 100%;
  max-width: 680px;
  margin: auto 0;
}

.hero-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 1.5rem 1.75rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 440px;
  outline: none;
  cursor: default;
  position: relative;
  transition: border-color 0.15s ease;
}

/* In Flashcard mode, fixed generous card height so it never resizes between questions or answers */
body.mode-flashcard .hero-card {
  height: 440px;
  min-height: 440px;
  cursor: pointer;
}

body.mode-flashcard .hero-card:hover {
  border-color: var(--border-hover);
}

body.mode-flashcard .question-container,
.hero-card:has(#choicesContainer.hidden) .question-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 1rem 0.5rem;
}

body.mode-flashcard .question-text,
.hero-card:has(#choicesContainer.hidden) .question-text {
  font-size: 1.35rem;
  line-height: 1.55;
  text-align: center;
}

/* Question Typography - THE HERO */
.question-container {
  min-height: 3.25rem;
  display: flex;
  align-items: center;
}

.question-text {
  font-size: 1.28rem;
  font-weight: 600;
  line-height: 1.5;
  color: var(--text-main);
  letter-spacing: -0.01em;
}

/* Figure / Schematic */
.figure-container {
  display: flex;
  justify-content: center;
}

.figure-card {
  position: relative;
  cursor: zoom-in;
  max-height: 220px;
  background: #ffffff;
  padding: 0.75rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  overflow: hidden;
  display: flex;
  justify-content: center;
}

.figure-card img {
  max-height: 200px;
  max-width: 100%;
  object-fit: contain;
}

.figure-badge {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background: rgba(15, 23, 42, 0.85);
  color: #ffffff;
  font-size: 0.72rem;
  padding: 0.2rem 0.5rem;
  border-radius: var(--radius-sm);
  pointer-events: none;
}

/* Choices - Clean, Clickable & Focusable */
.choices-container {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.choice-btn {
  display: flex;
  align-items: flex-start;
  gap: 0.95rem;
  background: var(--bg-choice);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0.95rem 1.15rem;
  font-size: 1.05rem;
  color: var(--text-main);
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  transition: all 0.15s ease;
  line-height: 1.45;
}

.choice-btn:hover:not(:disabled) {
  background: var(--bg-choice-hover);
  border-color: var(--border-hover);
  transform: translateY(-1px);
}

.choice-key {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--text-muted);
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
  margin-top: 1px;
}

.choice-text {
  flex: 1;
}

/* States on Choice Buttons */
.choice-btn.selected-correct,
.choice-btn.reveal-correct {
  background: var(--accent-green-bg);
  border-color: var(--accent-green);
  color: #ffffff;
  box-shadow: 0 0 0 1px var(--accent-green);
}

.choice-btn.selected-correct .choice-key,
.choice-btn.reveal-correct .choice-key {
  background: var(--accent-green);
  color: #ffffff;
  border-color: var(--accent-green);
}

.choice-btn.selected-correct::after,
.choice-btn.reveal-correct::after {
  content: "✓";
  margin-left: auto;
  color: var(--accent-green);
  font-weight: 800;
  font-size: 1.15rem;
  line-height: 1;
  padding-left: 0.5rem;
}

.choice-btn.selected-wrong {
  background: var(--accent-red-bg);
  border-color: var(--accent-red);
}

.choice-btn.selected-wrong .choice-key {
  background: var(--accent-red);
  color: #ffffff;
  border-color: var(--accent-red);
}

.choice-btn.selected-wrong::after {
  content: "✕";
  margin-left: auto;
  color: var(--accent-red);
  font-weight: 800;
  font-size: 1rem;
  line-height: 1;
  padding-left: 0.5rem;
}

.choice-btn.faded {
  opacity: 0.35;
}

/* --- Flashcard Mode Face (Only right answer) --- */
.flashcard-back {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  gap: 1.15rem;
  padding: 1.5rem 1.25rem;
  background: var(--bg-choice);
  border: 1px solid var(--accent-green);
  border-radius: var(--radius-md);
  box-shadow: 0 0 0 1px var(--accent-green);
  animation: fadeIn 0.2s ease-out;
}

.flashcard-pill {
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent-green);
  background: var(--accent-green-bg);
  padding: 0.25rem 0.75rem;
  border-radius: var(--radius-full);
}

.flashcard-answer-text {
  font-size: 1.35rem;
  font-weight: 600;
  color: #ffffff;
  line-height: 1.55;
  max-width: 580px;
}

.flashcard-rule {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.flip-hint {
  text-align: center;
  font-size: 0.775rem;
  color: var(--text-muted);
  user-select: none;
  padding-top: 0.5rem;
}

/* Action Navigation */
.action-nav {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.75rem;
}

.nav-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem 1.35rem;
  font-size: 0.95rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  border: none;
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-btn-secondary {
  background: var(--bg-card);
  color: var(--text-body);
  border: 1px solid var(--border-subtle);
}

.nav-btn-secondary:hover {
  background: var(--bg-card-subtle);
  color: var(--text-main);
  border-color: var(--border-hover);
}

.nav-btn-primary {
  background: var(--accent-primary);
  color: #ffffff;
  min-width: 170px;
}

.nav-btn-primary:hover {
  background: var(--accent-primary-hover);
}

.space-kbd {
  font-family: var(--font-mono);
  font-size: 0.725rem;
  background: rgba(0, 0, 0, 0.25);
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
  color: rgba(255, 255, 255, 0.85);
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1.5rem;
}

.modal-content {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 1.5rem;
  position: relative;
  max-width: 500px;
  width: 100%;
}

.modal-image-content {
  max-width: 750px;
  background: #ffffff;
  color: #0f172a;
}

.modal-image-content h3 {
  color: #0f172a;
  margin-bottom: 0.75rem;
}

.modal-image-content .modal-body {
  display: flex;
  justify-content: center;
  max-height: 70vh;
  overflow: auto;
}

.modal-image-content img {
  max-width: 100%;
  height: auto;
}

.modal-close-btn {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: transparent;
  border: none;
  font-size: 1.5rem;
  color: var(--text-muted);
  cursor: pointer;
}

.modal-close-btn:hover {
  color: var(--text-main);
}

.shortcuts-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 1rem;
}

.shortcuts-table td {
  padding: 0.5rem 0.25rem;
  border-bottom: 1px solid var(--border-subtle);
  font-size: 0.875rem;
}

.shortcuts-table td:first-child {
  font-weight: 600;
  color: var(--accent-primary);
}

.shortcuts-table kbd {
  font-family: var(--font-mono);
  background: var(--bg-card-subtle);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

.empty-state {
  text-align: center;
  padding: 3rem 1.5rem;
  background: var(--bg-card);
  border: 1px dashed var(--border-subtle);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.empty-icon {
  font-size: 2.5rem;
}

.hidden {
  display: none !important;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 640px) {
  .app-container {
    padding-left: max(0.75rem, env(safe-area-inset-left));
    padding-right: max(0.75rem, env(safe-area-inset-right));
    padding-bottom: max(0.4rem, env(safe-area-inset-bottom));
  }
  .app-header {
    padding-top: max(0.4rem, env(safe-area-inset-top));
    padding-bottom: 0.5rem;
  }
  .card-wrapper {
    gap: 0.6rem;
  }
  .hero-card {
    padding: 1rem 0.95rem;
    gap: 0.85rem;
    border-radius: var(--radius-md);
    min-height: 380px;
  }
  body.mode-flashcard .hero-card {
    height: 380px;
    min-height: 380px;
  }
  .question-container {
    min-height: unset;
  }
  .question-text {
    font-size: 1.12rem;
    line-height: 1.4;
  }
  body.mode-flashcard .question-text {
    font-size: 1.18rem;
  }
  .flashcard-answer-text {
    font-size: 1.18rem;
  }
  .choices-container {
    gap: 0.4rem;
  }
  .choice-btn {
    padding: 0.65rem 0.8rem;
    font-size: 0.92rem;
    gap: 0.65rem;
    line-height: 1.35;
  }
  .choice-key {
    padding: 0.15rem 0.45rem;
    font-size: 0.8rem;
  }
  .figure-card {
    max-height: 130px;
    padding: 0.35rem;
  }
  .figure-card img {
    max-height: 115px;
  }
  .action-nav {
    width: 100%;
    gap: 0.45rem;
    padding-top: 0.2rem;
  }
  .nav-btn {
    padding: 0.6rem 0.85rem;
    font-size: 0.875rem;
  }
  .nav-btn-primary {
    flex: 1;
    min-width: 130px;
  }
}

/* Compact screens or small mobile height */
@media (max-height: 720px) {
  .hero-card {
    padding: 0.85rem 0.85rem;
    gap: 0.65rem;
  }
  .question-text {
    font-size: 1.05rem;
    line-height: 1.35;
  }
  .choices-container {
    gap: 0.35rem;
  }
  .choice-btn {
    padding: 0.55rem 0.75rem;
    font-size: 0.88rem;
  }
  .figure-card {
    max-height: 100px;
  }
  .figure-card img {
    max-height: 90px;
  }
  .card-top-info {
    font-size: 0.75rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
