/*
 * 族谱管理系统 — self-hosted design system
 * A warm "heritage" theme with full light/dark support. No external CDNs.
 */

/* ---------- Design tokens ---------- */
:root {
  color-scheme: light;
  --bg: #f6f2ea;
  --bg-pattern: #efe9dd;
  --surface: #fffdf9;
  --surface-2: #f3eee4;
  --surface-3: #ece5d7;
  --ink: #2c2621;
  --ink-soft: #6d6459;
  --ink-faint: #9c9284;
  --border: #e6ddce;
  --border-strong: #d8ccb9;

  --accent: #b23a30;          /* seal red 印章红 */
  --accent-hover: #9a2f27;
  --accent-ink: #ffffff;
  --accent-soft: #f8e7e2;
  --accent-soft-ink: #8f2b23;

  --gold: #b8862f;            /* generations / lineage */
  --gold-soft: #f6ecd6;
  --gold-soft-ink: #8a6420;

  --male: #3f6f9e;
  --male-soft: #e5eef6;
  --female: #bb5b8b;
  --female-soft: #f7e8f0;

  --success: #3c8a5c;
  --success-soft: #e4f2e9;
  --warning: #c07a26;
  --warning-soft: #f9ecd8;
  --danger: #b23a30;

  --shadow-sm: 0 1px 2px rgba(44, 38, 33, .06), 0 1px 3px rgba(44, 38, 33, .04);
  --shadow-md: 0 4px 12px rgba(44, 38, 33, .08), 0 2px 4px rgba(44, 38, 33, .04);
  --shadow-lg: 0 12px 32px rgba(44, 38, 33, .12);

  --radius: 14px;
  --radius-sm: 9px;
  --radius-lg: 20px;
  --maxw: 1180px;

  --font: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC",
    "Songti SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #17140f;
    --bg-pattern: #1d1913;
    --surface: #221e18;
    --surface-2: #2a251e;
    --surface-3: #332d25;
    --ink: #ece4d6;
    --ink-soft: #b2a893;
    --ink-faint: #8a8072;
    --border: #373026;
    --border-strong: #473e31;

    --accent: #db6a5f;
    --accent-hover: #e57c72;
    --accent-ink: #1a1410;
    --accent-soft: #3a2622;
    --accent-soft-ink: #eaa79f;

    --gold: #d0a44e;
    --gold-soft: #33291a;
    --gold-soft-ink: #e0bd74;

    --male: #6fa0cc;
    --male-soft: #22303d;
    --female: #d488b3;
    --female-soft: #382634;

    --success: #62b183;
    --success-soft: #22332a;
    --warning: #d99a4e;
    --warning-soft: #35291a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-md: 0 4px 14px rgba(0, 0, 0, .45);
    --shadow-lg: 0 14px 36px rgba(0, 0, 0, .55);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #17140f;
  --bg-pattern: #1d1913;
  --surface: #221e18;
  --surface-2: #2a251e;
  --surface-3: #332d25;
  --ink: #ece4d6;
  --ink-soft: #b2a893;
  --ink-faint: #8a8072;
  --border: #373026;
  --border-strong: #473e31;

  --accent: #db6a5f;
  --accent-hover: #e57c72;
  --accent-ink: #1a1410;
  --accent-soft: #3a2622;
  --accent-soft-ink: #eaa79f;

  --gold: #d0a44e;
  --gold-soft: #33291a;
  --gold-soft-ink: #e0bd74;

  --male: #6fa0cc;
  --male-soft: #22303d;
  --female: #d488b3;
  --female-soft: #382634;

  --success: #62b183;
  --success-soft: #22332a;
  --warning: #d99a4e;
  --warning-soft: #35291a;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-md: 0 4px 14px rgba(0, 0, 0, .45);
  --shadow-lg: 0 14px 36px rgba(0, 0, 0, .55);
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background-color: var(--bg);
  background-image:
    radial-gradient(circle at 1px 1px, var(--bg-pattern) 1px, transparent 0);
  background-size: 22px 22px;
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; transition: color .15s; }
a:hover { color: var(--accent-hover); }

/* Icons: inherit color, sensible default size, overridable per context. */
.ic { width: 18px; height: 18px; flex: none; display: inline-block; vertical-align: -0.18em; }

h1, h2, h3, h4 { color: var(--ink); font-weight: 700; line-height: 1.25; margin: 0; }

img { max-width: 100%; display: block; }

.container { width: 100%; max-width: var(--maxw); margin: 0 auto; padding: 0 20px; }

.page { padding: 32px 0 56px; flex: 1 0 auto; }

.muted { color: var(--ink-soft); }
.faint { color: var(--ink-faint); }
.small { font-size: .82rem; }
.center { text-align: center; }

/* ---------- Navbar ---------- */
.nav {
  position: sticky; top: 0; z-index: 50;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(160%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.nav-inner { display: flex; align-items: center; justify-content: space-between; height: 62px; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; font-size: 1.18rem; font-weight: 800; color: var(--ink); letter-spacing: .3px; }
.brand:hover { color: var(--ink); }
.brand > span:not(.seal) { white-space: nowrap; }
.brand .seal {
  width: 34px; height: 34px; border-radius: 9px; flex: none;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  color: var(--accent-ink); font-weight: 800; font-size: 1.05rem;
  box-shadow: var(--shadow-sm);
}
.nav-links { display: flex; align-items: center; gap: 4px; }
.nav-link {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 13px; border-radius: 10px; color: var(--ink-soft);
  font-size: .93rem; font-weight: 600; transition: all .15s;
}
.nav-link:hover { color: var(--ink); background: var(--surface-2); }
.nav-link.active { color: var(--accent); background: var(--accent-soft); }
.nav-link svg { width: 17px; height: 17px; }
.nav-right { display: flex; align-items: center; gap: 12px; }
.nav-user { color: var(--ink-soft); font-size: .9rem; display: inline-flex; align-items: center; gap: 6px; }

.nav-search { position: relative; }
.nav-search input {
  width: 190px; padding: 8px 12px 8px 32px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='15' height='15' fill='none' stroke='%239c9284' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='6.5' cy='6.5' r='4.5'/%3E%3Cpath d='m14 14-4-4'/%3E%3C/svg%3E") no-repeat 10px center;
  color: var(--ink); font-size: .88rem; font-family: inherit; transition: all .15s;
}
.nav-search input:focus { outline: none; border-color: var(--accent); background-color: var(--surface); width: 230px; }
.search-dropdown {
  position: absolute; top: 46px; right: 0; width: 320px; max-height: 400px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-lg);
  padding: 6px; display: none; z-index: 60;
}
.search-dropdown.open { display: block; }
.search-item { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--ink); }
.search-item:hover { background: var(--surface-2); color: var(--ink); }
.search-item .avatar { width: 30px; height: 30px; font-size: .8rem; }
.search-group-label { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: var(--ink-faint); padding: 8px 10px 4px; }
.search-empty { padding: 16px; text-align: center; color: var(--ink-faint); font-size: .85rem; }
@media (max-width: 920px) { .nav-search { display: none; } }

.nav-toggle, .theme-toggle {
  display: grid; place-items: center; width: 40px; height: 40px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  color: var(--ink-soft); cursor: pointer; transition: all .15s;
}
.nav-toggle:hover, .theme-toggle:hover { color: var(--ink); border-color: var(--border-strong); }
.nav-toggle { display: none; }
.theme-toggle svg { width: 19px; height: 19px; }
.theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

