/* ═══════════════════════════════════════════════════════════
   SCROLL LOCK
   ModalManager.open() adds .modal-open to body automatically.
   Locking both html AND body is required — browsers scroll
   the html element, so locking only body does nothing.
   touch-action: none prevents iOS momentum scroll bleed-through.

   This covers ALL modals (solution, navigator, study, overtime)
   without needing manual lockBodyScroll() calls in each JS file.

   Settings panel uses .settings-panel-open class on body
   (set in navbar.js) so it can be released independently.
   ═══════════════════════════════════════════════════════════ */
body.modal-open,
html:has(body.modal-open) {
  overflow: hidden;
  touch-action: none;
}

body.settings-panel-open,
html:has(body.settings-panel-open) {
  overflow: hidden;
  touch-action: none;
}

/* ── Overlay ───────────────────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed;
  z-index: 2000;
  left: 0; top: 0;
  width: 100%; height: 100%;
  background-color: var(--color-modal-backdrop);
  backdrop-filter: blur(4px);
  font-family: var(--font-family);
  overscroll-behavior: contain;
  touch-action: none;
  /* Soft backdrop fade — modal panel itself uses slide animation */
  animation: modalFadeIn 0.15s ease-out;
}
.modal-overlay.open {
  display: flex !important;
  align-items: center;
  justify-content: center;
}

/* ── Modal shell ───────────────────────────────────────── */
.modal {
  background-color: var(--bg-card);
  margin: auto;
  padding: 0;
  border: none;
  width: 90%;
  max-width: 520px;
  border-radius: var(--border-radius-xl);
  box-shadow: var(--shadow-xl);
  position: relative;
  animation: modalSlideUp 0.18s cubic-bezier(0.16,1,0.3,1);
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  overflow: hidden; /* clips scrollbar track to border-radius */
}
.modal--large { max-width: 640px; }

/* ── Generic modal header (solution modal etc.) ────────── */
.modal-header {
  padding: var(--space-lg) var(--space-xl);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border-color);
}
.modal-header h2 {
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-bold);
}
.modal-close-btn {
  position: absolute;
  top: var(--space-md);
  right: var(--space-md);
  width: 32px; height: 32px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: var(--font-size-base);
  cursor: pointer;
  border-radius: var(--border-radius-sm);
  display: flex; align-items: center; justify-content: center;
  transition: background var(--transition-fast), color var(--transition-fast);
  z-index: 10; /* always above grid items and content */
}
.modal-close-btn:hover {
  background: var(--color-hover-bg);
  color: var(--text-main);
}

.modal-body {
  padding: var(--space-xl);
  overflow-y: auto;
  flex: 1;
  /* Custom thin scrollbar */
  scrollbar-width: thin;
  scrollbar-color: var(--color-scrollbar) transparent;
  /* Allow touch scrolling within the modal body itself */
  touch-action: pan-y;
  overscroll-behavior: contain;
}
.modal-body::-webkit-scrollbar { width: 4px; }
.modal-body::-webkit-scrollbar-track { background: transparent; }
.modal-body::-webkit-scrollbar-thumb {
  background: var(--color-scrollbar);
  border-radius: 99px;
}
.modal-body::-webkit-scrollbar-thumb:hover { background: var(--color-scrollbar-hover); }
.modal-footer {
  padding: var(--space-lg) var(--space-xl);
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-md);
}

