/* ==============================================================================
   Styles du site : thème clair / sombre, pages de lecture, exercices, terminal, adaptation téléphone.
   ============================================================================== */

/* --- Couleurs et tailles (une seule source, reprise par l'éditeur via var()) --- */
:root {
  color-scheme: light;
  --bg: #f8f7f4;
  --surface: #ffffff;
  --surface-2: #f1efea;
  --text: #1d1b18;
  --text-2: #4f4a43;
  --muted: #8b857c;
  --border: #e4e0d8;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-text: #ffffff;
  --accent-soft: #e8efff;
  --ok: #15803d;
  --ok-soft: #e7f5ec;
  --ko: #b91c1c;
  --ko-soft: #fdeceb;
  --tf: #6e3fbf;
  --tf-soft: #f2ecfb;
  --warn: #a15c07;
  --warn-soft: #fcf3e3;
  --note: #0f6f86;
  --note-soft: #e5f3f6;

  --code-bg: #f6f4ef;
  --active-line: rgba(0, 0, 0, 0.035);
  --selection: #cddcfc;
  --match: #dde6fb;
  --syn-keyword: #a626a4;
  --syn-string: #3d8a3d;
  --syn-comment: #9a958c;
  --syn-number: #b45309;
  --syn-function: #2563eb;
  --syn-def: #a86a12;
  --syn-operator: #0e7490;

  --term-bg: #17161b;
  --term-bar: #201f25;
  --term-text: #e9e7e3;
  --term-muted: #9b968f;
  --term-err: #ff8a80;
  --term-echo: #8fb8ff;
  --term-prompt: #7ddc8b;
  --term-info: #d8b678;

  --radius: 12px;
  --radius-sm: 8px;
  --header-h: 56px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --code-size: 14.5px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #111113;
    --surface: #18181b;
    --surface-2: #222226;
    --text: #ecebe8;
    --text-2: #bab6af;
    --muted: #858079;
    --border: #2d2d33;
    --accent: #6b9bff;
    --accent-hover: #8db2ff;
    --accent-text: #0d1424;
    --accent-soft: #1b2540;
    --ok: #5cc98a;
    --ok-soft: #142a1e;
    --ko: #ff8a80;
    --ko-soft: #321818;
    --tf: #b591f2;
    --tf-soft: #251c36;
    --warn: #e7b25e;
    --warn-soft: #2e2414;
    --note: #6cc4d8;
    --note-soft: #142a30;

    --code-bg: #1c1c20;
    --active-line: rgba(255, 255, 255, 0.04);
    --selection: #2f4373;
    --match: #2d3a5c;
    --syn-keyword: #c678dd;
    --syn-string: #98c379;
    --syn-comment: #7f848e;
    --syn-number: #d19a66;
    --syn-function: #61afef;
    --syn-def: #e5c07b;
    --syn-operator: #56b6c2;

    --term-bg: #0d0d10;
    --term-bar: #18181d;
  }
}

/* Clavier tactile : 16 px minimum, sinon l'iPhone zoome à chaque saisie */
@media (pointer: coarse) {
  :root { --code-size: 16px; }
}

/* --- Base --- */
*, *::before, *::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.65 var(--font);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); text-decoration: underline; text-underline-offset: 3px; }

h1, h2, h3 { line-height: 1.25; letter-spacing: -0.01em; }
h1 { font-size: 1.85rem; margin: 0.2em 0 0.6em; }
h2 { font-size: 1.25rem; margin: 2em 0 0.7em; }
h3 { font-size: 1.05rem; margin: 1.6em 0 0.5em; }

code { font-family: var(--mono); font-size: 0.9em; background: var(--surface-2); padding: 0.1em 0.35em; border-radius: 5px; }

button { font: inherit; color: inherit; }

.muted { color: var(--muted); }

.icon { display: inline-flex; width: 1.05em; height: 1.05em; flex: none; }
.icon svg { width: 100%; height: 100%; }

