:root {
    --paper: #f3efe5;
    --paper-deep: #e8dfcd;
    --ink: #1f2b28;
    --forest: #183b32;
    --forest-soft: #31564b;
    --vermillion: #ad3c2e;
    --gold: #b9924b;
    --line: #9a8060;
    --white: #fffdf8;
    --shadow: 0 12px 32px rgba(36, 42, 35, .12);
    font-family: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
    color: var(--ink);
    background: var(--paper);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { scroll-behavior: smooth; }
body { margin: 0; min-width: 320px; overflow-x: hidden; background: var(--paper); }
button, input, textarea, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

.topbar {
    position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
    min-height: 72px; padding: max(10px, env(safe-area-inset-top)) 18px 10px;
    background: rgba(243, 239, 229, .94); border-bottom: 1px solid rgba(66, 63, 50, .12);
    backdrop-filter: blur(16px);
}
.brand-mark {
    display: grid; place-items: center; width: 44px; height: 44px; flex: 0 0 44px;
    color: var(--white); background: var(--vermillion); border: 1px solid #832c22;
    font: 700 22px/1 "Songti SC", serif;
}
.brand-copy { min-width: 0; }
.brand-copy p, .brand-copy h1 { margin: 0; letter-spacing: 0; }
.brand-copy p { color: #7c7568; font-size: 11px; }
.brand-copy h1 { margin-top: 2px; font: 700 20px/1.25 "Songti SC", serif; }
.icon-button { margin-left: auto; color: var(--forest); text-decoration: none; border: 1px solid #9e9585; padding: 8px 12px; font-size: 13px; }

.intro-band {
    position: relative; display: flex; justify-content: space-between; align-items: center;
    min-height: 210px; padding: 34px 22px 38px; overflow: hidden;
    color: var(--white); background: var(--forest);
}
.intro-band::before {
    content: ""; position: absolute; inset: 0; opacity: .16;
    background-image: repeating-linear-gradient(45deg, transparent 0 18px, rgba(255,255,255,.15) 19px, transparent 20px 38px);
}
.intro-band > * { position: relative; }
.eyebrow { color: var(--gold); font-size: 12px; font-weight: 700; letter-spacing: 0; }
.intro-band h2 { max-width: 390px; margin: 12px 0 0; font: 700 34px/1.25 "Songti SC", serif; letter-spacing: 0; }
.seal { display: grid; grid-template-columns: 1fr 1fr; width: 82px; height: 82px; padding: 9px; border: 2px solid #d16554; color: #e78270; transform: rotate(-5deg); }
.seal span { display: grid; place-items: center; border: 1px solid currentColor; font: 700 24px/1 "Songti SC", serif; }

.toolbar { padding: 18px; background: var(--white); border-bottom: 1px solid #ded5c3; }
.search-box { display: flex; align-items: center; gap: 8px; height: 44px; padding: 0 13px; background: #efebe2; border: 1px solid transparent; }
.search-box:focus-within { border-color: var(--gold); background: #fff; }
.search-box span { font-size: 22px; color: #706a5d; }
.search-box input { width: 100%; border: 0; outline: 0; background: transparent; color: var(--ink); }
.generation-filter { display: flex; gap: 8px; margin-top: 12px; overflow-x: auto; scrollbar-width: none; }
.generation-filter::-webkit-scrollbar { display: none; }
.generation-filter button { flex: 0 0 auto; border: 1px solid #cfc4b0; color: #5f594d; background: transparent; padding: 7px 13px; }
.generation-filter button.active { color: #fff; background: var(--forest); border-color: var(--forest); }

.tree-section { padding: 24px 0 42px; }
.tree-heading { display: flex; align-items: end; justify-content: space-between; gap: 16px; padding: 0 18px 16px; }
.section-index { color: var(--vermillion); font-size: 11px; font-weight: 700; }
.tree-heading h2 { margin: 3px 0 0; font: 700 26px/1.2 "Songti SC", serif; }
.tree-heading p { margin: 0; color: #81796b; font-size: 12px; text-align: right; }
.tree-viewport { width: 100%; overflow: auto; padding: 10px 18px 24px; scrollbar-width: thin; scrollbar-color: #a8987d transparent; }
.tree-canvas { position: relative; min-width: 820px; min-height: 620px; }
.tree-lines { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.tree-line { fill: none; stroke: var(--line); stroke-width: 2; vector-effect: non-scaling-stroke; }
.tree-line.spouse { stroke: var(--vermillion); stroke-dasharray: 4 4; }
.tree-line.parent-trunk { stroke-width: 2.5; }
.family-unit { position: absolute; z-index: 1; border: 1px solid transparent; pointer-events: none; }
.family-unit.couple { border-color: rgba(173, 60, 46, .42); background: rgba(255, 253, 248, .32); }
.family-unit.couple > span {
    position: absolute; left: 50%; top: -12px; z-index: 3; padding: 2px 7px;
    color: #fff; background: var(--vermillion); font-size: 9px; line-height: 18px;
    transform: translateX(-50%);
}
.person-node {
    position: absolute; z-index: 2; display: flex; align-items: center; gap: 10px; width: var(--node-width, 174px); min-height: 82px;
    padding: 10px; border: 1px solid #b9aa90; border-left: 4px solid var(--gold);
    color: var(--ink); text-align: left; background: var(--white); box-shadow: 0 7px 18px rgba(42, 43, 35, .08);
    transition: transform .18s ease, box-shadow .18s ease, opacity .18s ease;
}
.person-node:active, .person-node:focus-visible { transform: translateY(-2px); box-shadow: var(--shadow); outline: 2px solid var(--gold); }
.person-node.dimmed { opacity: .14; }
.person-node.highlight { z-index: 4; border-color: var(--vermillion); box-shadow: 0 10px 30px rgba(173,60,46,.22); }
.avatar-wrap, .profile-avatar { position: relative; display: grid; place-items: center; overflow: hidden; flex: 0 0 auto; background: #d9d0be; border-radius: 50%; }
.avatar-wrap { width: 54px; height: 54px; }
.avatar-wrap img, .profile-avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-fallback { color: var(--forest); font: 700 20px/1 "Songti SC", serif; }
.node-copy { min-width: 0; display: grid; gap: 4px; }
.node-copy strong { overflow: hidden; font: 700 17px/1.2 "Songti SC", serif; white-space: nowrap; text-overflow: ellipsis; }
.node-copy small { color: #6d665a; font-size: 11px; }
.node-copy em { overflow: hidden; color: var(--forest-soft); font-size: 11px; font-style: normal; white-space: nowrap; text-overflow: ellipsis; }
.generation-label { position: absolute; left: 0; color: #9a8260; font: 700 13px/1 "Songti SC", serif; writing-mode: vertical-rl; }
.gesture-hint { margin: 4px 18px 0; color: #8d8476; font-size: 11px; text-align: center; }
.mobile-family-tree { display: grid; gap: 34px; }
.mobile-family-canvas { position: relative; }
.mobile-family-branch { position: relative; min-width: 0; }
.mobile-household { position: relative; padding: 0 0 8px; }
.mobile-household-heading {
    display: flex; align-items: center; justify-content: space-between; min-height: 34px;
    margin-bottom: 8px; padding: 0 10px; color: #fff; background: var(--forest);
}
.mobile-household-heading span { font-size: 11px; font-weight: 700; }
.mobile-household-heading strong { font: 700 13px/1 "Songti SC", serif; }
.mobile-household-members { display: flex; gap: 8px; min-width: 0; }
.mobile-household-members .person-node {
    position: relative; left: auto !important; top: auto !important; flex: 1 1 0; width: auto;
    min-width: 0; box-shadow: 0 5px 14px rgba(42,43,35,.08);
}
.mobile-household-members .person-node + .person-node { border-left-color: var(--vermillion); }
.mobile-children-divider { display: flex; align-items: center; gap: 8px; min-height: 38px; margin: 8px 0 0 14px; padding-left: 19px; border-left: 2px solid var(--line); }
.mobile-children-divider::before { content: ""; width: 16px; border-top: 2px solid var(--line); }
.mobile-children-divider span { color: var(--vermillion); font: 700 14px/1 "Songti SC", serif; }
.mobile-children-divider strong { padding: 3px 7px; color: #fff; background: var(--gold); font-size: 10px; }
.mobile-family-children { display: grid; gap: 24px; margin-left: 14px; padding: 8px 0 0 18px; border-left: 2px solid var(--line); }
.mobile-family-children > .mobile-family-branch::before { content: ""; position: absolute; left: -18px; top: 25px; width: 18px; border-top: 2px solid var(--line); }

.profile-sheet { width: min(560px, 100%); max-width: none; max-height: 88vh; margin: auto 0 0; padding: 0; border: 0; border-radius: 0; background: var(--paper); box-shadow: 0 -20px 60px rgba(0,0,0,.24); }
.profile-sheet::backdrop { background: rgba(15, 24, 22, .58); backdrop-filter: blur(3px); }
.sheet-close { position: sticky; top: 12px; z-index: 3; float: right; width: 40px; height: 40px; margin: 12px 12px -52px 0; border: 1px solid rgba(255,255,255,.45); border-radius: 50%; color: #fff; background: rgba(17,41,35,.72); font-size: 25px; }
.profile-hero { display: flex; gap: 18px; align-items: end; min-height: 180px; padding: 56px 22px 24px; color: #fff; background: var(--forest); }
.profile-avatar { width: 92px; height: 92px; border: 3px solid rgba(255,255,255,.75); }
.profile-avatar .avatar-fallback { color: #fff; font-size: 34px; }
.profile-hero h2 { margin: 0 0 6px; font: 700 30px/1.1 "Songti SC", serif; }
.profile-hero p { margin: 0; color: #d8d7c9; font-size: 13px; }
.profile-body { padding: 22px 22px calc(30px + env(safe-area-inset-bottom)); }
.profile-facts { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid #d6ccb9; border-left: 1px solid #d6ccb9; }
.profile-facts div { min-height: 68px; padding: 11px; border-right: 1px solid #d6ccb9; border-bottom: 1px solid #d6ccb9; }
.profile-facts span { display: block; color: #8a806f; font-size: 11px; }
.profile-facts strong { display: block; margin-top: 5px; font-size: 14px; font-weight: 600; }
.profile-section { margin-top: 24px; }
.profile-section h3 { margin: 0 0 8px; color: var(--vermillion); font: 700 17px/1.2 "Songti SC", serif; }
.profile-section p { margin: 0; color: #49483f; font-size: 14px; line-height: 1.8; white-space: pre-wrap; }
.relations-section { margin: -2px 0 22px; padding-bottom: 22px; border-bottom: 1px solid #d6ccb9; }
.relations-heading { display: flex; align-items: end; justify-content: space-between; gap: 16px; margin-bottom: 12px; }
.relations-heading span { color: var(--vermillion); font-size: 11px; font-weight: 700; }
.relations-heading h3 { margin: 3px 0 0; font: 700 19px/1.2 "Songti SC", serif; }
.relations-heading > strong { color: #8b806e; font-size: 12px; font-weight: 500; }
.relations-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.relative-button {
    position: relative; display: grid; grid-template-columns: 42px 1fr 12px; align-items: center; gap: 9px;
    min-height: 68px; padding: 20px 9px 8px; border: 1px solid #cfc2ad; color: var(--ink);
    text-align: left; background: #fffdf8;
}
.relative-button:active, .relative-button:focus-visible { border-color: var(--vermillion); outline: 2px solid rgba(173,60,46,.18); }
.relative-role { position: absolute; left: 8px; top: 5px; color: var(--vermillion); font-size: 9px; font-weight: 700; }
.relative-avatar { display: grid; place-items: center; width: 38px; height: 38px; overflow: hidden; border-radius: 50%; color: var(--forest); background: #ddd3c0; font: 700 15px/1 "Songti SC", serif; }
.relative-avatar img { width: 100%; height: 100%; object-fit: cover; }
.relative-name { min-width: 0; }
.relative-name strong, .relative-name small { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.relative-name strong { font-size: 13px; }
.relative-name small { margin-top: 3px; color: #81796b; font-size: 9px; }
.relative-arrow { color: var(--gold); font-size: 22px; }
.empty-relation { grid-column: 1 / -1; margin: 0; padding: 18px; color: #81796b; text-align: center; background: #ebe5d8; }

/* Admin */
.admin-body { background: #ece8df; }
.auth-screen { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 22px; background: var(--forest); }
.auth-panel { width: 100%; max-width: 420px; padding: 34px; background: var(--paper); box-shadow: 0 28px 70px rgba(0,0,0,.25); }
.auth-panel h1 { margin: 14px 0 8px; font: 700 27px/1.25 "Songti SC", serif; }
.auth-panel p { margin: 0 0 22px; color: #746d61; line-height: 1.6; }
.auth-panel label { display: grid; gap: 7px; font-size: 13px; }
.auth-panel input { height: 46px; padding: 0 12px; border: 1px solid #bdb29f; background: #fff; }
.auth-panel form { display: grid; gap: 14px; }
.auth-panel > a { display: inline-block; margin-top: 22px; color: var(--forest); font-size: 13px; }
.primary-button, .upload-button { display: inline-flex; justify-content: center; align-items: center; min-height: 44px; padding: 0 20px; border: 0; color: #fff; background: var(--vermillion); font-weight: 700; }
.primary-button.compact { min-height: 36px; padding: 0 12px; font-size: 12px; }
.admin-header { display: flex; justify-content: space-between; align-items: center; min-height: 86px; padding: 18px 24px; color: #fff; background: var(--forest); }
.admin-header h1 { margin: 3px 0 0; font: 700 25px/1.2 "Songti SC", serif; }
.admin-actions { display: flex; gap: 8px; }
.admin-actions a, .admin-actions button { padding: 9px 12px; border: 1px solid rgba(255,255,255,.4); color: #fff; background: transparent; text-decoration: none; font-size: 12px; }
.admin-layout { display: grid; grid-template-columns: 320px minmax(0, 1fr); min-height: calc(100vh - 86px); }
.people-panel { padding: 20px; background: #f8f5ee; border-right: 1px solid #d4ccbd; }
.panel-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.panel-title h2, .panel-title h3 { margin: 0; font: 700 20px/1.2 "Songti SC", serif; }
.people-panel .search-box { margin: 16px 0; }
.people-list { display: grid; gap: 7px; max-height: calc(100vh - 230px); overflow: auto; }
.people-list button { display: grid; grid-template-columns: 38px 1fr auto; align-items: center; gap: 10px; padding: 9px; border: 1px solid transparent; color: var(--ink); text-align: left; background: transparent; }
.people-list button:hover, .people-list button.active { border-color: #c8b99e; background: #fff; }
.list-avatar { display: grid; place-items: center; width: 38px; height: 38px; overflow: hidden; border-radius: 50%; color: var(--forest); background: #ddd3c0; font-weight: 700; }
.list-avatar img { width: 100%; height: 100%; object-fit: cover; }
.people-list strong { display: block; font-size: 14px; }
.people-list small { color: #847c6f; }
.people-list em { color: var(--vermillion); font-size: 11px; font-style: normal; }
.editor-panel { padding: 24px; overflow: hidden; }
.family-settings { display: flex; align-items: end; gap: 10px; max-width: 760px; margin: 0 auto 16px; padding: 14px; background: #fff; border: 1px solid #d7cfc0; }
.family-settings label { flex: 1; display: grid; gap: 6px; font-size: 12px; }
.family-settings input { height: 40px; border: 1px solid #c9bfad; padding: 0 10px; }
.family-settings button { height: 40px; border: 0; padding: 0 14px; color: #fff; background: var(--forest); }
#personForm { max-width: 760px; margin: auto; background: #fff; border: 1px solid #d7cfc0; }
.editor-heading { display: flex; justify-content: space-between; align-items: center; padding: 22px; border-bottom: 1px solid #e1dacf; }
.editor-heading h2 { margin: 4px 0 0; font: 700 26px/1.2 "Songti SC", serif; }
.danger-button { padding: 8px 12px; border: 1px solid #c1685d; color: #9c3025; background: transparent; }
.avatar-editor { display: flex; align-items: center; gap: 18px; padding: 20px 22px; background: #f5f1e8; }
.avatar-preview { display: grid; place-items: center; width: 76px; height: 76px; overflow: hidden; border-radius: 50%; color: #756d60; background: #d9cfbd; background-size: cover; background-position: center; }
.upload-button { min-height: 38px; padding: 0 14px; font-size: 12px; cursor: pointer; }
.avatar-editor p { margin: 7px 0 0; color: #847c6f; font-size: 11px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; padding: 22px; }
.form-grid label { display: grid; gap: 6px; color: #60594f; font-size: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.form-grid input, .form-grid select, .form-grid textarea, .custom-row input { width: 100%; border: 1px solid #c9bfad; border-radius: 0; color: var(--ink); background: #fff; outline: 0; }
.form-grid input, .form-grid select { min-height: 42px; padding: 0 10px; }
.form-grid textarea { padding: 10px; resize: vertical; }
.form-grid input:focus, .form-grid select:focus, .form-grid textarea:focus { border-color: var(--gold); }
.custom-section { padding: 0 22px 20px; }
.custom-section .panel-title { padding-top: 20px; border-top: 1px solid #e1dacf; }
.custom-section button { border: 1px solid #a69a87; padding: 7px 10px; background: transparent; }
#customFields { display: grid; gap: 8px; margin-top: 12px; }
.custom-row { display: grid; grid-template-columns: 160px 1fr 38px; gap: 8px; }
.custom-row input { min-height: 40px; padding: 0 10px; }
.custom-row button { color: #992d23; border-color: #d7a59e; }
.sticky-save { position: sticky; bottom: 0; display: flex; justify-content: flex-end; align-items: center; gap: 16px; padding: 14px 22px; border-top: 1px solid #d7cfc0; background: rgba(255,255,255,.94); backdrop-filter: blur(12px); }
#saveStatus { margin-right: auto; color: var(--forest); font-size: 12px; }

@media (min-width: 760px) {
    .topbar, .toolbar, .tree-heading, .gesture-hint { max-width: 1180px; margin-left: auto; margin-right: auto; }
    .intro-band { padding-left: max(24px, calc((100vw - 1136px) / 2)); padding-right: max(24px, calc((100vw - 1136px) / 2)); }
    .intro-band h2 { font-size: 46px; }
    .profile-sheet { margin: auto; max-height: 86vh; }
}

@media (max-width: 760px) {
    .admin-layout { display: block; }
    .people-panel { border-right: 0; border-bottom: 1px solid #d4ccbd; }
    .people-list { display: flex; max-height: none; overflow-x: auto; }
    .people-list button { min-width: 190px; }
    .editor-panel { padding: 12px; }
    .family-settings { align-items: stretch; }
    .form-grid { grid-template-columns: 1fr; padding: 18px; }
    .form-grid .full { grid-column: auto; }
    .custom-section { padding-left: 18px; padding-right: 18px; }
    .custom-row { grid-template-columns: 110px 1fr 38px; }
    .admin-header { padding: 14px; }
    .admin-header h1 { font-size: 20px; }
}

@media (max-width: 640px) {
    .topbar { min-height: 64px; padding-left: 14px; padding-right: 14px; }
    .brand-mark { width: 40px; height: 40px; flex-basis: 40px; font-size: 20px; }
    .brand-copy h1 { font-size: 18px; }
    .icon-button { padding: 7px 9px; }
    .intro-band { min-height: 176px; padding: 27px 18px 30px; }
    .intro-band h2 { font-size: 29px; }
    .seal { width: 64px; height: 64px; flex: 0 0 64px; }
    .seal span { font-size: 19px; }
    .toolbar { padding: 14px; }
    .generation-filter { padding-bottom: 2px; }
    .tree-section { padding-top: 20px; }
    .tree-heading { padding-left: 14px; padding-right: 14px; }
    .tree-viewport { overflow-x: hidden; padding: 10px 18px 18px; }
    .tree-canvas { min-width: 0; }
    .person-node { min-height: 78px; gap: 8px; padding: 8px; }
    .family-unit.couple { border-width: 2px; border-color: rgba(173, 60, 46, .55); }
    .avatar-wrap { width: 48px; height: 48px; flex-basis: 48px; }
    .avatar-fallback { font-size: 18px; }
    .node-copy strong { font-size: 15px; }
    .node-copy small, .node-copy em { font-size: 10px; }
    .generation-label.mobile {
        left: 50%; z-index: 3; width: 92px; min-height: 28px; padding: 6px 8px;
        color: #fff; background: var(--vermillion); border: 1px solid #862d23;
        box-shadow: 0 6px 16px rgba(113, 48, 39, .18); text-align: center;
        font-family: "Songti SC", serif; letter-spacing: 0;
        writing-mode: horizontal-tb; transform: translateX(-50%);
    }
    .tree-line { stroke-width: 2.4; }
    .tree-line.spouse { stroke-width: 2; }
    .mobile-family-canvas .tree-lines { display: none; }
    .gesture-hint { margin-top: 0; }
    .profile-sheet { width: 100%; max-height: 91vh; }
    .profile-hero { min-height: 160px; padding: 50px 18px 20px; }
    .profile-avatar { width: 78px; height: 78px; }
    .profile-hero h2 { font-size: 26px; }
    .profile-body { padding-left: 16px; padding-right: 16px; }
    .relations-list { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 430px) {
    .auth-screen { padding: 16px; }
    .auth-panel { padding: 26px 22px; }
    .intro-band h2 { font-size: 30px; }
    .seal { width: 70px; height: 70px; }
    .tree-heading { align-items: start; }
    .tree-heading p { max-width: 120px; }
    .profile-facts { grid-template-columns: 1fr 1fr; }
    .relations-list { grid-template-columns: 1fr; }
    .relative-button { min-height: 64px; }
    .mobile-household-members { gap: 6px; }
    .mobile-family-children { margin-left: 8px; padding-left: 12px; }
    .mobile-family-children > .mobile-family-branch::before { left: -12px; width: 12px; }
}
