/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f0f13;
  --bg-surface: #1a1a24;
  --bg-elevated: #22222f;
  --bg-hover: #2a2a3a;
  --border: #2e2e3e;
  --border-light: #3a3a4e;
  --text: #e8e8f0;
  --text-muted: #8888a0;
  --text-dim: #5e5e78;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --accent-glow: rgba(99, 102, 241, 0.15);
  --success: #34d399;
  --success-glow: rgba(52, 211, 153, 0.15);
  --cell-filled: #e8e8f0;
  --cell-empty: transparent;
  --cell-cross: #4a4a60;
  --radius: 8px;
  --radius-lg: 12px;
  --transition: 150ms ease;
}

html {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100vh;
  display: flex;
  justify-content: center;
}

#app {
  width: 100%;
  max-width: 900px;
  padding: 2rem 1.5rem;
}

/* === Header === */
header {
  text-align: center;
  margin-bottom: 2.5rem;
}

header h1 {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-hover) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 0.25rem;
  font-weight: 400;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  user-select: none;
}

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

.btn-primary:hover {
  background: var(--accent-hover);
  box-shadow: 0 0 20px var(--accent-glow);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn-ghost:hover {
  color: var(--text);
  border-color: var(--border-light);
  background: var(--bg-hover);
}

.btn-sm {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
}

/* === Puzzle Select === */
.puzzle-list {
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.difficulty-section {}

.difficulty-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

.difficulty-label {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.difficulty-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

.difficulty-dots {
  display: inline-flex;
  gap: 0.25rem;
}

.difficulty-progress {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
}

.puzzle-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.75rem;
}

.puzzle-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  cursor: pointer;
  transition: all var(--transition);
  position: relative;
  overflow: hidden;
}

.puzzle-card:hover {
  border-color: var(--accent);
  background: var(--bg-elevated);
  transform: translateY(-1px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.puzzle-card.completed {
  border-color: var(--success);
}

.puzzle-card.completed::after {
  content: '\2713';
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  color: var(--success);
  font-size: 0.9rem;
  font-weight: 600;
}

.puzzle-card-name {
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 0.35rem;
}

.puzzle-card-meta {
  color: var(--text-muted);
  font-size: 0.8rem;
}

.puzzle-card-difficulty {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-dim);
}

.difficulty-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-dim);
}

.difficulty-dot.active {
  background: var(--accent);
}

/* === Game Area === */
.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
  gap: 1rem;
}

.game-info {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.puzzle-title {
  font-weight: 600;
  font-size: 1rem;
}

.puzzle-meta {
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* === Tool Toggle === */
.game-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.tool-toggle {
  display: flex;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.tool-btn {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.8rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
}

.tool-btn.active {
  background: var(--accent);
  color: white;
}

.tool-btn:not(.active):hover {
  background: var(--bg-hover);
  color: var(--text);
}

.tool-icon {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
}

.fill-icon {
  background: var(--cell-filled);
}

.tool-btn.active .fill-icon {
  background: white;
}

.cross-icon {
  position: relative;
  border: 1.5px solid var(--cell-cross);
}

.tool-btn.active .cross-icon {
  border-color: rgba(255, 255, 255, 0.6);
}

.cross-icon::before,
.cross-icon::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 70%;
  height: 1.5px;
  background: var(--cell-cross);
}

.tool-btn.active .cross-icon::before,
.tool-btn.active .cross-icon::after {
  background: rgba(255, 255, 255, 0.6);
}

.cross-icon::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.cross-icon::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

/* === Grid === */
.grid-wrapper {
  display: flex;
  justify-content: center;
  overflow-x: auto;
  padding: 1rem 0;
}

.grid-container {
  display: grid;
  gap: 0;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 2px;
  user-select: none;
}

.corner {
  background: var(--bg-surface);
  border-radius: var(--radius-lg) 0 0 0;
}

/* Column clues */
.col-clues {
  display: flex;
}

.col-clue {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  padding: 0.25rem 0;
  gap: 1px;
  min-width: 32px;
}

.col-clue span {
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--text-muted);
  line-height: 1.2;
}

.col-clue.satisfied span {
  color: var(--text-dim);
}

/* Row clues */
.row-clues {
  display: flex;
  flex-direction: column;
}

.row-clue {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 0.4rem;
  gap: 3px;
  min-height: 32px;
}

.row-clue span {
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--text-muted);
}

