/**
 * High Velocity Labs - Interactive Horizontal Branching Timeline
 * Self-contained visualizer with alternating top/bottom stems, pan/zoom, and clustering.
 * Strict HVL Standard: < 400 lines.
 */

.branch-timeline-wrapper {
  background: var(--cream-card);
  border: 1px solid rgba(164, 32, 56, 0.22);
  border-radius: var(--radius-md);
  margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
  position: relative;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}
.timeline-fullscreen-active { overflow: hidden !important; }
.branch-timeline-wrapper.branch-fullscreen {
  position: fixed !important; inset: 0 !important; top: 0 !important; left: 0 !important;
  width: 100vw !important; height: 100vh !important; max-width: none !important;
  z-index: 99999 !important; border-radius: 0 !important; margin: 0 !important; border: none !important;
  display: flex !important; flex-direction: column !important; background: var(--cream-card) !important;
}
.branch-timeline-wrapper.branch-fullscreen .branch-viewport {
  flex: 1 1 0% !important; min-height: 0 !important; height: auto !important; width: 100% !important;
}

/* View Switcher */
.view-switch-group {
  display: inline-flex; background: rgba(164, 32, 56, 0.08); padding: 2px;
  border-radius: var(--radius-sm); border: 1px solid rgba(164, 32, 56, 0.2); flex-shrink: 0;
}
.view-btn {
  background: transparent; border: none; padding: 4px 10px; font-size: 0.76rem; font-weight: 600;
  color: var(--text-secondary); border-radius: var(--radius-xs); cursor: pointer;
  display: inline-flex; align-items: center; gap: 5px; transition: all 0.15s ease;
}
.view-btn:hover { color: var(--burgundy); }
.view-btn.active { background: var(--burgundy); color: #ffffff; box-shadow: 0 1px 4px rgba(164, 32, 56, 0.25); }

/* Branch Toolbar */
.branch-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 14px; background: rgba(255, 255, 240, 0.92); border-bottom: 1px solid rgba(164, 32, 56, 0.15);
  font-family: var(--font-sans);
}
.branch-toolbar-left { display: flex; align-items: center; gap: 8px; }
.branch-window-label { font-family: var(--font-serif-display); font-weight: 700; font-size: 0.95rem; color: var(--burgundy); }
.branch-count-pill {
  font-size: 0.72rem; font-weight: 600; background: var(--burgundy-subtle); color: var(--burgundy);
  padding: 2px 8px; border-radius: var(--radius-pill); border: 1px solid var(--burgundy-border);
}
.branch-toolbar-right { display: flex; align-items: center; gap: 6px; }
.branch-btn {
  background: #ffffff;
  border: 1px solid #d8c3a8;
  color: var(--text-secondary);
  padding: 4px 9px;
  border-radius: var(--radius-sm);
  font-size: 0.76rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s ease;
}
.branch-btn:hover { background: var(--burgundy-subtle); border-color: var(--burgundy); color: var(--burgundy); }
.branch-btn.active { background: var(--burgundy); color: #ffffff; border-color: var(--burgundy); }

/* Main Viewport */
.branch-viewport {
  position: relative;
  width: 100%;
  height: 480px;
  overflow: hidden;
  cursor: grab;
  background: radial-gradient(circle at center, rgba(255, 255, 240, 0.6) 0%, rgba(253, 250, 230, 0.95) 100%);
}
.branch-viewport:active {
  cursor: grabbing;
}

/* Edge Overflow Treatment & Indicators */
.branch-viewport::before,
.branch-viewport::after {
  content: ''; position: absolute; top: 0; bottom: 0; width: 48px;
  pointer-events: none; z-index: 18; opacity: 0; transition: opacity 0.25s ease;
}
.branch-viewport::before { left: 0; background: linear-gradient(to right, rgba(164, 32, 56, 0.14), transparent); }
.branch-viewport::after { right: 0; background: linear-gradient(to left, rgba(164, 32, 56, 0.14), transparent); }
.branch-viewport.has-overflow-left::before,
.branch-viewport.has-overflow-right::after { opacity: 1; }

.branch-edge-indicator {
  position: absolute; bottom: 12px;
  background: rgba(255, 255, 255, 0.94); border: 1px solid rgba(164, 32, 56, 0.28);
  color: var(--burgundy); border-radius: var(--radius-pill); padding: 3px 9px;
  font-family: var(--font-sans); font-size: 0.70rem; font-weight: 600;
  box-shadow: 0 2px 8px rgba(70, 19, 0, 0.10); z-index: 22;
  display: inline-flex; align-items: center; gap: 4px;
  pointer-events: none; backdrop-filter: blur(6px); user-select: none;
}
.branch-edge-indicator.edge-left { left: 16px; }
.branch-edge-indicator.edge-right { right: 16px; }

/* Canvas / SVG World Layer */
.branch-world {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  will-change: transform;
}

.branch-svg-layer, #branch-nodes-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#branch-nodes-layer { z-index: 10; }

/* Central Horizontal Axis Line */
.branch-axis-line {
  stroke: var(--burgundy);
  stroke-width: 2.5;
  stroke-linecap: round;
}
.branch-tick-line {
  stroke: rgba(164, 32, 56, 0.35);
  stroke-width: 1.5;
}
.branch-subtick-line {
  stroke: rgba(164, 32, 56, 0.18);
  stroke-width: 1;
}

/* Year Markings on Central Axis */
.branch-year-node {
  position: absolute;
  transform: translate(-50%, -50%);
  background: #ffffff;
  border: 1.5px solid var(--burgundy);
  color: var(--burgundy);
  font-family: var(--font-serif-display);
  font-size: 0.76rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-sm);
  z-index: 10;
  pointer-events: none;
}
.branch-year-node.today-marker {
  background: var(--burgundy);
  color: #ffffff;
  border-color: var(--burgundy-dark);
}

