:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-light: #818cf8;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-light: #273548;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #334155;
  --radius: 12px;
  --radius-sm: 6px;

  --start-color: #22c55e;
  --end-color: #ef4444;
  --wall-color: #475569;
  --weight-color: #f59e0b;
  --visited-color: #6366f1;
  --visited-color-alt: #818cf8;
  --path-color: #facc15;
  --path-glow: rgba(250, 204, 21, 0.4);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 1.5rem;
}

#app {
  width: 100%;
  max-width: 1100px;
}

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

header h1 {
  font-size: 1.8rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

header p {
  color: var(--text-muted);
  margin-top: 0.4rem;
  font-size: 0.95rem;
}

/* Main container */
main {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
}

/* Controls */
.controls {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: flex-end;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  flex: 1;
  min-width: 130px;
}

.control-group label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
  transition: border-color 0.2s, box-shadow 0.2s;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2394a3b8'%3E%3Cpath d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.6rem center;
  padding-right: 1.8rem;
}

select:hover {
  border-color: var(--primary);
}

select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25);
}

/* Buttons */
.controls-actions {
  gap: 0.5rem;
}

.btn {
  padding: 0.55rem 1rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.btn-primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}

.btn-primary:hover {
  background: var(--primary-dark);
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.4);
  transform: translateY(-1px);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-primary.running {
  background: #dc2626;
  box-shadow: 0 2px 8px rgba(220, 38, 38, 0.3);
  animation: pulse-btn 1.5s ease-in-out infinite;
}

.btn-primary.running:hover {
  background: #b91c1c;
}

@keyframes pulse-btn {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.8; }
}

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

.btn-secondary:hover {
  color: var(--text);
  border-color: var(--text-muted);
  background: var(--surface-light);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn-icon {
  margin-right: 0.25rem;
}

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

#grid {
  display: grid;
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  width: 100%;
  aspect-ratio: 1;
  background: var(--bg);
  cursor: pointer;
  transition: background-color 0.1s;
  position: relative;
}

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

.cell.wall {
  background: var(--wall-color);
  animation: wall-pop 0.2s ease;
}

@keyframes wall-pop {
  0% { transform: scale(0.6); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

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

.cell.weight::after {
  content: '';
  position: absolute;
  inset: 20%;
  background: var(--weight-color);
  border-radius: 50%;
  opacity: 0.7;
  animation: weight-pop 0.2s ease;
}

@keyframes weight-pop {
  0% { transform: scale(0); }
  100% { transform: scale(1); }
}

.cell.start {
  background: var(--start-color);
  animation: node-pop 0.3s ease;
}

.cell.end {
  background: var(--end-color);
  animation: node-pop 0.3s ease;
}

@keyframes node-pop {
  0% { transform: scale(0); border-radius: 50%; }
  60% { transform: scale(1.2); }
  100% { transform: scale(1); border-radius: 0; }
}

.cell.start::after {
  content: '';
  position: absolute;
  inset: 20%;
  background: white;
  clip-path: polygon(30% 10%, 30% 90%, 85% 50%);
  opacity: 0.9;
}

.cell.end::after {
  content: '';
  position: absolute;
  inset: 18%;
  border: 3px solid white;
  border-radius: 50%;
  opacity: 0.9;
}

.cell.visited {
  background: var(--visited-color);
  animation: visited-anim 0.4s ease forwards;
}

@keyframes visited-anim {
  0% {
    transform: scale(0.3);
    background: var(--primary-light);
    border-radius: 50%;
  }
  50% {
    background: var(--visited-color-alt);
  }
  100% {
    transform: scale(1);
    background: var(--visited-color);
    border-radius: 0;
  }
}

.cell.path {
  background: var(--path-color);
  animation: path-anim 0.4s ease forwards;
  box-shadow: inset 0 0 6px var(--path-glow);
}

@keyframes path-anim {
  0% {
    transform: scale(0.5);
    background: var(--primary-light);
  }
  50% {
    transform: scale(1.15);
  }
  100% {
    transform: scale(1);
    background: var(--path-color);
  }
}

/* Keep start/end visible on top of visited/path */
.cell.start.visited,
.cell.start.path {
  background: var(--start-color);
}

.cell.end.visited,
.cell.end.path {
  background: var(--end-color);
}

/* Stats bar */
.stats-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.stat {
  flex: 1;
  min-width: 100px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.8rem;
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.15rem;
}

.stat-value {
  display: block;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--primary-light);
  font-variant-numeric: tabular-nums;
}

/* Legend */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.8rem 1.2rem;
  justify-content: center;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.legend-swatch {
  width: 16px;
  height: 16px;
  border-radius: 3px;
  border: 1px solid rgba(255,255,255,0.1);
}

.legend-start { background: var(--start-color); }
.legend-end { background: var(--end-color); }
.legend-wall { background: var(--wall-color); }
.legend-weight {
  background: var(--bg);
  position: relative;
}
.legend-weight::after {
  content: '';
  position: absolute;
  inset: 3px;
  background: var(--weight-color);
  border-radius: 50%;
  opacity: 0.7;
}
.legend-visited { background: var(--visited-color); }
.legend-path { background: var(--path-color); }

/* Shortcuts info */
.shortcuts-info {
  font-size: 0.8rem;
}

.shortcuts-info details {
  color: var(--text-muted);
}

.shortcuts-info summary {
  cursor: pointer;
  text-align: center;
  padding: 0.3rem;
  transition: color 0.2s;
}

.shortcuts-info summary:hover {
  color: var(--text);
}

.shortcuts-grid {
  display: grid;
  grid-template-columns: auto 1fr auto 1fr;
  gap: 0.3rem 0.8rem;
  padding: 0.8rem 1rem;
  margin-top: 0.5rem;
  background: var(--bg);
  border-radius: var(--radius-sm);
  align-items: center;
}

kbd {
  display: inline-block;
  background: var(--surface-light);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.15rem 0.45rem;
  font-family: 'Segoe UI', system-ui, monospace;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text);
  min-width: 1.8rem;
  text-align: center;
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

/* Responsive */
@media (max-width: 700px) {
  body {
    padding: 0.75rem;
  }

  header h1 {
    font-size: 1.3rem;
  }

  header p {
    font-size: 0.82rem;
  }

  main {
    padding: 1rem;
    gap: 1rem;
  }

  .controls-row {
    gap: 0.5rem;
  }

  .control-group {
    min-width: 100px;
  }

  .btn {
    padding: 0.5rem 0.7rem;
    font-size: 0.8rem;
  }

  .stat {
    min-width: 70px;
    padding: 0.5rem;
  }

  .stat-value {
    font-size: 1rem;
  }

  .shortcuts-grid {
    grid-template-columns: auto 1fr;
  }
}

@media (max-width: 480px) {
  .control-group {
    min-width: 45%;
    flex: 1 1 45%;
  }

  .controls-actions {
    flex-wrap: wrap;
  }

  .controls-actions .btn {
    flex: 1;
    min-width: 45%;
  }
}