@media (max-width: 800px) {
  .nav-toggle { display: grid; }
  .nav-links {
    position: absolute; top: 62px; left: 0; right: 0;
    flex-direction: column; align-items: stretch; gap: 2px;
    background: var(--surface); border-bottom: 1px solid var(--border);
    padding: 10px 16px 16px; box-shadow: var(--shadow-md);
    display: none;
  }
  .nav.open .nav-links { display: flex; }
  .nav-link { padding: 11px 13px; }
  .nav-user span { display: none; }
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: 10px; font-size: .9rem; font-weight: 600;
  border: 1px solid transparent; cursor: pointer; transition: all .15s;
  white-space: nowrap; line-height: 1;
}
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--accent-hover); color: var(--accent-ink); transform: translateY(-1px); box-shadow: var(--shadow-md); }
.btn-outline { background: var(--surface); color: var(--ink); border-color: var(--border-strong); }
.btn-outline:hover { color: var(--ink); background: var(--surface-2); border-color: var(--ink-faint); }
.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-ghost:hover { color: var(--ink); background: var(--surface-2); }
.btn-sm { padding: 6px 12px; font-size: .82rem; }
.btn-gold { background: var(--gold); color: #fff; }
.btn-gold:hover { color: #fff; filter: brightness(.95); }

/* ---------- Cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.card-pad { padding: 22px; }
.card-hover { transition: transform .18s, box-shadow .18s, border-color .18s; }
.card-hover:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); border-color: var(--border-strong); }
.card-head { padding: 16px 22px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.card-head h3 { font-size: 1.05rem; }
.card-foot { padding: 14px 22px; border-top: 1px solid var(--border); background: var(--surface-2); }

/* ---------- Page header ---------- */
.page-head { margin-bottom: 26px; }
.page-title { font-size: 1.7rem; display: flex; align-items: center; gap: 12px; }
.page-title .ttl-icon {
  width: 40px; height: 40px; border-radius: 11px; flex: none; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
}
.page-title .ttl-icon svg { width: 22px; height: 22px; }
.page-sub { color: var(--ink-soft); margin-top: 8px; font-size: .96rem; }
.eyebrow { text-transform: uppercase; letter-spacing: 2px; font-size: .72rem; font-weight: 700; color: var(--gold); margin-bottom: 6px; }

.section-title { font-size: 1.22rem; display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.section-title svg { width: 20px; height: 20px; color: var(--ink-soft); }

/* ---------- Grid ---------- */
.grid { display: grid; gap: 18px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) { .grid-3, .grid-4 { grid-template-columns: repeat(2, 1fr); } }
/* On phones, big content grids collapse to a single column, but the compact
   numeric stat tiles (.grid-4) stay 2-up so they don't waste vertical space. */
@media (max-width: 620px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } .grid { gap: 14px; } }

/* ---------- Stat tiles ---------- */
.stat { display: flex; align-items: center; gap: 15px; padding: 18px 20px; }
.stat-ic { width: 46px; height: 46px; border-radius: 12px; flex: none; display: grid; place-items: center; }
.stat-ic svg { width: 23px; height: 23px; }
.stat-val { font-size: 1.7rem; font-weight: 800; line-height: 1; }
.stat-label { color: var(--ink-soft); font-size: .85rem; margin-top: 5px; }
.tone-red   .stat-ic, .tone-red.stat-ic   { background: var(--accent-soft); color: var(--accent); }
.tone-gold  .stat-ic, .tone-gold.stat-ic  { background: var(--gold-soft); color: var(--gold-soft-ink); }
.tone-blue  .stat-ic, .tone-blue.stat-ic  { background: var(--male-soft); color: var(--male); }
.tone-green .stat-ic, .tone-green.stat-ic { background: var(--success-soft); color: var(--success); }
.tone-rose  .stat-ic, .tone-rose.stat-ic  { background: var(--female-soft); color: var(--female); }
@media (max-width: 560px) {
  .stat { padding: 14px; gap: 11px; }
  .stat-ic { width: 40px; height: 40px; border-radius: 11px; }
  .stat-ic svg { width: 20px; height: 20px; }
  .stat-val { font-size: 1.42rem; }
  .stat-label { font-size: .8rem; margin-top: 3px; }
}

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 10px; border-radius: 999px; font-size: .76rem; font-weight: 600;
  background: var(--surface-3); color: var(--ink-soft); white-space: nowrap;
}
.badge svg { width: 12px; height: 12px; }
.badge-red { background: var(--accent-soft); color: var(--accent-soft-ink); }
.badge-gold { background: var(--gold-soft); color: var(--gold-soft-ink); }
.badge-blue { background: var(--male-soft); color: var(--male); }
.badge-rose { background: var(--female-soft); color: var(--female); }
.badge-gray { background: var(--surface-3); color: var(--ink-soft); }
.badge-green { background: var(--success-soft); color: var(--success); }
.badge-warn { background: var(--warning-soft); color: var(--warning); }

/* ---------- Avatars ---------- */
.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700; font-size: 1.05rem;
  color: #fff; object-fit: cover; overflow: hidden;
}
.avatar.male { background: linear-gradient(135deg, var(--male), #345c86); }
.avatar.female { background: linear-gradient(135deg, var(--female), #9c4674); }
.avatar-lg { width: 76px; height: 76px; font-size: 1.7rem; }
.avatar-sm { width: 34px; height: 34px; font-size: .85rem; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.tbl th {
  text-align: left; padding: 12px 16px; font-size: .76rem; text-transform: uppercase;
  letter-spacing: .6px; color: var(--ink-faint); font-weight: 700;
  background: var(--surface-2); border-bottom: 1px solid var(--border);
}
table.tbl td { padding: 13px 16px; border-bottom: 1px solid var(--border); color: var(--ink); }
table.tbl tbody tr { transition: background .12s; }
table.tbl tbody tr:hover { background: var(--surface-2); }
table.tbl tbody tr:last-child td { border-bottom: none; }

/* Responsive tables: on phones a `.tbl-stack` table drops its header and turns
   each row into a stacked card. Cells with a data-label show "LABEL  value";
   the primary cell reads as the card's heading, the actions cell right-aligns. */
@media (max-width: 560px) {
  table.tbl.tbl-stack thead { display: none; }
  .tbl-stack tbody tr { display: block; padding: 13px 16px 12px; }
  .tbl-stack tbody td {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 4px 0; border: none; font-size: .9rem;
  }
  .tbl-stack tbody td[data-label]::before {
    content: attr(data-label); flex: none;
    color: var(--ink-faint); font-size: .72rem; font-weight: 700;
    text-transform: uppercase; letter-spacing: .5px;
  }
  .tbl-stack tbody td.td-primary {
    justify-content: flex-start; font-size: 1rem;
    padding-bottom: 9px; margin-bottom: 4px; border-bottom: 1px dashed var(--border);
  }
  .tbl-stack tbody td.td-actions { justify-content: flex-end; gap: 4px; padding-top: 8px; }
  .tbl-stack tbody td.td-actions .btn-sm { padding: 7px 12px; }
}

/* ---------- List rows ---------- */
.list { display: flex; flex-direction: column; }
.list-row { display: flex; align-items: center; gap: 14px; padding: 15px 20px; border-bottom: 1px solid var(--border); transition: background .12s; }
.list-row:last-child { border-bottom: none; }
.list-row:hover { background: var(--surface-2); }
.list-ic { width: 38px; height: 38px; border-radius: 10px; flex: none; display: grid; place-items: center; }
.list-ic svg { width: 19px; height: 19px; }
.grow { flex: 1 1 auto; min-width: 0; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Empty state ---------- */
.empty { text-align: center; padding: 52px 24px; color: var(--ink-faint); }
.empty svg { width: 48px; height: 48px; margin: 0 auto 14px; opacity: .5; }
.empty p { margin: 4px 0; }
.empty .empty-title { color: var(--ink-soft); font-weight: 600; font-size: 1.02rem; }

/* ---------- Definition list ---------- */
.dl { display: grid; gap: 1px; background: var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.dl > div { display: grid; grid-template-columns: 130px 1fr; gap: 12px; padding: 14px 18px; background: var(--surface); }
.dl dt { color: var(--ink-soft); font-size: .88rem; font-weight: 600; }
.dl dd { margin: 0; color: var(--ink); font-size: .92rem; }
@media (max-width: 520px) { .dl > div { grid-template-columns: 1fr; gap: 2px; } }

/* ---------- Family tree (SVG) ---------- */
.tree-wrap { position: relative; width: 100%; height: 560px; overflow: hidden; padding: 0; touch-action: none; background:
  radial-gradient(circle at 1px 1px, var(--border) 1px, transparent 0) 0 0 / 22px 22px; }
.tree-wrap.fullscreen { position: fixed; inset: 0; z-index: 100; height: 100vh; background-color: var(--bg); }
.tree-wrap svg.tree-svg { display: block; width: 100%; height: 100%; cursor: grab; }
.tree-wrap svg.tree-svg.grabbing { cursor: grabbing; }
.tree-toolbar {
  position: absolute; top: 12px; right: 12px; z-index: 5;
  display: flex; flex-direction: column; gap: 6px;
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  border: 1px solid var(--border); border-radius: 12px; padding: 6px; box-shadow: var(--shadow-sm);
  backdrop-filter: blur(6px);
}
.tree-toolbar button {
  display: grid; place-items: center; width: 34px; height: 34px;
  border: none; border-radius: 8px; background: transparent; color: var(--ink-soft); cursor: pointer; transition: all .12s;
}
.tree-toolbar button:hover { background: var(--surface-2); color: var(--accent); }
.tree-toolbar button svg { width: 18px; height: 18px; }
.tree-toolbar .sep { height: 1px; background: var(--border); margin: 1px 4px; }
.tree-hint { position: absolute; left: 12px; bottom: 10px; z-index: 5; font-size: .76rem; color: var(--ink-faint); pointer-events: none; }

.tree-toggle { cursor: pointer; }
.tree-toggle circle { fill: var(--surface); stroke: var(--gold); stroke-width: 1.5; transition: fill .12s; }
.tree-toggle:hover circle { fill: var(--gold-soft); }
.tree-toggle text { fill: var(--gold-soft-ink); font-size: 13px; font-weight: 700; text-anchor: middle; }
.tree-hidden-badge { fill: var(--gold-soft-ink); font-size: 10px; font-weight: 700; text-anchor: middle; }
.tree-node.focused .tree-card-bg { stroke: var(--accent); stroke-width: 2.5; }
.tree-node { cursor: pointer; }
.tree-card-bg { fill: var(--surface); stroke: var(--border-strong); stroke-width: 1.5; transition: stroke .15s; }
.tree-node:hover .tree-card-bg { stroke: var(--accent); }
.tree-card-bg.male { fill: var(--male-soft); }
.tree-card-bg.female { fill: var(--female-soft); }
.tree-name { fill: var(--ink); font-weight: 700; font-size: 14px; }
.tree-meta { fill: var(--ink-soft); font-size: 11px; }
/* Circular node avatar (coloured initial + optional photo overlay). */
.tree-av-bg { stroke: var(--surface); stroke-width: 1.5; }
.tree-av-bg.male { fill: var(--male); }
.tree-av-bg.female { fill: var(--female); }
.tree-av-ini { fill: #fff; font-size: 14px; font-weight: 700; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.tree-link { fill: none; stroke: var(--border-strong); stroke-width: 2; }
.tree-link.spouse { stroke: var(--accent); stroke-dasharray: 4 3; }
/* Node context menu (right-click / long-press). */
.tree-menu {
  position: fixed; z-index: 300; min-width: 172px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow-lg); animation: treeMenuIn .12s ease;
}
@keyframes treeMenuIn { from { opacity: 0; transform: scale(.96); } to { opacity: 1; transform: none; } }
.tree-menu-head { padding: 6px 12px 8px; font-weight: 800; font-size: .92rem; color: var(--ink); border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.tree-menu-head .tree-menu-sub { display: block; margin-top: 2px; font-weight: 600; font-size: .74rem; color: var(--ink-faint); }
.tree-menu-sep { border: none; border-top: 1px solid var(--border); margin: 4px 6px; }
.tree-menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 12px;
  border: none; background: none; border-radius: 8px; cursor: pointer; text-decoration: none;
  font-size: .9rem; font-weight: 600; color: var(--ink-soft); font-family: inherit; text-align: left;
}
.tree-menu-item:hover { background: var(--surface-2); color: var(--ink); }
.tree-menu-item svg { width: 16px; height: 16px; flex: none; }
.tree-menu-item.danger { color: var(--danger); }
.tree-menu-item.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }

/* ============ In-context editor: quick-add + in-place edit ============ */
body.tm-open { overflow: hidden; }
.tm-overlay {
  position: fixed; inset: 0; z-index: 360; display: grid; place-items: center; padding: 20px;
  background: color-mix(in srgb, #000 46%, transparent); backdrop-filter: blur(2px);
  animation: tmFade .16s ease;
}
@keyframes tmFade { from { opacity: 0; } to { opacity: 1; } }
.tm-modal {
  width: 100%; max-width: 400px; max-height: min(90vh, 760px);
  display: flex; flex-direction: column; background: var(--surface);
  border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow-lg);
  overflow: hidden; animation: tmPop .18s cubic-bezier(.2,.9,.3,1.2);
}
.tm-modal.wide { max-width: 620px; }
@keyframes tmPop { from { opacity: 0; transform: translateY(10px) scale(.97); } to { opacity: 1; transform: none; } }

.tm-head { padding: 18px 20px; border-bottom: 1px solid var(--border); flex: none; }
.tm-eyebrow { display: flex; align-items: center; gap: 7px; font-size: .82rem; font-weight: 700; color: var(--accent); margin-bottom: 14px; }
.tm-eyebrow svg { width: 16px; height: 16px; }

/* Relationship strip (add modal hero). */
.tm-relate { display: flex; align-items: center; gap: 10px; }
.tm-rp { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.tm-rp .avatar { box-shadow: 0 2px 6px rgba(0,0,0,.14); }
.tm-rp-t { min-width: 0; }
.tm-rp-t b { display: block; font-size: .96rem; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tm-rp-t span { display: block; font-size: .76rem; color: var(--ink-faint); margin-top: 1px; }
.avatar.dash { background: var(--surface-2); border: 2px dashed var(--border-strong); color: var(--ink-faint); box-shadow: none; }
.tm-rel-link { display: flex; flex-direction: column; align-items: center; flex: none; color: var(--border-strong); }
.tm-rel-badge { font-size: .72rem; font-weight: 800; color: var(--accent); background: var(--accent-soft); padding: 3px 9px; border-radius: 20px; white-space: nowrap; }

/* Person header (edit modal). */
.tm-person-head { display: flex; align-items: center; gap: 12px; }
.tm-person-head .avatar { width: 46px; height: 46px; font-size: 1.15rem; }
.tm-ph-t { min-width: 0; }
.tm-ph-t b { display: block; font-size: 1.1rem; font-weight: 800; color: var(--ink); }
.tm-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }

.tm-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; flex: 1; }
.tm-grp { font-size: .78rem; font-weight: 800; letter-spacing: .04em; color: var(--ink-faint); text-transform: none; margin: 4px 0 -2px; }
.tm-grp:first-child { margin-top: 0; }
.tm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.tm-field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.tm-field.full { grid-column: 1 / -1; }
.tm-field > span { display: flex; align-items: baseline; gap: 5px; font-size: .8rem; font-weight: 700; color: var(--ink-soft); }
.tm-field > span i { color: var(--accent); font-style: normal; }
.tm-field > span .faint { font-weight: 500; }
.tm-input, .tm-gender { width: 100%; }
.tm-input {
  height: 40px; padding: 0 12px; border: 1px solid var(--border-strong); border-radius: 10px;
  background: var(--surface); color: var(--ink); font-size: .93rem; font-family: inherit;
}
textarea.tm-input { height: auto; padding: 9px 12px; line-height: 1.5; resize: vertical; }
input[type="file"].tm-input { height: auto; padding: 8px 12px; font-size: .85rem; color: var(--ink-soft); }
.tm-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
select.tm-input { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%239c9284' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4 6 4 4 4-4'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px; }

.tm-gender { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tm-gender button {
  display: flex; align-items: center; justify-content: center; gap: 7px; height: 40px;
  border: 1px solid var(--border-strong); border-radius: 10px; background: var(--surface);
  color: var(--ink-soft); font-weight: 700; font-size: .92rem; font-family: inherit; cursor: pointer; transition: all .12s;
}
.tm-gender button .tm-dot { width: 9px; height: 9px; border-radius: 50%; }
.tm-dot.male { background: var(--male); }
.tm-dot.female { background: var(--female); }
.tm-gender button.on[data-g="M"], .tm-gender[data-gender="M"] button[data-g="M"].on { border-color: var(--male); background: var(--male-soft); color: var(--male); }
.tm-gender button.on[data-g="F"], .tm-gender[data-gender="F"] button[data-g="F"].on { border-color: var(--female); background: var(--female-soft); color: var(--female); }

.tm-note { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); color: var(--ink-soft); font-size: .82rem; font-weight: 500; }
.tm-note svg { width: 15px; height: 15px; flex: none; color: var(--accent); }
.tm-error { min-height: 0; font-size: .82rem; font-weight: 600; color: var(--danger); }
.tm-error:not(:empty) { min-height: 1.1em; margin-top: -4px; }
.tm-error .tm-link, .tm-link { color: var(--accent); font-weight: 700; }
.tm-loading { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 40px 0; color: var(--ink-soft); font-weight: 600; }

.tm-foot { display: flex; align-items: center; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); flex: none; }
.tm-foot:empty { display: none; }
.tm-foot-l { margin-right: auto; }
.tm-foot-r { display: flex; gap: 8px; margin-left: auto; }
.btn.tm-danger { color: var(--danger); }
.btn.tm-danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }

/* Save button spinner. */
.tm-save.loading { color: transparent; position: relative; pointer-events: none; }
.tm-save.loading svg, .tm-save.loading span { visibility: hidden; }
.tm-save.loading::after {
  content: ""; position: absolute; inset: 0; margin: auto; width: 16px; height: 16px;
  border: 2px solid rgba(255,255,255,.5); border-top-color: #fff; border-radius: 50%; animation: tmSpin .6s linear infinite;
}
.tm-spin { width: 16px; height: 16px; border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%; display: inline-block; animation: tmSpin .6s linear infinite; }
@keyframes tmSpin { to { transform: rotate(360deg); } }

/* Member detail panel actions. */
.member-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; }

@media (max-width: 560px) {
  .tm-overlay { padding: 0; place-items: end center; }
  .tm-modal, .tm-modal.wide { max-width: 100%; max-height: 92vh; border-radius: 18px 18px 0 0; animation: tmSheet .22s cubic-bezier(.2,.9,.3,1); }
  .tm-grid { grid-template-columns: 1fr; }
}
@keyframes tmSheet { from { transform: translateY(100%); } to { transform: none; } }

/* Newly-added / edited node pulse (after ?focus). */
.tree-node.just-added .tree-card-bg { animation: nodePulse 1.8s ease; }
@keyframes nodePulse {
  0%, 100% { stroke: var(--border-strong); stroke-width: 1.5; }
  20%, 60% { stroke: var(--accent); stroke-width: 3; }
}

/* Inline rename input (double-click a node). */
.tree-edit {
  position: fixed; z-index: 350; height: 32px; padding: 2px 8px;
  border: 2px solid var(--accent); border-radius: 8px; background: var(--surface);
  color: var(--ink); font-size: 14px; font-weight: 700; font-family: inherit; box-shadow: var(--shadow-md);
}
.tree-edit:focus { outline: none; box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow-md); }

/* Transient toast (save feedback). */
.tree-toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 10px); z-index: 400;
  background: var(--ink); color: var(--surface); padding: 10px 18px; border-radius: 10px;
  font-size: .9rem; font-weight: 600; box-shadow: var(--shadow-lg);
  opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; max-width: 90vw;
}
.tree-toast.show { opacity: 1; transform: translate(-50%, 0); }
.tree-toast.ok { background: var(--success); color: #fff; }
.tree-toast.warn { background: var(--warning); color: #fff; }

/* Front-of-house inline editing (public/assets/js/inline-edit.js). Editable
   cells are inert until the viewer flips the 快速编辑 toggle, which adds .ie-on
   to the container; only then do fields show an affordance and accept clicks. */
.ie-value { display: inline; }
.ie-on .ie-field {
  cursor: text; border-radius: 7px;
  box-shadow: inset 0 0 0 1px var(--border-strong);
  transition: box-shadow .15s, background .15s;
}
.ie-on .ie-field:hover { background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.ie-on dd.ie-field { padding: 3px 8px; margin: -3px -8px; }
.ie-on .ie-inline { padding: 1px 6px; margin: 0 -6px; display: inline-block; }
.ie-on .ie-field .ie-value { border-bottom: 1px dashed var(--border-strong); }
.ie-on .ie-field:hover .ie-value { border-bottom-color: transparent; }
.ie-input { width: 100%; min-width: 8rem; margin: 0; font: inherit; }
textarea.ie-input { resize: vertical; min-height: 5.5rem; line-height: 1.7; }

.tree-legend { display: flex; flex-wrap: wrap; gap: 16px; padding: 4px 6px 0; color: var(--ink-soft); font-size: .82rem; }
.tree-legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 12px; height: 12px; border-radius: 4px; display: inline-block; }
.dot.male { background: var(--male); }
.dot.female { background: var(--female); }
.line-dash { width: 18px; height: 0; border-top: 2px dashed var(--accent); display: inline-block; }

/* generation bands used behind the tree */
.gen-tag {
  position: sticky; left: 0;
  font-size: .8rem; font-weight: 700; color: var(--gold-soft-ink);
  background: var(--gold-soft); padding: 3px 10px; border-radius: 999px;
}

/* ---------- Timeline ---------- */
.timeline { position: relative; padding-left: 28px; }
.timeline::before { content: ""; position: absolute; left: 8px; top: 6px; bottom: 6px; width: 2px; background: var(--border); }
.tl-item { position: relative; padding: 0 0 22px; }
.tl-item:last-child { padding-bottom: 0; }
.tl-item::before {
  content: ""; position: absolute; left: -24px; top: 5px; width: 12px; height: 12px;
  border-radius: 50%; background: var(--accent); border: 2px solid var(--surface); box-shadow: 0 0 0 2px var(--accent-soft);
}
.tl-item.female::before { background: var(--female); box-shadow: 0 0 0 2px var(--female-soft); }
.tl-item.birth::before { background: var(--success); box-shadow: 0 0 0 2px var(--success-soft); }
.tl-item.marriage::before { background: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.tl-item.death::before { background: var(--ink-faint); box-shadow: 0 0 0 2px var(--surface-3); }
.tl-date { font-size: .8rem; color: var(--gold-soft-ink); font-weight: 700; }
.tl-title { font-weight: 600; margin-top: 2px; }
.tl-sub { color: var(--ink-soft); font-size: .86rem; }
.tl-kind { display: inline-flex; align-items: center; gap: 4px; font-size: .7rem; font-weight: 700; padding: 1px 8px; border-radius: 999px; margin-left: 8px; vertical-align: middle; }
.tl-kind svg { width: 11px; height: 11px; }
.tl-kind.birth { background: var(--success-soft); color: var(--success); }
.tl-kind.marriage { background: var(--accent-soft); color: var(--accent-soft-ink); }
.tl-kind.death { background: var(--surface-3); color: var(--ink-soft); }

/* ---------- 世系录 / Register book ---------- */
.reg-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.book {
  --serif: "Songti SC", "STSong", "STZhongsong", "SimSun", "Noto Serif SC", serif;
  max-width: 760px; margin: 0 auto; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-md);
  padding: 48px 52px; font-family: var(--serif); color: var(--ink); line-height: 2;
}
.book-cover { text-align: center; padding: 20px 0 38px; border-bottom: 3px double var(--border); margin-bottom: 34px; }
.book-seal {
  width: 88px; height: 88px; margin: 0 auto 22px; border-radius: 14px; display: grid; place-items: center;
  font-size: 3rem; font-weight: 800; color: #fff; font-family: var(--serif);
  background: linear-gradient(135deg, var(--accent), var(--accent-hover)); box-shadow: var(--shadow-md);
}
.book-title { font-family: var(--serif); font-size: 2.2rem; letter-spacing: 6px; font-weight: 800; }
.book-rule { width: 60px; height: 3px; background: var(--accent); margin: 16px auto; border-radius: 2px; }
.book-origin { color: var(--ink-soft); font-size: 1rem; letter-spacing: 2px; }
.book-poem { color: var(--gold-soft-ink); font-size: 1rem; margin-top: 10px; letter-spacing: 3px; }
.book-meta { color: var(--ink-faint); font-size: .9rem; margin-top: 14px; letter-spacing: 1px; }
.book-preface { max-width: 34em; margin: 22px auto 0; text-align: justify; text-indent: 2em; color: var(--ink-soft); font-size: .98rem; line-height: 2; }

.reg-gen { margin-bottom: 28px; }
.reg-gen-head {
  display: flex; align-items: baseline; gap: 14px; padding: 8px 16px; margin-bottom: 14px;
  border-left: 4px solid var(--accent); background: var(--accent-soft); border-radius: 0 8px 8px 0;
}
.reg-gen-ord { font-size: 1.35rem; font-weight: 800; color: var(--accent-soft-ink); letter-spacing: 2px; }
.reg-gen-zi { color: var(--gold-soft-ink); font-size: .95rem; }
.reg-gen-count { margin-left: auto; color: var(--ink-faint); font-size: .85rem; font-family: var(--font); }

.reg-entry { padding: 12px 2px 14px; border-bottom: 1px dotted var(--border); }
.reg-entry:last-child { border-bottom: none; }
.reg-entry.has-portrait { display: flex; gap: 16px; align-items: flex-start; }
.reg-body { flex: 1; min-width: 0; }
.reg-portrait { width: 62px; height: 78px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); flex: none; box-shadow: var(--shadow-sm); }
.reg-name { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 3px; }
.reg-name > a { font-size: 1.28rem; font-weight: 800; color: var(--ink); letter-spacing: 2px; }
.reg-name > a:hover { color: var(--accent); }
.reg-seq {
  min-width: 26px; height: 24px; padding: 0 7px; border-radius: 7px; display: inline-grid; place-items: center;
  font-size: .8rem; font-weight: 700; background: var(--surface-3); color: var(--ink-soft); font-family: var(--font);
}
.reg-sex { font-size: .76rem; padding: 1px 8px; border-radius: 999px; font-family: var(--font); }
.reg-sex.m { background: var(--male-soft); color: var(--male); }
.reg-sex.f { background: var(--female-soft); color: var(--female); }
.reg-zi { font-size: .82rem; color: var(--gold-soft-ink); font-family: var(--font); }
.reg-prose { margin: 0; text-align: justify; font-size: 1.02rem; line-height: 2.05; }
.reg-prose a { color: var(--accent); font-weight: 600; }
.reg-bio { margin: 6px 0 0; text-indent: 2em; color: var(--ink-soft); font-size: .95rem; line-height: 1.9; text-align: justify; }

/* 目录 (table of contents) — one entry per generation, members linked. */
.reg-toc { margin: 0 0 34px; padding: 0 0 28px; border-bottom: 3px double var(--border); }
.reg-toc-title { text-align: center; font-family: var(--serif); font-size: 1.5rem; letter-spacing: 10px; font-weight: 800; margin: 0 0 24px; }
.reg-toc-gen { margin-bottom: 15px; }
.reg-toc-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 5px; }
.reg-toc-head:hover .reg-toc-ord { color: var(--accent); }
.reg-toc-ord { font-family: var(--serif); font-weight: 800; color: var(--accent-soft-ink); letter-spacing: 1px; }
.reg-toc-zi { color: var(--gold-soft-ink); font-size: .86rem; }
.reg-toc-count { margin-left: auto; color: var(--ink-faint); font-size: .82rem; font-family: var(--font); }
.reg-toc-names { display: flex; flex-wrap: wrap; align-items: baseline; gap: 3px 7px; padding-left: 8px; }
.reg-toc-names a { color: var(--ink-soft); }
.reg-toc-names a:hover { color: var(--accent); }
.reg-toc-dot { color: var(--ink-faint); }

