/* ===========================================================
   Theme Variables
   Dark mode is the default (:root)
   Light mode is activated via [data-theme="light"]
   =========================================================== */

:root {
  /* ---- Backgrounds & Surfaces ---- */
  --bg: #0b1120;
  --surface: #151f31;
  --surface-hover: #1e3254;
  --surface-active: #243b5c;
  --surface-glass: rgba(255, 255, 255, 0.03);

  /* ---- Text ---- */
  --text: #e8edf5;
  --text-secondary: rgba(232, 237, 245, 0.65);
  --text-muted: rgba(232, 237, 245, 0.38);

  /* ---- Accent ---- */
  --accent: #2dd4bf;
  --accent-gradient: linear-gradient(135deg, #1a5a4e, #2dd4bf);
  --accent-glow: rgba(45, 212, 191, 0.25);
  --accent-dim: #176c61;

  /* ---- Borders ---- */
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.14);
  --border-light: rgba(255, 255, 255, 0.04);

  /* ---- Nodes ---- */
  --node-default: #1f2d4a;
  --node-stroke: rgba(255, 255, 255, 0.15);
  --node-source: #b45309;
  --node-queue: #375284;
  --node-visited: #176c61;
  --node-arriving: #2dd4bf;
  --arrival-stroke: rgba(251, 146, 60, 0.95);

  /* ---- Node distance badge ---- */
  --dist-bg: rgba(60, 60, 70, 0.85);
  --dist-queue-bg: rgba(212, 186, 71, 0.75);
  --dist-visited-bg: rgba(45, 212, 191, 0.8);
  --queue-top-stroke: rgba(200, 68, 45, 0.95);

  /* ---- Edges ---- */
  --edge-default: rgba(148, 163, 184, 0.40);
  --edge-finished: rgba(99, 102, 241, 0.50);
  --edge-aborted: rgba(71, 85, 105, 0.40);
  --edge-partial: rgba(244, 63, 94, 0.55);

  /* ---- Edge weight labels ---- */
  --weight-text: #e2e8f0;
  --weight-stroke: rgba(8, 13, 24, 0.88);

  /* ---- Time display ---- */
  --time-bg: rgba(20, 25, 40, 0.92);
  --time-border: rgba(200, 200, 220, 0.15);
  --time-text: #e2e8f0;

  /* ---- Queue ---- */
  --queue-marker: #2dd4bf;

  /* ---- Theme toggle ---- */
  --toggle-bg: #1e293b;
  --toggle-border: rgba(255, 255, 255, 0.10);
  --toggle-hover: #334155;
  --toggle-icon: #fbbf24;
}

/* ---- Light Mode ---- */

[data-theme="light"] {
  --bg: #f0f2f8;
  --surface: #ffffff;
  --surface-hover: #e8ebf5;
  --surface-active: #dce0ed;
  --surface-glass: rgba(0, 0, 0, 0.02);

  --text: #1a1d2e;
  --text-secondary: rgba(26, 29, 46, 0.62);
  --text-muted: rgba(26, 29, 46, 0.35);

  --accent: #0d9488;
  --accent-gradient: linear-gradient(135deg, #0d9488, #14b8a6);
  --accent-glow: rgba(13, 148, 136, 0.20);
  --accent-dim: #5eead4;

  --border: rgba(0, 0, 0, 0.07);
  --border-strong: rgba(0, 0, 0, 0.13);
  --border-light: rgba(0, 0, 0, 0.03);

  --node-default: #d1d8ec;
  --node-stroke: rgba(0, 0, 0, 0.10);
  --node-source: #f59e0b;
  --node-queue: #93c5fd;
  --node-visited: #14b8a6;
  --node-arriving: #14b8a6;
  --arrival-stroke: rgba(234, 88, 12, 0.85);

  --dist-bg: rgba(180, 185, 200, 0.75);
  --dist-queue-bg: rgba(250, 204, 21, 0.60);
  --dist-visited-bg: rgba(20, 184, 166, 0.65);
  --queue-top-stroke: rgba(220, 129, 25, 0.9);

  --edge-default: rgba(100, 116, 139, 0.30);
  --edge-finished: rgba(99, 102, 241, 0.45);
  --edge-aborted: rgba(148, 163, 184, 0.35);
  --edge-partial: rgba(225, 29, 72, 0.50);

  --weight-text: #1e293b;
  --weight-stroke: rgba(255, 255, 255, 0.95);

  --time-bg: rgba(255, 255, 255, 0.92);
  --time-border: rgba(0, 0, 0, 0.10);
  --time-text: #1e293b;

  --queue-marker: #0d9488;

  --toggle-bg: #e2e8f0;
  --toggle-border: rgba(0, 0, 0, 0.08);
  --toggle-hover: #cbd5e1;
  --toggle-icon: #475569;
}


/* ===========================================================
   Base Styles
   =========================================================== */

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: Verdana, sans-serif;
  color: var(--text);
  background: var(--bg);
  padding: 1.5rem;
  transition: background 0.35s ease, color 0.35s ease;
}