/* ── Solution modal ────────────────────────────────────── */
.solution-section {
  margin-bottom: var(--space-lg);
}
.solution-section:last-child { margin-bottom: 0; }
.solution-label {
  font-weight: var(--font-weight-semibold);
  font-size: var(--font-size-xs);
  color: var(--primary);
  margin-bottom: var(--space-xs);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.solution-text {
  font-size: var(--font-size-base);
  line-height: var(--line-height-base);
  color: var(--text-main);
  word-wrap: break-word;
}
.solution-text.answer-text {
  font-weight: var(--font-weight-bold);
}
.solution-text.solution-details {
  background: var(--color-subtle-bg);
  padding: var(--space-md);
  border-radius: var(--border-radius-lg);
}
.solution-divider {
  height: 1px;
  background: var(--border-color);
  margin: var(--space-md) 0;
}

/* ── About modal ───────────────────────────────────────── */
.about-header {
  text-align: center;
  margin-bottom: var(--space-lg);
}
.about-header h2 {
  font-size: var(--font-size-2xl);
  font-weight: var(--font-weight-extrabold);
}
.about-header p { color: var(--text-muted); }
.contact-item {
  display: flex;
  align-items: center;
  padding: var(--space-md);
  background: var(--color-subtle-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-lg);
  margin-bottom: var(--space-sm);
}
.contact-icon {
  width: 42px; height: 42px;
  background: var(--color-selected-bg);
  color: var(--primary);
  border-radius: var(--border-radius-md);
  display: flex; align-items: center; justify-content: center;
  font-size: var(--font-size-lg);
  margin-right: var(--space-md);
  flex-shrink: 0;
}
.contact-label {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  font-weight: var(--font-weight-medium);
}
.contact-value {
  font-size: var(--font-size-base);
  color: var(--text-main);
  font-weight: var(--font-weight-semibold);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ══════════════════════════════════════════════════════════
   STUDY MODAL
   ══════════════════════════════════════════════════════════ */

/* Blue header removed — now uses modal-close-btn (✕) from modal shell */

/* Section building blocks */
.sm-section { margin-bottom: var(--space-lg); }
.sm-section:last-child { margin-bottom: 0; }
.sm-label {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-bold);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: var(--space-sm);
}
.sm-divider {
  height: 1px;
  background: var(--border-color);
  margin: var(--space-lg) 0;
}

/* Mode — compact tab strip, scales to any number of modes */
.sm-tabs {
  display: flex;
  gap: var(--space-xs);
  flex-wrap: wrap;
}
.sm-tab {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: var(--space-xs) var(--space-md);
  height: 36px;
  -webkit-tap-highlight-color: transparent;
  border: 1.5px solid var(--border-color);
  border-radius: var(--border-radius-full);
  background: var(--bg-card);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  font-family: var(--font-family);
  color: var(--text-main);
  cursor: pointer;
  transition: all var(--transition-fast);
  user-select: none;
  white-space: nowrap;
}
.sm-tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}
@media (hover: hover) and (pointer: fine) {
  .sm-tab:hover:not(.active) {
    background: var(--color-hover-bg);
    border-color: var(--border-color-hover);
  }
}

/* Dataset selector */
.dataset-selection-section {
  display: none;
  margin-bottom: var(--space-lg);
}
.dataset-selection-section.visible { display: block; }

