:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273548;
  --accent: #38bdf8;
  --accent-2: #fbbf24;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --border: #334155;
  --ok: #4ade80;
  --ko: #f87171;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Header ---- */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 16px; background: var(--panel);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 28px; }
h1 { font-size: 20px; font-weight: 600; }
.subtitle { font-size: 12px; color: var(--muted); }
nav { display: flex; gap: 8px; }
.nav-btn {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 14px; font-size: 14px; cursor: pointer;
}
.nav-btn:hover { border-color: var(--accent); }
.nav-btn.active { background: var(--accent); color: #082f49; font-weight: 600; }
a.nav-btn { text-decoration: none; display: inline-block; }

/* ---- Layout ---- */
.layout { display: flex; flex: 1; min-height: 0; position: relative; }
#sidebar {
  position: absolute; left: 0; top: 0; bottom: 0;
  width: min(360px, 92vw);
  background: var(--panel); border-right: 1px solid var(--border);
  overflow-y: auto; z-index: 20;
  transition: transform .25s ease;
  box-shadow: 4px 0 24px rgba(0,0,0,.45);
}
#sidebar.hidden { transform: translateX(-106%); box-shadow: none; }
.sidebar-head { padding: 14px 16px 8px; }
.sidebar-head h2 { font-size: 16px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

.activity {
  margin: 8px 12px; border: 1px solid var(--border);
  border-radius: 10px; background: var(--panel-2); overflow: hidden;
}
.activity > button.head {
  width: 100%; text-align: left; background: none; border: none;
  color: var(--text); padding: 10px 12px; cursor: pointer;
  display: flex; align-items: center; gap: 10px;
}
.activity > button.head:hover { background: rgba(56,189,248,.08); }
.activity.open > button.head { border-bottom: 1px solid var(--border); }
.tp-num {
  background: var(--accent); color: #082f49; font-weight: 700;
  font-size: 12px; border-radius: 6px; padding: 3px 7px; flex-shrink: 0;
}
.tp-title { flex: 1; font-size: 14px; font-weight: 600; }
.tp-level { font-size: 10px; color: var(--muted); border: 1px solid var(--border);
  border-radius: 4px; padding: 1px 6px; white-space: nowrap; }
.activity .body { display: none; padding: 10px 14px 14px; font-size: 13px; line-height: 1.5; }
.activity.open .body { display: block; }
.body h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--accent); margin: 10px 0 4px; }
.body h3:first-child { margin-top: 0; }
.body ol { padding-left: 18px; }
.body li { margin: 3px 0; }
.body .question {
  background: rgba(251,191,36,.08); border-left: 3px solid var(--accent-2);
  padding: 5px 8px; margin: 4px 0; border-radius: 0 6px 6px 0;
}

/* ---- Main ---- */
#main { flex: 1; min-width: 0; display: flex; flex-direction: column;
  overflow-y: auto; padding-bottom: 12px; }

#controls {
  padding: 10px 18px; background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex; flex-wrap: wrap; gap: 10px 30px; flex-shrink: 0;
}
.step-title { display: flex; align-items: center; gap: 8px; font-size: 13px;
  font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.step-num {
  background: var(--accent); color: #082f49; border-radius: 50%;
  width: 20px; height: 20px; display: inline-flex;
  align-items: center; justify-content: center; font-size: 12px; font-weight: 700;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--panel-2); border: 2px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 6px 13px; font-size: 13.5px; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.chip:hover:not(:disabled) { border-color: var(--accent); }
.chip.selected { border-color: var(--accent); background: rgba(56,189,248,.15); }
.chip:disabled { opacity: .5; cursor: default; }
.chip.unit { font-family: Consolas, monospace; padding: 5px 11px; }
.chip.unit.tgt.selected { border-color: var(--accent-2); background: rgba(251,191,36,.14); }
.units-rows { display: flex; flex-direction: column; gap: 5px; }
.urow { display: flex; align-items: center; gap: 8px; }
.urow-lbl { font-size: 12px; color: var(--muted); width: 40px; }

/* ---- Scène : question + tableau ---- */
#stage { flex: 1; padding: 14px 18px 0; }

#question {
  display: inline-block;
  font-size: 19px; font-weight: 700; margin-bottom: 12px;
  background: var(--panel); border: 2px solid var(--accent-2);
  border-radius: 10px; padding: 8px 18px;
}
#question .q-val { color: var(--accent); font-family: Consolas, monospace; }
#question .q-hole { color: var(--accent-2); }
#question.hidden, .hidden { display: none !important; }

