:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-light: #818cf8;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-light: #273549;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #334155;
  --radius: 12px;
  --radius-sm: 8px;
  --danger: #ef4444;
  --danger-dark: #dc2626;
  --warning: #f59e0b;
  --warning-dark: #d97706;
  --success: #10b981;
  --success-dark: #059669;
  --bar-default: #6366f1;
  --bar-comparing: #f59e0b;
  --bar-swapping: #ef4444;
  --bar-sorted: #10b981;
  --bar-pivot: #a78bfa;
}

* {
  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: 2rem;
  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 */
main {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

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

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

.controls-actions {
  justify-content: center;
}

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

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

select,
input[type="range"] {
  width: 100%;
}

select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.75rem;
  font-size: 0.9rem;
  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' viewBox='0 0 12 12'%3E%3Cpath fill='%2394a3b8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2rem;
}

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

/* Range slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  background: var(--bg);
  border-radius: 3px;
  outline: none;
  margin-top: 0.4rem;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 2px var(--primary);
  transition: transform 0.15s, box-shadow 0.15s;
}

input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.15);
  box-shadow: 0 0 0 3px var(--primary-light);
}

input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 2px var(--primary);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  font-size: 0.9rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s, opacity 0.2s;
  white-space: nowrap;
}

.btn:active:not(:disabled) {
  transform: scale(0.96);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-icon {
  font-size: 0.85em;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--primary-dark);
}

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

.btn-secondary:hover:not(:disabled) {
  background: var(--border);
}

.btn-warning {
  background: var(--warning);
  color: #1a1a1a;
}

.btn-warning:hover:not(:disabled) {
  background: var(--warning-dark);
}

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

.btn-danger:hover:not(:disabled) {
  background: var(--danger-dark);
}

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

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

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

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

/* Visualizer */
.visualizer-container {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.visualizer {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 1px;
  height: 350px;
  padding: 1rem 0.5rem 0.5rem;
}

.bar {
  flex: 1;
  background: var(--bar-default);
  border-radius: 2px 2px 0 0;
  transition: height 0.05s ease-out;
  min-width: 1px;
  max-width: 30px;
  position: relative;
}

.bar.comparing {
  background: var(--bar-comparing);
  box-shadow: 0 0 8px rgba(245, 158, 11, 0.5);
}

.bar.swapping {
  background: var(--bar-swapping);
  box-shadow: 0 0 8px rgba(239, 68, 68, 0.5);
}

.bar.sorted {
  background: var(--bar-sorted);
}

.bar.pivot {
  background: var(--bar-pivot);
  box-shadow: 0 0 8px rgba(167, 139, 250, 0.5);
}

/* Sorted animation */
@keyframes sortedPulse {
  0% { transform: scaleY(1); }
  50% { transform: scaleY(1.05); }
  100% { transform: scaleY(1); }
}

.bar.sorted-animate {
  animation: sortedPulse 0.3s ease-out;
  transform-origin: bottom;
}

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

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

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

/* Algorithm info */
.algo-info {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
}

.algo-info h3 {
  font-size: 1rem;
  color: var(--primary-light);
  margin-bottom: 0.5rem;
}

.algo-details {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.algo-complexity {
  display: flex;
  gap: 1.25rem;
  flex-wrap: wrap;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.algo-complexity strong {
  color: var(--text);
}

.algo-info p {
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.5;
}

/* Footer */
footer {
  text-align: center;
  margin-top: 1.25rem;
  color: var(--text-muted);
  font-size: 0.8rem;
}

kbd {
  display: inline-block;
  padding: 0.15em 0.45em;
  font-size: 0.85em;
  font-family: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 1px 0 var(--border);
}

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

  header h1 {
    font-size: 1.5rem;
  }

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

  .controls-row {
    flex-direction: column;
  }

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

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

  .controls-actions .btn {
    flex: 1;
    justify-content: center;
    min-width: 80px;
  }

  .visualizer {
    height: 250px;
  }

  .stats-bar {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .legend {
    gap: 0.75rem;
  }

  .algo-complexity {
    flex-direction: column;
    gap: 0.3rem;
  }
}

@media (max-width: 400px) {
  .visualizer {
    height: 200px;
    padding: 0.5rem 0.25rem 0.25rem;
  }

  .controls-actions .btn {
    padding: 0.5rem 0.6rem;
    font-size: 0.8rem;
  }
}
