/* ==========================================================================
   Ajedrez contra la máquina - Estilos canónicos y diseño responsive
   Fijate Tools • Normas de accesibilidad y rendimiento 60 FPS
   ========================================================================== */

/* 1. Variables y tokens de diseño */
:root {
  --bg-primary: #0f172a;
  --bg-surface: #1e293b;
  --bg-surface-alt: #334155;
  --bg-card: rgba(30, 41, 59, 0.85);
  --border-subtle: rgba(255, 255, 255, 0.08);
  --border-focus: rgba(99, 102, 241, 0.5);
  
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  
  --accent-primary: #6366f1;
  --accent-primary-hover: #4f46e5;
  --accent-success: #10b981;
  --accent-danger: #ef4444;
  --accent-warning: #f59e0b;

  --board-max-size: 860px;

  /* Temas de tablero por defecto (Madera nogal calibrada con alto contraste) */
  --sq-light: #d5b486;
  --sq-dark: #7d4924;
  --sq-highlight: rgba(205, 210, 106, 0.65);
  --sq-check: rgba(239, 68, 68, 0.7);

  --eval-white: #ffffff;
  --eval-black: #1e293b;

  --shadow-card: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 8px 10px -6px rgba(0, 0, 0, 0.3);
  --radius-lg: 1rem;
  --radius-md: 0.75rem;
  --radius-sm: 0.5rem;
}

/* Modo claro adaptativo */
[data-theme="light"] {
  --bg-primary: #f8fafc;
  --bg-surface: #ffffff;
  --bg-surface-alt: #f1f5f9;
  --bg-card: rgba(255, 255, 255, 0.95);
  --border-subtle: rgba(0, 0, 0, 0.08);
  --border-focus: rgba(99, 102, 241, 0.6);
  
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #64748b;

  --shadow-card: 0 10px 25px -5px rgba(0, 0, 0, 0.08), 0 8px 10px -6px rgba(0, 0, 0, 0.04);
}

/* 2. Tipografías corporativas obligatorias (Lección 195) */
body {
  margin: 0;
  padding: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  overflow-x: clip;
  width: 100%;
  max-width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

h1, h2, h3, h4, h5, h6, .brand-title, .modal-title, .section-title {
  font-family: 'Outfit', sans-serif;
  margin: 0;
}

button, input, select, textarea {
  font-family: inherit;
}

code, pre, .history-list, .chess-timer, .hud-metric-value {
  font-family: 'JetBrains Mono', monospace;
}

/* 3. Estructura principal y layout */
.tool-layout {
  max-width: 1420px;
  margin: 1.5rem auto 3rem;
  padding: 0 1rem;
  flex: 1 0 auto;
  box-sizing: border-box;
  width: 100%;
}

.tool-with-sidebar {
  display: flex;
  gap: 2rem;
  align-items: flex-start;
  width: 100%;
}

.tool-main-content {
  flex: 1 1 0%;
  min-width: 0;
  width: 100%;
}

/* 4. Tarjeta principal del juego */
.game-container-card {
  background: var(--bg-card);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 1.5rem;
  box-sizing: border-box;
}

/* 5. Cabecera de la partida y estatus */
.game-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
  padding-bottom: 1rem;
  margin-bottom: 1.25rem;
  border-bottom: 1px solid var(--border-subtle);
}

