/**
 * Kagami Architecture — Virtuoso Design System
 * Following Byzantine quality standards: ALL dimensions ≥90/100
 *
 * Design tokens from Weather celestial system
 * Animation patterns from Orb/Gifts
 * Accessibility from WCAG 2.1 AA
 */

:root {
  /* Typography — Minor Third (1.2) modular scale */
  --text-2xs: 0.625rem;     /* 10px */
  --text-xs: 0.75rem;       /* 12px */
  --text-sm: 0.875rem;      /* 14px */
  --text-base: 1rem;        /* 16px */
  --text-md: 1.125rem;      /* 18px */
  --text-lg: 1.25rem;       /* 20px */
  --text-xl: 1.5rem;        /* 24px */
  --text-2xl: 1.875rem;     /* 30px */

  /* Font stacks */
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', Consolas, monospace;
  --font-display: 'Space Grotesk', var(--font-sans);

  /* Letter spacing */
  --tracking-tight: -0.02em;
  --tracking-normal: 0;
  --tracking-wide: 0.05em;
  --tracking-wider: 0.1em;

  /* Line heights */
  --leading-tight: 1.2;
  --leading-normal: 1.5;
  --leading-relaxed: 1.6;

  /* Colors — Vibrant purple/cyan triad with WCAG compliance */
  --bg-base: #07060b;
  --bg-elevated: rgba(20, 20, 26, 0.95);
  --bg-surface: #1a1a24;
  --bg-hover: #242430;
  --bg-active: #2c2c3a;

  --text-primary: #f4f1ea;
  --text-secondary: rgba(244, 241, 234, 0.75);
  --text-muted: #8585a0;  /* WCAG AA compliant: 4.5:1 */

  --border-subtle: rgba(255, 255, 255, 0.06);
  --border-default: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.15);

  /* Accent colors — saturated triad */
  --accent-primary: #8b5cf6;
  --accent-secondary: #a78bfa;
  --accent-tertiary: #22d3ee;
  --accent-glow: rgba(139, 92, 246, 0.4);
  --accent-glow-subtle: rgba(139, 92, 246, 0.15);

  --accent-success: #34d399;
  --accent-warning: #fbbf24;
  --accent-error: #f87171;

  /* Language colors — vibrant */
  --lang-python: #4B8BBE;
  --lang-rust: #FF6B35;
  --lang-swift: #F05138;
  --lang-kotlin: #B125EA;
  --lang-typescript: #007ACC;
  --lang-javascript: #EAB308;
  --lang-mixed: #10B981;

  /* Spacing scale (4px base) */
  --space-1: 0.25rem;   /* 4px */
  --space-2: 0.5rem;    /* 8px */
  --space-3: 0.75rem;   /* 12px */
  --space-4: 1rem;      /* 16px */
  --space-5: 1.25rem;   /* 20px */
  --space-6: 1.5rem;    /* 24px */
  --space-8: 2rem;      /* 32px */
  --space-10: 2.5rem;   /* 40px */

  /* Border radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;

  /* Shadows */
  --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.4);
  --shadow-glow: 0 0 30px var(--accent-glow-subtle);

  /* Fibonacci timing */
  --duration-89: 89ms;
  --duration-144: 144ms;
  --duration-233: 233ms;
  --duration-377: 377ms;
  --duration-610: 610ms;

  /* Easings */
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-out-expo: cubic-bezier(0.19, 1, 0.22, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  scrollbar-width: thin;
  scrollbar-color: var(--border-default) transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
  color: var(--text-primary);
  background: var(--bg-base);
}

/* ========================================
 * ACCESSIBILITY
 * ======================================== */
.skip-link {
  position: absolute;
  top: -100%;
  left: var(--space-4);
  padding: var(--space-3) var(--space-4);
  background: var(--accent-primary);
  color: white;
  font-weight: 600;
  border-radius: var(--radius-md);
  z-index: 9999;
  text-decoration: none;
  transition: top var(--duration-233) var(--ease-out);
}

