:root {
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  --ink: #18231f;
  --muted: #65716c;
  --line: #d9dfdc;
  --surface: #ffffff;
  --soft: #f2f6f4;
  --green: #165c4b;
  --green-2: #0f7a5c;
  --red: #a42a2a;
  --amber: #8a5b00;
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--ink); background: #eef2f0; letter-spacing: 0; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { min-height: 44px; cursor: pointer; }
input, select, textarea {
  width: 100%;
  margin-top: 7px;
  border: 1px solid #bcc7c2;
  border-radius: 6px;
  background: white;
  padding: 11px 12px;
  color: var(--ink);
}
textarea { resize: vertical; }
label { display: block; color: #34423c; font-size: .94rem; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 66px;
  padding: max(12px, env(safe-area-inset-top)) 22px 12px;
  color: white;
  background: var(--green);
}
.topbar strong, .topbar span { display: block; }
.topbar strong { font-size: 1.04rem; }
.topbar span { margin-top: 2px; color: #d8ebe4; font-size: .76rem; }

main { width: min(920px, 100%); margin: 0 auto; padding: 22px 20px 104px; }
.view { display: none; }
.view.active { display: block; }
.section-heading { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 20px; }
.section-heading.compact { margin-top: 26px; margin-bottom: 12px; }
.heading-actions, .patient-row-actions { display: flex; gap: 8px; }
.week-number-field { display: block; max-width: 260px; margin: 12px 0 0; }
h1, h2 { margin: 0; font-weight: 700; }
h1 { font-size: 1.65rem; }
h2 { font-size: 1.12rem; }
.eyebrow { margin: 0 0 5px; color: var(--green-2); font-size: .78rem; font-weight: 700; text-transform: uppercase; }
.hint { color: var(--muted); font-size: .84rem; line-height: 1.5; }
.form-error { min-height: 1.3em; margin: 8px 0 0; color: var(--red); font-size: .86rem; font-weight: 650; }
.startup-error { margin: 12px; border: 2px solid var(--red); background: #fff2f2; padding: 14px; color: #721d1d; font-weight: 700; }
.hidden { display: none !important; }

.primary, .secondary {
  border-radius: 6px;
  padding: 9px 15px;
  font-weight: 650;
}
.primary { border: 1px solid var(--green); color: white; background: var(--green); }
.secondary { border: 1px solid #aebbb5; color: #26332e; background: white; }
.danger {
  border: 1px solid #c27b7b;
  border-radius: 6px;
  padding: 9px 13px;
  color: #8c2020;
  background: #fff7f7;
  font-weight: 650;
}

.stats-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.stats-strip div { padding: 17px 12px; text-align: center; border-right: 1px solid var(--line); }
.stats-strip div:last-child { border-right: 0; }
.stats-strip strong, .stats-strip span { display: block; }
.stats-strip strong { font-size: 1.45rem; color: var(--green); }
.stats-strip span { margin-top: 3px; color: var(--muted); font-size: .78rem; }

.lookup-band, .visit-link-band, .form-section, .result-panel, .notice {
  margin-top: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 18px;
}
.visit-link-band { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 14px; align-items: end; }
.change-list { display: grid; gap: 10px; margin-top: 12px; }
.change-item {
  display: flex;
  align-items: center;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 12px;
  background: var(--soft);
  font-weight: 650;
}
.change-item input { width: auto; margin: 0 9px 0 0; }
.change-fields { border-left: 4px solid var(--green); padding: 2px 0 8px 14px; }
.change-fields > label, .change-fields > .form-grid { margin-top: 10px; }
.inline-control { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: end; }
.inline-control input { margin-top: 0; }
.file-button { display: inline-flex; align-items: center; justify-content: center; }
.file-button input { display: none; }

.patient-list { border-top: 1px solid var(--line); background: var(--surface); }
.patient-filter { margin-bottom: 10px; }
.assessment-history { margin-top: 10px; }
.assessment-history-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  padding: 12px 0;
}
.assessment-history-row:last-child { border-bottom: 0; }
.assessment-history-row strong, .assessment-history-row span { display: block; }
.assessment-history-row span { margin-top: 4px; color: var(--muted); font-size: .8rem; }
.patient-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 14px;
  padding: 15px 12px;
  border-bottom: 1px solid var(--line);
}
.patient-row button { min-height: 36px; }
.patient-row-actions { flex-shrink: 0; }
.patient-row strong, .patient-row span { display: block; }
.patient-row span { margin-top: 4px; color: var(--muted); font-size: .82rem; }
.empty { padding: 28px 12px; color: var(--muted); text-align: center; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-section > label, .form-section > .form-grid, .form-section > .segmented { margin-top: 14px; }
.form-section h2 { padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.scale-block { margin-top: 18px; border-top: 3px solid var(--green); background: var(--surface); padding: 18px; }
.scale-block header { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.scale-score { color: var(--green); font-weight: 700; }
.question-row { padding: 13px 0; border-bottom: 1px solid #e5e9e7; }
.question-row:last-child { border-bottom: 0; }
.question-row p { margin: 0 0 9px; line-height: 1.45; }
.choices { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.choices label { position: relative; }
.choices input { position: absolute; opacity: 0; pointer-events: none; }
.choices span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  border: 1px solid #bdc7c2;
  border-radius: 5px;
  background: white;
  text-align: center;
  font-size: .8rem;
}
.choices input:checked + span { border-color: var(--green); color: white; background: var(--green); }

.symptom-row { display: grid; grid-template-columns: 1.4fr 1fr; gap: 10px; align-items: end; margin-top: 10px; }
.result-panel { border-left: 5px solid var(--green); line-height: 1.7; }
.result-panel strong { color: var(--green); }
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid #b9c4bf; border-radius: 6px; overflow: hidden; }
.segmented label { position: relative; padding: 12px 6px; text-align: center; border-right: 1px solid #b9c4bf; }
.segmented label:last-child { border-right: 0; }
.segmented input { width: auto; margin: 0 5px 0 0; }
.sticky-actions { position: sticky; bottom: 64px; display: flex; gap: 10px; margin-top: 18px; padding: 10px 0; background: rgba(238, 242, 240, .94); }
.sticky-actions button { flex: 1; }
#clinicalForm .sticky-actions { position: static; bottom: auto; background: transparent; }
.data-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.notice { border-left: 5px solid var(--amber); line-height: 1.6; }
.image-actions { display: flex; flex-wrap: wrap; gap: 9px; margin-top: 14px; }
.patient-image-list { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-top: 14px; }
.patient-image-card { min-width: 0; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: var(--soft); }
.patient-image-card img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: white; }
.patient-image-meta { padding: 9px; }
.patient-image-meta strong, .patient-image-meta span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.patient-image-meta strong { font-size: .84rem; }
.patient-image-meta span { margin-top: 3px; color: var(--muted); font-size: .74rem; }
.patient-image-controls { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; padding: 0 9px 9px; }
.patient-image-controls button { min-width: 0; min-height: 36px; padding: 6px 3px; font-size: .78rem; }
.image-viewer-dialog { width: min(920px, calc(100% - 24px)); }
.live-text-hint { margin: 8px 0 12px; color: var(--muted); line-height: 1.5; }
.image-viewer-content img {
  display: block;
  width: 100%;
  max-height: 72vh;
  object-fit: contain;
  background: #111;
  -webkit-touch-callout: default;
  user-select: auto;
}
#downloadViewedImageButton { width: 100%; margin-top: 12px; }

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid #bdc8c3;
  background: rgba(255, 255, 255, .96);
}
.bottom-nav button { border: 0; color: var(--muted); background: transparent; }
.bottom-nav button.active { color: var(--green); font-weight: 700; }

dialog { width: min(560px, calc(100% - 30px)); border: 0; border-radius: 8px; padding: 22px; }
dialog::backdrop { background: rgba(17, 28, 23, .55); }
dialog h2 { margin-bottom: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.qr-dialog-content { text-align: center; }
#qrCode { display: flex; justify-content: center; margin: 18px auto; }
#qrCode canvas, #qrCode img { width: 220px !important; height: 220px !important; }
#qrCodeText { display: block; margin-bottom: 6px; font-size: 1.2rem; }

@media (max-width: 640px) {
  main { padding-inline: 14px; }
  .form-grid, .inline-control, .visit-link-band { grid-template-columns: 1fr; }
  .section-heading { align-items: flex-start; }
  .choices { grid-template-columns: repeat(2, 1fr); }
  .symptom-row { grid-template-columns: 1fr; }
  .patient-image-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .patient-row, .assessment-history-row { grid-template-columns: 1fr; }
  .patient-row-actions { flex-wrap: wrap; }
  .topbar { padding-inline: 14px; }
}