.engine-status-box {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.engine-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.85rem;
  border-radius: 2rem;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.engine-badge-ready {
  background: rgba(16, 185, 129, 0.15);
  color: #10b981;
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.engine-badge-busy {
  background: rgba(245, 158, 11, 0.15);
  color: #f59e0b;
  border: 1px solid rgba(245, 158, 11, 0.3);
  animation: pulse-busy 1.5s infinite;
}

@keyframes pulse-busy {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.game-status-text {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-secondary);
}

/* Selector de modo 2D / 3D */
.view-mode-toggle-group {
  display: inline-flex;
  background: var(--bg-surface-alt);
  border: 1px solid var(--border-subtle);
  border-radius: 2rem;
  padding: 2px;
}

.btn-toggle-mode {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: 0.35rem 0.85rem;
  border-radius: 2rem;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition: background-color 0.2s, color 0.2s;
}

.btn-toggle-mode:hover {
  color: var(--text-primary);
}

.btn-toggle-mode.active {
  background: var(--accent-primary);
  color: #ffffff;
  box-shadow: 0 2px 6px rgba(99, 102, 241, 0.4);
}

/* 6. Cuadrícula del juego: Tablero + Barra de evaluación + Panel de control */
.game-play-area {
  display: grid;
  grid-template-columns: auto 1fr 340px;
  gap: 1.25rem;
  align-items: start;
}

/* Barra de evaluación vertical */
.eval-bar-container {
  width: 24px;
  height: min(var(--board-max-size), 85vw);
  background-color: var(--eval-black);
  border-radius: var(--radius-sm);
  overflow: hidden;
  position: relative;
  border: 1px solid var(--border-subtle);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.4);
}

.eval-bar-fill {
  width: 100%;
  height: 50%;
  background-color: var(--eval-white);
  transition: height 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: absolute;
  bottom: 0;
  left: 0;
}

.eval-bar-score {
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.7rem;
  font-weight: 700;
  color: #ffffff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
  pointer-events: none;
  z-index: 2;
}

/* Columna central: Tarjetas de jugador y tablero */
.board-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  max-width: var(--board-max-size);
  margin: 0 auto;
}

.player-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.5rem 0.85rem;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-sizing: border-box;
  transition: border-color 0.2s, background-color 0.2s;
}

.player-card.player-active {
  border-color: var(--accent-primary);
  background: rgba(99, 102, 241, 0.08);
}

.player-info {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
}

.player-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-surface-alt);
  font-weight: 700;
  font-size: 0.85rem;
  flex-shrink: 0;
}

.player-title-box {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.player-name {
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-captured-tray {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 18px;
}

.captured-mini {
  display: inline-block;
  width: 16px;
  height: 16px;
}

.captured-mini svg {
  width: 100%;
  height: 100%;
}

.material-advantage {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--accent-warning);
  margin-left: 4px;
}

.chess-timer {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  padding: 0.35rem 0.65rem;
  background: var(--bg-surface-alt);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  min-width: 60px;
  text-align: center;
}

/* 7. Tablero de ajedrez (Chessboard 8x8) */
.chessboard-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-width: var(--board-max-size);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.4);
  border: 4px solid var(--bg-surface-alt);
  box-sizing: border-box;
  touch-action: none;
  user-select: none;
}

/* Tablero 3D WebGL */
.chessboard-3d-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-width: var(--board-max-size);
  min-height: 640px;
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.45);
  border: 4px solid var(--bg-surface-alt);
  box-sizing: border-box;
  touch-action: none;
  user-select: none;
  background-color: #0f172a;
}

.chessboard-3d-canvas-container {
  width: 100%;
  height: 100%;
  min-height: 640px;
  display: block;
}

.camera-presets-bar {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(15, 23, 42, 0.88);
  backdrop-filter: blur(12px);
  padding: 4px 8px;
  border-radius: 2rem;
  border: 1px solid rgba(255, 255, 255, 0.16);
  z-index: 10;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
}

.btn-cam-lock {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: transparent;
  border: none;
  color: #f8fafc;
  font-size: 0.76rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 1rem;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}

.btn-cam-lock.active {
  background: rgba(16, 185, 129, 0.25);
  color: #10b981;
  border: 1px solid rgba(16, 185, 129, 0.4);
}

.btn-cam-lock:not(.active) {
  background: rgba(245, 158, 11, 0.2);
  color: #f59e0b;
  border: 1px solid rgba(245, 158, 11, 0.35);
}