/* 竖排 — traditional vertical, right-to-left register body. The cover and TOC
   stay horizontal (title / navigation matter). Each generation becomes a block
   of vertical columns flowing right-to-left; a bounded height lets a long life
   wrap into successive columns, and the block scrolls sideways if it runs wide.
   Standard CSS writing-mode — renders in any modern browser (the headless CJK
   preview used during development cannot lay this out, so it was verified via
   computed styles rather than a screenshot). */
.book-vertical .reg-gen {
  writing-mode: vertical-rl; text-orientation: mixed;
  height: 32em; overflow-x: auto; overflow-y: hidden;
  margin-bottom: 30px; padding-bottom: 8px;
}
.book-vertical .reg-gen-head {
  display: inline-block; vertical-align: top; text-align: center; height: 100%; box-sizing: border-box;
  border-left: none; border-right: 4px solid var(--accent); border-radius: 8px 0 0 8px;
  padding: 16px 12px; margin: 0 0 0 12px; letter-spacing: 3px;
}
.book-vertical .reg-gen-count { display: block; margin: 16px auto 0; }
.book-vertical .reg-entry {
  display: inline-block; vertical-align: top; height: 100%; box-sizing: border-box;
  border-bottom: none; border-left: 1px dotted var(--border); padding: 2px 14px; margin: 0;
}
.book-vertical .reg-entry.has-portrait { display: inline-block; }
.book-vertical .reg-body { height: 100%; }
.book-vertical .reg-portrait { display: none; }               /* photos are a horizontal-mode nicety */
.book-vertical .reg-name { display: block; margin: 0 0 6px; }
.book-vertical .reg-name > a { font-size: 1.32rem; letter-spacing: 3px; }
.book-vertical .reg-seq { display: none; }                     /* column order already conveys sequence */
.book-vertical .reg-sex, .book-vertical .reg-zi { writing-mode: horizontal-tb; display: inline-block; font-size: .72rem; }
.book-vertical .reg-prose { text-indent: 0; margin: 0; }
.book-vertical .reg-bio { margin: 0; color: var(--ink-soft); }

