:root {
  --ink: #1c2b2d;
  --muted: #667377;
  --paper: #f4f6f6;
  --panel: #ffffff;
  --line: #d8e0e0;
  --accent: #8d3038;
  --accent-dark: #6f242b;
  --pine: #164c4f;
  --gold: #b98a3b;
  --shadow: 0 12px 32px rgba(20, 45, 48, .1);
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  color: var(--ink);
  background: var(--paper);
  font-family: "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
  letter-spacing: 0;
}
button, input, select, textarea {
  font: inherit;
  color: inherit;
}
button { cursor: pointer; }
.hidden { display: none !important; }

.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 12% 8%, rgba(185, 138, 59, .14), transparent 34%),
    linear-gradient(135deg, #eef3f2 0%, #f7f2e9 48%, #e4eceb 100%);
}
.auth-card {
  width: min(430px, 100%);
  background: rgba(255, 255, 255, .92);
  border: 1px solid rgba(255, 255, 255, .6);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 28px;
}
.brand-lockup {
  display: flex;
  gap: 14px;
  align-items: center;
  margin-bottom: 22px;
}
.brand-lockup h1 { margin: 0 0 4px; font-size: 24px; }
.brand-lockup p { margin: 0; color: var(--muted); font-size: 13px; }
.brand-mark {
  width: 52px;
  height: 52px;
  flex: 0 0 52px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: #fff;
  font-family: "KaiTi", "STKaiti", serif;
  font-size: 28px;
  background: linear-gradient(135deg, var(--pine), #0b2f32);
  box-shadow: 0 8px 18px rgba(22, 76, 79, .25);
}
.brand-mark.small { width: 38px; height: 38px; flex-basis: 38px; font-size: 21px; }

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: #e8eeed;
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: 20px;
}
.segment {
  border: 0;
  background: transparent;
  padding: 9px 0;
  border-radius: 6px;
  color: var(--muted);
}
.segment.active { background: #fff; color: var(--ink); box-shadow: 0 2px 8px rgba(0,0,0,.06); }
.auth-form { display: grid; gap: 14px; }
label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 9px 11px;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--pine); box-shadow: 0 0 0 3px rgba(22,76,79,.12); }
textarea { min-height: 84px; resize: vertical; line-height: 1.55; }

