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

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

/* =========================
   BOARD
   ========================= */
.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;
  margin: 1rem auto;

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

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

/* =========================
   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;
}

.board > .square.light { background: #f0d9b5; }
.board > .square.dark  { background: #b58863; }

.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(0,0,0,0.15);
  background: rgba(255,255,255,0.6);

  /* 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)
   ========================= */
.board.flipped {
  transform: rotate(180deg);
}

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

/* Les badges gardent leur position (vers adversaire) mais leur contenu est contre-roté */
.board.flipped .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 {
  transition: transform 300ms ease;
}

.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: min(90vmin, 800px);
}

.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: min(90vmin, 800px);
  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); }

/* =========================
   SCORE DISPLAY
   ========================= */
.score-display {
  max-width: min(90vmin, 800px);
  margin: 0 auto 1rem;
  padding: .75rem 1rem;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border: 2px solid #dee2e6;
  border-radius: 8px;
  text-align: center;
  font-size: 1.1rem;
  font-weight: 600;
  color: #495057;
}

.score-display.positive {
  background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
  border-color: #81c784;
  color: #2e7d32;
}

.score-display.negative {
  background: linear-gradient(135deg, #ffebee 0%, #ffcdd2 100%);
  border-color: #e57373;
  color: #c62828;
}

.score-display.mate {
  background: linear-gradient(135deg, #fff9c4 0%, #fff59d 100%);
  border-color: #fdd835;
  color: #f57f17;
  font-size: 1.2rem;
}

/* =========================
   NAV CONTROLS (navigation PGN)
   ========================= */
.nav-controls {
  display: flex;
  gap: .5rem;
  align-items: center;
  justify-content: center;
  max-width: min(90vmin, 800px);
  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: min(90vmin, 800px);
  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); }
}
