:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --ring: rgba(11, 11, 11, 0.1);
  --hover: rgba(11, 11, 11, 0.05);
  --up: #2a78d6;
  --down: #eb6834;
  --top: #0ca30c;
  --excellent: #898781;
  --inaccuracy: #fab219;
  --mistake: #ec835a;
  --blunder: #d03b3b;
  --board: max(300px, min(100vh - 372px, 100vw - 470px));
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --grid: #2c2c2a;
    --axis: #383835;
    --ring: rgba(255, 255, 255, 0.1);
    --hover: rgba(255, 255, 255, 0.07);
    --up: #3987e5;
    --down: #d95926;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 0 16px 16px;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 52px;
  max-width: 1280px;
  margin: 0 auto;
}
h1 { margin: 0; font-size: 17px; font-weight: 600; white-space: nowrap; }
h2 { margin: 0; font-size: 14px; font-weight: 600; }

.keys {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 0;
  padding: 5px 10px;
  border: 1px solid var(--ring);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink-2);
  font-size: 12px;
  list-style: none;
  white-space: nowrap;
}
.keys li { display: flex; align-items: center; gap: 4px; }
kbd {
  min-width: 18px;
  padding: 0 4px;
  border: 1px solid var(--ring);
  border-radius: 4px;
  background: var(--page);
  color: var(--ink);
  font: 600 11px/16px system-ui, -apple-system, "Segoe UI", sans-serif;
  text-align: center;
}

.status { display: flex; align-items: center; gap: 10px; color: var(--ink-2); font-size: 13px; text-align: right; }
.status progress { width: 140px; height: 6px; accent-color: var(--up); }

main {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 16px;
  max-width: 1280px;
  margin: 0 auto;
}

.board-row { display: flex; gap: 8px; }
#board { width: var(--board); height: var(--board); }
cg-board { background: repeating-conic-gradient(#b58863 0 25%, #f0d9b5 0 50%) 0 0 / 25% 25%; }
.cg-wrap coords { opacity: 1; font-size: 11px; font-weight: 600; }
.cg-wrap coords coord { color: #3a2a1a !important; }
.cg-wrap coords.files { bottom: 0; left: 0; height: auto; }
.cg-wrap coords.files coord { flex: 1 1 0; padding-right: 3px; text-align: right; }

#evalbar {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  width: 32px;
  overflow: hidden;
  border-radius: 4px;
  background: #2b2b29;
}
#evalbar.flipped { justify-content: flex-start; }
#evalbar .fill { height: 50%; background: #f5f5f3; transition: height 0.2s; }
#evalbar span {
  position: absolute;
  bottom: 4px;
  left: 0;
  right: 0;
  color: #0b0b0b;
  font-size: 10px;
  font-weight: 600;
  text-align: center;
}
#evalbar.lead-black span { top: 4px; bottom: auto; color: #f5f5f3; }
#evalbar.flipped span { top: 4px; bottom: auto; }
#evalbar.flipped.lead-black span { top: auto; bottom: 4px; }

.controls { display: flex; justify-content: center; gap: 6px; margin-top: 8px; padding-left: 40px; }
.controls button { width: 46px; }
.controls #flip { width: auto; white-space: nowrap; }

button, .button {
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--ring);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}
.button { display: inline-flex; align-items: center; }
button:hover, .button:hover { background: var(--hover); }
button:focus-visible, .button:focus-within, summary:focus-visible { outline: 2px solid var(--up); outline-offset: 1px; }
button.primary { background: var(--up); border-color: transparent; color: #fff; font-weight: 600; }

.right { display: flex; flex-direction: column; gap: 12px; min-width: 0; height: calc(var(--board) + 42px); }

details, #review, #chart {
  border: 1px solid var(--ring);
  border-radius: 8px;
  background: var(--surface);
}
details { flex: none; max-height: 100%; overflow-y: auto; padding: 10px 12px; }
summary { font-weight: 600; cursor: pointer; }

textarea, select, input {
  border: 1px solid var(--ring);
  border-radius: 6px;
  background: var(--page);
  color: var(--ink);
  font: inherit;
}
textarea {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 8px;
  resize: vertical;
  font: 12px/1.5 ui-monospace, Menlo, monospace;
}
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; }
.depth { display: inline-flex; align-items: center; gap: 6px; margin-left: auto; color: var(--ink-2); }
.depth select { height: 34px; padding: 0 6px; }
#cc-user { flex: 1 1 150px; min-width: 0; height: 34px; padding: 0 8px; }
#cc-month { height: 34px; padding: 0 6px; }
#cc-status { margin: 8px 0 0; }
#cc-games { display: flex; flex-direction: column; gap: 2px; max-height: min(240px, 36vh); margin-top: 8px; overflow-y: auto; }
#cc-games .mv { flex: none; }
#cc-games .san { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#cc-games .ev { flex: none; }
#games { width: 100%; height: 34px; margin-top: 8px; }
#error { margin: 8px 0 0; color: var(--blunder); overflow-wrap: anywhere; }

#review {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 12px;
  overflow-y: auto;
  container-type: inline-size;
}
#review.exploring .tally { display: none; }
#overview { display: flex; flex-wrap: wrap; gap: 10px 20px; flex: none; }
.tally { flex: 1 1 230px; min-width: 0; }
#players { display: flex; flex: none; flex-wrap: wrap; align-items: baseline; gap: 0 10px; }
.muted { color: var(--ink-2); font-size: 13px; }