.branch-viewport.is-moving { cursor: grabbing; }
.branch-viewport.is-moving .branch-node,
.branch-viewport.is-moving .branch-cluster { pointer-events: none !important; }
.branch-viewport.is-moving .branch-popover { display: none !important; }

@keyframes bubblePopIn {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.35); }
  100% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

@keyframes bubblePopOut {
  0% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(0.35); }
}

.branch-node.bubble-leaving,
.branch-cluster.bubble-leaving {
  animation: bubblePopOut 0.68s cubic-bezier(0.16, 1, 0.3, 1) forwards !important;
  pointer-events: none !important;
  z-index: 5 !important;
}

/* Branch Nodes (Moments) */
.branch-node {
  position: absolute; transform: translate(-50%, -50%); display: flex; align-items: center; gap: 6px;
  padding: 4px 9px 4px 6px; background: #ffffff; border-radius: var(--radius-pill);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.28), 0 1px 4px rgba(0, 0, 0, 0.16), 0 0 0 1.5px rgba(255, 255, 255, 0.95);
  border: 1.5px solid var(--node-border, #d8c3a8); cursor: pointer; pointer-events: auto;
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.25s ease, border-color 0.25s ease;
  animation: bubblePopIn 0.68s cubic-bezier(0.16, 1, 0.3, 1) backwards; z-index: 15; max-width: 180px; white-space: nowrap;
}
.branch-node.has-extra { cursor: pointer; }
.branch-node.has-photos { border-color: rgba(164, 32, 56, 0.50); }
.branch-node:hover {
  transform: translate(-50%, -50%) scale(1.08);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.38), 0 2px 8px rgba(164, 32, 56, 0.30), 0 0 0 2px var(--burgundy);
  border-color: var(--burgundy); z-index: 30;
}
.branch-node-avatar-wrap { position: relative; width: 24px; height: 24px; flex-shrink: 0; }
.branch-node-icon-box {
  width: 24px; height: 24px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  background: var(--node-bg, rgba(164, 32, 56, 0.10)); color: var(--node-color, var(--burgundy)); flex-shrink: 0;
}
.branch-node-icon-box.is-photo-box { background: rgba(164, 32, 56, 0.14); color: var(--burgundy); }
.branch-node-photo { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; flex-shrink: 0; border: 1px solid var(--burgundy); }
.branch-photo-badge {
  position: absolute; bottom: -3px; right: -3px; width: 13px; height: 13px; border-radius: 50%;
  background: var(--burgundy); color: #ffffff; display: flex; align-items: center; justify-content: center;
  border: 1.5px solid #ffffff; box-shadow: 0 1px 4px rgba(70, 19, 0, 0.25); pointer-events: none;
}
.branch-photo-badge .jt-icon { width: 8px; height: 8px; }
.branch-photo-title-icon { color: var(--burgundy); vertical-align: -1px; margin-left: 2px; }
.branch-node-text { display: flex; flex-direction: column; overflow: hidden; line-height: 1.15; }
.branch-node-date { font-size: 0.65rem; font-weight: 700; color: var(--burgundy); font-family: var(--font-serif-body); }
.branch-node-title { font-size: 0.74rem; font-weight: 600; color: var(--text-primary); overflow: hidden; text-overflow: ellipsis; max-width: 120px; }