.skip-link:focus {
  top: var(--space-4);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

:focus {
  outline: none;
}

:focus-visible {
  outline: 2px solid var(--accent-primary);
  outline-offset: 2px;
}

::selection {
  background: var(--accent-primary);
  color: white;
}

/* ========================================
 * BACKGROUND LAYERS — Ambient life
 * ======================================== */
.bg-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

.bg-layer-1 {
  background: radial-gradient(ellipse 80% 50% at 50% -10%, rgba(139, 92, 246, 0.08), transparent);
  animation: bg-breathe-1 12s ease-in-out infinite;
}

.bg-layer-2 {
  background: radial-gradient(ellipse 60% 40% at 80% 70%, rgba(34, 211, 238, 0.04), transparent);
  animation: bg-breathe-2 16s ease-in-out infinite;
}

.bg-layer-3 {
  background: radial-gradient(ellipse 50% 30% at 20% 80%, rgba(139, 92, 246, 0.03), transparent);
  animation: bg-breathe-3 20s ease-in-out infinite;
}

@keyframes bg-breathe-1 {
  0%, 100% { opacity: 0.8; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.05); }
}

@keyframes bg-breathe-2 {
  0%, 100% { opacity: 0.6; transform: translateX(0); }
  50% { opacity: 0.9; transform: translateX(-30px); }
}

@keyframes bg-breathe-3 {
  0%, 100% { opacity: 0.5; transform: scale(1); }
  50% { opacity: 0.8; transform: scale(1.08); }
}

/* ========================================
 * APP LAYOUT
 * ======================================== */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
  z-index: 1;
}

/* ========================================
 * HEADER — Glass morphism
 * ======================================== */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  padding: 0 var(--space-6);
  background: var(--bg-elevated);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
  z-index: 100;
  position: sticky;
  top: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: var(--space-6);
}

.header-right {
  display: flex;
  align-items: center;
  gap: var(--space-4);
}

/* Logo with glow animation */
.logo {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  cursor: default;
}

.logo-symbol {
  font-size: 24px;
  color: var(--accent-primary);
  text-shadow: 0 0 24px var(--accent-glow);
  animation: logo-pulse 4s ease-in-out infinite;
}

@keyframes logo-pulse {
  0%, 100% { text-shadow: 0 0 24px var(--accent-glow); }
  50% { text-shadow: 0 0 40px var(--accent-glow), 0 0 60px rgba(139, 92, 246, 0.2); }
}

.logo-text {
  font-family: var(--font-display);
  font-size: var(--text-md);
  font-weight: 600;
  letter-spacing: var(--tracking-tight);
}

/* Navigation */
.nav {
  display: flex;
  gap: var(--space-1);
  padding: var(--space-1);
  background: var(--bg-surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-subtle);
}

.nav-btn {
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--duration-144) var(--ease-out);
}

.nav-btn:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.nav-btn.active {
  color: var(--text-primary);
  background: var(--accent-primary);
  box-shadow: 0 2px 8px var(--accent-glow-subtle);
}

/* Stats */
.stats {
  display: flex;
  gap: var(--space-5);
}

.stat {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.stat-value {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-secondary);
}

/* ========================================
 * MAIN LAYOUT
 * ======================================== */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
  max-width: 1920px;
  margin: 0 auto;
  width: 100%;
}

/* ========================================
 * SIDEBAR
 * ======================================== */
.sidebar {
  width: 260px;
  background: var(--bg-elevated);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-section {
  padding: var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
}

.sidebar-section:last-child {
  border-bottom: none;
}

.sidebar-title {
  font-size: var(--text-2xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-muted);
  margin-bottom: var(--space-3);
}

/* Component List */
.component-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  max-height: 50vh;
  overflow-y: auto;
}

.component-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3);
  min-height: 44px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--duration-144) var(--ease-out);
}

.component-item:hover {
  background: var(--bg-hover);
  transform: translateX(4px);
}

.component-item.active {
  background: var(--accent-primary);
  box-shadow: 0 4px 12px var(--accent-glow-subtle);
}

.component-item.active .component-name,
.component-item.active .component-meta {
  color: white;
}

.component-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 0 8px currentColor;
}

.component-name {
  flex: 1;
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.component-meta {
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  color: var(--text-muted);
}

/* Legend */
.legend {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--text-secondary);
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  box-shadow: 0 0 6px currentColor;
}