#tiles { display: grid; flex: 1 1 340px; grid-template-columns: repeat(3, 1fr); align-content: start; gap: 8px; }
.tile { display: flex; flex-direction: column; padding: 6px 10px; border-radius: 6px; background: var(--page); }
.tile strong { font-size: 21px; font-weight: 600; line-height: 1.25; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th { color: var(--ink-2); font-weight: 400; }
th { text-align: left; font-weight: 400; }
th + th, td { width: 52px; text-align: right; font-variant-numeric: tabular-nums; }
th, td { padding: 2px 0; }
tbody tr + tr { border-top: 1px solid var(--grid); }
tbody th { display: flex; align-items: center; gap: 8px; }

.tag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  min-width: 20px;
  height: 18px;
  padding: 0 3px;
  border-radius: 4px;
  color: #fff;
  font-size: 11px;
  font-style: normal;
  font-weight: 700;
}
.tag.top { background: var(--top); }
.tag.excellent { background: var(--excellent); }
.tag.inaccuracy { background: var(--inaccuracy); color: #0b0b0b; }
.tag.mistake { background: var(--mistake); color: #0b0b0b; }
.tag.blunder { background: var(--blunder); }

/* Le commentaire fixe la hauteur : il ne doit jamais être coupé. La liste des coups s'y adapte et défile. */
#study { position: relative; display: flex; flex: 1 0 auto; min-height: 210px; padding-top: 8px; border-top: 1px solid var(--grid); }
#detail { display: flex; flex-direction: column; width: calc(50% - 8px); min-width: 0; }
#detail-text { display: flex; flex: none; flex-direction: column; }
#comment { flex: 1 1 auto; }
#detail .title { display: flex; align-items: center; gap: 8px; }
#comment p, #opening-body p { margin: 4px 0 0; }
#comment p:first-child { margin-top: 6px; }
#actions { flex: none; margin-top: 6px; }
#opening {
  max-height: none;
  padding: 8px 0 0;
  border: 0;
  border-top: 1px solid var(--grid);
  border-radius: 0;
  background: none;
  overflow: visible;
}
#opening summary { font-weight: 500; }
#actions button, #leave { height: 28px; padding: 0 10px; font-size: 13px; }

#line { display: flex; flex-direction: column; gap: 6px; padding: 8px 10px; border-radius: 6px; background: var(--page); }
.line-head { display: flex; align-items: center; gap: 8px; }
#leave { margin-left: auto; white-space: nowrap; }
#line-moves { display: flex; flex-wrap: wrap; gap: 2px 4px; max-height: 58px; overflow-y: auto; }
#line-moves:empty { display: none; }

#moves {
  position: absolute;
  top: 8px;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: 34px 1fr 1fr;
  align-content: start;
  gap: 2px 4px;
  width: calc(50% - 8px);
  overflow-y: auto;
}
.no { align-self: center; color: var(--ink-2); font-size: 12px; font-variant-numeric: tabular-nums; }
.mv {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 26px;
  padding: 0 6px;
  border: 0;
  background: transparent;
  text-align: left;
}
.mv.on { background: var(--hover); box-shadow: inset 0 0 0 1px var(--ring); font-weight: 600; }
.mv .ev { margin-left: auto; color: var(--ink-2); font-size: 11px; font-weight: 400; font-variant-numeric: tabular-nums; }

@container (max-width: 600px) {
  #study { flex-direction: column; gap: 10px; }
  #detail { width: auto; }
  #moves { position: relative; top: 0; width: auto; max-height: 220px; }
}

#chart { position: relative; grid-column: 1 / -1; padding: 10px 12px 6px; }
.chart-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 16px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 0; padding: 0; list-style: none; color: var(--ink-2); font-size: 12px; }
.legend li { display: flex; align-items: center; gap: 6px; }
.swatch { width: 14px; height: 10px; border-radius: 2px; }
.swatch.up { background: var(--up); }
.swatch.down { background: var(--down); }
.key { width: 9px; height: 9px; border-radius: 50%; }
.key.inaccuracy { background: var(--inaccuracy); }
.key.mistake { background: var(--mistake); }
.key.blunder { background: var(--blunder); }

#chart svg { display: block; width: 100%; height: 170px; margin-top: 6px; touch-action: pan-y; cursor: crosshair; }
#chart .grid { stroke: var(--grid); stroke-width: 1; }
#chart .base { stroke: var(--axis); stroke-width: 1; }
#chart .axis { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
#chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
#chart .area { stroke: none; opacity: 0.12; }
#chart .up .line { stroke: var(--up); }
#chart .up .area { fill: var(--up); }
#chart .down .line { stroke: var(--down); }
#chart .down .area { fill: var(--down); }
#chart .dot { stroke: var(--surface); stroke-width: 2; }
#chart .dot.inaccuracy { fill: var(--inaccuracy); }
#chart .dot.mistake { fill: var(--mistake); }
#chart .dot.blunder { fill: var(--blunder); }
#chart .dot.now { fill: var(--ink); }
#chart .cursor { stroke: var(--ink-2); stroke-width: 1; }
#chart .hair { stroke: var(--muted); stroke-width: 1; }

.tip {
  position: absolute;
  top: 0;
  left: 0;
  margin-top: 40px;
  padding: 6px 10px;
  border: 1px solid var(--ring);
  border-radius: 6px;
  background: var(--surface);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
  font-size: 12px;
  white-space: nowrap;
  pointer-events: none;
}
.tip strong { display: block; font-size: 14px; }
.tip span { color: var(--ink-2); }

@media (max-width: 860px) {
  :root { --board: min(100vw - 72px, 560px); }
  main { grid-template-columns: minmax(0, 1fr); }
  .keys { display: none; }
  .right { height: auto; }
  .depth { margin-left: 0; }
}
