/* 朱氏头针 Web 3D 版 —— 布局与配色（对齐桌面版） */
:root {
  --accent: #1f4e79;
  --txt: #243447;
  --muted: #7b8794;
  --line: #dde3ea;
  --soft: #eef2f7;
  --panel: #ffffff;
  --bg: #eef2f7;
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--txt);
  font-family: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB",
               "Noto Sans CJK SC", "SimHei", sans-serif;
}

#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: 60% 40%;
  overflow: hidden;
}

/* ---------------- 左侧 3D ---------------- */
#stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

#canvasWrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
}

#c { display: block; width: 100%; height: 100%; touch-action: none; }

#labels {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.pt-label {
  font-size: 11px;
  line-height: 1;
  color: #000;
  text-shadow: 0 0 3px #eef2f7, 0 0 3px #eef2f7;
  white-space: nowrap;
  pointer-events: none;
  transform: translate(-50%, -50%);
}
.pt-label.off { color: #8b98a5; }
.seg-label {
  font-size: 12px;
  font-weight: 700;
  color: #000;
  text-shadow: 0 0 3px #f7fafc, 0 0 3px #f7fafc;
  white-space: nowrap;
  pointer-events: none;
  transform: translate(-50%, -50%);
}

.legend {
  position: absolute;
  pointer-events: none;
  font-size: 11px;
  line-height: 1.55;
  color: #5a6570;
}
.legend-bands { right: 8px; bottom: 8px; text-align: right; }
.legend-mer   { left: 8px;  bottom: 8px; }
.legend-mer .t { color: #5a6570; }
.legend-mer .h { color: #5a6570; margin-top: 2px; }

.hud {
  position: absolute;
  left: 50%;
  top: 8px;
  transform: translateX(-50%);
  max-width: 88%;
  padding: 4px 10px;
  border-radius: 14px;
  background: rgba(255, 255, 255, .88);
  border: 1px solid var(--line);
  font-size: 12px;
  color: var(--txt);
  opacity: 0;
  transition: opacity .18s;
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hud.show { opacity: 1; }

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  color: var(--muted);
  background: var(--bg);
}
.loading.done { display: none; }
.load-fail {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  max-width: 82%;
  text-align: center;
}
.load-fail p { margin: 0; font-size: 13px; color: #b03a2e; line-height: 1.6; }
.load-fail .lf-hint { font-size: 12px; color: var(--muted); }
.load-fail button {
  appearance: none;
  border: 0;
  border-radius: 8px;
  padding: 10px 26px;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  font-family: inherit;
}
.spin {
  width: 16px; height: 16px;
  border: 2px solid #c8d2dd;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: sp .8s linear infinite;
}
@keyframes sp { to { transform: rotate(360deg); } }

/* ---------------- 底部按钮 ---------------- */
#toolbar {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid var(--line);
}
#toolbar button {
  appearance: none;
  border: 0;
  border-radius: 8px;
  padding: 10px 4px;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  font-family: inherit;
}
#toolbar button[data-act="clear"] { background: #8e44ad; }
#toolbar button[data-act="all"]   { background: var(--accent); }
#toolbar button[data-act="print"] { background: #d35400; }
#toolbar button[data-act="share"] { background: #27ae60; }
#toolbar button.ghost {
  background: #fff;
  color: var(--accent);
  border: 1px solid var(--accent);
}
#toolbar button.ghost.off { color: var(--muted); border-color: var(--line); }
#toolbar button:active { filter: brightness(.92); }

/* ---------------- 右侧面板 ---------------- */
#panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-left: 1px solid #c8d2dd;
}

.p-head { padding: 14px 16px 8px; border-bottom: 1px solid var(--line); }
.p-head h1 {
  margin: 0 0 6px;
  font-size: 19px;
  color: var(--accent);
  letter-spacing: .5px;
}
.p-head p { margin: 0; font-size: 11px; color: var(--muted); line-height: 1.5; }
/* 移动端抽屉的显式“收起”按钮（桌面固定双栏，不需要） */
.p-close { display: none; }

