:root {
  --bg: #f4f1ec;
  --surface: #fffdf9;
  --surface-2: #f0ebe3;
  --fg: #2a241d;
  --muted: #776d61;
  --line: #e2d9cc;
  --accent: #9a5b20;
  --accent-soft: #f3e3cf;
  --good: #2f7d4f;
  --good-soft: #e1f2e6;
  --bad: #b23a3a;
  --bad-soft: #f8e1e1;
  --light-sq: #f0d9b5;
  --dark-sq: #b58863;
  --last: rgb(205 210 106 / 0.75);
  --select: rgb(20 85 30 / 0.5);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #181614;
    --surface: #221f1c;
    --surface-2: #2b2723;
    --fg: #eee7dd;
    --muted: #a69b8e;
    --line: #3a3530;
    --accent: #e0a464;
    --accent-soft: #3d2e1f;
    --good: #7fd19c;
    --good-soft: #1f3527;
    --bad: #f08a8a;
    --bad-soft: #3d2222;
  }
}
:root[data-theme="dark"] {
  --bg: #181614; --surface: #221f1c; --surface-2: #2b2723; --fg: #eee7dd; --muted: #a69b8e; --line: #3a3530;
  --accent: #e0a464; --accent-soft: #3d2e1f; --good: #7fd19c; --good-soft: #1f3527; --bad: #f08a8a; --bad-soft: #3d2222;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif;
}
button {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.4; cursor: default; }
button:focus-visible, input:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
a { color: var(--accent); }

/* ---------- En-tête ---------- */
.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px 20px 8px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: var(--dark-sq);
  color: #fff;
  font-size: 26px;
}
.top h1 { margin: 0; font: 600 22px/1.2 var(--serif); }
.top p { margin: 0; color: var(--muted); font-size: 14px; }

/* ---------- Mise en page ---------- */
.layout {
  display: grid;
  grid-template-columns: 270px minmax(0, 1fr) 360px;
  gap: 20px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 8px 20px 24px;
  align-items: start;
}

/* ---------- Bibliothèque ---------- */
.library {
  position: sticky;
  top: 12px;
  max-height: calc(100vh - 24px);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 4px 10px; border-radius: 999px; font-size: 13px; }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--surface); }
.gameList {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.gameCard {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface);
}
.gameCard.active { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.gameCard strong { display: block; font: 600 15px/1.3 var(--serif); }
.gameCard .meta { display: block; color: var(--muted); font-size: 13px; margin-top: 2px; }
.gameCard .tags { width: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 6px; font-size: 12px; }
.tag { padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.progress { margin-left: auto; color: var(--good); font-weight: 600; }

/* ---------- Échiquier ---------- */
.stage { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.gameHead { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.gameHead h2 { margin: 0; font: 600 24px/1.2 var(--serif); }
.players { margin: 2px 0 0; color: var(--muted); }
.badge {
  flex: none;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
}
.boardWrap { width: min(100%, 620px, calc(100vh - 260px)); min-width: 280px; margin: 0 auto; }
.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  aspect-ratio: 1;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.18);
  user-select: none;
  touch-action: manipulation;
}
.sq { position: relative; background: var(--light-sq); }
.sq.dark { background: var(--dark-sq); }
.sq.last::before, .sq.sel::before { content: ""; position: absolute; inset: 0; }
.sq.last::before { background: var(--last); }
.sq.sel::before { background: var(--select); }
.sq.check { background-image: radial-gradient(circle, rgb(255 30 30) 0%, rgb(230 20 20 / 0.6) 30%, transparent 70%); }
.sq.hint::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 4px #2b7de9;
  animation: pulse 1s ease-in-out infinite alternate;
}
.sq.wrong::before { content: ""; position: absolute; inset: 0; background: rgb(220 40 40 / 0.45); }
.sq.right::before { content: ""; position: absolute; inset: 0; background: rgb(40 170 80 / 0.5); }
.sq.target::after {
  content: "";
  position: absolute;
  left: 36%;
  top: 36%;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgb(20 85 30 / 0.45);
}
.sq.target.occupied::after {
  left: 0; top: 0; width: 100%; height: 100%;
  background: radial-gradient(transparent 62%, rgb(20 85 30 / 0.45) 63%);
}
.interactive .sq { cursor: pointer; }
.piece { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; pointer-events: none; }
.piece.moving { transition: transform 0.22s ease-out; z-index: 2; }
.coord {
  position: absolute;
  font-size: clamp(9px, 1.6vw, 12px);
  font-weight: 700;
  line-height: 1;
  z-index: 1;
  pointer-events: none;
}
.coord.rank { left: 3px; top: 3px; }
.coord.file { right: 3px; bottom: 2px; }
.sq .coord { color: var(--dark-sq); }
.sq.dark .coord { color: var(--light-sq); }
@keyframes pulse { from { opacity: 0.35; } to { opacity: 1; } }

.scrub { width: min(100%, 620px); margin: 0 auto; accent-color: var(--accent); }
.controls { display: flex; justify-content: center; align-items: center; flex-wrap: wrap; gap: 6px; }
.controls button { display: inline-grid; place-items: center; min-width: 44px; height: 40px; }
.controls .primary { background: var(--accent); border-color: var(--accent); color: var(--surface); }
.sep { width: 10px; }
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}
.toggle:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.toggle input { accent-color: var(--accent); }

