:root {
  --paper: #FAFAF8;
  --ink: #1D1D1F;
  --ink-soft: #6E6E73;
  --pine: #2C5F4F;
  --pine-soft: #E4ECE8;
  --amber: #C4622D;
  --amber-soft: #F5E4D9;
  --line: #E8E6DE;
  --line-strong: #D8D5CB;
  --white: #FFFFFF;

  --font-display: 'Fraunces', Georgia, serif;
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-soft: 0 1px 2px rgba(29,29,31,0.04), 0 8px 24px rgba(29,29,31,0.06);
  --shadow-float: 0 4px 12px rgba(29,29,31,0.08), 0 16px 40px rgba(29,29,31,0.12);

  --topbar-h: 64px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  height: 100%;
  overflow: hidden;
}

h1, h2, h3 { font-family: var(--font-display); font-weight: 600; margin: 0; letter-spacing: -0.01em; }
p { margin: 0; }
button { font-family: inherit; }

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ===== TOPBAR ===== */
.topbar {
  height: var(--topbar-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  border-bottom: 1px solid var(--line);
  background: var(--white);
  z-index: 30;
}
.topbar__brand { display: flex; align-items: center; gap: 10px; }
.topbar__mark { color: var(--pine); width: 26px; height: 26px; }
.topbar__name { font-family: var(--font-display); font-size: 19px; font-weight: 600; }
.topbar__actions { display: flex; align-items: center; gap: 10px; }

/* ===== BUTTONS ===== */
.btn { font-size: 14px; font-weight: 600; border-radius: 100px; border: none; cursor: pointer; transition: transform 0.1s, opacity 0.15s, background 0.15s, border-color 0.15s; white-space: nowrap; display: inline-flex; align-items: center; text-decoration: none; }
.btn--primary { background: var(--ink); color: var(--white); padding: 10px 18px; }
.btn--primary:hover:not(:disabled) { opacity: 0.88; }
.btn--primary:active:not(:disabled) { transform: scale(0.97); }
.btn--primary:disabled { background: var(--line-strong); color: var(--ink-soft); cursor: default; }
.btn--secondary { background: var(--white); color: var(--ink); border: 1.5px solid var(--line-strong); padding: 10px 18px; }
.btn--secondary:hover { border-color: var(--pine); background: var(--pine-soft); }
.btn--ghost { background: none; color: var(--ink-soft); padding: 9px 14px; }
.btn--ghost:hover { background: var(--paper); color: var(--ink); }
.btn--large { padding: 13px 24px; font-size: 15px; }
.btn--sm { padding: 8px 16px; font-size: 13.5px; }
.btn--text { background: none; color: var(--ink-soft); padding: 8px 10px; }
.btn--text:hover { color: var(--ink); }
.btn:focus-visible { outline: 2px solid var(--pine); outline-offset: 2px; }

/* ===== CANVAS ===== */
.canvas-area {
  position: relative;
  flex: 1;
  overflow: hidden;
  background:
    linear-gradient(var(--line) 1px, transparent 1px) 0 0 / 100% 32px,
    var(--paper);
}

.chart-wrap {
  position: absolute;
  inset: 0;
  overflow: auto;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 60px 40px 100px;
}
#pedigreeSvg { width: 100%; max-width: 1100px; height: auto; -webkit-tap-highlight-color: transparent; }

/* ===== EMPTY STATE ===== */
.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
}
.empty-state.is-hidden { display: none; }
.empty-state__inner { max-width: 440px; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 16px; }
.empty-state__icon { width: 56px; height: 56px; color: var(--pine); margin-bottom: 4px; }
.empty-state h1 { font-size: 27px; line-height: 1.25; }
.empty-state p { font-size: 14.5px; color: var(--ink-soft); line-height: 1.55; }
.empty-state__actions { display: flex; gap: 10px; margin-top: 10px; flex-wrap: wrap; justify-content: center; }

