/* ============================================================================
 * app.css —— 淡雅纸感风格，移动优先
 * 节点尺寸必须与 layout.js 的 NODE_W / NODE_H 保持一致（112 × 96）
 * ==========================================================================*/
:root {
  --paper: #f7f3ec;
  --card: #fffdfa;
  --ink: #2d2823;
  --ink2: #5b5249;
  --ink3: #9a8f83;
  --line: #e4d9cb;
  --line2: #cfbfab;
  --wang: #9a6a42;
  --ji: #54789c;
  --other: #8c8378;
  --focus: #b4613c;
  --dead: #b0a8a0;
  --adoptive: #b08a3e;
  --shadow-1: 0 1px 2px rgba(72, 54, 34, .05), 0 8px 20px -14px rgba(72, 54, 34, .3);
  --shadow-2: 0 2px 6px rgba(72, 54, 34, .07), 0 18px 46px -22px rgba(72, 54, 34, .38);
  --radius: 14px;
  --ui-font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
             "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  --name-font: "Songti SC", "Noto Serif SC", "Source Han Serif SC", STSong, SimSun, serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--ui-font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
body::before {
  content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background:
    radial-gradient(1200px 600px at 12% -10%, rgba(154, 106, 66, .06), transparent 60%),
    radial-gradient(900px 500px at 92% 108%, rgba(84, 120, 156, .06), transparent 60%);
}

/* ------------------------------------------------------------------ 顶栏 */
.bar {
  position: fixed; z-index: 30; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  background: rgba(247, 243, 236, .86);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid rgba(228, 217, 203, .8);
}
.brand { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.seal {
  width: 28px; height: 28px; border-radius: 8px;
  display: grid; place-items: center;
  background: linear-gradient(160deg, #a9764f, #8a5a37);
  color: #fff8ee; font-family: var(--name-font); font-size: 15px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .25);
}
.title { font-family: var(--name-font); font-size: 15px; letter-spacing: 1px; color: var(--ink); }
.subtitle { font-size: 11px; color: var(--ink3); margin-left: 2px; }

.search-wrap { position: relative; flex: 1 1 auto; max-width: 420px; }
#q {
  width: 100%; height: 34px; padding: 0 12px 0 30px;
  border: 1px solid var(--line); border-radius: 999px;
  background: rgba(255, 253, 250, .9); color: var(--ink);
  font-size: 13px; outline: none; transition: border-color .15s, box-shadow .15s;
}
#q::placeholder { color: var(--ink3); }
#q:focus { border-color: var(--line2); box-shadow: 0 0 0 3px rgba(180, 97, 60, .08); }
.search-wrap::before {
  content: ''; position: absolute; left: 11px; top: 11px; width: 12px; height: 12px;
  border: 1.6px solid var(--ink3); border-radius: 50%;
}
.search-wrap::after {
  content: ''; position: absolute; left: 21px; top: 22px; width: 5px; height: 1.6px;
  background: var(--ink3); transform: rotate(45deg);
}
.suggest {
  position: absolute; top: 40px; left: 0; right: 0; z-index: 40;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow-2); overflow: hidden; display: none;
}
.suggest.open { display: block; }
.suggest-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 12px; cursor: pointer; border-bottom: 1px solid rgba(228, 217, 203, .55);
  font-size: 13px;
}
.suggest-item:last-child { border-bottom: 0; }
.suggest-item:hover, .suggest-item.active { background: #fbf6ef; }
.suggest-item .nm { font-family: var(--name-font); }
.suggest-item .rl { margin-left: auto; color: var(--ink3); font-size: 11px; }
.suggest-empty { padding: 14px; color: var(--ink3); font-size: 12px; text-align: center; }

.icon-btn {
  flex: 0 0 auto; width: 36px; height: 36px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); color: var(--ink2);
  font-size: 16px; cursor: pointer; display: grid; place-items: center;
}
.icon-btn:active { transform: scale(.96); }

/* --------------------------------------------------------------- 焦点条 */
.focusbar {
  position: fixed; z-index: 25; top: 55px; left: 0; right: 0;
  padding: 7px 14px; font-size: 12px; color: var(--ink2);
  background: linear-gradient(180deg, rgba(247, 243, 236, .95), rgba(247, 243, 236, .55) 70%, transparent);
  display: flex; align-items: center; gap: 8px; pointer-events: none;
}
.focusbar b { color: var(--focus); font-family: var(--name-font); font-size: 14px; letter-spacing: .5px; }
.focusbar .hint { color: var(--ink3); font-size: 11px; }