@media (max-width: 620px) {
  .book { padding: 26px 20px; line-height: 1.95; }
  .book-title { font-size: 1.7rem; letter-spacing: 3px; }
  .book-seal { width: 72px; height: 72px; font-size: 2.4rem; }
}

@media print {
  @page { size: A4; margin: 16mm 15mm 18mm; }
  .nav, .footer, .no-print, .modal-overlay, .to-top, .skip-link { display: none !important; }
  html, body { background: #fff !important; }
  .page { padding: 0 !important; }
  .container { max-width: none; padding: 0; }
  .book { max-width: none; border: none; box-shadow: none; border-radius: 0; padding: 0; color: #000; line-height: 1.9; font-size: 11pt; }
  .book-cover { break-after: page; padding-top: 30mm; }
  .reg-toc { break-after: page; }
  .book-seal, .reg-gen-head, .reg-sex, .reg-seq, .reg-portrait { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .reg-gen { break-inside: auto; }
  .reg-gen-head { break-after: avoid; }          /* never leave a gen header alone at page bottom */
  .reg-entry { break-inside: avoid; }            /* keep each person's entry whole */
  .reg-name { break-after: avoid; }
  .reg-prose { orphans: 2; widows: 2; }
  .reg-prose a, .reg-name > a { color: #000 !important; text-decoration: none; }
}

/* ---------- Data completeness ---------- */
.cov-row { display: grid; grid-template-columns: 84px 1fr auto; align-items: center; gap: 14px; padding: 9px 0; }
.cov-label { font-size: .9rem; color: var(--ink-soft); font-weight: 600; }
.cov-track { height: 10px; border-radius: 6px; background: var(--surface-3); overflow: hidden; }
.cov-fill { display: block; height: 100%; border-radius: 6px; background: linear-gradient(90deg, var(--accent), var(--gold)); transition: width .4s; }
.cov-pct { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--ink-soft); font-size: .84rem; min-width: 88px; text-align: right; }
@media (max-width: 480px) { .cov-row { grid-template-columns: 68px 1fr auto; gap: 10px; } .cov-pct { min-width: 74px; } }

/* 字辈谱 chips that drill down to a generation. */
.poem-chip { display: block; transition: transform .12s, box-shadow .12s, border-color .12s; }
.poem-chip:hover { transform: translateY(-2px); box-shadow: var(--shadow-sm); border-color: var(--gold) !important; }

/* ---------- Media wall / timeline ---------- */
.gallery-year { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; }
.gallery-year-num { font-size: 1.3rem; font-weight: 800; color: var(--accent); letter-spacing: 1px; font-variant-numeric: tabular-nums; }
.gallery-year-line { flex: 1; height: 1px; background: var(--border); }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px; }
.gallery-cell { margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; box-shadow: var(--shadow-sm); transition: transform .18s, box-shadow .18s; }
.gallery-cell:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); }
.gallery-cell .gallery-img { width: 100%; aspect-ratio: 4 / 5; object-fit: cover; cursor: zoom-in; display: block; }
.gallery-cap { padding: 10px 12px; }
.gallery-cap-title { display: block; font-weight: 700; font-size: .92rem; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gallery-cap-meta { display: block; color: var(--ink-soft); font-size: .8rem; margin-top: 3px; }
.gallery-cap-meta a { color: var(--ink-soft); }
.gallery-cap-meta a:hover { color: var(--accent); }
.gallery-doc { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; min-height: 180px; text-align: center; padding: 16px; color: var(--ink-soft); }
.gallery-doc svg { width: 34px; height: 34px; }

/* Gallery member + tag filter chips. */
.gallery-filter { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 22px; align-items: center; }
.gallery-tagfilter { margin-top: -10px; }
.gf-label { display: inline-flex; align-items: center; gap: 5px; color: var(--ink-faint); font-size: .82rem; font-weight: 600; margin-right: 2px; }
.gf-label svg { width: 14px; height: 14px; }
.gf-chip { padding: 6px 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--ink-soft); font-size: .88rem; font-weight: 600; cursor: pointer; transition: all .12s; }
.gf-chip:hover { border-color: var(--ink-faint); color: var(--ink); }
.gf-chip.on { background: var(--accent-soft); border-color: transparent; color: var(--accent-soft-ink); }
.gf-count { font-size: .72rem; opacity: .6; margin-left: 1px; }

