/* =========================================================
   Fiches interactives — Terminale spécialité mathématiques
   ========================================================= */

:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --text: #1d2127;
  --muted: #5d6573;
  --border: #e2ded4;
  --shadow: 0 1px 2px rgba(20, 20, 30, .05), 0 6px 22px rgba(20, 20, 30, .06);

  --accent: #4f46e5;
  --accent-ink: #ffffff;

  --b-def: #2563eb;
  --b-thm: #7c3aed;
  --b-warn: #dc2626;
  --b-tip: #15803d;
  --b-ex: #64748b;
  --b-meth: #c2410c;

  --ok: #15803d;
  --bad: #dc2626;

  /* couleurs des graphiques */
  --plot-bg: #ffffff;
  --plot-grid: #ece9e2;
  --plot-axis: #6b7280;
  --plot-text: #4b5563;
  --c1: #2563eb;
  --c2: #ea580c;
  --c3: #16a34a;
  --c4: #9333ea;
  --c5: #db2777;
  --c6: #0891b2;

  --radius: 14px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "Cascadia Code", Consolas, "SFMono-Regular", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1116;
    --surface: #171a21;
    --surface-2: #1f232c;
    --text: #e7e9ec;
    --muted: #9ba3af;
    --border: #2b303b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 22px rgba(0, 0, 0, .25);
    --b-def: #60a5fa;
    --b-thm: #a78bfa;
    --b-warn: #f87171;
    --b-tip: #4ade80;
    --b-ex: #94a3b8;
    --b-meth: #fb923c;
    --ok: #4ade80;
    --bad: #f87171;
    --plot-bg: #171a21;
    --plot-grid: #252a34;
    --plot-axis: #8b93a1;
    --plot-text: #aab2bf;
    --c1: #60a5fa;
    --c2: #fb923c;
    --c3: #4ade80;
    --c4: #c084fc;
    --c5: #f472b6;
    --c6: #22d3ee;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1116;
  --surface: #171a21;
  --surface-2: #1f232c;
  --text: #e7e9ec;
  --muted: #9ba3af;
  --border: #2b303b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 22px rgba(0, 0, 0, .25);
  --b-def: #60a5fa;
  --b-thm: #a78bfa;
  --b-warn: #f87171;
  --b-tip: #4ade80;
  --b-ex: #94a3b8;
  --b-meth: #fb923c;
  --ok: #4ade80;
  --bad: #f87171;
  --plot-bg: #171a21;
  --plot-grid: #252a34;
  --plot-axis: #8b93a1;
  --plot-text: #aab2bf;
  --c1: #60a5fa;
  --c2: #fb923c;
  --c3: #4ade80;
  --c4: #c084fc;
  --c5: #f472b6;
  --c6: #22d3ee;
}

