@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Outfit:wght@500;600;700;800&display=swap');

:root {
  --bg-dark: #0f172a;
  --bg-card: rgba(30, 41, 59, 0.94);
  --bg-card-hover: rgba(47, 63, 90, 0.98);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --accent-gold: #fbbf24;
  --accent-gold-glow: rgba(251, 191, 36, 0.35);
  --accent-blue: #38bdf8;
  --accent-pink: #f43f5e;
  --accent-emerald: #34d399;
  --border-subtle: rgba(255, 255, 255, 0.14);
  --border-highlight: rgba(251, 191, 36, 0.6);
  --shadow-lg: 0 10px 30px -5px rgba(0, 0, 0, 0.6), 0 0 15px rgba(0,0,0,0.4);
  --radius-xl: 18px;
  --radius-md: 10px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Inter', sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  background-color: var(--bg-dark);
  background-image: 
    radial-gradient(at 0% 0%, rgba(30, 58, 138, 0.35) 0px, transparent 50%),
    radial-gradient(at 100% 100%, rgba(120, 53, 15, 0.3) 0px, transparent 50%),
    radial-gradient(at 50% 50%, rgba(15, 23, 42, 0.9) 0px, transparent 100%);
  color: var(--text-main);
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ==========================================
   HEADER & TOOLBAR EJECUTIVA
   ========================================== */
header {
  height: 68px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(15, 23, 42, 0.94);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 100;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 42px;
  height: 42px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
}

.brand-crest {
  max-height: 36px;
  max-width: 36px;
  object-fit: contain;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.5));
}

.brand-text h1 {
  font-family: 'Outfit', sans-serif;
  font-size: 1.25rem;
  font-weight: 700;
  background: linear-gradient(90deg, #ffffff, #fef08a);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.brand-text p {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
}

.search-box {
  position: relative;
  width: 190px;
}

.search-box input {
  width: 100%;
  padding: 8px 12px 8px 34px;
  background: rgba(30, 41, 59, 0.9);
  border: 1px solid var(--border-subtle);
  border-radius: 20px;
  color: white;
  font-size: 0.82rem;
  outline: none;
  transition: all 0.2s ease;
}

.search-box input:focus {
  border-color: var(--accent-gold);
  box-shadow: 0 0 0 3px var(--accent-gold-glow);
}

.search-box i {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 0.85rem;
}

.select-pill {
  padding: 8px 14px;
  border-radius: 20px;
  background: rgba(30, 41, 59, 0.9);
  border: 1px solid var(--border-subtle);
  color: white;
  font-size: 0.82rem;
  font-weight: 600;
  outline: none;
  cursor: pointer;
  transition: all 0.2s ease;
}

.select-pill:hover, .select-pill:focus {
  border-color: var(--accent-gold);
}

.btn {
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 0.82rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s ease;
}

.btn-primary {
  background: linear-gradient(135deg, #d97706, #b45309);
  color: white;
  box-shadow: 0 4px 14px rgba(180, 83, 9, 0.4);
}

.btn-primary:hover {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  transform: translateY(-1px);
}

.btn-secondary {
  background: rgba(30, 41, 59, 0.9);
  color: var(--text-main);
  border: 1px solid var(--border-subtle);
}

.btn-secondary:hover {
  background: rgba(51, 65, 85, 0.9);
  border-color: rgba(255, 255, 255, 0.25);
}

.btn-icon {
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 50%;
  background: rgba(30, 41, 59, 0.9);
  border: 1px solid var(--border-subtle);
  color: var(--text-main);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
}

.btn-icon:hover {
  border-color: var(--accent-gold);
  color: var(--accent-gold);
}

/* MENÚ DESPLEGABLE DE HERRAMIENTAS (⋮) */
.tools-menu-wrapper {
  position: relative;
}

.tools-dropdown-menu {
  position: absolute;
  top: 48px;
  right: 0;
  width: 240px;
  background: #1e293b;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-shadow: 0 15px 35px rgba(0,0,0,0.6);
  padding: 8px 0;
  display: none;
  flex-direction: column;
  z-index: 120;
}

.tools-dropdown-menu.active {
  display: flex;
}

.dropdown-item {
  padding: 10px 16px;
  background: transparent;
  border: none;
  color: var(--text-main);
  font-size: 0.83rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background 0.15s ease;
}

.dropdown-item:hover {
  background: rgba(51, 65, 85, 0.8);
  color: var(--accent-gold);
}

.dropdown-item.text-danger { color: #f87171; }
.dropdown-item.text-danger:hover { background: rgba(153, 27, 27, 0.4); color: #fca5a5; }
.dropdown-divider { border: 0; border-top: 1px solid var(--border-subtle); margin: 4px 0; }

/* ==========================================
   MAIN WORKSPACE & CANVAS
   ========================================== */
.app-body {
  flex: 1;
  position: relative;
  overflow: hidden;
}

#tree-viewport {
  width: 100%;
  height: 100%;
  cursor: grab;
  overflow: hidden;
  position: relative;
}

#tree-viewport:active { cursor: grabbing; }

#tree-container {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  transition: transform 0.05s linear;
  min-width: 8000px;
  min-height: 4000px;
  padding: 40px;
}

/* GENERATION LANES DISCRETAS */
.generation-lanes-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.gen-lane {
  position: absolute;
  left: 0;
  width: 100%;
  height: 210px;
  border-top: 1px dashed rgba(255, 255, 255, 0.06);
  display: flex;
  align-items: flex-start;
  padding: 12px 20px;
}

.gen-lane-label {
  font-family: 'Outfit', sans-serif;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: rgba(251, 191, 36, 0.75);
  background: rgba(15, 23, 42, 0.9);
  padding: 3px 10px;
  border-radius: 12px;
  border: 1px solid rgba(251, 191, 36, 0.25);
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

/* SVG CONNECTOR CANVAS (EXPANDIDO DINÁMICAMENTE) */
#svg-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
  overflow: visible;
}

.tree-connector {
  stroke: #fbbf24;
  stroke-width: 2.8px;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: stroke 0.3s ease;
  opacity: 0.9;
}

.tree-connector:hover, .tree-connector.active {
  stroke: #ffffff;
  stroke-width: 4px;
  opacity: 1;
}

/* NODES LAYER */
.nodes-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
}

/* UNIDAD FAMILIAR / PAREJA */
.family-unit-card {
  position: absolute;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-card);
  border: 1.5px solid var(--border-subtle);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  user-select: none;
}