.btn {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 9px 13px;
  background: #e8eeed;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-pine { background: var(--pine); color: #fff; }
.btn-ghost { background: transparent; border-color: rgba(255,255,255,.22); color: #eaf1ef; }
.btn-outline { background: transparent; border-color: var(--line); }
.btn-sm { padding: 6px 9px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 238px 1fr; }
.sidebar {
  background: linear-gradient(180deg, #123b3d 0%, #0c292b 100%);
  color: #eef6f4;
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px; }
.sidebar-brand strong { display: block; font-size: 16px; }
.sidebar-brand span { font-size: 12px; color: #a9c3c0; }
.side-nav { display: grid; gap: 8px; }
.nav-item {
  text-align: left;
  border: 0;
  background: transparent;
  color: #c9dcd9;
  padding: 11px 12px;
  border-radius: var(--radius);
  display: flex;
  gap: 9px;
  align-items: center;
}
.nav-item.active { background: rgba(255,255,255,.12); color: #fff; }
.logout-wrap { margin-top: auto; }

.content { padding: 30px clamp(18px, 4vw, 52px) 60px; max-width: 1280px; width: 100%; }
.page-head { display: flex; align-items: end; justify-content: space-between; gap: 14px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head h2 { margin: 0; font-size: 26px; }
.page-head p { margin: 7px 0 0; color: var(--muted); }

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 22px;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stat strong { display: block; font-size: 26px; margin-top: 5px; }
.stat span { font-size: 13px; color: var(--muted); }

.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.card h3 { margin: 0; font-size: 18px; }
.card p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.6; }
.card-actions { margin-top: auto; display: flex; flex-wrap: wrap; gap: 7px; }
.meta-line { font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 8px; }
.badge { display: inline-flex; padding: 3px 8px; border-radius: 999px; background: #e9efee; font-size: 12px; color: var(--pine); }
.badge.red { background: #f7e6e7; color: var(--accent); }
.badge.gold { background: #f4ead5; color: #785b22; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-top: 14px;
}
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar .spacer { flex: 1; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; min-width: 720px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); font-size: 14px; vertical-align: top; }
th { background: #eef3f2; font-weight: 600; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
.actions-cell { display: flex; gap: 5px; white-space: nowrap; }
.clickable { color: var(--pine); cursor: pointer; }

.empty {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 34px;
  text-align: center;
  color: var(--muted);
  background: rgba(255,255,255,.5);
}

.tree-list, .tree-list ul { list-style: none; margin: 0; padding-left: 22px; }
.tree-root { padding-left: 0 !important; }
.tree-list li { position: relative; padding: 4px 0; }
.tree-list li::before {
  content: "";
  position: absolute;
  left: -15px;
  top: 0;
  bottom: 50%;
  width: 14px;
  border-left: 1px solid #9db0ae;
  border-bottom: 1px solid #9db0ae;
}
.tree-list > li:first-child::before { border: 0; }
.tree-list li:last-child::after {
  content: "";
  position: absolute;
  left: -15px;
  top: 50%;
  bottom: 0;
  width: 1px;
  background: transparent;
}
.tree-person {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 7px 11px;
}
.tree-person strong { font-size: 14px; }
.tree-person small { color: var(--muted); }

.modal {
  position: fixed;
  z-index: 40;
  inset: 0;
  margin: auto;
  width: min(880px, calc(100vw - 24px));
  max-height: calc(100vh - 40px);
  overflow: auto;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
  padding: 22px;
}
.overlay { position: fixed; inset: 0; background: rgba(10,30,32,.45); z-index: 30; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal-head h3 { margin: 0; font-size: 20px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.form-full { grid-column: 1 / -1; }
.form-section-title { grid-column: 1 / -1; margin: 8px 0 0; color: var(--pine); font-weight: 700; border-bottom: 1px solid var(--line); padding-bottom: 6px; }
.row-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.muted { color: var(--muted); }
.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: #163a3c;
  color: #fff;
  border-radius: 8px;
  padding: 10px 16px;
  box-shadow: var(--shadow);
  z-index: 50;
}
.toast.error { background: var(--accent-dark); }

.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tab { border: 0; background: transparent; padding: 10px 14px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.workbench { display: grid; grid-template-columns: 238px minmax(0, 1fr); gap: 16px; align-items: start; }
.workbench-menu {
  position: sticky;
  top: 20px;
  display: grid;
  gap: 12px;
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.menu-group { display: grid; gap: 4px; }
.menu-group + .menu-group { border-top: 1px solid var(--line); padding-top: 12px; }
.menu-title { padding: 2px 9px 5px; color: var(--muted); font-size: 12px; font-weight: 700; }
.menu-item {
  width: 100%;
  border: 0;
  border-radius: 6px;
  padding: 9px 10px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  line-height: 1.4;
}
.menu-item:hover { background: #eef3f2; }
.menu-item.active { background: var(--pine); color: #fff; font-weight: 600; }
.workbench-body { min-width: 0; }
.workbench-body > .panel:first-child { margin-top: 0; }
.pagoda-panel { padding: 0; overflow: hidden; }
.pagoda-head { margin: 0; padding: 18px 18px 0; align-items: center; }
.pagoda-stat {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 11px;
  color: var(--muted);
  background: #f8faf9;
  font-size: 12px;
}
.pagoda-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 13px 18px;
}
.pagoda-stage {
  position: relative;
  height: min(72vh, 780px);
  min-height: 540px;
  overflow: hidden;
  border-top: 1px solid var(--line);
  background: #f5f0eb;
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.pagoda-stage.dragging { cursor: grabbing; }
#pagodaSvg { display: block; width: 100%; height: 100%; }
.pagoda-level-line { stroke: #b08968; stroke-width: 2; }
.pagoda-edge {
  fill: none;
  stroke: #b08968;
  stroke-width: 2.5;
  stroke-linejoin: round;
}
.pagoda-node rect { cursor: pointer; transition: filter .15s ease; }
.pagoda-node:hover rect { filter: brightness(.94); }
.pagoda-node-name {
  pointer-events: none;
  font-size: 13px;
  font-weight: 700;
  fill: #333;
}
.pagoda-node-meta {
  pointer-events: none;
  font-size: 10px;
  fill: #888;
}
.pagoda-node-spouse {
  pointer-events: none;
  font-size: 9px;
  fill: #777;
}
.pagoda-collapse {
  fill: #fff;
  stroke: #b08968;
  stroke-width: 1.5;
  cursor: pointer;
}
.pagoda-collapse-mark {
  pointer-events: none;
  fill: #b08968;
  font-size: 14px;
  font-weight: 700;
}
.pagoda-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
}
.pagoda-export summary { cursor: pointer; color: var(--pine); font-weight: 700; }
.pagoda-export[open] summary { margin-bottom: 14px; }
.pagoda-print-root { display: none; }
.section-sheet { padding-top: 8px; padding-bottom: 8px; }
.section-note { margin: 4px 0 0; }
.section-editor { padding: 16px 0; border-top: 1px solid var(--line); }
.section-editor:first-of-type { border-top: 0; }
.section-editor-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 9px; }
.section-editor-head h4 { margin: 0; font-size: 15px; }
.section-editor textarea { min-height: 170px; }
.preface-list, .attachment-list, .lineage-list { display: grid; gap: 10px; }
.preface-card {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}
.preface-card h4 { margin: 0 0 6px; }
.preface-card p { margin: 0; color: var(--muted); line-height: 1.6; white-space: pre-wrap; }
.asset-editor { display: grid; grid-template-columns: 170px minmax(0, 1fr); gap: 18px; align-items: start; }
.cover-preview {
  width: 150px;
  aspect-ratio: 210 / 297;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #eef3f2;
}
.asset-actions { display: grid; justify-items: start; gap: 10px; align-content: start; }
.asset-actions p { margin: 0; font-size: 12px; }
.attachment-card {
  display: grid;
  grid-template-columns: 82px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
}
.attachment-card img, .pdf-mark {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 6px;
  background: #eef3f2;
}
.pdf-mark { display: grid; place-items: center; color: var(--accent); font-weight: 700; }
.attachment-card strong, .attachment-card small { display: block; }
.attachment-card small { color: var(--muted); margin-top: 5px; }
.lineage-card {
  display: grid;
  grid-template-columns: 140px minmax(0, 1fr);
  gap: 28px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  padding: 18px 28px;
}
.lineage-photo {
  width: 96px;
  height: 96px;
  justify-self: center;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.lineage-photo span { font-size: 68px; line-height: 1; }
.lineage-photo img { width: 96px; height: 96px; object-fit: cover; border-radius: 8px; }
.lineage-content h4 { margin: 0 0 14px; font-size: 18px; }
.lineage-content p { margin: 6px 0; line-height: 1.55; }
.lineage-content .lineage-bio { color: var(--muted); margin-top: 10px; }
.share-url { display: flex; gap: 8px; align-items: center; }
.share-url code { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.memorial-grid {
  display: grid;
  grid-template-columns: minmax(260px, 340px) 1fr;
  gap: 14px;
  align-items: start;
}
.memorial-side { display: grid; gap: 10px; }
.persona-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  padding: 12px;
  display: grid;
  gap: 8px;
}
.persona-card .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.persona-card h4 { margin: 0; }
.persona-card p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #b9c6c5; display: inline-block; }
.status-dot.on { background: #3d9a6e; }

.chat-main {
  background: #edf0ef;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  height: min(720px, calc(100vh - 180px));
  min-height: 520px;
}
.chat-head {
  padding: 12px 16px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.chat-head h3 { margin: 0; }
.chat-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.msg-row { display: flex; gap: 8px; align-items: flex-end; }
.msg-row.user { justify-content: flex-end; }
.avatar {
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  border-radius: 8px;
  background: #dbe3e2;
  color: var(--pine);
  display: grid;
  place-items: center;
  font-weight: 700;
}
.msg-row.user .avatar { background: var(--pine); color: #fff; order: 2; }
.bubble {
  max-width: min(68%, 560px);
  background: #fff;
  padding: 9px 12px;
  border-radius: 9px 9px 9px 3px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.msg-actions { margin-top: 6px; }
.msg-actions .btn { margin-right: 6px; }
.msg-row.user .bubble {
  background: #a8e06b;
  border-radius: 9px 9px 3px 9px;
}
.msg-time { display: block; font-size: 11px; color: #7c8b89; margin-top: 4px; }
.voice-section {
  border-top: 1px solid var(--line);
  padding-top: 12px;
  margin-top: 6px;
}
.voice-fields { display: grid; gap: 10px; margin-top: 10px; }
.voice-field { display: grid; gap: 10px; }
.voice-field p { margin: 0; }
.chat-input-bar {
  border-top: 1px solid var(--line);
  background: #fff;
  padding: 10px;
  display: flex;
  gap: 8px;
}
.chat-input-bar textarea { flex: 1; min-height: 42px; max-height: 120px; }
.chat-empty {
  margin: auto;
  color: var(--muted);
  text-align: center;
  max-width: 320px;
}
.greeting-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  border-top: 1px solid var(--line);
  padding: 9px 0;
}
.greeting-row:first-of-type { border-top: 0; }

@media (max-width: 760px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; align-items: center; justify-content: space-between; }
  .side-nav { display: flex; }
  .logout-wrap { margin-top: 0; }
  .content { padding: 20px 14px 40px; }
  .workbench { grid-template-columns: 1fr; }
  .workbench-menu { position: static; grid-template-columns: 1fr; }
  .asset-editor, .lineage-card { grid-template-columns: 1fr; }
  .lineage-photo { justify-self: start; }
  .preface-card { flex-direction: column; }
  .attachment-card { grid-template-columns: 64px minmax(0, 1fr); }
  .attachment-card .actions-cell { grid-column: 1 / -1; }
  .tree-list, .tree-list ul { padding-left: 18px; }
  .memorial-grid { grid-template-columns: 1fr; }
  .chat-main { height: calc(100vh - 150px); }
  .bubble { max-width: 82%; }
  .pagoda-stage { height: 64vh; min-height: 420px; }
  .pagoda-head { align-items: flex-start; }
}

@media print {
  @page { size: landscape; margin: 8mm; }
  body.pagoda-printing > :not(.pagoda-print-root) { display: none !important; }
  body.pagoda-printing .pagoda-print-root {
    display: block !important;
    position: fixed;
    inset: 0;
    background: #f5f0eb;
  }
  body.pagoda-printing .pagoda-print-root svg {
    display: block;
    width: 100vw;
    height: 100vh;
  }
}

.conversation-layout {
  display: grid;
  grid-template-columns: minmax(240px, 340px) minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}
.call-panel {
  position: sticky;
  top: 18px;
  display: grid;
  gap: 8px;
  padding: 14px;
  border-radius: 8px;
  background: #1f2928;
  color: #fff;
}
.call-media {
  aspect-ratio: 3 / 4;
  min-height: 320px;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, .72);
  background: linear-gradient(160deg, #50615e, #202827);
}
.call-media img,
.call-media video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.call-fallback { font-size: 64px; color: #fff; }
.call-panel strong { font-size: 17px; }
.call-state { color: rgba(255, 255, 255, .72); font-size: 12px; }
.distill-result { display: grid; gap: 10px; }
.distill-block {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  background: #fbfaf7;
}
.distill-block h4 { margin: 0 0 7px; font-size: 14px; color: var(--pine); }
.distill-block p { margin: 0; white-space: pre-wrap; line-height: 1.7; color: var(--ink); }

@media (max-width: 900px) {
  .conversation-layout { grid-template-columns: 1fr; }
  .call-panel { position: static; }
  .call-media { min-height: 260px; aspect-ratio: 4 / 3; }
}