/* ============================================================
   FAMILY TREE — Tree Visualization Styles
   ============================================================ */

/* ── Layout ──────────────────────────────────────────────── */
.tree-page {
  display: flex;
  height: 100vh;
  padding-top: 60px; /* header height */
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────── */
.sidebar {
  width: 320px;
  flex-shrink: 0;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width var(--transition-slow), transform var(--transition-slow);
  z-index: var(--z-above);
}
.sidebar.collapsed {
  width: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--color-surface-alt);
}
.sidebar-title {
  font-family: var(--font-serif);
  font-size: 1rem;
  font-weight: 700;
  color: var(--color-primary);
}

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-5);
}

/* Member profile in sidebar */
.profile-photo-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}
.profile-photo {
  width: 100px;
  height: 100px;
  border-radius: var(--radius-full);
  object-fit: cover;
  border: 3px solid var(--color-border);
  box-shadow: var(--shadow-sm);
}
.profile-name {
  font-family: var(--font-serif);
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--color-primary);
  text-align: center;
}
.profile-dates {
  font-size: 0.875rem;
  color: var(--color-text-muted);
  text-align: center;
}

.profile-section {
  margin-bottom: var(--space-4);
}
.profile-section-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-faint);
  margin-bottom: var(--space-2);
}
.profile-field {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--color-border);
  font-size: 0.875rem;
}
.profile-field:last-child { border-bottom: none; }
.profile-field-label {
  min-width: 80px;
  color: var(--color-text-muted);
  font-weight: 500;
}
.profile-field-value { color: var(--color-text); flex: 1; }

.profile-bio {
  font-size: 0.875rem;
  color: var(--color-text);
  line-height: 1.6;
  background: var(--color-surface-alt);
  padding: var(--space-3);
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
}

.profile-relations {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.relation-chip {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast);
  font-size: 0.875rem;
}
.relation-chip:hover {
  background: var(--color-living-bg);
  border-color: var(--color-living);
}
.relation-chip-role {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  margin-left: auto;
}

.sidebar-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--color-border);
}

/* Relationship finder panel */
.rel-finder {
  padding: var(--space-4) var(--space-5);
  background: var(--color-surface-alt);
  border-top: 1px solid var(--color-border);
}
.rel-finder-title {
  font-family: var(--font-serif);
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--color-primary);
  margin-bottom: var(--space-3);
}
.rel-person-slot {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border: 1px dashed var(--color-border-dark);
  border-radius: var(--radius);
  margin-bottom: var(--space-2);
  font-size: 0.875rem;
  min-height: 42px;
  cursor: pointer;
  transition: border-color var(--transition-fast), background var(--transition-fast);
}
.rel-person-slot.filled {
  border-style: solid;
  border-color: var(--color-primary-light);
  background: #eef3f8;
}
.rel-person-slot:hover { border-color: var(--color-primary-light); }
.rel-person-name { font-weight: 500; }
.rel-person-clear {
  margin-left: auto;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text-muted);
  font-size: 1rem;
  padding: 0 2px;
  line-height: 1;
}
.rel-person-clear:hover { color: var(--color-error); }
.rel-vs {
  text-align: center;
  font-size: 0.75rem;
  color: var(--color-text-faint);
  margin-bottom: var(--space-2);
}
.rel-result {
  margin-top: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--color-primary);
  color: #fff;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-family: var(--font-serif);
  font-weight: 600;
  text-align: center;
  display: none;
}
.rel-result.visible { display: block; }
.rel-result.no-connection {
  background: var(--color-deceased-bg);
  color: var(--color-deceased);
}

/* ── Tree Canvas ─────────────────────────────────────────── */
.tree-canvas {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--color-bg);
  background-image:
    radial-gradient(circle, var(--color-border) 1px, transparent 1px);
  background-size: 24px 24px;
}

#family-tree-svg {
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none; /* let D3 zoom/pan handle all touch gestures */
}
#family-tree-svg:active { cursor: grabbing; }

