:root {
    --green: #047857;
    --green-d: #065f46;
    --ink: #1f2937;
    --muted: #6b7280;
    --line: #e5e7eb;
    --bg: #f8faf9;
    --danger: #b91c1c;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    color: var(--ink);
    background: var(--bg);
}

/* ---------- Auth ---------- */
.auth-body {
    display: flex;
    min-height: 100vh;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #065f46, #0f766e);
}
.auth-card {
    background: #fff;
    width: 360px;
    max-width: 92vw;
    padding: 30px;
    border-radius: 14px;
    box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.brand { margin: 0; font-size: 20px; color: var(--green-d); }
.subtitle { color: var(--muted); margin: 4px 0 18px; }
.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab {
    flex: 1; padding: 8px; border: 1px solid var(--line); background: #fff;
    border-radius: 8px; cursor: pointer; color: var(--muted);
}
.tab.active { background: var(--green); color: #fff; border-color: var(--green); }
.auth-form { display: none; flex-direction: column; gap: 12px; }
.auth-form.active { display: flex; }
.auth-form label, .modal label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.auth-form input, .modal input, .modal select, .modal textarea {
    padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; color: var(--ink);
}
.hint { color: #9ca3af; font-weight: normal; }
.date-preview { color: var(--green-d); font-size: 11px; font-weight: 600; min-height: 14px; margin-top: 2px; }
.date-preview:empty { display: none; }
.alert { background: #fef2f2; color: var(--danger); padding: 10px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; }

.btn-primary {
    background: var(--green); color: #fff; border: none; padding: 10px 16px;
    border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 600;
}
.btn-primary:hover { background: var(--green-d); }
.btn-ghost { background: #fff; border: 1px solid var(--line); padding: 10px 16px; border-radius: 8px; cursor: pointer; }
.btn-danger { background: #fff; color: var(--danger); border: 1px solid #fecaca; padding: 10px 16px; border-radius: 8px; cursor: pointer; }
.btn-mini { background: var(--green); color: #fff; border: none; padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 12px; }

/* ---------- App shell ---------- */
.topbar {
    display: flex; align-items: center; gap: 14px;
    padding: 10px 18px; background: #fff; border-bottom: 1px solid var(--line);
    position: sticky; top: 0; z-index: 10;
}
.topbar .brand { font-size: 16px; }
.spacer { flex: 1; }
.view-toggle { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.vbtn { border: none; background: #fff; padding: 6px 14px; cursor: pointer; color: var(--muted); font-size: 13px; }
.vbtn.active { background: var(--green); color: #fff; }
.root-select { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.root-select select { padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: 13px; color: var(--ink); max-width: 200px; }

/* ---------- Searchable select (View from) ---------- */
.ss { position: relative; }
.ss-display {
    display: flex; align-items: center; gap: 8px; width: 190px;
    padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px;
    background: #fff; font-size: 13px; color: var(--ink); cursor: pointer; text-align: left;
}
.ss-display .ss-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ss-display .ss-caret { color: var(--muted); font-size: 11px; }
.ss-panel {
    position: absolute; top: calc(100% + 4px); left: 0; z-index: 60;
    width: 240px; max-width: 80vw; background: #fff; border: 1px solid var(--line);
    border-radius: 10px; box-shadow: 0 14px 34px rgba(0,0,0,.18); overflow: hidden;
}
.ss-panel[hidden] { display: none; }
.ss-search {
    width: 100%; border: none; border-bottom: 1px solid var(--line);
    padding: 9px 11px; font-size: 13px; color: var(--ink); outline: none;
}
.ss-list { list-style: none; margin: 0; padding: 4px; max-height: 260px; overflow-y: auto; }
.ss-opt { padding: 7px 9px; border-radius: 6px; font-size: 13px; cursor: pointer; }
.ss-opt:hover { background: #f0f4f2; }
.ss-opt.selected { background: var(--green); color: #fff; }
.ss-empty { padding: 9px; font-size: 13px; color: var(--muted); }
.user { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.logout { color: var(--green-d); text-decoration: none; }

main { position: relative; }
/* scope to page sections — bare .view collides with family-chart's <g class="view"> */
main > section.view { display: none; }
main > section.view.active { display: block; }

#treeView, .f3 { height: calc(100vh - 57px); width: 100%; }
.f3 { background:
    radial-gradient(circle, #e8efe9 1px, transparent 1px) 0 0 / 22px 22px,
    var(--bg);
}

.empty {
    position: absolute; inset: 0; display: flex; flex-direction: column;
    align-items: center; justify-content: center; text-align: center; gap: 6px; z-index: 5;
}
.empty[hidden] { display: none; }
.empty h2 { margin: 0; }
.empty p { color: var(--muted); margin: 0 0 10px; }

/* ---------- List view ---------- */
.people-table { width: 100%; border-collapse: collapse; background: #fff; }
.people-table th, .people-table td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); font-size: 14px; }
.people-table th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.people-table tr:hover td { background: #fafbfa; }
.tag { font-size: 11px; padding: 2px 8px; border-radius: 999px; }
.tag.living { background: #ecfdf5; color: var(--green-d); }
.tag.deceased { background: #f3f4f6; color: var(--muted); }
.row-link { color: var(--green-d); cursor: pointer; text-decoration: none; }

/* ---------- Family chart cards ---------- */
/* family-chart leaves link stroke unset (defaults to white via --text-color) */
.f3 .link { stroke: #94a3b8; stroke-width: 1.5px; fill: none; }

/* let the expand/collapse chips poke out above/below the card */
.f3 foreignObject { overflow: visible; }

.fc-card {
    position: relative;
    width: 220px; padding: 10px 12px; border-radius: 10px; background: #fff;
    border: 1px solid var(--line); box-shadow: 0 2px 6px rgba(0,0,0,.08);
    cursor: pointer; display: flex; gap: 10px; align-items: center;
    font-family: inherit; color: var(--ink);
}
.fc-toggle {
    position: absolute; left: 50%; transform: translateX(-50%);
    border: 1px solid var(--line); background: #fff; color: var(--green-d);
    border-radius: 999px; padding: 1px 9px; font-size: 11px; font-weight: 600;
    line-height: 1.5; cursor: pointer; box-shadow: 0 1px 3px rgba(0,0,0,.12);
    white-space: nowrap; z-index: 2;
}
.fc-toggle:hover { background: var(--green); color: #fff; border-color: var(--green); }
.fc-toggle.collapsed { color: var(--muted); }
.fc-up { top: -11px; }
.fc-down { bottom: -11px; }
.fc-root { left: auto; transform: none; top: -11px; right: 6px; padding: 1px 6px; }
.fc-info { left: 6px; transform: none; top: -11px; padding: 1px 7px; font-style: italic; font-family: Georgia, "Times New Roman", serif; }

/* ---------- Card details popover ---------- */
.card-popover {
    position: fixed; z-index: 90; width: 260px; max-width: calc(100vw - 16px);
    background: #fff; border: 1px solid var(--line); border-radius: 12px;
    box-shadow: 0 12px 34px rgba(0,0,0,.22); padding: 14px 16px;
    font-size: 13px; color: var(--ink);
}
.card-popover[hidden] { display: none; }
.card-popover .pd-name { font-weight: 700; font-size: 15px; margin-bottom: 8px; padding-right: 16px; color: var(--green-d); }
.card-popover .pd-row { display: flex; gap: 8px; padding: 3px 0; border-top: 1px solid #f1f3f2; }
.card-popover .pd-row:first-of-type { border-top: none; }
.card-popover .pd-k { color: var(--muted); flex: 0 0 92px; }
.card-popover .pd-v { color: var(--ink); flex: 1; }
.card-popover .pd-empty { color: var(--muted); border-top: none; }
.card-popover .pd-notes { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--line); white-space: pre-wrap; color: #374151; }
.card-popover .pd-close {
    position: absolute; top: 6px; right: 8px; border: none; background: none;
    font-size: 20px; line-height: 1; cursor: pointer; color: var(--muted);
}
.fc-card.male { border-left: 4px solid #2563eb; }
.fc-card.female { border-left: 4px solid #db2777; }
.fc-card.deceased { background: #f6f7f6; filter: saturate(.65); }
.fc-card.stub { border-style: dashed; opacity: .85; }
.fc-avatar {
    width: 44px; height: 44px; border-radius: 50%; flex: none;
    background: #e5e7eb center/cover no-repeat; display: flex; align-items: center;
    justify-content: center; color: var(--muted); font-weight: 700; font-size: 16px;
}
.fc-name { font-weight: 600; font-size: 14px; line-height: 1.2; }
.fc-sub { font-size: 11px; color: var(--muted); margin-top: 2px; }
.fc-cousin {
    display: inline-block; margin-top: 3px; font-size: 10px; font-weight: 600;
    color: #92400e; background: #fef3c7; border-radius: 999px; padding: 1px 7px;
    cursor: pointer;
}
.fc-cousin:hover { background: #fde68a; }
/* dashed connector linking the two copies of a person in a cousin marriage */
.f3 .dup-link { fill: none; stroke: #d97706; stroke-width: 2px; stroke-dasharray: 5 4; opacity: .85; }
.fc-card.fc-dup { box-shadow: 0 0 0 2px #f59e0b, 0 2px 6px rgba(0,0,0,.08); }
.fc-dead-dot { color: var(--muted); }

/* ---------- Modal ---------- */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(17,24,39,.5);
    display: flex; align-items: flex-start; justify-content: center; z-index: 50;
    padding: 30px 16px; overflow-y: auto;
}
.modal-overlay[hidden] { display: none; }
.modal {
    background: #fff; width: 640px; max-width: 100%; border-radius: 14px;
    box-shadow: 0 30px 60px rgba(0,0,0,.3);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-close { border: none; background: none; font-size: 24px; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-body { padding: 18px 20px; }
.modal fieldset { border: 1px solid var(--line); border-radius: 10px; margin: 0 0 16px; padding: 14px; }
.modal legend { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--green-d); padding: 0 6px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.more-toggle {
    margin-top: 12px; background: none; border: none; padding: 4px 0;
    color: var(--green-d); font-size: 13px; font-weight: 600; cursor: pointer;
}
.more-toggle:hover { text-decoration: underline; }
#moreFields[hidden] { display: none; }
.modal label { margin-bottom: 0; }
.checkline { flex-direction: row !important; align-items: center; gap: 8px; font-size: 14px; color: var(--ink); margin-bottom: 12px; }
.checkline input { width: auto; }
.death-fields { opacity: 1; }
.death-fields.hidden { display: none; }
.photo-preview img { max-height: 70px; border-radius: 8px; }
.modal-foot { display: flex; align-items: center; gap: 10px; padding-top: 6px; }
.form-error { color: var(--danger); font-size: 13px; margin-top: 10px; }

.rel-block { margin-bottom: 16px; }
.rel-block h4 { margin: 0 0 8px; font-size: 14px; }
.rel-list { list-style: none; margin: 0 0 8px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.rel-list li { display: flex; align-items: center; gap: 8px; font-size: 13px; background: #f9fafb; padding: 6px 10px; border-radius: 6px; }
.rel-list .rl-meta { color: var(--muted); font-size: 11px; }
.rel-list .rl-remove { margin-left: auto; border: none; background: none; color: var(--danger); cursor: pointer; font-size: 16px; }
.rel-add { display: flex; gap: 8px; flex-wrap: wrap; }
.rel-add select { flex: 1; min-width: 120px; }
.rel-combo { flex: 2; min-width: 160px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; color: var(--ink); }

/* ---------- Toast ---------- */
.toast {
    position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px);
    background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 999px;
    font-size: 13px; box-shadow: 0 8px 24px rgba(0,0,0,.25); z-index: 100;
    opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Outline (descendancy) view ---------- */
.outline { padding: 14px 18px 80px; max-width: 1000px; margin: 0 auto; }
.ol-empty { padding: 50px 20px; text-align: center; color: var(--muted); }

.ol-children { margin-left: 20px; border-left: 2px solid #e6ebe8; padding-left: 14px; }
.ol-row {
    position: relative; display: flex; align-items: center; gap: 10px;
    padding: 6px 8px; border-radius: 8px; cursor: pointer;
}
.ol-row:hover { background: #f4f7f5; }
.ol-children > .ol-node > .ol-row::before {
    content: ''; position: absolute; left: -14px; top: 20px; width: 12px; height: 2px; background: #e6ebe8;
}
.ol-chev {
    flex: none; width: 20px; height: 20px; border: none; background: none; color: var(--muted);
    cursor: pointer; font-size: 12px; line-height: 20px; border-radius: 4px; padding: 0;
}
.ol-chev:hover { background: #e5efe9; color: var(--green-d); }
.ol-chev.leaf { visibility: hidden; }
.ol-avatar {
    flex: none; width: 36px; height: 36px; border-radius: 50%; background: #e5e7eb center/cover no-repeat;
    display: flex; align-items: center; justify-content: center; color: var(--muted); font-weight: 700; font-size: 14px;
}
.ol-dot { flex: none; width: 9px; height: 9px; border-radius: 2px; background: #9ca3af; }
.ol-dot.male { background: #2563eb; }
.ol-dot.female { background: #db2777; }
.ol-body { min-width: 0; display: flex; flex-direction: column; }
.ol-name { font-weight: 600; font-size: 14px; color: var(--ink); line-height: 1.25; }
.ol-row.deceased .ol-name { color: #4b5563; }
.ol-meta { font-size: 12px; color: var(--muted); line-height: 1.3; }
.ol-count { color: var(--green-d); }
.ol-cousin { color: #92400e; }
.ol-sp { color: var(--green-d); cursor: pointer; }
.ol-sp:hover { text-decoration: underline; }
.ol-actions { margin-left: 10px; display: flex; gap: 4px; opacity: 0; transition: opacity .12s; }
.ol-row:hover .ol-actions { opacity: 1; }
.ol-act {
    width: 24px; height: 24px; border: 1px solid var(--line); background: #fff; border-radius: 999px;
    cursor: pointer; color: var(--green-d); font-size: 12px; line-height: 1; padding: 0;
}
.ol-act.ol-info { font-style: italic; font-family: Georgia, serif; }
.ol-act:hover { background: var(--green); color: #fff; border-color: var(--green); }
@media (hover: none) { .ol-actions { opacity: 1; } }
@media (max-width: 760px) {
    .outline { padding: 10px 10px 80px; }
    .ol-children { margin-left: 12px; padding-left: 10px; }
}
