/* Right-hand family tree panel: title, SVG graph canvas, legend. */

/* Height comes from stretching to fill .case-form-workspace's full height
   (see case-form.css), not from content or from this panel's own width --
   the panel is a flex column so the title/toolbar/legend take their
   natural height and .family-tree__body/__stage absorb whatever's left,
   automatically filling the empty space down to the footer. This keeps
   the graph's height stable while its width flexes with the node
   inspector opening/closing. */
.family-tree {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 22rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  padding: 1rem 1.25rem 1.25rem;
}

@media (max-width: 960px) {
  .family-tree {
    height: 32rem;
  }
}

.family-tree__title {
  margin: 0 0 0.75rem;
  font-size: 0.9375rem;
  font-weight: 700;
}

.family-tree__empty {
  color: var(--color-text-muted);
  font-size: 0.875rem;
  padding-block: 2rem;
  text-align: center;
  margin: 0;
}

/* Fills whatever's left in the .family-tree column below the title (see
   the flex column set up on .family-tree above) -- both possible
   children (.family-tree__interactive once a graph exists, or
   .family-tree__empty before/without one) need this to actually be tall,
   so each is also flexed to 100% below. */
.family-tree__canvas {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.family-tree__canvas > .family-tree__empty {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
}

.family-tree__empty p {
  margin: 0;
}

.family-tree__interactive {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

.family-tree__empty--error {
  color: var(--color-danger);
}

/* ---------- Toolbar ---------- */

.family-tree__toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-block-end: 0.75rem;
}

/* "Show inferred relationships": a checkbox among the toolbar buttons. */
.family-tree__toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--color-text);
  cursor: pointer;
  user-select: none;
}

.family-tree__toggle input {
  accent-color: var(--color-accent);
  cursor: pointer;
}

/* While it is off (the default), inferred relationships -- the edge, its
   label, its click target and its legend entry -- are not shown: the tree
   is only what was entered. Set by family-tree.js on the interactive root. */
.family-tree__interactive[data-inferred="hidden"] .family-tree__edge--inferred,
.family-tree__interactive[data-inferred="hidden"] .family-tree__edge-hit--inferred,
.family-tree__interactive[data-inferred="hidden"] .family-tree__edge-label--inferred,
.family-tree__interactive[data-inferred="hidden"] .family-tree__legend-item--inferred {
  display: none;
}

.family-tree__btn {
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 0.375rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface-alt);
  color: var(--color-text);
  cursor: pointer;
  transition: background var(--transition-fast);
}

.family-tree__btn:hover {
  background: var(--color-border);
}

/* Square icon buttons (+ / − zoom, full screen): fixed size, glyph or
   icon centred, instead of sizing to a single character. */
.family-tree__btn--icon {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 2rem;
  block-size: 2rem;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
}

/* Pushed to the far end of the toolbar, apart from the view/data actions. */
.family-tree__fullscreen {
  margin-inline-start: auto;
}

/* Zoom + / − overlaid on the tree's top-left corner (inside the stage,
   above the SVG), stacked like a map's zoom control. On the surface
   colour with a light shadow so they read over the canvas. */
.family-tree__zoom {
  position: absolute;
  inset-block-start: 0.5rem;
  inset-inline-start: 0.5rem;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.family-tree__zoom .family-tree__btn {
  background: var(--color-surface);
  box-shadow: var(--shadow-sm);
}

.family-tree__zoom .family-tree__btn:hover {
  background: var(--color-surface-alt);
}

/* ---------- Full screen ---------- */

/* Set by family-tree.js both for the browser's real full screen and for
   the fixed-overlay fallback where that isn't available. The background
   matters in the real case too: a full-screen element is transparent by
   default, over the browser's black ::backdrop. */
.family-tree__interactive[data-fullscreen="on"] {
  position: fixed;
  inset: 0;
  z-index: 150;
  padding: 1rem 1.25rem;
  background: var(--color-surface);
  color: var(--color-text);
}

/* ---------- Stale banner ---------- */

.family-tree__stale-banner {
  margin: 0 0 0.75rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-node-incomplete-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface-alt);
  color: var(--color-text);
  font-size: 0.8125rem;
}

.family-tree__stale-action {
  margin-inline-start: 0.25rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--color-accent);
  font: inherit;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

.family-tree__stale-action:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

