:root {
  --paper: #f5efe3;
  --paper-2: #ebe2d0;
  --ink: #2a211b;
  --ink-2: #6b5d50;
  --line: #b9a98f;
  --accent: #a23b2a;
  --card: #fffdf8;
  --shadow: 0 1px 2px rgba(60, 40, 20, .12), 0 4px 14px rgba(60, 40, 20, .08);
  --serif: "Songti SC", "STSong", "Noto Serif SC", "Source Han Serif SC", "SimSun", serif;
  --sans: -apple-system, "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --top-h: 60px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--paper); color: var(--ink); font-family: var(--sans); overflow: hidden; -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; }

/* 顶栏 */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--top-h); z-index: 20;
  display: flex; align-items: center; gap: 18px; padding: 0 18px;
  background: rgba(250, 246, 238, .94); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--paper-2);
}
.brand { font-family: var(--serif); font-size: 20px; margin: 0; white-space: nowrap; letter-spacing: .08em; color: var(--accent); }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; flex: 1; min-width: 0; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 1px solid transparent; background: none; border-radius: 8px; padding: 4px 12px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; line-height: 1.15; flex: none;
}
.tab-name { font-family: var(--serif); font-size: 18px; font-weight: 700; }
.tab-years { font-size: 10.5px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.tab:hover { background: var(--paper-2); }
.tab[aria-selected="true"] { background: var(--accent); color: #fff8ec; }
.tab[aria-selected="true"] .tab-years { color: #f3d9cf; }

.search-wrap { position: relative; width: 260px; flex: none; }
#search {
  width: 100%; height: 36px; border-radius: 18px; border: 1px solid var(--line); background: var(--card);
  padding: 0 14px; font-size: 14px; color: var(--ink); outline: none;
}
#search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(162, 59, 42, .15); }
.search-results {
  position: absolute; top: 42px; left: 0; right: 0; margin: 0; padding: 4px; list-style: none;
  background: var(--card); border: 1px solid var(--paper-2); border-radius: 10px; box-shadow: var(--shadow); max-height: 60vh; overflow: auto;
}
.search-results li { display: flex; align-items: baseline; gap: 8px; padding: 7px 10px; border-radius: 6px; cursor: pointer; font-size: 14px; }
.search-results li span { color: var(--ink-2); font-size: 13px; }
.search-results li em { margin-left: auto; font-style: normal; font-size: 12px; color: var(--accent); }
.search-results li:hover, .search-results li.active { background: var(--paper); }
.search-results li.empty { color: var(--ink-2); cursor: default; }

/* 画布 */
.stage { position: fixed; inset: var(--top-h) 0 0 0; }
#tree { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none;
  background-image: radial-gradient(rgba(120, 90, 50, .10) 1px, transparent 1px); background-size: 22px 22px; }
#tree.dragging { cursor: grabbing; }
#tree.animating #viewport { transition: transform .35s ease; }

.dyn-head { position: absolute; left: 20px; top: 14px; z-index: 5; pointer-events: none; max-width: min(520px, 60vw); }
.dyn-head h2 { margin: 0; font-family: var(--serif); font-size: 26px; letter-spacing: .04em; }
#dyn-meta { margin: 2px 0 0; font-size: 13px; color: var(--accent); font-variant-numeric: tabular-nums; }
#dyn-summary { margin: 4px 0 0; font-size: 13px; color: var(--ink-2); line-height: 1.5; }

/* SVG 元素 */
.blood { fill: none; stroke: var(--line); stroke-width: 1.4; }
.blood.direct { stroke: #a23b2a; stroke-width: 3; }
.blood.adopt { stroke-dasharray: 2 4; stroke-width: 1.8; stroke-linecap: round; }
.blood.lineage { stroke: #c9930e; stroke-width: 4; }
.succ-arrow { fill: none; stroke-width: 2.2; stroke-dasharray: 7 5; opacity: .9; }
.succ-arrow.active { stroke-width: 3.6; opacity: 1; }
.succ-label rect { fill: #fffdf8; stroke-width: 1.2; }
.succ-label text { font-size: 11.5px; font-weight: 600; }

.node { cursor: pointer; outline: none; }
.node .box { fill: var(--card); filter: drop-shadow(0 1px 1.5px rgba(60, 40, 20, .18)); }
.node .frame { fill: none; stroke: #cdbfa6; stroke-width: 1.2; }
.node.per .box { fill: #efe7d7; }
.node.per .frame { stroke-dasharray: 4 3; }
.node .title { font-size: 12px; fill: var(--ink-2); }
.node.emp .name { font-family: var(--serif); font-size: 18px; font-weight: 700; fill: var(--ink); }
.node .reign { font-size: 11.5px; fill: var(--accent); font-variant-numeric: tabular-nums; }
.node.per .name { font-family: var(--serif); font-size: 15px; font-weight: 700; fill: #4a3d32; }
.node.per .note { font-size: 10.5px; fill: var(--ink-2); }
.node .badge { fill: var(--ink); }
.node .badge-t { font-size: 11px; font-weight: 700; fill: #fff; font-variant-numeric: tabular-nums; }
.node:hover .frame, .node:focus-visible .frame { stroke: var(--accent); stroke-width: 2; }
.node.selected .frame { stroke: #c9930e; stroke-width: 3.5; stroke-dasharray: none; }
.node.flash .frame { animation: flash 1.2s ease 2; }
@keyframes flash { 0%, 100% { stroke-width: 3.5; } 50% { stroke-width: 9; stroke: #f0b429; } }

/* 缩放按钮 */
.zoom { position: absolute; right: 18px; bottom: 22px; display: flex; flex-direction: column; gap: 6px; z-index: 6; }
.zoom button { width: 44px; height: 40px; border-radius: 10px; border: 1px solid var(--paper-2); background: var(--card); box-shadow: var(--shadow); cursor: pointer; font-size: 18px; }
.zoom #zoom-fit { font-size: 13px; }
.zoom button:hover { border-color: var(--accent); }

/* 导出 */
.export { position: absolute; right: 18px; top: 14px; display: flex; gap: 6px; z-index: 6; }
.export button { height: 34px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--paper-2); background: var(--card); box-shadow: var(--shadow); cursor: pointer; font-size: 13px; }
.export button:hover { border-color: var(--accent); color: var(--accent); }
.toast { position: absolute; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 30; background: var(--ink); color: #fff8ec; font-size: 13px; padding: 8px 16px; border-radius: 18px; box-shadow: var(--shadow); }

/* 图例 */
.legend { position: absolute; left: 18px; bottom: 22px; z-index: 6; max-width: 360px; }
#legend-toggle { border: 1px solid var(--paper-2); background: var(--card); border-radius: 10px; padding: 7px 14px; box-shadow: var(--shadow); cursor: pointer; font-size: 13px; }
.legend-body { display: none; margin-top: 8px; background: var(--card); border: 1px solid var(--paper-2); border-radius: 12px; box-shadow: var(--shadow); padding: 12px 14px; font-size: 12.5px; max-height: 60vh; overflow: auto; }
.legend.open .legend-body { display: block; }
.legend ul { list-style: none; margin: 0; padding: 0; }
.legend li { display: flex; align-items: center; gap: 8px; padding: 3px 0; }
.legend li.lg-note { display: block; color: var(--ink-2); font-size: 12px; line-height: 1.55; padding: 2px 0 6px 34px; }
.legend-h { margin: 10px 0 4px; font-weight: 700; color: var(--ink-2); }
.legend-types b { width: 4.5em; flex: none; }
.lg-desc { color: var(--ink-2); }
.sw-node { width: 26px; height: 16px; border-radius: 4px; background: var(--card); border: 1.2px solid #cdbfa6; flex: none; }
.sw-node.per { background: #efe7d7; border-style: dashed; border-radius: 8px; }
.sw-line { width: 26px; height: 0; border-top: 1.5px solid var(--line); flex: none; }
.sw-line.direct { border-top: 3px solid #a23b2a; }
.sw-line.adopt { border-top: 2px dotted var(--line); }
.sw-arrow { width: 26px; height: 0; border-top: 2.2px dashed var(--c); flex: none; position: relative; }
.sw-arrow::after { content: ""; position: absolute; right: -2px; top: -5px; border: 4px solid transparent; border-left: 6px solid var(--c); }

/* 详情侧栏 */
.panel {
  position: absolute; top: 0; right: 0; bottom: 0; width: 380px; z-index: 10;
  background: var(--card); border-left: 1px solid var(--paper-2); box-shadow: -6px 0 24px rgba(60, 40, 20, .08);
  transform: translateX(100%); transition: transform .25s ease; overflow-y: auto;
}
body.panel-open .panel { transform: none; }
.panel-close { position: absolute; top: 10px; right: 12px; width: 34px; height: 34px; border-radius: 50%; border: none; background: var(--paper); font-size: 20px; cursor: pointer; }
.panel-grip { display: none; }
.panel-body { padding: 24px 24px 32px; }
.p-kicker { margin: 0; font-size: 12.5px; color: var(--accent); letter-spacing: .05em; }
.p-title { margin: 4px 0 2px; font-family: var(--serif); font-size: 28px; }
.p-sub { margin: 0 0 14px; color: var(--ink-2); font-size: 14px; }
.facts { margin: 0; border-top: 1px solid var(--paper-2); }
.facts .row { display: grid; grid-template-columns: 5.5em 1fr; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--paper-2); font-size: 14px; }
.facts dt { color: var(--ink-2); }
.facts dd { margin: 0; line-height: 1.6; }
.reign-years { font-weight: 700; font-variant-numeric: tabular-nums; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; margin: 3px 0; }
.tag { font-size: 12px; padding: 1px 8px; border-radius: 10px; color: var(--c); border: 1px solid var(--c); background: #fff; }
.eras { color: var(--ink-2); font-size: 13px; }
.pn { font-size: 13px; color: var(--ink-2); }
.plink { border: none; background: none; padding: 0; color: var(--accent); cursor: pointer; text-decoration: underline; text-underline-offset: 3px; text-decoration-color: rgba(162, 59, 42, .35); font-size: inherit; }
.plink:hover { text-decoration-color: var(--accent); }
.bio { margin: 16px 0 0; line-height: 1.85; font-size: 14.5px; text-align: justify; }

/* 移动端 */
@media (max-width: 760px) {
  :root { --top-h: 104px; }
  .topbar { flex-wrap: wrap; height: var(--top-h); padding: 8px 12px 0; gap: 6px 10px; align-content: flex-start; }
  .brand { font-size: 17px; }
  .search-wrap { flex: 1; width: auto; }
  #search { height: 34px; }
  .tabs { order: 3; flex-basis: 100%; }
  .tab { padding: 3px 10px; }
  .dyn-head { left: 12px; top: 8px; max-width: 70vw; }
  .dyn-head h2 { font-size: 20px; }
  #dyn-summary { display: none; }
  .panel {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; max-height: 62vh;
    border-left: none; border-top: 1px solid var(--paper-2); border-radius: 16px 16px 0 0;
    transform: translateY(100%); box-shadow: 0 -6px 24px rgba(60, 40, 20, .12);
  }
  .panel-grip { display: block; width: 40px; height: 4px; border-radius: 2px; background: var(--paper-2); margin: 8px auto 0; }
  .panel-body { padding: 10px 18px 28px; }
  .p-title { font-size: 24px; }
  .legend { left: 12px; bottom: 14px; max-width: calc(100vw - 90px); }
  .zoom { right: 12px; bottom: 14px; }
  .export { right: 12px; top: 8px; gap: 4px; }
  .export button { height: 30px; padding: 0 9px; font-size: 12px; }
  .dyn-head { max-width: calc(100vw - 170px); }
  body.panel-open .zoom, body.panel-open .legend, body.panel-open .dyn-head, body.panel-open .export { display: none; }
}
