:root {
  --bg: #0f1115;
  --bg-elev: #161a22;
  --bg-soft: #1d222c;
  --border: #262d3a;
  --text: #e7ecf3;
  --text-dim: #9aa4b2;
  --accent: #7c9eff;
  --accent-2: #57e2c8;
  --warn: #ffb86b;
  --bad: #ff7a90;
  --good: #6bd49a;
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: var(--sans); font-size: 15.5px; line-height: 1.55; }
body { display: grid; grid-template-columns: 280px 1fr; min-height: 100vh; }
button { font-family: inherit; }

/* Sidebar */
#sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  padding: 22px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #06121f; font-weight: 800; font-size: 22px;
  display: grid; place-items: center;
}
.brand h1 { font-size: 16px; margin: 0; }
.brand .tag { margin: 2px 0 0; font-size: 12px; color: var(--text-dim); }

#nav-modes { display: grid; gap: 4px; }
.nav-sep { height: 1px; background: var(--border); margin: 10px 4px; }
.mode-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  padding: 9px 12px;
  border-radius: 8px;
  text-align: left;
  cursor: pointer;
  font-size: 14px;
}
.mode-btn:hover { background: var(--bg-soft); color: var(--text); }
.mode-btn.active { background: var(--bg-soft); color: var(--text); border-color: var(--border); }

#chapter-list h2 { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--text-dim); margin: 8px 4px; }
#chapters { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
#chapters li button {
  width: 100%; text-align: left; background: transparent; border: 0;
  color: var(--text-dim); padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 13.5px;
}
#chapters li button:hover { background: var(--bg-soft); color: var(--text); }
#chapters li button.active { background: var(--bg-soft); color: var(--accent-2); }

.footer-side { margin-top: auto; }
.mini { background: transparent; border: 1px solid var(--border); color: var(--text-dim); padding: 6px 10px; border-radius: 6px; font-size: 12px; cursor: pointer; }
.mini:hover { color: var(--bad); border-color: var(--bad); }

/* Main */
#main { padding: 40px 56px 80px; max-width: 980px; margin: 0 auto; width: 100%; }
h1.page { font-size: 28px; margin: 0 0 6px; }
.page-sub { color: var(--text-dim); margin: 0 0 24px; }

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 22px;
  margin-bottom: 18px;
}
.card h2 { margin: 0 0 10px; font-size: 18px; }
.card h3 { margin: 18px 0 8px; font-size: 15px; color: var(--accent-2); }
.card p, .card li { color: var(--text); }
.card ul { padding-left: 18px; }
.card .meta { font-size: 13px; color: var(--text-dim); }

/* Home */
.grid-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.tile {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; cursor: pointer; transition: transform 0.1s ease, border-color 0.15s;
}
.tile:hover { transform: translateY(-2px); border-color: var(--accent); }
.tile h3 { margin: 0 0 6px; font-size: 15px; }
.tile p { margin: 0; color: var(--text-dim); font-size: 13px; }

.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin: 18px 0 24px; }
.kpi { background: var(--bg-elev); border: 1px solid var(--border); border-radius: 10px; padding: 14px; }
.kpi .v { font-size: 22px; font-weight: 700; }
.kpi .l { font-size: 12px; color: var(--text-dim); margin-top: 4px; }

/* Quiz */
.q-card { background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; }
.q-num { font-size: 12px; color: var(--text-dim); letter-spacing: 0.06em; }
.q-text { font-size: 18px; margin: 8px 0 18px; line-height: 1.4; }
.q-opts { display: grid; gap: 10px; }
.q-opt {
  background: var(--bg-soft); border: 1px solid var(--border); color: var(--text);
  padding: 12px 14px; border-radius: 10px; cursor: pointer; text-align: left; font-size: 14.5px;
}
.q-opt:hover { border-color: var(--accent); }
.q-opt.correct { border-color: var(--good); background: rgba(107, 212, 154, 0.12); }
.q-opt.wrong { border-color: var(--bad); background: rgba(255, 122, 144, 0.10); }
.q-opt.locked { cursor: default; }
.q-opt .badge { float: right; font-size: 12px; color: var(--text-dim); }
.q-opt.correct .badge { color: var(--good); }
.q-opt.wrong .badge { color: var(--bad); }