/* ---------- Panneau ---------- */
.panel {
  position: sticky;
  top: 12px;
  max-height: calc(100vh - 24px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}
.comment, .trainBox, .lesson, .movesHead { flex: none; }
.comment {
  padding: 14px 16px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  min-height: 110px;
}
.comment .moveLabel { font: 600 18px/1.3 var(--serif); margin-bottom: 4px; }
.comment .nag { color: var(--accent); margin-left: 2px; }
.comment p { margin: 0; }
.comment .muted { color: var(--muted); }
.comment .result { margin-top: 8px; font-weight: 600; }

.trainBox {
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
}
.trainBox.good { background: var(--good-soft); border-color: var(--good); }
.trainBox.bad { background: var(--bad-soft); border-color: var(--bad); }
.trainBox p { margin: 0 0 8px; font-weight: 600; }
.trainBtns { display: flex; gap: 6px; }

.movesHead { margin: 4px 0 0; font-size: 14px; }
.movesHead .hint { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 6px; }
.dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); vertical-align: middle; }
.star { color: var(--accent); }
.moves {
  list-style: none;
  margin: 0;
  padding: 4px;
  overflow-y: auto;
  min-height: 120px;
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  display: grid;
  grid-template-columns: 2.6em 1fr 1fr;
  align-content: start;
  font-variant-numeric: tabular-nums;
}
.moves .num { padding: 3px 6px; color: var(--muted); text-align: right; }
.move {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border: 0;
  border-radius: 6px;
  background: none;
  text-align: left;
  font-weight: 500;
}
.move:hover { background: var(--surface-2); }
.move.current { background: var(--accent); color: var(--surface); }
.move.future { color: var(--muted); }
.move.hidden { visibility: hidden; }
.move .dot { flex: none; }
.move.current .dot { background: var(--surface); }
.move .nag { color: var(--accent); font-weight: 700; }
.move.current .nag, .move.current .star { color: var(--surface); }

.lesson {
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
}
.lesson summary { cursor: pointer; font-weight: 600; }
.lesson ul { margin: 8px 0 0; padding-left: 20px; }
.lesson li + li { margin-top: 4px; }

