*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f0f0f0;
}

/* ─── Layout ────────────────────────────────────────────────────────────────── */

#container {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.5rem 1.25rem;
  background: #fff;
  border-bottom: 1px solid #ddd;
  flex-shrink: 0;
}

h1 {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #555;
}

.topbar-actions {
  display: flex;
  gap: 0.5rem;
}

#export-png-btn,
#start-over-btn,
#share-btn {
  font-size: 0.85rem;
  font-weight: 500;
  padding: 0.4rem 0.85rem;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  font-family: inherit;
  min-height: 34px;
}

#share-btn {
  border: 1.5px solid #7c3aed;
  color: #6d28d9;
}

#share-btn:hover {
  background: #7c3aed;
  color: #fff;
}

#share-btn:focus-visible {
  outline: 2px solid #7c3aed;
  outline-offset: 2px;
}

#export-png-btn {
  border: 1.5px solid #2563eb;
  color: #1d4ed8;
}

#export-png-btn:hover {
  background: #2563eb;
  color: #fff;
}

#export-png-btn:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

#export-png-btn[disabled] {
  opacity: 0.6;
  cursor: progress;
}

#start-over-btn {
  border: 1.5px solid #bbb;
  color: #555;
}

#start-over-btn:hover {
  background: #f3f4f6;
  color: #b91c1c;
  border-color: #b91c1c;
}

#start-over-btn:focus-visible {
  outline: 2px solid #b91c1c;
  outline-offset: 2px;
}

#canvas {
  flex: 1;
  overflow: hidden;
  position: relative;
  cursor: grab;
}

#canvas:active {
  cursor: grabbing;
}

#genogram-svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* ─── Zoom controls ──────────────────────────────────────────────────────────── */

#zoom-controls {
  position: absolute;
  bottom: 0.75rem;
  right: 0.75rem;
  display: flex;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  overflow: hidden;
  z-index: 10;
}

#zoom-controls button {
  background: #fff;
  border: none;
  border-right: 1px solid #eee;
  padding: 0.4rem 0.7rem;
  font-size: 0.95rem;
  color: #333;
  cursor: pointer;
  min-width: 36px;
  min-height: 34px;
  font-family: inherit;
  line-height: 1;
}

#zoom-controls button:last-child {
  border-right: none;
}

#zoom-controls button:hover {
  background: #f3f4f6;
  color: #1d4ed8;
}

#zoom-controls button:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: -2px;
}

#zoom-label {
  font-variant-numeric: tabular-nums;
  font-size: 0.8rem;
  font-weight: 500;
  color: #555;
}

@media (max-width: 640px) {
  #zoom-controls button { min-height: 40px; min-width: 40px; }
}

/* ─── Nodes ─────────────────────────────────────────────────────────────────── */

.node {
  cursor: pointer;
}

.node rect,
.node circle,
.node polygon {
  fill: #fff;
  stroke: #222;
  stroke-width: 3;
}

/* Neutral/unknown diamond is slightly muted */
.node polygon.neutral {
  stroke: #888;
  stroke-dasharray: 6 3;
}

.node:hover rect,
.node:hover circle,
.node:hover polygon {
  fill: #dbeafe;
  filter: drop-shadow(0 0 8px rgba(37,99,235,0.5));
}

/* Inner shapes for trans symbols. Excludes .primary-outline so a primary
 * "me" outer shape doesn't accidentally trigger the inner-shape style on
 * the gender shape that follows it. */
.node circle:not(.primary-outline) + circle,
.node rect:not(.primary-outline) + circle,
.node circle:not(.primary-outline) + rect {
  fill: none;
  stroke: #222;
  stroke-width: 2;
}

.node.selected rect,
.node.selected circle,
.node.selected polygon {
  stroke: #2563eb;
  filter: drop-shadow(0 0 6px rgba(37,99,235,0.4));
}

/* Shift multi-select */
.node.multi-selected rect,
.node.multi-selected circle,
.node.multi-selected polygon {
  stroke: #7c3aed;
  filter: drop-shadow(0 0 6px rgba(124,58,237,0.4));
}

/* Connect mode */
.node.connecting-source rect,
.node.connecting-source circle,
.node.connecting-source polygon {
  stroke: #16a34a;
  filter: drop-shadow(0 0 6px rgba(22,163,74,0.4));
}

.node.connecting-target rect,
.node.connecting-target circle,
.node.connecting-target polygon {
  stroke: #888;
  stroke-dasharray: 5 3;
}

