/* ==========================================================================
   Sudoku Web App - Main Styles & Themes
   ========================================================================== */

:root {
  /* Tema Claro (Por Defecto) */
  --bg-primary: #f8fafc;
  --bg-surface: #ffffff;
  --bg-surface-elevated: #f1f5f9;
  --bg-hover: #e2e8f0;

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --text-initial: #0f172a;
  --text-user: #2563eb;
  --text-notes: #64748b;

  --accent-primary: #3b82f6;
  --accent-hover: #2563eb;
  --accent-light: #dbeafe;
  --accent-active: #1d4ed8;

  --highlight-selected: #bfdbfe;
  --highlight-peer: #eff6ff;
  --highlight-same: #cbd5e1;
  --highlight-same-candidate: #3b82f6;
  --highlight-error: #fecaca;
  --text-error: #dc2626;

  --border-light: #e2e8f0;
  --border-medium: #94a3b8;
  --border-strong: #334155;

  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-full: 9999px;

  --font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  --transition-fast: 0.15s ease;
  --transition-normal: 0.25s ease;

  /* Escala de números del tablero */
  --cell-font-scale: 1;

  /* Ancho de la botonera lateral */
  --actions-side-width: 92px;

  /* Dimensiones dinámicas del tablero para ajustarse a la ventana */
  --board-size: clamp(270px, min(calc(100vw - var(--actions-side-width) - 44px), calc(100dvh - 200px)), 640px);
  --stage-width: calc(var(--board-size) + var(--actions-side-width) + 12px);
}

/* Tema Oscuro */
[data-theme="dark"] {
  --bg-primary: #0f172a;
  --bg-surface: #1e293b;
  --bg-surface-elevated: #334155;
  --bg-hover: #475569;

  --text-primary: #f8fafc;
  --text-secondary: #cbd5e1;
  --text-muted: #64748b;
  --text-initial: #ffffff;
  --text-user: #60a5fa;
  --text-notes: #94a3b8;

  --accent-primary: #3b82f6;
  --accent-hover: #60a5fa;
  --accent-light: #1e3a8a;
  --accent-active: #93c5fd;

  --highlight-selected: #1e3a8a;
  --highlight-peer: #172554;
  --highlight-same: #334155;
  --highlight-same-candidate: #60a5fa;
  --highlight-error: #7f1d1d;
  --text-error: #f87171;

  --border-light: #334155;
  --border-medium: #475569;
  --border-strong: #94a3b8;
}

/* Tema Sepia (Descanso Visual) */
[data-theme="sepia"] {
  --bg-primary: #f4ecd8;
  --bg-surface: #fbf0d9;
  --bg-surface-elevated: #e9dec4;
  --bg-hover: #ded2b5;

  --text-primary: #433422;
  --text-secondary: #63503b;
  --text-muted: #8c7860;
  --text-initial: #2c1810;
  --text-user: #8f4e1f;
  --text-notes: #79634b;

  --accent-primary: #b45309;
  --accent-hover: #d97706;
  --accent-light: #fef3c7;
  --accent-active: #92400e;

  --highlight-selected: #fde68a;
  --highlight-peer: #fef3c7;
  --highlight-same: #e4d5b7;
  --highlight-same-candidate: #b45309;
  --highlight-error: #fecaca;
  --text-error: #b91c1c;

  --border-light: #dfd3b7;
  --border-medium: #a89476;
  --border-strong: #6d5b45;
}

/* Tema OLED Midnight */
[data-theme="oled"] {
  --bg-primary: #000000;
  --bg-surface: #121212;
  --bg-surface-elevated: #1e1e1e;
  --bg-hover: #2a2a2a;

  --text-primary: #ffffff;
  --text-secondary: #a0a0a0;
  --text-muted: #555555;
  --text-initial: #ffffff;
  --text-user: #38bdf8;
  --text-notes: #888888;

  --accent-primary: #0284c7;
  --accent-hover: #38bdf8;
  --accent-light: #082f49;
  --accent-active: #7dd3fc;

  --highlight-selected: #0c4a6e;
  --highlight-peer: #082f49;
  --highlight-same: #262626;
  --highlight-same-candidate: #38bdf8;
  --highlight-error: #450a0a;
  --text-error: #f87171;

  --border-light: #262626;
  --border-medium: #404040;
  --border-strong: #737373;
}