.family-unit-card:hover {
  transform: translateY(-4px);
  background: var(--bg-card-hover);
  border-color: var(--accent-gold);
  box-shadow: 0 14px 35px rgba(0, 0, 0, 0.7), 0 0 20px var(--accent-gold-glow);
  z-index: 30 !important;
}

.family-unit-card.highlighted {
  border-color: #38bdf8;
  box-shadow: 0 0 25px rgba(56, 189, 248, 0.7);
}

/* PERSON PILL */
.person-pill {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(15, 23, 42, 0.85);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 170px;
  max-width: 240px;
}

.person-pill:hover {
  background: rgba(30, 58, 138, 0.7);
  border-color: var(--accent-gold);
}

.person-pill.male { border-left: 3.5px solid var(--accent-blue); }
.person-pill.female { border-left: 3.5px solid var(--accent-pink); }

.pill-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.9rem;
  color: white;
  flex-shrink: 0;
}

.pill-avatar.male { background: linear-gradient(135deg, #2563eb, #1d4ed8); }
.pill-avatar.female { background: linear-gradient(135deg, #db2777, #be185d); }

.pill-info {
  flex: 1;
  word-wrap: break-word;
  overflow: visible;
}

.pill-name {
  font-size: 0.88rem;
  font-weight: 600;
  color: white;
  line-height: 1.25;
  white-space: normal;
  word-break: break-word;
}

.pill-role {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.marriage-badge {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(251, 191, 36, 0.18);
  border: 1px solid rgba(251, 191, 36, 0.5);
  color: var(--accent-gold);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  flex-shrink: 0;
}

/* BOTÓN DE APERTURA (+ / -) */
.toggle-branch-btn {
  position: absolute;
  bottom: -15px;
  left: 50%;
  transform: translateX(-50%);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent-gold);
  color: #0f172a;
  border: 2.5px solid #0f172a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.6);
  z-index: 10;
  transition: all 0.2s ease;
}

.toggle-branch-btn:hover {
  transform: translateX(-50%) scale(1.2);
  background: #ffffff;
}

/* SIDE DRAWER */
.side-drawer {
  position: fixed;
  top: 68px;
  right: 0;
  width: 360px;
  height: calc(100vh - 68px);
  background: rgba(30, 41, 59, 0.96);
  backdrop-filter: blur(16px);
  border-left: 1px solid var(--border-subtle);
  box-shadow: -10px 0 30px rgba(0,0,0,0.5);
  z-index: 150;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
}

.side-drawer.active { transform: translateX(0); }

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

.drawer-header h3 {
  font-family: 'Outfit', sans-serif;
  font-size: 1.15rem;
  color: var(--accent-gold);
}

.drawer-content { flex: 1; padding: 20px; overflow-y: auto; }
.drawer-item { margin-bottom: 16px; }
.drawer-item label { font-size: 0.75rem; color: var(--text-muted); display: block; margin-bottom: 2px; text-transform: uppercase; letter-spacing: 0.5px; }
.drawer-item p { font-size: 0.92rem; color: white; font-weight: 500; line-height: 1.35; }
.drawer-footer { padding: 16px 20px; border-top: 1px solid var(--border-subtle); }

/* FLOATING CONTROLS */
.controls-floating {
  position: absolute;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
}

.control-btn {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: rgba(30, 41, 59, 0.95);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-subtle);
  color: white;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  transition: all 0.2s ease;
}

.control-btn:hover {
  background: rgba(51, 65, 85, 1);
  border-color: var(--accent-gold);
  color: var(--accent-gold);
}

/* MODAL OVERLAY */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; width: 100vw; height: 100vh;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(10px);
  z-index: 200;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; pointer-events: none; transition: opacity 0.25s ease;
}

.modal-overlay.active { opacity: 1; pointer-events: auto; }

.modal-card {
  width: 92%;
  max-width: 540px;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  background: #1e293b;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-xl);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.8);
  padding: 22px;
  position: relative;
  transform: translateY(20px);
  transition: transform 0.25s ease;
}

#person-form {
  overflow-y: auto;
  max-height: calc(88vh - 90px);
  padding-right: 6px;
}

.modal-overlay.active .modal-card { transform: translateY(0); }

.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; flex-shrink: 0; }
.modal-title { font-family: 'Outfit', sans-serif; font-size: 1.25rem; font-weight: 700; color: var(--accent-gold); }
.close-btn { background: transparent; border: none; color: var(--text-muted); font-size: 1.4rem; cursor: pointer; }
.close-btn:hover { color: white; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 0.8rem; color: var(--text-muted); margin-bottom: 4px; font-weight: 500; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 10px 12px; background: #0f172a; border: 1px solid var(--border-subtle); border-radius: var(--radius-md); color: white; font-size: 0.9rem; outline: none;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { border-color: var(--accent-gold); }

#group-new-branch {
  background: rgba(251, 191, 36, 0.08);
  border: 1px dashed var(--accent-gold);
  padding: 12px;
  border-radius: var(--radius-md);
  margin-top: -6px;
}

