:root {
  --bg: #ece6d8;
  --fg: #2d2822;
  --muted: #7d7265;
  --line: rgba(70, 56, 40, 0.13);
  --panel: rgba(252, 249, 243, 0.52);
  --accent: #5a8a72;
  --seal: #b03a32;
  --good: #5f9a7b;
  --warn: #b4883c;
  --bad: #b0584c;
  --ink: 70, 56, 40;
  --heart: #b03a32;
  --glass-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.55), 0 6px 24px rgba(90, 70, 40, 0.08);
  --dot-off: #b8ad9d;
  --btn-bg: rgba(255, 255, 255, 0.5);
  --btn-hover: rgba(255, 255, 255, 0.85);
  --primary-bg: #b03a32;
  --primary-fg: #f7efe4;
  --seg-active-bg: rgba(255, 255, 255, 0.85);
  --breath-color: rgba(50, 42, 34, 0.85);
  --breath-shadow: none;
  --banner-bg: rgba(250, 246, 238, 0.88);
  --banner-shadow: 0 4px 18px rgba(90, 70, 40, 0.1);
  --summary-bg: rgba(250, 246, 238, 0.9);
  --closing-color: rgba(50, 42, 34, 0.78);
  --drawer-bg: rgba(248, 244, 236, 0.96);
  --drawer-shadow: -10px 0 40px rgba(90, 70, 40, 0.1);
  --field-bg: rgba(255, 255, 255, 0.6);
  --coach-color: rgba(40, 33, 27, 0.92);
  --coach-shadow: 0 0 14px rgba(240, 234, 222, 0.95), 0 0 4px rgba(240, 234, 222, 0.9);
  --coach-bg: none;
  --kaiti: "Kaiti SC", "STKaiti", "KaiTi", "Noto Serif SC", "Songti SC", serif;
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}

body { overflow: hidden; }

#stage {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  z-index: 0;
}

.glass {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--glass-shadow);
  backdrop-filter: blur(14px) saturate(1.05);
  -webkit-backdrop-filter: blur(14px) saturate(1.05);
}

.lab { font-size: 12px; letter-spacing: 0.14em; color: var(--muted); }
.unit { font-size: 14px; color: var(--muted); }

/* ---------- 布局 ---------- */
#app {
  position: relative;
  z-index: 1;
  height: 100vh;
  padding: 16px 20px 20px;
  display: grid;
  gap: 16px;
  grid-template-columns: minmax(220px, 290px) 1fr minmax(190px, 240px);
  grid-template-rows: auto minmax(0, 1fr) 176px;
  grid-template-areas:
    "top top top"
    "left slot right"
    "charts charts charts";
}

