:root {
  --bg: #f7f8fa;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --text: #101828;
  --text-secondary: #475467;
  --text-tertiary: #667085;
  --text-weak: #98a2b3;
  --border-fine: rgba(16, 24, 40, 0.07);
  --border-strong: rgba(16, 24, 40, 0.12);
  --track: rgba(16, 24, 40, 0.08);
  --veil: rgba(16, 24, 40, 0.4);

  --accent: oklch(0.6164 0.1081 161.32);
  --accent-text: oklch(0.5164 0.1081 161.32);
  --accent-hover: oklch(0.4164 0.1081 161.32);
  --accent-shadow: oklch(0.6164 0.1081 161.32 / 0.4);

  --danger: #e05c5c;
  --danger-hover: #c94a4a;

  --font-body: "Segoe UI", system-ui, -apple-system, Arial, sans-serif;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-pill: 999px;

  --shadow-card: 0 0 0 1px var(--border-fine), 0 6px 14px rgba(16, 24, 40, 0.08);
  --shadow-modal: 0 24px 60px rgba(16, 24, 40, 0.25);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 3rem;
}

h1, h2, h3 {
  margin: 0;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--text-secondary);
  margin: 0.25rem 0 0;
}

button {
  font-family: inherit;
  cursor: pointer;
}

/* ---------- Boutons ---------- */

.btn {
  border: none;
  border-radius: var(--radius-pill);
  padding: 0.65rem 1.25rem;
  font-size: 0.95rem;
  font-weight: 600;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.15s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 8px 20px var(--accent-shadow);
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border-strong);
}

.btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent-text);
}

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

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

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}

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

.btn-sm {
  padding: 0.4rem 0.85rem;
  font-size: 0.85rem;
}

