@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=Montserrat:wght@400;500;600;700;800&display=swap');

/* ==========================================================================
   DESIGN SYSTEM & VARIABLES
   ========================================================================== */
:root {
  --color-bg: #f8fafc;
  --color-surface: rgba(255, 255, 255, 0.75);
  --color-surface-card: #ffffff;
  --color-border: #e2e8f0;
  --color-text-main: #0f172a;
  --color-text-muted: #64748b;
  
  --color-indigo: #6366f1;
  --color-indigo-light: #e0e7ff;
  --color-blue: #3b82f6;
  --color-blue-light: #dbeafe;
  --color-emerald: #10b981;
  --color-emerald-light: #d1fae5;
  --color-amber: #f59e0b;
  --color-amber-light: #fef3c7;
  --color-rose: #f43f5e;
  --color-rose-light: #ffe4e6;
  --color-cyan: #06b6d4;
  --color-cyan-light: #ecfeff;
  --color-slate: #64748b;
  --color-slate-light: #f1f5f9;

  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
  --shadow-glass: 0 8px 32px 0 rgba(31, 38, 135, 0.07);

  --font-family-sans: 'Inter', sans-serif;
  --font-family-title: 'Montserrat', sans-serif;
}

body.dark-mode {
  --color-bg: #0b0f19;
  --color-surface: rgba(17, 24, 39, 0.8);
  --color-surface-card: #1e293b;
  --color-border: #334155;
  --color-text-main: #f8fafc;
  --color-text-muted: #94a3b8;

  --color-indigo-light: rgba(99, 102, 241, 0.15);
  --color-blue-light: rgba(59, 130, 246, 0.15);
  --color-emerald-light: rgba(16, 185, 129, 0.15);
  --color-amber-light: rgba(245, 158, 11, 0.15);
  --color-rose-light: rgba(244, 63, 94, 0.15);
  --color-cyan-light: rgba(6, 182, 212, 0.15);
  --color-slate-light: rgba(100, 116, 139, 0.15);

  --shadow-glass: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
}

/* ==========================================================================
   RESET & LAYOUT BASE
   ========================================================================== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-family-sans);
  background-color: var(--color-bg);
  color: var(--color-text-main);
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  transition: background-color 0.3s ease, color 0.3s ease;
}


.bg-shape {
  position: absolute;
  border-radius: 50%;
  filter: blur(100px);
  z-index: -1;
  opacity: 0.35;
  transition: opacity 0.5s ease;
}
.shape-1 {
  width: 400px;
  height: 400px;
  background: var(--color-indigo);
  top: -100px;
  left: -100px;
}
.shape-2 {
  width: 350px;
  height: 350px;
  background: var(--color-emerald);
  bottom: -50px;
  right: -50px;
}

.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  position: relative;
}

/* ==========================================================================
   TOP BAR / HEADER CONTROLS
   ========================================================================== */