/* ===== HINT TOAST ===== */
.hint-toast {
  position: absolute;
  top: 18px;
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  background: var(--ink);
  color: var(--white);
  font-size: 13px;
  font-weight: 500;
  padding: 9px 16px;
  border-radius: 100px;
  box-shadow: var(--shadow-float);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s, transform 0.3s;
  z-index: 20;
  white-space: nowrap;
}
.hint-toast.is-visible { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== LEGEND ===== */
.legend {
  position: absolute;
  bottom: 18px;
  left: 18px;
  display: none;
  align-items: center;
  gap: 14px;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 100px;
  padding: 8px 16px;
  box-shadow: var(--shadow-soft);
  z-index: 10;
}
.legend__item { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-soft); font-weight: 500; white-space: nowrap; }
.legend__shape { width: 11px; height: 11px; border: 2px solid var(--ink); flex-shrink: 0; }
.legend__shape.male { border-radius: 2px; }
.legend__shape.affected { background: var(--amber); border-color: var(--amber); }

/* ===== SVG PEDIGREE ELEMENTS ===== */
.ped-background { fill: transparent; }
.ped-shape { fill: var(--white); stroke: var(--ink); stroke-width: 2; cursor: pointer; transition: stroke 0.15s, fill 0.15s; }
.ped-shape.affected { fill: var(--amber); stroke: var(--ink); }
.ped-shape:hover { stroke: var(--pine); }
.ped-shape.is-selected { stroke: var(--pine); stroke-width: 3; }
.ped-hit-area { fill: transparent; cursor: pointer; }
.ped-line { stroke: var(--ink); stroke-width: 1.6; fill: none; }
.ped-label {
  font-family: var(--font-body); font-size: 12.5px; fill: var(--ink);
  text-anchor: middle; font-weight: 500; cursor: text;
  text-decoration: none; -webkit-tap-highlight-color: transparent;
}
.ped-label:hover { fill: var(--pine); }

/* plain plus button: shown only for the selected person, only when they
   have no partner yet. Quiet outline at rest, fills in with the system's
   pine accent on hover — no color-coding or metaphor needed since there's
   exactly one possible meaning in this context. */
.ped-plus-btn { cursor: pointer; }
.ped-plus-circle {
  fill: var(--white); stroke: var(--line-strong); stroke-width: 1.4;
  transition: fill 0.15s, stroke 0.15s;
}
.ped-plus-icon { stroke: var(--ink-soft); stroke-width: 1.6; stroke-linecap: round; transition: stroke 0.15s; }
.ped-plus-btn:hover .ped-plus-circle { fill: var(--pine); stroke: var(--pine); }
.ped-plus-btn:hover .ped-plus-icon { stroke: var(--white); }

/* remove button: tiny x, quiet until hovered */
.ped-remove-btn { cursor: pointer; opacity: 0.45; transition: opacity 0.15s; }
.ped-remove-btn:hover { opacity: 1; }
.ped-remove-circle { fill: var(--white); stroke: var(--line-strong); stroke-width: 1.2; }
.ped-remove-icon { stroke: var(--ink-soft); stroke-width: 1.4; stroke-linecap: round; }
.ped-remove-btn:hover .ped-remove-circle { fill: var(--amber-soft); stroke: var(--amber); }
.ped-remove-btn:hover .ped-remove-icon { stroke: var(--amber); }

/* line-hover add-children control. On devices with real hover (mouse,
   trackpad), invisible at rest and fades in on hover — keeps the chart
   calm until the line is investigated. On touch devices :hover never
   fires from a tap, so hiding it entirely would make "add children"
   undiscoverable and unreachable; touch devices instead get a
   permanently faint version that's always there to tap, brightening
   only on the tap itself via :active. The hit-zone is larger than the
   visible circle so the control responds near the midpoint, not just
   the exact pixel of the line. */
