:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --text: #1f1d1a;
  --muted: #6b665d;
  --border: #e2ddd3;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --good: #2f9e44;
  --good-bg: #e6f6ea;
  --bad: #d9480f;
  --bad-bg: #fff0e8;
  --lvl-facile: #2f9e44;
  --lvl-moyen: #1c7ed6;
  --lvl-difficile: #e8590c;
  --lvl-expert: #9c36b5;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514;
    --surface: #211f1d;
    --surface-2: #2a2825;
    --text: #ece8e1;
    --muted: #a09a90;
    --border: #3a3733;
    --accent: #748ffc;
    --accent-text: #10131f;
    --good: #69db7c;
    --good-bg: #1d3323;
    --bad: #ff8a5b;
    --bad-bg: #3a2219;
    --lvl-facile: #69db7c;
    --lvl-moyen: #4dabf7;
    --lvl-difficile: #ff922b;
    --lvl-expert: #da77f2;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: .75rem;
  padding: .75rem 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); font-size: 1.05rem; }
.topbar #logout { margin-left: auto; }
.badge { font-size: .75rem; padding: .15rem .55rem; border-radius: 99px; background: var(--bad-bg); color: var(--bad); font-weight: 600; }

main { max-width: 860px; margin: 0 auto; padding: 1.25rem 16px 4rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  box-shadow: var(--shadow);
}
.stack > * + * { margin-top: .9rem; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }

button, .btn {
  font: inherit; font-weight: 600; font-size: .95rem;
  border: 1px solid transparent; border-radius: 10px;
  padding: .6rem 1rem; cursor: pointer;
  background: var(--accent); color: var(--accent-text);
  display: inline-flex; align-items: center; gap: .4rem;
  transition: transform .05s, opacity .15s;
}
button:active { transform: scale(.98); }
button:disabled { opacity: .5; cursor: default; }
button.ghost, .btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
button.small { padding: .35rem .7rem; font-size: .85rem; }
button.good { background: var(--good); color: #fff; }
button.bad { background: var(--bad); color: #fff; }
button.danger { background: transparent; color: var(--bad); border-color: var(--border); }

input[type=password], textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px;
  padding: .65rem .8rem; width: 100%;
}
textarea { min-height: 140px; resize: vertical; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Upload */
.drop {
  display: block;
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 1.6rem 1rem; text-align: center; cursor: pointer;
  background: var(--surface); transition: border-color .15s, background .15s;
}
.drop.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--surface)); }
.drop strong { display: block; font-size: 1.05rem; margin-bottom: .2rem; }

/* Liste de cours */
.course-list { display: grid; gap: .8rem; }
.course-item { display: block; color: inherit; }
.course-item:hover { border-color: var(--accent); }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .4s; }
.pct { font-variant-numeric: tabular-nums; font-weight: 700; }

/* Niveaux */
.lvl { --c: var(--muted); }
.lvl-facile { --c: var(--lvl-facile); }
.lvl-moyen { --c: var(--lvl-moyen); }
.lvl-difficile { --c: var(--lvl-difficile); }
.lvl-expert { --c: var(--lvl-expert); }
.levels-mini { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem; margin-top: .6rem; }
.levels-mini .lvl .progress > span { background: var(--c); }
.levels-mini .lvl small { display: flex; justify-content: space-between; color: var(--muted); font-size: .75rem; margin-bottom: .2rem; }

.tabs, .level-pick { display: flex; gap: .35rem; flex-wrap: wrap; }
.tabs button, .level-pick button {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  padding: .45rem .8rem; font-size: .9rem;
}
.tabs button[aria-pressed=true] { background: var(--text); color: var(--bg); border-color: var(--text); }
.level-pick button { border-left: 4px solid var(--c); }
.level-pick button[aria-pressed=true] { background: var(--c); color: #fff; border-color: var(--c); }
.level-pick button .n { opacity: .75; font-weight: 500; }

/* Notions */
.notion-item { display: block; color: inherit; }
.notion-item:hover { border-color: var(--accent); }
.status { font-size: .8rem; color: var(--muted); }
.status.error { color: var(--bad); }
.spinner {
  display: inline-block; width: 1em; height: 1em; vertical-align: -.15em;
  border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.keypoints { margin: .3rem 0 0; padding-left: 1.2rem; }
.keypoints li { margin: .25rem 0; }

/* Flashcard */
.flash { perspective: 1200px; cursor: pointer; user-select: none; }
.flash-inner {
  position: relative; min-height: 240px;
  transition: transform .45s; transform-style: preserve-3d;
}
.flash.flipped .flash-inner { transform: rotateY(180deg); }
.flash-face {
  position: absolute; inset: 0; backface-visibility: hidden;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  text-align: center; padding: 1.5rem; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-top: 5px solid var(--c);
  overflow-y: auto; white-space: pre-wrap;
}
.flash-face .label { position: absolute; top: .6rem; left: .9rem; font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.flash-front { font-size: 1.2rem; font-weight: 600; }
.flash-back { transform: rotateY(180deg); }

/* QCM */
.choices { display: grid; gap: .5rem; margin-top: .8rem; }
.choice {
  text-align: left; justify-content: flex-start;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  font-weight: 500; padding: .75rem .9rem; white-space: normal; line-height: 1.4;
}
.choice .letter { font-weight: 700; color: var(--muted); margin-right: .3rem; }
.choice.right { background: var(--good-bg); border-color: var(--good); }
.choice.wrong { background: var(--bad-bg); border-color: var(--bad); }
.explain { border-left: 4px solid var(--accent); padding: .6rem .9rem; background: var(--surface-2); border-radius: 0 10px 10px 0; white-space: pre-wrap; }

.counter { font-size: .85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.score-big { font-size: 2.6rem; font-weight: 800; }

/* Exercices */
.exercise .statement, .exercise .solution, .exercise .hint { white-space: pre-wrap; }
details { border: 1px solid var(--border); border-radius: 10px; padding: .5rem .8rem; background: var(--surface-2); }
details summary { cursor: pointer; font-weight: 600; }
details[open] summary { margin-bottom: .4rem; }
.grade { font-size: 1.6rem; font-weight: 800; }
.feedback ul { margin: .2rem 0 .6rem; padding-left: 1.2rem; }

.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); }
.crumbs { font-size: .9rem; margin-bottom: .6rem; }

#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(150%);
  background: var(--text); color: var(--bg); padding: .65rem 1.1rem; border-radius: 10px;
  font-size: .9rem; transition: transform .25s; max-width: calc(100% - 32px); z-index: 50;
}
#toast.show { transform: translateX(-50%) translateY(0); }

.login { max-width: 360px; margin: 12vh auto 0; }

@media (max-width: 560px) {
  h1 { font-size: 1.35rem; }
  .levels-mini { grid-template-columns: repeat(2, 1fr); }
  .flash-inner { min-height: 280px; }
}