/* ---------- Body: stage + inspector side by side ---------- */

/* Two columns, not stage-then-inspector-below: selecting a node/edge must
   show its details without scrolling the page. The inspector only takes
   space when a selection exists (it's `hidden`, not just empty), so the
   stage alone fills this row the rest of the time. */
.family-tree__body {
  display: flex;
  align-items: stretch;
  gap: 0.75rem;
  flex: 1 1 auto;
  min-height: 0;
}

/* No aspect-ratio: height comes from .family-tree__body stretching to
   fill the panel's viewport-derived height (see .family-tree above), not
   from this stage's own width -- so it stays put while the column's width
   flexes (inspector open/closed, sidebar toggle, ...). */
.family-tree__stage {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-tree-stage);
  overflow: hidden;
  touch-action: none;
}

.family-tree__svg {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

.family-tree__svg:active {
  cursor: grabbing;
}

/* ---------- Edges ---------- */

/* Every edge is a <path> (see buildEdges() in family-tree.js) -- elbow,
   straight or curved -- and a path fills black unless told otherwise. */
.family-tree__edge {
  stroke: var(--color-edge);
  stroke-width: 1.5;
  fill: none;
  stroke-linejoin: round;
}

.family-tree__edge--curved {
  fill: none;
}

/* Same-generation relationships (spouse, sibling) -- no ancestor/
   descendant direction, but still drawn as a directed edge like every
   other one (see family-tree.js), just in this color instead of
   --color-edge so "peer-level" reads at a glance without needing a
   different line style to do it. */
.family-tree__edge--lateral {
  stroke: var(--color-edge-lateral);
}

.family-tree__edge--incomplete {
  stroke-dasharray: 2 4;
  opacity: 0.6;
}

/* Derived, not typed in -- kept visually quieter (finer dash, lower
   opacity) than a stated relationship so the two are never mistaken for
   each other at a glance; the inspector spells out "Inferred" on click. */
.family-tree__edge--inferred {
  stroke-dasharray: 1.5 3;
  opacity: 0.55;
}

.family-tree__arrowhead path {
  fill: var(--color-edge);
}

/* Matching arrowhead for lateral edges -- a second <marker> (see
   buildArrowMarker() in family-tree.js) rather than trying to recolor
   one shared marker per edge, since an SVG marker's fill isn't
   selector-addressable from the element using it. */
.family-tree__arrowhead--lateral path {
  fill: var(--color-edge-lateral);
}

/* Hover focus (see setHover() in family-tree.js): dimmed rather than
   hidden, so the overall shape of the graph stays visible while a node's
   immediate neighborhood is picked out -- same idea as Neo4j Browser's
   hover highlight. Transition matches the node dim below. */
.family-tree__edge--dim,
.family-tree__edge-label--dim {
  opacity: 0.12;
  transition: opacity var(--transition-fast);
}

/* font-size divides by --ft-scale (set in family-tree.js's applyTransform)
   so relationship text stays a constant, readable *screen* size at any
   zoom level -- without this it lives inside the same scaled <g> as the
   node circles and shrinks right along with them once a larger tree gets
   zoomed out to fit. The stroke/paint-order pair behind it is a text
   "halo" the same color as the stage background, giving the label a
   readable backdrop wherever an edge or another label crosses behind it
   -- cheaper than measuring and drawing a real background rect per label. */
.family-tree__edge-label {
  fill: var(--color-edge-label);
  font-size: calc(0.6875rem / var(--ft-scale, 1));
  paint-order: stroke;
  stroke: var(--color-surface-alt);
  stroke-width: calc(3px / var(--ft-scale, 1));
  stroke-linejoin: round;
  pointer-events: none;
}

.family-tree__edge-label--incomplete {
  opacity: 0.6;
}

/* An elbow edge's label sits *on* the child's vertical drop (see
   elbowRoute() in family-tree-shapes.js), centered on its point rather
   than raised above a straight line; the halo above keeps it readable
   over the line itself. */
.family-tree__edge-label--elbow {
  dominant-baseline: central;
}

/* Invisible, wide duplicate of each edge's path/line -- the visible
   stroke is too thin to reliably click; this is what pointer events
   actually hit. Follows the same "d"/coordinates as its visible sibling
   (see buildEdges() in family-tree.js). */
.family-tree__edge-hit {
  stroke: transparent;
  stroke-width: 14;
  fill: none;
  cursor: pointer;
}

.family-tree__edge.is-selected {
  stroke: var(--color-accent);
  opacity: 1;
}

/* ---------- Nodes ---------- */

.family-tree__node-circle {
  stroke: var(--color-surface);
  stroke-width: 2;
  transition: stroke-width var(--transition-fast);
}

.family-tree__node--deceased .family-tree__node-circle {
  fill: var(--color-tree-node-deceased);
}

/* Alive (or not stated): no state colour -- deceased, Layak and Terhalang
   are the states worth marking, alive is the normal case. A plain circle
   with a thin border, so the node still reads as a shape behind its icon. */
.family-tree__node--heir .family-tree__node-circle {
  fill: var(--color-surface);
  stroke: var(--color-border);
}

/* Step 2's outcome, applied only once Step 2 has been run (see
   setFamilyTreeEligibility() in family-tree.js). Deceased nodes never get
   these classes, so they stay red. Must follow the --heir rule above. */
.family-tree__node--eligible .family-tree__node-circle {
  fill: var(--color-tree-node-eligible);
}

.family-tree__node--tanazul .family-tree__node-circle {
  fill: var(--color-tree-node-tanazul);
}

.family-tree__node--blocked .family-tree__node-circle {
  fill: var(--color-tree-node-blocked);
}

.family-tree__node--incomplete .family-tree__node-circle {
  stroke: var(--color-node-incomplete-border);
  stroke-dasharray: 3 2;
  opacity: 0.75;
}

/* Grab/grabbing rather than pointer -- nodes are draggable now, not just
   clickable (see the pointerdown handler in family-tree.js). Scaling the
   circle itself (not the whole <g>, which also carries the simulation's
   translate()) needs fill-box/center so it grows in place instead of
   drifting toward the SVG origin. */
.family-tree__node {
  cursor: grab;
}

.family-tree__node:active {
  cursor: grabbing;
}

.family-tree__node:hover .family-tree__node-circle {
  stroke-width: 3;
}

.family-tree__node.is-selected .family-tree__node-circle {
  stroke: var(--color-accent);
  stroke-width: 4;
}

.family-tree__node:focus-visible .family-tree__node-circle {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

/* Hover focus companion to .family-tree__edge--dim above -- everything
   outside the hovered node's immediate neighborhood fades back. */
.family-tree__node--dim {
  opacity: 0.3;
  transition: opacity var(--transition-fast);
}

/* Person icon (male / female / not stated -- see family-tree-shapes.js)
   drawn on top of the node circle in the fixed on-fill color, since it
   sits directly on a saturated node fill. Not a pointer target: the
   circle underneath stays the solid drag/click area. */
.family-tree__node-icon {
  fill: var(--color-node-label-on-fill);
  pointer-events: none;
}

/* On the plain alive circle: muted, like other secondary marks. */
.family-tree__node--heir .family-tree__node-icon {
  fill: var(--color-text-muted);
}

/* ...but dark on the light deceased / Layak / Terhalang fills. */
.family-tree__node--deceased .family-tree__node-icon,
.family-tree__node--eligible .family-tree__node-icon,
.family-tree__node--blocked .family-tree__node-icon,
.family-tree__node--tanazul .family-tree__node-icon {
  fill: var(--color-tree-node-icon-on-light);
}

/* Male / female avatar (frontend/assets/icons/): full colour of its own,
   drawn inside the node circle so the state colour shows as a ring around
   it. Not a pointer target either, same as the icon above. */
.family-tree__node-avatar {
  pointer-events: none;
}

/* The name, just below the circle (the icon fills it): page text color
   with the same stage-colored halo as .family-tree__edge-label, so it
   stays readable where an edge passes behind it. font-size divides by
   --ft-scale for the same reason as .family-tree__edge-label above --
   constant, readable screen size at any zoom level rather than shrinking
   with the node circles. Unlike the icon it stays a pointer target:
   clicking or dragging the name acts on its node, as it did when the
   name sat inside the circle. */
.family-tree__node-label {
  fill: var(--color-text);
  font-size: calc(0.6875rem / var(--ft-scale, 1));
  font-weight: 600;
  dominant-baseline: hanging;
  paint-order: stroke;
  stroke: var(--color-surface-alt);
  stroke-width: calc(3px / var(--ft-scale, 1));
  stroke-linejoin: round;
}

/* ---------- Legend ---------- */

.family-tree__legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  margin-block-start: 0.75rem;
  padding-block-start: 0.75rem;
  border-block-start: 1px solid var(--color-border);
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.family-tree__legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
}

.family-tree__legend-icon {
  inline-size: 1rem;
  block-size: 1rem;
  flex: 0 0 auto;
  fill: var(--color-text-muted);
}

.family-tree__legend-swatch {
  width: 0.75rem;
  height: 0.75rem;
  border-radius: 50%;
  flex: 0 0 auto;
}

.family-tree__legend-swatch--deceased {
  background: var(--color-tree-node-deceased);
}

.family-tree__legend-swatch--eligible {
  background: var(--color-tree-node-eligible);
}

.family-tree__legend-swatch--blocked {
  background: var(--color-tree-node-blocked);
}

.family-tree__legend-swatch--tanazul {
  background: var(--color-tree-node-tanazul);
}

/* Layak / Terhalang only mean something once Step 2 has run. */
/* "Incomplete row": only when the tree has one (see renderFamilyTree()). */
.family-tree__interactive:not([data-incomplete="present"]) .family-tree__legend-item--incomplete {
  display: none;
}

.family-tree__interactive:not([data-eligibility="on"]) .family-tree__legend-item--eligibility {
  display: none;
}

/* Tanazul: only when an heir on this tree gave up a share (setFamilyTreeEligibility()). */
.family-tree__interactive:not([data-tanazul="on"]) .family-tree__legend-item--tanazul {
  display: none;
}

.family-tree__legend-swatch--incomplete {
  background: transparent;
  border: 2px dashed var(--color-node-incomplete-border);
}

.family-tree__legend-line {
  width: 1rem;
  height: 0;
  border-block-start: 2px dashed var(--color-edge);
  opacity: 0.55;
  flex: 0 0 auto;
}

.family-tree__legend-line--lateral {
  border-block-start: 2px solid var(--color-edge-lateral);
  opacity: 1;
}

.family-tree__legend-hint {
  margin-inline-start: auto;
  font-style: italic;
}

/* ---------- Inspector ---------- */

/* Fixed, narrow, and scrolls internally rather than growing taller than
   the stage (whose height comes from .family-tree__body stretching to
   fill the panel -- see .family-tree__stage above) -- just wide enough to
   read facts/triples comfortably without eating into the graph's own
   space. */
.family-tree__inspector {
  position: relative;
  flex: 0 0 15rem;
  min-width: 0;
  overflow-y: auto;
  padding: 0.875rem 1rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface-alt);
  font-size: 0.8125rem;
}