.node.connecting-target:hover rect,
.node.connecting-target:hover circle,
.node.connecting-target:hover polygon {
  stroke: #16a34a;
  stroke-dasharray: none;
  filter: drop-shadow(0 0 5px rgba(22,163,74,0.3));
}

.node-label {
  font-size: 12px;
  font-weight: 600;
  fill: #222;
  pointer-events: none;
}

.node-attr {
  font-size: 10px;
  fill: #666;
  pointer-events: none;
}

.deceased-mark {
  stroke: #222;
  stroke-width: 2.5;
  pointer-events: none;
}

/* ─── Edges ─────────────────────────────────────────────────────────────────── */

.edge-line {
  stroke: #888;
  stroke-width: 2.5;
  fill: none;
}

/* Unknown/undefined relationship: thick gray */
.edge-unknown {
  stroke: #aaa;
  stroke-width: 5;
  stroke-linecap: round;
}

.edge-married,
.edge-parent {
  stroke: #222;
  stroke-width: 2.5;
}

.edge-common-law {
  stroke: #222;
  stroke-width: 2.5;
  stroke-dasharray: 8 5;
}

.edge-separated,
.edge-divorced {
  stroke: #222;
  stroke-width: 2.5;
}

.edge-slash {
  stroke: #222;
  stroke-width: 2.5;
}

.edge-zigzag {
  stroke: #c0392b;
  stroke-width: 2.5;
}

.edge-abusive-overlay {
  stroke: #c0392b;
  stroke-width: 2.5;
}

.edge-label {
  font-size: 11px;
  fill: #555;
  pointer-events: none;
}

/* Highlight on hover */
.edge:hover .edge-line {
  filter: drop-shadow(0 0 5px rgba(0,0,0,0.45));
  stroke-width: 4;
}

/* ─── Drawer ─────────────────────────────────────────────────────────────────── */

#drawer {
  flex-shrink: 0;
  background: #fff;
  border-top: 2px solid #ddd;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.25s ease;
}

#drawer.open {
  max-height: 200px;
  overflow-y: auto;
}

#drawer-content {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.75rem 1.25rem 0.9rem;
  position: relative;
}

.drawer-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: none;
  border: 1px solid transparent;
  font-size: 1rem;
  color: #555;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.drawer-close:hover {
  background: #f0f0f0;
  color: #111;
}

.drawer-close:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
  border-color: transparent;
}

.drawer-section {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 0.6rem 1.1rem;
  align-items: flex-end;
}

.drawer-field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.drawer-field label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #555;
}

.drawer-field input,
.drawer-field select,
.drawer-field textarea {
  font-size: 0.9rem;
  padding: 0.35rem 0.55rem;
  border: 1px solid #bbb;
  border-radius: 5px;
  background: #fff;
  color: #111;
  min-width: 120px;
  font-family: inherit;
  min-height: 36px;
}

.drawer-field textarea {
  resize: vertical;
  min-width: 220px;
  line-height: 1.4;
}

.drawer-field input:focus-visible,
.drawer-field select:focus-visible,
.drawer-field textarea:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 1px;
  border-color: #2563eb;
}

/* ─── Action toolbar ─────────────────────────────────────────────────────────── */

.drawer-actions {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding-right: 2.75rem; /* leave room for close button */
  align-items: center;
}

.drawer-actions-divider {
  width: 1px;
  height: 22px;
  background: #ddd;
  margin: 0 0.25rem;
}

.add-btn {
  font-size: 0.85rem;
  font-weight: 500;
  padding: 0.45rem 0.85rem;
  border: 1.5px solid #2563eb;
  border-radius: 6px;
  background: #fff;
  color: #1d4ed8;
  cursor: pointer;
  white-space: nowrap;
  min-height: 36px;
  font-family: inherit;
}

.add-btn:hover {
  background: #2563eb;
  color: #fff;
}

.add-btn:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

