:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --ink: #1c2330;
  --muted: #5c6675;
  --border: #d9dee6;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --ok-bg: #e5f6ec; --ok-ink: #157347; --ok-border: #9bd8b4;
  --ko-bg: #fdeaea; --ko-ink: #b02a2a; --ko-border: #eeb4b4;
  --warn-bg: #fff6e0; --warn-ink: #8a6100; --warn-border: #e6cf90;
  --tok-keyword: #0b57d0; --tok-function: #8250df; --tok-string: #0a7d33;
  --tok-number: #b5330a; --tok-comment: #6a737d; --tok-punct: #57606a;
  --mono: ui-monospace, "SFMono-Regular", "Cascadia Code", "Fira Code", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b; --card: #1b2029; --ink: #e7ebf1; --muted: #9aa4b2;
    --border: #2c333f; --accent: #5b8cff; --accent-ink: #0b0e13;
    --ok-bg: #14301f; --ok-ink: #63d495; --ok-border: #2c6b45;
    --ko-bg: #351a1a; --ko-ink: #f0908f; --ko-border: #6d3130;
    --warn-bg: #33290f; --warn-ink: #e6c471; --warn-border: #6a561f;
    --tok-keyword: #79b8ff; --tok-function: #d2a8ff; --tok-string: #7ee787;
    --tok-number: #ffab70; --tok-comment: #8b949e; --tok-punct: #b1bac4;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}

#app { max-width: 900px; margin: 0 auto; padding: 1.5rem 1rem 4rem; }

/* En-tête */
.app-header { border-bottom: 1px solid var(--border); padding-bottom: 1rem; margin-bottom: 1.5rem; }
.title-row { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.app-header h1 { font-size: 1.5rem; margin: 0; }
.progress {
  font-family: var(--mono); font-size: .9rem; color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  padding: .2rem .6rem; border-radius: 999px; white-space: nowrap;
}
.intro { color: var(--muted); margin: .6rem 0; }
.subject-pdf { margin: .6rem 0; }
.subject-pdf a {
  display: inline-block; text-decoration: none; font-size: .9rem;
  color: var(--accent); border: 1px solid var(--border); border-radius: 6px;
  padding: .35rem .7rem;
}
.subject-pdf a:hover { border-color: var(--accent); }
.hint { color: var(--muted); font-size: .85rem; margin: .4rem 0 0; }
.header-actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-top: .8rem; }
.name-input {
  font-family: var(--sans); font-size: .9rem; padding: .35rem .6rem;
  border: 1px solid var(--border); border-radius: 6px; background: var(--card); color: var(--ink);
  flex: 1 1 auto; min-width: 12rem;
}