/* ---------- Navigation entre les pages ---------- */
.nav { display: flex; flex-wrap: wrap; gap: 6px; }
.nav a {
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: inherit;
  text-decoration: none;
  white-space: nowrap;
}
.nav a:hover { border-color: var(--accent); }
.nav a[aria-current="page"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

/* ---------- Analyse de partie ---------- */
.entry { max-width: 720px; margin: 0 auto; padding: 16px 20px 32px; }
[hidden] { display: none !important; }
.entry h2 { margin: 8px 0 4px; font: 600 24px/1.2 var(--serif); }
.muted { color: var(--muted); }
.small { margin: 0; font-size: 13px; }
.entry form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.field { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 14px; }
textarea, select, .field input {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-weight: 400;
  width: 100%;
  min-width: 0;
}
textarea { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; resize: vertical; }
textarea:focus-visible, select:focus-visible, .field input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.entryTools { display: flex; flex-wrap: wrap; gap: 6px; }
.fileBtn {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 7px 12px;
  cursor: pointer;
}
.fileBtn:hover { border-color: var(--accent); }
.fileBtn:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.fileBtn input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.entry form p { margin: 0; }
.notices { max-width: 1400px; margin: 0 auto; padding: 0 20px; }
.notices .error { margin: 8px 0; color: var(--bad); font-weight: 600; }
.entry .gameList { flex-direction: column; flex-wrap: nowrap; max-height: 340px; padding: 0; scroll-snap-type: none; }
.entry .gameList li { flex: none; }
.report .gameList { flex: none; max-height: 280px; }
.gameCard .failed { color: var(--bad); font-weight: 600; }
.gameCard .tags .progress { margin-left: 0; }
.entry .primary { align-self: flex-start; background: var(--accent); border-color: var(--accent); color: var(--surface); font-weight: 600; padding: 9px 18px; }
.progressBox { margin: 8px 0; display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.progressBox p { margin: 0; }
.progressBox progress { width: 100%; height: 10px; accent-color: var(--accent); }

.report { gap: 12px; overflow-y: auto; }
.scores { width: 100%; border-collapse: separate; border-spacing: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; font-variant-numeric: tabular-nums; }
.scores th, .scores td { padding: 5px 10px; text-align: right; font-weight: 500; }
.scores td { white-space: nowrap; }
.scores th[scope="row"] { text-align: left; color: var(--muted); font-weight: 400; }
.scores thead th { font-weight: 600; }
.report .lesson h3 { margin: 0; font-size: 15px; }
.report .lesson ul { font-size: 14px; }

.practice { display: flex; flex-direction: column; gap: 8px; }
.practiceBtns { display: flex; flex-direction: column; gap: 6px; }

/* ---------- Mes erreurs ---------- */
.drillHead { max-width: 1400px; margin: 0 auto; padding: 8px 20px 0; }
.drillHead h2 { margin: 0; font: 600 24px/1.2 var(--serif); }
.drill { grid-template-columns: minmax(0, 1fr) 360px; }
.drill .panel { position: static; max-height: none; }
.home { max-width: 1400px; padding-top: 8px; }
.home > * { max-width: 720px; }
.home p { margin: 0; }
.homeActions { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; margin: 12px 0 20px; }
.home h3 { margin: 0; font: 600 19px/1.3 var(--serif); }
.themes { list-style: none; margin: 12px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.theme { display: flex; flex-direction: column; gap: 6px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.themeHead { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.themeHead strong { font: 600 16px/1.3 var(--serif); }
.themeHead span { flex: none; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.themeBar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.themeBar span { display: block; height: 100%; border-radius: 4px; background: var(--accent); }
.themeActions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 2px; font-size: 14px; }
.drill .primary { background: var(--accent); border-color: var(--accent); color: var(--surface); font-weight: 600; padding: 9px 18px; }

.verdict { font: 600 13px/1 system-ui, sans-serif; color: var(--muted); margin-left: 4px; }
.verdict.blunder, .verdict.mistake { color: var(--bad); }
.verdict.great, .verdict.best { color: var(--good); }
.comment .evalLine { margin-top: 8px; font-size: 13px; }
.move .nag.blunder, .move .nag.mistake { color: var(--bad); }
.move .nag.great { color: var(--good); }
.move.current .nag.blunder, .move.current .nag.mistake, .move.current .nag.great { color: var(--surface); }

.evalChart { position: relative; width: min(100%, 620px); margin: 0 auto; }
.evalChart figcaption { font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.evalChart .hint { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 6px; }
.chart { height: 120px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; cursor: crosshair; touch-action: pan-y; }
.chart svg { display: block; }
.chart .axis { stroke: var(--line); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .axisLabel { fill: var(--muted); font-size: 11px; }
.chart .curve { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
.chart .cursor { stroke: var(--muted); stroke-width: 1; }
.chart .marker { stroke: var(--surface); stroke-width: 2; fill: var(--muted); }
.chart .marker.blunder { fill: var(--bad); }
.chartTip {
  position: absolute;
  top: 24px;
  padding: 2px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  font-size: 12px;
  white-space: nowrap;
  pointer-events: none;
}

footer {
  max-width: 1400px;
  margin: 0 auto;
  padding: 8px 20px 24px;
  color: var(--muted);
  font-size: 12px;
}

/* ---------- Écrans plus petits ---------- */
@media (max-width: 1180px) {
  .layout { grid-template-columns: minmax(0, 1fr) 340px; }
  .library { grid-column: 1 / -1; position: static; max-height: none; }
  .gameList { flex-direction: row; overflow-x: auto; padding-bottom: 4px; scroll-snap-type: x mandatory; }
  .gameList li { flex: 0 0 220px; scroll-snap-align: start; }
  .gameCard { height: 100%; }
}
@media (max-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 8px 16px 24px; gap: 14px; }
  .top { padding: 12px 16px 4px; flex-wrap: wrap; }
  .drillHead { padding: 4px 16px 0; }
  .home { padding-left: 16px; padding-right: 16px; }
  .report { order: 1; }
  .top h1 { font-size: 19px; }
  .top p { font-size: 13px; }
  .logo { width: 36px; height: 36px; font-size: 22px; }
  .panel { position: static; max-height: none; }
  .moves { max-height: 260px; }
  .boardWrap { width: 100%; min-width: 0; }
  .gameHead h2 { font-size: 20px; }
  .controls button { min-width: 40px; padding: 6px 8px; }
  .sep { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .piece.moving { transition: none; }
  .sq.hint::after { animation: none; }
}

/* Compte */
.formStatus { min-height: 1.4em; color: var(--bad); font-size: 14px; }
.danger { margin-top: 28px; font-size: 14px; }
