/* ===== Design Tokens ===== */
:root {
  --primary: #5b6fd6;
  --primary-dark: #4856b8;
  --primary-light: #eef0fb;
  --success: #4caf83;
  --success-light: #e9f7f0;
  --danger: #e0716b;
  --danger-light: #fbeceb;
  --warning: #d9a356;
  --warning-light: #faf2e6;

  --bg: #fafafb;
  --surface: #ffffff;
  --surface-muted: #f3f4f7;
  --border: #e6e7ec;
  --text: #2b2d3a;
  --text-muted: #80828f;

  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 22px;
  --radius-pill: 999px;

  --shadow-sm: 0 1px 3px rgba(30, 32, 50, 0.05);
  --shadow-md: 0 6px 16px rgba(30, 32, 50, 0.06);
  --shadow-lg: 0 12px 28px rgba(30, 32, 50, 0.08);

  --font: 'Cairo', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

[data-theme="dark"] {
  --primary: #8b97e8;
  --primary-dark: #aab3ef;
  --primary-light: rgba(139, 151, 232, 0.14);
  --success: #6fcaa0;
  --success-light: rgba(111, 202, 160, 0.14);
  --danger: #e8918c;
  --danger-light: rgba(232, 145, 140, 0.14);
  --warning: #e0bb80;
  --warning-light: rgba(224, 187, 128, 0.14);

  --bg: #1a1b22;
  --surface: #23242e;
  --surface-muted: #2a2b36;
  --border: #34353f;
  --text: #ebebf0;
  --text-muted: #93939f;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.25);
  --shadow-md: 0 6px 16px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 12px 28px rgba(0, 0, 0, 0.35);
}

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

html {
  scroll-behavior: smooth;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  transition: background 0.25s ease, color 0.25s ease;
}

button, input {
  font-family: var(--font);
}

/* ===== App Shell ===== */
.app-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-header-inner {
  max-width: 720px;
  margin: 0 auto;
  padding: 1.1rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.app-logo {
  font-size: 1.5rem;
}

.app-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
}

.icon-btn {
  background: var(--surface-muted);
  border: none;
  border-radius: var(--radius-pill);
  width: 42px;
  height: 42px;
  font-size: 1.1rem;
  cursor: pointer;
  transition: transform 0.2s ease, background 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
}

.icon-btn:hover {
  background: var(--primary-light);
  transform: scale(1.06);
}

.app-main {
  flex: 1;
  display: flex;
}

/* ===== Screens ===== */
.screen {
  display: none;
  width: 100%;
  padding: 1.75rem 1.25rem 3rem;
  flex: 1;
}

.screen.active {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.screen.active > * {
  width: 100%;
  max-width: 640px;
}

#matchingScreen.active > * {
  max-width: 860px;
}

.fade-in {
  animation: fadeSlideIn 0.35s ease both;
}

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

.hidden {
  display: none !important;
}

/* ===== Card (generic) ===== */
.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 1.75rem;
}

/* ===== Start Screen / Hero ===== */
.hero {
  text-align: center;
  margin-bottom: 1.75rem;
}

.hero-emoji {
  font-size: 2.75rem;
  display: block;
  margin-bottom: 0.75rem;
}

.hero-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 0.4rem;
  color: var(--text);
}

.hero-subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
}

.cta-stack {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  margin-bottom: 1.5rem;
}

.cta-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 1.1rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 0.9rem;
  cursor: pointer;
  text-align: right;
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
  width: 100%;
}

.cta-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--primary);
}

.cta-icon {
  font-size: 1.6rem;
  flex-shrink: 0;
}

.cta-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.cta-text strong {
  font-size: 1.02rem;
  color: var(--text);
}

.cta-text small {
  color: var(--text-muted);
  font-size: 0.82rem;
}

.cta-arrow {
  color: var(--text-muted);
  font-size: 1.3rem;
  flex-shrink: 0;
}

.cta-primary {
  border-color: var(--primary);
  background: var(--primary-light);
}

.cta-secondary {
  border-color: var(--border);
}

.cta-resume {
  border-color: var(--success);
  background: var(--success-light);
}

.settings-card {
  background: var(--surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 1.1rem 1.25rem;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.4rem 0;
}

.settings-row + .settings-row {
  border-top: 1px solid var(--border);
  margin-top: 0.4rem;
  padding-top: 0.75rem;
}

.settings-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.92rem;
  color: var(--text);
}

.settings-icon {
  font-size: 1.05rem;
}

.number-input {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-muted);
  color: var(--text);
  font-size: 0.9rem;
  width: 90px;
  text-align: center;
}

.number-input:focus {
  outline: none;
  border-color: var(--primary);
}

/* ===== Switch ===== */
.switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 28px;
  flex-shrink: 0;
}

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