/* ========================================
 * CANVAS CONTAINER
 * ======================================== */
.canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--bg-base);
}

.graph {
  width: 100%;
  height: 100%;
  display: block;
}

/* Controls with glass effect */
.controls {
  position: absolute;
  bottom: var(--space-5);
  right: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-2);
  background: var(--bg-elevated);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
}

.control-btn {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--duration-144) var(--ease-out);
}

.control-btn:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
  border-color: var(--border-strong);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.control-btn:active {
  transform: translateY(0);
}

/* ========================================
 * SVG NODES — Breathing animations
 * ======================================== */
.node {
  cursor: pointer;
}

.node-circle {
  stroke: var(--bg-base);
  stroke-width: 2px;
  transition: all var(--duration-233) var(--ease-out);
  animation: node-breathe 4s ease-in-out infinite;
  animation-delay: var(--breathe-delay, 0ms);
}

@keyframes node-breathe {
  0%, 100% {
    transform: scale(1);
    filter: drop-shadow(0 0 0 transparent);
  }
  50% {
    transform: scale(1.02);
    filter: drop-shadow(0 0 6px currentColor);
  }
}

.node:nth-child(3n) .node-circle { --breathe-delay: 0ms; }
.node:nth-child(3n+1) .node-circle { --breathe-delay: 377ms; }
.node:nth-child(3n+2) .node-circle { --breathe-delay: 754ms; }

.node:hover .node-circle {
  stroke-width: 3px;
  transform: scale(1.1);
  filter: drop-shadow(0 0 16px currentColor) drop-shadow(0 0 32px currentColor);
}

.node:hover .node-label {
  fill: white;
}

.node.selected .node-circle {
  stroke: var(--accent-primary);
  stroke-width: 3px;
  animation: node-selected 2s ease-in-out infinite;
}

@keyframes node-selected {
  0%, 100% {
    filter: drop-shadow(0 0 12px var(--accent-primary)) drop-shadow(0 0 24px var(--accent-primary));
  }
  50% {
    filter: drop-shadow(0 0 20px var(--accent-primary)) drop-shadow(0 0 40px var(--accent-primary));
  }
}

.node-label {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  fill: var(--text-primary);
  text-anchor: middle;
  pointer-events: none;
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.8),
    0 0 8px var(--bg-base);
  transition: fill var(--duration-144) var(--ease-out);
}

/* ========================================
 * SVG LINKS — Subtle pulse
 * ======================================== */
.link {
  stroke: var(--border-strong);
  stroke-opacity: 0.4;
  fill: none;
  transition: all var(--duration-233) var(--ease-out);
  animation: link-pulse 6s ease-in-out infinite;
  animation-delay: var(--link-delay, 0ms);
}

@keyframes link-pulse {
  0%, 100% { stroke-opacity: 0.3; }
  50% { stroke-opacity: 0.5; }
}

.link:nth-child(4n) { --link-delay: 0ms; }
.link:nth-child(4n+1) { --link-delay: 233ms; }
.link:nth-child(4n+2) { --link-delay: 466ms; }
.link:nth-child(4n+3) { --link-delay: 699ms; }

.link.highlighted {
  stroke: var(--accent-primary);
  stroke-opacity: 1;
  stroke-width: 2px;
  stroke-dasharray: 8 4;
  animation: link-flow 1s linear infinite;
}

@keyframes link-flow {
  from { stroke-dashoffset: 12; }
  to { stroke-dashoffset: 0; }
}

/* ========================================
 * DETAIL PANEL
 * ======================================== */
.detail-panel {
  width: 340px;
  background: var(--bg-elevated);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-left: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transform: translateX(100%);
  opacity: 0;
  transition:
    transform var(--duration-377) var(--ease-out),
    opacity var(--duration-233) var(--ease-out);
}

.detail-panel.open {
  transform: translateX(0);
  opacity: 1;
}

.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
}

.detail-title {
  font-family: var(--font-display);
  font-size: var(--text-md);
  font-weight: 600;
  letter-spacing: var(--tracking-tight);
  color: var(--text-primary);
}

.detail-close {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--duration-144) var(--ease-out);
}

