:root {
  --red: #b71c1c;
  --red-dark: #7f0000;
  --orange: #fb8c00;
  --green: #43a047;
  --gray-900: #212121;
  --gray-700: #424242;
  --gray-500: #757575;
  --gray-300: #e0e0e0;
  --gray-100: #f5f5f5;
  --bg: #fafafa;
  --card-bg: #ffffff;
  --radius: 14px;
  --font-sans: "Inter", "Segoe UI", Roboto, Arial, sans-serif;
  --shadow: 0 2px 10px rgba(0,0,0,0.08);
  --shadow-lg: 0 6px 24px rgba(0,0,0,0.14);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--gray-900);
  font-family: var(--font-sans);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2, h3 { font-family: var(--font-sans); letter-spacing: -0.01em; }

.app-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(135deg, var(--red) 0%, var(--red-dark) 100%);
  color: #fff;
  padding: 14px 20px;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow);
}
.app-topbar .brand {
  color: #fff;
  text-decoration: none;
  font-weight: 700;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  gap: 10px;
}
.topbar-nav { display: flex; gap: 18px; }
.topbar-nav a {
  color: #fff;
  font-size: 1.2rem;
  opacity: 0.9;
  text-decoration: none;
}
.topbar-nav a:hover { opacity: 1; }

.app-container {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px 16px 60px;
  min-height: calc(100vh - 140px);
}

.app-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  text-align: left;
  font-size: 0.78rem;
  color: var(--gray-500);
  padding: 22px 16px 34px;
  max-width: 900px;
  margin: 0 auto;
  border-top: 1px solid var(--gray-300);
}
.footer-logo { flex-shrink: 0; width: 44px; height: auto; }
.footer-text { max-width: 640px; }
.footer-institution {
  margin: 0 0 2px;
  font-weight: 700;
  color: var(--gray-700);
  font-size: 0.8rem;
}
.footer-text p:last-child { margin-top: 8px; font-weight: 400; color: var(--gray-500); }
@media (max-width: 480px) {
  .app-footer { flex-direction: column; text-align: center; }
  .footer-text { max-width: 100%; }
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: none;
  border-radius: 999px;
  padding: 10px 20px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--red); color: #fff; }
.btn-primary:hover { background: var(--red-dark); }
.btn-outline { background: #fff; color: var(--red); border: 2px solid var(--red); }
.btn-outline:hover { background: #fff5f5; }
.btn-danger-outline { background: #fff; color: #b71c1c; border: 2px solid #ffcdd2; }
.btn-danger-outline:hover { background: #fff0f0; }
.btn-large { padding: 14px 28px; font-size: 1.05rem; }
.btn-back {
  background: none;
  border: none;
  color: var(--gray-700);
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  margin-bottom: 10px;
  font-size: 0.92rem;
}
.btn-back:hover { color: var(--red); }

/* ---------- Home ---------- */
.hero {
  text-align: center;
  margin: 10px 0 34px;
  padding: 36px 20px;
  border-radius: 20px;
  background: radial-gradient(120% 140% at 50% -20%, rgba(183,28,28,0.10), rgba(251,140,0,0.05) 55%, transparent 75%);
}
.hero h1 { font-size: 2rem; font-weight: 800; margin-bottom: 10px; color: var(--red-dark); letter-spacing: -0.02em; }
.hero h1 i { margin-right: 10px; }
.hero p { color: var(--gray-700); max-width: 640px; margin: 0 auto; font-size: 1.02rem; }

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 18px;
  margin-bottom: 30px;
}
.level-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px 20px;
  text-align: center;
  border-top: 5px solid var(--accent, var(--red));
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.level-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 14px 30px -10px color-mix(in srgb, var(--accent) 55%, transparent), var(--shadow-lg);
}
.level-card-icon {
  width: 64px; height: 64px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--accent) 15%, white);
  color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.7rem;
  margin: 0 auto 14px;
}
.level-card h2 { margin: 6px 0; font-weight: 800; }
.level-subtitle { color: var(--gray-500); margin-bottom: 14px; font-size: 0.9rem; }

