/* ── Variables ───────────────────────────────────────────────────────────── */
:root {
  --primary:      #6366f1;
  --primary-dark: #4f46e5;
  --primary-light:#ede9fe;
  --success:      #22c55e;
  --success-light:#dcfce7;
  --error:        #ef4444;
  --error-light:  #fee2e2;
  --bg:           #f1f5f9;
  --card-bg:      #ffffff;
  --border:       #e2e8f0;
  --text:         #1e293b;
  --muted:        #64748b;
  --radius:       12px;
  --shadow:       0 1px 4px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.06);
  --shadow-lg:    0 4px 24px rgba(0,0,0,.12);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

/* ── Screens ─────────────────────────────────────────────────────────────── */
.screen { display: none; min-height: 100vh; padding: 32px 16px 64px; }
.screen.active { display: block; }

/* ── Containers ──────────────────────────────────────────────────────────── */
.container       { max-width: 520px;  margin: 0 auto; }
.container.wide  { max-width: 1100px; margin: 0 auto; }
.container.narrow{ max-width: 480px;  margin: 0 auto; }

/* ── Logo / hero ─────────────────────────────────────────────────────────── */
.logo { text-align: center; margin-bottom: 32px; }
.logo-icon { font-size: 48px; display: block; margin-bottom: 8px; }
.logo h1 { font-size: 2rem; font-weight: 700; color: var(--primary-dark); }
.tagline { color: var(--muted); margin-top: 4px; }

/* ── Card ────────────────────────────────────────────────────────────────── */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 8px;
  border: none;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform .1s, box-shadow .1s, background .15s;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); box-shadow: 0 2px 8px rgba(99,102,241,.4); }

.btn-secondary {
  background: var(--card-bg);
  color: var(--text);
  border: 1.5px solid var(--border);
}
.btn-secondary:hover { background: var(--bg); }

.btn-full { width: 100%; justify-content: center; }
.btn-large { padding: 14px 24px; font-size: 1rem; }

.btn-icon {
  background: none;
  border: 1.5px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 1.1rem;
  cursor: pointer;
  transition: background .15s;
}
.btn-icon:hover { background: var(--bg); }

.btn-danger {
  background: transparent;
  color: var(--error);
  border: 1.5px solid #fecaca;
  padding: 6px 12px;
  font-size: 0.82rem;
}
.btn-danger:hover { background: var(--error-light); }

.btn-edit {
  background: transparent;
  color: var(--primary);
  border: 1.5px solid #c7d2fe;
  padding: 6px 12px;
  font-size: 0.82rem;
}
.btn-edit:hover { background: var(--primary-light); }

/* ── Forms ───────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 20px; }
.form-group:last-of-type { margin-bottom: 24px; }

label {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 6px;
  color: var(--text);
}

input[type="text"],
input[type="password"],
textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}
input:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99,102,241,.15);
}

small { display: block; color: var(--muted); font-size: 0.8rem; margin-top: 5px; }

.range-input { width: 100%; accent-color: var(--primary); margin-top: 4px; cursor: pointer; }
.range-labels { display: flex; justify-content: space-between; font-size: 0.78rem; color: var(--muted); margin-top: 2px; }

/* ── Review screen header ────────────────────────────────────────────────── */
.screen-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 28px;
  gap: 16px;
  flex-wrap: wrap;
}
.screen-header h2 { font-size: 1.5rem; font-weight: 700; }
.screen-header-right { display: flex; gap: 10px; align-items: center; }

/* ── Cards grid ──────────────────────────────────────────────────────────── */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.review-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: box-shadow .2s;
}
.review-card:hover { box-shadow: var(--shadow-lg); }

.review-card-image {
  width: 100%;
  height: 160px;
  object-fit: cover;
  display: block;
  background: var(--bg);
}

.img-loading {
  width: 100%;
  height: 160px;
  background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.85rem;
}

@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.review-card-body { padding: 14px 16px; flex: 1; }
.review-card-q { font-weight: 600; font-size: 0.9rem; margin-bottom: 8px; line-height: 1.4; }
.review-card-a { font-size: 0.85rem; color: var(--muted); line-height: 1.5; }
.review-card-divider { border: none; border-top: 1px solid var(--border); margin: 10px 0; }

.review-card-actions { padding: 10px 16px 14px; display: flex; gap: 8px; }

.add-card-row { text-align: center; margin-top: 8px; }

/* ── Study screen ────────────────────────────────────────────────────────── */
.study-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 28px;
}

.progress-area { flex: 1; }

.progress-bar-wrap {
  height: 8px;
  background: var(--border);
  border-radius: 100px;
  overflow: hidden;
  margin-bottom: 6px;
}
.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 100px;
  transition: width .5s ease;
}