.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 2rem;
  background: var(--color-surface);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--color-border);
  z-index: 10;
  box-shadow: var(--shadow-sm);
  transition: background 0.3s ease, border-color 0.3s ease;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand .logo {
  width: 40px;
  height: 40px;
  border-radius: 11px;
  background: linear-gradient(135deg, #00549A, #0B2545);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
}

.brand h1 {
  font-family: var(--font-family-title);
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: -0.5px;
  color: var(--color-text-main);
  line-height: 1.2;
}

.brand h1 span {
  display: block;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  font-weight: 500;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  margin-top: 2px;
}

.search-wrapper {
  position: relative;
  width: 320px;
}

.search-input {
  width: 100%;
  padding: 0.625rem 1rem 0.625rem 2.5rem;
  border-radius: 9999px;
  border: 1px solid var(--color-border);
  background-color: var(--color-surface-card);
  color: var(--color-text-main);
  font-family: var(--font-family-sans);
  font-size: 0.875rem;
  font-weight: 500;
  outline: none;
  transition: all 0.2s ease;
  box-shadow: var(--shadow-sm);
}

.search-input:focus {
  border-color: var(--color-indigo);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.search-icon {
  position: absolute;
  left: 0.875rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--color-text-muted);
  pointer-events: none;
  width: 16px;
  height: 16px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn {
  background: var(--color-surface-card);
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
  padding: 0.5rem;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  box-shadow: var(--shadow-sm);
}

.btn:hover {
  background-color: var(--color-indigo);
  border-color: var(--color-indigo);
  color: #ffffff;
  transform: translateY(-1px);
}

.btn svg {
  width: 20px;
  height: 20px;
  stroke-width: 2;
}

.divider {
  width: 1px;
  height: 24px;
  background-color: var(--color-border);
  margin: 0 0.25rem;
}

/* ==========================================================================
   CANVAS & CHART VISUALS (D3 / SVG)
   ========================================================================== */
.canvas-container {
  flex-grow: 1;
  width: 100%;
  height: 100%;
  position: relative;
  outline: none;
  cursor: grab;
}

.canvas-container:active {
  cursor: grabbing;
}

.svg-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* Conectores verticales */
.link {
  fill: none;
  stroke: #e2e8f0;
  stroke-width: 2px;
  stroke-linejoin: round;
  stroke-linecap: round;
  transition: stroke 0.4s ease, stroke-width 0.4s ease;
}

body.dark-mode .link {
  stroke: #334155;
}

.link.highlighted {
  stroke: var(--color-indigo);
  stroke-width: 3.5px;
  stroke-dasharray: 6, 4;
  animation: link-pulse 30s linear infinite;
}

@keyframes link-pulse {
  to {
    stroke-dashoffset: -400px;
  }
}

.node-card-wrapper {
  padding: 10px;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow: visible;
}

/* Tarjeta base */
.node-card {
  width: 250px;
  height: 90px;
  background-color: var(--color-surface-card);
  border: 1.5px solid var(--color-border);
  border-radius: 16px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-md);
  cursor: pointer;
  user-select: none;
  position: relative;
  overflow: hidden;
  transition: transform 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275), 
              box-shadow 0.25s ease, 
              border-color 0.25s ease,
              height 0.3s ease;
}

.node-card:hover {
  transform: translateY(-2px) scale(1.01);
  box-shadow: var(--shadow-lg);
  border-color: var(--color-indigo);
}

.node-card.active-search {
  border-color: var(--color-indigo);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25), var(--shadow-lg);
  animation: pulse-border 1.5s infinite alternate;
}

@keyframes pulse-border {
  from { border-color: var(--color-indigo); }
  to { border-color: var(--color-rose); }
}

/* Bordes de color por área */
.node-card.indigo { border-left: 5px solid var(--color-indigo); }
.node-card.blue { border-left: 5px solid var(--color-blue); }
.node-card.emerald { border-left: 5px solid var(--color-emerald); }
.node-card.amber { border-left: 5px solid var(--color-amber); }
.node-card.rose { border-left: 5px solid var(--color-rose); }
.node-card.cyan { border-left: 5px solid var(--color-cyan); }
.node-card.slate { border-left: 5px solid var(--color-slate); }

/* Fila principal superior de la tarjeta */
.card-main-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  height: 64px;
}

/* Avatar circular minimalista */
.node-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.875rem;
  color: #ffffff;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  text-transform: uppercase;
}