.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; }
.modal-actions { display: flex; justify-content: flex-end; gap: 12px; margin-top: 20px; flex-shrink: 0; }

/* ==========================================
   PERFIL FOTOS, DECEASED BADGES Y GALERÍA
   ========================================== */
.pill-avatar img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  cursor: pointer;
}

.deceased-badge {
  font-size: 0.72rem;
  padding: 2px 7px;
  border-radius: 10px;
  background: #020617;
  color: #f8fafc;
  border: 1px solid rgba(255,255,255,0.25);
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.pill-age-tag {
  font-size: 0.72rem;
  color: var(--accent-gold);
  font-weight: 600;
  margin-top: 2px;
}

.drawer-photo-gallery {
  margin-top: 14px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 8px;
}

.gallery-thumb-img {
  width: 100%;
  height: 75px;
  border-radius: var(--radius-md);
  object-fit: cover;
  border: 1.5px solid var(--border-subtle);
  cursor: pointer;
  transition: transform 0.2s ease;
}

.gallery-thumb-img:hover {
  transform: scale(1.06);
  border-color: var(--accent-gold);
}

/* ==========================================
   TEMA CLARO / BLANCO PROLIJO (LIGHT THEME)
   ========================================== */
body.light-theme {
  --bg-dark: #f8fafc;
  --bg-card: #ffffff;
  --bg-card-hover: #f1f5f9;
  --text-main: #0f172a;
  --text-muted: #475569;
  --accent-gold: #d97706;
  --accent-gold-glow: rgba(217, 119, 6, 0.2);
  --accent-blue: #0284c7;
  --accent-pink: #e11d48;
  --accent-emerald: #059669;
  --border-subtle: #cbd5e1;
  --border-highlight: #d97706;
  --shadow-lg: 0 10px 30px -5px rgba(15, 23, 42, 0.12);
  background-color: #f8fafc;
  background-image: 
    radial-gradient(at 0% 0%, rgba(226, 232, 240, 0.8) 0px, transparent 50%),
    radial-gradient(at 100% 100%, rgba(254, 243, 199, 0.4) 0px, transparent 50%);
  color: #0f172a;
}

body.light-theme header {
  background: rgba(255, 255, 255, 0.95);
  border-bottom-color: #cbd5e1;
}

body.light-theme .brand-text h1 {
  background: linear-gradient(90deg, #0f172a, #b45309);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

body.light-theme .person-pill {
  background: #f1f5f9;
  border-color: #cbd5e1;
}

body.light-theme .pill-name {
  color: #0f172a;
  font-weight: 700;
}

body.light-theme .pill-role {
  color: #475569;
}

body.light-theme .side-drawer,
body.light-theme .modal-card {
  background: #ffffff;
  color: #0f172a;
  border-color: #cbd5e1;
}

body.light-theme .drawer-header h3,
body.light-theme .modal-title {
  color: #b45309;
}

body.light-theme .drawer-item label,
body.light-theme .form-group label {
  color: #475569;
  font-weight: 600;
}

body.light-theme .drawer-item p {
  color: #0f172a;
  font-weight: 600;
}

body.light-theme .form-group input,
body.light-theme .form-group select,
body.light-theme .form-group textarea {
  background: #f8fafc;
  color: #0f172a;
  border-color: #cbd5e1;
}

body.light-theme .search-box input,
body.light-theme .select-pill,
body.light-theme .btn-secondary,
body.light-theme .btn-icon,
body.light-theme .control-btn {
  background: #ffffff;
  color: #0f172a;
  border-color: #cbd5e1;
}

body.light-theme .gen-lane-label {
  background: #ffffff;
  color: #b45309;
  border-color: rgba(217, 119, 6, 0.3);
}

body.light-theme .tree-connector {
  stroke: #d97706;
}

/* ==========================================
   RESPONSIVE DESIGN PARA CELULARES (MÓVIL)
   ========================================== */
@media (max-width: 768px) {
  header {
    height: auto;
    padding: 12px 14px;
    flex-direction: column;
    gap: 10px;
  }
  .toolbar {
    width: 100%;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 6px;
  }
  .search-box {
    width: 100%;
  }
  .select-pill {
    flex: 1;
    min-width: 120px;
    font-size: 0.78rem;
  }
  .btn-text {
    display: none;
  }
  .btn {
    padding: 8px 12px;
  }
  .side-drawer {
    width: 100vw;
    height: 75vh;
    top: auto;
    bottom: 0;
    border-left: none;
    border-top: 2px solid var(--border-subtle);
    border-radius: 20px 20px 0 0;
    transform: translateY(100%);
  }
  .side-drawer.active {
    transform: translateY(0);
  }
  .modal-card {
    width: 95%;
    max-height: 90vh;
    overflow-y: auto;
    padding: 16px;
  }
  .controls-floating {
    bottom: 16px;
    right: 16px;
  }
}

/* ==========================================
   PRINT & PDF EXPORT OPTIMIZATIONS (A2/A3 LANDSCAPE)
   ========================================== */
@page {
  size: A2 landscape;
  margin: 10mm;
}

@media print {
  body {
    background: #ffffff !important;
    color: #000000 !important;
    overflow: visible !important;
  }
  header, .controls-floating, .toolbar, .side-drawer, .toggle-branch-btn {
    display: none !important;
  }
  #tree-container {
    position: absolute !important;
    top: 0 !important;
    left: 0 !important;
    transform: scale(0.65) !important;
    transform-origin: 0 0 !important;
  }
  .family-unit-card {
    background: #ffffff !important;
    border: 2px solid #000000 !important;
    box-shadow: none !important;
  }
  .person-pill {
    background: #f8fafc !important;
    border: 1px solid #94a3b8 !important;
  }
  .pill-name { color: #000000 !important; font-weight: 700 !important; }
  .pill-role { color: #475569 !important; }
  .tree-connector {
    stroke: #000000 !important;
    stroke-width: 3px !important;
    opacity: 1 !important;
  }
}