.family-tree__inspector h3 {
  margin: 0 0.5rem 0.5rem 0;
  font-size: 0.9375rem;
}

.family-tree__inspector h4 {
  margin: 0.75rem 0 0.375rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
}

.family-tree__inspector-close {
  position: absolute;
  inset-block-start: 0.5rem;
  inset-inline-end: 0.5rem;
  width: 1.5rem;
  height: 1.5rem;
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  border-radius: var(--radius-sm);
}

.family-tree__inspector-close:hover {
  background: var(--color-border);
  color: var(--color-text);
}

.family-tree__inspector-facts {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.25rem 0.75rem;
  margin: 0;
}

.family-tree__inspector-facts dt {
  color: var(--color-text-muted);
}

.family-tree__inspector-facts dd {
  margin: 0;
}

.family-tree__inspector-facts code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.75rem;
}

.family-tree__inspector-warn {
  color: var(--color-node-incomplete-border);
  font-weight: 600;
}

.family-tree__inspector-triples {
  margin: 0;
  padding-inline-start: 1.125rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.family-tree__inspector-triples code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.75rem;
  color: var(--color-edge-label);
}

/* A fixed 15rem side panel is fine once the tree column has real width to
   spare (see case-form.css's own breakpoint at the same width), but would
   crowd the stage on a phone-width column -- stack instead below it. */
@media (max-width: 960px) {
  .family-tree__body {
    flex-direction: column;
  }

  .family-tree__inspector {
    flex: 0 0 auto;
    width: 100%;
    max-height: 16rem;
  }
}