.add-btn[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

.connect-btn {
  border-color: #15803d;
  color: #15803d;
}

.connect-btn:hover {
  background: #15803d;
  color: #fff;
}

.connect-btn:focus-visible {
  outline-color: #15803d;
}

.me-btn {
  border-color: #b45309;
  color: #b45309;
}

.me-btn:hover {
  background: #b45309;
  color: #fff;
}

.me-btn:focus-visible {
  outline-color: #b45309;
}

.me-btn.is-me {
  background: #fef3c7;
  border-color: #b45309;
  color: #92400e;
}

.me-btn.is-me:hover {
  background: #fde68a;
  color: #78350f;
}

.delete-btn {
  border-color: #b91c1c;
  color: #b91c1c;
  margin-left: auto;
}

.delete-btn:hover {
  background: #b91c1c;
  color: #fff;
}

.delete-btn:focus-visible {
  outline-color: #b91c1c;
}

.connecting-hint {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding-right: 2.75rem;
}

.connecting-hint p {
  margin: 0;
  font-size: 0.9rem;
  color: #15803d;
  font-weight: 500;
}

.multi-rel-select {
  font-size: 0.85rem;
  padding: 0.35rem 0.55rem;
  border: 1.5px solid #7c3aed;
  border-radius: 6px;
  background: #fff;
  color: #5b21b6;
  font-family: inherit;
  min-height: 34px;
  cursor: pointer;
}

/* ─── Mobile ─────────────────────────────────────────────────────────────────── */

@media (max-width: 640px) {
  #container {
    height: 100svh;
    height: 100dvh;
  }

  #topbar {
    padding: 0.4rem 0.75rem;
    gap: 0.5rem;
  }

  h1 {
    font-size: 0.7rem;
  }

  #export-png-btn,
  #start-over-btn {
    min-height: 44px;
    padding: 0.35rem 0.6rem;
    font-size: 0.8rem;
  }

  #drawer.open {
    max-height: 50vh;
    max-height: 50dvh;
  }

  #drawer-content {
    padding: 0.5rem 0.75rem 0.75rem;
    gap: 0.45rem;
  }

  .drawer-close {
    width: 44px;
    height: 44px;
    top: 0.2rem;
    right: 0.2rem;
  }

  .drawer-section {
    flex-direction: column;
    gap: 0.45rem;
  }

  .drawer-field {
    width: 100%;
  }

  /* 1rem = 16px: prevents iOS from zooming on input focus */
  .drawer-field input,
  .drawer-field select,
  .drawer-field textarea {
    min-height: 44px;
    min-width: unset;
    width: 100%;
    font-size: 1rem;
  }

  .drawer-field textarea {
    min-width: unset;
    min-height: 60px;
  }

  .drawer-actions {
    padding-right: 3rem;
    gap: 0.35rem;
    row-gap: 0.35rem;
  }

  .add-btn {
    min-height: 44px;
    font-size: 0.8rem;
    padding: 0.4rem 0.65rem;
  }

  .connecting-hint {
    flex-wrap: wrap;
    padding-right: 3rem;
    gap: 0.5rem;
  }

  .multi-rel-select {
    min-height: 44px;
    font-size: 1rem;
  }
}

/* ─── Share modal ────────────────────────────────────────────────────────────── */

.modal[hidden] {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
}

.modal-content {
  position: relative;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
  padding: 1.25rem 1.5rem 1.5rem;
  width: min(560px, 100%);
  max-height: calc(100dvh - 2rem);
  overflow-y: auto;
}

.modal-content h2 {
  margin: 0 0 0.75rem;
  font-size: 1.05rem;
  padding-right: 2.5rem;
}

.modal-section {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid #eee;
}

.modal-section:first-of-type {
  border-top: none;
  padding-top: 0;
  margin-top: 0;
}

.modal-section h3 {
  margin: 0 0 0.35rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #555;
}

.modal-section p {
  margin: 0 0 0.55rem;
  font-size: 0.9rem;
  color: #444;
  line-height: 1.4;
}

.modal-section textarea {
  width: 100%;
  min-height: 110px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.8rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid #bbb;
  border-radius: 6px;
  background: #fafafa;
  color: #111;
  resize: vertical;
  line-height: 1.4;
}

.modal-section textarea:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 1px;
  border-color: #2563eb;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.55rem;
}

.modal-status {
  font-size: 0.85rem;
  color: #555;
}

.modal-status.success {
  color: #15803d;
  font-weight: 500;
}

.modal-status.error {
  color: #b91c1c;
}

.modal-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
}

@media (max-width: 640px) {
  #share-btn {
    min-height: 44px;
    padding: 0.35rem 0.6rem;
    font-size: 0.8rem;
  }

  .modal-content {
    padding: 1rem 1rem 1.25rem;
    max-height: calc(100dvh - 1rem);
  }

  .modal-section textarea {
    font-size: 0.85rem;
    min-height: 90px;
  }

  .modal-actions .add-btn {
    min-height: 44px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #drawer { transition: none; }
  .add-btn, .drawer-close { transition: none; }
}