/* 影像标签 chips (gallery captions + admin media cards). */
.gallery-cap-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.tag-chip { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-soft); font-size: .72rem; font-weight: 600; line-height: 1.6; }

/* Shared lightbox (prev/next). */
.lb-overlay { position: fixed; inset: 0; z-index: 500; background: rgba(20, 16, 12, .88); display: none; align-items: center; justify-content: center; padding: 24px; backdrop-filter: blur(3px); }
.lb-overlay.open { display: flex; }
.lb-figure { margin: 0; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.lb-img { max-width: 92vw; max-height: 80vh; border-radius: 14px; box-shadow: var(--shadow-lg); object-fit: contain; background: #fff; }
.lb-cap { color: rgba(255, 255, 255, .92); font-size: .95rem; text-align: center; font-variant-numeric: tabular-nums; }
.lb-close { position: fixed; top: 16px; right: 20px; width: 44px; height: 44px; border: none; background: rgba(255, 255, 255, .14); color: #fff; font-size: 1.7rem; line-height: 1; border-radius: 50%; cursor: pointer; }
.lb-close:hover { background: rgba(255, 255, 255, .26); }
.lb-nav { position: fixed; top: 50%; transform: translateY(-50%); width: 52px; height: 52px; border: none; background: rgba(255, 255, 255, .14); color: #fff; font-size: 2rem; line-height: 1; border-radius: 50%; cursor: pointer; display: grid; place-items: center; }
.lb-nav:hover { background: rgba(255, 255, 255, .26); }
.lb-prev { left: 20px; }
.lb-next { right: 20px; }
@media (max-width: 560px) {
  .lb-nav { width: 46px; height: 46px; top: auto; bottom: 24px; transform: none; }
}

/* ---------- Kinship relationship path ---------- */
.kin-path-cap { margin-top: 16px; font-size: .78rem; font-weight: 700; letter-spacing: 1px; color: var(--ink-faint); text-transform: uppercase; }
.kin-path { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 4px; margin-top: 8px; }
.kin-node {
  display: inline-flex; align-items: center; padding: 7px 15px; border-radius: 999px;
  font-weight: 700; font-size: .95rem; border: 1px solid transparent; transition: transform .12s;
}
.kin-node:hover { transform: translateY(-1px); }
.kin-node.m { background: var(--male-soft); color: var(--male); }
.kin-node.f { background: var(--female-soft); color: var(--female); }
.kin-node.start { box-shadow: 0 0 0 2px var(--accent); }
.kin-node.end { box-shadow: 0 0 0 2px var(--gold); }
.kin-edge { display: inline-flex; flex-direction: column; align-items: center; line-height: 1; gap: 2px; padding: 0 2px; }
.kin-edge-lbl { font-size: .72rem; font-weight: 700; color: var(--gold-soft-ink); }
.kin-edge-arr { font-size: 1.05rem; color: var(--ink-faint); }

/* ---------- Calendar ---------- */
.cal { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--border); }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 1px; }
.cal-dow { background: var(--surface-2); text-align: center; padding: 10px 4px; font-size: .8rem; font-weight: 700; color: var(--ink-soft); }
.cal-dow.wk { color: var(--accent); }
.cal-cell { background: var(--surface); min-height: 108px; padding: 8px; display: flex; flex-direction: column; gap: 4px; }
.cal-cell.empty { background: var(--surface-2); }
.cal-cell.today { outline: 2px solid var(--accent); outline-offset: -2px; background: var(--accent-soft); }
.cal-num { font-size: .82rem; font-weight: 700; color: var(--ink-soft); }
.cal-cell.today .cal-num { color: var(--accent); }
.cal-event { font-size: .72rem; padding: 3px 6px; border-radius: 6px; line-height: 1.3; cursor: pointer; transition: filter .12s; }
.cal-event:hover { filter: brightness(.96); }
.cal-event .ev-name { opacity: .8; }
@media (max-width: 680px) {
  .cal-cell { min-height: 74px; padding: 5px; }
  .cal-event .ev-name { display: none; }
}

/* ---------- Info banner ---------- */
.banner { display: flex; gap: 12px; align-items: flex-start; padding: 14px 18px; border-radius: var(--radius-sm); border: 1px solid; }
.banner svg { width: 20px; height: 20px; flex: none; margin-top: 1px; }
.banner-red { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 30%, transparent); color: var(--accent-soft-ink); }
.banner-gold { background: var(--gold-soft); border-color: color-mix(in srgb, var(--gold) 30%, transparent); color: var(--gold-soft-ink); }

/* ---------- Misc utilities ---------- */
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: 8px; } .gap-3 { gap: 12px; } .gap-4 { gap: 16px; }
.wrap { flex-wrap: wrap; }
.mt-2 { margin-top: 8px; } .mt-4 { margin-top: 16px; } .mt-6 { margin-top: 24px; } .mt-8 { margin-top: 32px; }
.mb-4 { margin-bottom: 16px; } .mb-6 { margin-bottom: 24px; }
.hide-sm { display: inline; }
@media (max-width: 560px) { .hide-sm { display: none; } }
.chip-link { font-size: .86rem; font-weight: 600; display: inline-flex; align-items: center; gap: 5px; }
.back-link { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-soft); font-weight: 600; font-size: .9rem; margin-bottom: 18px; }
.back-link:hover { color: var(--accent); }
.back-link svg { width: 16px; height: 16px; }