.switch-track {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: var(--radius-pill);
  transition: background 0.2s ease;
  cursor: pointer;
}

.switch-thumb {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 22px;
  height: 22px;
  background: var(--surface);
  border-radius: 50%;
  box-shadow: var(--shadow-sm);
  transition: transform 0.2s ease;
}

.switch input:checked + .switch-track {
  background: var(--primary);
}

.switch input:checked + .switch-track .switch-thumb {
  transform: translateX(-20px);
}

/* ===== Section Head (chapter screen) ===== */
.section-head {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.back-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  width: 38px;
  height: 38px;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease;
}

.back-btn:hover {
  background: var(--surface-muted);
}

.section-title {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--text);
}

/* ===== Chapters Grid ===== */
.chapters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
}

.chapter-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 1.25rem;
  cursor: pointer;
  text-align: right;
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}

.chapter-btn:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-md);
  border-color: var(--primary);
}

.chapter-btn h3 {
  font-size: 1.02rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
  color: var(--text);
}

.chapter-stats {
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* ===== Quiz Screen ===== */
.quiz-wrap {
  width: 100%;
}

.quiz-top {
  margin-bottom: 1.5rem;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.question-counter {
  font-weight: 600;
  color: var(--text);
}

.timer {
  font-weight: 700;
  color: var(--primary);
  font-size: 1.05rem;
}

.timer.low-time {
  color: var(--danger);
  animation: softPulse 1.2s ease-in-out infinite;
}

@keyframes softPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

.progress-bar-track {
  height: 7px;
  background: var(--surface-muted);
  border-radius: var(--radius-pill);
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: var(--primary);
  border-radius: var(--radius-pill);
  transition: width 0.3s ease;
}

.question-card {
  margin-bottom: 1.5rem;
  overflow: hidden;
}

.question-card-inner {
  animation: fadeSlideIn 0.3s ease both;
}

.chapter-badge {
  display: inline-block;
  background: var(--primary-light);
  color: var(--primary-dark);
  padding: 0.35rem 0.85rem;
  border-radius: var(--radius-pill);
  font-size: 0.78rem;
  font-weight: 700;
  margin-bottom: 1rem;
}

.question-text {
  font-size: 1.2rem;
  line-height: 1.7;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 1.25rem;
}

.question-image-wrap {
  margin-bottom: 1.25rem;
  text-align: center;
}

.question-image {
  max-width: 100%;
  max-height: 260px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}

/* ===== Options ===== */
.options-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.option-btn {
  text-align: right;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 0.98rem;
  font-weight: 500;
  transition: border-color 0.18s ease, background 0.18s ease, transform 0.18s ease;
}

.option-btn:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.option-btn.selected {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary-dark);
  font-weight: 700;
}

.option-btn.correct {
  border-color: var(--success);
  background: var(--success-light);
  color: var(--success);
  font-weight: 700;
}

.option-btn.incorrect {
  border-color: var(--danger);
  background: var(--danger-light);
  color: var(--danger);
  font-weight: 700;
}

.tf-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.tf-options .option-btn {
  text-align: center;
  font-weight: 700;
  padding: 18px 16px;
}

/* ===== Matching Screen ===== */
.matching-hint {
  background: var(--primary-light);
  color: var(--primary-dark);
  font-size: 0.9rem;
  font-weight: 600;
  margin: 0 0 1.5rem 0;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  border-right: 3px solid var(--primary);
  text-align: right;
}

.matching-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  direction: ltr;
  margin-bottom: 1.5rem;
}

.matching-container {
  display: flex;
  flex-direction: column;
  gap: 2rem;
  margin-bottom: 1.5rem;
}

.matching-bank-section,
.matching-terms-section {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.matching-label {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text);
  text-align: right;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.matching-label::before {
  display: inline-block;
  width: 6px;
  height: 6px;
  background: var(--primary);
  border-radius: 50%;
  flex-shrink: 0;
}

.matching-col {
  direction: rtl;
  text-align: right;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.matching-row {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem;
  background: var(--surface-muted);
  border-radius: var(--radius-sm);
  border-right: 2px solid var(--primary-light);
}

.matching-term {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.5;
}

.matching-slot {
  min-height: 48px;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem;
  font-size: 0.9rem;
  color: var(--text-muted);
  cursor: pointer;
  text-align: center;
  transition: border-color 0.18s ease, background 0.18s ease;
  font-weight: 500;
  line-height: 1.4;
}

.matching-slot:hover:not(.filled) {
  border-color: var(--primary);
  background: var(--primary-light);
}

.matching-slot.filled {
  border-style: solid;
  border-color: var(--success);
  background: var(--success-light);
  color: var(--success);
  font-weight: 700;
}

.matching-slot.filled::before {
  content: '✓ ';
  margin-right: 0.25rem;
}

.matching-bank {
  flex-direction: row;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 0.5rem;
}

.matching-word {
  background: var(--surface-muted);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-pill);
  padding: 0.65rem 0.95rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  cursor: grab;
  user-select: none;
  transition: border-color 0.18s ease, background 0.18s ease, transform 0.18s ease;
}

.matching-word:hover:not(.used) {
  border-color: var(--primary);
  background: var(--primary-light);
  transform: translateY(-2px);
}

.matching-word.active {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary-dark);
  transform: scale(1.08);
  box-shadow: var(--shadow-md);
}

