:root {
  --page-bg: #fbfaf4;
  --surface: #f4f3eb;
  --panel-border: rgba(12, 95, 56, 0.16);
  --panel-shadow: 0 10px 26px rgba(35, 53, 38, 0.09);
  --text-main: #193528;
  --text-soft: #686960;
  --brand: #0c5f38;
  --brand-dark: #073d27;
  --line: #d8ddc9;
  --success-bg: #dff3e5;
  --success-text: #075d2a;
  --danger-bg: #fbe2e6;
  --danger-text: #8f1024;
}

* { box-sizing: border-box; }
button, input, a { font: inherit; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Tajawal", "Segoe UI", Tahoma, Arial, sans-serif;
  background: linear-gradient(180deg, #f4f2e8 0%, var(--page-bg) 100%);
  color: var(--text-main);
}

/* Identité visuelle alignée sur la page d'accueil */
body { background: #f2f1e9; }
.page-shell { background: var(--page-bg); }
.hero { border: 1px solid var(--line); background: rgba(255, 254, 250, 0.96); color: var(--brand-dark); border-radius: 20px; }
.hero::before { content: ""; position: absolute; inset: 0 0 auto; height: 4px; background: #e8aa35; }
.hero h1 { color: var(--brand-dark); }
.hero-logo { background: #fff; border: 1px solid var(--line); }
.exam-panel { background: rgba(255, 254, 250, 0.96); border-color: var(--line); box-shadow: var(--panel-shadow); }
.mode-card { border-color: var(--line); border-radius: 20px; background: #fffefa; }
.mode-card:hover, .mode-card:focus-visible { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(12, 95, 56, 0.12); }
.primary-button { background: var(--brand); border-color: var(--brand); box-shadow: 0 10px 22px rgba(12, 95, 56, 0.18); }
.secondary-button { background: #fffefa; border-color: var(--line); }
.image-option { border-color: var(--line); border-radius: 20px; }
.image-option.selected { border-color: #c9962f; background: #fff6d8; }
.image-option.correct { border-color: var(--brand); background: #dff0e3; }
.image-option.incorrect { border-color: #bd3847; background: #f8dfe3; }
.result-panel.result-green { background: #e6f1e5; border-color: #a8cba8; }
.result-panel.result-yellow { background: #fff6d8; border-color: #e5cc70; }
.result-panel.result-red { background: #f8e1e4; border-color: #dfa4ac; }
.bottom-nav { padding-inline: 0.6rem; }
.bottom-nav-inner { border: 1px solid rgba(28, 58, 40, 0.08); border-radius: 28px 28px 18px 18px; background: rgba(255, 255, 255, 0.97); box-shadow: 0 -6px 20px rgba(35, 53, 38, 0.12); }
.bottom-nav-link { position: relative; color: #414740; }
.bottom-nav-link.active { background: transparent; color: var(--brand); }
.bottom-nav-link.active::before { content: ""; position: absolute; top: -0.45rem; width: 26px; height: 4px; border-radius: 4px; background: var(--brand); }
.bottom-nav-icon svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.page-shell {
  width: 100%;
  max-width: 460px;
  min-height: 100vh;
  margin: 0 auto;
  padding: 0.8rem 0.9rem 5.6rem;
  background: #fff;
}

.hero, .exam-panel { border-radius: 18px; box-shadow: var(--panel-shadow); }
.hero { position: relative; padding: 0.72rem 0.85rem; background: linear-gradient(135deg, rgba(0, 105, 62, 0.97), rgba(14, 128, 81, 0.93)); color: #f7fbfc; }
.hero h1 { margin: 0; font-family: "Amiri", serif; font-size: 1.55rem; }
.hero-logo { position: absolute; left: 0.8rem; top: 50%; width: 38px; height: 38px; transform: translateY(-50%); border-radius: 50%; object-fit: cover; }

.exam-panel { margin-top: 0.65rem; padding: 1rem; border: 1px solid var(--panel-border); background: rgba(255, 255, 255, 0.94); }
.exam-panel h2 { margin: 0.25rem 0 0; color: var(--brand-dark); font-size: 1.18rem; }
.section-label { margin: 0; color: var(--text-soft); font-size: 0.8rem; font-weight: 700; }

.mode-grid { display: grid; gap: 0.7rem; margin-top: 1rem; }
.mode-card { display: grid; grid-template-columns: auto 1fr; gap: 0.2rem 0.7rem; width: 100%; padding: 0.9rem; text-align: right; border: 1px solid var(--line); border-radius: 16px; background: #fff; color: var(--text-main); cursor: pointer; }
.mode-card:hover, .mode-card:focus-visible { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(0, 105, 62, 0.12); }
.mode-card strong { color: var(--brand-dark); }
.mode-card span:last-child { grid-column: 2; color: var(--text-soft); font-size: 0.8rem; line-height: 1.55; }
.mode-icon { grid-row: 1 / span 2; align-self: center; font-size: 1.4rem; color: var(--brand); }

#exam-setup { padding: 1.25rem; }
#exam-setup .mode-grid { gap: 1rem; margin-top: 1.2rem; }
#exam-setup .mode-card { min-height: 120px; padding: 1.2rem; gap: 0.35rem 0.9rem; }
#exam-setup .mode-card strong { font-size: 1.06rem; }
#exam-setup .mode-card span:last-child { font-size: 0.9rem; }
#exam-setup .mode-icon { font-size: 1.8rem; }

.question-header { display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; }
.question-counter { margin: 0; padding: 0.35rem 0.7rem; border-radius: 999px; background: #eef7f1; color: var(--brand-dark); font-size: 0.82rem; font-weight: 800; }
.question-name { margin: 0.85rem 0 0; color: var(--brand-dark); font-size: 1.15rem; line-height: 1.5; }
.option-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.65rem; margin-top: 0.85rem; }
.image-option { position: relative; display: grid; place-items: center; min-height: 145px; padding: 0.6rem; border: 2px solid var(--line); border-radius: 17px; background: #fff; cursor: pointer; }
.image-option img { display: block; max-width: 100%; max-height: 115px; object-fit: contain; }
.image-option.selected { border-color: #4b6f98; background: #e8f0fb; }
.image-option.correct { border-color: #167c3d; background: #ccebd6; }
.image-option.incorrect { border-color: #be2d43; background: #f6c5cc; }
.image-option:disabled { cursor: default; }

.free-image-shell { display: grid; place-items: center; min-height: 230px; margin-top: 0.85rem; padding: 1rem; border: 1px solid var(--line); border-radius: 18px; background: #fff; }
.free-image-shell img { display: block; max-width: 100%; max-height: 210px; object-fit: contain; }
.answer-field { display: grid; gap: 0.35rem; margin-top: 0.8rem; color: var(--text-soft); font-size: 0.82rem; font-weight: 700; }
.answer-field input { width: 100%; min-height: 44px; padding: 0.65rem 0.75rem; border: 1px solid var(--line); border-radius: 14px; direction: rtl; }

.answer-feedback { margin-top: 0.8rem; padding: 0.75rem; border-radius: 14px; background: #eef7f1; color: var(--brand-dark); font-weight: 700; line-height: 1.6; }
.exam-actions, .result-actions { display: grid; gap: 0.55rem; margin-top: 0.85rem; }
.primary-button, .secondary-button { display: grid; place-items: center; width: 100%; min-height: 44px; padding: 0.7rem 0.9rem; border-radius: 14px; text-decoration: none; cursor: pointer; }
.primary-button { border: 1px solid var(--brand); background: var(--brand); color: #fff; font-weight: 800; }
.primary-button:disabled { opacity: 0.5; cursor: not-allowed; }
.secondary-button { border: 1px solid var(--line); background: var(--surface); color: var(--brand); font-weight: 800; }
.self-evaluation { display: grid; grid-template-columns: 1fr 1fr; gap: 0.55rem; }
.evaluation-good { background: var(--success-bg); color: var(--success-text); border-color: #9bdbaf; }
.evaluation-bad { background: var(--danger-bg); color: var(--danger-text); border-color: #efadb7; }
.status-message { margin: 0.8rem 0 0; padding: 0.7rem; border-radius: 14px; font-size: 0.82rem; font-weight: 700; }
.status-message.error { background: var(--danger-bg); color: var(--danger-text); }
.result-panel { text-align: center; border-width: 2px; }
.result-panel.result-green { background: var(--success-bg); border-color: #9bd4aa; }
.result-panel.result-yellow { background: #fff6d8; border-color: #e5cc70; }
.result-panel.result-red { background: var(--danger-bg); border-color: #e4a3ad; }
.result-score { margin: 0.6rem 0 0; font-size: 1.05rem; font-weight: 800; }
.result-rule { margin: 0.75rem 0 0; padding: 0.65rem; border-radius: 14px; background: rgba(255, 255, 255, 0.72); color: var(--text-soft); font-size: 0.8rem; font-weight: 700; line-height: 1.6; }
.result-summary-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.55rem; margin-top: 0.8rem; }
.result-summary-grid div { display: grid; gap: 0.2rem; padding: 0.65rem; border: 1px solid rgba(0, 105, 62, 0.12); border-radius: 14px; background: rgba(255, 255, 255, 0.72); }
.result-summary-grid span { color: var(--text-soft); font-size: 0.76rem; font-weight: 700; }
.result-summary-grid strong { color: var(--brand-dark); font-size: 1rem; }

.review-header { display: grid; gap: 0.75rem; }
.review-toggle { display: flex; align-items: center; gap: 0.45rem; width: fit-content; color: var(--text-soft); font-size: 0.8rem; font-weight: 700; }
.review-toggle input { width: 1.1rem; height: 1.1rem; accent-color: var(--brand); }
.review-list { display: grid; gap: 0.7rem; margin: 0.85rem 0; }
.review-card { padding: 0.75rem; border: 1px solid var(--line); border-inline-start-width: 4px; border-radius: 16px; background: #fff; text-align: right; }
.review-card.correct { border-inline-start-color: #258b4a; }
.review-card.incorrect { border-inline-start-color: #c23d51; }
.review-card-heading { display: flex; justify-content: space-between; gap: 0.5rem; color: var(--brand-dark); font-size: 0.8rem; }
.review-card.incorrect .review-card-heading span { color: #a32136; }
.review-card.correct .review-card-heading span { color: #147038; }
.review-question { margin: 0.55rem 0 0; font-weight: 800; line-height: 1.5; }
.review-image-shell { display: grid; place-items: center; min-height: 150px; margin-top: 0.6rem; padding: 0.65rem; border-radius: 14px; background: var(--surface); }
.review-image-shell img { display: block; max-width: 100%; max-height: 130px; object-fit: contain; }
.review-details { display: grid; gap: 0.45rem; margin: 0.65rem 0 0; }
.review-details div { display: grid; gap: 0.1rem; padding-top: 0.45rem; border-top: 1px solid var(--line); }
.review-details dt { color: var(--text-soft); font-size: 0.72rem; font-weight: 700; }
.review-details dd { margin: 0; font-size: 0.82rem; font-weight: 700; line-height: 1.5; }
.review-empty { margin: 0; padding: 0.8rem; border-radius: 14px; background: var(--success-bg); color: var(--success-text); text-align: center; font-weight: 700; }

.result-effect-neutral { animation: result-neutral 650ms ease both; }
.result-effect-failure { animation: result-failure 520ms ease both; }

@keyframes result-neutral {
  0% { opacity: 0.72; transform: translateY(5px); }
  100% { opacity: 1; transform: translateY(0); }
}

@keyframes result-failure {
  0%, 100% { transform: translateX(0); }
  35% { transform: translateX(3px); }
  70% { transform: translateX(-3px); }
}

.bottom-nav { position: fixed; left: 50%; bottom: 0; z-index: 20; width: min(460px, 100vw); padding: 0 0.9rem calc(0.65rem + env(safe-area-inset-bottom)); transform: translateX(-50%); }
.bottom-nav-inner { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0.35rem; padding: 0.45rem; border-top: 1px solid var(--line); border-radius: 18px 18px 0 0; background: rgba(255, 255, 255, 0.98); box-shadow: 0 -10px 24px rgba(20, 47, 35, 0.08); }
.bottom-nav-link { display: grid; justify-items: center; gap: 0.12rem; min-height: 44px; padding: 0.35rem 0.2rem; border-radius: 14px; color: var(--text-soft); text-decoration: none; font-size: 0.72rem; font-weight: 700; }
.bottom-nav-link.active { background: #eaf4ed; color: var(--brand); }
.bottom-nav-icon { font-size: 1rem; line-height: 1; }

@media (min-width: 760px) {
  body { padding-inline: 1rem; }
  .page-shell { min-height: calc(100vh - 2rem); margin-block: 1rem; border-radius: 28px; }
}

@media (max-width: 390px) {
  .page-shell, .bottom-nav { padding-inline: 0.75rem; }
  .option-grid { gap: 0.5rem; }
  .image-option { min-height: 130px; }
}

@media (prefers-reduced-motion: reduce) {
  .result-effect-neutral,
  .result-effect-failure {
    animation: none;
  }
}

/* Couche finale d'harmonisation */
.page-shell { background: var(--page-bg); }
.hero { border: 1px solid var(--line); background: rgba(255, 254, 250, 0.96); color: var(--brand-dark); border-radius: 20px; box-shadow: var(--panel-shadow); }
.hero h1 { color: var(--brand-dark); }
.exam-panel { background: rgba(255, 254, 250, 0.96); border-color: var(--line); box-shadow: var(--panel-shadow); }
.answer-field input { background: #fffefa; border-color: var(--line); }
.bottom-nav-inner { border: 1px solid rgba(28, 58, 40, 0.08); border-radius: 28px 28px 18px 18px; background: rgba(255, 255, 255, 0.97); box-shadow: 0 -6px 20px rgba(35, 53, 38, 0.12); }
.bottom-nav-link.active { background: transparent; color: var(--brand); }