.cam-presets-divider {
  width: 1px;
  height: 16px;
  background: rgba(255, 255, 255, 0.2);
  margin: 0 2px;
}

.btn-cam-preset {
  background: transparent;
  border: none;
  color: #f8fafc;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 1rem;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}

.btn-cam-preset:hover {
  background: rgba(255, 255, 255, 0.18);
}

.chessboard {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
}

.chess-square {
  position: relative;
  width: 100%;
  height: 100%;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  cursor: pointer;
  overflow: hidden;
}

/* Colores de casillas según tema (calibrados para alto contraste con piezas blancas) */
.theme-wood .square-light { background-color: #d5b486; }
.theme-wood .square-dark { background-color: #7d4924; }

.theme-tournament .square-light { background-color: #cbd0aa; }
.theme-tournament .square-dark { background-color: #557b3e; }

.theme-ocean .square-light { background-color: #aec2d4; }
.theme-ocean .square-dark { background-color: #3e6587; }

.theme-slate .square-light { background-color: #8a97a8; }
.theme-slate .square-dark { background-color: #334155; }

/* Resaltados de casillas */
.square-highlight-last {
  box-shadow: inset 0 0 0 999px rgba(255, 230, 0, 0.38);
}

.square-selected {
  box-shadow: inset 0 0 0 999px rgba(99, 102, 241, 0.5);
}

.square-in-check {
  box-shadow: inset 0 0 0 999px rgba(239, 68, 68, 0.65);
}

/* Indicadores de movimientos legales (Canónico: Centrado absoluto, sin empujar piezas) */
.move-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 5;
  box-sizing: border-box;
}

.move-hint-dot {
  width: 26%;
  height: 26%;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.22);
}

[data-theme="dark"] .move-hint-dot {
  background: rgba(255, 255, 255, 0.32);
}

.move-hint-capture {
  width: 82%;
  height: 82%;
  border-radius: 50%;
  border: 5px solid rgba(0, 0, 0, 0.24);
  background: transparent;
}

[data-theme="dark"] .move-hint-capture {
  border-color: rgba(255, 255, 255, 0.34);
}

/* Coordenadas en los bordes */
.square-coord-rank,
.square-coord-file {
  position: absolute;
  font-size: 0.68rem;
  font-weight: 700;
  pointer-events: none;
  opacity: 0.75;
  z-index: 4;
}

.square-coord-rank {
  top: 2px;
  left: 3px;
}

.square-coord-file {
  bottom: 2px;
  right: 3px;
}

/* Piezas de ajedrez */
.chess-piece {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  z-index: 2;
  transition: transform 0.1s ease-out;
}

.chess-piece:active {
  cursor: grabbing;
}

.chess-piece svg {
  width: 88%;
  height: 88%;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.25));
}

/* Pieza arrastrándose */
.chess-piece-dragging {
  position: fixed;
  width: 64px;
  height: 64px;
  pointer-events: none;
  transform: translate(-50%, -50%);
  z-index: 9999;
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.45));
}

/* 8. Panel de control lateral */
.game-control-panel {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  box-sizing: border-box;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.control-label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: none; /* Regla de no Title Case */
  color: var(--text-secondary);
}

/* Desplegables antidesborde según norma canónica */
.form-select {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  min-height: 42px;
  padding: 0.5rem 0.75rem;
  background-color: var(--bg-surface-alt);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  box-sizing: border-box;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}

.form-select:focus {
  border-color: var(--accent-primary);
}

/* Historial de jugadas PGN */
.history-container {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-list {
  background: var(--bg-surface-alt);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  height: 180px;
  overflow-y: auto;
  padding: 0.5rem;
  box-sizing: border-box;
  font-size: 0.85rem;
}

.history-row {
  display: grid;
  grid-template-columns: 36px 1fr 1fr;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
}

.history-row:nth-child(even) {
  background: rgba(255, 255, 255, 0.03);
}

.history-num {
  color: var(--text-muted);
}

.history-move {
  font-weight: 500;
}

.history-active {
  background: rgba(99, 102, 241, 0.25);
  border-radius: 3px;
  padding: 0 4px;
}

/* Botones de acción canónicos */
.btn-grid-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
}

.btn-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-height: 42px;
  width: 100%;
  padding: 0.5rem 0.85rem;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: background-color 0.2s, transform 0.1s;
  box-sizing: border-box;
}

.btn-action:active {
  transform: scale(0.98);
}

.btn-primary {
  background-color: var(--accent-primary);
  color: #ffffff;
}

.btn-primary:hover {
  background-color: var(--accent-primary-hover);
}

.btn-secondary {
  background-color: var(--bg-surface-alt);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
}

.btn-secondary:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.btn-danger {
  background-color: rgba(239, 68, 68, 0.15);
  color: #ef4444;
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.btn-danger:hover {
  background-color: rgba(239, 68, 68, 0.25);
}

/* 9. Sección didáctica y consejos */
.chess-tips-card {
  margin-top: 1.5rem;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  box-sizing: border-box;
}

.chess-tips-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
  margin-top: 0.75rem;
}

.tip-item {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.tip-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-primary);
}

.tip-desc {
  font-size: 0.85rem;
  color: var(--text-secondary);
  line-height: 1.45;
  margin: 0;
}

/* 10. Modales nativos (cero window.alert()) */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease-out;
  padding: 1rem;
  box-sizing: border-box;
}

