:root {
  --viz-bg: #0f172a;
  --viz-card-bg: rgba(30, 41, 59, 0.7);
  --viz-border: rgba(148, 163, 184, 0.1);
  --viz-primary: #818cf8;
  --viz-secondary: #fb923c;
  --viz-success: #4ade80;
  --viz-error: #f87171;
  --viz-text-main: #f1f5f9;
  --viz-text-muted: #94a3b8;
  --viz-blur: 12px;
}

body {
  margin: 0;
  padding: 20px;
  font-family: "Segoe UI", "Microsoft JhengHei", sans-serif;
  background: var(--viz-bg);
  color: var(--viz-text-main);
  overflow-x: hidden;
}

/* Glassmorphism Card */
.viz-card {
  background: var(--viz-card-bg);
  backdrop-filter: blur(var(--viz-blur));
  -webkit-backdrop-filter: blur(var(--viz-blur));
  border: 1px solid var(--viz-border);
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
}

.viz-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--viz-text-muted);
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.viz-title::before {
  content: "";
  width: 4px;
  height: 18px;
  background: var(--viz-primary);
  border-radius: 2px;
  box-shadow: 0 0 10px var(--viz-primary); /* Neon glow */
}

/* Chart Container */
.chart-container {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  height: 280px;
  gap: 8px;
  padding: 20px;
  border-radius: 12px;
  margin-bottom: 24px;
  position: relative;
  background: rgba(0, 0, 0, 0.2);
}

.bar {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 40px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.bar-fill {
  width: 100%;
  border-radius: 6px 6px 0 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 6px;
  font-weight: bold;
  font-size: 1rem;
  color: white;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  background: var(--viz-primary);
  opacity: 0.8;
  transition: all 0.3s;
}

.bar-index {
  margin-top: 8px;
  color: var(--viz-text-muted);
  font-family: monospace;
}

/* States */
.bar-fill.current {
  background: var(--viz-secondary);
  box-shadow: 0 0 15px var(--viz-secondary);
  opacity: 1;
  z-index: 2;
}

.bar-fill.in-stack {
  background: var(--viz-success);
  opacity: 0.9;
}

.bar-fill.calculating {
  background: var(--viz-error);
  box-shadow: 0 0 15px var(--viz-error);
  z-index: 3;
  transform: scale(1.05);
}

.bar-fill.area-highlight {
  border: 2px dashed #fcd34d;
  background: rgba(251, 191, 36, 0.3);
}

/* Controls */
.controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  margin-top: 24px;
}

.viz-btn {
  padding: 10px 24px;
  border: 1px solid var(--viz-border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--viz-text-main);
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 8px;
}

.viz-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.1);
  border-color: var(--viz-text-muted);
}

.viz-btn.primary {
  background: var(--viz-primary);
  border-color: var(--viz-primary);
  color: #0f172a;
  font-weight: 600;
}

.viz-btn.primary:hover:not(:disabled) {
  background: #a5b4fc;
  box-shadow: 0 0 15px rgba(129, 140, 248, 0.4);
}

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

/* Step Indicator */
.step-indicator {
  background: rgba(0, 0, 0, 0.3);
  padding: 8px 16px;
  border-radius: 20px;
  font-family: monospace;
}

.step-indicator span {
  color: var(--viz-primary);
  font-weight: bold;
}

/* State Grid */
.state-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 16px;
  margin-top: 20px;
}

.state-item {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 12px;
  padding: 16px;
  text-align: center;
  border: 1px solid transparent;
  transition: border-color 0.3s;
}

.state-item:hover {
  border-color: rgba(255, 255, 255, 0.1);
}

.state-label {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--viz-text-muted);
  margin-bottom: 8px;
}

.state-value {
  font-family: "Consolas", monospace;
  font-size: 1.4rem;
  font-weight: 700;
}

/* Explanation */
.explanation {
  color: var(--viz-text-muted);
  line-height: 1.6;
  min-height: 80px;
}

.explanation h3 {
  color: var(--viz-text-main);
  font-size: 1rem;
  margin: 0 0 8px 0;
}

.explanation .highlight {
  color: var(--viz-secondary);
  font-weight: bold;
}

/* Code Panel */
.code-panel {
  background: #0d1117;
  border-radius: 12px;
  padding: 16px;
  font-family: "Consolas", "Monaco", "Andale Mono", monospace;
  font-size: 0.9rem;
  line-height: 1.5;
  overflow-x: auto;
  border: 1px solid var(--viz-border);
  white-space: pre; /* Prevent text wrapping */
}

.code-line {
  display: block;
  padding: 2px 12px; /* More horizontal padding */
  border-radius: 4px;
  margin-bottom: 2px;
}

.code-line.active {
  background: rgba(129, 140, 248, 0.2);
  color: white;
  border-left: 2px solid var(--viz-primary);
}

/* Layout */
.main-layout {
  display: grid;
  grid-template-columns: 55% 45%; /* Reduced viz area, wider code */
  gap: 20px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 10px;
}

/* Code panel adjustments for full visibility */
.code-panel {
  font-size: 0.75rem; /* Smaller font to fit more code */
  line-height: 1.4;
  padding: 12px;
}

@media (max-width: 1024px) {
  .main-layout {
    grid-template-columns: 1fr;
  }
}

/* Auto Play Button */
.viz-btn.play-btn {
  background: linear-gradient(135deg, #10b981, #059669);
  border-color: #059669;
  color: white;
  min-width: 120px;
  justify-content: center;
}

.viz-btn.play-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #34d399, #10b981);
  box-shadow: 0 0 15px rgba(16, 185, 129, 0.4);
}

