:root {
  --paper: #f3f0e7;
  --paper-2: #fbf9f3;
  --card: #ffffff;
  --ink: #1d2231;
  --ink-soft: #4d5566;
  --muted: #7b8493;
  --line: rgba(29, 34, 49, .14);
  --line-soft: rgba(29, 34, 49, .08);
  --accent: #a9482a;
  --accent-soft: #e2b38a;
  --teal: #2d6a66;
  --gold: #c9932f;
  --shadow: 0 10px 30px rgba(20, 25, 40, .10);
  --radius: 10px;
}

[data-theme="dark"] {
  --paper: #12151b;
  --paper-2: #191d25;
  --card: #1e232c;
  --ink: #e9e6dd;
  --ink-soft: #a9b2c0;
  --muted: #7f8894;
  --line: rgba(233, 230, 221, .16);
  --line-soft: rgba(233, 230, 221, .08);
  --accent: #e0804f;
  --accent-soft: #7a4d31;
  --teal: #5b9f96;
  --gold: #d9a441;
  --shadow: 0 12px 34px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }

html, body { margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  min-height: 100vh;
}

h1, h2, h3 { font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif; margin: 0; }

/* ---------- header ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 18px 26px;
  border-bottom: 1px solid var(--line);
  background: var(--paper-2);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 14px; }
.crest { width: 42px; height: 42px; color: var(--accent); }
.brand h1 { font-size: 26px; letter-spacing: .01em; }
.brand .light { color: var(--muted); font-style: italic; }
.brand .sub { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }
.brand .sub em { font-style: italic; color: var(--ink-soft); }

.quickstats { display: flex; gap: 18px; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; align-items: flex-end; }
.stat b { font-size: 20px; font-family: Georgia, serif; }
.stat span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }

/* ---------- tabs ---------- */
.tabs {
  display: flex;
  gap: 6px;
  padding: 10px 26px;
  border-bottom: 1px solid var(--line);
  /* a sticky bar needs an opaque background, otherwise the page scrolls under it */
  background: var(--paper-2);
  align-items: center;
  flex-wrap: wrap;
}
.tab {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-soft);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13.5px;
  cursor: pointer;
}
.tab:hover { background: var(--card); }
.tab.is-active { background: var(--accent); color: #fff; border-color: var(--accent); }
.theme-toggle { margin-left: auto; }
.theme-toggle button {
  border: 1px solid var(--line); background: var(--card); color: var(--ink-soft);
  width: 34px; height: 30px; border-radius: 8px; cursor: pointer; font-size: 15px;
}

/* ---------- controls ---------- */
.controls {
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
  padding: 12px 26px;
  border-bottom: 1px solid var(--line);
  background: var(--paper-2);
}
.field { display: flex; align-items: center; gap: 8px; }
.field span { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.field input[type="search"], .field input[type="number"] {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  padding: 8px 10px;
  border-radius: 8px;
  font: inherit;
  font-size: 14px;
}
.field input[type="search"] { min-width: 240px; }
.field input[type="number"] { width: 74px; }
.seg { display: flex; gap: 4px; }
.seg-btn {
  border: 1px solid var(--line); background: transparent; color: var(--ink-soft);
  padding: 6px 10px; border-radius: 8px; cursor: pointer; font-size: 12.5px;
}
.seg-btn.is-active { background: var(--ink); color: var(--paper-2); border-color: var(--ink); }
.checkboxes label { font-size: 13px; color: var(--ink-soft); display: flex; align-items: center; gap: 6px; }
.counter { margin-left: auto; font-size: 13px; color: var(--muted); }

/* ---------- main layout ---------- */
.main {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 20px;
  padding: 20px 26px 40px;
  align-items: start;
}
.main.no-detail { grid-template-columns: 1fr; }
.view { min-width: 0; }

/* ---------- fan ---------- */
.fan-wrap { display: flex; flex-direction: column; gap: 12px; }
.fan-tools { display: flex; gap: 8px; justify-content: center; }
.fan-tools button {
  border: 1px solid var(--line); background: var(--card); color: var(--ink-soft);
  padding: 7px 12px; border-radius: 8px; cursor: pointer; font-size: 12.5px;
}
.fan-tools button:hover { color: var(--ink); border-color: var(--accent); }
#fan { width: 100%; max-width: 1000px; height: auto; display: block; margin: 0 auto; touch-action: none; }
.touch-hint { margin-top: 2px; }
.fan-caption { font-size: 12.5px; color: var(--muted); max-width: 700px; margin: 0 auto; text-align: center; }
.legend { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; font-size: 11.5px; color: var(--muted); }
.legend .chip { display: inline-flex; align-items: center; gap: 6px; }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--card); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; font-size: 12.5px; box-shadow: var(--shadow); max-width: 320px;
}
.tooltip b { font-family: Georgia, serif; font-size: 14px; }
.tooltip .m { color: var(--muted); }