.modal-backdrop.modal-visible {
  opacity: 1;
  pointer-events: auto;
}

.modal-box {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  width: 100%;
  max-width: 440px;
  padding: 1.5rem;
  box-sizing: border-box;
  text-align: center;
  transform: translateY(20px) scale(0.95);
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-backdrop.modal-visible .modal-box {
  transform: translateY(0) scale(1);
}

.modal-title {
  font-size: 1.35rem;
  font-weight: 700;
  margin-bottom: 0.6rem;
  color: var(--text-primary);
}

.modal-desc {
  font-size: 0.95rem;
  color: var(--text-secondary);
  line-height: 1.5;
  margin: 0 0 1.25rem 0;
}

.modal-choices-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.btn-promotion-choice {
  aspect-ratio: 1 / 1;
  background: var(--bg-surface-alt);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
  transition: background-color 0.15s, transform 0.1s;
}

.btn-promotion-choice:hover {
  background-color: var(--accent-primary);
  transform: translateY(-2px);
}

.btn-promotion-choice svg {
  width: 100%;
  height: 100%;
}

.modal-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
}

/* Toast de confirmación sutil */
.toast-feedback {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%) translateY(40px);
  background: #1e293b;
  color: #f8fafc;
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 0.6rem 1.25rem;
  border-radius: 2rem;
  font-size: 0.88rem;
  font-weight: 500;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  opacity: 0;
  transition: transform 0.25s, opacity 0.25s;
  z-index: 100000;
}

.toast-feedback.toast-visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* 11. Adaptación móvil y tabletas */
@media (max-width: 960px) {
  .game-play-area {
    grid-template-columns: auto 1fr;
  }

  .game-control-panel {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) {
  .tool-layout {
    margin: 1rem auto 2rem;
    padding: 0 0.5rem;
  }

  .game-container-card {
    padding: 1rem 0.75rem;
  }

  .game-play-area {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
  }

  .eval-bar-container {
    width: 100%;
    height: 12px;
    order: -1;
  }

  .eval-bar-fill {
    height: 100%;
    width: 50%;
    left: 0;
    bottom: 0;
    transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .eval-bar-score {
    top: -20px;
    left: 50%;
  }

  .board-column {
    max-width: 100%;
  }

  .chessboard-wrapper {
    max-width: 100%;
    border-width: 2px;
  }

  .btn-grid-actions {
    grid-template-columns: 1fr;
  }
}
