:root {
  --bg: #f6f1e7;
  --ink: #3a3226;
  --ink-soft: #7a705e;
  --bar-bg: rgba(250, 246, 238, 0.92);
  --accent: #8b5a2b;
  --panel-bg: #fffdf7;
  --border: #d9cfba;
  --card-bg: #ffffff;
  --card-border: #d8cdb6;
  --hub-open: #e7ddc8;
  --halo: rgba(255, 255, 255, 0.55);
  --hit: #a33000;
  --accent-wash: rgba(139, 90, 43, 0.11);
  --sidebar-width: 320px;
}

html.dark {
  --bg: #1f1c16;
  --ink: #e6dec9;
  --ink-soft: #9a917c;
  --bar-bg: rgba(34, 30, 24, 0.92);
  --accent: #d3a05c;
  --panel-bg: #2a261e;
  --border: #4a4334;
  --card-bg: #332e24;
  --card-border: #5a5140;
  --hub-open: #4a4334;
  --halo: rgba(42, 38, 30, 0.72);
  --hit: #ff8a5c;
  --accent-wash: rgba(211, 160, 92, 0.14);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: "Noto Serif SC", "Songti SC", "STSong", serif;
  background: var(--bg);
  color: var(--ink);
}

/* ---------- library sidebar ---------- */
#library-sidebar {
  position: fixed;
  inset: 57px auto 0 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  width: var(--sidebar-width);
  max-width: 88vw;
  background: var(--panel-bg);
  border-right: 1px solid var(--border);
  box-shadow: 8px 0 24px rgba(60, 45, 20, 0.14);
  transform: translateX(-100%);
  transition: transform 0.24s ease;
}

html.library-open #library-sidebar { transform: translateX(0); }

.library-head {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 12px 9px 16px;
  border-bottom: 1px solid var(--border);
}
.library-head strong {
  font-size: 17px;
  color: var(--accent);
}
#library-total {
  margin-left: 7px;
  font-size: 11px;
  color: var(--ink-soft);
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 32px;
  flex: 0 0 auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-bg);
  color: var(--ink);
  font: inherit;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
.icon-button:hover,
.icon-button:focus-visible {
  border-color: var(--accent);
  color: var(--accent);
  outline: none;
}
#btn-library-close { border-color: transparent; background: transparent; }

.library-filter-wrap { padding: 12px 12px 8px; }
#library-filter {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card-bg);
  color: var(--ink);
  font-family: inherit;
  font-size: 13px;
  outline: none;
}
#library-filter:focus { border-color: var(--accent); }

#library-tree {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0 8px 16px;
}

.library-folder { margin: 2px 0; }
.library-folder > summary {
  display: flex;
  align-items: center;
  min-height: 34px;
  padding: 4px 7px;
  border-radius: 5px;
  list-style: none;
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.library-folder > summary::-webkit-details-marker { display: none; }
.library-folder > summary::before {
  content: '›';
  width: 15px;
  flex: 0 0 15px;
  color: var(--ink-soft);
  font-size: 17px;
  transform-origin: center;
  transition: transform 0.15s ease;
}
.library-folder[open] > summary::before { transform: rotate(90deg); }
.library-folder > summary:hover { background: var(--accent-wash); }
.library-folder-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.library-folder-count {
  margin-left: 8px;
  color: var(--ink-soft);
  font-size: 10px;
  font-weight: 400;
  font-variant-numeric: tabular-nums;
}
.library-subfolder { margin-left: 14px; }
.library-subfolder > summary { font-weight: 400; color: var(--ink-soft); }

.library-work-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 32px;
  align-items: stretch;
  margin: 1px 0 1px 29px;
  border-left: 2px solid transparent;
  border-radius: 4px;
}
.library-work-row:hover { background: var(--accent-wash); }
.library-work-row.active {
  border-left-color: var(--accent);
  background: var(--accent-wash);
}
.library-work-button,
.library-star {
  border: 0;
  background: transparent;
  color: var(--ink);
  font-family: inherit;
  cursor: pointer;
}
.library-work-button {
  min-width: 0;
  padding: 6px 5px 6px 8px;
  overflow: hidden;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12.5px;
}
.library-work-button:focus-visible,
.library-star:focus-visible {
  outline: 1px solid var(--accent);
  outline-offset: -2px;
}
.library-star {
  width: 32px;
  font-size: 17px;
  color: var(--ink-soft);
}
.library-star.is-favorite { color: #c18424; }
.library-empty {
  padding: 12px 10px 12px 29px;
  color: var(--ink-soft);
  font-size: 12px;
}
.library-search-empty { padding: 24px 12px; text-align: center; }

#library-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  border: 0;
  background: rgba(31, 28, 22, 0.34);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- top bar ---------- */
#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--bar-bg);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--border);
  min-width: 0;
  transition: left 0.24s ease;
}