/* ── Tree Controls Overlay ───────────────────────────────── */
.tree-controls {
  position: absolute;
  top: var(--space-4);
  left: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: var(--z-above);
}

.tree-toolbar {
  position: absolute;
  top: var(--space-4);
  right: var(--space-4);
  display: flex;
  gap: var(--space-2);
  z-index: var(--z-above);
}

.tree-btn {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: var(--space-2) var(--space-3);
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--space-1);
  box-shadow: var(--shadow-sm);
  color: var(--color-text);
  transition: background var(--transition-fast), border-color var(--transition-fast),
              color var(--transition-fast);
  white-space: nowrap;
}
.tree-btn:hover {
  background: var(--color-surface-alt);
  border-color: var(--color-border-dark);
}
.tree-btn.active {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}
.tree-btn-icon {
  width: 36px;
  height: 36px;
  justify-content: center;
  padding: 0;
}

.view-mode-group {
  display: flex;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.view-mode-group .tree-btn {
  border: none;
  border-radius: 0;
  box-shadow: none;
}
.view-mode-group .tree-btn + .tree-btn {
  border-left: 1px solid var(--color-border);
}

/* ── Tree Nodes (D3 foreignObject) ───────────────────────── */
.tree-node-card {
  background: var(--color-surface);
  border: 2px solid var(--color-border-dark);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  overflow: hidden;
  width: 120px;
  min-height: 80px;    /* fallback floor; actual height is driven by content */
  user-select: none;
  /* NOTE: do NOT add position:relative here — it triggers WebKit Bug #165516
     which makes the entire div invisible inside SVG foreignObject on iOS Safari.
     .tree-node-status uses position:absolute inside .tree-node-photo-placeholder
     (a nested element with position:relative). .tree-drag-handle uses
     position:absolute — safe because the bug only affects the direct fO child. */
}
.tree-node-card:hover {
  border-color: var(--color-primary-light);
  box-shadow: var(--shadow-md);
  /* NOTE: do NOT use transform here — CSS transforms inside foreignObject
     cause additional rendering failures on iOS Safari. */
}
.tree-node-card.living { border-color: var(--color-living); }
.tree-node-card.deceased {
  border-color: var(--color-deceased);
  /* NOTE: opacity and filter removed — both create compositing layers on
     foreignObject's direct HTML child, which breaks rendering on iOS Safari
     (WebKit Bug #165516 and iOS 26 extension). Opacity is applied to the
     SVG foreignObject element itself in tree.js instead. */
}
.tree-node-card.selected {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(201,168,76,0.3), var(--shadow-md);
}
.tree-node-card.rel-selected-a {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(26,58,92,0.25), var(--shadow-md);
}
.tree-node-card.rel-selected-b {
  border-color: var(--color-living);
  box-shadow: 0 0 0 3px rgba(42,122,80,0.25), var(--shadow-md);
}
.tree-node-card.highlighted {
  animation: highlight-pulse 1.5s ease-in-out;
}
@keyframes highlight-pulse {
  0%, 100% { box-shadow: var(--shadow); }
  50% { box-shadow: 0 0 0 6px rgba(201,168,76,0.4), var(--shadow-md); }
}

.tree-node-info {
  padding: var(--space-2) var(--space-2) var(--space-2);
}
.tree-node-name {
  font-family: var(--font-serif);
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--color-primary);
  line-height: 1.2;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.tree-node-maiden {
  font-size: 0.68rem;
  color: var(--color-text-muted);
  font-style: italic;
  margin-top: 1px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.tree-node-dates {
  font-size: 0.72rem;
  color: var(--color-text-muted);
  margin-top: 2px;
}

/* Linked-user icon — block element at the bottom of the info area. */
.tree-node-linked-icon {
  font-size: 0.8rem;
  line-height: 1;
  text-align: right;
  padding: 2px 3px 0 0;
}

/* ── D3 SVG Links ────────────────────────────────────────── */
.tree-link {
  fill: none;
  stroke: var(--color-border-dark);
  stroke-width: 1.5;
  opacity: 0.7;
}
/* Solid horizontal marriage bar between adjacent couple cards */
.tree-link-couple {
  stroke: var(--color-primary);
  stroke-width: 2;
  opacity: 0.5;
}
/* Dashed bar for ended marriages (divorce or death + remarriage) */
.tree-link-couple.ended {
  stroke-dasharray: 6, 4;
  opacity: 0.4;
}
/* Bar segment for 3rd+ marriages (between adjacent spouse tiles) */
.tree-link-couple.secondary {
}

/* Line for non-adjacent spouses (cross-tree links) — solid by default */
.tree-link-spouse {
  fill: none;
  stroke: var(--color-primary);
  stroke-width: 1.5;
  opacity: 0.5;
}
/* Dashed when marriage ended */
.tree-link-spouse.ended {
  stroke: var(--color-accent);
  stroke-dasharray: 6, 4;
  opacity: 0.4;
}

/* ── Empty State ─────────────────────────────────────────── */
.tree-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  color: var(--color-text-muted);
  pointer-events: none;
}
.tree-empty.hidden { display: none; }
.tree-empty-icon {
  font-size: 4rem;
  opacity: 0.4;
}
.tree-empty-title {
  font-family: var(--font-serif);
  font-size: 1.5rem;
  color: var(--color-primary);
  opacity: 0.6;
}
.tree-empty-subtitle {
  font-size: 0.9rem;
  max-width: 280px;
  text-align: center;
}
.tree-empty .btn { pointer-events: auto; }

/* ── Relationship mode overlay ───────────────────────────── */
.rel-mode-banner {
  position: absolute;
  bottom: var(--space-4);
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-primary);
  color: #fff;
  padding: var(--space-2) var(--space-3) var(--space-2) var(--space-5);
  border-radius: var(--radius-full);
  font-size: 0.875rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  display: none;
  white-space: nowrap;
  z-index: var(--z-above);
  align-items: center;
  gap: var(--space-3);
}
.rel-mode-banner.visible { display: flex; }