.detail-close:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.detail-content {
  flex: 1;
  padding: var(--space-4);
  overflow-y: auto;
}

.detail-hint {
  color: var(--text-muted);
  font-size: var(--text-sm);
  text-align: center;
  padding: var(--space-10) var(--space-4);
}

.detail-section {
  margin-bottom: var(--space-5);
  animation: slide-up var(--duration-377) var(--ease-out) backwards;
}

.detail-panel.open .detail-section:nth-child(1) { animation-delay: 89ms; }
.detail-panel.open .detail-section:nth-child(2) { animation-delay: 144ms; }
.detail-panel.open .detail-section:nth-child(3) { animation-delay: 233ms; }
.detail-panel.open .detail-section:nth-child(4) { animation-delay: 377ms; }

@keyframes slide-up {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.detail-section:last-child {
  margin-bottom: 0;
}

.detail-section-title {
  font-size: var(--text-2xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
  color: var(--text-muted);
  margin-bottom: var(--space-2);
}

.detail-description {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  line-height: var(--leading-relaxed);
  margin-bottom: var(--space-3);
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-2);
}

.detail-stat {
  padding: var(--space-3);
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
}

.detail-stat-label {
  font-size: var(--text-2xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  color: var(--text-muted);
  margin-bottom: var(--space-1);
}

.detail-stat-value {
  font-family: var(--font-mono);
  font-size: var(--text-md);
  font-weight: 600;
  color: var(--text-primary);
}

.detail-deps {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.detail-dep {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3);
  min-height: 44px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  cursor: pointer;
  transition: all var(--duration-144) var(--ease-out);
}

.detail-dep:hover {
  background: var(--bg-hover);
  border-color: var(--accent-primary);
  transform: translateX(4px);
}

.detail-dep-arrow {
  color: var(--accent-tertiary);
  font-size: var(--text-xs);
}

.detail-dep-name {
  flex: 1;
  color: var(--text-primary);
}

.detail-dep-count {
  color: var(--text-muted);
  font-size: var(--text-2xs);
}

/* ========================================
 * TOOLTIP
 * ======================================== */
.tooltip {
  position: fixed;
  padding: var(--space-3) var(--space-4);
  background: var(--bg-surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg), var(--shadow-glow);
  pointer-events: none;
  opacity: 0;
  transform: translateY(8px) scale(0.95);
  transition: all var(--duration-144) var(--ease-out);
  z-index: 1000;
  max-width: 300px;
}

.tooltip.visible {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.tooltip-title {
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: 600;
  letter-spacing: var(--tracking-tight);
  color: var(--text-primary);
  margin-bottom: var(--space-1);
}

.tooltip-desc {
  font-size: var(--text-xs);
  color: var(--text-secondary);
  line-height: var(--leading-relaxed);
}

.tooltip-stats {
  display: flex;
  gap: var(--space-4);
  margin-top: var(--space-2);
  padding-top: var(--space-2);
  border-top: 1px solid var(--border-subtle);
}

.tooltip-stat {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.tooltip-stat strong {
  color: var(--accent-tertiary);
}

/* ========================================
 * SCROLLBAR
 * ======================================== */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-default);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--border-strong);
}

/* ========================================
 * RESPONSIVE
 * ======================================== */
@media (max-width: 1024px) {
  .sidebar {
    width: 220px;
  }
  .detail-panel {
    width: 300px;
  }
}

@media (max-width: 768px) {
  .sidebar {
    display: none;
  }
  .stats {
    display: none;
  }
}

/* ========================================
 * ACCESSIBILITY — Reduced motion
 * ======================================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .node-circle,
  .link,
  .logo-symbol,
  .bg-layer-1,
  .bg-layer-2,
  .bg-layer-3 {
    animation: none !important;
  }
}

/* Forced colors for Windows High Contrast */
@media (forced-colors: active) {
  .nav-btn.active,
  .component-item.active {
    forced-color-adjust: none;
    background: Highlight;
    color: HighlightText;
  }

  .node-circle {
    stroke: CanvasText;
  }

  .link {
    stroke: CanvasText;
  }

  :focus-visible {
    outline: 3px solid Highlight;
  }
}