.avatar-indigo { background: linear-gradient(135deg, var(--color-indigo), #4f46e5); }
.avatar-blue { background: linear-gradient(135deg, var(--color-blue), #2563eb); }
.avatar-emerald { background: linear-gradient(135deg, var(--color-emerald), #059669); }
.avatar-amber { background: linear-gradient(135deg, var(--color-amber), #d97706); }
.avatar-rose { background: linear-gradient(135deg, var(--color-rose), #e11d48); }
.avatar-cyan { background: linear-gradient(135deg, var(--color-cyan), #0891b2); }
.avatar-slate { background: linear-gradient(135deg, var(--color-slate), #475569); }

.node-info {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-grow: 1;
}

.node-name {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--color-text-main);
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.node-title {
  font-size: 0.725rem;
  color: var(--color-text-muted);
  font-weight: 500;
  margin-top: 2px;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Botón indicador de colapso de hijos - Ubicado abajo en el centro para layout vertical */
.collapse-badge {
  position: absolute;
  bottom: -6px;
  right: 50%;
  transform: translateX(50%);
  width: 16px;
  height: 16px;
  background-color: var(--color-indigo);
  border: 1.5px solid #ffffff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  z-index: 5;
  transition: all 0.2s ease;
}

.collapse-badge::after {
  content: '';
  width: 6px;
  height: 1px;
  background-color: #ffffff;
}

.node-card.collapsed .collapse-badge {
  background-color: var(--color-rose);
  transform: translateX(50%) scale(1.15);
}

.node-card.collapsed .collapse-badge::before {
  content: '';
  width: 1px;
  height: 6px;
  background-color: #ffffff;
  position: absolute;
}

/* ==========================================================================
   OFF-CANVAS DRAWER & OVERLAY
   ========================================================================== */
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 95;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.drawer-overlay.open {
  opacity: 1;
  visibility: visible;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  height: 100dvh;
  width: min(420px, 92vw);
  background-color: var(--color-surface-card);
  border-left: 1px solid var(--color-border);
  box-shadow: -10px 0 30px -10px rgba(0, 0, 0, 0.15);
  transform: translateX(100%);
  transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1);
  z-index: 100;
  display: flex;
  flex-direction: column;
}

body.dark-mode .drawer {
  box-shadow: -10px 0 30px -10px rgba(0, 0, 0, 0.5);
  border-left: 1px solid var(--color-border);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-head {
  padding: 24px;
  color: #ffffff;
  position: relative;
  display: flex;
  align-items: center;
  gap: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.drawer-head.indigo { background: linear-gradient(135deg, var(--color-indigo), #4f46e5); }
.drawer-head.blue { background: linear-gradient(135deg, var(--color-blue), #2563eb); }
.drawer-head.emerald { background: linear-gradient(135deg, var(--color-emerald), #059669); }
.drawer-head.amber { background: linear-gradient(135deg, var(--color-amber), #d97706); }
.drawer-head.rose { background: linear-gradient(135deg, var(--color-rose), #e11d48); }
.drawer-head.cyan { background: linear-gradient(135deg, var(--color-cyan), #0891b2); }
.drawer-head.slate { background: linear-gradient(135deg, var(--color-slate), #475569); }

.d-initials {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  border: 2px solid rgba(255, 255, 255, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.15rem;
  flex-shrink: 0;
  overflow: hidden;
}

.d-initials img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.drawer-head h2 {
  font-family: var(--font-family-title);
  font-size: 1.15rem;
  font-weight: 700;
  margin: 0 0 4px;
  line-height: 1.2;
}

.drawer-head p {
  margin: 0;
  font-size: 0.8rem;
  color: rgba(255, 255, 255, 0.8);
  font-weight: 500;
}

.drawer-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #ffffff;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease;
}

.drawer-close:hover {
  background: rgba(255, 255, 255, 0.3);
}

.drawer-body {
  padding: 24px;
  overflow-y: auto;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.d-bio {
  font-size: 0.825rem;
  line-height: 1.6;
  color: var(--color-text-main);
  background-color: var(--color-slate-light);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  padding: 14px 16px;
  font-style: italic;
  text-align: justify;
}

.drawer-body h3 {
  font-size: 0.725rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  margin-bottom: 12px;
  font-weight: 700;
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 4px;
}

.d-empty {
  color: var(--color-text-muted);
  font-size: 0.85rem;
  font-style: italic;
}

.person {
  display: flex;
  gap: 14px;
  padding: 12px;
  background-color: var(--color-slate-light);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  margin-bottom: 10px;
}

.p-av {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background-color: var(--color-surface-card);
  color: var(--color-indigo);
  font-weight: 700;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: 1px solid var(--color-border);
}

.p-info {
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.p-name {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--color-text-main);
}

.p-role {
  font-size: 0.725rem;
  color: var(--color-text-muted);
  margin-top: 2px;
}

.p-meta {
  font-size: 0.725rem;
  color: var(--color-text-muted);
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
}

.p-meta a {
  color: var(--color-indigo);
  text-decoration: none;
  font-weight: 600;
}

.p-meta a:hover {
  text-decoration: underline;
}

.p-meta .tel {
  font-weight: 600;
}

/* ==========================================================================
   LEGEND & HINT VISUALS
   ========================================================================== */
.legend {
  position: absolute;
  left: 16px;
  bottom: 14px;
  background: var(--color-surface);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 11px;
  color: var(--color-text-muted);
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  max-width: 70vw;
  box-shadow: var(--shadow-sm);
  z-index: 10;
  transition: all 0.3s ease;
}

.legend span {
  display: flex;
  align-items: center;
}

.legend i {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  margin-right: 6px;
  vertical-align: middle;
}

.hint {
  position: absolute;
  right: 16px;
  bottom: 14px;
  background: var(--color-surface);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 11.5px;
  color: var(--color-text-muted);
  box-shadow: var(--shadow-sm);
  z-index: 10;
  transition: all 0.3s ease;
}

/* ==========================================================================
   MOBILE VIEWS & RESPONSIVE DESIGN (Acordeones/Lista)
   ========================================================================== */
.mobile-list-container {
  display: none;
  flex-grow: 1;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  padding: 2rem 1rem;
  background-color: var(--color-bg);
}

.mobile-list-wrapper {
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.mobile-item {
  position: relative;
  background-color: var(--color-surface-card);
  border: 1px solid var(--color-border);
  border-radius: 14px;
  padding: 0.9rem 1.1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.mobile-item:active {
  transform: scale(0.98);
  background-color: var(--color-slate-light);
}

.mobile-item .collapse-badge {
  display: none !important;
}

.mobile-item-arrow {
  color: var(--color-text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-slate-light);
  transition: all 0.2s ease;
}

body.dark-mode .mobile-item-arrow {
  background: rgba(255, 255, 255, 0.08);
}

/* Selector de vista móvil en la barra superior */
.mobile-view-toggle {
  display: none;
  background: var(--color-slate-light);
  border: 1px solid var(--color-border);
  padding: 3px;
  border-radius: 12px;
  gap: 2px;
  align-items: center;
}

body.dark-mode .mobile-view-toggle {
  background: rgba(30, 41, 59, 0.5);
  border-color: var(--color-border);
}

.view-toggle-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-text-muted);
  background: transparent;
  border: none;
  border-radius: 9px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.view-toggle-btn svg {
  width: 15px;
  height: 15px;
}

.view-toggle-btn:hover {
  color: var(--color-text-main);
}

.view-toggle-btn.active {
  background: var(--color-surface-card);
  color: var(--color-indigo);
  box-shadow: var(--shadow-sm);
}

body.dark-mode .view-toggle-btn.active {
  background: var(--color-surface-card);
  color: #fff;
}

/* Responsive Media Queries */
@media (max-width: 768px) {
  body, .app-container {
    height: 100vh;
    height: 100dvh;
  }

  /* Por defecto en móvil: ocultar canvas y leyendas, mostrar lista */
  .canvas-container, .legend, .hint {
    display: none;
  }

  .mobile-list-container {
    display: block;
    height: calc(100dvh - 125px);
    padding: 1rem 0.75rem 3rem;
  }

  /* Si está activa la vista de árbol en móvil */
  body.view-tree .canvas-container {
    display: block;
    height: calc(100dvh - 125px);
    width: 100vw;
  }

  body.view-tree .legend {
    display: flex;
    flex-wrap: wrap;
    position: absolute;
    bottom: 35px;
    left: 10px;
    right: 10px;
    background: var(--color-surface);
    backdrop-filter: blur(12px);
    padding: 8px 12px;
    border-radius: 12px;
    box-shadow: var(--shadow-md);
    gap: 8px;
  }

  body.view-tree .hint {
    display: block;
    position: absolute;
    bottom: 8px;
    left: 10px;
    right: 10px;
    text-align: center;
    background: var(--color-surface);
    backdrop-filter: blur(12px);
    padding: 4px 8px;
    border-radius: 8px;
    font-size: 10px;
  }

  body.view-tree .mobile-list-container {
    display: none;
  }

  .mobile-view-toggle {
    display: flex;
  }

  .header-bar {
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
    gap: 0.6rem;
  }

  .search-wrapper {
    width: 100%;
    order: 3;
  }

  .brand h1 {
    font-size: 1.05rem;
  }

  .btn.zoom-ctrl,
  .divider.zoom-ctrl {
    display: none !important;
  }
}