/* Boutons */
.btn {
  font-family: var(--sans); font-size: .9rem; cursor: pointer;
  padding: .4rem .8rem; border-radius: 6px; border: 1px solid var(--border);
  background: var(--card); color: var(--ink);
}
.btn:hover { border-color: var(--accent); }
.btn-danger { color: var(--ko-ink); border-color: var(--ko-border); }
.btn-run { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn-run:hover { filter: brightness(1.05); }

/* Panneau schéma */
.schema-panel { margin-bottom: 1.5rem; border: 1px solid var(--border); border-radius: 8px; background: var(--card); }
.schema-panel summary { cursor: pointer; padding: .7rem 1rem; font-weight: 600; }
.schema-sql {
  margin: 0; padding: 1rem; overflow-x: auto; font-family: var(--mono); font-size: .82rem;
  border-top: 1px solid var(--border); white-space: pre; color: var(--muted);
}

/* Sections */
.section-title { font-size: 1.15rem; margin: 2rem 0 .8rem; padding-bottom: .3rem; border-bottom: 2px solid var(--border); }
.instruction { font-weight: 600; margin: 1rem 0 .5rem; }
.free-text { color: var(--muted); }

/* Carte question */
.question-card { background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 1rem; margin-bottom: 1.2rem; }
.q-head { display: flex; align-items: center; gap: .6rem; }
.q-num { font-weight: 700; font-size: 1.05rem; }
.q-diff { color: var(--warn-ink); font-size: .85rem; letter-spacing: .05em; }
.q-status { margin-left: auto; font-weight: 700; font-size: 1.1rem; color: transparent; }
.q-status.ok { color: var(--ok-ink); }
.q-statement { margin: .5rem 0; white-space: pre-line; }
.q-statement code { font-family: var(--mono); font-size: .9em; background: var(--bg); border: 1px solid var(--border); border-radius: 4px; padding: 0 .25em; }
.q-expected { font-style: italic; color: var(--accent); font-size: .9rem; margin: .2rem 0; }
.q-sameas { color: var(--muted); font-size: .85rem; margin: .2rem 0; }

/* Éditeur SQL : textarea transparent au-dessus d'un calque colorié.
   Les deux calques DOIVENT partager exactement la même typographie et le même
   modèle de boîte pour que le texte s'aligne au pixel. */
.q-editor { position: relative; }
.q-editor .q-input,
.q-editor .q-highlight {
  margin: 0;
  font-family: var(--mono);
  font-size: .9rem;
  line-height: 1.5;
  padding: .6rem;
  border: 1px solid transparent;
  border-radius: 6px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  tab-size: 4;
  /* Réserve la gouttière d'ascenseur des deux côtés pour que la largeur de texte
     reste identique que la barre de défilement soit affichée ou non (sinon le
     retour à la ligne du calque colorié diverge de celui du textarea). */
  scrollbar-gutter: stable;
}
.q-highlight {
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  background: var(--bg);
  color: var(--ink);
}
.q-highlight code { font: inherit; white-space: inherit; overflow-wrap: inherit; }
.q-input {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  min-height: 4.5rem;
  resize: vertical;
  background: transparent;
  color: transparent;
  -webkit-text-fill-color: transparent;
  caret-color: var(--ink);
  border-color: var(--border);
}
.q-input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); border-color: var(--accent); }

/* Couleurs des jetons SQL */
.tok-keyword  { color: var(--tok-keyword); font-weight: 600; }
.tok-function { color: var(--tok-function); }
.tok-string   { color: var(--tok-string); }
.tok-number   { color: var(--tok-number); }
.tok-comment  { color: var(--tok-comment); font-style: italic; }
.tok-punct    { color: var(--tok-punct); }

.q-actions { display: flex; align-items: center; gap: .7rem; margin: .6rem 0; }
.run-hint { color: var(--muted); font-size: .8rem; }

/* Table de résultat */
.table-wrap { overflow-x: auto; margin-top: .4rem; }
.result-table { border-collapse: collapse; font-family: var(--mono); font-size: .82rem; width: auto; min-width: 100%; }
.result-table th, .result-table td { border: 1px solid var(--border); padding: .3rem .6rem; text-align: left; white-space: nowrap; }
.result-table th { background: color-mix(in srgb, var(--accent) 10%, var(--card)); }
.cell-null span { color: var(--muted); text-decoration: line-through; font-style: italic; }
.table-more { color: var(--muted); font-size: .8rem; margin: .3rem 0 0; }
.sql-error { color: var(--ko-ink); background: var(--ko-bg); border: 1px solid var(--ko-border); border-radius: 6px; padding: .6rem .8rem; font-family: var(--mono); font-size: .85rem; white-space: pre-wrap; }

/* Feedback */
.feedback { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .6rem; }
.chip { font-size: .82rem; padding: .25rem .6rem; border-radius: 999px; border: 1px solid var(--border); }
.chip-ok { background: var(--ok-bg); color: var(--ok-ink); border-color: var(--ok-border); }
.chip-ko { background: var(--ko-bg); color: var(--ko-ink); border-color: var(--ko-border); }
.chip-warn { background: var(--warn-bg); color: var(--warn-ink); border-color: var(--warn-border); }
.chip-strong { font-weight: 700; }

.engine-status { text-align: center; color: var(--muted); font-size: .9rem; padding: 1rem; }
.fatal { color: var(--ko-ink); font-weight: 600; }