.row-clue.satisfied span {
  color: var(--text-dim);
}

/* Grid cells */
.grid {
  display: grid;
  gap: 1px;
  background: var(--border);
  border-radius: 0 0 var(--radius-lg) 0;
  overflow: hidden;
}

.cell {
  width: 32px;
  height: 32px;
  background: var(--bg-elevated);
  cursor: pointer;
  transition: background var(--transition);
  position: relative;
}

.cell:hover {
  background: var(--bg-hover);
}

.cell.filled {
  background: var(--cell-filled);
}

.cell.filled:hover {
  background: #d0d0e0;
}

.cell.crossed {
  background: var(--bg-elevated);
}

.cell.crossed::before,
.cell.crossed::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 50%;
  height: 2px;
  background: var(--cell-cross);
  border-radius: 1px;
}

.cell.crossed::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.cell.crossed::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

/* Auto-crossed cells (inferred from satisfied row/column) */
.cell.auto-crossed {
  background: var(--bg-elevated);
}

.cell.auto-crossed::before,
.cell.auto-crossed::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40%;
  height: 1.5px;
  background: var(--text-dim);
  border-radius: 1px;
  opacity: 0.35;
}

.cell.auto-crossed::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.cell.auto-crossed::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.cell.auto-crossed:hover {
  background: var(--bg-hover);
}

/* Grid group separators (every 5 cells) */
.cell.group-right {
  border-right: 2px solid var(--border-light);
}

.cell.group-bottom {
  border-bottom: 2px solid var(--border-light);
}

.col-clue.group-right {
  border-right: 2px solid var(--border-light);
}

.row-clue.group-bottom {
  border-bottom: 2px solid var(--border-light);
}

/* === Win Overlay === */
.win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 300ms ease;
}

.win-content {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 2.5rem;
  text-align: center;
  max-width: 360px;
  animation: slideUp 400ms ease;
}

.win-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--success-glow);
  color: var(--success);
  font-size: 1.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1rem;
}

.win-content h2 {
  font-size: 1.3rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.win-content p {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 1.5rem;
}

.win-content .btn-primary {
  padding: 0.6rem 1.5rem;
}

/* === Animations === */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* === Endless Mode === */
.endless-section {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.endless-infinity {
  font-size: 1.2rem;
  color: var(--accent);
  margin-left: 0.3rem;
}

.endless-desc {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-bottom: 1rem;
}

.size-grid {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.size-btn {
  flex: 1;
  min-width: 70px;
  padding: 0.75rem 1rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}

.size-btn:hover {
  border-color: var(--accent);
  background: var(--bg-elevated);
  box-shadow: 0 0 15px var(--accent-glow);
}

/* === Fireworks Canvas === */
#fireworks-canvas {
  position: fixed;
  inset: 0;
  z-index: 200;
  pointer-events: none;
}

#fireworks-canvas.hidden {
  display: none;
}

/* === Utility === */
.hidden {
  display: none !important;
}

/* === Responsive === */
@media (max-width: 600px) {
  #app {
    padding: 1rem;
  }

  header h1 {
    font-size: 1.5rem;
  }

  .cell {
    width: 26px;
    height: 26px;
  }

  .col-clue {
    min-width: 26px;
  }

  .row-clue {
    min-height: 26px;
  }

  .col-clue span,
  .row-clue span {
    font-size: 0.6rem;
  }

  .game-header {
    flex-wrap: wrap;
    justify-content: center;
  }
}