/* ---------- tree : infinite canvas ---------- */
.canvas-wrap {
  position: relative;
  height: min(78vh, 760px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-2);
  overflow: hidden;
  box-shadow: var(--shadow);
}
#treeCanvas { display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; }
#treeCanvas.dragging { cursor: grabbing; }
.canvas-help { font-size: 12.5px; color: var(--muted); margin: 10px 0 0; }

.canvas-hud {
  position: absolute; left: 12px; bottom: 12px; display: flex; gap: 6px; align-items: center;
  flex-wrap: wrap; background: var(--card); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; box-shadow: var(--shadow); max-width: 70%;
}
.canvas-hud button {
  border: 1px solid var(--line); background: var(--paper-2); color: var(--ink-soft);
  padding: 5px 9px; border-radius: 7px; cursor: pointer; font-size: 12.5px;
}
.canvas-hud button:hover { color: var(--ink); border-color: var(--accent); }
.canvas-hud .hud-zoom { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.minimap {
  position: absolute; right: 12px; bottom: 12px;
  width: 150px; height: 118px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--paper-2); cursor: crosshair; box-shadow: var(--shadow); touch-action: none;
}
.mmnode { opacity: .5; }
.mmhit { opacity: 1; }
.mmview { fill: none; stroke: var(--accent); stroke-width: 1.6; }

.tedge { fill: none; stroke: var(--line); stroke-width: 1.1; }
.tedge.hot { stroke: var(--accent); stroke-width: 2.2; }
.tdup { fill: none; stroke: var(--gold); stroke-width: 1.2; stroke-dasharray: 5 4; opacity: .7; }
.tnode { cursor: pointer; }
.tcard { fill: var(--card); stroke: var(--line); stroke-width: 1; }
.tbar { fill: var(--gen, var(--accent)); }
.tname { font-family: Georgia, serif; font-size: 16px; fill: var(--ink); }
.tmeta { font-size: 11px; fill: var(--muted); }
.tnode:hover .tcard { stroke: var(--accent); }
.tnode.dim .tcard { fill: var(--paper-2); opacity: .45; }
.tnode.dim .tname, .tnode.dim .tmeta { opacity: .5; }
.tnode.is-selected .tcard { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2; }
.tnode.orphan .tcard { stroke: var(--gold); stroke-dasharray: 4 3; }
.tnode.dup .tcard { stroke: var(--gold); }
.tdot { opacity: .8; cursor: pointer; }
.tdot.dim { opacity: .3; }
.tdot.hit { stroke: var(--accent); stroke-width: 3; opacity: 1; }
.tdot.is-selected { opacity: 1; stroke: var(--accent); stroke-width: 2; }
.tdot.orphan { stroke: var(--gold); stroke-dasharray: 4 3; }
.tdot.dup { stroke: var(--gold); }
.tdot:hover { opacity: 1; }
.genmark { cursor: pointer; }
.genbox { fill: var(--card); stroke: var(--line); }
.genlab { font-size: 10.5px; fill: var(--ink-soft); }