/* Modos de Disposición de Acciones */
[data-actions-layout="side"] {
  --board-size: clamp(270px, min(calc(100vw - var(--actions-side-width) - 44px), calc(100dvh - 200px)), 640px);
  --stage-width: calc(var(--board-size) + var(--actions-side-width) + 12px);
}

[data-actions-layout="bottom"] {
  --board-size: clamp(270px, min(calc(100vw - 32px), calc(100dvh - 265px)), 600px);
  --stage-width: var(--board-size);
}

/* Modos de Tamaño de Fuente de Números */
[data-font-size="normal"] {
  --cell-font-scale: 1;
}

[data-font-size="large"] {
  --cell-font-scale: 1.25;
}

[data-font-size="xlarge"] {
  --cell-font-scale: 1.5;
}

/* Modos de Tipografía */
[data-font-family="system"] {
  --font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}

[data-font-family="outfit"] {
  --font-family: 'Outfit', system-ui, -apple-system, sans-serif;
}

[data-font-family="fredoka"] {
  --font-family: 'Fredoka', cursive, system-ui, sans-serif;
}

[data-font-family="fira-code"] {
  --font-family: 'Fira Code', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

[data-font-family="lora"] {
  --font-family: 'Lora', Georgia, Cambria, 'Times New Roman', serif;
}

/* Reset y Base */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  min-height: 100vh;
  min-height: 100dvh;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-family);
  transition: background-color var(--transition-normal), color var(--transition-normal);
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* Contenedor Principal de la App */
.app-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  min-height: 100vh;
  min-height: 100dvh;
  width: 100%;
  max-width: var(--stage-width);
  margin: 0 auto;
  padding: 6px 12px 14px;
}

/* Cabecera Principal */
.app-header {
  width: 100%;
  max-width: var(--stage-width);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 0 8px;
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-logo {
  width: 32px;
  height: 32px;
  color: var(--accent-primary);
}

.brand-title {
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, var(--accent-primary), #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.brand-version {
  font-size: 0.72rem;
  font-weight: 700;
  vertical-align: middle;
  padding: 2px 7px;
  border-radius: var(--radius-sm);
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-light);
  color: var(--text-secondary);
  -webkit-text-fill-color: var(--text-secondary);
  letter-spacing: 0;
  display: inline-block;
  line-height: 1.2;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Botón Predominante de Nueva Partida */
.btn-new-game {
  background: linear-gradient(135deg, var(--accent-primary), #2563eb);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-md);
  height: 38px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: inherit;
  font-size: 0.92rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
  transition: all var(--transition-fast);
}

.btn-new-game:hover {
  background: linear-gradient(135deg, var(--accent-hover), #1d4ed8);
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.5);
  transform: translateY(-1.5px);
  filter: brightness(1.05);
}

.btn-new-game:active {
  transform: translateY(0) scale(0.97);
}

.new-game-icon {
  width: 18px;
  height: 18px;
}

.new-game-label {
  letter-spacing: 0.2px;
}

/* Botón Solucionador (Azul considerablemente más claro) */
.btn-solver {
  background: linear-gradient(135deg, #38bdf8, #0ea5e9);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-md);
  height: 38px;
  padding: 0 13px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: inherit;
  font-size: 0.90rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(14, 165, 233, 0.35);
  transition: all var(--transition-fast);
}

.btn-solver:hover {
  background: linear-gradient(135deg, #7dd3fc, #0284c7);
  box-shadow: 0 4px 14px rgba(14, 165, 233, 0.5);
  transform: translateY(-1.5px);
  filter: brightness(1.05);
}

.btn-solver:active {
  transform: translateY(0) scale(0.97);
}

.solver-icon {
  width: 17px;
  height: 17px;
}

.solver-label {
  letter-spacing: 0.2px;
}

@media (max-width: 520px) {
  .app-container {
    padding: 4px 8px 10px;
  }

  .brand-title {
    font-size: 1.25rem;
  }

  .brand-logo {
    width: 26px;
    height: 26px;
  }

  .btn-new-game, .btn-solver {
    padding: 0 8px;
    height: 34px;
    font-size: 0.8rem;
    gap: 4px;
  }

  .icon-btn {
    width: 34px;
    height: 34px;
  }

  .icon-btn svg {
    width: 17px;
    height: 17px;
  }

  [data-actions-layout="side"] {
    --actions-side-width: 68px;
  }

  [data-actions-layout="side"] .action-btn {
    font-size: 0.68rem;
    padding: 4px 2px;
  }

  [data-actions-layout="side"] .action-icon {
    width: 17px;
    height: 17px;
  }
}

@media (max-width: 370px) {
  [data-actions-layout="side"] .game-layout-stage {
    flex-direction: column;
    align-items: center;
    gap: 6px;
  }

  [data-actions-layout="side"] .game-actions {
    width: 100%;
    max-width: var(--board-size);
    height: auto;
    flex-direction: row;
    justify-content: space-between;
    gap: 4px;
    margin: 6px 0 4px;
  }

  [data-actions-layout="side"] .action-btn {
    flex: 1;
    font-size: 0.72rem;
    padding: 6px 2px;
  }
}

.icon-btn {
  background: var(--bg-surface);
  border: 1px solid var(--border-light);
  color: var(--text-secondary);
  border-radius: var(--radius-md);
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition-fast);
}

.icon-btn:hover {
  background: var(--bg-surface-elevated);
  color: var(--text-primary);
  border-color: var(--border-medium);
  transform: translateY(-1px);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
}

/* Barra de Estado del Juego */
.status-bar {
  width: 100%;
  max-width: var(--stage-width);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  padding: 6px 14px;
  margin-bottom: 8px;
  box-shadow: var(--shadow-sm);
}

.status-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.status-label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

.difficulty-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent-light);
  color: var(--accent-active);
  font-weight: 700;
  font-size: 0.85rem;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  cursor: pointer;
  border: 1px solid transparent;
  transition: all var(--transition-fast);
}

.difficulty-badge:hover {
  border-color: var(--accent-primary);
  transform: scale(1.02);
}

.timer-wrapper {
  display: flex;
  align-items: center;
  gap: 6px;
}

.timer-value {
  font-variant-numeric: tabular-nums;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-primary);
  min-width: 55px;
}