/* ---- Layout ---- */

.app-shell {
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  flex-wrap: wrap;
}

header h1 {
  margin: 0;
  font-size: 1.6rem;
}

/* ---- Theme Toggle ---- */

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.theme-toggle-btn {
  background: var(--toggle-bg);
  color: var(--text);
  border: 1px solid var(--toggle-border);
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, transform 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.theme-toggle-btn:hover {
  background: var(--toggle-hover);
  border-color: var(--border-strong);
  transform: scale(1.05);
}

.theme-toggle-btn:active {
  transform: scale(0.95);
}

.theme-toggle-btn .icon {
  transition: transform 0.4s ease;
  display: inline-block;
}

.theme-toggle-btn .icon.spin {
  transform: rotate(360deg);
}

.theme-label {
  font-size: 0.8rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

/* ---- Graph Select ---- */

.graph-select-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.graph-select-group label {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

#graph-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 0.4rem 0.75rem;
  font-size: 0.9rem;
  cursor: pointer;
  transition: border-color 0.2s ease;
}

#graph-select:hover {
  border-color: var(--accent);
}

/* ---- Main Layout ---- */

.main-layout {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 1.25rem;
  align-items: start;
}

/* ---- Viz Panel ---- */

.viz-panel {
  background: var(--surface-glass);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 0.5rem;
  transition: background 0.35s ease, border-color 0.35s ease;
}

#graph-svg {
  width: 100%;
  height: auto;
  display: block;
}

/* ---- Side Panel ---- */

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ---- Mode Buttons ---- */

.mode-buttons {
  display: flex;
  gap: 0.4rem;
}

.mode-btn {
  flex: 1;
  background: var(--surface);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.4rem;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  text-align: center;
}

.mode-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
  border-color: var(--border-strong);
}

.mode-btn.active {
  background: var(--accent-gradient);
  color: white;
  border-color: var(--accent);
  box-shadow: 0 0 12px var(--accent-glow);
}

/* ---- Controls ---- */

.controls {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  background: var(--surface-glass);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  transition: background 0.35s ease, border-color 0.35s ease;
}

.controls-row {
  display: flex;
  gap: 0.4rem;
}

.controls-row button {
  flex: 1;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.controls-row button:hover:not(:disabled) {
  background: var(--surface-hover);
  border-color: var(--border-strong);
}

.controls-row button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.speed-row {
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
}

.speed-row label {
  color: var(--text-secondary);
  white-space: nowrap;
}

#speed-range {
  flex: 1;
  accent-color: var(--accent);
  cursor: pointer;
}

#speed-label {
  min-width: 3.2rem;
  text-align: right;
  color: var(--accent);
  font-weight: 700;
}

/* ---- Table Section ---- */

.table-section {
  background: var(--surface-glass);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  transition: background 0.35s ease, border-color 0.35s ease;
}

.table-section h3 {
  margin: 0 0 0.5rem 0;
  font-size: 0.95rem;
  color: var(--text-secondary);
}

.table-wrapper {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

table th {
  text-align: left;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--border-strong);
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
}

table td {
  padding: 0.35rem 0.5rem;
  border-bottom: 1px solid var(--border-light);
}

table tr:last-child td {
  border-bottom: none;
}

/* ---- Queue List ---- */

#queue-list {
  margin: 0;
  padding-left: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

#queue-list {
  gap: 0.4rem;
}

#queue-list li {
  color: var(--text);
  font-size: 0.85rem;
  padding: 0.3rem 0.5rem;
  margin-left: -0.5rem;
  margin-right: -0.5rem;
  list-style-position: inside;
  background: var(--surface);
  border-radius: 6px;
  border: 1px solid var(--border);
}

#queue-list li::marker {
  color: var(--queue-marker);
  font-weight: 700;
}