.ped-line-add { cursor: pointer; }
.ped-line-add-hitzone { fill: transparent; }
.ped-line-add-circle {
  fill: var(--white); stroke: var(--line-strong); stroke-width: 1.4;
  opacity: 0; transition: opacity 0.15s, fill 0.15s, stroke 0.15s;
}
.ped-line-add-icon {
  stroke: var(--ink-soft); stroke-width: 1.6; stroke-linecap: round;
  opacity: 0; transition: opacity 0.15s, stroke 0.15s; pointer-events: none;
}
@media (hover: hover) and (pointer: fine) {
  .ped-line-add:hover .ped-line-add-circle,
  .ped-line-add:focus-visible .ped-line-add-circle { opacity: 1; }
  .ped-line-add:hover .ped-line-add-icon,
  .ped-line-add:focus-visible .ped-line-add-icon { opacity: 1; }
  .ped-line-add:hover .ped-line-add-circle { fill: var(--pine); stroke: var(--pine); }
  .ped-line-add:hover .ped-line-add-icon { stroke: var(--white); }
}
@media (hover: none), (pointer: coarse) {
  .ped-line-add-circle { opacity: 1; fill: var(--paper); }
  .ped-line-add-icon { opacity: 1; }
  .ped-line-add:active .ped-line-add-circle { fill: var(--pine); stroke: var(--pine); }
  .ped-line-add:active .ped-line-add-icon { stroke: var(--white); }
}

/* ===== ADD CHILDREN POPUP ===== */
.children-popup {
  position: fixed;
  z-index: 40;
  background: var(--white);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-float);
  border: 1px solid var(--line);
  padding: 16px;
  width: 220px;
  display: none;
}
.children-popup.is-open { display: block; }
.children-popup__arrow {
  position: absolute;
  width: 12px; height: 12px;
  background: var(--white);
  border-left: 1px solid var(--line);
  border-top: 1px solid var(--line);
  transform: rotate(45deg);
  top: -7px;
  left: 24px;
}
.children-popup__title { font-size: 12.5px; font-weight: 600; color: var(--ink-soft); margin-bottom: 12px; }

.count-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; gap: 10px; }
.count-row label { font-size: 13.5px; font-weight: 500; color: var(--ink); }
.count-row input[type="number"] {
  width: 64px;
  font-family: var(--font-body);
  font-size: 14.5px;
  text-align: center;
  padding: 7px 8px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink);
}
.count-row input[type="number"]:focus { outline: none; border-color: var(--pine); box-shadow: 0 0 0 3px var(--pine-soft); }

.count-readout {
  font-size: 12.5px;
  color: var(--ink-soft);
  background: var(--paper);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  margin-bottom: 12px;
  text-align: center;
  font-weight: 500;
}

.popup-actions { display: flex; justify-content: flex-end; gap: 6px; }

/* ===== INLINE RENAME INPUT ===== */
.inline-name-input {
  position: fixed;
  z-index: 45;
  font-family: var(--font-body);
  font-size: 12.5px;
  font-weight: 500;
  text-align: center;
  padding: 4px 8px;
  border: 1.5px solid var(--pine);
  border-radius: 6px;
  background: var(--white);
  color: var(--ink);
  width: 130px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}
.inline-name-input:focus { outline: none; box-shadow: 0 0 0 3px var(--pine-soft); }

/* ===== RESULTS SLIDE-IN PANEL ===== */
.results-panel { position: fixed; inset: 0; z-index: 50; display: none; }
.results-panel.is-open { display: block; }
.results-panel__backdrop {
  position: absolute; inset: 0; background: rgba(29,29,31,0.25);
  opacity: 0; transition: opacity 0.25s;
}
.results-panel.is-open .results-panel__backdrop { opacity: 1; }

.results-panel__card {
  position: absolute; top: 0; right: 0; bottom: 0;
  width: min(440px, 92vw);
  background: var(--white);
  box-shadow: -8px 0 40px rgba(0,0,0,0.18);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}
.results-panel.is-open .results-panel__card { transform: translateX(0); }