/* Cluster Bubble (+N) */
.branch-cluster {
  position: absolute;
  transform: translate(-50%, -50%);
  background: var(--burgundy);
  color: #ffffff;
  border: 2px solid #ffffff;
  border-radius: var(--radius-pill);
  padding: 4px 11px;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 700;
  box-shadow: 0 5px 18px rgba(0, 0, 0, 0.32), 0 2px 6px rgba(164, 32, 56, 0.35);
  cursor: pointer;
  pointer-events: auto;
  z-index: 20;
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), background 0.2s ease, box-shadow 0.2s ease;
  animation: bubblePopIn 0.68s cubic-bezier(0.16, 1, 0.3, 1) backwards;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 5px;
}
.branch-cluster:hover {
  transform: translate(-50%, -50%) scale(1.14);
  background: var(--burgundy-dark);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45), 0 2px 8px rgba(164, 32, 56, 0.40);
}
.branch-cluster .branch-photo-badge {
  position: absolute; bottom: -4px; right: -5px; width: 14px; height: 14px; border-radius: 50%;
  background: #ffffff; color: var(--burgundy); border: 1.5px solid var(--burgundy);
  display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 5px rgba(70, 19, 0, 0.28);
}
.branch-cluster .branch-photo-badge .jt-icon { width: 8px; height: 8px; }

/* Stem Line */
.branch-stem-line {
  stroke: var(--stem-color, rgba(164, 32, 56, 0.45));
  stroke-width: 1.5;
  stroke-dasharray: 2, 2;
}
.branch-stem-line.solid {
  stroke-dasharray: none;
}
.branch-stem-joint {
  fill: var(--burgundy);
}