/* ---------- charts ---------- */
.charts { display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; }
.panel { min-width: 0; }
.panel {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 18px;
}
.panel h2 { font-size: 17px; margin-bottom: 10px; }
.panel p.note { font-size: 12.5px; color: var(--muted); margin: 0 0 12px; }
svg.chart { width: 100%; height: auto; display: block; }
svg.chart .cgl { stroke: var(--line-soft); stroke-width: 1; }
svg.chart .cax { stroke: var(--line); stroke-width: 1.2; }
svg.chart .ctx { stroke: var(--line); stroke-width: 1; }
svg.chart .ytick, svg.chart .xtick { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
svg.chart .bval { font-size: 10.5px; fill: var(--ink-soft); font-variant-numeric: tabular-nums; }
svg.chart .axtitle { font-size: 11.5px; fill: var(--ink-soft); letter-spacing: .04em; text-transform: uppercase; }
svg.chart .cbar { stroke: var(--paper); stroke-width: .5; }
svg.chart .cbar:hover { opacity: .85; }
svg.chart .xtick-long { font-size: 12px; }

.bars { display: flex; flex-direction: column; gap: 7px; }
.bars-head { display: grid; grid-template-columns: 170px 1fr 44px; gap: 10px; align-items: center;
  font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em;
  border-bottom: 1px solid var(--line-soft); padding-bottom: 5px; }
.bars-head span:last-child { text-align: right; }
.bar-row { display: grid; grid-template-columns: 170px 1fr 44px; gap: 10px; align-items: center; font-size: 13px; }
.bar-row .bname { color: var(--ink-soft); }
.bar-row .bar { height: 10px; border-radius: 5px; background: var(--line); overflow: hidden; }
.bar-row .bar i { display: block; height: 100%; background: var(--accent); }
.bar-row .val { font-size: 12px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
.bar-row:hover .bar i { background: var(--teal); }
@media (max-width: 640px) {
  .bars-head { display: none; }
  .bar-row { grid-template-columns: 1fr 42px; }
  .bar-row .bname { grid-column: 1 / -1; font-size: 12.5px; }
  .tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; min-width: 0; }
  /* the two least useful columns are dropped on small screens */
  .mini th:nth-child(3), .mini td:nth-child(3),
  .mini th:nth-child(6), .mini td:nth-child(6) { display: none; }
  .mini { font-size: 12.5px; }
  .mini th, .mini td { padding: 6px 8px; }
}
table.mini { width: 100%; border-collapse: collapse; font-size: 13px; }
table.mini th, table.mini td { border-bottom: 1px solid var(--line-soft); padding: 6px 8px; text-align: left; }
table.mini th { color: var(--muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; }

/* ---------- detail ---------- */
.detail {
  position: sticky; top: 20px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow);
}
.detail h2 { font-size: 20px; margin-bottom: 4px; }
.detail .who { color: var(--muted); font-size: 12px; margin-bottom: 12px; }
.detail dl { margin: 0; display: grid; grid-template-columns: 82px 1fr; gap: 6px 10px; font-size: 13.5px; }
.detail dt { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; }
.detail dd { margin: 0; }
.detail .note { margin-top: 12px; font-size: 13px; color: var(--ink-soft); border-top: 1px solid var(--line-soft); padding-top: 10px; }
.detail .links { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.detail .links button {
  border: 1px solid var(--line); background: var(--paper-2); color: var(--ink-soft);
  padding: 6px 10px; border-radius: 8px; cursor: pointer; font-size: 12.5px;
}
.detail .links button:hover { color: var(--ink); border-color: var(--accent); }
.breadcrumb { margin-top: 12px; font-size: 12px; color: var(--muted); line-height: 1.7; }
.breadcrumb a { color: var(--accent); text-decoration: none; }
.breadcrumb a:hover { text-decoration: underline; }
.unnumbered { margin-top: 12px; font-size: 12.5px; color: var(--muted); }
.unnumbered li { margin-bottom: 4px; }

.foot {
  padding: 14px 26px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted);
  display: flex; gap: 20px; flex-wrap: wrap;
}
.foot code { background: var(--card); padding: 2px 6px; border-radius: 5px; border: 1px solid var(--line); }

@media (max-width: 980px) {
  .main { grid-template-columns: 1fr; }
  .detail { position: static; }
  .topbar { padding: 14px 16px; }
  .controls, .main { padding-left: 16px; padding-right: 16px; }
}

/* ---------- responsive / mobile ---------- */
.tabs {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--paper-2);   /* solid: the tree/fan scrolls behind the bar */
  box-shadow: 0 6px 14px rgba(20, 25, 40, .08);
}
.detail-close {
  border: 1px solid var(--line); background: var(--paper-2); color: var(--ink-soft);
  width: 34px; height: 30px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.detail-close:hover { color: var(--ink); border-color: var(--accent); }
.detail-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.detail-head h2 { flex: 1; }
.filters-toggle { display: none; }

@media (max-width: 1024px) {
  .main { grid-template-columns: 1fr; padding: 16px 14px 30px; }
  .detail { position: static; }
  .topbar { padding: 12px 14px; gap: 12px; }
  .tabs, .controls, .foot { padding-left: 14px; padding-right: 14px; }
  .brand h1 { font-size: 22px; }
  .canvas-wrap { height: min(70vh, 620px); }
}

@media (max-width: 760px) {
  body { font-size: 14.5px; }
  * { -webkit-tap-highlight-color: transparent; }
  button, a, input, select { touch-action: manipulation; }
  .topbar { flex-direction: column; align-items: flex-start; gap: 10px; }
  .brand { gap: 10px; }
  .brand h1 { font-size: 19px; }
  .brand .sub { font-size: 11.5px; }
  .crest { width: 32px; height: 32px; }
  .quickstats { gap: 10px 14px; }
  .stat { flex-direction: row; align-items: baseline; gap: 5px; }
  .stat b { font-size: 14px; }
  .stat span { font-size: 10px; }

  .tab { padding: 9px 12px; font-size: 13px; min-height: 34px; }
  .theme-toggle button { width: 36px; height: 34px; }

  /* filters : search always available, the rest collapses behind a toggle */
  .controls { display: flex; flex-wrap: wrap; gap: 10px; }
  .controls .field { min-width: 0; gap: 8px; }
  .filters-toggle { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 6px;
    border: 1px solid var(--line); background: var(--card); color: var(--ink-soft);
    padding: 7px 12px; border-radius: 8px; cursor: pointer; font-size: 12.5px; min-height: 32px;
  }
  .filters-toggle:hover { color: var(--ink); border-color: var(--accent); }
  .controls:not(.is-open) .radios, .controls:not(.is-open) .checkboxes { display: none; }
  .field input[type="search"] { min-width: 0; flex: 1; font-size: 16px; padding: 10px; }
  .controls .field:nth-child(2) { flex: 1 1 100%; }    /* search */
  .controls .field:nth-child(3) { flex: 0 0 auto; }    /* generations */
  .controls .field:nth-child(4) { flex: 1 1 200px; }   /* side */
  .controls .field:nth-child(5) { flex: 1 1 100%; }    /* checkboxes */
  .field input[type="number"] { width: 56px; font-size: 16px; }
  .seg { flex: 1; }
  .seg-btn { flex: 1; min-height: 34px; padding: 8px 6px; font-size: 12px; }
  .field.radios > span { display: none; }
  .checkboxes { display: flex; gap: 14px; flex-wrap: wrap; }
  .checkboxes label { min-height: 32px; }
  .counter { flex: 1 1 120px; margin-left: 0; min-width: 0; text-align: right; font-size: 12px; }

  .canvas-wrap { height: min(62vh, 480px); }
  .canvas-hud { left: 8px; bottom: 8px; max-width: calc(100% - 150px); }
  .canvas-hud button { min-height: 32px; }
  .minimap { width: 122px; height: 96px; }

  .fan-tools { flex-wrap: wrap; }
  .fan-tools button { min-height: 34px; }
  .fan-caption { font-size: 12px; }

  /* the person sheet slides up from the bottom so a tap always shows its result */
  body.sheet-open { padding-bottom: 48vh; }
  /* keep the whole drawing above the open sheet */
  body.sheet-open .canvas-wrap { height: min(44vh, 380px); }
  .detail {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 60;
    max-height: 46vh; overflow-y: auto;
    border-radius: 14px 14px 0 0; border-bottom: none;
    padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
    box-shadow: 0 -14px 34px rgba(0, 0, 0, .30);
  }
  .detail dl { grid-template-columns: 88px 1fr; }
  .foot { padding: 12px 14px; }
  .tooltip { max-width: calc(100vw - 24px); }
  .res { min-height: 34px; }
  .breadcrumb { line-height: 2; }
}

@media (max-width: 460px) {
  .minimap { display: none; }
  .brand .sub { display: none; }
  .canvas-wrap { height: min(58vh, 420px); }
  .canvas-hud { max-width: calc(100% - 16px); }
  .detail { max-height: 50vh; }
  .detail dl { grid-template-columns: 76px 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* search results + orphans */
.results { margin-top: 10px; }
.res {
  display: inline-block; margin: 4px 6px 0 0; padding: 5px 9px;
  border: 1px solid var(--line); border-radius: 4px; background: var(--paper-2);
  font-size: 13px; cursor: pointer;
}
.res:hover { border-color: var(--accent); background: var(--accent-soft); }
.res b { font-family: Georgia, serif; color: var(--accent); font-size: 12px; }
.orphan { border-style: dashed; border-color: var(--gold); }

/* duplicated ancestor (same person at two places of the pedigree) */
.also {
  margin: 0 0 12px; padding: 7px 10px; font-size: 12.5px; color: var(--ink-soft);
  background: var(--paper-2); border-left: 3px solid var(--gold); border-radius: 6px;
}
.also a { color: var(--accent); }
.node .meta.dup { color: var(--gold); }
.node .meta.dup a { color: var(--accent); text-decoration: none; }
.node .meta.dup a:hover { text-decoration: underline; }
.legend .sw.dup { background: transparent; border: 2px solid var(--gold); }

@media print {
  body { background: #fff; color: #111; }
  .tabs, .fan-tools, .theme-toggle, .tooltip, .canvas-hud, .canvas-help,
  .filters-toggle, .detail-close, .touch-hint { display: none !important; }
  .main { grid-template-columns: 1fr; padding: 12px; }
  .detail { position: static; box-shadow: none; }
  .arc { stroke: #fff; }
}