/* ---------- Charts ---------- */
.chart { width: 100%; overflow: visible; }
.chart-label { fill: var(--ink-soft); font-size: 13px; font-family: var(--font); }
.chart-value { fill: var(--ink); font-size: 12px; font-weight: 700; font-family: var(--font); }
.chart-axis { fill: var(--ink-faint); font-size: 11px; font-family: var(--font); }
.chart-bar { transition: opacity .12s; }
.chart-bar:hover { opacity: .82; }
.chart-link { cursor: pointer; }
.chart-link:hover .chart-bar { opacity: .82; }
.chart-link:hover .chart-label, .chart-link:hover .chart-value { fill: var(--accent); font-weight: 700; }
a.gender-seg { text-decoration: none; cursor: pointer; transition: filter .12s; }
a.gender-seg:hover { filter: brightness(1.06); }
.gender-bar { display: flex; height: 34px; border-radius: 8px; overflow: hidden; gap: 2px; }
.gender-seg { display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 700; font-size: .85rem; min-width: 2px; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 12px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: .85rem; color: var(--ink-soft); }
.legend-dot { width: 12px; height: 12px; border-radius: 4px; flex: none; }

/* Dashboard stat tiles are clickable (whole tile → manage). */
.card > a.stat { text-decoration: none; transition: background .12s; border-radius: var(--radius) var(--radius) 0 0; }
.card > a.stat:hover { background: var(--surface-2); }