/* Topics grid */
.sm-topics-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-sm);
}
.sm-select-all {
  height: 28px;
  padding: 0 var(--space-sm);
  border-radius: var(--border-radius-md);
  background: transparent;
  border: 1px solid var(--border-color);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  font-family: var(--font-family);
  cursor: pointer;
  color: var(--text-main);
  transition: all var(--transition-fast);
}
.sm-select-all.all-selected {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.sm-topics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: var(--space-xs);
  padding: var(--space-sm);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-lg);
}
.topic-item {
  padding: var(--space-sm);
  border: 1.5px solid var(--border-color);
  border-radius: var(--border-radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  background: var(--bg-card);
  user-select: none;
  overflow: hidden; /* clip the scrolling text */
}
.topic-item.selected {
  border-color: var(--primary);
  background: var(--color-selected-bg);
}
@media (hover: hover) and (pointer: fine) {
  .topic-item:hover:not(.selected) {
    background: var(--color-subtle-bg);
    border-color: var(--border-color-hover);
  }
}

.topic-name {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  color: var(--text-main);
  white-space: nowrap; /* desktop: single line with marquee on hover */
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* Mobile: allow wrapping so long titles don't get cut off */
@media (max-width: 768px) {
  .topic-name {
    white-space: normal;
    overflow: visible;
    text-overflow: unset;
  }
  /* Disable marquee on mobile — not needed when text wraps */
  .topic-item.overflows .topic-name {
    animation: none;
  }
}
.topic-item.overflows:hover .topic-name {
  overflow: visible;
  text-overflow: clip;
  animation: marquee var(--marquee-duration, 3s) linear infinite;
}
@keyframes marquee {
  0%   { transform: translateX(0); }
  15%  { transform: translateX(0); }
  85%  { transform: translateX(var(--marquee-offset, 0px)); }
  100% { transform: translateX(var(--marquee-offset, 0px)); }
}
.topic-count {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  margin-top: 2px;
}

/* Number of cards */
.sm-count-section { text-align: center; }
.sm-count-row {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  margin-bottom: var(--space-xs);
}
.sm-count-btn {
  width: 36px; height: 36px;
  border-radius: var(--border-radius-md);
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-bold);
  font-family: var(--font-family);
  cursor: pointer;
  transition: background var(--transition-fast);
  display: flex; align-items: center; justify-content: center;
}
.sm-count-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.sm-count-btn:not(:disabled):hover { background: var(--color-hover-bg); }
.sm-count-input {
  width: 72px; height: 36px;
  text-align: center;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-bold);
  font-family: var(--font-family);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-md);
  -moz-appearance: textfield;
}
.sm-count-input::-webkit-outer-spin-button,
.sm-count-input::-webkit-inner-spin-button { -webkit-appearance: none; }
.sm-count-input:disabled { opacity: 0.35; cursor: not-allowed; }
.sm-max-btn {
  height: 36px;
  padding: 0 var(--space-md);
  border-radius: var(--border-radius-md);
  border: 1px solid var(--border-color);
  background: transparent;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  font-family: var(--font-family);
  cursor: pointer;
  color: var(--text-main);
  transition: all var(--transition-fast);
}
.sm-max-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.sm-validation {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  text-align: center;
  margin-top: var(--space-xs);
}
.sm-validation.success { color: var(--text-muted); }

/* Pinned footer */
/* ── Study modal header — pinned top, mirrors footer ─────── */
.sm-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px var(--space-lg);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0; /* never scrolls away */
}
.sm-header-title {
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-bold);
  color: var(--text-main);
  letter-spacing: -0.01em;
}
/* Close button inside header — override absolute positioning */
.sm-header-close {
  position: static;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.sm-footer {
  padding: var(--space-md) var(--space-xl);
  flex-shrink: 0;
}

/* ── Slider (number of flashcards) ─────────────────────── */
.sm-slider-row {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: var(--space-xs);
}

.sm-slider {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border-color);
  border-radius: var(--border-radius-full);
  outline: none;
  cursor: pointer;
  accent-color: var(--primary);
}

.sm-slider:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.sm-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,0.18);
}

.sm-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
}

.sm-slider-badge {
  min-width: 36px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-selected-bg);
  color: var(--primary);
  border-radius: var(--border-radius-md);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-bold);
  flex-shrink: 0;
}

/* sm-count-section centring no longer needed — left-aligned now */
.sm-count-section { text-align: left; }

/* ── Animations ────────────────────────────────────────── */
@keyframes modalFadeIn {
  from { opacity: 0; } to { opacity: 1; }
}
@keyframes modalSlideUp {
  from { transform: translateY(16px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@keyframes modalSlideUpSheet {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

/* ══════════════════════════════════════════════════════════
   MODE-SPECIFIC SETTINGS
   ══════════════════════════════════════════════════════════ */

.mode-settings {
  display: block;
}

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

/* Setting row */
.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-sm) 0;
  gap: var(--space-lg);
  -webkit-tap-highlight-color: transparent;
}

/* setting-info kept for backward compat but optional now */
.setting-info { flex: 1; }

.setting-name {
  flex: 1;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  color: var(--text-main);
}

.setting-desc {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
}

.setting-select {
  min-width: 140px;
  height: 36px;
  padding: 0 var(--space-md);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-md);
  font-size: var(--font-size-sm);
  font-family: var(--font-family);
  background: var(--color-input-bg);
  cursor: pointer;
  color: var(--text-main);
}

.setting-input {
  width: 100px;
  height: 36px;
  padding: 0 var(--space-md);
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius-md);
  font-size: var(--font-size-sm);
  font-family: var(--font-family);
  text-align: center;
  background: var(--color-input-bg);
  color: var(--text-main);
}

