/* ============ Tokens ============ */
:root {
    --bg: #f5f3ee;
    --surface: #ffffff;
    --surface-2: #faf9f6;
    --ink: #1d2621;
    --muted: #6b736e;
    --line: #e5e1d8;
    --brand: #2e6b57;
    --brand-strong: #235343;
    --brand-soft: #e4efe9;
    --accent: #c2703a;
    --accent-soft: #f8ebe1;
    --danger: #b42318;
    --danger-soft: #fdecea;
    --success: #1f7a4d;
    --tree-line: #bdb6a7;
    --radius: 14px;
    --radius-sm: 10px;
    --shadow: 0 1px 2px rgba(29, 38, 33, .06), 0 4px 16px rgba(29, 38, 33, .06);
    --shadow-lg: 0 12px 40px rgba(29, 38, 33, .18);
    --topbar-h: 60px;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
    font-family: "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
    font-size: 15px;
    line-height: 1.5;
    color: var(--ink);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
.icon { flex: none; vertical-align: middle; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ============ Topbar ============ */
.topbar {
    position: sticky; top: 0; z-index: 40;
    height: var(--topbar-h);
    display: flex; align-items: center; gap: 16px;
    padding: 0 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); min-width: 0; }
.brand:hover { text-decoration: none; }
.brand-mark {
    display: grid; place-items: center; flex: none;
    width: 34px; height: 34px; border-radius: 10px;
    background: var(--brand); color: #fff; font-weight: 700; font-size: 13px; letter-spacing: .5px;
}
.brand-name { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.nav-link {
    display: inline-flex; align-items: center; gap: 8px;
    padding: 8px 12px; border-radius: var(--radius-sm);
    color: var(--muted); font-weight: 600; font-size: 14px;
}
.nav-link:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.nav-link.is-active { background: var(--brand-soft); color: var(--brand-strong); }
.nav-user { display: flex; align-items: center; gap: 10px; margin-left: 12px; padding-left: 16px; border-left: 1px solid var(--line); }
.nav-user-text { display: flex; flex-direction: column; line-height: 1.2; }
.nav-user-text strong { font-size: 14px; }
.nav-user-text small { color: var(--muted); font-size: 12px; text-transform: capitalize; }
.topbar .nav-toggle { display: none; margin-left: auto; }

@media (max-width: 860px) {
    .topbar .nav-toggle { display: inline-grid; }
    .nav {
        display: none;
        position: absolute; top: var(--topbar-h); left: 0; right: 0;
        flex-direction: column; align-items: stretch; gap: 2px;
        padding: 10px 12px 14px;
        background: var(--surface); border-bottom: 1px solid var(--line); box-shadow: var(--shadow);
    }
    .nav.is-open { display: flex; }
    .nav-link { padding: 12px; }
    .nav-user { margin: 8px 0 0; padding: 12px 12px 0; border-left: 0; border-top: 1px solid var(--line); }
    .nav-user .icon-btn { margin-left: auto; }
}
@media (max-width: 420px) { .brand-name { font-size: 14px; } }

/* ============ Buttons & inputs ============ */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    height: 40px; padding: 0 16px;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    font: inherit; font-weight: 600; font-size: 14px; cursor: pointer;
    background: var(--brand); color: #fff; white-space: nowrap;
    transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: var(--brand-strong); text-decoration: none; }
.btn-ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn-ghost:hover { background: var(--surface-2); border-color: #d6d1c5; }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #8f1c13; }
.btn-block { width: 100%; }
.btn-sm { height: 34px; padding: 0 12px; font-size: 13px; }
.icon-btn {
    display: inline-grid; place-items: center;
    width: 36px; height: 36px; padding: 0;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .label { font-weight: 600; font-size: 13px; }
.field .hint { font-size: 12px; color: var(--muted); }
.input, .select {
    width: 100%; height: 42px; padding: 0 12px;
    border: 1px solid var(--line); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--ink); font: inherit;
    transition: border-color .15s, box-shadow .15s;
}
.select { appearance: none; padding-right: 36px; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236b736e' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; }
.input:focus, .select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.input-icon { position: relative; }
.input-icon .icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.input-icon .input { padding-left: 38px; }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--brand); }

/* ============ Layout pieces ============ */
.page { max-width: 1120px; margin: 0 auto; padding: 28px 20px 60px; }
.page-narrow { max-width: 760px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1 { font-size: 24px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.back-link { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-weight: 600; font-size: 14px; margin-bottom: 10px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-body { padding: 22px; }
.card-head { padding: 16px 22px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.card-head h2 { font-size: 16px; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 640px) { .grid-2 { grid-template-columns: 1fr; } }

.badge {
    display: inline-flex; align-items: center; height: 22px; padding: 0 8px;
    border-radius: 999px; font-size: 12px; font-weight: 600;
    background: var(--surface-2); color: var(--muted); border: 1px solid var(--line);
}
.badge-brand { background: var(--brand-soft); color: var(--brand-strong); border-color: transparent; }
.badge-accent { background: var(--accent-soft); color: #9a5326; border-color: transparent; }

.alert { padding: 12px 14px; border-radius: var(--radius-sm); font-size: 14px; }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert ul { margin: 0; padding-left: 18px; }

.toast {
    position: fixed; z-index: 100; left: 50%; top: calc(var(--topbar-h) + 14px);
    transform: translateX(-50%);
    max-width: calc(100% - 32px); padding: 12px 18px; border-radius: var(--radius-sm);
    background: var(--ink); color: #fff; font-weight: 600; font-size: 14px; box-shadow: var(--shadow-lg);
    animation: toast-in .25s ease-out;
}
.toast-error { background: var(--danger); }
.toast.is-hiding { opacity: 0; transition: opacity .3s; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -8px); } }

/* ============ Avatar ============ */
.avatar {
    display: inline-grid; place-items: center; flex: none;
    width: 44px; height: 44px; border-radius: 50%;
    object-fit: cover; background: var(--brand-soft); color: var(--brand-strong);
    font-weight: 700; font-size: 15px;
}
.avatar-sm { width: 32px; height: 32px; font-size: 12px; }
.avatar-lg { width: 96px; height: 96px; font-size: 30px; }

/* ============ Table ============ */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
    text-align: left; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px;
    color: var(--muted); padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--surface-2);
}
.table td { padding: 12px 16px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--surface-2); }
.table .actions { display: flex; gap: 4px; justify-content: flex-end; }
.person-cell { display: flex; align-items: center; gap: 12px; min-width: 200px; }
.person-cell strong { display: block; font-weight: 600; }
.person-cell small { color: var(--muted); }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

@media (max-width: 720px) {
    .table-cards thead { display: none; }
    .table-cards tr { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
    .table-cards td { padding: 0; border: 0; }
    .table-cards td[data-label] { grid-column: 1; font-size: 13px; color: var(--muted); }
    .table-cards td[data-label]::before { content: attr(data-label) ": "; font-weight: 600; }
    .table-cards td.cell-main { grid-column: 1; }
    .table-cards td.cell-actions { grid-column: 2; grid-row: 1; }
    .table-cards td.hide-sm { display: none; }
}

/* ============ Login ============ */
.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background:
    radial-gradient(1200px 500px at 10% -10%, #e4efe9 0%, transparent 60%),
    radial-gradient(900px 500px at 110% 110%, #f8ebe1 0%, transparent 60%), var(--bg); }
.auth-card { width: 100%; max-width: 400px; }
.auth-card .card-body { padding: 32px; }
.auth-brand { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 12px; margin-bottom: 26px; }
.auth-brand .brand-mark { width: 52px; height: 52px; font-size: 18px; border-radius: 14px; }
.auth-brand h1 { font-size: 20px; }
.auth-brand p { margin: 0; color: var(--muted); font-size: 14px; }

/* ============ Member form ============ */
.photo-picker { display: flex; align-items: center; gap: 18px; }
.photo-picker .avatar-lg { border: 1px solid var(--line); }
.photo-picker .actions { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.form-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 22px; border-top: 1px solid var(--line); background: var(--surface-2); border-radius: 0 0 var(--radius) var(--radius); }
.relation-note { font-size: 13px; color: var(--muted); background: var(--surface-2); border: 1px dashed var(--line); padding: 10px 12px; border-radius: var(--radius-sm); }

/* ============ Users page ============ */
.users-layout { display: grid; grid-template-columns: 340px 1fr; gap: 20px; align-items: start; }
@media (max-width: 860px) { .users-layout { grid-template-columns: 1fr; } }

/* ============ Tree page ============ */
.tree-page { height: 100vh; overflow: hidden; display: flex; flex-direction: column; }
.tree-shell { position: relative; flex: 1; min-height: 0; }
.viewport {
    position: absolute; inset: 0; overflow: hidden; cursor: grab; touch-action: none;
    background-color: var(--bg);
    background-image: radial-gradient(#d9d4c8 1px, transparent 1px);
    background-size: 22px 22px;
}
.viewport.is-dragging { cursor: grabbing; }
.canvas { position: absolute; left: 0; top: 0; transform-origin: 0 0; width: max-content; padding: 40px; }
.canvas.is-animating { transition: transform .45s cubic-bezier(.2, .8, .2, 1); }

.tree-toolbar {
    position: absolute; z-index: 10; top: 16px; left: 16px; right: 16px;
    display: flex; gap: 10px; align-items: flex-start; pointer-events: none;
}
.tree-toolbar > * { pointer-events: auto; }
.search-box { position: relative; width: 320px; max-width: 100%; }
.search-box .input { height: 44px; box-shadow: var(--shadow); border-color: transparent; }
.search-results {
    position: absolute; top: calc(100% + 6px); left: 0; right: 0;
    background: var(--surface); border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
    max-height: 340px; overflow-y: auto; padding: 6px; display: none;
}
.search-results.is-open { display: block; }
.search-item {
    display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 8px; border: 0; border-radius: 8px; background: none; font: inherit; text-align: left; cursor: pointer;
}
.search-item:hover, .search-item.is-active { background: var(--surface-2); }
.search-item strong { display: block; font-size: 14px; font-weight: 600; }
.search-item small { color: var(--muted); font-size: 12px; }
.search-empty { padding: 12px; color: var(--muted); font-size: 14px; }

.stats { display: flex; gap: 8px; margin-left: auto; }
.stat { background: var(--surface); border-radius: var(--radius-sm); box-shadow: var(--shadow); padding: 6px 14px; text-align: center; }
.stat b { display: block; font-size: 17px; line-height: 1.2; }
.stat span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
@media (max-width: 760px) { .stats { display: none; } .search-box { width: 100%; } }

.zoom-controls {
    position: absolute; z-index: 10; right: 16px; bottom: 16px;
    display: flex; flex-direction: column; gap: 2px; padding: 4px;
    background: var(--surface); border-radius: 12px; box-shadow: var(--shadow);
}
.zoom-controls .icon-btn { width: 40px; height: 40px; }
.zoom-level { font-size: 11px; font-weight: 700; color: var(--muted); text-align: center; padding: 2px 0; }
.tree-hint { position: absolute; z-index: 10; left: 16px; bottom: 16px; font-size: 12px; color: var(--muted); background: rgba(255,255,255,.8); padding: 6px 10px; border-radius: 8px; }
@media (max-width: 760px) { .tree-hint { display: none; } }

/* Forest of families */
.forest { display: flex; flex-wrap: wrap; justify-content: center; align-items: flex-start; gap: 80px 96px; max-width: 3800px; }
.family-title {
    text-align: center; margin-bottom: 18px;
    font-size: 12px; font-weight: 700; letter-spacing: .6px; text-transform: uppercase; color: var(--muted);
}
.loose { max-width: 720px; }
.loose-grid { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; }

/* Org-chart connectors */
.tree ul { position: relative; display: flex; justify-content: center; margin: 0; padding: 26px 0 0; list-style: none; }
.tree > ul { padding-top: 0; }
.tree li { position: relative; display: flex; flex-direction: column; align-items: center; padding: 26px 10px 0; }
.tree > ul > li { padding-top: 0; }
.tree li::before, .tree li::after {
    content: ""; position: absolute; top: 0; right: 50%; width: 50%; height: 26px;
    border-top: 2px solid var(--tree-line);
}
.tree li::after { right: auto; left: 50%; border-left: 2px solid var(--tree-line); }
.tree li:only-child::before { display: none; }
.tree li:only-child::after { border-top: 0; }
.tree li:first-child::before, .tree li:last-child::after { border: 0; }
.tree li:last-child::before { border-right: 2px solid var(--tree-line); border-radius: 0 10px 0 0; }
.tree li:first-child::after { border-radius: 10px 0 0 0; }
.tree li:only-child::after { border-radius: 0; }
.tree > ul > li::before, .tree > ul > li::after { display: none; }
.tree ul ul::before {
    content: ""; position: absolute; top: 0; left: 50%; height: 26px;
    border-left: 2px solid var(--tree-line);
}

/* Unit = person + spouses */
.unit { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.couple {
    display: flex; align-items: center; gap: 0;
    padding: 6px; border-radius: 18px;
}
.couple.has-spouse { background: rgba(255,255,255,.55); border: 1px dashed #d6cfbf; }
.tie { display: grid; place-items: center; width: 26px; color: var(--accent); flex: none; }

.person {
    display: flex; flex-direction: column; align-items: center; gap: 8px;
    width: 148px; padding: 14px 10px 12px;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    box-shadow: var(--shadow); cursor: pointer; font: inherit; color: inherit; text-align: center;
    transition: transform .15s, box-shadow .15s, border-color .15s;
}
.person:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(29,38,33,.12); border-color: #cfc9bb; }
.person:focus-visible { outline: 3px solid var(--brand-soft); border-color: var(--brand); }
.person .avatar { width: 64px; height: 64px; font-size: 20px; box-shadow: 0 0 0 3px #fff, 0 0 0 4px var(--line); }
.person-name { font-weight: 700; font-size: 13.5px; line-height: 1.3; word-break: break-word; }
.person-role { font-size: 12px; color: var(--muted); margin-top: -4px; }
.person.is-main { border-top: 3px solid var(--brand); }
.person.is-spouse { background: var(--surface-2); border-top: 3px solid var(--accent); }
.person.is-spouse .avatar { background: var(--accent-soft); color: #9a5326; }
.person.is-ref { border-style: dashed; }
.person.is-highlight { animation: pulse 1.6s ease-out 2; border-color: var(--brand); }
@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(46,107,87,.55); }
    100% { box-shadow: 0 0 0 22px rgba(46,107,87,0); }
}
.ref-chip {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 4px 10px; border-radius: 999px; border: 0; cursor: pointer;
    background: var(--accent-soft); color: #9a5326; font: inherit; font-size: 12px; font-weight: 600;
}
.ref-chip:hover { background: #f3dccb; }
.loose .person { width: 132px; }

/* ============ Drawer ============ */
.drawer-backdrop { position: fixed; inset: 0; z-index: 60; background: rgba(29,38,33,.28); opacity: 0; pointer-events: none; transition: opacity .2s; }
.drawer {
    position: fixed; z-index: 61; top: 0; right: 0; bottom: 0;
    width: 400px; max-width: 100%;
    background: var(--surface); box-shadow: var(--shadow-lg);
    transform: translateX(100%); transition: transform .25s cubic-bezier(.2,.8,.2,1);
    display: flex; flex-direction: column;
}
.drawer-open .drawer { transform: none; }
.drawer-open .drawer-backdrop { opacity: 1; pointer-events: auto; }
.drawer-head { display: flex; justify-content: flex-end; padding: 12px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 0 24px 24px; }
.drawer-hero { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 10px; margin-bottom: 20px; }
.drawer-hero .avatar { width: 120px; height: 120px; font-size: 36px; box-shadow: 0 0 0 4px #fff, 0 0 0 5px var(--line); }
.drawer-hero h2 { font-size: 20px; }
.drawer-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-top: 6px; }
.rel-section { margin-top: 18px; }
.rel-section h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin-bottom: 8px; }
.rel-list { display: flex; flex-direction: column; gap: 4px; }
.rel-item {
    display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 8px; border: 0; border-radius: 10px; background: none; font: inherit; text-align: left; cursor: pointer;
}
.rel-item:hover { background: var(--surface-2); }
.rel-item strong { display: block; font-size: 14px; font-weight: 600; }
.rel-item small { color: var(--muted); font-size: 12px; }