/* ---------- Admin list rows ---------- */
.fam-seal {
  width: 36px; height: 36px; border-radius: 10px; flex: none; display: grid; place-items: center;
  font-weight: 800; font-size: 1rem; color: #fff; box-shadow: var(--shadow-sm);
  background: linear-gradient(135deg, var(--gold), #9c6f28);
  font-family: "Songti SC", "STSong", "Noto Serif SC", serif;
}
.admin-person { display: flex; align-items: center; gap: 11px; min-width: 0; }
.admin-person-t { min-width: 0; }
.admin-person-t b { display: block; font-weight: 700; color: var(--ink); line-height: 1.2; }
.admin-person-sub { display: block; font-size: .76rem; color: var(--ink-faint); margin-top: 2px; }
/* Role-tinted avatars for the user list (no gender). */
.avatar.neutral { background: linear-gradient(135deg, #8a8072, #6d6459); }
.avatar.super { background: linear-gradient(135deg, var(--gold), #9c6f28); }

/* ---------- Auth screens (login / register) ---------- */
.auth-wrap { display: flex; justify-content: center; padding: 48px 20px 72px; }
.auth-card {
  width: 100%; max-width: 440px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
  padding: 32px 30px;
}
.auth-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 24px; }
.auth-seal {
  width: 52px; height: 52px; border-radius: 14px; flex: none; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  color: var(--accent-ink); font-weight: 800; font-size: 1.7rem; box-shadow: var(--shadow-sm);
  font-family: var(--serif, "Songti SC", serif);
}
.auth-brand-t h1 { font-size: 1.35rem; font-weight: 800; }
.auth-brand-t p { margin: 4px 0 0; color: var(--ink-soft); font-size: .88rem; }
.auth-card form { margin: 0; }
.auth-card .form-field + .form-field, .auth-card .form-grid { margin-top: 14px; }
.auth-card .form-actions { margin-top: 22px; padding-top: 0; border-top: none; }
.auth-submit { width: 100%; justify-content: center; }
.auth-alt { text-align: center; color: var(--ink-soft); font-size: .86rem; margin: 16px 0 0; }
.auth-seg { margin: 18px 0 8px; font-weight: 700; font-size: .9rem; color: var(--ink); }
.auth-intent { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.auth-intent label {
  flex: 1; min-width: 140px; display: flex; align-items: center; gap: 8px; cursor: pointer;
  padding: 11px 14px; border: 1px solid var(--border-strong); border-radius: 12px;
  font-weight: 600; font-size: .9rem; color: var(--ink-soft); transition: all .12s;
}
.auth-intent label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.auth-intent input { accent-color: var(--accent); }

.auth-banner { padding: 11px 14px; border-radius: 10px; font-size: .88rem; font-weight: 600; margin-bottom: 18px; border: 1px solid; }
.auth-banner-error { color: var(--danger); background: color-mix(in srgb, var(--danger) 9%, transparent); border-color: color-mix(in srgb, var(--danger) 28%, transparent); }
.auth-banner-info { color: var(--gold-soft-ink); background: var(--gold-soft); border-color: color-mix(in srgb, var(--gold) 30%, transparent); }
.auth-banner-ok { color: var(--success); background: var(--success-soft); border-color: color-mix(in srgb, var(--success) 30%, transparent); }
.auth-banner b { font-weight: 800; }

/* ---------- Forms / admin ---------- */
.admin-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 24px; border-bottom: 1px solid var(--border); padding-bottom: 12px; }
.admin-tab { display: inline-flex; align-items: center; gap: 7px; padding: 8px 14px; border-radius: 10px; color: var(--ink-soft); font-weight: 600; font-size: .92rem; }
.admin-tab:hover { background: var(--surface-2); color: var(--ink); }
.admin-tab.active { background: var(--accent-soft); color: var(--accent); }
.admin-tab svg { width: 17px; height: 17px; }

.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 18px; }
.form-grid .col-full { grid-column: 1 / -1; }
@media (max-width: 620px) { .form-grid { grid-template-columns: 1fr; } }

.form-field { display: flex; flex-direction: column; gap: 6px; }
.form-field > label { font-size: .85rem; font-weight: 600; color: var(--ink-soft); }
/* Section heading inside a grouped admin form. */
.form-section {
  font-size: .82rem; font-weight: 800; letter-spacing: .03em; color: var(--ink-faint);
  margin: 22px 0 14px; padding-bottom: 9px; border-bottom: 1px solid var(--border);
}
.form-section:first-of-type { margin-top: 2px; }
.input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border-strong); border-radius: 10px;
  background: var(--surface); color: var(--ink); font-family: inherit; font-size: .92rem; transition: border-color .15s, box-shadow .15s;
}
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.input { resize: vertical; min-height: 90px; line-height: 1.6; }
select.input { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%239c9284' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4 6 4 4 4-4'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 34px; }
.checkbox { display: inline-flex; align-items: center; gap: 8px; font-size: .92rem; color: var(--ink); cursor: pointer; }
.checkbox input { width: 17px; height: 17px; accent-color: var(--accent); }
.form-actions { display: flex; gap: 12px; margin-top: 22px; padding-top: 20px; border-top: 1px solid var(--border); }
/* Relationship context banner atop the person form (added a relative from the tree). */
.form-context { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; margin-bottom: 20px; padding: 12px 16px; border-radius: var(--radius-sm); background: var(--gold-soft); color: var(--gold-soft-ink); border: 1px solid color-mix(in srgb, var(--gold) 30%, transparent); font-size: .9rem; font-weight: 600; }
.form-context svg { width: 17px; height: 17px; margin-right: 4px; flex: none; }
.form-context strong { font-weight: 800; }

/* Drag-and-drop upload zone (multi-file). */
.dropzone { position: relative; display: block; border: 2px dashed var(--border-strong); border-radius: 12px; padding: 24px 18px; text-align: center; cursor: pointer; background: var(--surface-2); transition: border-color .15s, background .15s; }
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone .dz-input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.dz-hint { color: var(--ink-soft); font-weight: 600; display: inline-flex; align-items: center; gap: 8px; pointer-events: none; }
.dz-hint svg { width: 18px; height: 18px; }
.dz-files { display: block; margin-top: 10px; font-size: .85rem; color: var(--accent-soft-ink); pointer-events: none; }

.flash { padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 20px; font-weight: 600; font-size: .9rem; }
.flash-ok { background: var(--success-soft); color: var(--success); border: 1px solid color-mix(in srgb, var(--success) 30%, transparent); }

/* ---------- Share modal ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(20, 16, 12, .5); display: none; align-items: center; justify-content: center; z-index: 200; padding: 20px; backdrop-filter: blur(2px); }
.modal-overlay.open { display: flex; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); max-width: 360px; width: 100%; padding: 26px 24px; text-align: center; position: relative; }
.modal h3 { font-size: 1.15rem; }
.modal-close { position: absolute; top: 14px; right: 14px; width: 32px; height: 32px; border: none; background: transparent; color: var(--ink-faint); cursor: pointer; border-radius: 8px; display: grid; place-items: center; }
.modal-close:hover { background: var(--surface-2); color: var(--ink); }
.modal .qr { width: 200px; height: 200px; margin: 18px auto; border-radius: 12px; background: #fff; padding: 12px; box-shadow: var(--shadow-sm); }
.share-url { display: flex; gap: 8px; margin-top: 8px; }
.share-url input { flex: 1; min-width: 0; }

/* ---------- Mobile refinements ---------- */
/* Respect the safe area on notched phones (esp. landscape). */
@supports (padding: max(0px)) {
  .container {
    padding-left: max(20px, env(safe-area-inset-left));
    padding-right: max(20px, env(safe-area-inset-right));
  }
}
@media (max-width: 620px) {
  /* iOS zooms the page when focusing an input whose text is < 16px. Bump all
     form fields to 16px on phones to keep focus from jarringly zooming in. */
  .input, select.input, textarea.input, .nav-search input { font-size: 16px; }
}
@media (max-width: 560px) {
  /* A touch shorter so page content around it stays reachable by scroll. */
  .tree-wrap { height: 460px; }
  /* Give the page a little more breathing room on small screens. */
  .page { padding: 22px 0 44px; }
  .page-title { font-size: 1.45rem; }
}
@media (max-width: 360px) {
  /* Very small phones (iPhone SE, compact Androids): keep the brand on one
     line by trimming the logo, brand text, and control spacing. */
  .container { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  .brand { font-size: 1.02rem; gap: 8px; }
  .brand .seal { width: 30px; height: 30px; font-size: .95rem; }
  .nav-right { gap: 8px; }
  .nav-toggle, .theme-toggle { width: 38px; height: 38px; }
}

/* ---------- Accessibility & UX polish ---------- */
html { scroll-behavior: smooth; }

/* Keyboard skip-link — hidden until focused. */
.skip-link {
  position: fixed; top: -60px; left: 12px; z-index: 400;
  background: var(--accent); color: #fff; padding: 10px 16px; border-radius: 10px;
  font-weight: 700; box-shadow: var(--shadow-md); transition: top .18s;
}
.skip-link:focus { top: 12px; color: #fff; }

/* Visible focus ring for keyboard users (mouse clicks don't trigger it). */
a:focus-visible, button:focus-visible, select:focus-visible, textarea:focus-visible,
[tabindex]:focus-visible, .tree-node:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.input:focus-visible { outline: none; } /* already has an accent ring */

/* Back-to-top button — appears after scrolling. */
.to-top {
  position: fixed; right: 20px; bottom: 20px; z-index: 90;
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); color: var(--ink-soft); cursor: pointer; box-shadow: var(--shadow-md);
  display: grid; place-items: center;
  opacity: 0; visibility: hidden; transform: translateY(10px); transition: opacity .2s, transform .2s, visibility .2s, color .15s, border-color .15s;
}
.to-top.show { opacity: 1; visibility: visible; transform: none; }
.to-top:hover { color: var(--accent); border-color: var(--accent); }
.to-top svg { width: 20px; height: 20px; transform: rotate(90deg); }
@supports (padding: max(0px)) {
  .to-top { right: max(20px, env(safe-area-inset-right)); bottom: max(20px, env(safe-area-inset-bottom)); }
}

/* Friendly 404 / 500 page. */
.error-page { text-align: center; padding: 72px 20px 90px; max-width: 560px; margin: 0 auto; }
.error-code { font-size: 5rem; font-weight: 800; line-height: 1; color: var(--accent); letter-spacing: 2px; }
.error-title { font-size: 1.6rem; margin-top: 12px; }
.error-sub { color: var(--ink-soft); margin: 12px 0 28px; }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; scroll-behavior: auto !important; }
}

/* ---------- Footer ---------- */
.footer { border-top: 1px solid var(--border); background: var(--surface); margin-top: auto; }
.footer-inner { padding: 24px 0; text-align: center; color: var(--ink-faint); font-size: .85rem; }
