/* Family Tree — full-viewport app. No max-width column anywhere: the
   chart fills the whole window and fit-to-page scales the tree UP as
   well as down to fill it. Every overlay is constrained to the viewport
   height with internal scrolling, so controls are always reachable. */

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  font-family: ui-sans-system, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: linear-gradient(180deg, #20301f 0%, #2e4023 45%, #3a2f1d 100%);
  color: #1d1a13;
}
.hidden { display: none !important; }
button {
  font: inherit;
  border: 1px solid #8a7a55;
  background: #f7efdb;
  color: #2c2413;
  border-radius: 8px;
  padding: 5px 10px;
  cursor: pointer;
}
button:hover:not(:disabled) { background: #fff6df; }
button:disabled { opacity: 0.45; cursor: default; }
input, select, textarea {
  font: inherit;
  border: 1px solid #b6a67f;
  border-radius: 8px;
  padding: 6px 8px;
  background: #fffdf6;
  color: #1d1a13;
  width: 100%;
}
label { display: block; font-size: 0.85rem; margin: 8px 0 2px; }

/* ------------------------------------------------------------- backdrop */
/* The generated Yggdrasil artwork (public/yggdrasil.webp, 1120×2240 —
   trunk at top, roots spreading down and wide) is the PAGE backdrop:
   a layer of the viewport BEHIND #world, never inside it, so the
   chart's zoom/pan transform cannot scale or move it (the user's
   ruling: zoom affects the family tree, not the background). Pure
   CSS geometry, no script sizing: the img keeps its natural 1:2
   aspect and is constrained by BOTH max-width and max-height, so the
   WHOLE painting is always shown — never cropped — anchored to the
   top of the page (flex-start) and centred horizontally. Half-muted:
   the painting becomes texture and the opaque cards + cream branch
   lines stay legible over it. */
#backdrop {
  position: absolute;
  inset: 0;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  pointer-events: none;
}
#yggdrasil {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  opacity: 0.5;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
}

/* ------------------------------------------------------------------ app */
#app {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: 100dvh;
}
#topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(24, 33, 19, 0.88);
  color: #f3ead2;
  box-shadow: 0 2px 10px rgba(0,0,0,0.35);
}
#topbar .brand { font-family: Georgia, "Times New Roman", serif; font-size: 1.15rem; font-weight: 700; }
#topbar .rootpick { display: flex; align-items: center; gap: 6px; margin: 0; font-size: 0.9rem; }
#topbar .rootpick select { width: auto; background: #fffdf6; }
#topbar .spacer { flex: 1; }
#topbar .switch { display: flex; align-items: center; gap: 5px; margin: 0; font-size: 0.9rem; cursor: pointer; }
#topbar .switch input { width: auto; }
.zoomctl { display: flex; align-items: center; gap: 4px; }
.zoomctl #zoomPct { min-width: 3.4em; text-align: center; font-variant-numeric: tabular-nums; font-size: 0.9rem; }

/* --------------------------------------------------------------- chart */
#viewport {
  position: relative;
  flex: 1;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
}
#viewport.panning { cursor: grabbing; }
#world {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1; /* above the #backdrop painting; the ONLY transformed layer */
  transform-origin: 0 0;
  will-change: transform;
}
#lines { position: absolute; left: 0; top: 0; overflow: visible; }
#lines path { fill: none; stroke: #d8c690; stroke-width: 2.5; opacity: 0.85; }
#lines path.partner { stroke: #a8c4ea; stroke-width: 3; }
#cards { position: absolute; left: 0; top: 0; }