.top { grid-area: top; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; transition: opacity 0.8s; }
.left { grid-area: left; display: flex; flex-direction: column; gap: 14px; align-self: center; }
.right { grid-area: right; align-self: center; }
.slot { grid-area: slot; position: relative; display: flex; align-items: center; justify-content: center; min-height: 0; }
.charts { grid-area: charts; display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* ---------- 顶栏 ---------- */
.conn, .ctrl, .chips { display: flex; align-items: center; gap: 10px; padding: 8px 14px; font-size: 14px; }
.sep { width: 1px; height: 14px; background: var(--line); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--dot-off); }
.dot.on { background: var(--good); box-shadow: 0 0 0 3px rgba(95, 154, 123, 0.18); }
.dot.busy { background: var(--warn); animation: blink 1s infinite; }
.batt { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.sq { display: inline-flex; align-items: flex-end; gap: 2px; height: 14px; }
.sq i { width: 4px; border-radius: 1px; background: rgba(var(--ink), 0.18); }
.sq i:nth-child(1) { height: 5px; }
.sq i:nth-child(2) { height: 9px; }
.sq i:nth-child(3) { height: 14px; }
.sq.q-good i { background: var(--good); }
.sq.q-fair i:nth-child(-n+2) { background: var(--warn); }
.sq.q-poor i:nth-child(1) { background: var(--bad); }

.chips { padding: 5px; gap: 2px; flex-wrap: wrap; }
.chip {
  position: relative;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 12.5px;
  padding: 6px 12px;
  border-radius: 11px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.chip:hover { color: var(--fg); }
.chip.active { background: rgba(var(--ink), 0.1); color: var(--fg); }
.chip.live::before {
  content: "";
  display: inline-block;
  width: 6px; height: 6px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--accent);
  vertical-align: 1px;
}
.chip.event { color: var(--warn); }

.timer { font-size: 15px; min-width: 44px; }
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--btn-bg);
  color: var(--fg);
  font: inherit;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 11px;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
}
.btn:hover:not(:disabled) { background: var(--btn-hover); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.icon { padding: 6px 8px; display: inline-flex; align-items: center; }
.btn.primary { background: var(--primary-bg); border-color: transparent; color: var(--primary-fg); font-weight: 600; padding: 10px 22px; font-size: 15px; letter-spacing: 0.12em; }
.btn.primary:hover:not(:disabled) { background: var(--primary-bg); filter: brightness(1.08); }

.seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 11px; background: rgba(var(--ink), 0.07); }
.seg button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  padding: 5px 12px;
  border-radius: 9px;
  cursor: pointer;
  transition: background 0.3s, color 0.3s;
}
.seg button.active { background: var(--seg-active-bg); color: var(--fg); }

/* ---------- 心率 / 指标 ---------- */
.hrbox { padding: 16px 20px 18px; }
.hr-row { display: flex; align-items: baseline; gap: 8px; }
.hr { font-size: clamp(64px, 8vw, 112px); font-weight: 100; line-height: 1; letter-spacing: -0.02em; }
.heart { color: var(--heart); align-self: center; margin-left: auto; transform-origin: center; }
.hrbox.stale .hr, .tiles.stale .val { opacity: 0.35; }

.tiles { padding: 6px 20px; }
.tile { display: flex; align-items: baseline; gap: 8px; padding: 11px 0; border-bottom: 1px solid var(--line); }
.tile:last-child { border-bottom: 0; }
.tile .lab { flex: 1; }
.tile .val { font-size: 26px; font-weight: 200; }

/* ---------- 生理反馈 ---------- */
.fbbox { padding-top: 14px; }
.fb-title { margin-bottom: 2px; }
.fbbox .tile .val { font-size: 24px; }

/* ---------- 画面中央 ---------- */
/* 位置由 app.js 每帧跟随白玉兰设置 */
.breath { position: fixed; left: 0; top: 0; transform: translate(-50%, -50%); text-align: center; pointer-events: none; }
.breath b { display: block; font-family: var(--kaiti); font-size: clamp(20px, 2.1vw, 29px); font-weight: 400; letter-spacing: 0.4em; margin-right: -0.4em; color: var(--breath-color); text-shadow: var(--breath-shadow); transition: opacity 0.6s; }

.banner {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, -8px);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 15px;
  background: var(--banner-bg);
  border: 1px solid var(--line);
  box-shadow: var(--banner-shadow);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s, transform 0.3s;
}
.banner.show { opacity: 1; transform: translate(-50%, 0); }
.banner.warn { border-color: rgba(180, 136, 60, 0.45); color: var(--warn); }
.banner.error { border-color: rgba(176, 88, 76, 0.45); color: var(--bad); }
.spin { display: none; width: 14px; height: 14px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: rot 0.8s linear infinite; }
.banner.busy .spin { display: block; }

.summary {
  position: absolute;
  padding: 28px 34px;
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  background: var(--summary-bg);
}
.summary.show { display: flex; }
.s-title { font-family: var(--kaiti); font-size: 22px; font-weight: 500; letter-spacing: 0.2em; }
.s-grid { display: flex; gap: 30px; }
.s-grid div { display: flex; flex-direction: column; gap: 6px; align-items: center; }
.s-grid b { font-size: 30px; font-weight: 200; }
.s-closing { max-width: 360px; text-align: center; font-family: var(--kaiti); font-size: 15px; line-height: 1.9; letter-spacing: 0.1em; color: var(--closing-color); }