#brand {
  flex: 0 0 auto;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 4px;
  color: var(--accent);
  white-space: nowrap;
}

#current-work {
  flex: 0 1 300px;
  max-width: 300px;
  min-width: 0;
  overflow: hidden;
  color: var(--ink-soft);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#toolbar {
  flex: 1 1 auto;
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
#toolbar::-webkit-scrollbar { display: none; }

#toolbar .sep {
  width: 1px; height: 20px;
  background: var(--border);
  margin: 0 4px;
}

#search {
  width: 220px;
  padding: 5px 10px;
  font-family: inherit;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel-bg);
  color: var(--ink);
  outline: none;
}
#search:focus { border-color: var(--accent); }

.search-menu {
  position: fixed;
  z-index: 25;
  width: min(300px, calc(100vw - 16px));
  max-height: min(70vh, 420px);
  overflow-y: auto;
  padding: 8px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 28px rgba(60, 45, 20, 0.2);
}
.search-menu[hidden] { display: none; }
.search-menu-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 3px 4px 8px;
  color: var(--ink-soft);
  font-size: 12px;
}
#btn-clear-search-history {
  padding: 2px 4px;
  border: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
}
#btn-clear-search-history:hover,
#btn-clear-search-history:focus-visible {
  text-decoration: underline;
  outline: none;
}
#btn-clear-search-history:disabled {
  color: var(--ink-soft);
  cursor: default;
  opacity: 0.55;
  text-decoration: none;
}
.search-history { display: grid; gap: 2px; }
.search-history-empty {
  padding: 18px 8px;
  color: var(--ink-soft);
  font-size: 12px;
  text-align: center;
}
.search-history-item {
  display: flex;
  align-items: center;
  width: 100%;
  min-width: 0;
  padding: 7px 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  line-height: 1.35;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.search-history-item::before {
  content: '↺';
  flex: 0 0 auto;
  margin-right: 8px;
  color: var(--ink-soft);
  font-family: Arial, sans-serif;
}
.search-history-item:hover,
.search-history-item:focus-visible {
  background: var(--accent-wash);
  outline: none;
}
.search-nav {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) 34px;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}
.search-nav button {
  display: grid;
  place-items: center;
  width: 34px;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-bg);
  color: var(--ink);
  font: inherit;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.search-nav button:hover,
.search-nav button:focus-visible {
  border-color: var(--accent);
  color: var(--accent);
  outline: none;
}
.search-nav button:disabled {
  color: var(--ink-soft);
  cursor: default;
  opacity: 0.45;
}
#search-info {
  min-width: 0;
  color: var(--ink-soft);
  font-size: 12px;
  text-align: center;
}

#toolbar button, #toolbar select {
  font-family: inherit;
  font-size: 14px;
  padding: 4px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-bg);
  color: var(--ink);
  cursor: pointer;
}
#toolbar button:hover { border-color: var(--accent); color: var(--accent); }
#toolbar button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ---------- stage ---------- */
#stage { position: absolute; inset: 0; transition: left 0.24s ease; }