.viz-btn.play-btn.playing {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  border-color: #d97706;
}

.viz-btn.play-btn.playing:hover:not(:disabled) {
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  box-shadow: 0 0 15px rgba(245, 158, 11, 0.4);
}

/* Speed Control */
.speed-control {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0, 0, 0, 0.3);
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
}

.speed-control label {
  color: var(--viz-text-muted);
}

.speed-control select {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--viz-border);
  border-radius: 6px;
  color: var(--viz-text-main);
  padding: 4px 8px;
  font-family: inherit;
  cursor: pointer;
}

.speed-control select:focus {
  outline: none;
  border-color: var(--viz-primary);
}

/* Responsive Design for Mobile */
@media (max-width: 768px) {
  body {
    padding: 10px;
  }

  .main-layout {
    grid-template-columns: 1fr;
    gap: 15px;
  }

  .viz-card {
    padding: 16px;
  }

  .controls {
    flex-wrap: wrap;
    gap: 10px;
  }

  .viz-btn {
    padding: 8px 16px;
    font-size: 0.9rem;
  }

  .code-panel {
    font-size: 0.7rem;
    max-height: 300px;
    overflow-y: auto;
  }

  .chart-container {
    height: 200px;
  }

  .state-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Controls Row 2 for Auto Play */
.controls-row-2 {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  margin-top: 12px;
}

/* ============================================
   Light Theme Support
   ============================================ */
body.light-theme {
  --viz-bg: #f8fafc;
  --viz-card-bg: rgba(255, 255, 255, 0.9);
  --viz-border: rgba(148, 163, 184, 0.3);
  --viz-text-main: #1e293b;
  --viz-text-muted: #64748b;
}

body.light-theme .code-panel {
  background: #f1f5f9;
  border-color: #e2e8f0;
}

body.light-theme .code-line.active {
  background: rgba(129, 140, 248, 0.3);
  color: #1e293b;
}

/* Theme Toggle Button */
.theme-toggle {
  position: fixed;
  top: 10px;
  right: 10px;
  background: var(--viz-card-bg);
  border: 1px solid var(--viz-border);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  z-index: 1000;
  transition: all 0.3s;
}

.theme-toggle:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

/* Custom Input Section */
.custom-input-section {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 12px;
  padding: 16px;
  margin-top: 16px;
}

.custom-input-section label {
  display: block;
  color: var(--viz-text-muted);
  font-size: 0.85rem;
  margin-bottom: 8px;
}

.custom-input-section input,
.custom-input-section textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--viz-border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--viz-text-main);
  font-family: 'Consolas', monospace;
  font-size: 0.9rem;
  box-sizing: border-box;
}

.custom-input-section input:focus,
.custom-input-section textarea:focus {
  outline: none;
  border-color: var(--viz-primary);
}

.custom-input-section button {
  margin-top: 12px;
  padding: 10px 20px;
  background: var(--viz-primary);
  border: none;
  border-radius: 8px;
  color: white;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.custom-input-section button:hover {
  background: #a5b4fc;
  transform: translateY(-1px);
}

body.light-theme .custom-input-section {
  background: rgba(0, 0, 0, 0.05);
}

/* ==================================
   Progress Tracking Button
   ================================== */
.completion-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  margin: 12px 0;
  border: 2px solid var(--md-primary-fg-color, #5c6bc0);
  border-radius: 24px;
  background: transparent;
  color: var(--md-primary-fg-color, #5c6bc0);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.completion-btn:hover {
  background: var(--md-primary-fg-color, #5c6bc0);
  color: white;
  transform: translateY(-1px);
}

.completion-btn.completed {
  background: linear-gradient(135deg, #10b981, #059669);
  border-color: transparent;
  color: white;
}

.completion-btn.completed:hover {
  background: linear-gradient(135deg, #059669, #047857);
}

/* ==================================
   Legend (顏色圖例)
   ================================== */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  padding: 10px 16px;
  margin-top: 12px;
  background: rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  font-size: 0.8rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--viz-text-muted);
}

.legend-color {
  width: 16px;
  height: 16px;
  border-radius: 4px;
}

.legend-color.normal { background: var(--viz-primary); }
.legend-color.current { background: var(--viz-secondary); }
.legend-color.in-stack { background: var(--viz-success); }
.legend-color.calculating { background: var(--viz-error); }

/* ==================================
   Complexity Badge (複雜度標記)
   ================================== */
.complexity-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  margin: 8px 0;
  background: linear-gradient(135deg, rgba(129, 140, 248, 0.2), rgba(99, 102, 241, 0.1));
  border: 1px solid rgba(129, 140, 248, 0.3);
  border-radius: 20px;
  font-size: 0.8rem;
  color: var(--viz-text-main);
}

.complexity-badge .label {
  font-weight: 600;
  color: var(--viz-primary);
}

body.light-theme .complexity-badge {
  background: linear-gradient(135deg, rgba(129, 140, 248, 0.15), rgba(99, 102, 241, 0.08));
}

/* ==================================
   Enhanced Explanation
   ================================== */
.explanation .formula {
  display: block;
  margin-top: 10px;
  padding: 10px 14px;
  background: rgba(251, 146, 60, 0.15);
  border-left: 3px solid var(--viz-secondary);
  border-radius: 0 8px 8px 0;
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 0.9rem;
  color: var(--viz-secondary);
}