.btn-block {
  width: 100%;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ---------- Connexion ---------- */

#view-login {
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-card {
  background: var(--panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.login-card h1 {
  font-size: 1.8rem;
}

.login-sub {
  color: var(--text-secondary);
  margin: 0.5rem 0 1.75rem;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.form-error {
  color: var(--danger);
  min-height: 1.2rem;
  margin: 0.9rem 0 0;
  font-size: 0.9rem;
}

/* ---------- Champs formulaire ---------- */

input[type="text"],
input[type="url"],
input[type="password"],
input[type="file"],
select,
textarea {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.85rem;
  font-size: 0.95rem;
  font-family: inherit;
  background: var(--panel);
  color: var(--text);
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

textarea {
  resize: vertical;
  min-height: 4.5rem;
}

input[type="color"] {
  width: 3.5rem;
  height: 2.4rem;
  padding: 0.15rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--panel);
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.9rem;
}

/* ---------- Structure app ---------- */

#app {
  width: 100%;
  max-width: 900px;
  padding: 1.25rem 1rem 4rem;
}

header {
  margin-bottom: 1.5rem;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.brand {
  font-size: 1.4rem;
}

.stats-bar {
  background: var(--panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 1rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.stat-level {
  flex: 1 1 220px;
  min-width: 180px;
}

.stat-level-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  display: block;
  margin-bottom: 0.4rem;
}

.stat-level-label b {
  color: var(--accent-text);
}

.stat-xp-text {
  font-size: 0.75rem;
  color: var(--text-weak);
  display: block;
  margin-top: 0.3rem;
}

.stat-streak {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 1.1rem;
  font-weight: 700;
  background: var(--panel-2);
  padding: 0.4rem 0.8rem;
  border-radius: var(--radius-pill);
}

.stat-badges {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.badge-pill {
  font-size: 1.15rem;
  background: var(--panel-2);
  border-radius: var(--radius-pill);
  width: 2.1rem;
  height: 2.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: help;
}

/* ---------- Barres de progression ---------- */

.progress-track {
  background: var(--track);
  border-radius: var(--radius-pill);
  height: 0.6rem;
  overflow: hidden;
  width: 100%;
}

.stat-xp-track {
  height: 0.45rem;
}

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

/* ---------- Vues ---------- */

.view {
  display: none;
}

.view.active {
  display: block;
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.empty-state {
  color: var(--text-weak);
  text-align: center;
  padding: 2rem 1rem;
}

/* ---------- Cartes sujets ---------- */

.subjects-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
}

.subject-card {
  background: var(--panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 1.25rem;
  border-top: 4px solid var(--card-color, var(--accent));
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.subject-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 24px rgba(16, 24, 40, 0.14);
}

.subject-card h3 {
  font-size: 1.1rem;
}

.subject-card p {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin: 0;
  min-height: 1.2em;
}

.subject-card-pct {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent-text);
  text-align: right;
}

/* ---------- Détail sujet ---------- */

.back-btn {
  margin-bottom: 1rem;
  padding-left: 0;
}

.subject-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.subject-progress-track {
  height: 0.7rem;
  margin-bottom: 0.4rem;
}

.subject-progress-text {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin: 0 0 1.5rem;
}

.subject-tabs {
  display: flex;
  gap: 0.5rem;
  border-bottom: 1px solid var(--border-fine);
  margin-bottom: 1.25rem;
}

.tab-btn {
  background: none;
  border: none;
  padding: 0.6rem 0.2rem;
  font-weight: 600;
  color: var(--text-tertiary);
  border-bottom: 2px solid transparent;
  margin-right: 1rem;
}

.tab-btn.active {
  color: var(--accent-text);
  border-bottom-color: var(--accent);
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.panel-actions {
  display: flex;
  gap: 0.6rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

/* ---------- Topics ---------- */

.topic-row {
  background: var(--panel);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  padding: 0.85rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.6rem;
}

.topic-row.child {
  margin-left: 1.75rem;
  background: var(--panel-2);
  box-shadow: none;
  border: 1px solid var(--border-fine);
}

.topic-status-btn {
  flex-shrink: 0;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  transition: all 0.15s ease;
}

.topic-status-btn[data-status="in_progress"] {
  border-color: #f0a94a;
  background: #fdf1e0;
}

.topic-status-btn[data-status="done"] {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.topic-label {
  flex: 1;
  font-weight: 600;
}

.topic-label.status-done {
  color: var(--text-weak);
  text-decoration: line-through;
}

.topic-actions {
  display: flex;
  gap: 0.4rem;
}

/* ---------- Ressources ---------- */

.resource-row {
  background: var(--panel);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  padding: 0.85rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.6rem;
}

.resource-icon {
  font-size: 1.3rem;
}

.resource-main {
  flex: 1;
  min-width: 0;
}

.resource-title {
  font-weight: 600;
  display: block;
  text-decoration: none;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.resource-title:hover {
  color: var(--accent-text);
}

.resource-meta {
  font-size: 0.75rem;
  color: var(--text-weak);
}

/* ---------- Quiz ---------- */

.quiz-row {
  background: var(--panel);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  padding: 0.85rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.6rem;
  flex-wrap: wrap;
}

.quiz-row-main {
  flex: 1;
  min-width: 160px;
}

.quiz-row-title {
  font-weight: 600;
}

.quiz-row-meta {
  font-size: 0.75rem;
  color: var(--text-weak);
}

.quiz-source-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--accent-text);
  background: color-mix(in oklch, var(--accent) 15%, white);
  padding: 0.15rem 0.5rem;
  border-radius: var(--radius-pill);
  margin-left: 0.5rem;
}

/* ---------- Écran quiz ---------- */

.quiz-progress {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin: 0.5rem 0;
}

.quiz-question-card {
  background: var(--panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 1.5rem;
  margin: 1.25rem 0;
}

.quiz-question-card h3 {
  margin-bottom: 1.1rem;
  font-size: 1.15rem;
}

.quiz-choice {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--panel-2);
  border: 2px solid transparent;
  border-radius: var(--radius-md);
  padding: 0.75rem 1rem;
  margin-bottom: 0.6rem;
  font-size: 0.95rem;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.quiz-choice:hover {
  border-color: var(--border-strong);
}

.quiz-choice.selected {
  border-color: var(--accent);
  background: color-mix(in oklch, var(--accent) 12%, white);
  font-weight: 600;
}

.quiz-actions {
  display: flex;
  justify-content: flex-end;
}

.quiz-result {
  text-align: center;
  padding: 3rem 1rem;
  position: relative;
}

.quiz-result-score {
  font-size: 2.2rem;
  font-weight: 700;
  color: var(--accent-text);
  margin: 0.75rem 0 0.25rem;
}

.quiz-history-list {
  max-height: 300px;
  overflow-y: auto;
}

.quiz-history-row {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border-fine);
  font-size: 0.9rem;
}

/* ---------- Confetti (CSS uniquement) ---------- */

.confetti-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.confetti-piece {
  position: absolute;
  top: -1.5rem;
  font-size: 1.4rem;
  animation: confetti-fall 2.4s ease-in forwards;
}

@keyframes confetti-fall {
  0% {
    transform: translateY(0) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translateY(110vh) rotate(360deg);
    opacity: 0;
  }
}

/* ---------- Panneau "Apprendre" ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: var(--veil);
  z-index: 40;
}

.side-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(420px, 100vw);
  background: var(--panel);
  box-shadow: -12px 0 40px rgba(16, 24, 40, 0.2);
  z-index: 50;
  transform: translateX(0);
  transition: transform 0.2s ease;
  display: flex;
  flex-direction: column;
}

.side-panel.hidden {
  display: flex !important;
  transform: translateX(105%);
  pointer-events: none;
}

.side-panel-inner {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow-y: auto;
}

.side-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}

.lesson-content {
  flex: 1;
  font-size: 0.95rem;
  line-height: 1.6;
}

.lesson-content h2 {
  font-size: 1.05rem;
  margin: 1rem 0 0.5rem;
}

.lesson-content ul {
  padding-left: 1.2rem;
}

.lesson-content p {
  margin: 0.6rem 0;
}

.lesson-loading {
  color: var(--text-weak);
  font-style: italic;
}

.side-panel-actions {
  margin-top: 1rem;
}

/* ---------- Modales ---------- */

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
  padding: 1.75rem;
  width: min(440px, 92vw);
  max-height: 88vh;
  overflow-y: auto;
  z-index: 60;
}

.modal-lg {
  width: min(620px, 92vw);
}

.modal h3 {
  margin-bottom: 1.25rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 0.5rem;
}

.question-builder-block {
  border: 1px solid var(--border-fine);
  border-radius: var(--radius-md);
  padding: 1rem;
  margin-bottom: 1rem;
  position: relative;
}

.question-builder-block .btn-danger {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
}

.choice-input-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.choice-input-row input[type="text"] {
  flex: 1;
}

/* ---------- Toasts ---------- */

#toast-container {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 100;
  width: min(360px, 92vw);
}

.toast {
  background: var(--text);
  color: #fff;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  font-size: 0.9rem;
  box-shadow: 0 12px 24px rgba(16, 24, 40, 0.3);
  animation: toast-in 0.2s ease;
  text-align: center;
}

.toast.success {
  background: var(--accent-hover);
}

.toast.error {
  background: var(--danger-hover);
}

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

/* ---------- Responsive ---------- */

@media (max-width: 480px) {
  .stats-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .stat-streak {
    align-self: flex-start;
  }

  .subject-header {
    flex-direction: column;
  }

  .subjects-grid {
    grid-template-columns: 1fr;
  }

  .quiz-row {
    flex-direction: column;
    align-items: stretch;
  }

  .side-panel {
    width: 100vw;
  }
}