/* Rich Floating Hover Popover (Always Bounded in Viewport) */
.branch-popover {
  position: absolute;
  z-index: 50;
  background: #ffffff;
  border: 1.5px solid var(--burgundy);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  box-shadow: var(--shadow-lg);
  max-width: 290px;
  max-height: 250px;
  overflow-y: auto;
  pointer-events: auto;
  font-family: var(--font-sans);
  animation: popoverFadeIn 0.15s ease-out;
  scrollbar-width: thin;
  scrollbar-color: rgba(164, 32, 56, 0.35) transparent;
}
.branch-popover.popover-above {
  transform: translate(-50%, -100%);
  margin-top: -10px;
}
.branch-popover.popover-below {
  transform: translate(-50%, 0);
  margin-top: 10px;
}
@keyframes popoverFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.branch-popover-date {
  font-family: var(--font-serif-body);
  font-weight: 700;
  font-size: 0.80rem;
  color: var(--burgundy);
  margin-bottom: 2px;
}
.branch-popover-title {
  font-size: 0.90rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 5px;
  line-height: 1.25;
}
.branch-popover-desc {
  font-size: 0.80rem;
  color: var(--text-secondary);
  line-height: 1.4;
  margin-bottom: 6px;
  max-height: 120px;
  overflow-y: auto;
}
.branch-popover-hint {
  font-size: 0.70rem;
  color: var(--burgundy);
  font-weight: 600;
  border-top: 1px dashed rgba(164, 32, 56, 0.2);
  padding-top: 5px;
}
.branch-popover-hint.is-clickable {
  cursor: pointer;
  transition: color 0.15s ease, transform 0.15s ease;
}
.branch-popover-hint.is-clickable:hover {
  color: var(--burgundy-dark);
  text-decoration: underline;
}
.branch-cluster-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-top: 8px;
  max-height: 160px;
  overflow-y: auto;
}
.branch-cluster-item {
  display: flex; align-items: center; gap: 7px; padding: 4px 7px;
  border-radius: var(--radius-xs); background: rgba(164, 32, 56, 0.04);
  cursor: default; font-size: 0.78rem; color: var(--text-primary);
}
.branch-cluster-item.has-extra { cursor: pointer; transition: all 0.15s ease; }
.branch-cluster-item.has-extra:hover { background: var(--burgundy-subtle); color: var(--burgundy); }
.branch-cluster-item-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.branch-cluster-item-date { font-size: 0.70rem; color: var(--burgundy); font-weight: 700; flex-shrink: 0; }
.branch-cluster-item-icon-box {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 50%; background: var(--burgundy-subtle); color: var(--burgundy); flex-shrink: 0;
}
.branch-cluster-item-icon-box .branch-photo-badge {
  position: absolute; bottom: -3px; right: -4px; width: 10px; height: 10px;
}
.branch-cluster-item-icon-box .branch-photo-badge .jt-icon { width: 6px; height: 6px; }
.branch-cluster-badge {
  font-size: 0.65rem; background: var(--burgundy-subtle); color: var(--burgundy);
  padding: 1px 5px; border-radius: var(--radius-pill); font-weight: 600; margin-left: auto; flex-shrink: 0;
  display: inline-flex; align-items: center; gap: 3px;
}
.branch-popover-photo-pill {
  display: inline-flex; align-items: center; gap: 3px; background: var(--burgundy-subtle);
  color: var(--burgundy); font-size: 0.65rem; font-weight: 600; padding: 1px 6px; border-radius: var(--radius-pill); margin-left: 6px;
}

/* Category Color Mappings */
.cat-wedding { --node-color: var(--cat-wedding); --node-bg: var(--cat-wedding-bg); --node-border: var(--cat-wedding-border); --stem-color: var(--cat-wedding); }
.cat-travel { --node-color: var(--cat-travel); --node-bg: var(--cat-travel-bg); --node-border: var(--cat-travel-border); --stem-color: var(--cat-travel); }
.cat-family, .cat-dating { --node-color: var(--cat-family); --node-bg: var(--cat-family-bg); --node-border: var(--cat-family-border); --stem-color: var(--cat-family); }
.cat-fun { --node-color: var(--cat-fun); --node-bg: var(--cat-fun-bg); --node-border: var(--cat-fun-border); --stem-color: var(--cat-fun); }
.cat-career { --node-color: var(--cat-career); --node-bg: var(--cat-career-bg); --node-border: var(--cat-career-border); --stem-color: var(--cat-career); }
.cat-milestone { --node-color: var(--cat-milestone); --node-bg: var(--cat-milestone-bg); --node-border: var(--cat-milestone-border); --stem-color: var(--cat-milestone); }