/* Toggle switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 26px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--color-toggle-off);
  transition: 0.3s;
  border-radius: 26px;
}

.toggle-slider:before {
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  left: 3px;
  bottom: 3px;
  background-color: var(--bg-card);
  transition: 0.3s;
  border-radius: 50%;
}

.toggle-switch input:checked + .toggle-slider {
  background-color: var(--primary);
}

.toggle-switch input:checked + .toggle-slider:before {
  transform: translateX(24px);
}

/* ── Mobile ────────────────────────────────────────────── */
@media (max-width: 768px) {
  /* Bottom sheet ONLY for study modal */
  #studyModal {
    align-items: flex-end;
  }
  #studyModal .modal {
    width: 100%;
    max-width: 100%;
    height: auto;
    max-height: 88vh;
    border-radius: 20px 20px 0 0;
    margin: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    animation: modalSlideUpSheet 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  }
  /* Drag handle pill — sits above the header with clear gap */
  #studyModal .modal::before {
    content: '';
    display: block;
    width: 40px;
    height: 4px;
    background: var(--border-color);
    border-radius: var(--border-radius-full);
    margin: 10px auto 4px;
    flex-shrink: 0;
  }
  /* Hide X on study modal mobile — swipe to dismiss */
  #studyModal .modal-close-btn {
    display: none;
  }
  /* All other modals stay centered with X button */
  .modal-overlay:not(#studyModal) {
    align-items: center;
  }
  /* Hide scrollbar on modal body for mobile */
  .modal-body {
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .modal-body::-webkit-scrollbar { display: none; }
  /* sm-footer — safe area padding for iOS home indicator */
  .sm-footer {
    padding: var(--space-md) var(--space-lg);
    padding-bottom: max(var(--space-md), env(safe-area-inset-bottom, var(--space-md)));
  }
  .sm-header {
    /* Pill above provides top spacing — keep header compact on mobile */
    padding: 8px var(--space-lg) 10px;
  }
  #studyModal .modal-body {
    padding: var(--space-lg);
  }

  /* Topics section should NOT grow on mobile */
  .sm-section.topics-section {
    flex-shrink: 0;
    display: block;
  }

  .sm-topics-grid {
    grid-template-columns: 1fr;
  }
  /* Compact topic items on mobile — keep card count, just tighter */
  #studyModal .topic-item {
    padding: 8px var(--space-sm);
    overflow: visible; /* allow box to grow with wrapped text */
  }
  #studyModal .topic-name {
    font-size: var(--font-size-sm);
  }
  #studyModal .topic-count {
    font-size: 11px;
    margin-top: 1px;
  }

  /* Number of cards and settings stay natural height */
  .sm-section.sm-count-section,
  .mode-settings {
  display: block;
}

  /* Mode section doesn't grow */
  .sm-section:not(.topics-section):not(.sm-count-section) {
    flex-shrink: 0;
  }

  .topic-item {
    overflow: visible;
    padding: var(--space-sm);
  }
  .topic-count {
    display: block;
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    margin-top: 2px;
  }
  .topic-item.overflows:hover .topic-name {
    overflow: hidden;
    text-overflow: ellipsis;
    animation: none;
  }

  /* Settings mobile layout - keep inline */
  .setting-row {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-md);
    flex-wrap: nowrap;
  }

  .setting-info {
    flex: 1;
    min-width: 0;
  }

  .setting-select,
  .setting-input {
    flex-shrink: 0;
    min-width: 120px;
  }

  .toggle-switch {
    flex-shrink: 0;
  }
}