/* ── Study mode toggle ───────────────────────────────────────────────────── */
.study-mode-toggle {
  display: flex;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}
.study-mode-btn {
  background: none;
  border: none;
  padding: 6px 14px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s, color .15s;
}
.study-mode-btn.active { background: var(--primary); color: #fff; }

.btn-reverse {
  background: none;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
  flex-shrink: 0;
  white-space: nowrap;
}
.btn-reverse:hover { background: var(--bg); color: var(--text); }
.btn-reverse.active {
  background: var(--primary-light);
  color: var(--primary-dark);
  border-color: var(--primary);
}

/* ── 3D card flip ────────────────────────────────────────────────────────── */
.study-card-scene {
  perspective: 1200px;
  margin-bottom: 24px;
}

.study-card {
  position: relative;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(.4,0,.2,1);
}

.study-card.flipped {
  transform: rotateY(180deg);
  -webkit-transform: rotateY(180deg);
}

.study-card-face {
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 16px;
  background: var(--card-bg);
  box-shadow: var(--shadow-lg);
}

.study-card-front { overflow: hidden; }

.study-card-back {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  transform: rotateY(180deg);
  -webkit-transform: rotateY(180deg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 32px;
  text-align: center;
  gap: 12px;
}

.result-icon {
  font-size: 44px;
  line-height: 1;
}
.result-icon.correct { color: var(--success); }
.result-icon.wrong   { color: var(--error); }

.back-label {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
}

.study-answer-reveal {
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.4;
  color: var(--text);
  word-break: break-word;
}

/* ── Card front content ──────────────────────────────────────────────────── */
.study-card-image-wrap {
  width: 100%;
  height: 240px;
  overflow: hidden;
  position: relative;
  background: var(--bg);
}

.study-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.img-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  color: var(--border);
  background: var(--bg);
}

.study-card-body { padding: 24px; }
.study-question {
  font-size: 1.2rem;
  font-weight: 600;
  line-height: 1.5;
  text-align: center;
  margin-bottom: 16px;
}

.mastery-dots {
  display: flex;
  justify-content: center;
  gap: 8px;
}
.mastery-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--border);
  transition: background .3s, border-color .3s;
}
.mastery-dot.filled {
  background: var(--success);
  border-color: var(--success);
}

/* ── Answer area ─────────────────────────────────────────────────────────── */
.answer-area { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }

.answer-row { display: flex; gap: 8px; }
.answer-row .answer-input { flex: 1; }

.answer-input {
  width: 100%;
  padding: 18px 16px;
  font-size: 1.2rem;
  border-radius: 10px;
  border: 2px solid var(--border);
  outline: none;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
.answer-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(99,102,241,.15); }

.btn-mic {
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 0 18px;
  font-size: 1.3rem;
  cursor: pointer;
  transition: background .15s, border-color .15s;
  flex-shrink: 0;
  outline: none;
}
.btn-mic:hover { background: var(--border); }
.btn-mic.listening {
  background: var(--error-light);
  border-color: var(--error);
  animation: micpulse .8s ease-in-out infinite;
}
@keyframes micpulse {
  0%, 100% { transform: scale(1); }
  50%       { transform: scale(1.08); }
}

/* ── Browse area ─────────────────────────────────────────────────────────── */
.browse-area { margin-bottom: 16px; }

/* ── Post-flip area ──────────────────────────────────────────────────────── */
.post-flip-area {
  display: flex;
  gap: 10px;
  align-items: center;
}
.btn-outline-danger {
  background: transparent;
  color: var(--error);
  border: 1.5px solid #fecaca;
  padding: 12px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
  transition: background .15s;
  flex-shrink: 0;
}
.btn-outline-danger:hover { background: var(--error-light); }
.post-flip-next { flex: 1; justify-content: center; }

/* ── Complete screen ─────────────────────────────────────────────────────── */
.center-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 70vh;
  text-align: center;
  gap: 16px;
}

.complete-icon { font-size: 72px; }
.complete-icon + h2 { font-size: 1.8rem; font-weight: 700; }

.complete-actions { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; margin-top: 8px; }

/* ── Modals ──────────────────────────────────────────────────────────────── */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal.hidden { display: none; }

.modal-overlay { position: absolute; inset: 0; background: rgba(15,23,42,.4); backdrop-filter: blur(2px); }

.modal-box {
  position: relative;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 28px;
  width: 100%;
  max-width: 480px;
  z-index: 1;
}
.modal-box h3 { font-size: 1.15rem; font-weight: 700; margin-bottom: 20px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 8px; }