.pause-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
  border-radius: var(--radius-sm);
  transition: color var(--transition-fast);
}

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

.pause-btn svg {
  width: 18px;
  height: 18px;
}

.mistakes-counter {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-secondary);
}

.mistakes-counter.has-mistakes {
  color: var(--text-error);
}

/* Contenedor Principal del Tablero y Botonera */
.game-layout-stage {
  display: flex;
  width: 100%;
  max-width: var(--stage-width);
  justify-content: center;
  align-items: stretch;
  gap: 12px;
}

[data-actions-layout="side"] .game-layout-stage {
  flex-direction: row;
}

[data-actions-layout="bottom"] .game-layout-stage {
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

/* Área Central del Tablero */
.game-workspace {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Pantalla de Inicio / Bienvenida (Tablero Vacío Difuminado con Botón Jugar) */
.start-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(248, 250, 252, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: var(--radius-md);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 40;
  transition: opacity var(--transition-normal), visibility var(--transition-normal);
}

[data-theme="dark"] .start-overlay {
  background: rgba(15, 23, 42, 0.55);
}

[data-theme="sepia"] .start-overlay {
  background: rgba(244, 236, 216, 0.55);
}

[data-theme="oled"] .start-overlay {
  background: rgba(0, 0, 0, 0.65);
}

.start-overlay.is-active {
  display: flex;
}

.start-overlay-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  animation: welcomePop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes welcomePop {
  0% {
    opacity: 0;
    transform: scale(0.8);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

.btn-welcome-play {
  background: linear-gradient(135deg, var(--accent-primary), #2563eb);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-full);
  padding: 16px 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-family: inherit;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.45), 0 2px 6px rgba(0, 0, 0, 0.15);
  transition: all var(--transition-fast);
}

.btn-welcome-play:hover {
  background: linear-gradient(135deg, var(--accent-hover), #1d4ed8);
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 12px 32px rgba(37, 99, 235, 0.6), 0 4px 10px rgba(0, 0, 0, 0.2);
}

.btn-welcome-play:active {
  transform: translateY(0) scale(0.97);
}

.welcome-play-icon {
  width: 24px;
  height: 24px;
}

/* Overlay de Pausa */
.pause-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 23, 42, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: var(--radius-lg);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 50;
  color: #ffffff;
}

.pause-overlay.is-active {
  display: flex;
}

.pause-title {
  font-size: 1.6rem;
  font-weight: 800;
}

.btn-resume {
  background: var(--accent-primary);
  color: #ffffff;
  border: none;
  padding: 12px 28px;
  font-size: 1rem;
  font-weight: 700;
  border-radius: var(--radius-full);
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  transition: all var(--transition-fast);
}

.btn-resume:hover {
  background: var(--accent-hover);
  transform: scale(1.05);
}

/* Overlay de Generación / Transición de Partida */
.generating-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 23, 42, 0.82);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.generating-overlay.is-active {
  opacity: 1;
  pointer-events: auto;
}

.generating-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  padding: 24px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 14px;
  box-shadow: var(--shadow-xl);
  max-width: 320px;
  width: 88%;
  transform: scale(0.9);
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.generating-overlay.is-active .generating-card {
  transform: scale(1);
}

/* Spinner Cuadrícula 3x3 de Sudoku */
.mini-sudoku-spinner {
  width: 54px;
  height: 54px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 3px;
  padding: 3px;
  background: var(--border-light);
  border-radius: var(--radius-sm);
}

.spinner-cell {
  background: var(--accent-primary);
  border-radius: 2px;
  animation: spinner-pulse 1.2s infinite ease-in-out;
}

.spinner-cell:nth-child(1) {
  animation-delay: 0.0s;
}

.spinner-cell:nth-child(2) {
  animation-delay: 0.1s;
}

.spinner-cell:nth-child(3) {
  animation-delay: 0.2s;
}

.spinner-cell:nth-child(4) {
  animation-delay: 0.7s;
}

.spinner-cell:nth-child(5) {
  animation-delay: 0.8s;
}

.spinner-cell:nth-child(6) {
  animation-delay: 0.3s;
}

.spinner-cell:nth-child(7) {
  animation-delay: 0.6s;
}

.spinner-cell:nth-child(8) {
  animation-delay: 0.5s;
}

.spinner-cell:nth-child(9) {
  animation-delay: 0.4s;
}

@keyframes spinner-pulse {

  0%,
  100% {
    transform: scale(0.4);
    opacity: 0.3;
  }

  50% {
    transform: scale(1);
    opacity: 1;
    background-color: #8b5cf6;
  }
}

.generating-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.generating-title {
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--text-primary);
}

.generating-subtitle {
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.3;
}

.generating-loader-bar {
  width: 100%;
  height: 4px;
  background: var(--bg-surface-elevated);
  border-radius: var(--radius-full);
  overflow: hidden;
  position: relative;
}

.loader-bar-fill {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 50%;
  background: linear-gradient(90deg, var(--accent-primary), #8b5cf6);
  border-radius: var(--radius-full);
  animation: loader-slide 1s infinite ease-in-out;
}

@keyframes loader-slide {
  0% {
    left: -50%;
    width: 30%;
  }

  50% {
    left: 25%;
    width: 60%;
  }

  100% {
    left: 100%;
    width: 30%;
  }
}

/* Pie de Página / Herramientas Extra */
.app-footer {
  width: 100%;
  max-width: var(--stage-width);
  margin-top: 10px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

/* Estilos de Impresión */
@media print {
  body {
    background: #ffffff !important;
    color: #000000 !important;
  }

  .app-header,
  .status-bar,
  .game-actions,
  .keypad-container,
  .icon-btn,
  dialog {
    display: none !important;
  }

  .app-container {
    max-width: 100% !important;
    padding: 0 !important;
    margin: 0 auto !important;
  }

  .sudoku-board {
    width: 90vw !important;
    max-width: 500px !important;
    border: 3px solid #000000 !important;
    box-shadow: none !important;
  }

  .sudoku-cell {
    border: 1px solid #666666 !important;
    color: #000000 !important;
  }

  .sudoku-cell.border-right-box {
    border-right: 3px solid #000000 !important;
  }

  .sudoku-cell.border-bottom-box {
    border-bottom: 3px solid #000000 !important;
  }

  .cell-value {
    color: #000000 !important;
    font-weight: 700 !important;
  }
}