/* --- En-tête --- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; color: var(--text); white-space: nowrap; }
.brand:hover { color: var(--text); text-decoration: none; }
.brand-mark { font-family: var(--mono); font-size: 0.85em; color: var(--accent-text); background: var(--accent); padding: 2px 6px; border-radius: 6px; }
.brand-sub { color: var(--muted); font-weight: 500; }

.topbar { justify-content: flex-start; gap: 12px; }
.topbar nav { display: flex; gap: 4px; margin-left: auto; }
.topbar nav a { color: var(--text-2); padding: 6px 12px; border-radius: 999px; font-size: 0.95rem; }
.topbar nav a:hover { text-decoration: none; background: var(--surface-2); }
.topbar nav a.is-active { color: var(--text); background: var(--surface-2); font-weight: 550; }

/* Réglages : comparaisons Terraform et langue */
.topbar-controls { display: flex; align-items: center; gap: 8px; }
.tf-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}
.tf-toggle[aria-pressed="true"] { color: var(--tf); background: var(--tf-soft); border-color: transparent; }
.tf-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; opacity: 0.35; }
.tf-toggle[aria-pressed="true"] .tf-dot { opacity: 1; }
.tf-short { display: none; }
.lang-switch { display: inline-flex; padding: 2px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); }
.lang-btn { display: inline-flex; align-items: center; gap: 5px; height: 26px; padding: 0 8px; border: 0; border-radius: 999px; background: none; color: var(--muted); font-size: 0.78rem; font-weight: 600; cursor: pointer; }
.lang-btn[aria-pressed="true"] { background: var(--surface-2); color: var(--text); }
.flag { display: inline-flex; width: 18px; height: 12px; border-radius: 2px; overflow: hidden; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.12); }
.flag svg { width: 100%; height: 100%; }
.tf-toggle:focus-visible, .lang-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Masquer les comparaisons Terraform (réglage de l'en-tête) */
.hide-terraform .callout-terraform { display: none; }

@media (max-width: 640px) {
  .brand-sub, .tf-long, .lang-code { display: none; }
  .tf-short { display: inline; }
  .topbar { gap: 8px; }
  .topbar nav a { padding: 6px 8px; }
}
@media (max-width: 400px) {
  .brand-name { display: none; }
}

/* --- Mise en page --- */
.page { max-width: 760px; margin: 0 auto; padding: 28px 16px 56px; }
.page-wide { max-width: 1440px; margin: 0 auto; padding: 16px; }

.crumbs { display: flex; flex-wrap: wrap; gap: 6px; font-size: 0.9rem; color: var(--muted); }
.crumbs > * + *::before { content: "›"; display: inline-block; margin-right: 6px; color: var(--muted); }

.links { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px; font-size: 0.95rem; }
.links a { display: inline-flex; align-items: center; gap: 4px; }
.links .icon { width: 0.85em; height: 0.85em; }

.site-footer { max-width: 760px; margin: 0 auto; padding: 24px 16px 40px; color: var(--muted); font-size: 0.82rem; text-align: center; }
.site-footer a { color: var(--text-2); text-decoration: underline; text-underline-offset: 2px; }

/* --- Boutons --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { text-decoration: none; color: var(--text); border-color: var(--muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--accent-text); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); border-color: transparent; }
.btn-sm { min-height: 32px; padding: 0 12px; font-size: 0.9rem; }
.btn:focus-visible, .wb-tab-name:focus-visible, .term-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.file-chip {
  display: inline-block;
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--text-2);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 1px 8px;
  border-radius: 6px;
}

.badge-ok {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  vertical-align: middle;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ok);
  background: var(--ok-soft);
  padding: 2px 10px;
  border-radius: 999px;
}

/* --- Accueil --- */
.hero { padding-top: 48px; padding-bottom: 8px; }
.eyebrow { margin: 0; color: var(--accent); font-weight: 600; font-size: 0.9rem; }
.hero h1 { font-size: clamp(1.9rem, 5vw, 2.6rem); margin-top: 0.3em; }
.lead { font-size: 1.1rem; color: var(--text-2); max-width: 62ch; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 24px; }

.weeks, .items { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }

.week-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
}
a.week-card:hover { text-decoration: none; color: var(--text); border-color: var(--accent); }
a.week-card > .icon { color: var(--muted); }
.week-card.is-soon { opacity: 0.55; }
.week-num {
  flex: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-family: var(--mono);
}
.is-soon .week-num { background: var(--surface-2); color: var(--muted); }
.week-body { flex: 1; display: grid; gap: 2px; min-width: 0; }
.week-body small { color: var(--muted); font-size: 0.85rem; }

.progress { display: block; height: 4px; margin-top: 6px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--ok); border-radius: 4px; }

/* --- Listes de leçons / exercices --- */
.item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
}
.item:hover { text-decoration: none; color: var(--text); border-color: var(--accent); }
.item-num { flex: none; width: 2ch; text-align: right; font-family: var(--mono); color: var(--muted); font-size: 0.9rem; }
.item-title { flex: 1; min-width: 0; }
.done-mark { display: inline-flex; color: var(--ok); }

/* --- Leçon --- */
.prose > * + * { margin-top: 1em; }
.prose p, .prose ul, .prose ol { margin: 0; }
.prose > :is(p, ul, ol) + :is(p, ul, ol) { margin-top: 0.8em; }
.prose ul, .prose ol { padding-left: 1.3em; }
.prose li + li { margin-top: 0.3em; }
.lesson h1 { margin-top: 0.4em; }