/* ── Spinner ─────────────────────────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Utility ─────────────────────────────────────────────────────────────── */
.hidden  { display: none !important; }
.muted   { color: var(--muted); }
.small   { font-size: 0.82rem; }

.error-msg {
  margin-top: 12px;
  background: var(--error-light);
  color: #b91c1c;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.9rem;
  line-height: 1.4;
}

/* ── Landing page ────────────────────────────────────────────────────────── */
.landing {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
  gap: 48px;
}

.landing-hero { text-align: center; }
.landing-hero .logo-icon { font-size: 56px; display: block; margin-bottom: 12px; }
.landing-hero h1 { font-size: 2.4rem; font-weight: 800; color: var(--primary-dark); }
.landing-hero .tagline { color: var(--muted); margin-top: 6px; }

.landing-choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  width: 100%;
  max-width: 560px;
}

.landing-card {
  background: var(--card-bg);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 40px 24px;
  text-align: center;
  text-decoration: none;
  color: var(--text);
  transition: transform .15s, box-shadow .15s;
  display: block;
}
.landing-card:hover { transform: translateY(-4px); box-shadow: var(--shadow-lg); }
.landing-card-icon { font-size: 48px; display: block; margin-bottom: 16px; }
.landing-card h2 { font-size: 1.4rem; font-weight: 700; margin-bottom: 8px; }
.landing-card p { color: var(--muted); font-size: 0.9rem; }

@media (max-width: 480px) {
  .landing-choices { grid-template-columns: 1fr; }
}

/* ── Mode tabs ───────────────────────────────────────────────────────────── */
.mode-tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin: 0 -28px 20px;   /* bleed to card edges */
  padding: 0 28px;
}

.mode-tab {
  background: none;
  border: none;
  padding: 10px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  transition: color .15s, border-color .15s;
}
.mode-tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.mode-tab:hover:not(.active) { color: var(--text); }

/* ── Upload zone ─────────────────────────────────────────────────────────── */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: 10px;
  padding: 32px 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  margin-bottom: 16px;
}
.upload-zone:hover,
.upload-zone.drag-over {
  border-color: var(--primary);
  background: var(--primary-light);
}
.upload-zone-icon  { font-size: 36px; display: block; margin-bottom: 8px; }
.upload-zone-label { font-weight: 600; margin-bottom: 4px; }

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
  padding: 0;
  font-size: inherit;
  text-decoration: underline;
}
.link-btn:hover { color: var(--primary-dark); }

/* ── Image previews ──────────────────────────────────────────────────────── */
.image-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}

.image-preview-item {
  position: relative;
  width: 80px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.image-preview-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.image-preview-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  background: rgba(0,0,0,.55);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.image-preview-remove:hover { background: var(--error); }

/* ── Student deck select ─────────────────────────────────────────────────── */
.deck-select-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 20px;
  margin-top: 4px;
}

.deck-select-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transition: transform .15s, box-shadow .15s;
}
.deck-select-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); }

.deck-select-icon  { font-size: 40px; }
.deck-select-topic { font-size: 1.05rem; font-weight: 700; }
.deck-select-meta  { color: var(--muted); font-size: 0.82rem; margin-bottom: 8px; }

.no-decks {
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
}
.no-decks-icon { font-size: 56px; margin-bottom: 16px; }
.no-decks p { margin-bottom: 6px; }

.loading-decks { color: var(--muted); padding: 40px 0; text-align: center; }

.home-link-row { text-align: center; margin-top: 20px; }
.link-muted { color: var(--muted); font-size: 0.9rem; text-decoration: none; }
.link-muted:hover { color: var(--text); }

/* ── Saved decks ─────────────────────────────────────────────────────────── */
.saved-decks-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  margin-bottom: 10px;
}

.saved-deck-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card-bg);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  margin-bottom: 8px;
  gap: 12px;
}

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

.saved-deck-topic {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.saved-deck-meta {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 2px;
  display: block;
}

.saved-deck-actions { display: flex; gap: 6px; flex-shrink: 0; }

.merge-check-label {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}
.merge-check {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  cursor: pointer;
}

.btn-sm { padding: 6px 12px; font-size: 0.82rem; }

.section-divider {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin: 20px 0 16px;
  position: relative;
}
.section-divider::before,
.section-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 38%;
  height: 1px;
  background: var(--border);
}
.section-divider::before { left: 0; }
.section-divider::after  { right: 0; }

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .screen-header { flex-direction: column; align-items: flex-start; }
  .screen-header-right { width: 100%; justify-content: flex-end; }
  .cards-grid { grid-template-columns: 1fr; }
  .study-header { flex-wrap: wrap; }
  .study-mode-toggle { margin-left: auto; }
}
