/* =================== RESET & BASE =================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #0f1117;
  --bg2:        #171b26;
  --bg3:        #1e2330;
  --bg-glass:   rgba(23, 27, 38, 0.85);
  --border:     rgba(255,255,255,0.08);
  --border2:    rgba(255,255,255,0.14);
  --text:       #e8eaf0;
  --text2:      #9aa0b4;
  --text3:      #5a6075;

  --purple:     #7c6ff7;
  --purple-l:   #a89ef9;
  --purple-bg:  rgba(124,111,247,0.12);
  --blue:       #4b9cf5;
  --blue-l:     #7db8f8;
  --blue-bg:    rgba(75,156,245,0.12);
  --amber:      #f5a623;
  --amber-l:    #f8c268;
  --amber-bg:   rgba(245,166,35,0.12);
  --teal:       #2ec4a7;
  --teal-l:     #62d9c4;
  --teal-bg:    rgba(46,196,167,0.12);
  --coral:      #f0614e;
  --coral-l:    #f58e80;
  --coral-bg:   rgba(240,97,78,0.12);
  --green:      #4dbb6e;
  --green-l:    #79cc92;
  --green-bg:   rgba(77,187,110,0.12);
  --root-c:     #f5a623;

  --radius-sm:  6px;
  --radius-md:  10px;
  --radius-lg:  16px;
  --radius-xl:  22px;

  --shadow-node: 0 4px 20px rgba(0,0,0,0.5);
  --shadow-panel: 0 8px 40px rgba(0,0,0,0.6);
  --header-h:   70px;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* =================== HEADER =================== */
.site-header {
  position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  background: var(--bg-glass);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  gap: 16px;
}

.header-logo {
  display: flex;
  align-items: center;
  gap: 14px;
}

.logo-icon {
  font-size: 28px;
  width: 46px; height: 46px;
  background: var(--amber-bg);
  border: 1px solid rgba(245,166,35,0.3);
  border-radius: var(--radius-md);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.header-logo h1 {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.2;
}

.header-logo p {
  font-size: 12px;
  color: var(--text2);
  margin-top: 2px;
}

.header-controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

.header-controls button {
  background: var(--bg3);
  border: 1px solid var(--border2);
  color: var(--text2);
  font-size: 12px;
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.header-controls button:hover {
  background: var(--bg2);
  color: var(--text);
  border-color: rgba(255,255,255,0.25);
}

.header-controls button.active {
  background: var(--amber-bg);
  border-color: rgba(245,166,35,0.35);
  color: var(--amber-l);
}

/* Progress bar */
.progress-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 0 8px;
}

.progress-label {
  font-size: 11px;
  color: var(--text3);
  white-space: nowrap;
}

.progress-track {
  flex: 1;
  height: 3px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--amber), var(--coral));
  border-radius: 3px;
  transition: width 0.4s ease;
}

/* =================== LEGEND =================== */
.legend-panel {
  position: fixed;
  top: calc(var(--header-h) + 24px + 18px);
  left: 20px;
  z-index: 90;
  background: var(--bg-glass);
  backdrop-filter: blur(16px);
  border: 1px solid var(--border2);
  border-radius: var(--radius-lg);
  padding: 16px;
  min-width: 190px;
  transition: opacity 0.3s, transform 0.3s;
}

.legend-panel.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(-10px);
}

.legend-panel h3 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
  margin-bottom: 12px;
}

.legend-items { display: flex; flex-direction: column; gap: 8px; }

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text2);
}

.legend-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot-root   { background: var(--amber); }
.dot-purple { background: var(--purple); }
.dot-blue   { background: var(--blue); }
.dot-amber  { background: var(--amber); }
.dot-teal   { background: var(--teal); }
.dot-coral  { background: var(--coral); }
.dot-green  { background: var(--green); }

.legend-tip {
  font-size: 11px;
  color: var(--text3);
  margin-top: 8px;
  line-height: 1.5;
}

/* =================== MAP CANVAS =================== */
.map-container {
  position: fixed;
  top: var(--header-h);
  left: 0; right: 0; bottom: 0;
  overflow: hidden;
  cursor: grab;
  background:
    radial-gradient(ellipse at 30% 40%, rgba(124,111,247,0.04) 0%, transparent 60%),
    radial-gradient(ellipse at 70% 70%, rgba(75,156,245,0.04) 0%, transparent 60%),
    var(--bg);
}

.map-container:active { cursor: grabbing; }

#canvas-wrapper {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

#map-svg { overflow: visible; }

/* =================== NODES =================== */
.node-group {
  cursor: pointer;
  transition: filter 0.2s;
}

.node-group:hover { filter: brightness(1.15); }

.node-group.visited .node-bg { stroke-dasharray: none; }

.node-bg {
  filter: drop-shadow(0 4px 12px rgba(0,0,0,0.4));
  transition: all 0.25s;
}

.node-group:hover .node-bg {
  filter: drop-shadow(0 6px 20px rgba(0,0,0,0.6));
}