.callout {
  position: relative;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  border-left: 3px solid var(--note);
  background: var(--note-soft);
}
.callout > * + * { margin-top: 0.6em; }
.callout p { margin: 0; }
.callout-label { display: block; font-size: 0.75rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: var(--note); }
.callout-terraform { border-color: var(--tf); background: var(--tf-soft); }
.callout-terraform .callout-label { color: var(--tf); }
.callout-piege { border-color: var(--warn); background: var(--warn-soft); }
.callout-piege .callout-label { color: var(--warn); }
.callout-astuce { border-color: var(--ok); background: var(--ok-soft); }
.callout-astuce .callout-label { color: var(--ok); }

.static-code {
  margin: 0;
  padding: 12px 16px;
  overflow-x: auto;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: var(--code-size) / 1.6 var(--mono);
}
.static-code code { background: none; padding: 0; font-size: inherit; }

.codeblock { border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; background: var(--code-bg); }
.codeblock-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 8px 6px 12px; border-bottom: 1px solid var(--border); background: var(--surface); }
.codeblock-actions { display: flex; gap: 6px; }
.codeblock-editor .cm-editor { max-height: 480px; }
.codeblock-output .term { border-radius: 0; border: none; border-top: 1px solid var(--border); }

.lesson-end { display: flex; justify-content: center; margin-top: 48px; }
.lesson-done { display: flex; align-items: center; gap: 10px; margin: 0; }
.lesson-done .badge-ok { font-size: 0.9rem; padding: 6px 14px; }
.pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 28px; padding-top: 20px; border-top: 1px solid var(--border); }

/* --- Terminal --- */
.term {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  background: var(--term-bg);
  color: var(--term-text);
  font: 13.5px/1.55 var(--mono);
}
.term-bar { display: flex; align-items: center; gap: 10px; padding: 4px 6px 4px 12px; background: var(--term-bar); font-family: var(--font); font-size: 0.8rem; }
.term-label { color: var(--term-muted); font-weight: 600; }
.term-status { flex: 1; color: var(--term-info); }
.term-actions { display: flex; gap: 2px; }
.term-btn { border: 0; background: transparent; color: var(--term-muted); padding: 4px 8px; border-radius: 6px; cursor: pointer; font-size: 0.78rem; font-family: var(--mono); }
.term-btn:hover { background: rgba(255, 255, 255, 0.08); color: var(--term-text); }
.term-screen { flex: 1; min-height: 0; overflow: auto; padding: 10px 12px 14px; cursor: text; }
.term-mini .term-screen { max-height: 300px; }
.term-line { min-height: 1.55em; white-space: pre-wrap; overflow-wrap: anywhere; }
.term-line:has(.term-input) { display: flex; flex-wrap: wrap; }
.term-input {
  flex: 1 1 8ch;
  min-width: 8ch;
  padding: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--term-text);
  caret-color: var(--term-prompt);
  font: inherit;
}
@media (pointer: coarse) {
  .term { font-size: 15px; }
  .term-input { font-size: 16px; }
}
.t-stderr { color: var(--term-err); }
.t-echo { color: var(--term-echo); }
.t-prompt { color: var(--term-prompt); }
.t-info { color: var(--term-info); }