/* ---------- 曲线 ---------- */
.chart { padding: 12px 16px 10px; display: flex; flex-direction: column; min-width: 0; }
.chart-head { display: flex; justify-content: space-between; align-items: baseline; }
.chart .cur { font-size: 13px; color: var(--muted); }
.chart .cur b { font-size: 20px; font-weight: 400; color: var(--fg); }
.chart .plot { position: relative; flex: 1; min-height: 0; margin-top: 4px; }
.chart canvas { position: absolute; inset: 0; width: 100%; height: 100%; }

/* ---------- 配置面板 ---------- */
.drawer {
  position: fixed;
  z-index: 5;
  top: 0; right: 0; bottom: 0;
  width: 340px;
  max-width: 100vw;
  padding: 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  overflow-y: auto;
  background: var(--drawer-bg);
  border-left: 1px solid var(--line);
  box-shadow: var(--drawer-shadow);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  transform: translateX(100%);
  transition: transform 0.3s ease;
  scrollbar-width: thin;
  scrollbar-color: rgba(var(--ink), 0.2) transparent;
}
.drawer.open { transform: none; }
.drawer-head { display: flex; justify-content: space-between; align-items: center; font-family: var(--kaiti); font-size: 19px; font-weight: 500; letter-spacing: 0.12em; }
.drawer h3 { margin: 6px 0 -4px; padding-top: 14px; border-top: 1px solid var(--line); font-size: 13px; font-weight: 500; letter-spacing: 0.08em; color: var(--accent); }
.drawer-head + h3 { border-top: 0; padding-top: 0; }
.field { display: flex; flex-direction: column; gap: 8px; }
.f-lab { font-size: 13px; color: var(--muted); }
.f-lab em { font-style: normal; color: var(--fg); margin-left: 6px; }
select {
  appearance: none;
  background: var(--field-bg);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font: inherit;
  font-size: 14px;
}
select option { color: #222; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }

/* ---------- 患者视图：只保留白玉兰、菩提树与一个小的心率读数 ---------- */
.hr-mini {
  position: absolute;
  bottom: 2%;
  left: 50%;
  transform: translateX(-50%);
  display: none;
  align-items: baseline;
  gap: 8px;
  color: var(--muted);
  transition: opacity 0.6s;
}
.hr-mini svg { align-self: center; color: var(--heart); }
.hr-mini b { font-size: 32px; font-weight: 100; color: var(--fg); }
.hr-mini.stale b { opacity: 0.35; }
body.patient #app { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "top" "slot"; }
body.patient .left, body.patient .right, body.patient .charts { display: none; }
body.patient .hr-mini { display: flex; }
body.patient.idle .top { opacity: 0; pointer-events: none; }
body.patient.idle { cursor: none; }

/* ---------- 引导话术：底部字幕，淡入淡出 ---------- */
.coach {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 26%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0 8% 8%;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 1.4s ease;
  background: var(--coach-bg);
  /* 两侧渐隐，避免出现可见的底板边缘 */
  -webkit-mask-image: linear-gradient(to right, transparent, #000 18%, #000 82%, transparent);
  mask-image: linear-gradient(to right, transparent, #000 18%, #000 82%, transparent);
}
.coach.show { opacity: 1; }
.coach span {
  font-family: var(--kaiti);
  font-size: clamp(18px, 1.9vw, 26px);
  line-height: 1.7;
  letter-spacing: 0.18em;
  color: var(--coach-color);
  /* 水墨：纸色的晕边，压在山、树、地面的墨色上也读得清；夜景：深色晕边 */
  text-shadow: var(--coach-shadow);
}
body:not(.patient) .coach span { font-size: clamp(15px, 1.5vw, 21px); }

@keyframes blink { 50% { opacity: 0.35; } }
@keyframes rot { to { transform: rotate(360deg); } }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes appear { from { opacity: 0; } to { opacity: 1; } }

#stage { animation: appear 1.8s ease both; }
.left, .right, .charts { animation: rise 0.7s ease both; }
.right { animation-delay: 0.08s; }
.charts { animation-delay: 0.16s; }

:focus-visible { outline: 1.5px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
  #stage, .left, .right, .charts { animation: none; }
}

/* ---------- 窄屏 ---------- */
@media (max-width: 860px) {
  body { overflow: auto; }
  #app {
    height: auto;
    min-height: 100vh;
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    grid-template-areas: "top" "left" "slot" "right" "charts";
  }
  .left { flex-direction: row; align-self: stretch; }
  .left > * { flex: 1; }
  .hr { font-size: 60px; }
  .slot { min-height: 340px; }
  body.patient .slot { min-height: 72vh; }
  .charts { grid-template-columns: 1fr; }
  .chart { height: 170px; }
}

/* ---------- 夜景主题：只覆盖变量，结构与水墨主题完全一致 ---------- */
:root[data-theme="night"] {
  --bg: #0d1a2b;
  --fg: #e3e9f4;
  --muted: #8d9ab3;
  --line: rgba(255, 255, 255, 0.075);
  --panel: rgba(255, 255, 255, 0.04);
  --accent: #9fd0b4;
  --good: #7fcfb0;
  --warn: #e6c07b;
  --bad: #e0a4a0;
  --ink: 255, 255, 255;
  --kaiti: "Songti SC", "STSong", "Noto Serif SC", "Source Han Serif SC", "SimSun", serif;
  --heart: #9fd0b4;
  --glass-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 8px 30px rgba(3, 8, 16, 0.18);
  --dot-off: #8d9ab3;
  --btn-bg: rgba(255, 255, 255, 0.06);
  --btn-hover: rgba(255, 255, 255, 0.13);
  --primary-bg: #9fd0b4;
  --primary-fg: #0a1620;
  --seg-active-bg: rgba(255, 255, 255, 0.12);
  --breath-color: rgba(246, 248, 250, 0.9);
  --breath-shadow: 0 1px 16px rgba(0, 0, 0, 0.45);
  --banner-bg: rgba(16, 24, 42, 0.8);
  --banner-shadow: none;
  --summary-bg: rgba(14, 22, 40, 0.86);
  --closing-color: rgba(236, 240, 244, 0.82);
  --drawer-bg: rgba(12, 18, 34, 0.94);
  --drawer-shadow: none;
  --field-bg: rgba(255, 255, 255, 0.06);
  --coach-color: rgba(244, 247, 250, 0.94);
  --coach-shadow: 0 0 20px rgba(6, 12, 22, 0.9), 0 1px 3px rgba(6, 12, 22, 0.6);
  --coach-bg: linear-gradient(to top, rgba(6, 12, 20, 0.5), rgba(6, 12, 20, 0));
}
:root[data-theme="night"] .hr { font-weight: 100; }
:root[data-theme="night"] .dot.on { box-shadow: 0 0 8px rgba(127, 207, 176, 0.6); }
:root[data-theme="night"] .btn.primary { letter-spacing: 0.06em; }

/* 风格切换按钮：水墨下显示“月”（点一下去夜景），夜景下显示“日”（点一下回水墨） */
#btnTheme .ic-sun { display: none; }
:root[data-theme="night"] #btnTheme .ic-moon { display: none; }
:root[data-theme="night"] #btnTheme .ic-sun { display: block; }

/* ---------- 动效：文字、数字、卡片 ---------- */
/* 呼吸提示与话术：每个字从淡墨里依次显出，像毛笔落在纸上 */
.breath b i,
.coach span i {
  font-style: normal;
  display: inline-block;
  animation: brushIn 0.9s cubic-bezier(0.2, 0.7, 0.2, 1) both;
}
.breath b i { animation-duration: 0.7s; }
@keyframes brushIn {
  from { opacity: 0; filter: blur(6px); transform: translateY(6px) scale(1.08); }
  to { opacity: 1; filter: blur(0); transform: none; }
}

/* 呼吸提示下的一道细笔触：随当前这一口吸气 / 呼气的进度从左写到右 */
.breath-track {
  display: block;
  position: relative;
  width: 6.2em;
  height: 2px;
  margin: 10px auto 0;
  font-size: clamp(20px, 2.1vw, 29px);
  background: linear-gradient(90deg, rgba(var(--ink), 0), rgba(var(--ink), 0.12) 15%, rgba(var(--ink), 0.12) 85%, rgba(var(--ink), 0));
  border-radius: 2px;
  overflow: hidden;
}
.breath-track i {
  position: absolute;
  inset: 0;
  transform-origin: left center;
  transform: scaleX(0);
  background: linear-gradient(90deg, rgba(var(--ink), 0.15), rgba(var(--ink), 0.6));
  border-radius: 2px;
}

/* 叶片数增加时数字轻轻一跳 */
.pop { display: inline-block; animation: pop 0.6s cubic-bezier(0.3, 1.6, 0.5, 1); }
@keyframes pop { 0% { transform: scale(1); } 35% { transform: scale(1.28); color: var(--accent); } 100% { transform: scale(1); } }

/* 结束卡片：淡入，数字滚动计数，最后盖上一枚印 */
.summary.show { animation: cardIn 0.8s cubic-bezier(0.2, 0.8, 0.2, 1) both; }
@keyframes cardIn {
  from { opacity: 0; transform: translateY(14px) scale(0.96); filter: blur(4px); }
  to { opacity: 1; transform: none; filter: blur(0); }
}
.s-seal {
  position: absolute;
  top: 14px;
  right: 16px;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 4px;
  background: var(--seal);
  color: #f4ecde;
  font-family: "Kaiti SC", "STKaiti", "KaiTi", serif;
  font-size: 22px;
  transform: rotate(-6deg);
  opacity: 0;
}
.summary.show .s-seal { animation: stamp 0.5s cubic-bezier(0.3, 1.4, 0.5, 1) 1.5s both; }
@keyframes stamp {
  from { opacity: 0; transform: rotate(-6deg) scale(1.9); }
  to { opacity: 0.92; transform: rotate(-6deg) scale(1); }
}

/* 按下时的回弹手感 */
.btn:active:not(:disabled), .chip:active, .seg button:active { transform: scale(0.96); }
.btn, .chip, .seg button { transition: background 0.2s, color 0.2s, opacity 0.2s, transform 0.15s; }

/* 切换风格的那一秒里，所有界面配色柔和过渡 */
html.theming *, html.theming *::before, html.theming *::after {
  transition: background-color 0.9s ease, color 0.9s ease, border-color 0.9s ease, box-shadow 0.9s ease !important;
}

@media (prefers-reduced-motion: reduce) {
  .breath b i, .coach span i, .pop, .summary.show, .summary.show .s-seal { animation: none; opacity: 1; }
}

/* 话术出现时，字下先刷出一抹淡淡的墨底，再一个字一个字写上去 */
.coach span { position: relative; }
.coach span::before {
  content: "";
  position: absolute;
  left: -8%;
  right: -8%;
  top: 50%;
  height: 1.6em;
  transform: translateY(-50%) scaleX(0);
  transform-origin: left center;
  background: radial-gradient(ellipse at center, rgba(var(--ink), 0.09), rgba(var(--ink), 0) 70%);
  filter: blur(4px);
  z-index: -1;
  transition: transform 1.3s cubic-bezier(0.2, 0.7, 0.2, 1);
}
.coach.show span::before { transform: translateY(-50%) scaleX(1); }

/* 心率旁的小心形：每跳一下荡开一圈 */
.hr-mini::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  width: 22px;
  height: 22px;
  margin-top: -11px;
  border-radius: 50%;
  border: 1.5px solid var(--heart);
  opacity: calc(var(--pulse, 0) * 0.6);
  transform: scale(calc(1 + (1 - var(--pulse, 0)) * 1.3));
  pointer-events: none;
}