.results-panel__head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 22px 24px 16px; border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.results-panel__head h2 { font-size: 20px; }
.results-panel__close { background: none; border: none; color: var(--ink-soft); cursor: pointer; padding: 6px; border-radius: 100px; }
.results-panel__close:hover { background: var(--paper); color: var(--ink); }

.results-panel__assumption {
  flex-shrink: 0;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}
.results-panel__assumption-note {
  font-size: 11.5px;
  color: var(--ink-soft);
  line-height: 1.5;
  margin-top: 6px;
}

.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 13px; font-weight: 500; color: var(--ink); cursor: pointer; }
.switch {
  width: 38px; height: 22px; border-radius: 100px; background: var(--line-strong);
  border: none; position: relative; cursor: pointer; flex-shrink: 0; padding: 0;
  transition: background 0.15s;
}
.switch[aria-pressed="true"] { background: var(--pine); }
.switch__knob {
  position: absolute; top: 2px; left: 2px; width: 18px; height: 18px;
  background: var(--white); border-radius: 50%; transition: transform 0.15s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
}
.switch[aria-pressed="true"] .switch__knob { transform: translateX(16px); }

.results-panel__body { flex: 1; overflow-y: auto; padding: 20px 24px 32px; display: flex; flex-direction: column; gap: 12px; }

.results__intro { font-size: 13.5px; color: var(--ink-soft); line-height: 1.55; }

.summary-banner { border-radius: var(--radius-md); padding: 16px 18px; background: var(--pine-soft); border: 1px solid var(--pine); font-size: 13.5px; line-height: 1.55; }
.summary-banner strong { color: var(--pine); }
.summary-banner.warn { background: var(--amber-soft); border-color: var(--amber); }
.summary-banner.warn strong { color: var(--amber); }

.pattern-card { border: 1px solid var(--line); border-radius: var(--radius-md); overflow: hidden; background: var(--white); }
.pattern-card.is-consistent { border-color: var(--pine); }
.pattern-card__head { display: flex; align-items: center; justify-content: space-between; padding: 13px 14px; cursor: pointer; user-select: none; }
.pattern-card.is-consistent .pattern-card__head { background: var(--pine-soft); }
.pattern-card.is-ruled-out .pattern-card__head { background: var(--paper); }
.pattern-card__title-group { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.pattern-card__name { font-size: 13.5px; font-weight: 600; }
.pattern-card__badge { font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; padding: 3px 8px; border-radius: 100px; }
.pattern-card.is-consistent .pattern-card__badge { background: var(--pine); color: var(--white); }
.pattern-card.is-ruled-out .pattern-card__badge { background: var(--line-strong); color: var(--ink-soft); }
.pattern-card__chevron { color: var(--ink-soft); transition: transform 0.2s; flex-shrink: 0; }
.pattern-card.is-open .pattern-card__chevron { transform: rotate(180deg); }
.pattern-card__body { padding: 0 14px; max-height: 0; overflow: hidden; transition: max-height 0.25s ease, padding 0.25s ease; }
.pattern-card.is-open .pattern-card__body { padding: 4px 14px 14px; max-height: 1000px; }

.violation-item { font-size: 12.5px; line-height: 1.55; color: var(--ink); padding: 9px 11px; background: var(--amber-soft); border-radius: var(--radius-sm); margin-bottom: 7px; border-left: 3px solid var(--amber); }
.consistent-note { font-size: 12.5px; line-height: 1.55; color: var(--ink-soft); }

.genotype-table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 12px; }
.genotype-table th { text-align: left; font-weight: 600; color: var(--ink-soft); text-transform: uppercase; font-size: 10px; letter-spacing: 0.04em; padding: 6px 7px; border-bottom: 1px solid var(--line); }
.genotype-table td { padding: 7px; border-bottom: 1px solid var(--line); }
.genotype-table td.mono { font-family: var(--font-mono); font-size: 12px; }
.certainty-tag { font-size: 9.5px; font-weight: 600; padding: 2px 7px; border-radius: 100px; text-transform: uppercase; letter-spacing: 0.02em; }
.certainty-tag.certain { background: var(--pine-soft); color: var(--pine); }
.certainty-tag.likely { background: #FFF3D6; color: #8A6116; }
.certainty-tag.possible { background: var(--line); color: var(--ink-soft); }

/* ===== DISCLAIMER (collapsed footer strip) ===== */
.disclaimer {
  flex-shrink: 0;
  padding: 9px 24px;
  border-top: 1px solid var(--line);
  background: var(--white);
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--ink-soft);
  text-align: center;
}
.disclaimer strong { color: var(--ink); }

