:root{
  --label-size: clamp(12px, 4vmin, 44px); /* fallback / used when container units not supported */
  --board-max: 90vmin;
  --border-color: #000;

  /* badges */
  --badge-pad-y: 0.18em;
  --badge-pad-x: 0.35em;
  --badge-radius: 0.55em;
}

/* =========================
   BOARD
   ========================= */
.board-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  max-width: var(--board-max);
  margin: 1rem auto;
  transition: transform 300ms ease;
}

.board {
  display: grid;
  grid-template-columns: var(--label-size) repeat(8, 1fr) var(--label-size);
  grid-template-rows:    var(--label-size) repeat(8, 1fr) var(--label-size);

  width: 100%;
  max-width: var(--board-max);
  aspect-ratio: 1 / 1;

  border: 4px solid var(--border-color);
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.12);
  background: #000;
  overflow: hidden;

  /* enable cqw units based on this element's inline size */
  container-type: inline-size;

  /* smooth optional */
  transition: transform 300ms ease;
}

/* =========================
   SQUARES
   ========================= */
.board > .square{
  border: 1px solid black;
  min-width: 0;
  min-height: 0;

  /* important: allow overlays (badges) */
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background-clip: padding-box;
}

/* LEDs aux coins des cases surlignées */
.board > .square .led {
  position: absolute;
  width: clamp(4px, 1.5cqw, 10px);
  height: clamp(4px, 1.5cqw, 10px);
  border-radius: 50%;
  background: #ff2020;
  box-shadow: 0 0 4px rgba(255,30,30,0.8);
  z-index: 2;
}
.board > .square .led.tl { top: 2px;    left: 2px; }
.board > .square .led.tr { top: 2px;    right: 2px; }
.board > .square .led.bl { bottom: 2px; left: 2px; }
.board > .square .led.br { bottom: 2px; right: 2px; }

@keyframes led-blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }
.board > .square .led.blink { animation: led-blink 0.5s step-start infinite; }

/* Curseur personnalisé quand une pièce du stock est sélectionnée */
.board.cursor-placing {
  cursor: none;
}

.board.cursor-placing .square {
  cursor: none;
}

.board.cursor-eraser {
  cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32"><text y="24" font-size="24">🗑️</text></svg>') 16 16, auto;
}

.board.cursor-hand {
  cursor: grab;
}

.board.cursor-hand .square {
  cursor: grab;
}