/* Questions de cours (mode algèbre) */
.question-theory { border-left: 3px solid var(--accent); }
.q-theory-note { color: var(--muted); font-size: .85rem; font-style: italic; margin: .4rem 0 0; }
.q-start { color: var(--muted); font-size: .85rem; margin: .2rem 0; }
.q-locked { color: var(--warn-ink); font-weight: 600; font-size: .9rem; margin: .2rem 0; }
.q-locked:empty { display: none; }
.btn-run:disabled { opacity: .5; cursor: not-allowed; filter: none; }
.session-steps { display: flex; flex-direction: column; gap: .35rem; }
.session-step { font-family: var(--mono); font-size: .8rem; color: var(--muted); white-space: pre-wrap; overflow-wrap: anywhere; }

/* Schéma relationnel (mode algèbre) */
.schema-rel { padding: .5rem 1rem 1rem; border-top: 1px solid var(--border); }
.schema-rel-line { font-family: var(--mono); font-size: .9rem; margin: .35rem 0; }
.schema-rel-line u { text-decoration: underline; }
.schema-rel-line em { font-style: italic; }
.schema-rel-line strong { font-weight: 700; }

/* Encart « SQL généré » (option enseignant ?sql, mode algèbre) */
.sql-generated { margin-top: .6rem; border: 1px dashed var(--border); border-radius: 6px; }
.sql-generated summary { cursor: pointer; padding: .3rem .6rem; font-size: .8rem; color: var(--muted); }
.sql-generated-pre { margin: 0; padding: .6rem; overflow-x: auto; font-family: var(--mono); font-size: .78rem; border-top: 1px dashed var(--border); white-space: pre-wrap; color: var(--muted); }

/* Rappel de syntaxe (mode algèbre) */
.syntax-body { padding: .5rem 1rem 1rem; border-top: 1px solid var(--border); font-size: .88rem; }
.syntax-body p { margin: .5rem 0; }
.syntax-body pre { margin: .5rem 0; padding: .6rem; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; font-family: var(--mono); font-size: .82rem; overflow-x: auto; }
.syntax-body code { font-family: var(--mono); font-size: .85em; background: var(--bg); padding: .05rem .3rem; border-radius: 4px; }
.syntax-table { border-collapse: collapse; width: 100%; margin: .5rem 0; }
.syntax-table th, .syntax-table td { border: 1px solid var(--border); padding: .3rem .6rem; text-align: left; font-family: var(--mono); font-size: .8rem; }
.syntax-table th { background: color-mix(in srgb, var(--accent) 10%, var(--card)); }
.syntax-table td:first-child { font-weight: 600; }
.syntax-note { color: var(--muted); }

/* Section « Comment fonctionne cette page ? » + terrain d'essai */
.howto-body { padding: .5rem 1rem 1rem; border-top: 1px solid var(--border); }
.howto-text p { margin: .5rem 0; font-size: .9rem; }
.howto-text code { font-family: var(--mono); font-size: .85em; background: var(--bg); padding: .05rem .3rem; border-radius: 4px; }
.howto-pg-title { font-weight: 700; margin: 1.1rem 0 .2rem; }
.howto-pg-sub { color: var(--muted); font-size: .85rem; margin: 0 0 .5rem; }

/* Popup d'autocomplétion (positionnée en absolu sur <body>, au niveau du curseur) */
.ac-popup {
  position: absolute; z-index: 50; margin: 0; padding: .2rem; list-style: none;
  min-width: 12rem; max-width: 22rem; max-height: 14rem; overflow-y: auto;
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .18); font-family: var(--mono); font-size: .85rem;
}
.ac-item {
  display: flex; align-items: baseline; justify-content: space-between; gap: .8rem;
  padding: .25rem .5rem; border-radius: 5px; cursor: pointer; white-space: nowrap;
}
.ac-item:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.ac-active { background: var(--accent); color: var(--accent-ink); }
.ac-active .ac-kind { color: var(--accent-ink); opacity: .8; }
.ac-label { overflow: hidden; text-overflow: ellipsis; }
.ac-kind { font-size: .72rem; color: var(--muted); font-family: system-ui, sans-serif; }
.ac-kind-table, .ac-kind-rel { color: #2f855a; }
.ac-kind-col { color: var(--accent); }
.ac-kind-op { color: #b7791f; }
@media (prefers-color-scheme: dark) { .ac-kind-table, .ac-kind-rel { color: #68d391; } .ac-kind-op { color: #f6ad55; } }
