:root {
  /* Fresh teal-green + blue palette. Always light, even when the phone is in dark mode. */
  --bg: #f3f9f8;
  --surface: #ffffff;
  --surface-2: #e8f5f2;
  --text: #12302c;
  --muted: #5a716d;
  --line: #c9e2dc;
  --brand: #0f766e;
  --brand-2: #2563eb;
  --brand-ink: #ffffff;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgb(15 60 55 / .06), 0 4px 12px rgb(15 60 55 / .07);
  --g0: #0f766e; --g1: #2563eb; --g2: #16a34a; --g3: #0891b2; --g4: #64748b;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light only;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px 10px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.brand > div { min-width: 0; }
.brand img { border-radius: 10px; flex: none; }
.brand h1 { margin: 0; font-size: 17px; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand p { margin: 0; font-size: 13px; color: var(--muted); }
.actions { display: flex; gap: 2px; flex: none; }
.icon {
  width: 40px; height: 44px; display: grid; place-items: center;
  border: 0; background: transparent; border-radius: 12px;
}
.icon:active { background: var(--surface-2); }
body.editing #btnEdit { color: var(--brand); background: var(--surface-2); }

.editbar {
  position: sticky; top: calc(env(safe-area-inset-top, 0px) + 65px); z-index: 9;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 12px 8px 16px; background: linear-gradient(90deg, var(--brand), var(--brand-2)); color: var(--brand-ink); font-size: 14px;
}
.editbar[hidden] { display: none; }
.editbar button { border: 0; background: var(--brand-ink); color: var(--brand); font-weight: 600; padding: 6px 14px; border-radius: 999px; }

/* ---------- tree ---------- */
.tree-wrap { padding: 12px 12px calc(32px + var(--safe-b)); max-width: 760px; margin: 0 auto; }
.tree, .kids { list-style: none; margin: 0; padding: 0; }
.kids { margin-left: 29px; }
.node { position: relative; padding-top: 6px; }
.kids > .node { padding-left: 14px; }
.kids > .node::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; border-left: 2px solid var(--line); }
.kids > .node:last-child::before { bottom: auto; height: 36px; }
.kids > .node::after { content: ""; position: absolute; left: 0; top: 36px; width: 12px; border-top: 2px solid var(--line); }

.row { display: flex; align-items: center; gap: 6px; }
.card {
  flex: 1; min-width: 0; min-height: 60px;
  display: flex; align-items: center; gap: 10px; text-align: left;
  padding: 7px 10px 7px 7px; border: 1px solid var(--line); border-left: 4px solid var(--gen);
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  transition: transform .08s;
}
.card:active { transform: scale(.985); }
.g0 { --gen: var(--g0); } .g1 { --gen: var(--g1); } .g2 { --gen: var(--g2); } .g3 { --gen: var(--g3); } .g4 { --gen: var(--g4); }
.names { min-width: 0; display: flex; flex-direction: column; }
.nm { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avs { display: flex; align-items: center; flex: none; }
.avs .av.sm { margin-left: -12px; border: 2px solid var(--surface); }

.tog {
  flex: none; height: 44px; min-width: 52px; padding: 0 8px;
  display: flex; align-items: center; justify-content: center; gap: 2px;
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  font-size: 14px; font-weight: 600; color: var(--muted);
}
.tog svg { width: 18px; height: 18px; transition: transform .15s; }
.tog[aria-expanded="true"] svg { transform: rotate(180deg); }
.tog[aria-expanded="true"] { color: var(--gen); border-color: var(--gen); }
.add {
  display: none; flex: none; width: 44px; height: 44px; place-items: center;
  border: 2px dashed var(--brand); color: var(--brand); background: transparent; border-radius: 999px;
}
body.editing .add { display: grid; }
.flash > .row .card, .flash > .couple { animation: flash 1.6s ease-out; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 4px var(--gen); } 100% { box-shadow: var(--shadow); } }

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty .btn { margin-top: 16px; }

/* ---------- avatars ---------- */
.av {
  --s: 44px; width: var(--s); height: var(--s); flex: none; border-radius: 50%;
  display: grid; place-items: center; overflow: hidden;
  background: hsl(var(--h, 170) 60% 90%); color: hsl(var(--h, 170) 55% 26%);
  font-weight: 700; font-size: calc(var(--s) * .38); letter-spacing: .02em;
}
.av img { width: 100%; height: 100%; object-fit: cover; display: block; }
.av.sm { --s: 30px; }
.av.xs { --s: 28px; }
.av.lg { --s: 56px; }
.av.xl { --s: 128px; }

/* ---------- bottom sheet ---------- */
.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh; margin: auto auto 0;
  padding: 0 0 var(--safe-b); border: 0; border-radius: 22px 22px 0 0;
  background: var(--surface); color: var(--text); overflow: auto; overscroll-behavior: contain;
}
.sheet[open] { animation: up .22s cubic-bezier(.2, .8, .2, 1); }
.sheet::backdrop { background: rgb(10 40 36 / .35); }
@keyframes up { from { transform: translateY(40%); opacity: .4; } }
@media (min-width: 700px) { .sheet { margin: auto; border-radius: 22px; } }
.grab { width: 40px; height: 5px; border-radius: 9px; background: var(--line); margin: 8px auto 0; }
#sheetBody { padding: 8px 20px 20px; }
.sheet h2 { margin: 8px 0 2px; font-size: 22px; line-height: 1.2; }
.sheet h3 { margin: 20px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.hero { display: flex; flex-direction: column; align-items: center; text-align: center; padding-top: 8px; }
.hero .av { border: 0; padding: 0; }
.muted { color: var(--muted); margin: 0; }
.chip { display: inline-block; margin-top: 8px; padding: 3px 10px; border-radius: 999px; background: var(--surface-2); color: var(--gen); font-size: 13px; font-weight: 600; }

.pchips { display: flex; flex-wrap: wrap; gap: 8px; }
.pchip {
  display: inline-flex; align-items: center; gap: 8px; max-width: 100%;
  padding: 4px 12px 4px 4px; min-height: 40px;
  border: 1px solid var(--line); border-radius: 999px; background: var(--surface);
}
.pchip span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 22px; }
.btn {
  min-height: 48px; padding: 10px 16px; border-radius: 14px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--line); background: var(--surface-2); font-weight: 600;
}
.btn.primary { background: linear-gradient(135deg, var(--brand), #0e7f9e); color: var(--brand-ink); border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.wide { grid-column: 1 / -1; }
.btn:disabled { opacity: .55; }
.btn svg { width: 20px; height: 20px; }

/* ---------- forms ---------- */
form { display: flex; flex-direction: column; gap: 14px; }
label.f { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
label.f small { font-weight: 400; color: var(--muted); }
input[type="text"], input[type="password"], input[type="search"], select {
  width: 100%; min-height: 48px; padding: 10px 14px;
  font: inherit; font-size: 16px; /* 16px stops iOS zoom-on-focus */
  color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.photo-pick { display: flex; align-items: center; gap: 14px; }
.photo-pick .av { --s: 72px; }
.photo-pick .links { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
.linkbtn { border: 0; background: none; padding: 6px 0; color: var(--brand); font-weight: 600; }
.linkbtn.danger { color: var(--danger); }
.order { display: flex; gap: 10px; }
.order .btn { flex: 1; }
.err { color: var(--danger); font-size: 14px; margin: 0; min-height: 1em; }

/* ---------- search ---------- */
.results { list-style: none; margin: 12px 0 0; padding: 0; }
.results button { width: 100%; display: flex; align-items: center; gap: 12px; padding: 10px 4px; border: 0; border-bottom: 1px solid var(--line); background: none; text-align: left; }

/* ---------- photo viewer / toast ---------- */
.viewer { border: 0; padding: 0; background: transparent; max-width: 100vw; max-height: 100dvh; }
.viewer::backdrop { background: rgb(0 0 0 / .9); }
.viewer img { display: block; max-width: 100vw; max-height: 100dvh; object-fit: contain; }
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); z-index: 100;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- top-down tree (chart view) ---------- */
body.chart-mode { height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
body.chart-mode .top, body.chart-mode .editbar { position: relative; top: 0; flex: none; }
.chart-wrap {
  flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain;
  touch-action: pan-x pan-y; /* we handle pinch ourselves */
}
.sizer { margin: 0 auto; }
.chart { width: max-content; padding: 20px 16px calc(110px + var(--safe-b)); transform-origin: 0 0; }
.chart ul { display: flex; justify-content: center; margin: 0; padding: 0; list-style: none; }
.chart li { position: relative; display: flex; flex-direction: column; align-items: center; padding: 22px 5px 0; }
.chart > ul > li { padding-top: 0; }
/* connectors: each child draws half of the horizontal bar on both sides, plus its own drop line */
.chart ul ul > li::before, .chart ul ul > li::after {
  content: ""; position: absolute; top: 0; width: 50%; height: 22px; border-top: 2px solid var(--line);
}
.chart ul ul > li::before { right: 50%; }
.chart ul ul > li::after { left: 50%; margin-left: -1px; border-left: 2px solid var(--line); }
.chart ul ul > li:first-child::before, .chart ul ul > li:last-child::after { border-top-color: transparent; }
.chart ul ul > li:first-child::after { border-top-left-radius: 10px; }
.chart ul ul > li:last-child::before { border-top-right-radius: 10px; border-right: 2px solid var(--line); margin-right: -1px; }
.chart ul ul > li:last-child:not(:only-child)::after { border-left: 0; }
.chart ul ul > li:only-child::before { display: none; }
.chart ul ul > li:only-child::after { border-radius: 0; }

.couple {
  position: relative; z-index: 1; display: flex; gap: 2px; padding: 6px;
  background: var(--surface); border: 1px solid var(--line); border-top: 4px solid var(--gen);
  border-radius: 16px; box-shadow: var(--shadow);
}
.pcard {
  width: 84px; display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 6px 2px 4px; border: 0; border-radius: 12px; background: transparent;
}
.pcard:active { background: var(--surface-2); }
.pcard.sp { border-left: 1px dashed var(--line); border-radius: 0 12px 12px 0; }
.pn {
  font-size: 13px; font-weight: 600; line-height: 1.2; text-align: center; word-break: break-word;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.pcard.sp .pn { font-weight: 500; color: var(--muted); }

.stem { position: relative; display: flex; justify-content: center; padding-top: 8px; min-height: 8px; }
.stem.down::before, .stem.has-kids::after {
  content: ""; position: absolute; left: 50%; margin-left: -1px; border-left: 2px solid var(--line);
}
.stem.down::before { top: 0; bottom: 0; }
.stem.has-kids::after { top: 0; height: 8px; }
.chart .tog { position: relative; z-index: 1; height: 34px; min-width: 50px; }
.chart .add { width: 34px; height: 34px; }
.chart .stem.has-kids .add { position: absolute; left: calc(50% + 32px); top: 8px; }
.chart .stem:not(.has-kids) .add { margin-top: 2px; }

.zoombar {
  display: none; position: fixed; right: 12px; bottom: calc(16px + var(--safe-b)); z-index: 5;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden;
}
body.chart-mode .zoombar { display: flex; }
.zoombar button { width: 46px; height: 46px; display: grid; place-items: center; border: 0; background: transparent; }
.zoombar button + button { border-left: 1px solid var(--line); }
.zoombar button:active { background: var(--surface-2); }

/* ---------- photo cropper ---------- */
.cropper {
  width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; padding: 0; border: 0;
  background: var(--bg); color: var(--text);
}
.cropper::backdrop { background: var(--bg); }
.crop-inner {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px;
  padding: calc(env(safe-area-inset-top, 0px) + 16px) 16px calc(16px + var(--safe-b));
}
.crop-title { margin: 0; font-size: 15px; color: var(--muted); }
.crop-stage {
  position: relative; width: min(100%, 440px, 62dvh); aspect-ratio: 1; flex: none;
  touch-action: none; cursor: grab; border-radius: 4px; overflow: hidden; background: #dfe9e7; box-shadow: var(--shadow);
}
.crop-stage:active { cursor: grabbing; }
.crop-stage canvas { width: 100%; height: 100%; display: block; }
/* circle shows how it looks as an avatar; outside the circle is dimmed but still kept in the square */
.crop-guide {
  position: absolute; inset: 0; pointer-events: none; border-radius: 50%;
  box-shadow: 0 0 0 9999px rgb(255 255 255 / .5); outline: 2px solid rgb(255 255 255 / .85);
}
.crop-stage::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; border: 2px solid rgb(255 255 255 / .5);
}
.crop-zoom { display: flex; align-items: center; gap: 12px; width: min(100%, 440px); color: var(--muted); }
.crop-zoom input { flex: 1; accent-color: var(--brand); height: 32px; }
.crop-actions { display: flex; gap: 12px; width: min(100%, 440px); }
.crop-actions .btn { flex: 1; }
.photo-pick button.av { border: 0; padding: 0; cursor: pointer; }

/* ---------- family photo ---------- */
@media (max-width: 420px) { .hide-sm { display: none; } }
.poster { display: block; width: 100%; margin: 14px 0 0; padding: 0; border: 0; background: none; border-radius: 14px; overflow: hidden; box-shadow: var(--shadow); }
.poster img { display: block; width: 100%; height: auto; }
a.btn { text-decoration: none; color: inherit; }
a.btn.primary { color: var(--brand-ink); }
.spinner { width: 40px; height: 40px; margin: 28px auto; border-radius: 50%; border: 4px solid var(--line); border-top-color: var(--brand); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