.board > .square.light { background: #f2f2f2; }
.board > .square.dark  { background: #1a1a1a; }

.board > .square.dark  img.piece { filter: drop-shadow(0 0 3px rgba(255,255,255,0.7)); }
.board > .square.light img.piece { filter: drop-shadow(0 0 2px rgba(0,0,0,0.5)); }

.square img.piece {
  display: block;
  max-width: 100%;
  max-height: 100%;

  /* piece sits under overlays */
  position: relative;
  z-index: 1;
}

/* =========================
   LABELS (letters + numbers)
   ========================= */
.board > .label{
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  user-select: none;
  border: 1px solid rgba(255,255,255,0.1);
  background: #000;
  color: #fff;

  /* fallback font-size */
  font-size: clamp(12px, calc(var(--label-size) * 0.42), 28px);

  line-height: 1;
  padding: 0.05em;
  box-sizing: border-box;
}

@supports (font-size: 3cqw) {
  .board > .label{
    font-size: clamp(10px, 3cqw, 28px);
  }

  .coord-inside {
    font-size: clamp(9px, 1.8cqw, 16px);
  }
}

.coord-inside {
  font-size: clamp(10px, calc(var(--label-size) * 0.28), 16px);
  color: rgba(0,0,0,0.45);
  pointer-events: none;
}

/* =========================
   TURN INDICATOR (cercles colorés dans les coins)
   ========================= */
.turn-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.turn-indicator::after {
  content: '';
  display: block;
  width: clamp(10px, 60%, 24px);
  height: clamp(10px, 60%, 24px);
  border-radius: 50%;
  background: #888; /* gris par défaut */
  border: 2px solid rgba(0,0,0,0.3);
}

/* Couleur du cercle selon le trait */
.turn-indicator.white::after {
  background: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.turn-indicator.black::after {
  background: #333;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

/* =========================
   BADGES pleine largeur (responsive + SVG icons)
   ========================= */

.badge {
  position: absolute;
  left: 0;
  width: 100%;

  display: flex;
  justify-content: center;
  align-items: center;

  /* hauteur responsive : never larger than ~22px, scales with board */
  height: clamp(10px, 3.2cqw, 18px);
  padding: 0;
  box-sizing: border-box;

  line-height: 1;
  user-select: none;
  pointer-events: none;

  z-index: 3;

  font-size: clamp(8px, 1.2cqw, 12px);
  /* subtle blur and translucent background so piece remains visible */
  backdrop-filter: blur(2px);
  background: rgba(255,255,255,0.6);

  border: 1px solid rgba(0,0,0,0.12);
  transition: background-color 180ms ease, border-color 180ms ease, color 180ms ease;
  overflow: hidden;
}

/* Compact badge positioning */
.badge.badge-top {
  top: 0;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.badge.badge-bottom {
  bottom: 0;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.compact-content {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: nowrap;
}

.icon-spacer {
  width: 0.4em;
  display: inline-block;
  flex-shrink: 0;
}

.compact-icon {
  width: clamp(10px, 1.8cqw, 14px);
  height: clamp(10px, 1.8cqw, 14px);
  display: inline-block;
  flex-shrink: 0;
}

/* Overlap icons for compact display */
.compact-icon:not(:first-child) {
  margin-left: -0.8em;
}

/* =========================
   FLIP (visuel seulement)
   ========================= */
/* Contre-rotation du contenu (pièces et labels) */
.board-container.flipped .board .square img.piece,
.board-container.flipped .board .label {
  transform: rotate(180deg);
}

/* Les badges gardent leur position (vers adversaire) mais leur contenu est contre-roté */
.board-container.flipped .board .badge {
  transform: rotate(180deg);
}

/* Pas besoin d'inverser position des badges :
   les blancs restent toujours en haut (vers noirs)
   les noirs restent toujours en bas (vers blancs) */

/* smooth optional */
.board .square img.piece,
.board .square .badge,
.board .label {
  transition: transform 300ms ease;
}

/* =========================
   CONTROL BAR (Import/Export + modes)
   ========================= */
.control-bar {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  align-items: center;
  justify-content: center;
  margin: .75rem auto;
  max-width: 90vmin;
}

.control-bar button {
  padding: .45rem .7rem;
  border-radius: 6px;
  border: 1px solid rgba(0,0,0,0.12);
  background: white;
  cursor: pointer;
  font-weight: 600;
  transition: background-color 200ms ease, color 200ms ease, border-color 200ms ease;
}

.control-bar button:active { transform: translateY(1px); }

/* Mode toggle states (Def/Att, Score) */
.control-bar button.mode-off {
  background: #e8e8e8;
  color: #888;
  border-color: #ccc;
}

.control-bar button.mode-on {
  background: #4da3ff;
  color: white;
  border-color: #1f6ed4;
  font-weight: 700;
}

.control-bar button.mode-on:hover {
  background: #3d8fe0;
}

.control-bar .small {
  font-size: .85rem;
  color: #444;
  margin-left: .5rem;
}

/* =========================
   DROPDOWN (Import/Export)
   ========================= */
.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-btn {
  min-width: 120px;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: .25rem;
  background: white;
  border: 2px solid #dee2e6;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  min-width: 200px;
  z-index: 1000;
}

.dropdown-menu.show {
  display: block;
}

.dropdown-menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: .6rem 1rem;
  border: none;
  border-radius: 0;
  background: white;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 150ms ease;
}

.dropdown-menu button:first-child {
  border-radius: 4px 4px 0 0;
}

.dropdown-menu button:last-child {
  border-radius: 0 0 4px 4px;
}

.dropdown-menu button:hover {
  background: #f8f9fa;
}

.dropdown-menu button:active {
  background: #e9ecef;
}

/* =========================
   IMPORT ZONE (textarea collapsible)
   ========================= */
.import-zone {
  max-width: 90vmin;
  margin: 0 auto .75rem;
  padding: 1rem;
  background: #f8f9fa;
  border: 2px solid #dee2e6;
  border-radius: 8px;
  position: relative;
}

.import-zone textarea {
  width: 100%;
  padding: .45rem .6rem;
  border-radius: 6px;
  border: 1px solid rgba(0,0,0,0.12);
  font-family: 'Courier New', monospace;
  font-size: .9rem;
  resize: vertical;
  box-sizing: border-box;
  margin-bottom: .5rem;
}

.input-type-indicator {
  position: absolute;
  top: 1.3rem;
  right: 1.5rem;
  font-size: .75rem;
  color: #666;
  background: rgba(255,255,255,0.9);
  padding: .2rem .4rem;
  border-radius: 4px;
  pointer-events: none;
}

.import-actions {
  display: flex;
  gap: .5rem;
}

.import-actions button {
  padding: .45rem .7rem;
  border-radius: 6px;
  border: 1px solid rgba(0,0,0,0.12);
  background: white;
  cursor: pointer;
  font-weight: 600;
}

.import-actions button:active { transform: translateY(1px); }

/* =========================
   EVALUATION BAR (barre verticale Stockfish)
   ========================= */
.eval-bar {
  width: 24px;
  /* Hauteur synchronisée avec l'échiquier via JS */
  height: auto;
  background: #333;
  border: 2px solid var(--border-color);
  border-radius: 6px;
  overflow: hidden;
  position: relative;
  flex-shrink: 0;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  transition: transform 300ms ease, opacity 300ms ease, visibility 300ms ease;
  opacity: 0;
  visibility: hidden;
}

.eval-bar.visible {
  opacity: 1;
  visibility: visible;
}

.eval-bar-white {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  transition: height 300ms ease;
}

.eval-bar-score {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: rgba(0,0,0,0.6);
  padding: 4px 3px;
  border-radius: 3px;
  z-index: 10;
  white-space: nowrap;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  pointer-events: none;
}

/* Quand l'échiquier est retourné, inverser la barre d'évaluation */
.board-container.flipped .eval-bar {
  transform: rotate(180deg);
}

.board-container.flipped .board {
  transform: rotate(180deg);
}

/* =========================
   PIECE STOCK (mode Édition)
   ========================= */
.piece-stock {
  max-width: 90vmin;
  margin: 0 auto 1rem;
  padding: 1rem;
  background: #f8f9fa;
  border: 2px solid #dee2e6;
  border-radius: 8px;
}

.stock-section {
  margin-bottom: 1rem;
}

.stock-section:last-of-type {
  margin-bottom: 0.5rem;
}

.stock-label {
  font-weight: 700;
  font-size: 0.9rem;
  color: #495057;
  margin-bottom: 0.5rem;
}

.stock-pieces {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.stock-piece {
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  border: 2px solid #dee2e6;
  border-radius: 6px;
  cursor: pointer;
  transition: all 200ms ease;
  user-select: none;
}

.stock-piece:hover {
  background: #e9ecef;
  border-color: #adb5bd;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.stock-piece.selected {
  background: #4da3ff;
  border-color: #1f6ed4;
  box-shadow: 0 0 0 3px rgba(77, 163, 255, 0.3);
  transform: scale(1.1);
}

.stock-piece img {
  width: 80%;
  height: 80%;
  pointer-events: none;
}

.stock-piece.hand {
  font-size: 1.5rem;
  background: #d1ecf1;
  border-color: #17a2b8;
}

.stock-piece.hand:hover {
  background: #bee5eb;
}

.stock-piece.hand.selected {
  background: #17a2b8;
  border-color: #117a8b;
}

.stock-piece.eraser {
  font-size: 1.5rem;
  background: #fff3cd;
  border-color: #ffc107;
}

.stock-piece.eraser:hover {
  background: #ffe69c;
}

.stock-piece.eraser.selected {
  background: #ff9800;
  border-color: #f57c00;
}

.stock-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.stock-actions button {
  padding: 0.45rem 0.7rem;
  border-radius: 6px;
  border: 1px solid rgba(0,0,0,0.12);
  background: white;
  cursor: pointer;
  font-weight: 600;
  transition: background-color 200ms ease;
}

.stock-actions button:hover {
  background: #e9ecef;
}

.stock-actions button:active {
  transform: translateY(1px);
}

/* =========================
   NAV CONTROLS (navigation PGN)
   ========================= */
.nav-controls {
  display: flex;
  gap: .5rem;
  align-items: center;
  justify-content: center;
  max-width: 90vmin;
  margin: 0 auto 1rem;
}

.nav-controls button {
  min-width: 40px;
  font-size: 1.1rem;
  padding: .45rem .7rem;
  border-radius: 6px;
  border: 1px solid rgba(0,0,0,0.12);
  background: white;
  cursor: pointer;
  font-weight: 600;
}

.nav-controls button:active { transform: translateY(1px); }
.nav-controls button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.nav-controls .small {
  font-size: .85rem;
  color: #444;
  min-width: 100px;
  text-align: center;
}

/* =========================
   MOVES LIST (liste des coups)
   ========================= */
.moves-list {
  max-width: 90vmin;
  margin: 0 auto 1rem;
  background: #f8f9fa;
  border: 2px solid #dee2e6;
  border-radius: 8px;
  overflow: hidden;
}

.moves-header {
  background: linear-gradient(135deg, #495057 0%, #343a40 100%);
  color: white;
  padding: .5rem 1rem;
  font-weight: 600;
  font-size: .9rem;
  text-align: center;
}

.moves-content {
  padding: .75rem;
  max-height: 200px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: auto 1fr 1fr;
  gap: .25rem .5rem;
  align-items: center;
}

.move-number {
  font-weight: 600;
  color: #666;
  text-align: right;
  font-size: .85rem;
  padding-right: .25rem;
}

.move-item {
  padding: .3rem .5rem;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 150ms ease;
  font-family: 'Courier New', monospace;
  font-size: .9rem;
}

.move-item:hover {
  background: #e9ecef;
}

.move-item.active {
  background: #4da3ff;
  color: white;
  font-weight: 700;
}

.move-item.empty {
  visibility: hidden;
}

/* =========================
   BADGE STATES (4 states)
   ========================= */

/* rouge vif (danger réel) */
.badge.danger {
  background: #ff3b3b;
  color: white;
  border: 1px solid #cc0000;
}

/* rouge clair (vulnérable, 0/0) */
.badge.vulnerable {
  background: #ffb3b3;
  color: #660000;
  border: 1px solid #cc9999;
}

/* bleu clair (défense suffisante) */
.badge.safe {
  background: #d9ecff;
  color: #003d7a;
  border: 1px solid #8ec5ff;
}

/* bleu vif (surprotégée) */
.badge.overprotected {
  background: #4da3ff;
  color: white;
  border: 1px solid #1f6ed4;
}

/* tiny tweak for very small viewports */
@media (max-width: 420px) {
  .badge .icons { gap: 0.06em; }
  .badge { height: clamp(10px, 3.2cqw, 16px); }
}