.rel-banner-cancel {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: #fff;
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-full);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}
.rel-banner-cancel:hover {
  background: rgba(255, 255, 255, 0.28);
}

/* ── Metrics pill ────────────────────────────────────────── */
.tree-metrics {
  position: absolute;
  top: calc(var(--space-4) + 36px + var(--space-2));
  right: var(--space-4);
  font-size: 0.72rem;
  color: var(--color-text-muted);
  background: rgba(255,255,255,0.9);
  padding: 3px 10px;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  pointer-events: none;
  z-index: var(--z-above);
}

/* ── Context Menu ────────────────────────────────────────── */
.tree-context-menu {
  position: fixed;
  z-index: var(--z-overlay);
  background: var(--color-surface);
  border: 1px solid var(--color-border-dark);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  min-width: 180px;
  padding: var(--space-1) 0;
  opacity: 0;
  transform: scale(0.95);
  transition: opacity 120ms ease, transform 120ms ease;
  pointer-events: none;
}
.tree-context-menu.visible {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}
.tree-context-menu-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  font-size: 0.875rem;
  color: var(--color-text);
  cursor: pointer;
  transition: background var(--transition-fast);
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: var(--font-sans);
}
.tree-context-menu-item:hover {
  background: var(--color-surface-alt);
}
.tree-context-menu-item-icon {
  width: 16px;
  text-align: center;
  flex-shrink: 0;
}
.tree-context-menu-sep {
  height: 1px;
  background: var(--color-border);
  margin: var(--space-1) 0;
}

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 900px) {
  .sidebar {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    width: 100%;
    height: 50vh;
    border-right: none;
    border-top: 1px solid var(--color-border);
    transform: translateY(100%);
    transition: transform var(--transition-slow);
  }
  .sidebar.open {
    transform: translateY(0);
  }
  .sidebar.collapsed {
    width: 100%;
    transform: translateY(100%);
  }
  .tree-canvas {
    width: 100%;
  }
  .tree-node-card {
    width: 100px;
  }
  .tree-toolbar {
    max-width: calc(100vw - 80px); /* leave room for zoom controls on the left */
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* ── Relationship Path Highlight ─────────────────────────── */
/* Applied to .tree-node-card divs whose member is on the path */
.rel-path-node {
  outline: 2.5px solid #f59e0b;
  box-shadow: 0 0 14px 3px rgba(245, 158, 11, 0.5), var(--shadow-md);
  border-radius: 12px;
  z-index: 2;
  position: relative;
}
/* Applied to SVG path/line elements whose endpoints are on the path */
.rel-path-link {
  stroke: #f59e0b !important;
  stroke-width: 3px !important;
  stroke-dasharray: none !important;
  opacity: 1 !important;
  filter: drop-shadow(0 0 4px rgba(245, 158, 11, 0.6));
}

/* ── Drag-to-link ────────────────────────────────────────── */
.tree-drag-handle {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 18px; height: 18px;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; line-height: 1;
  color: var(--color-text-faint);
  cursor: grab;
  border-radius: 3px;
  background: rgba(255,255,255,0.8);
  opacity: 0;
  transition: opacity var(--transition-fast);
  user-select: none;
}
.tree-node-card:hover .tree-drag-handle { opacity: 1; }
.tree-drag-handle:active { cursor: grabbing; }

.tree-node-card.drag-source {
  opacity: 0.55;
  border-style: dashed;
}
.tree-node-card.drag-snap-target {
  border-color: var(--color-accent) !important;
  box-shadow: 0 0 0 4px rgba(201,168,76,0.4), var(--shadow-md);
}
.tree-link-couple.drag-snap-bar {
  stroke: var(--color-accent) !important;
  stroke-width: 4px !important;
  opacity: 0.9 !important;
}
.tree-drag-line { pointer-events: none; }

/* Touch: drag handle always visible on touch-primary devices */
@media (hover: none) {
  .tree-drag-handle { opacity: 1; }
}

/* ── Link hit areas & break hover ────────────────────────── */
.tree-link-hit,
.tree-link-couple-hit,
.tree-link-spouse-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 12px;
  cursor: pointer;
  pointer-events: stroke;
}
.tree-link.link-hover {
  stroke: #dc2626 !important;
  stroke-width: 3px !important;
  opacity: 1 !important;
}
.tree-link-couple.link-hover,
.tree-link-spouse.link-hover {
  stroke: #dc2626 !important;
  stroke-width: 4px !important;
  opacity: 1 !important;
}
.remove-link-btn { color: #dc2626 !important; }
.remove-link-btn:hover { background: #fee2e2 !important; }

/* ── Per-Member Change History ───────────────────────────── */
.profile-history-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  color: inherit;
}
.profile-history-toggle:hover .profile-section-title {
  color: var(--color-primary);
}
.profile-history-chevron {
  color: var(--color-text-faint);
  transition: transform var(--transition-fast);
  flex-shrink: 0;
}
.profile-history-body {
  margin-top: var(--space-2);
}
.history-empty {
  padding: var(--space-2) 0;
}
.history-entry {
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--color-border);
  font-size: 0.8125rem;
}
.history-entry:last-child {
  border-bottom: none;
}
.history-entry-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-1);
}
.history-entry-who {
  font-weight: 600;
  color: var(--color-primary);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-entry-when {
  font-size: 0.75rem;
  color: var(--color-text-faint);
  white-space: nowrap;
  flex-shrink: 0;
}
.history-rollback-btn {
  padding: 2px 4px !important;
  font-size: 0.75rem !important;
  height: auto !important;
  flex-shrink: 0;
  color: var(--color-text-faint) !important;
}
.history-rollback-btn:hover {
  color: var(--color-primary) !important;
}
.history-rollback-badge {
  font-size: 0.7rem;
  color: var(--color-text-faint);
  background: var(--color-bg-subtle, #f0ede8);
  border-radius: 4px;
  padding: 1px 5px;
  flex-shrink: 0;
  white-space: nowrap;
}
.history-entry-changes {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.history-field-row {
  color: var(--color-text-muted);
  line-height: 1.4;
  word-break: break-word;
}