.card {
  position: absolute;
  border-radius: 12px;
  border: 2px solid;
  padding: 6px 9px;
  overflow: hidden;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  box-shadow: 0 3px 10px rgba(0,0,0,0.35);
  display: flex;
  gap: 7px;
  align-items: center;
}
.card.blood { background: #fdeceb; border-color: #c0453a; }
.card.marriage { background: #e2ecfd; border-color: #3a66c0; }
.card.root { outline: 3px solid #e9c767; outline-offset: 1px; }
.card.dragging { opacity: 0.85; cursor: grabbing; box-shadow: 0 10px 26px rgba(0,0,0,0.5); }
.card.drop-target { outline: 4px solid #ffd94d; outline-offset: 2px; }
.card .thumb {
  width: 38px; height: 38px; border-radius: 50%; object-fit: cover;
  border: 2px solid rgba(0,0,0,0.25); flex: none; background: #ddd;
}
.card .txt { min-width: 0; }
.card .nm {
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.15;
}
.card.g0 .nm { font-size: 17px; }
.card.g1 .nm { font-size: 15px; }
.card.g2 .nm { font-size: 13px; }
.card.g3 .nm { font-size: 12px; }
.card .rel {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.75;
  white-space: nowrap;
}
.card .dates { display: none; font-size: 11px; opacity: 0.85; white-space: nowrap; }
.card.expanded .dates { display: block; }
.card.expanded { z-index: 5; overflow: visible; }
.card.expanded .nm { white-space: normal; }

#emptyState {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: #f3ead2;
  font-size: 1.05rem;
  pointer-events: none;
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}

/* ----------------------------------------------------------------- dock */
#dock {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  background: rgba(24, 33, 19, 0.88);
  color: #f3ead2;
  overflow-x: auto;
}
#dock .dock-label { font-size: 0.85rem; white-space: nowrap; opacity: 0.9; }
#dockCards { display: flex; gap: 6px; }
.chip {
  border: 2px solid;
  border-radius: 999px;
  padding: 3px 12px;
  font-family: Georgia, serif;
  font-weight: 700;
  cursor: grab;
  white-space: nowrap;
  user-select: none;
}
.chip.blood { background: #fdeceb; border-color: #c0453a; color: #571f19; }
.chip.marriage { background: #e2ecfd; border-color: #3a66c0; color: #1d3560; }

/* --------------------------------------------------------------- editor */
#editor {
  position: fixed;
  top: 0;
  right: 0;
  height: 100dvh;
  width: min(400px, 96vw);
  overflow-y: auto;
  overscroll-behavior: contain;
  z-index: 50;
  background: #fffdf4;
  box-shadow: -6px 0 24px rgba(0,0,0,0.45);
  padding: 14px 16px 28px;
}
#editor h2 { font-family: Georgia, serif; margin: 2px 0 0; font-size: 1.35rem; }
#editor h3 { margin: 20px 0 6px; font-size: 1rem; border-bottom: 2px solid #e4d9b8; padding-bottom: 3px; }
#editor .rel-line { color: #6b5a2e; font-size: 0.9rem; margin-top: 2px; }
#editor .close-x { float: right; }
#editor .row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
#editor .danger { border-color: #c0453a; color: #8f2318; background: #fdeceb; }
.union-box {
  border: 1px solid #d8cba0;
  border-radius: 10px;
  padding: 8px 10px;
  margin: 8px 0;
  background: #fbf6e6;
}
.union-box .u-head { font-weight: 700; font-family: Georgia, serif; }
.union-box ul { margin: 6px 0; padding-left: 18px; }
.union-box li { margin: 3px 0; }
.mini { font-size: 0.8rem; padding: 2px 8px; border-radius: 999px; }
.badge {
  font-size: 0.72rem; background: #e9c767; border-radius: 999px;
  padding: 1px 7px; margin-left: 5px; vertical-align: middle;
}
.photo-grid { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; }
.photo-cell { width: 108px; }
.photo-cell img { width: 108px; height: 108px; object-fit: cover; border-radius: 10px; border: 1px solid #b6a67f; display: block; }
.photo-cell input { margin-top: 4px; font-size: 0.78rem; padding: 3px 5px; }
.photo-cell .mini { margin-top: 3px; }

/* -------------------------------------------------------------- chooser */
#chooser {
  position: fixed;
  z-index: 70;
  min-width: min(300px, 94vw);
  max-width: min(430px, 94vw);
  max-height: calc(100dvh - 16px);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: #fffdf4;
  border: 1px solid #b6a67f;
  border-radius: 12px;
  box-shadow: 0 14px 40px rgba(0,0,0,0.5);
  padding: 6px;
}
#chooser .ch-head {
  position: sticky;
  top: -6px;
  background: #fffdf4;
  font-family: Georgia, serif;
  font-weight: 700;
  padding: 8px 8px 6px;
  border-bottom: 2px solid #e4d9b8;
  z-index: 1;
}
#chooser .ch-opt {
  display: block;
  width: 100%;
  text-align: left;
  margin: 4px 0;
  padding: 9px 10px;
  background: #f7efdb;
}
#chooser .ch-opt .hint { display: block; font-size: 0.78rem; opacity: 0.75; margin-top: 1px; }
#chooser .ch-cancel {
  position: sticky;
  bottom: -6px;
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 10px;
  background: #efe6cf;
  font-weight: 700;
}

/* --------------------------------------------------------------- modals */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(10, 14, 8, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}
.modal-card {
  background: #fffdf4;
  border-radius: 14px;
  padding: 18px 20px 20px;
  width: min(430px, 94vw);
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  overscroll-behavior: contain;
  box-shadow: 0 18px 50px rgba(0,0,0,0.55);
}
.modal-card h3 { margin: 0 0 8px; font-family: Georgia, serif; }
.modal-card .row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.form-error { color: #a02c1e; min-height: 1.1em; margin: 6px 0 0; font-size: 0.9rem; }
.login-tree { font-size: 44px; text-align: center; }
#login .modal-card h3 { text-align: center; }
.login-sub { text-align: center; margin: 0 0 6px; opacity: 0.8; }
#pickerList { max-height: 46dvh; overflow-y: auto; margin-top: 8px; }
#pickerList button { display: block; width: 100%; text-align: left; margin: 4px 0; padding: 8px 10px; }
#pickerCreate {
  display: block; width: 100%; margin-top: 10px; padding: 9px 10px;
  font-weight: 700; text-align: left;
  background: #f3ead2; border: 2px dashed #7a5f36; border-radius: 8px; cursor: pointer;
}
#pickerCreate:hover { background: #eee0bc; }
.cancel-btn { background: #efe6cf; }

/* ---------------------------------------------------------------- toast */
#toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 90;
  max-width: 92vw;
  background: #2c2413;
  color: #f7efdb;
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  font-size: 0.95rem;
}

@media (max-width: 640px) {
  #topbar { gap: 6px; padding: 6px 8px; }
  #topbar .brand span { display: none; }
}