/* ---- Le tableau de conversion ---- */
#table-wrap { overflow-x: auto; }
table.conv {
  border-collapse: collapse;
  background: var(--panel);
  border-radius: 10px; overflow: hidden;
}
table.conv th {
  background: var(--panel-2); color: var(--accent);
  font-family: Consolas, monospace; font-size: 15px; font-weight: 700;
  padding: 8px 4px; border: 1px solid var(--border); min-width: 58px; text-align: center;
}
table.conv th.col-src { border-bottom: 3px solid var(--accent); }
table.conv th.col-tgt { border-bottom: 3px solid var(--accent-2); }
table.conv td.rowlbl {
  background: var(--panel-2); border: 1px solid var(--border);
  font-size: 12px; color: var(--muted); padding: 6px 10px;
  white-space: nowrap; text-align: right;
}
table.conv td.cell {
  border: 1px solid var(--border); border-left-style: dotted;
  height: 42px; width: 30px; min-width: 30px;
  text-align: center; font-size: 20px; font-family: Consolas, monospace;
  position: relative; background: rgba(15,23,42,.4);
}
table.conv td.cell.grp { border-left: 2px solid var(--border); }
table.conv th {
  min-width: 0; border-left: 2px solid var(--border);
}
td.cell .comma {
  position: absolute; right: -4px; bottom: 2px;
  font-size: 22px; font-weight: 700; color: var(--accent-2);
}
td.cell.filled { background: rgba(56,189,248,.07); }
td.cell.ans-cursor { outline: 2px dashed var(--accent); outline-offset: -2px; }
table.conv.ok-row td.cell { background: rgba(74,222,128,.12); }
table.conv.ko-row td.cell { background: rgba(248,113,113,.10); }

#jump {
  margin-top: 12px; font-size: 15px; font-weight: 600;
  color: var(--accent-2);
  background: rgba(251,191,36,.08); border: 1px solid var(--accent-2);
  border-radius: 10px; padding: 8px 14px; display: inline-block;
}

#exo-btns { margin-top: 12px; display: flex; gap: 8px; align-items: center; }
#score { font-size: 15px; font-weight: 700; color: var(--accent); margin-left: 10px; }

#feedback {
  margin: 12px 18px 0; border-radius: 10px; padding: 10px 16px;
  font-size: 15px; font-weight: 600; border: 2px solid var(--border);
  background: var(--panel);
}
#feedback.ok { border-color: var(--ok); color: var(--ok); }
#feedback.ko { border-color: var(--ko); color: var(--ko); }

/* ---- Pavé numérique ---- */
#keypad {
  display: grid; grid-template-columns: repeat(3, 64px); gap: 8px;
  padding: 14px 18px; align-self: flex-start;
}
#keypad button {
  height: 48px; font-size: 20px; font-weight: 700;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; cursor: pointer;
}
#keypad button:hover { border-color: var(--accent); }
#keypad button[data-k="del"] { color: var(--ko); }

@media (max-width: 760px) {
  #keypad { grid-template-columns: repeat(3, 1fr); align-self: stretch; }
}

/* Note enseignant dans les fiches TP */
.prof-note { margin-top: 12px; background: rgba(245,158,11,.10);
  border-left: 3px solid #f59e0b; border-radius: 8px; padding: 8px 10px; }
.prof-note h3 { color: #fbbf24; }
.prof-note p { font-size: 12.5px; color: var(--muted); margin-top: 4px; line-height: 1.45; }

/* ---- Chiffre des unités, zéros ajoutés ---- */
td.cell.unit-digit { color: #fbbf24; font-weight: 800; background: rgba(251,191,36,.16); }
td.cell.added { color: #94a3b8; font-style: italic; outline: 2px dashed #94a3b8; outline-offset: -5px; }
#table-wrap.walk-tgt th.col-tgt { animation: walkPulse 1s ease-in-out infinite alternate; }
@keyframes walkPulse { from { background: var(--panel-2); } to { background: rgba(251,191,36,.45); color: #fff; } }
@media (prefers-reduced-motion: reduce) { #table-wrap.walk-tgt th.col-tgt { animation: none; background: rgba(251,191,36,.45); } }

/* ---- Pas à pas ---- */
#walk-box { margin-top: 12px; background: var(--panel); border: 2px solid var(--accent); border-radius: 12px;
  padding: 10px 14px; max-width: 760px; }
#walk-text { font-size: 15px; line-height: 1.5; margin: 4px 0 8px; }
#walk-dots { display: flex; gap: 6px; }
#walk-dots span { width: 26px; height: 6px; border-radius: 3px; background: var(--border); }
#walk-dots span.on { background: var(--accent); }
.walk-btns { display: flex; gap: 8px; }

/* ---- Défi ---- */
#defi-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; margin-bottom: 12px;
  background: var(--panel); border: 2px solid var(--accent-2); border-radius: 12px; padding: 8px 14px; }
#defi-time { font-size: 20px; font-weight: 800; color: var(--accent-2); font-family: Consolas, monospace; }
#defi-score { font-size: 17px; font-weight: 700; color: var(--ok); }
#defi-best { font-size: 14px; color: var(--muted); }
.defi-hint { font-size: 13px; color: var(--muted); }
#defi-bar.running .defi-hint { display: none; }