.level-best {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 700;
  padding: 4px 12px;
  border-radius: 999px;
  margin-bottom: 16px;
  background: color-mix(in srgb, var(--accent) 14%, white);
  color: var(--accent);
}
.level-best.is-passed { background: #e8f5e9; color: #1b5e20; }
.level-best i { font-size: 0.85em; }

.home-footer { text-align: center; }

/* ---------- Level header (shared) ---------- */
.level-header {
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  margin-bottom: 22px;
  border-left: 6px solid var(--accent, var(--red));
}
.level-header-icon {
  width: 52px; height: 52px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--accent) 15%, white);
  color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem;
  flex-shrink: 0;
}
.level-header h1 { margin: 0 0 4px; font-size: 1.3rem; }
.level-header p { margin: 0; color: var(--gray-500); font-size: 0.92rem; }
.badge {
  display: inline-block;
  background: var(--gray-100);
  color: var(--gray-700);
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 3px 10px;
  border-radius: 999px;
  vertical-align: middle;
  margin-left: 6px;
}

/* ---------- Level menu tiles ---------- */
.menu-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}
.menu-tile {
  background: var(--card-bg);
  border: none;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px 16px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.menu-tile:hover { transform: translateY(-3px); box-shadow: 0 12px 26px -10px rgba(183,28,28,0.35), var(--shadow-lg); }
.menu-tile i { font-size: 1.8rem; color: var(--red); margin-bottom: 10px; }
.menu-tile h3 { margin: 6px 0; font-size: 1.05rem; }
.menu-tile p { margin: 0; color: var(--gray-500); font-size: 0.85rem; }

/* ---------- Formazione / moduli ---------- */
.modules { display: flex; flex-direction: column; gap: 14px; }
.module-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.module-header {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  background: linear-gradient(135deg, #fff3e0, #fff);
  border: none;
  padding: 16px 18px;
  font-size: 1.02rem;
  font-weight: 700;
  color: var(--gray-900);
  cursor: pointer;
  text-align: left;
}
.module-header span { flex: 1; }
.module-header i.fa-layer-group { color: var(--orange); }
.chevron { transition: transform 0.2s ease; }
.module-header[aria-expanded="true"] .chevron { transform: rotate(180deg); }
.module-body { padding: 10px 18px 18px; }

.section-card { border-top: 1px solid var(--gray-100); }
.section-card:first-child { border-top: none; }
.section-header {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: none;
  padding: 12px 4px;
  font-size: 0.96rem;
  font-weight: 600;
  color: var(--red-dark);
  cursor: pointer;
  text-align: left;
}
.section-header i { color: var(--red); flex-shrink: 0; }
.section-body { padding: 4px 6px 16px 30px; color: var(--gray-700); font-size: 0.94rem; }
.section-body p { margin: 0 0 10px; }
.section-body ul { margin: 0 0 10px; padding-left: 20px; }
.section-body li { margin-bottom: 6px; }

.content-figure {
  margin: 14px 0;
  text-align: center;
  background: var(--gray-100);
  border-radius: 10px;
  padding: 12px;
}
.content-figure img { max-width: 100%; height: auto; max-height: 260px; }
.content-figure figcaption { font-size: 0.78rem; color: var(--gray-500); margin-top: 8px; }

/* ---------- Quiz intro ---------- */
.quiz-intro-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px 24px;
  text-align: center;
}
.quiz-intro-card label { display: block; font-weight: 600; margin-bottom: 12px; }
.quiz-intro-card input[type="range"] { width: 100%; max-width: 400px; }
.qcount-value { margin: 12px 0 20px; font-size: 1.1rem; font-weight: 700; color: var(--red); }

/* ---------- Quiz play ---------- */
.quiz-progress-wrap { margin-bottom: 18px; }
.quiz-progress-bar {
  height: 8px;
  background: var(--gray-300);
  border-radius: 999px;
  overflow: hidden;
}
.quiz-progress-fill { height: 100%; transition: width 0.3s ease; }
.quiz-progress-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 6px;
}
.quiz-progress-label { font-size: 0.8rem; color: var(--gray-500); }
.quiz-abort-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: var(--gray-500);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  flex-shrink: 0;
  transition: color 0.15s ease, background 0.15s ease;
}
.quiz-abort-btn:hover { color: var(--red); background: #ffebee; }

.quiz-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px 22px;
}
.quiz-module-tag {
  display: inline-block;
  background: var(--gray-100);
  color: var(--gray-700);
  font-size: 0.75rem;
  font-weight: 700;
  padding: 4px 12px;
  border-radius: 999px;
  margin-bottom: 12px;
}
.quiz-question { font-size: 1.15rem; margin: 0 0 20px; }
.quiz-options { display: flex; flex-direction: column; gap: 10px; }
.option-btn {
  text-align: left;
  background: var(--gray-100);
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 13px 16px;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--gray-900);
  transition: background 0.15s ease, border-color 0.15s ease;
}
.option-btn:hover:not(:disabled) { background: #ffe9e9; }
.quiz-options.answered .option-btn:disabled { cursor: default; opacity: 0.85; }
.option-btn.correct { background: #e8f5e9; border-color: var(--green); font-weight: 700; }
.option-btn.incorrect { background: #ffebee; border-color: var(--red); font-weight: 700; }

.quiz-feedback {
  margin-top: 18px;
  padding: 14px 16px;
  border-radius: 10px;
  font-size: 0.92rem;
}
.feedback-correct { background: #e8f5e9; color: #1b5e20; }
.feedback-incorrect { background: #ffebee; color: #b71c1c; }
.quiz-feedback i { margin-right: 6px; }
.quiz-feedback p { margin: 8px 0 0; font-weight: 400; color: var(--gray-700); }

.quiz-actions { margin-top: 18px; text-align: right; }

/* ---------- Risultato ---------- */
.result-card {
  text-align: center;
  padding: 32px 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 24px;
}
.result-passed { background: #e8f5e9; }
.result-failed { background: #ffebee; }
.result-icon { font-size: 3rem; margin-bottom: 10px; }
.result-passed .result-icon { color: var(--green); }
.result-failed .result-icon { color: var(--red); }
.result-card h1 { margin: 0 0 10px; }
.result-score { font-size: 1.1rem; margin: 4px 0; }
.result-percentage { font-size: 2.2rem; font-weight: 800; margin: 6px 0; }
.result-meta { color: var(--gray-500); font-size: 0.85rem; }

.review-section { margin-bottom: 24px; }
.review-section h2 { font-size: 1.05rem; margin-bottom: 14px; }
.review-item {
  background: var(--card-bg);
  border-left: 4px solid var(--red);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 14px 16px;
  margin-bottom: 12px;
  font-size: 0.9rem;
}
.review-question { font-weight: 700; margin: 0 0 8px; }
.review-your-answer { color: var(--red); margin: 4px 0; }
.review-correct-answer { color: #1b5e20; margin: 4px 0; }
.review-explanation { color: var(--gray-700); margin: 8px 0 0; font-style: italic; }
.all-correct { text-align: center; color: #1b5e20; font-weight: 700; font-size: 1rem; }

.result-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

/* ---------- Storico ---------- */
.storico-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 22px;
}
.stat-card {
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 16px 12px;
  text-align: center;
}
.stat-value { font-size: 1.7rem; font-weight: 800; color: var(--red); line-height: 1.1; }
.stat-label {
  font-size: 0.72rem;
  color: var(--gray-500);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 4px;
}

.storico-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 18px;
  align-items: center;
}
.filter-btn {
  background: var(--gray-100);
  border: none;
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--gray-700);
}
.filter-btn.active { background: var(--red); color: #fff; }

.storico-list { display: flex; flex-direction: column; gap: 10px; }
.storico-item {
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  border-left: 5px solid var(--accent, var(--gray-500));
  overflow: hidden;
}
.storico-item-header {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  background: none;
  border: none;
  padding: 14px 16px;
  cursor: pointer;
  font-size: 0.88rem;
  text-align: left;
}
.storico-item-header .chevron { margin-left: auto; color: var(--gray-500); }
.storico-badge {
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
}
.badge-pass { background: #e8f5e9; color: #1b5e20; }
.badge-fail { background: #ffebee; color: #b71c1c; }
.storico-level { font-weight: 700; color: var(--gray-700); }
.storico-date { color: var(--gray-500); }
.storico-score { font-weight: 700; margin-left: auto; }
.storico-item-header[aria-expanded="true"] .chevron { transform: rotate(180deg); }
.storico-item-body { padding: 4px 18px 18px; font-size: 0.88rem; }

.loading, .empty-state {
  text-align: center;
  color: var(--gray-500);
  padding: 30px 0;
}

/* ---------- Responsive ---------- */
@media (max-width: 480px) {
  .hero h1 { font-size: 1.4rem; }
  .level-header { flex-direction: column; text-align: center; }
  .result-actions { flex-direction: column; }
  .result-actions .btn { width: 100%; justify-content: center; }
  .storico-score { margin-left: 0; }
}