.matching-word.used {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
  text-decoration: line-through;
}

.matching-submit {
  grid-column: 1 / -1;
  width: 100%;
}

@media (max-width: 700px) {
  .matching-container {
    gap: 1.5rem;
  }

  .matching-terms-section {
    order: 2;
  }

  .matching-bank-section {
    order: 3;
  }

  .matching-label {
    order: -1;
    margin-bottom: 0.25rem;
  }

  .matching-bank {
    gap: 0.5rem;
  }

  .matching-word {
    padding: 0.6rem 0.85rem;
    font-size: 0.82rem;
  }

  .matching-slot {
    min-height: 44px;
    padding: 0.6rem;
    font-size: 0.85rem;
  }

  .matching-row {
    padding: 0.6rem;
  }

  .matching-term {
    font-size: 0.9rem;
  }

  .matching-col {
    padding: 0.75rem;
  }
}

/* ===== Quiz Nav ===== */
.quiz-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
}

@media (max-width: 560px) {
  .quiz-nav {
    grid-template-columns: 1fr;
  }
}

/* ===== Buttons ===== */
.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.18s ease, box-shadow 0.18s ease, opacity 0.18s ease, background 0.18s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
}

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

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-dark);
}

.btn-ghost {
  background: var(--surface-muted);
  color: var(--text);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--border);
}

.btn-danger-ghost {
  background: var(--danger-light);
  color: var(--danger);
}

.btn-danger-ghost:hover:not(:disabled) {
  filter: brightness(0.97);
}

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

.btn-text:hover:not(:disabled) {
  color: var(--text);
}

/* ===== Result Screen ===== */
.result-card {
  text-align: center;
}

.result-title {
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 1.5rem;
  color: var(--text);
}

.result-ring {
  width: 160px;
  height: 160px;
  border-radius: 50%;
  margin: 0 auto 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: conic-gradient(var(--primary) calc(var(--pct, 0) * 1%), var(--surface-muted) 0);
  transition: background 0.4s ease;
}

.result-ring-inner {
  width: 124px;
  height: 124px;
  border-radius: 50%;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
}

.result-percent {
  font-size: 1.9rem;
  font-weight: 800;
  color: var(--primary);
}

.result-fraction {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.2rem;
}

.score-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1.75rem;
}

.stat {
  background: var(--surface-muted);
  padding: 1.1rem;
  border-radius: var(--radius-md);
}

.stat-num {
  display: block;
  font-size: 1.6rem;
  font-weight: 800;
  margin-bottom: 0.3rem;
}

.stat-correct .stat-num {
  color: var(--success);
}

.stat-wrong .stat-num {
  color: var(--danger);
}

.stat-label {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.matching-summary {
  background: var(--surface-muted);
  border-radius: var(--radius-md);
  padding: 0.75rem 1rem;
  margin-top: 1rem;
  text-align: center;
  font-weight: 600;
  color: var(--text);
}

.result-actions {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

/* ===== Review ===== */
.review-list {
  text-align: right;
}

.review-item {
  background: var(--surface);
  padding: 1.1rem 1.25rem;
  border-radius: var(--radius-sm);
  margin-bottom: 0.85rem;
  border-right: 3px solid var(--primary);
  text-align: right;
}

.review-q {
  font-weight: 700;
  margin-bottom: 0.6rem;
  color: var(--text);
}

.review-image {
  max-width: 100%;
  max-height: 200px;
  border-radius: var(--radius-sm);
  margin: 0.75rem 0;
}

.review-answer-row {
  margin: 0.4rem 0;
  font-size: 0.9rem;
}

.label-correct {
  color: var(--success);
  font-weight: 700;
}

.label-wrong {
  color: var(--danger);
  font-weight: 700;
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .app-header-inner {
    padding: 0.9rem 1rem;
  }

  .screen {
    padding: 1.25rem 1rem 2.5rem;
  }

  .card {
    padding: 1.25rem;
  }

  .question-text {
    font-size: 1.05rem;
  }

  .result-ring {
    width: 130px;
    height: 130px;
  }

  .result-ring-inner {
    width: 100px;
    height: 100px;
  }

  .result-percent {
    font-size: 1.5rem;
  }

  .tf-options {
    grid-template-columns: 1fr;
  }
}