/* ===== ABOUT MODAL ===== */
.modal { position: fixed; inset: 0; z-index: 60; display: none; }
.modal.is-open { display: block; }
.modal__backdrop { position: absolute; inset: 0; background: rgba(29,29,31,0.4); backdrop-filter: blur(4px); }
.modal__card { position: relative; max-width: 520px; margin: 8vh auto 0; background: var(--white); border-radius: var(--radius-lg); padding: 30px 32px; box-shadow: 0 20px 60px rgba(0,0,0,0.25); max-height: 80vh; overflow-y: auto; }
.modal__card h3 { font-size: 20px; margin-bottom: 14px; }
.modal__card p { font-size: 14px; line-height: 1.6; color: var(--ink); margin-bottom: 12px; }
.modal__card ul { margin: 0 0 14px; padding-left: 20px; display: flex; flex-direction: column; gap: 8px; }
.modal__card li { font-size: 13.5px; line-height: 1.55; color: var(--ink); }
.modal__card .btn { margin-top: 6px; }

/* ===== RESPONSIVE ===== */
@media (max-width: 640px) {
  .topbar { padding: 0 12px; gap: 4px; }
  .topbar__name { display: none; } /* logo mark alone is enough at this width; frees room for action buttons */
  .topbar__actions { gap: 4px; }
  /* "How this works" and "Start over" were previously hidden entirely on
     mobile, which silently removed the methodology explanation and the
     reset action with no alternative. Shrinking padding/font keeps both
     fully readable as text rather than switching to icon-only — icons
     without labels reopened exactly the guessing problem already worked
     through for the chart's own controls. */
  .topbar__actions .btn--ghost { padding: 8px 9px; font-size: 12.5px; }
  .topbar__actions .btn--primary { padding: 8px 14px; font-size: 13px; }

  .empty-state__inner { padding: 0 20px; }
  .empty-state h1 { font-size: 23px; }
  .empty-state__actions { width: 100%; flex-direction: column; }
  .empty-state__actions .btn { width: 100%; }

  .results-panel__card { width: 100vw; }

  .legend { left: 12px; bottom: 12px; padding: 6px 12px; gap: 10px; }
  .legend__item { font-size: 11px; }

  /* Less horizontal padding so narrow viewports keep more usable chart
     width; the chart-wrap already scrolls (overflow: auto) once a tree
     grows past the viewport, same as a map or a wide table would. */
  .chart-wrap { padding: 50px 14px 90px; }

  /* Never let the chart shrink below a width where touch targets (the
     remove x, the line-hover +) become too small to tap reliably — once
     the viewport is narrower than this, horizontal scroll takes over
     instead of squeezing everything down to fit. */
  #pedigreeSvg { width: auto; min-width: 480px; max-width: none; }

  .hint-toast { font-size: 12px; padding: 8px 14px; max-width: 88vw; white-space: normal; text-align: center; }

  .popup-actions .btn { min-height: 38px; }
  .count-row input[type="number"] { min-height: 38px; }
}

/* Respect notches / home-indicator areas on phones that report safe-area
   insets, so the topbar and bottom-anchored legend/disclaimer never sit
   under the notch or get clipped by the home-indicator gesture area. */
@supports (padding: max(0px)) {
  .topbar { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
  .legend { left: max(18px, env(safe-area-inset-left)); }
  .disclaimer { padding-bottom: max(9px, env(safe-area-inset-bottom)); }
}

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