/* ========================================
   Pathfinder Visualizer Styles
   Overrides & additions on top of the shared
   CSS from the sorting visualizer.
   ======================================== */

/* ---- Layout overrides ---- */
main {
  padding: 0 1rem 2rem;
}

/* _base.css uses !important on .active-section -- match that */
section[role="tabpanel"]:not(.active-section) {
  display: none !important;
}

/* ---- Back link in header ---- */
/* ---- Advanced Options override (pathfinder uses data-open attribute) ---- */
.advanced-content[data-open="true"] {
  display: flex;
}

.advanced-toggle[aria-expanded="true"] .toggle-icon {
  transform: rotate(180deg);
}

.back-link {
  color: var(--text-secondary);
  text-decoration: none;
  display: inline-flex;
  transition: color var(--transition);
}

.back-link:hover {
  color: var(--text-primary);
}

/* ---- Grid Legend ---- */
.grid-legend {
  display: flex;
  gap: 1rem;
  justify-content: center;
  padding: 0.5rem 0;
  flex-wrap: wrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.legend-swatch {
  width: 16px;
  height: 16px;
  border-radius: 3px;
  border: 1px solid var(--border-color);
}

.legend-start {
  background: #2ecc71;
}

.legend-end {
  background: #e74c3c;
}

.legend-wall {
  background: #1a1a2e;
}

[data-theme="light"] .legend-wall {
  background: #2c3e50;
}

.legend-weight {
  background: #9b59b6;
}

.legend-visited {
  background: rgba(74, 144, 217, 0.5);
}

.legend-weight-visited {
  background: rgba(142, 68, 173, 0.55);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.legend-weight-visited::after {
  content: '';
  width: 7px;
  height: 7px;
  background: rgba(255, 255, 255, 0.55);
  transform: rotate(45deg);
  position: absolute;
}

.legend-path {
  background: #f5c842;
}

/* ---- Grid Container ---- */
.grid-container {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: auto;
  background: var(--bg-secondary);
  user-select: none;
  min-height: 400px;
  height: calc(100vh - 340px);
  box-sizing: border-box;
}

.grid-container canvas,
.step-grid-wrap canvas {
  display: block;
  cursor: pointer;
  border-radius: 2px;
}

/* ---- Cell state animations (kept for DOM-fallback if any) ---- */
.cell-empty {
  background: var(--bg-primary);
}

.cell-wall {
  background: #1a1a2e;
  animation: wallAppear 0.15s ease-out;
}

[data-theme="light"] .cell-wall {
  background: #2c3e50;
}

@keyframes wallAppear {
  from {
    transform: scale(0.3);
    opacity: 0;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

.cell-start {
  background: #2ecc71;
  z-index: 2;
}

.cell-end {
  background: #e74c3c;
  z-index: 2;
}

.cell-weight {
  background: #9b59b6;
}

.cell-weight::after {
  content: '';
  width: 40%;
  height: 40%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.4);
}

.cell-visited {
  background: rgba(74, 144, 217, 0.45);
  animation: visitPulse 0.4s ease-out;
}

@keyframes visitPulse {
  0% {
    background: rgba(74, 144, 217, 0.05);
    transform: scale(0.3);
    border-radius: 50%;
  }

  50% {
    background: rgba(74, 144, 217, 0.7);
  }

  100% {
    background: rgba(74, 144, 217, 0.45);
    transform: scale(1);
    border-radius: 0;
  }
}

.cell-visited-a {
  background: rgba(46, 204, 113, 0.35);
  animation: visitPulse 0.4s ease-out;
}

.cell-visited-b {
  background: rgba(231, 76, 60, 0.35);
  animation: visitPulse 0.4s ease-out;
}

.cell-path {
  background: #f5c842;
  animation: pathDraw 0.3s ease-out;
  z-index: 1;
}

@keyframes pathDraw {
  0% {
    background: #f5c842;
    transform: scale(0.4);
    border-radius: 50%;
  }

  70% {
    transform: scale(1.1);
  }

  100% {
    transform: scale(1);
    border-radius: 0;
  }
}

.cell-current {
  background: #f39c12;
  z-index: 2;
}

.cell-frontier {
  background: rgba(243, 156, 18, 0.3);
}

/* ---- Weight-aware cell states ---- */
.cell-visited-weight {
  background: rgba(142, 68, 173, 0.55);
  animation: visitPulse 0.4s ease-out;
}

.cell-visited-weight::after {
  content: '';
  width: 36%;
  height: 36%;
  background: rgba(255, 255, 255, 0.55);
  transform: rotate(45deg);
  position: absolute;
}

.cell-frontier-weight {
  background: rgba(155, 89, 182, 0.30);
}

.cell-frontier-weight::after {
  content: '';
  width: 30%;
  height: 30%;
  background: rgba(255, 255, 255, 0.35);
  transform: rotate(45deg);
  position: absolute;
}

.cell-path-weight {
  background: linear-gradient(135deg, #f5c842 40%, #9b59b6 100%);
  animation: pathDraw 0.3s ease-out;
  z-index: 1;
}

.cell-path-weight::after {
  content: '';
  width: 36%;
  height: 36%;
  background: rgba(255, 255, 255, 0.6);
  transform: rotate(45deg);
  position: absolute;
}

/* ---- Light mode overrides for weight indicators ---- */
[data-theme="light"] .cell-weight::after {
  background: rgba(0, 0, 0, 0.2);
}

[data-theme="light"] .cell-visited-weight::after {
  background: rgba(0, 0, 0, 0.25);
}

[data-theme="light"] .cell-frontier-weight::after {
  background: rgba(0, 0, 0, 0.2);
}

[data-theme="light"] .cell-path-weight::after {
  background: rgba(0, 0, 0, 0.3);
}

[data-theme="light"] .legend-weight-visited::after {
  background: rgba(0, 0, 0, 0.25);
}

/* ---- Step Mode -- pathfinding-specific layout ---- */
.step-header {
  text-align: center;
  padding: 1rem 0 0.5rem;
}

.step-header h2 {
  font-size: 1.2rem;
  margin-bottom: 0.25rem;
}

.step-header p {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.step-controls {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin-bottom: 0.75rem;
}

.step-config {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.step-actions {
  display: flex;
  gap: 0.5rem;
}

.step-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin-bottom: 0.75rem;
  font-size: 0.85rem;
}

.step-counter {
  font-weight: 600;
  color: var(--text-primary);
}

.step-description {
  color: var(--text-secondary);
  flex: 1;
  text-align: center;
  padding: 0 1rem;
}

.step-stats {
  display: flex;
  gap: 1rem;
  color: var(--text-muted);
}

.step-stats strong {
  color: var(--text-primary);
}

.step-split-container {
  display: flex;
  flex: 1;
  gap: 0;
  min-height: 0;
}

.step-vis-pane {
  flex: 2;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.step-grid-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: auto;
  background: var(--bg-secondary);
  user-select: none;
  min-height: 300px;
  height: calc(100vh - 400px);
  box-sizing: border-box;
}

.step-code-resizer {
  width: 6px;
  cursor: col-resize;
  background: var(--border-color);
  transition: background var(--transition);
  flex-shrink: 0;
}

.step-code-resizer:hover,
.step-code-resizer:focus {
  background: var(--bar-default);
}

.step-code-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 250px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.pseudocode-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border-color);
}

.pseudocode-title {
  font-weight: 600;
  font-size: 0.9rem;
}

.pseudocode-container,
#state-container {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  background: var(--bg-secondary);
}

.state-section {
  margin-bottom: 1.25rem;
}

.state-section h4 {
  font-size: 0.78rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 0.5rem;
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--border-color);
}

.state-list {
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-family: 'Courier New', monospace;
  max-height: 150px;
  overflow-y: auto;
  word-wrap: break-word;
}

.state-value {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-primary);
}