.p-search {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px 8px;
}
.p-search label { font-size: 12px; color: var(--muted); }
.p-search input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  color: var(--txt);
  border: 1px solid #9fb6c4;
  border-bottom-width: 2px;
  border-radius: 6px;
  outline: none;
  background: #fff;
}
.p-search input:focus { border-color: var(--accent); }
.p-search button {
  appearance: none;
  border: 0;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  font-family: inherit;
  color: #fff;
  background: var(--accent);
}

.p-listhead {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 16px 6px;
}
.p-label { font-size: 18px; font-weight: 700; }
.p-page { margin-left: auto; font-size: 11px; color: var(--muted); }
.pager { display: flex; flex-direction: column; gap: 2px; }
.pager button {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--accent);
  border-radius: 4px;
  font-size: 9px;
  line-height: 1;
  padding: 2px 6px;
}

#names {
  list-style: none;
  margin: 0;
  padding: 0 8px;
  flex: 0 0 auto;
  overflow: hidden;
}
#names li {
  padding: 5px 8px;
  font-size: 14.4px;
  line-height: 1.25;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#names li .cnt { color: var(--muted); font-size: 12px; }
#names li.on { color: var(--accent); font-weight: 700; background: #f2f6fa; }
#names li:active { background: #eef2f7; }

.p-detail {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--line);
  padding: 8px 16px 12px;
  overflow: hidden;
}
.p-detailhead {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding-bottom: 6px;
}
.p-dtitle { font-size: 13px; font-weight: 700; color: var(--accent); }

#plans { flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.plan {
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  margin-bottom: 2px;
}
.plan:active { background: #f2f6fa; }
.plan.on { background: #eef2f7; }
.plan .cat { font-size: 13.3px; color: var(--muted); }
.plan.on .cat { color: var(--accent); font-weight: 700; }
.plan .toks {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 10px;
  margin-top: 2px;
}
.plan .tok { font-size: 13.3px; }
.plan.on .tok { font-weight: 700; }
.plan-empty { font-size: 12px; color: var(--muted); padding: 6px 2px; }

/* ---------------- 移动端：底部抽屉 ---------------- */
@media (max-width: 720px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  #stage { grid-row: 1; }

  #panel {
    grid-row: 2;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(52px + env(safe-area-inset-bottom));   /* 让底部按钮始终可用 */
    z-index: 20;
    height: 50px;
    max-height: 62vh;
    overflow: hidden;
    border-left: 0;
    border-top: 1px solid #c8d2dd;
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -8px 24px rgba(31, 78, 121, .18);
    transition: height .24s ease;
  }
  #panel.open { height: 66vh; }

  /* 抽屉收起时只露出标题把手（点标题栏或“收起”按钮收起） */
  .p-head {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 8px;
    height: 50px;
    padding: 0 12px 0 16px;
    cursor: pointer;
  }
  .p-head h1 { flex: 1 1 auto; font-size: 15px; margin: 0; }
  .p-head h1::after { content: "  ▴"; color: var(--muted); font-size: 12px; }
  #panel.open .p-head h1::after { content: "  ▾"; }
  .p-head p { display: none; }

  /* 展开后始终有一个明确的收起入口，避免盖住头模无法还原 */
  .p-close {
    display: none;
    flex: 0 0 auto;
    appearance: none;
    border: 1px solid var(--accent);
    border-radius: 14px;
    padding: 4px 12px;
    font-size: 12px;
    font-family: inherit;
    color: var(--accent);
    background: #fff;
  }
  #panel.open .p-close { display: block; }

  .legend { font-size: 9.5px; line-height: 1.4; }
  /* 抽屉收起时固定占据视口底部 52~102px：图例改用视口定位，稳定落在抽屉上方 */
  .legend-bands {
    position: fixed;
    right: 8px;
    bottom: calc(108px + env(safe-area-inset-bottom));
  }
  .legend-mer {
    position: fixed;
    left: 8px;
    bottom: calc(108px + env(safe-area-inset-bottom));
  }

  #toolbar { padding: 6px 8px 8px; }
  #toolbar button { font-size: 13px; padding: 8px 2px; }

  #names { flex: 0 1 auto; max-height: 30vh; overflow-y: auto; }
  .p-detail { flex: 1 1 auto; min-height: 72px; }
}

@media (max-width: 420px) {
  .pt-label { font-size: 9.5px; }
  .seg-label { font-size: 10.5px; }
}

/* ---------------- 手机端 PDF 保存弹窗 ---------------- */
#pdfModal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(18, 32, 48, .45);
}
#pdfModal .pm-card {
  width: 100%;
  max-width: 330px;
  max-height: 86vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: #fff;
  border-radius: 14px;
  padding: 18px 18px 16px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .28);
}
#pdfModal .pm-prev {
  display: block;
  width: 100%;
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
#pdfModal .pm-title { font-size: 16px; font-weight: 700; color: var(--accent); margin-bottom: 8px; }
#pdfModal .pm-txt { margin: 0 0 14px; font-size: 12.5px; line-height: 1.6; color: var(--muted); }
#pdfModal .pm-dl {
  display: block;
  text-align: center;
  padding: 12px;
  border-radius: 10px;
  background: #27ae60;
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
}
#pdfModal .pm-dl:active { filter: brightness(.92); }
#pdfModal .pm-open {
  display: block;
  margin-top: 10px;
  text-align: center;
  font-size: 13px;
  color: var(--accent);
  text-decoration: underline;
}
#pdfModal .pm-close {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--muted);
  font-size: 14px;
  font-family: inherit;
}