/* ------------------------------------------------------------------ 画布 */
.viewport {
  position: absolute; inset: 0; z-index: 1;
  overflow: hidden; cursor: grab; touch-action: none;
}
.viewport.dragging { cursor: grabbing; }
.stage {
  position: absolute; top: 0; left: 0;
  transform-origin: 0 0; will-change: transform;
}
.links { position: absolute; top: 0; left: 0; overflow: visible; }
.nodes { position: absolute; inset: 0; }

.links .blood { fill: none; stroke: var(--line2); stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.links .marriage { stroke: #c2a98e; stroke-width: 2; stroke-linecap: round; }
.links .marriage-dot { fill: #c2a98e; }
.links .joint { fill: var(--line2); }
.links .dashed { fill: none; stroke-dasharray: 4 5; stroke-width: 1.3; stroke-linecap: round; opacity: .6; }
.links .dashed.marry { stroke: #b99a7e; }
.links .dashed.adoptive { stroke: var(--adoptive); stroke-dasharray: 2 6; opacity: .8; }

/* ------------------------------------------------------------------ 节点 */
.node {
  position: absolute; width: 112px; height: 96px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  padding: 6px 6px 5px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  cursor: pointer; user-select: none;
  transition: box-shadow .16s ease, border-color .16s ease, transform .16s ease;
  overflow: hidden;
}
.node::before {
  content: ''; position: absolute; left: 0; top: 18px; bottom: 18px; width: 3px;
  border-radius: 0 3px 3px 0; background: var(--accent, var(--other)); opacity: .55;
}
.node[data-clan="wang"] { --accent: var(--wang); }
.node[data-clan="ji"] { --accent: var(--ji); }
.node[data-clan="other"] { --accent: var(--other); }
.node:active { transform: scale(.985); }
.node:hover { box-shadow: var(--shadow-2); border-color: var(--line2); }
.node.is-focus {
  border-color: rgba(180, 97, 60, .7);
  box-shadow: 0 0 0 3px rgba(180, 97, 60, .12), var(--shadow-2);
}
.node.is-deceased { background: #fbf9f5; }
.node.is-deceased .name { color: var(--dead); }
.node.is-deceased .avatar { filter: grayscale(1); opacity: .6; }
.node.is-placeholder { border-style: dashed; background: rgba(255, 253, 250, .7); }
.node.is-placeholder .name { color: var(--ink3); }
.node.is-assumed::after {
  content: '推'; position: absolute; right: 6px; bottom: 5px;
  font-size: 8px; color: var(--ink3); opacity: .8;
}

.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center; overflow: hidden;
  background: rgba(154, 106, 66, .1);
  color: var(--accent, var(--other));
  font-family: var(--name-font); font-size: 15px; line-height: 1;
  flex: 0 0 auto;
}
.node[data-clan="ji"] .avatar { background: rgba(84, 120, 156, .11); }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.name {
  font-family: var(--name-font); font-size: 14.5px; line-height: 1.15;
  letter-spacing: .3px; color: var(--ink);
  max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.rel {
  font-size: 10px; line-height: 1.1; color: var(--ink3);
  max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  padding: 1px 5px; border-radius: 999px; background: rgba(228, 217, 203, .35);
}
.node.is-focus .rel { color: var(--focus); background: rgba(180, 97, 60, .1); }
.rank {
  position: absolute; top: 4px; right: 7px;
  font-size: 8.5px; color: var(--ink3); letter-spacing: .2px;
}
.dead {
  position: absolute; top: 4px; left: 7px;
  font-size: 8.5px; color: var(--dead);
  border: 1px solid rgba(176, 168, 160, .5); border-radius: 4px;
  padding: 0 3px; line-height: 1.35;
}

/* ------------------------------------------------------------- 世代标尺 */
.rail {
  position: fixed; left: 0; top: 0; bottom: 0; width: 46px; z-index: 20;
  pointer-events: none;
  background: linear-gradient(90deg, rgba(247, 243, 236, .95), rgba(247, 243, 236, 0));
}
.rail-item {
  position: absolute; left: 8px; transform: translateY(-50%);
  font-size: 10px; color: var(--ink3); writing-mode: vertical-rl;
  letter-spacing: 2px; opacity: .9;
}
.rail-item::after {
  content: ''; display: inline-block; width: 1px; height: 18px;
  background: var(--line); margin-top: 6px;
}

/* ------------------------------------------------------------------ 工具 */
.tools {
  position: fixed; z-index: 28; right: 12px; bottom: calc(16px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 8px;
}
.tools button {
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--line); background: rgba(255, 253, 250, .95);
  color: var(--ink2); font-size: 13px; cursor: pointer;
  box-shadow: var(--shadow-1); backdrop-filter: blur(6px);
  display: grid; place-items: center;
}
.tools button:active { transform: scale(.95); }
.tools button.wide { font-size: 11px; }

.legend {
  position: fixed; z-index: 24; left: 12px; bottom: calc(16px + env(safe-area-inset-bottom));
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  padding: 7px 11px; border-radius: 999px;
  background: rgba(255, 253, 250, .92); border: 1px solid var(--line);
  box-shadow: var(--shadow-1); font-size: 10.5px; color: var(--ink3);
  max-width: calc(100vw - 120px);
}
.legend i { width: 7px; height: 7px; border-radius: 50%; display: inline-block; margin-right: 4px; }
.legend .w { background: var(--wang); }
.legend .j { background: var(--ji); }
.legend .o { background: var(--other); }
.legend .d { background: var(--dead); }
.legend .a { background: var(--adoptive); }

/* ------------------------------------------------------------------ 抽屉 */
.scrim {
  position: fixed; inset: 0; z-index: 50; background: rgba(45, 40, 35, .32);
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
.scrim.open { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed; z-index: 60; left: 0; right: 0; bottom: 0;
  max-height: 82vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--card); border-radius: 20px 20px 0 0;
  box-shadow: 0 -10px 40px -18px rgba(60, 45, 30, .5);
  transform: translateY(102%); transition: transform .26s cubic-bezier(.2, .8, .3, 1);
  padding: 8px 18px calc(20px + env(safe-area-inset-bottom));
}
.sheet.open { transform: translateY(0); }
.sheet-handle {
  width: 38px; height: 4px; border-radius: 2px; background: var(--line);
  margin: 6px auto 12px;
}
@media (min-width: 900px) {
  .sheet {
    top: 0; left: auto; right: 0; bottom: 0; width: 400px; max-height: none;
    border-radius: 0; transform: translateX(102%);
    padding: 16px 22px 28px;
    border-left: 1px solid var(--line);
  }
  .sheet.open { transform: translateX(0); }
  .sheet-handle { display: none; }
}

.person-head { display: flex; gap: 14px; align-items: flex-start; }
.person-photo {
  width: 68px; height: 68px; border-radius: 16px; flex: 0 0 auto;
  background: rgba(154, 106, 66, .08); display: grid; place-items: center;
  font-family: var(--name-font); font-size: 26px; color: var(--wang);
  overflow: hidden; border: 1px solid var(--line);
}
.person-photo img { width: 100%; height: 100%; object-fit: cover; }
.person-meta { min-width: 0; flex: 1 1 auto; }
.person-name {
  font-family: var(--name-font); font-size: 22px; letter-spacing: 1px;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.person-name .tag {
  font-family: var(--ui-font); font-size: 10px; letter-spacing: 0;
  padding: 2px 7px; border-radius: 999px; background: rgba(180, 97, 60, .1); color: var(--focus);
}
.person-name .tag.gray { background: rgba(176, 168, 160, .18); color: var(--ink3); }
.person-sub { margin-top: 6px; font-size: 12px; color: var(--ink3); }
.person-rel {
  margin-top: 8px; font-size: 12.5px; color: var(--ink2);
}
.person-rel b { color: var(--focus); font-size: 15px; font-family: var(--name-font); }
.person-rel .chain { display: block; margin-top: 3px; color: var(--ink3); font-size: 11px; }

.section { margin-top: 18px; }
.section h4 {
  margin: 0 0 8px; font-size: 11px; font-weight: 600; color: var(--ink3);
  letter-spacing: 1.5px;
}
.kv { display: grid; grid-template-columns: 68px 1fr; gap: 6px 10px; font-size: 13px; }
.kv dt { color: var(--ink3); }
.kv dd { margin: 0; color: var(--ink); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 11.5px; padding: 3px 9px; border-radius: 999px;
  background: #f6f0e7; color: var(--ink2); border: 1px solid rgba(228, 217, 203, .9);
}
.chip.link { cursor: pointer; }
.chip.link:hover { background: #f1e8db; }
.rel-list { display: flex; flex-direction: column; gap: 6px; }
.rel-row {
  display: flex; align-items: center; gap: 8px; padding: 7px 10px;
  border: 1px solid var(--line); border-radius: 10px; cursor: pointer;
  background: #fffefb;
}
.rel-row:hover { background: #fbf6ef; }
.rel-row .rr-term { font-size: 11px; color: var(--ink3); min-width: 44px; }
.rel-row .rr-name { font-family: var(--name-font); font-size: 13.5px; }
.rel-row .rr-arrow { margin-left: auto; color: var(--ink3); font-size: 11px; }

.actions { display: flex; gap: 8px; margin-top: 20px; flex-wrap: wrap; }
.btn {
  flex: 1 1 auto; min-width: 88px; height: 38px; border-radius: 11px;
  border: 1px solid var(--line); background: #fffefb; color: var(--ink2);
  font-size: 13px; cursor: pointer; font-family: var(--ui-font);
}
.btn:active { transform: scale(.98); }
.btn.primary {
  background: linear-gradient(160deg, #b4613c, #9c5233); border-color: #9c5233;
  color: #fff8f0;
}
.btn.danger { color: #a8432f; border-color: rgba(168, 67, 47, .3); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* ------------------------------------------------------------------ 表单 */
.form { display: flex; flex-direction: column; gap: 12px; margin-top: 4px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 11.5px; color: var(--ink3); }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px; font-size: 13.5px; font-family: var(--ui-font);
  border: 1px solid var(--line); border-radius: 10px; background: #fffefb;
  color: var(--ink); outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--line2); box-shadow: 0 0 0 3px rgba(180, 97, 60, .07);
}
.field textarea { min-height: 68px; resize: vertical; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1 1 0; }
.seg { display: flex; gap: 6px; }
.seg button {
  flex: 1 1 0; height: 34px; border-radius: 9px; border: 1px solid var(--line);
  background: #fffefb; color: var(--ink2); font-size: 13px; cursor: pointer;
}
.seg button.on { background: rgba(180, 97, 60, .1); border-color: rgba(180, 97, 60, .45); color: var(--focus); }

.sheet-title { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.sheet-title h3 { margin: 0; font-size: 15px; font-weight: 600; }
.sheet-title .close {
  margin-left: auto; width: 30px; height: 30px; border-radius: 8px;
  border: 1px solid var(--line); background: #fffefb; color: var(--ink3);
  cursor: pointer; font-size: 15px; line-height: 1;
}
.hintline { font-size: 11px; color: var(--ink3); line-height: 1.6; }
.err { color: #a8432f; font-size: 12px; min-height: 16px; }

/* ------------------------------------------------------------------ 登录 */
.login {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center; padding: 24px;
  background:
    radial-gradient(900px 500px at 20% 0%, rgba(154, 106, 66, .12), transparent 60%),
    radial-gradient(800px 460px at 90% 100%, rgba(84, 120, 156, .12), transparent 60%),
    var(--paper);
}
.login.hidden { display: none; }
.login-card {
  width: 100%; max-width: 340px; background: var(--card);
  border: 1px solid var(--line); border-radius: 20px; padding: 26px 22px 22px;
  box-shadow: var(--shadow-2); text-align: center;
}
.login-card .seal { width: 48px; height: 48px; border-radius: 14px; font-size: 22px; margin: 0 auto 12px; }
.login-card h1 { font-family: var(--name-font); font-size: 19px; margin: 0 0 4px; letter-spacing: 2px; }
.login-card p { margin: 0 0 18px; font-size: 12px; color: var(--ink3); }
.login-card .form { text-align: left; }
.login-card .btn { height: 42px; margin-top: 4px; }

.toast {
  position: fixed; z-index: 200; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(10px);
  background: rgba(45, 40, 35, .92); color: #fff8f0; font-size: 12.5px;
  padding: 9px 16px; border-radius: 999px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; max-width: 80vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.menu {
  position: fixed; z-index: 70; top: 52px; right: 12px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow-2); overflow: hidden; display: none; min-width: 168px;
}
.menu.open { display: block; }
.menu button {
  display: block; width: 100%; text-align: left; padding: 11px 14px;
  background: none; border: 0; font-size: 13px; color: var(--ink2); cursor: pointer;
  font-family: var(--ui-font);
}
.menu button:hover { background: #fbf6ef; }
.menu .sep { height: 1px; background: var(--line); margin: 4px 0; }
.menu .ver { padding: 7px 14px; font-size: 10.5px; color: var(--ink3); }

/* 移动端微调 */
@media (max-width: 560px) {
  .title { font-size: 14px; }
  .subtitle { display: none; }
  .legend { display: none; }
  .node { width: 112px; height: 96px; }
}