/* ---- Compare Mode -- pathfinding-specific overrides ---- */
/* The shared _compare.css is built for sorting bar charts (fixed 280px height,
   1rem padding, 220px min columns). Pathfinding grids need auto-height square
   panels with the canvas filling available space. */

.compare-header {
  text-align: center;
  padding: 1rem 0 0.5rem;
}

.compare-header h2 {
  font-size: 1.2rem;
  margin-bottom: 0.25rem;
}

.compare-header p {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.compare-controls {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin-bottom: 0.75rem;
}

.compare-config {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.compare-actions {
  display: flex;
  gap: 0.5rem;
}

.race-count {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 0.75rem;
  background: none;
  padding: 0;
}

.race-count span {
  font-weight: 700;
  color: var(--text-primary);
}

/* Override shared grid: wider min columns for pathfinding grids */
.compare-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 1rem;
  flex: 1;
  width: 100%;
}

/* Override shared panel: remove fixed height & padding (pathfinder has its own
   internal structure with .compare-panel-header + .compare-panel-grid) */
.compare-panel {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg-secondary);
  height: auto;
  padding: 0;
}

.compare-panel:hover {
  transform: none;
  box-shadow: none;
}

.compare-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.75rem;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.compare-panel-header select {
  appearance: auto;
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 0.25rem 0.4rem;
  font-family: 'Outfit', sans-serif;
  font-size: 0.8rem;
}