/* ---------------- 打印 ---------------- */
#printRoot { display: none; }

@page {
  size: A4 portrait;
  margin: 10mm;
}

@media print {
  html, body { height: auto; overflow: visible; background: #fff; }
  #app { display: none !important; }
  #printRoot { display: block !important; }

  #printRoot .sheet {
    width: 190mm;
    min-height: 253mm;
    padding: 0;
    color: #1a252f;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  #printRoot .bar {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    background: #2c3e50;
    color: #fff;
    padding: 6mm 7mm;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  #printRoot .bar h2 { margin: 0; font-size: 20pt; }
  #printRoot .bar .ts { font-size: 11pt; color: #ecf0f1; }
  #printRoot .cols { display: block; padding: 6mm 7mm 0; }
  #printRoot .shot { width: 100%; max-width: 150mm; margin: 0 auto 4mm; }
  #printRoot .shot img {
    width: 100%;
    display: block;
    border: 1px solid #2c3e50;
  }
  #printRoot .plans { flex: 1 1 auto; min-width: 0; }
  #printRoot .plans h3 { margin: 0 0 2mm; font-size: 15pt; color: #2c3e50; }
  #printRoot .dis { font-size: 12pt; margin-bottom: 2mm; }
  #printRoot .entry { margin-bottom: 3mm; page-break-inside: avoid; }
  #printRoot .entry .cat { font-size: 10pt; color: #7f8c8d; }
  #printRoot .entry .toks {
    display: flex;
    flex-wrap: wrap;
    gap: 2mm 7mm;
    margin-top: 1mm;
  }
  #printRoot .entry .tok { font-size: 11.5pt; }
  #printRoot .foot {
    margin: 5mm 7mm 0;
    padding-top: 3mm;
    border-top: 1px solid #bdc3c7;
  }
  #printRoot .foot h4 { margin: 0 0 1mm; font-size: 12pt; color: #34495e; }
  #printRoot .foot .chips {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5mm 4mm;
    font-size: 10.5pt;
    margin-bottom: 3mm;
  }
  #printRoot .foot .pts { font-size: 11pt; line-height: 1.45; word-break: break-all; }
  #printRoot .foot .none { color: #95a5a6; }
}