#mindmap {
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
}
#mindmap:active { cursor: grabbing; }

/* ---------- nodes & links ---------- */
.link {
  fill: none;
  stroke-linecap: round;
}

.node { cursor: pointer; }

.node text {
  font-family: "Noto Serif SC", "Songti SC", "STSong", serif;
  fill: var(--ink);
  paint-order: stroke;
  stroke: var(--bg);
  stroke-width: 3px;
  stroke-linejoin: round;
}

.node.dim text { opacity: 0.35; }
.node.dim circle { opacity: 0.35; }

.node circle {
  stroke-width: 1.6px;
}

.node text.count {
  font-size: 9px;
  stroke: none;
  font-weight: 700;
}

.node .info { cursor: pointer; }
.node .info circle {
  stroke: #fff;
  stroke-width: 1.4px;
}
.node .info text {
  font-size: 9px;
  fill: #fff;
  stroke: none;
  text-anchor: middle;
}
.node .info:hover circle { stroke: #d98a2b; fill: #b56f1d; }

.node.hit text.label { fill: var(--hit) !important; font-weight: 700; }

.root-node rect.card {
  fill: var(--card-bg);
  stroke: var(--card-border);
  stroke-width: 1.2px;
  filter: drop-shadow(0 2px 5px rgba(60, 45, 20, 0.18));
}
.root-node text.label {
  font-weight: 700;
  fill: var(--ink);
}

/* ---------- 颂词节点（📖 开启后作为最后一层排入布局） ---------- */
.node rect.notecard {
  fill: var(--card-bg);
  stroke-width: 1px;
  stroke-opacity: 0.45;
  filter: drop-shadow(0 1px 3px rgba(60, 45, 20, 0.12));
}
.node text.notetext {
  font-size: 12px;
  fill: var(--ink);
  stroke: none;
}

/* ---------- 原文浮卡（节点旁就地弹出，Wordflex 式） ---------- */
.popcard {
  position: absolute;
  cursor: grab;
  touch-action: none;
  width: 340px;
  max-width: calc(100vw - 24px);
  max-height: 46vh;
  background: var(--panel-bg);
  border: 1px solid #e0b070;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(60, 45, 20, 0.22);
  padding: 14px 18px;
  overflow-y: auto;
  z-index: 9;
  transition: opacity 0.18s ease;
}
.popcard.hidden { opacity: 0; pointer-events: none; }
.popcard.dragging { cursor: grabbing; user-select: none; }
.popcard-text { cursor: auto; }

.popcard::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: #d98a2b;
  border-radius: 12px 12px 0 0;
}

.popcard-close {
  position: absolute;
  top: 6px; right: 9px;
  border: none; background: none;
  font-size: 18px;
  color: var(--ink-soft);
  cursor: pointer;
}
.popcard-close:hover { color: var(--ink); }

.popcard-path {
  font-size: 11px;
  color: var(--ink-soft);
  line-height: 1.6;
  margin-bottom: 6px;
  padding-right: 18px;
}

.popcard-title {
  font-size: 15px;
  line-height: 1.5;
  margin-bottom: 8px;
  color: var(--accent);
}

.popcard-text {
  font-size: 13.5px;
  line-height: 1.9;
  white-space: pre-wrap;
}

/* ---------- 物理参数 / 层级颜色面板 ---------- */
#color-panel,
#physics-panel {
  position: absolute;
  top: 56px; right: 12px;
  width: 252px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(60, 45, 20, 0.16);
  padding: 12px 14px;
  z-index: 11;
  font-size: 12px;
}
#color-panel.hidden,
#physics-panel.hidden { display: none; }

#color-panel .pp-head,
#physics-panel .pp-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
}
#color-panel .pp-head button,
#physics-panel .pp-head button {
  font-family: inherit;
  font-size: 11px;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--panel-bg);
  color: var(--ink-soft);
  cursor: pointer;
}
#color-panel .pp-head button:hover,
#physics-panel .pp-head button:hover { color: var(--accent); border-color: var(--accent); }