#queue-list li.queue-item-next {
  background: var(--accent-glow);
  border-color: var(--accent);
}

/* ===========================================================
   SVG Internal Styles
   =========================================================== */

svg text {
  user-select: none;
  pointer-events: none;
}

.hidden {
  display: none !important;
}

/* ---- Nodes ---- */

.node circle {
  fill: var(--node-default);
  stroke: var(--node-stroke);
  stroke-width: 2.2;
  transition: fill 0.5s ease, stroke 0.5s ease, opacity 0.5s ease;
}

.node.source circle {
  fill: var(--node-source) !important;
}

.node.in-queue circle {
  fill: var(--node-queue);
}

.node.visited circle {
  fill: var(--node-visited);
}

.node.node-arriving circle {
  fill: var(--node-arriving);
}

/* ---- Node Labels ---- */

.node-label {
  fill: var(--text);
  font-size: 14px;
  font-weight: 800;
}

/* ---- Distance Badge ---- */

.distance-label-background {
  fill: var(--dist-bg);
  transition: fill 0.5s ease, opacity 0.5s ease;
  transform-origin: center;
  transform-box: fill-box;
}

.node.in-queue .distance-label-background {
  fill: var(--dist-queue-bg);
}

.node.visited .distance-label-background {
  fill: var(--dist-visited-bg);
}

.node .distance-label-background.queue-top {
  stroke: var(--queue-top-stroke);
  stroke-width: 2;
}

.distance-label {
  fill: var(--text);
  font-size: 12px;
  font-weight: 800;
}

/* ---- Arrival Animation ---- */

.arrival-circle {
  fill: none;
  stroke: var(--arrival-stroke);
  stroke-width: 4;
  opacity: 0;
  transform-box: fill-box;
  transform-origin: center;
}

.node-arriving .arrival-circle {
  animation: node-arrival-pulse 0.8s ease-out;
}

/* ---- Edges ---- */

.edge-line {
  stroke: var(--edge-default);
  stroke-width: 9;
  fill: none;
  transition: stroke 0.5s ease, opacity 0.5s ease;
}

.edge-line.finished-line {
  stroke: var(--edge-finished);
}

.edge-line.aborted-line {
  stroke: var(--edge-aborted);
}

.partial-edge-line {
  stroke: var(--edge-partial);
  stroke-width: 6;
  fill: none;
  transition: d 0.33s linear, stroke 0.5s ease, opacity 0.5s ease;
}

.partial-edge-line.finished-line {
  stroke: transparent;
}

/* ---- Edge Weights ---- */

.edge-weight {
  fill: var(--weight-text);
  font-size: 14px;
  font-weight: 800;
  paint-order: stroke;
  stroke: var(--weight-stroke);
  stroke-width: 4px;
  stroke-linejoin: round;
}

/* ---- Time Display ---- */

.time-display-background {
  fill: var(--time-bg);
  stroke: var(--time-border);
  stroke-width: 4px;
  stroke-linejoin: round;
  transition: fill 0.35s ease, stroke 0.35s ease;
}

.time-display-text {
  fill: var(--time-text);
  font-size: 14px;
  font-weight: 800;
  paint-order: stroke;
  stroke: var(--weight-stroke);
  stroke-width: 4px;
  stroke-linejoin: round;
}

/* ---- Distance Label Pulse ---- */

@keyframes distance-bg-pulse {
  0% {
    transform: scale(1);
    fill: var(--accent);
    opacity: 1;
  }
  50% {
    transform: scale(1.35);
    fill: var(--accent);
    opacity: 0.85;
  }
  100% {
    transform: scale(1);
  }
}

.distance-label-background.pulse {
  animation: distance-bg-pulse 0.6s ease-out;
}

/* ---- Animations ---- */

@keyframes node-arrival-pulse {
  0% {
    transform: scale(0.55);
    opacity: 0.9;
  }
  70% {
    opacity: 0.2;
  }
  100% {
    transform: scale(1.7);
    opacity: 0;
  }
}

/* ===========================================================
   Responsive
   =========================================================== */

@media (max-width: 820px) {
  body {
    padding: 1rem;
  }

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

  .side-panel {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
  }

  .mode-buttons {
    grid-column: 1 / -1;
  }

  .controls {
    grid-column: 1 / -1;
  }

  header {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (max-width: 520px) {
  .side-panel {
    grid-template-columns: 1fr;
  }
}