/* couleur propre à chaque chapitre */
body.ch-suites { --accent: #4f46e5; }
body.ch-limites { --accent: #d9480f; }
body.ch-espace { --accent: #0f8a7e; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) body.ch-suites { --accent: #818cf8; --accent-ink: #0f1116; }
  :root:not([data-theme="light"]) body.ch-limites { --accent: #fb8a4c; --accent-ink: #0f1116; }
  :root:not([data-theme="light"]) body.ch-espace { --accent: #2dd4bf; --accent-ink: #0f1116; }
  :root:not([data-theme="light"]) body.ch-home { --accent: #818cf8; --accent-ink: #0f1116; }
}
:root[data-theme="dark"] body.ch-suites { --accent: #818cf8; --accent-ink: #0f1116; }
:root[data-theme="dark"] body.ch-limites { --accent: #fb8a4c; --accent-ink: #0f1116; }
:root[data-theme="dark"] body.ch-espace { --accent: #2dd4bf; --accent-ink: #0f1116; }
:root[data-theme="dark"] body.ch-home { --accent: #818cf8; --accent-ink: #0f1116; }

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 74px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.62;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 .5em; }
p { margin: .55em 0; }
code, pre { font-family: var(--mono); font-size: .88em; }
.katex { font-size: 1.07em; }
.katex-display { margin: .7em 0; overflow-x: auto; overflow-y: hidden; padding: 2px 0; }

/* ---------- barre du haut ---------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand .logo {
  width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-size: 15px; font-weight: 800;
}
.topnav { display: flex; gap: 4px; margin-left: auto; flex-wrap: wrap; }
.topnav a {
  text-decoration: none; color: var(--muted); padding: 6px 12px; border-radius: 999px; font-size: 15px; font-weight: 600;
}
.topnav a:hover { background: var(--surface-2); color: var(--text); }
.topnav a.on { background: var(--accent); color: var(--accent-ink); }
.theme-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 999px; width: 36px; height: 36px; cursor: pointer; font-size: 16px; flex: none;
}

/* ---------- en-tête de chapitre ---------- */
.hero {
  max-width: 1180px; margin: 0 auto; padding: 34px 20px 10px;
}
.hero .kicker { color: var(--accent); font-weight: 700; letter-spacing: .06em; text-transform: uppercase; font-size: 13px; }
.hero h1 { font-size: clamp(28px, 4vw, 40px); margin: 6px 0 8px; }
.hero p { color: var(--muted); max-width: 760px; }

/* ---------- mise en page ---------- */
.layout {
  max-width: 1180px; margin: 0 auto; padding: 10px 20px 60px;
  display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 28px;
}
.toc {
  position: sticky; top: 74px; align-self: start;
  max-height: calc(100vh - 90px); overflow: auto;
  padding: 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
}
.toc h4 { font-size: 12px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); margin: 0 0 8px; }
.toc a {
  display: flex; gap: 8px; align-items: baseline; text-decoration: none; color: var(--text);
  padding: 6px 8px; border-radius: 8px; font-size: 14.5px; line-height: 1.35;
}
.toc a:hover { background: var(--surface-2); }
.toc a.active { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.toc .tid { font-weight: 700; color: var(--accent); min-width: 26px; font-size: 13px; }
.toc .tok { margin-left: auto; font-size: 12px; color: var(--ok); font-weight: 700; white-space: nowrap; }
.toc .prog { margin-top: 12px; font-size: 13px; color: var(--muted); }
.bar { height: 6px; border-radius: 6px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.bar > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .4s; }

main { min-width: 0; }

/* ---------- fiche ---------- */
.fiche {
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  box-shadow: var(--shadow); padding: 26px 30px 30px; margin-bottom: 34px;
}
.fiche-head { border-bottom: 1px dashed var(--border); padding-bottom: 12px; margin-bottom: 16px; }
.fiche-num {
  display: inline-block; font-size: 13px; font-weight: 800; letter-spacing: .05em; text-transform: uppercase;
  color: var(--accent-ink); background: var(--accent); padding: 3px 10px; border-radius: 999px;
}
.fiche-head h2 { font-size: 27px; margin: 10px 0 4px; }
.fiche-goal { color: var(--muted); margin: 0; font-size: 15.5px; }
.prog-tag { font-size: 12.5px; color: var(--muted); margin-top: 6px; }
.prog-tag b { color: var(--text); }

.bloc { margin-top: 24px; }
.bloc > h3 {
  display: flex; align-items: center; gap: 10px; font-size: 19px; margin-bottom: 12px;
}
.bloc > h3::before {
  content: attr(data-ico); display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px;
  background: color-mix(in srgb, var(--accent) 14%, transparent); font-size: 16px;
}

/* boîtes */
.box {
  --box: var(--b-def);
  border-left: 4px solid var(--box);
  background: color-mix(in srgb, var(--box) 7%, var(--surface));
  border-radius: 0 12px 12px 0;
  padding: 12px 16px; margin: 12px 0;
}
.box-t { font-weight: 700; color: var(--box); font-size: 14px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 2px; }
.box.def { --box: var(--b-def); }
.box.thm { --box: var(--b-thm); }
.box.warn { --box: var(--b-warn); }
.box.tip { --box: var(--b-tip); }
.box.ex { --box: var(--b-ex); }
.box.meth { --box: var(--b-meth); }
.box ul, .box ol { margin: .3em 0; padding-left: 1.3em; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }

/* tableau */
.tbl-wrap { overflow-x: auto; margin: 10px 0; }
table.t {
  border-collapse: collapse; width: 100%; font-size: 15.5px; background: var(--surface);
}
table.t th, table.t td { border: 1px solid var(--border); padding: 7px 10px; text-align: center; }
table.t th { background: var(--surface-2); font-weight: 700; }
table.t td.fi { color: var(--bad); font-weight: 700; }
table.t tr.hl td { background: color-mix(in srgb, var(--accent) 14%, transparent); }

/* étapes de méthode */
ol.steps { counter-reset: s; list-style: none; padding: 0; margin: 10px 0; }
ol.steps > li { counter-increment: s; position: relative; padding: 6px 0 6px 40px; }
ol.steps > li::before {
  content: counter(s); position: absolute; left: 0; top: 6px; width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: 14px;
  background: color-mix(in srgb, var(--b-meth) 16%, transparent); color: var(--b-meth);
}

/* démonstration repliable */
details.demo {
  border: 1px solid var(--border); border-radius: 12px; margin: 14px 0; background: var(--surface-2);
}
details.demo > summary {
  cursor: pointer; padding: 11px 16px; font-weight: 650; list-style: none; display: flex; gap: 10px; align-items: center;
}
details.demo > summary::-webkit-details-marker { display: none; }
details.demo > summary::before { content: "▸"; color: var(--accent); transition: transform .2s; }
details.demo[open] > summary::before { transform: rotate(90deg); }
details.demo > summary .badge {
  margin-left: auto; font-size: 11.5px; padding: 2px 8px; border-radius: 999px;
  background: color-mix(in srgb, var(--b-thm) 16%, transparent); color: var(--b-thm); font-weight: 700; white-space: nowrap;
}
details.demo > .demo-body { padding: 4px 18px 14px; border-top: 1px solid var(--border); }

/* code */
pre.code {
  background: #1e1f29; color: #e9e9f2; border-radius: 12px; padding: 14px 16px; overflow-x: auto; line-height: 1.5; margin: 10px 0;
}
pre.code .k { color: #ff79c6; } pre.code .c { color: #8b93b8; } pre.code .n { color: #bd93f9; } pre.code .f { color: #50fa7b; }

/* ---------- widgets ---------- */
.widget {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--plot-bg);
  padding: 12px; margin: 12px 0;
}
.widget canvas { display: block; width: 100%; border-radius: 10px; touch-action: none; }
.widget .w-title { font-weight: 700; font-size: 15px; margin: 0 0 8px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.widget .w-title small { font-weight: 500; color: var(--muted); }
.controls { display: flex; flex-wrap: wrap; gap: 10px 22px; margin-top: 10px; align-items: center; }
.ctl { display: flex; align-items: center; gap: 10px; font-size: 14.5px; min-width: 200px; flex: 1 1 220px; }
.ctl label { white-space: nowrap; color: var(--muted); font-weight: 600; min-width: 0; }
.ctl input[type=range] { flex: 1; accent-color: var(--accent); min-width: 90px; }
.ctl .val {
  font-family: var(--mono); font-size: 13.5px; background: var(--surface-2); border-radius: 6px; padding: 2px 8px; min-width: 52px; text-align: center;
}
.seg { display: inline-flex; flex-wrap: wrap; gap: 6px; }
.seg button, .btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px;
  padding: 6px 13px; font: inherit; font-size: 14px; cursor: pointer; font-weight: 600;
}
.seg button:hover, .btn:hover { border-color: var(--accent); }
.seg button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.chk { display: inline-flex; align-items: center; gap: 6px; font-size: 14.5px; cursor: pointer; color: var(--muted); font-weight: 600; }
.chk input { accent-color: var(--accent); width: 16px; height: 16px; }
.readout {
  margin-top: 10px; padding: 10px 14px; border-radius: 10px; background: var(--surface-2); font-size: 15px;
  min-height: 44px;
}
.readout .good { color: var(--ok); font-weight: 700; }
.readout .bad { color: var(--bad); font-weight: 700; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13.5px; color: var(--muted); margin-top: 8px; }
.legend > span::before { content: ""; display: inline-block; width: 14px; height: 4px; border-radius: 2px; background: var(--lc); margin-right: 6px; vertical-align: middle; }
.hint-3d { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.cv-wrap { position: relative; }
.cv-tools { position: absolute; right: 8px; top: 8px; display: flex; gap: 6px; }
.cv-tools button {
  width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); cursor: pointer; font-size: 15px; opacity: .9;
}
.cv-tools button.on { background: var(--accent); color: var(--accent-ink); }
.mini-tbl { font-family: var(--mono); font-size: 13px; }
.mini-tbl td, .mini-tbl th { padding: 3px 8px !important; }
.num-in {
  width: 80px; font: inherit; font-size: 14px; padding: 4px 8px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}

/* ---------- exercices ---------- */
.ex {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; margin: 14px 0; background: var(--surface);
  position: relative;
}
.ex.solved { border-color: color-mix(in srgb, var(--ok) 55%, var(--border)); }
.ex-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.ex-tag {
  font-size: 12px; font-weight: 800; letter-spacing: .05em; text-transform: uppercase; padding: 3px 9px; border-radius: 6px;
  background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent);
}
.ex-title { font-weight: 700; }
.ex-count { margin-left: auto; font-size: 12.5px; color: var(--ok); font-weight: 700; }
.ex-q { margin: 6px 0 10px; }
.ex-fields { display: flex; flex-direction: column; gap: 10px; margin: 8px 0; }
.field { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; }
.field > .lbl { min-width: 0; }
.field input[type=text] {
  font: inherit; font-size: 15.5px; padding: 6px 10px; border-radius: 9px; border: 1.5px solid var(--border);
  background: var(--surface); color: var(--text); width: 150px;
}
.field input[type=text]:focus { outline: none; border-color: var(--accent); }
.field input.small { width: 70px; }
.field.ok input, .field.ok .opt.sel { border-color: var(--ok) !important; background: color-mix(in srgb, var(--ok) 10%, var(--surface)); }
.field.ko input, .field.ko .opt.sel { border-color: var(--bad) !important; background: color-mix(in srgb, var(--bad) 8%, var(--surface)); }
.field .mark { font-weight: 800; }
.field.ok .mark::after { content: "✓"; color: var(--ok); }
.field.ko .mark::after { content: "✗"; color: var(--bad); }
.limkeys { display: inline-flex; gap: 4px; }
.limkeys button {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); border-radius: 7px; padding: 3px 9px; cursor: pointer; font: inherit; font-size: 14px;
}
.opts { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
.opt {
  border: 1.5px solid var(--border); background: var(--surface); color: var(--text); border-radius: 10px; padding: 7px 14px;
  cursor: pointer; font: inherit; font-size: 15px; text-align: left;
}
.opt:hover { border-color: var(--accent); }
.opt.sel { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface)); }
.opt.right { border-color: var(--ok) !important; }
.ex-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.ex-fb { margin-top: 10px; font-weight: 650; min-height: 1em; }
.ex-fb.good { color: var(--ok); }
.ex-fb.bad { color: var(--bad); }
.ex-hint, .ex-sol { margin-top: 10px; padding: 10px 14px; border-radius: 10px; }
.ex-hint { background: color-mix(in srgb, var(--b-tip) 9%, var(--surface)); border-left: 4px solid var(--b-tip); }
.ex-sol { background: var(--surface-2); border-left: 4px solid var(--b-thm); }
.ex-sol .box-t, .ex-hint .box-t { font-size: 12.5px; }
.ex-fig .widget { margin: 6px 0; }

/* ---------- accueil ---------- */
.home-wrap { max-width: 1180px; margin: 0 auto; padding: 10px 20px 60px; }
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin: 18px 0 30px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 20px 20px 18px;
  box-shadow: var(--shadow); text-decoration: none; color: var(--text); display: flex; flex-direction: column; gap: 8px;
  transition: transform .15s, border-color .15s;
}
.card:hover { transform: translateY(-3px); border-color: var(--cc); }
.card .cv { border-radius: 12px; background: var(--surface-2); height: 150px; width: 100%; display: block; }
.card h2 { font-size: 22px; margin: 6px 0 0; color: var(--cc); }
.card ul { margin: 0; padding-left: 1.1em; color: var(--muted); font-size: 14.5px; }
.card .go { margin-top: auto; font-weight: 700; color: var(--cc); }
.steps3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.step3 { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; }
.step3 b { display: block; margin-bottom: 4px; }
.note { font-size: 14px; color: var(--muted); }
.src { font-size: 13.5px; color: var(--muted); }
.src li { margin: 3px 0; }
.footer { text-align: center; color: var(--muted); font-size: 13px; padding: 26px 10px 40px; }

/* ---------- responsive ---------- */
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; gap: 14px; }
  .toc { position: static; max-height: none; display: flex; flex-wrap: nowrap; overflow-x: auto; gap: 4px; padding: 8px; }
  .toc h4, .toc .prog { display: none; }
  .toc a { white-space: nowrap; flex: none; }
  .cards { grid-template-columns: 1fr; }
  .steps3 { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  body { font-size: 16px; }
  .topbar { padding: 8px 16px; flex-wrap: wrap; }
  .brand .txt { display: none; }
  .topnav a { padding: 5px 9px; font-size: 14px; }
  .hero { padding: 22px 16px 6px; }
  .layout, .home-wrap { padding-left: 16px; padding-right: 16px; }
  .fiche { padding: 18px 16px 20px; border-radius: 16px; }
  .fiche-head h2 { font-size: 22px; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .ctl { min-width: 0; flex-basis: 100%; }
  .ex { padding: 14px; }
}

@media print {
  .topbar, .toc, .ex-actions, .cv-tools, .controls .btn { display: none !important; }
  .layout { display: block; }
  .fiche { box-shadow: none; break-inside: avoid-page; }
  details.demo > .demo-body { display: block; }
}