.node-label {
  font-size: 13px;
  font-weight: 600;
  fill: #fff;
  pointer-events: none;
  text-anchor: middle;
  dominant-baseline: central;
  user-select: none;
}

.node-sublabel {
  font-size: 10px;
  font-weight: 400;
  fill: rgba(255,255,255,0.6);
  pointer-events: none;
  text-anchor: middle;
  dominant-baseline: central;
  user-select: none;
}

.node-icon {
  font-size: 20px;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
  user-select: none;
}

.node-visited-badge {
  fill: rgba(255,255,255,0.9);
  font-size: 9px;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

/* Root node pulse */
@keyframes pulse-ring {
  0%   { r: 58; opacity: 0.4; }
  70%  { r: 72; opacity: 0; }
  100% { r: 72; opacity: 0; }
}
.root-pulse { animation: pulse-ring 2.5s ease-out infinite; }

/* Edge lines */
.edge-line {
  fill: none;
  stroke-width: 1.5;
  opacity: 0.4;
  transition: opacity 0.2s, stroke-width 0.2s;
}

.edge-line.highlighted {
  opacity: 0.9;
  stroke-width: 2.5;
}

/* =================== ZOOM CONTROLS =================== */
.zoom-controls {
  position: fixed;
  bottom: 32px;
  right: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  z-index: 90;
}

.zoom-controls button {
  width: 36px; height: 36px;
  background: var(--bg-glass);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border2);
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
  display: flex; align-items: center; justify-content: center;
}

.zoom-controls button:hover {
  background: var(--bg2);
  border-color: rgba(255,255,255,0.25);
}

#zoom-label {
  font-size: 11px;
  color: var(--text3);
  text-align: center;
}

/* =================== DETAIL PANEL =================== */
.overlay {
  position: fixed; inset: 0;
  z-index: 200;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(4px);
  transition: opacity 0.3s;
}

.overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.detail-panel {
  position: fixed;
  top: 0; right: 0;
  width: min(480px, 100vw);
  height: 100%;
  z-index: 300;
  background: var(--bg2);
  border-left: 1px solid var(--border2);
  padding: 32px 28px;
  overflow-y: auto;
  transform: translateX(0);
  transition: transform 0.35s cubic-bezier(0.4,0,0.2,1);
  display: flex;
  flex-direction: column;
  gap: 0;
}

.detail-panel.hidden {
  transform: translateX(100%);
}

.detail-close {
  position: absolute;
  top: 16px; right: 16px;
  width: 32px; height: 32px;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text2);
  font-size: 18px;
  border-radius: 50%;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.2s;
}
.detail-close:hover { color: var(--text); border-color: var(--border2); }

.detail-category {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 10px;
  opacity: 0.8;
}

.detail-panel h2 {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.25;
  margin-bottom: 6px;
}

.detail-icon {
  font-size: 36px;
  margin: 12px 0;
  line-height: 1;
}

#detail-body {
  font-size: 14px;
  color: var(--text2);
  line-height: 1.7;
  margin-top: 4px;
}

#detail-body p { margin-bottom: 10px; }
#detail-body p:last-child { margin-bottom: 0; }

#detail-body strong {
  color: var(--text);
  font-weight: 600;
}

.detail-facts {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.fact-item {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.fact-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
}

.fact-value {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}

.detail-nav {
  margin-top: auto;
  padding-top: 24px;
  display: flex;
  gap: 10px;
}

.detail-nav button {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--border2);
  color: var(--text2);
  font-size: 13px;
  padding: 10px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
}

.detail-nav button:hover {
  color: var(--text);
  border-color: rgba(255,255,255,0.25);
}

/* Children sub-list in detail panel */
.detail-children {
  margin-top: 20px;
}

.detail-children h4 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
  margin-bottom: 10px;
}

.child-tag {
  display: inline-block;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text2);
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 20px;
  margin: 0 4px 6px 0;
  cursor: pointer;
  transition: all 0.2s;
}
.child-tag:hover {
  color: var(--text);
  border-color: var(--border2);
  background: var(--bg2);
}

/* =================== TOAST =================== */
.toast {
  position: fixed;
  bottom: 32px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-glass);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border2);
  color: var(--text);
  font-size: 13px;
  padding: 10px 20px;
  border-radius: 100px;
  z-index: 9999;
  opacity: 0;
  transition: all 0.3s;
  pointer-events: none;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* =================== GRID BACKGROUND =================== */
.map-container::before {
  content: '';
  position: absolute; inset: 0;
  background-image:
    linear-gradient(rgba(255,255,255,0.015) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.015) 1px, transparent 1px);
  background-size: 48px 48px;
  pointer-events: none;
}

/* =================== SCROLLBAR =================== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 4px; }

/* =================== RESPONSIVE =================== */
@media (max-width: 700px) {
  .header-logo h1 { font-size: 14px; }
  .header-logo p { display: none; }
  .legend-panel { display: none; }
  .detail-panel { width: 100vw; }
  .header-controls button:not(#btn-legend) { display: none; }
}