#color-panel { max-height: 70vh; overflow-y: auto; }
#color-panel .cp-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 5px 0;
}
#color-panel .cp-row input[type="color"] {
  width: 44px; height: 22px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: none;
  cursor: pointer;
}

#physics-panel .pp-row {
  display: grid;
  grid-template-columns: 64px 1fr 40px;
  align-items: center;
  gap: 8px;
  margin: 6px 0;
  color: var(--ink);
}
#physics-panel .pp-row input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
#physics-panel .pp-row .pp-val {
  text-align: right;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

/* ---------- lazy-loading status / hint ---------- */
.load-status {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 2;
  width: min(420px, calc(100vw - 48px));
  transform: translate(-50%, -50%);
  padding: 12px 18px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-bg);
  color: var(--ink-soft);
  font-size: 15px;
  line-height: 1.6;
  text-align: center;
  overflow-wrap: anywhere;
  pointer-events: none;
  box-shadow: 0 8px 24px rgba(60, 45, 20, 0.12);
}
.load-status[data-state="error"] {
  border-color: #c66a5e;
  color: #a03d32;
}

#hint {
  position: absolute;
  bottom: 10px; left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  color: var(--ink-soft);
  background: var(--bar-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 4px 14px;
  pointer-events: none;
  white-space: nowrap;
}

/* ---------- 版本与联系信息 ---------- */
.info-toggle {
  position: fixed;
  right: 14px;
  bottom: 14px;
  z-index: 14;
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--panel-bg);
  color: var(--ink-soft);
  font: inherit;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(60, 45, 20, 0.16);
}
.info-toggle span {
  font-family: Arial, sans-serif;
  font-size: 14px;
  line-height: 1;
}
.info-toggle:hover,
.info-toggle:focus-visible,
.info-toggle[aria-expanded="true"] {
  border-color: var(--accent);
  color: var(--accent);
  outline: none;
}

.info-panel {
  position: fixed;
  right: 14px;
  bottom: 52px;
  z-index: 15;
  width: min(320px, calc(100vw - 32px));
  max-height: min(70vh, 420px);
  overflow-y: auto;
  padding: 16px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 28px rgba(60, 45, 20, 0.2);
}
.info-panel[hidden] { display: none; }
.info-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.info-panel h2 {
  color: var(--accent);
  font-size: 16px;
  line-height: 1.4;
}
.info-panel-close {
  width: 28px;
  height: 28px;
  border: 0;
  background: transparent;
  color: var(--ink-soft);
  font: inherit;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.info-panel-close:hover,
.info-panel-close:focus-visible {
  color: var(--accent);
  outline: none;
}
.info-list { display: grid; gap: 8px; }
.info-item {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 12px;
  align-items: baseline;
  font-size: 13px;
  line-height: 1.5;
}
.info-item dt { color: var(--ink-soft); }
.info-item dd { color: var(--ink); overflow-wrap: anywhere; }
.info-note {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  color: var(--ink-soft);
  font-size: 12px;
  line-height: 1.7;
}

@media (min-width: 1101px) {
  html.library-open #stage { left: var(--sidebar-width); }
}

@media (max-width: 1100px) {
  html.library-open #library-backdrop {
    opacity: 1;
    pointer-events: auto;
  }
  #current-work { display: none; }
}

@media (max-width: 760px) {
  #topbar { gap: 6px; padding-inline: 8px; }
  #brand { font-size: 18px; letter-spacing: 2px; }
  #search { width: 120px; }
  #hint { display: none; }
  .popcard { width: calc(100vw - 24px); }
  .info-toggle { right: 10px; bottom: 10px; }
  .info-panel { right: 10px; bottom: 46px; width: calc(100vw - 20px); }
}

/* ---------- 弹窗虚线 ---------- */
#tethers {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 8;
}
#tethers line {
  stroke-dasharray: 4 5;
  stroke-width: 1.4px;
  opacity: 0.65;
}
