:root {
  --bg: #faf7f2;
  --card: #ffffff;
  --primary: #397c73;
  --primary-dark: #28655c;
  --danger: #e5484d;
  --ok: #2f9e6e;
  --text: #1f2430;
  --text-2: #6b7280;
  --border: #e5e8f0;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .06), 0 1px 2px rgba(16, 24, 40, .04);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  font-family: "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
}
.container { max-width: 960px; margin: 0 auto; padding: 0 16px; }

.app-header {
  background: linear-gradient(120deg, #397c73, #4f8ff7);
  color: #fff;
  padding: 20px 0;
}
.app-header .container { display: flex; align-items: center; justify-content: space-between; }
.app-header h1 { margin: 0; font-size: 20px; font-weight: 600; }
.header-right { display: flex; align-items: center; gap: 10px; }
.conn-status { font-size: 12px; padding: 3px 10px; border-radius: 999px; background: rgba(255,255,255,.2); }
.conn-status.ok { background: rgba(46,213,115,.35); }
.conn-status.err { background: rgba(255,107,107,.45); }
.user-bar { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.user-avatar { width: 26px; height: 26px; border-radius: 50%; background: rgba(255,255,255,.25); }
.btn-ghost-light { background: rgba(255,255,255,.18); color: #fff; border: 1px solid rgba(255,255,255,.35); }

main.container { padding-top: 20px; padding-bottom: 40px; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  margin-bottom: 18px;
}
.card h2 {
  margin: 0 0 14px;
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.card h2 small { color: var(--text-2); font-weight: 400; font-size: 12px; }

/* 表单 */
.record-form { display: flex; flex-direction: column; gap: 14px; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}
.field { display: flex; flex-direction: column; gap: 5px; }
.field span { font-size: 13px; color: var(--text-2); }
.field em { font-style: normal; color: #9aa1b2; font-size: 12px; }
.field-full { grid-column: 1 / -1; }
.field input {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
  width: 100%;
}
.field select {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  background: #fff;
  width: 100%;
}
.field input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(47,111,237,.12); }
.form-actions { display: flex; gap: 10px; }

/* 医生列表 */
.doctor-form { margin-bottom: 16px; }
.doctor-grid { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.doctor-list { display: flex; flex-direction: column; gap: 10px; }
.doctor-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  background: #fafbfe;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
}
.doctor-item .doc-name { font-weight: 600; font-size: 14px; }
.doctor-item .doc-link {
  font-size: 12px;
  color: var(--text-2);
  flex: 1 1 220px;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
.doctor-item .doc-link code {
  background: #eef1f7;
  padding: 2px 8px;
  border-radius: 6px;
  color: var(--primary-dark);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 260px;
}
.doctor-item .doc-ops { display: flex; gap: 6px; flex-wrap: wrap; }
.doctor-item .doc-status { font-size: 12px; }
.doc-status.ok { color: var(--ok); }
.doc-status.bad { color: var(--danger); }
.doc-status.gray { color: var(--text-2); }
.btn-sm { padding: 4px 10px; font-size: 12px; border-radius: 6px; }

.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 22px;
  font-size: 14px;
  cursor: pointer;
  transition: filter .15s;
}
.btn:hover { filter: brightness(1.06); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-ghost { background: #eef1f7; color: var(--text-2); }
.btn-sm { padding: 4px 10px; font-size: 12px; border-radius: 6px; }
.btn-link { background: none; color: var(--primary); padding: 4px 8px; }
.btn-link.danger { color: var(--danger); }

/* 摘要 */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}
.summary-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  background: #fafbfe;
}
.summary-item .k { font-size: 12px; color: var(--text-2); }
.summary-item .v { font-size: 24px; font-weight: 700; margin-top: 2px; }
.summary-item .v .unit { font-size: 12px; font-weight: 400; color: var(--text-2); margin-left: 3px; }
.summary-item .d { font-size: 12px; color: var(--text-2); margin-top: 2px; }
.summary-item .tag { display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 999px; margin-left: 6px; vertical-align: 2px; }
.tag.normal { background: #e4f6ee; color: var(--ok); }
.tag.high { background: #fdeaea; color: var(--danger); }
.tag.low { background: #fdeaea; color: var(--danger); }
.tag.trend { background: #eaf1fe; color: var(--primary); }
.summary-empty { color: var(--text-2); padding: 8px 0; }

/* 图表 */
.chart { height: 280px; margin-bottom: 14px; }
.chart:last-child { margin-bottom: 0; }

/* 周期选择 */
.period-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}
.period-btn {
  border: 1px solid var(--border);
  background: #fafbfe;
  color: var(--text-2);
  border-radius: 999px;
  padding: 5px 16px;
  font-size: 13px;
  cursor: pointer;
  transition: all .15s;
}
.period-btn:hover { border-color: var(--primary); color: var(--primary); }
.period-btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.custom-range {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-2);
}
.custom-range input[type="date"] {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 13px;
  outline: none;
}
.custom-range input[type="date"]:focus { border-color: var(--primary); }

/* 表格 */
.table-wrap { overflow: visible; }
.record-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.record-table th, .record-table td {
  text-align: left;
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.record-table th { color: #1f2430; font-weight: 700; background: #fafbfe; }
/* 表头随页面滚动吸顶；小屏保留横向滚动（吸顶在窄屏失效属可接受的降级） */
.record-table thead th { position: sticky; top: 0; z-index: 5; box-shadow: 0 1px 0 var(--border); }
@media (max-width: 760px) {
  .table-wrap { overflow-x: auto; }
}
.record-table tbody tr:hover { background: #f7f9fd; }
.record-table .num { font-variant-numeric: tabular-nums; }
.record-table .abn { color: var(--danger); font-weight: 600; }
.record-table .nor { color: var(--ok); }
.record-table .remark { color: var(--text-2); max-width: 220px; white-space: normal; word-break: break-all; }
.record-table .ops { display: flex; gap: 2px; }

/* 分页导航条 */
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 0 2px;
  flex-wrap: wrap;
}
.pg-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--primary);
  font-size: 13px;
  padding: 5px 14px;
  border-radius: 6px;
  cursor: pointer;
}
.pg-btn:hover:not(:disabled) { background: #f0f5ff; }
.pg-btn:disabled { color: #b0b6c4; cursor: not-allowed; background: #f7f8fb; }
.pg-info { font-size: 13px; color: var(--text-2); padding: 0 4px; }

.app-footer { color: var(--text-2); font-size: 12px; text-align: center; padding: 20px 0 30px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 36px;
  transform: translateX(-50%);
  background: rgba(31,36,48,.92);
  color: #fff;
  padding: 9px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 99;
  box-shadow: 0 4px 14px rgba(0,0,0,.18);
}

/* 登录遮罩 */
.login-mask {
  position: fixed;
  inset: 0;
  background: rgba(20,26,40,.55);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}
.login-card {
  background: #fff;
  border-radius: 16px;
  width: 340px;
  max-width: 100%;
  padding: 28px 30px 22px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,.2);
}
.login-card h2 { margin: 0 0 6px; font-size: 18px; }
.login-tip { color: var(--text-2); font-size: 13px; margin: 0 0 18px; }
.qrcode-box {
  position: relative;
  width: 200px;
  height: 200px;
  margin: 0 auto 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  overflow: hidden;
}
.qrcode canvas { display: block; }
.qrcode-loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--text-2); font-size: 13px; background: #fff; }
.scan-status { min-height: 20px; font-size: 13px; color: var(--primary); margin-bottom: 10px; }
.scan-status.err { color: var(--danger); }
.login-actions { display: flex; gap: 10px; justify-content: center; }
.login-divider {
  display: flex; align-items: center; gap: 10px; color: var(--text-2);
  font-size: 12px; margin: 14px 0 12px;
}
.login-divider::before, .login-divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.btn-block { width: 100%; }
.login-error { color: var(--danger); font-size: 12px; min-height: 18px; margin: 10px 0 0; }

@media (max-width: 640px) {
  .chart { height: 240px; }
  .summary-grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
}

.doc-view-head { background: linear-gradient(120deg,#397c73,#65988a); }
.record-table { table-layout: fixed; }
.record-table th, .record-table td, .record-table .remark { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chart-expanded { position: fixed; inset: 0; z-index: 100; background: #faf7f2; overflow: auto; padding: 64px 16px 24px; margin: 0; border-radius: 0; }
.chart-expanded .chart { height: 65vh; }
.chart-return { position: fixed; top: 12px; left: 16px; z-index: 101; background: #397c73; color: white; border: 0; border-radius: 8px; padding: 10px 20px; }
@media(max-width:600px) { .record-table th,.record-table td { padding: 10px 3px; font-size: 11px; } .container { padding-left: 12px; padding-right: 12px; } .summary-grid { grid-template-columns: 1fr; } }

.record-table th:first-child { width: 25%; }
/* 竖持手机时旋转整个独立画布容器，图表按横向实际尺寸重绘。 */
.landscape-overlay { position: fixed; inset: 0; z-index: 1000; background: #faf7f2; box-sizing: border-box; padding: max(64px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left)); }
.landscape-overlay .chart { width: 100%; height: 100%; margin: 0; }
.landscape-back { position: absolute; top: max(12px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); height: 38px; padding: 0 18px; border: 0; border-radius: 19px; color: #fff; background: #397c73; font-size: 15px; cursor: pointer; }
@media (orientation: portrait) {
  .landscape-overlay { inset: auto; left: 50%; top: 50%; width: 100vh; width: 100dvh; height: 100vw; height: 100dvw; transform: translate(-50%, -50%) rotate(90deg); }
}

.landscape-title { position: absolute; top: max(12px, env(safe-area-inset-top)); left: 116px; right: 116px; height: 38px; line-height: 38px; text-align: center; color: #343e3a; font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chart-open { display: inline-flex; align-items: center; justify-content: center; height: 32px; padding: 0 14px; border: 0; border-radius: 16px; background: #edf3ef; color: #397c73; line-height: 32px; vertical-align: middle; }
.record-table th:first-child { width: 12ch; }
.record-table th:nth-child(2), .record-table th:nth-child(3), .record-table th:nth-child(4) { width: 9ch; }
.record-table .remark { max-width: none; }
@media(max-width:600px) { .record-table th:nth-child(2), .record-table th:nth-child(3), .record-table th:nth-child(4) { width: 8ch; } }