.q-actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.btn { background: var(--accent); color: #07101f; border: 0; padding: 10px 14px; border-radius: 8px; font-weight: 600; cursor: pointer; }
.btn:hover { filter: brightness(1.08); }
.btn.secondary { background: var(--bg-soft); color: var(--text); border: 1px solid var(--border); }
.btn.ghost { background: transparent; color: var(--text-dim); border: 1px solid var(--border); }

.explain {
  margin-top: 14px;
  padding: 12px 14px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  font-size: 14px;
}

.progress-bar { height: 6px; background: var(--bg-soft); border-radius: 99px; overflow: hidden; margin-bottom: 18px; }
.progress-bar > div { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width 0.25s ease; }

/* Open question */
.q-open input, .q-open textarea {
  width: 100%; background: var(--bg-soft); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px; font-family: inherit; font-size: 14.5px;
  resize: vertical;
}

/* Flashcards */
.flash {
  perspective: 1500px;
  height: 380px;
  margin: 24px 0;
}
.flash.tall { height: 540px; }
.flash-inner {
  position: relative;
  width: 100%; height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.55s;
}
.flash.flipped .flash-inner { transform: rotateY(180deg); }
.flash-face {
  position: absolute; inset: 0;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  display: grid; place-items: center; padding: 30px; text-align: center;
  backface-visibility: hidden;
}
.flash-face.back { transform: rotateY(180deg); background: var(--bg-soft); }
.flash-face .label { font-size: 12px; color: var(--text-dim); position: absolute; top: 16px; left: 20px; }
.flash-face .content { font-size: 19px; line-height: 1.45; max-width: 760px; max-height: 100%; overflow-y: auto; padding: 20px 0; }
.flash-face.back .content { font-size: 15px; text-align: left; }
.flash-actions { display: flex; justify-content: space-between; gap: 10px; }

/* Videos */
.video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.video-card { background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.video-card a { color: var(--accent); text-decoration: none; word-break: break-word; }
.video-card a:hover { text-decoration: underline; }
.video-card .yt { display: inline-block; padding: 4px 8px; border-radius: 4px; background: #ff0033; color: white; font-size: 11px; font-weight: 700; margin-bottom: 8px; }
.video-card .arte { background: #000; }
.video-card .khan { background: #14bf96; }

/* Stats */
.bar { height: 8px; background: var(--bg-soft); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar > div { height: 100%; }
.bar.good > div { background: var(--good); }
.bar.warn > div { background: var(--warn); }
.bar.bad > div { background: var(--bad); }
table.stats { width: 100%; border-collapse: collapse; }
table.stats th, table.stats td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); font-size: 14px; }
table.stats th { color: var(--text-dim); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; }

/* Course */
.course-toc { display: grid; gap: 8px; }
.toc-row { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 10px; cursor: pointer; }
.toc-row:hover { border-color: var(--accent); }
.toc-row .chip { font-size: 11px; padding: 3px 8px; border-radius: 99px; background: var(--bg-soft); color: var(--text-dim); }
.toc-row .chip.done { background: rgba(107, 212, 154, 0.15); color: var(--good); }

.back-link { background: none; border: 0; color: var(--text-dim); cursor: pointer; padding: 0; font-size: 13px; margin-bottom: 8px; }
.back-link:hover { color: var(--text); }

.tip { padding: 12px 14px; background: rgba(124, 158, 255, 0.08); border: 1px solid rgba(124, 158, 255, 0.3); border-radius: 8px; font-size: 13.5px; color: var(--text); margin-bottom: 16px; }

/* Result screen */
.result-big { text-align: center; padding: 40px 20px; }
.result-big .score { font-size: 56px; font-weight: 800; background: linear-gradient(135deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.result-big .label { color: var(--text-dim); margin-bottom: 24px; }

/* Editor forms */
.card label { display: block; margin: 10px 0 4px; font-size: 13px; color: var(--text-dim); }
.card input[type="text"], .card textarea, .card select {
  width: 100%; background: var(--bg-soft); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px;
  font-family: inherit; font-size: 14px; resize: vertical;
}
.card input[type="text"]:focus, .card textarea:focus, .card select:focus { outline: none; border-color: var(--accent); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid label { margin: 0; }
.ed-section, .ed-question {
  background: var(--bg-soft); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px; margin: 12px 0;
}
.ed-q-head { display: flex; gap: 10px; align-items: center; margin-bottom: 6px; }
.chip { font-size: 11px; padding: 3px 8px; border-radius: 99px; background: var(--bg-elev); color: var(--text-dim); }
.chip.done { background: rgba(107, 212, 154, 0.15); color: var(--good); }
code { background: var(--bg-soft); padding: 1px 6px; border-radius: 4px; font-family: var(--mono); font-size: 13px; }
pre code { background: transparent; padding: 0; }

/* Hamburger button (mobile only) */
#menu-toggle {
  display: none;
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  left: calc(14px + env(safe-area-inset-left));
  z-index: 200;
  width: 44px;
  height: 44px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
}
#menu-toggle:hover { border-color: var(--accent); }
#menu-toggle svg.lucide, #menu-toggle [data-lucide] { width: 22px; height: 22px; }

/* Sidebar backdrop on mobile */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 140;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
body.sidebar-open .sidebar-backdrop { display: block; }

/* iOS / Android safe areas everywhere */
#sidebar {
  padding-top: calc(22px + env(safe-area-inset-top));
  padding-left: calc(16px + env(safe-area-inset-left));
}
#main {
  padding-top: calc(40px + env(safe-area-inset-top));
  padding-right: calc(56px + env(safe-area-inset-right));
  padding-left: calc(56px + env(safe-area-inset-left));
  padding-bottom: calc(80px + env(safe-area-inset-bottom));
}

/* Prevent iOS auto-zoom on text inputs (font-size must be >=16px) */
input[type="text"], input[type="password"], textarea, select {
  font-size: 16px;
}

/* Touch-friendly button sizing across the app */
.btn, .mini, .mode-btn, .q-opt, .toc-row, .tile, .back-link, button[type="submit"] {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Responsive */
@media (max-width: 900px) {
  body { grid-template-columns: 1fr; }

  #menu-toggle { display: flex; }
  body[data-onboarding="true"] #menu-toggle { display: none; }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(85vw, 320px);
    height: 100vh;
    border-right: 1px solid var(--border);
    border-bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 150;
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.4);
  }
  body.sidebar-open #sidebar { transform: translateX(0); }

  #main {
    padding-top: calc(68px + env(safe-area-inset-top));
    padding-right: calc(16px + env(safe-area-inset-right));
    padding-left: calc(16px + env(safe-area-inset-left));
    padding-bottom: calc(40px + env(safe-area-inset-bottom));
  }

  h1.page { font-size: 24px; line-height: 1.2; }
  .page-sub { font-size: 14px; }

  .kpi-row { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .kpi { padding: 12px; }
  .kpi .v { font-size: 18px; }

  .grid-tiles { grid-template-columns: 1fr; gap: 10px; }
  .tile { padding: 14px; }

  .welcome-card {
    margin: 24px 12px 0;
    padding: 22px;
    max-width: none;
  }
  .welcome-card .brand-big .logo { width: 42px; height: 42px; font-size: 22px; }

  .greeting-row { flex-wrap: wrap; gap: 6px; }

  .q-card { padding: 18px; }
  .q-text { font-size: 16px; }
  .q-opt { padding: 14px; min-height: 48px; }
  .btn { padding: 12px 16px; min-height: 44px; font-size: 15px; }
  .mode-btn { padding: 11px 12px; min-height: 44px; }
  .mini { padding: 10px; min-height: 40px; }

  .flash { height: 320px; margin: 16px 0; }
  .flash.tall { height: 480px; }
  .flash-face { padding: 22px; }
  .flash-face .content { font-size: 16px; }

  .flash-actions { flex-direction: column; gap: 8px; }
  .flash-actions .btn { width: 100%; }

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

  table.stats { font-size: 12px; }
  table.stats th, table.stats td { padding: 8px 6px; }

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

  pre { font-size: 11px; }

  .card { padding: 16px; }
  .toc-row { padding: 12px 14px; }

  .q-actions { gap: 8px; }
  .q-actions .btn { flex: 1 1 auto; }
}

@media (max-width: 480px) {
  h1.page { font-size: 20px; }
  .welcome-card { padding: 18px; }
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
}

/* Lucide icons - base sizing + alignment */
[data-lucide], svg.lucide {
  width: 16px;
  height: 16px;
  stroke-width: 2;
  flex-shrink: 0;
  vertical-align: -3px;
}
.mode-btn, #sound-toggle, #reset-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}
#sound-toggle, #reset-progress { margin-bottom: 6px; }
.mode-btn svg.lucide, .mode-btn [data-lucide] { width: 17px; height: 17px; }
.brand .logo svg.lucide { width: 22px; height: 22px; stroke-width: 2.4; }

.tile h3 { display: flex; align-items: center; gap: 8px; }
.tile h3 svg.lucide, .tile h3 [data-lucide] { width: 18px; height: 18px; color: var(--accent-2); }

.tip { display: flex; align-items: flex-start; gap: 10px; }
.tip svg.lucide, .tip [data-lucide] { width: 18px; height: 18px; color: var(--accent); margin-top: 1px; }

.btn svg.lucide, .btn [data-lucide], .back-link svg.lucide, .back-link [data-lucide] {
  width: 15px;
  height: 15px;
  vertical-align: -3px;
  margin-right: 4px;
}

.ico-good { color: var(--good); }
.ico-bad { color: var(--bad); }
.ico-warn { color: var(--warn); }
.ico-accent { color: var(--accent); }

.explain svg.lucide, .explain [data-lucide] { width: 16px; height: 16px; vertical-align: -3px; margin-right: 4px; }

/* Onboarding gate : pas de sidebar tant que le prénom n'est pas saisi */
body[data-onboarding="true"] {
  grid-template-columns: 1fr;
}
body[data-onboarding="true"] #sidebar { display: none; }
body[data-onboarding="true"] #main { padding-top: 60px; }

/* Welcome card */
.welcome-card {
  max-width: 520px;
  margin: 40px auto 0;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
}
.welcome-card .brand-big {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}
.welcome-card .brand-big .logo {
  width: 48px; height: 48px; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #06121f; font-weight: 800; font-size: 26px;
  display: grid; place-items: center;
}
.welcome-card .brand-big h1 { margin: 0; font-size: 20px; }
.welcome-card form { display: grid; gap: 12px; margin-top: 18px; }
.welcome-card input[type="text"] {
  background: var(--bg-soft); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 14px; font-size: 15px; font-family: inherit;
}
.welcome-card input[type="text"]:focus { outline: none; border-color: var(--accent); }
.welcome-card button[type="submit"] {
  background: var(--accent); color: #07101f; border: 0;
  padding: 11px 16px; border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 15px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.welcome-card button[type="submit"]:hover { filter: brightness(1.08); }

.greeting-row { display: flex; align-items: center; gap: 10px; }
.greeting-row h1 { margin: 0; }
.greeting-row .change-name {
  background: none; border: 0; color: var(--text-dim);
  font-size: 13px; cursor: pointer; text-decoration: underline; font-family: inherit;
}
.greeting-row .change-name:hover { color: var(--text); }

.feedback-tag { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }

.model-answer {
  margin-top: 10px;
  padding: 12px;
  background: var(--bg-elev);
  border-radius: 8px;
  border-left: 3px solid var(--accent-2);
}
.model-answer .head { display: flex; align-items: center; gap: 6px; font-weight: 600; margin-bottom: 6px; }