.compare-panel-stats {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.compare-panel-stats span {
  color: var(--text-primary);
  font-weight: 600;
}

.compare-panel-grid {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  min-height: 260px;
  user-select: none;
  overflow: hidden;
  padding: 0.5rem;
  background: var(--bg-primary);
}

.compare-panel-grid canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
}

.compare-panel .btn-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 0.2rem;
  transition: color var(--transition);
}

.compare-panel .btn-remove:hover {
  color: var(--bar-swap);
}

/* ---- Compare Editor Overlay (fullscreen modal) ---- */
.compare-editor {
  position: fixed;
  inset: 0;
  z-index: 1050;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding: 3rem;
}

.compare-editor-content {
  width: 100%;
  height: 100%;
  max-width: 1200px;
  max-height: 900px;
  background: var(--bg-secondary);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.compare-editor-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border-color);
}

.compare-editor-close {
  background: rgba(255, 255, 255, 0.05);
  border: none;
  color: var(--text-primary);
  font-size: 1.4rem;
  cursor: pointer;
  line-height: 1;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.compare-editor-close:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: rotate(90deg);
}

.compare-editor-title {
  font-weight: 600;
  font-size: 1.1rem;
  color: var(--text-primary);
}

.compare-editor-hint {
  flex: 1;
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
}

.compare-editor-actions {
  display: flex;
  gap: 0.75rem;
}

.compare-editor-canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  overflow: auto;
  background-image:
    radial-gradient(circle at 2px 2px, rgba(255, 255, 255, 0.03) 1px, transparent 0);
  background-size: 24px 24px;
}

.compare-editor-canvas-wrap canvas {
  background: var(--bg-primary);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  border-radius: 4px;
  border: 1px solid var(--border-color);
  cursor: crosshair;
}

[data-theme="light"] .compare-editor {
  background: rgba(255, 255, 255, 0.3);
}

[data-theme="light"] .compare-editor-content {
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.15);
  background: #ffffff;
}

[data-theme="light"] .compare-editor-close {
  background: rgba(0, 0, 0, 0.05);
}

[data-theme="light"] .compare-editor-close:hover {
  background: rgba(0, 0, 0, 0.1);
}

[data-theme="light"] .compare-editor-canvas-wrap {
  background-image:
    radial-gradient(circle at 2px 2px, rgba(0, 0, 0, 0.03) 1px, transparent 0);
}

/* ---- Leaderboard ---- */
.leaderboard {
  margin-top: 1.5rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1.5rem;
}

.leaderboard h3 {
  text-align: center;
  margin-bottom: 1rem;
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.leaderboard-table th,
.leaderboard-table td {
  padding: 0.6rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
}

.leaderboard-table th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.leaderboard-table tbody tr:hover {
  background: var(--bg-tertiary);
}

/* ---- Pathfinding-specific responsive ---- */
@media (max-width: 768px) {
  .step-split-container {
    flex-direction: column;
  }

  .step-code-resizer {
    display: none;
  }

  .step-code-pane {
    min-height: 200px;
  }

  .compare-grid {
    grid-template-columns: 1fr;
  }

  .compare-panel {
    height: auto;
  }

  .compare-panel-grid {
    min-height: 200px;
  }
}

/* ---- Custom Mode -- pathfinding grid overrides ---- */
/* The shared _custom-mode.css aligns bars to flex-end. Pathfinding
   grids need centered canvas display instead. */
.custom-vis-box {
  align-items: center;
  justify-content: center;
  background: var(--bg-main, #0d1117);
}

.custom-vis-box canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
}

#section-custom .custom-controls {
  flex-wrap: wrap;
}

/* Fix: shared _custom-mode.css styles the textarea via #custom-code-editor
   (sorting ID). The pathfinder uses #pf-custom-code-editor, so duplicate
   the essential textarea styles here. */
#pf-custom-code-editor {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 1rem;
  border: none;
  resize: none;
  background: transparent;
  color: #e6edf3;
  caret-color: #e6edf3;
  font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.5;
  tab-size: 4;
  -moz-tab-size: 4;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  overflow: auto;
  outline: none;
  box-sizing: border-box;
  z-index: 1;
}