/* --- Poste de travail (exercice, terminal libre) --- */
.workbench {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.wb-head { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.wb-tabs { flex: 1; display: flex; align-items: center; gap: 2px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.wb-tab { display: inline-flex; align-items: center; border-radius: 6px; flex: none; }
.wb-tab.is-active { background: var(--surface-2); }
.wb-tab-name { border: 0; background: none; padding: 5px 10px; font-family: var(--mono); font-size: 0.85rem; color: var(--text-2); cursor: pointer; }
.wb-tab.is-active .wb-tab-name { color: var(--text); }
.wb-tab-close, .wb-tab-add { display: inline-grid; place-items: center; border: 0; background: none; padding: 4px; margin-right: 2px; border-radius: 5px; color: var(--muted); cursor: pointer; }
.wb-tab-close .icon { width: 0.8em; height: 0.8em; }
.wb-tab-close:hover, .wb-tab-add:hover { background: var(--border); color: var(--text); }
.wb-buttons { display: flex; gap: 6px; flex: none; }
.wb-editor { flex: 1 1 auto; min-height: 0; display: flex; background: var(--code-bg); }
.wb-editor .cm-editor { flex: 1; min-width: 0; }
.wb-bottom { display: flex; flex-direction: column; flex: 0 0 40%; min-height: 0; border-top: 1px solid var(--border); background: var(--term-bg); }
.wb-bottom-head { display: flex; flex-wrap: wrap-reverse; align-items: flex-end; gap: 0 8px; padding: 4px 6px 0; background: var(--term-bar); }
.wb-panel-tabs { display: flex; gap: 2px; }
.wb-terminal-bar { flex: 1; display: flex; justify-content: flex-end; align-items: center; gap: 8px; min-width: 0; padding-bottom: 4px; font-size: 0.8rem; }
.term-status { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.wb-terminal-bar .term-status { flex: 1; text-align: right; }
.term-btn { white-space: nowrap; }
.prose code { white-space: break-spaces; }
.wb-panel-tab { border: 0; background: none; padding: 6px 12px; border-radius: 6px 6px 0 0; color: var(--term-muted); font-size: 0.85rem; cursor: pointer; }
.wb-panel-tab.is-active { background: var(--term-bg); color: var(--term-text); }
.wb-panel { flex: 1; min-height: 0; overflow: auto; }
.wb-panel[hidden] { display: none; }

.keybar { display: none; }
@media (pointer: coarse) {
  .keybar { display: flex; gap: 4px; padding: 6px; overflow-x: auto; border-top: 1px solid var(--border); background: var(--surface); scrollbar-width: none; }
  .keybar-key { flex: none; min-width: 40px; height: 38px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); font-family: var(--mono); font-size: 16px; }
}

/* --- Exercice --- */
.brief { min-width: 0; }
.brief h1 { font-size: 1.6rem; margin-top: 0.3em; }
.brief .prose { margin-top: 18px; }
.fold { margin-top: 18px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); }
.fold summary { padding: 10px 14px; cursor: pointer; font-weight: 600; }
.fold > :not(summary) { padding: 0 14px 12px; }
.tests-intro { margin: 0 0 10px; font-size: 0.9rem; color: var(--text-2); }
.tests { display: grid; gap: 8px; margin: 0; padding-left: 1.4em; }
.tests li::marker { color: var(--muted); font-size: 0.9em; }
.fold-solution summary { color: var(--text-2); }
.tests li { display: list-item; line-height: 2; }
.tests li > * + * { margin-left: 8px; }
.tests-arrow { color: var(--muted); }
.related { font-size: 0.95rem; color: var(--text-2); margin-top: 18px; }

.checks { padding: 14px 16px; background: var(--surface); color: var(--text); min-height: 100%; }
.checks-summary { margin: 0 0 12px; font-weight: 650; }
.checks-summary.is-ok { color: var(--ok); }
.checks-summary.is-ko { color: var(--ko); }
.check-list { list-style: none; margin: 0 0 14px; padding: 0; display: grid; gap: 10px; }
.check { display: flex; gap: 10px; padding: 10px 12px; border-radius: var(--radius-sm); background: var(--ok-soft); }
.check.is-ko { background: var(--ko-soft); }
.check p { margin: 0; }
.check-icon { font-weight: 700; color: var(--ok); }
.check.is-ko .check-icon { color: var(--ko); }
.check-reason { margin-top: 6px !important; font-size: 0.9rem; color: var(--text-2); }
.check-detail { margin: 6px 0 0; padding: 8px 10px; max-height: 180px; overflow: auto; white-space: pre-wrap; background: var(--surface); border-radius: 6px; font: 0.85rem/1.5 var(--mono); }

/* Grand écran : consigne à gauche, poste de travail à droite, sur toute la hauteur */
@media (min-width: 980px) {
  .exercise {
    display: grid;
    grid-template-columns: minmax(340px, 5fr) 7fr;
    gap: 24px;
    height: calc(100dvh - var(--header-h));
  }
  .brief { overflow-y: auto; padding: 8px 8px 24px 0; }
  .work { min-height: 0; display: flex; }
  .work .workbench { flex: 1; }
  .sandbox { display: flex; flex-direction: column; height: calc(100dvh - var(--header-h)); }
  .sandbox .workbench { flex: 1; }
}

/* Téléphone / tablette : tout s'empile, l'éditeur et le terminal ont une hauteur fixe */
@media (max-width: 979px) {
  .work { margin-top: 24px; }
  .wb-editor { height: 48vh; min-height: 240px; }
  .wb-bottom { flex-basis: auto; height: 320px; }
  .wb-head { flex-wrap: wrap; }
  .wb-buttons { margin-left: auto; }
}

.sandbox-head h1 { font-size: 1.5rem; margin: 0.2em 0 0.1em; }
.sandbox-head p { margin: 0 0 14px; font-size: 0.95rem; }

@media (max-width: 560px) {
  h1 { font-size: 1.55rem; }
  .page { padding-top: 20px; }
  .pager .btn { flex: 1; }
}
