/* ผล Lab ผู้ป่วย NCD (ncd.mshprimary.com/lab) — การ์ดกราฟแนวโน้มแบบ Apple Health + ใบพิมพ์ A5 แนวนอน */
.ncd-route {
  --ncd-good: #248a3d;
  --ncd-good-bg: #e8f6ec;
  --ncd-bad: #e5352b;
  --ncd-bad-bg: #fdecea;
  --ncd-ink: #1d2433;
  --ncd-muted: #667085;
  --ncd-line: #e4e7ec;
  --ncd-card: #fff;
  --ncd-blue: #1f6fc5;
  background: #f5f7fb;
}
/* hidden ต้องชนะ display ของคลาส (รายการผลค้นหาเป็น grid) */
.ncd-route [hidden] {
  display: none !important;
}
.ncd-shell {
  max-width: 1280px;
  margin: 0 auto;
  padding: 18px 16px 48px;
  color: var(--ncd-ink);
}
.ncd-head h1 {
  margin: 0;
  font-size: 1.6rem;
}
.ncd-muted {
  color: var(--ncd-muted);
  margin: 4px 0 0;
  font-size: 0.92rem;
}
.ncd-icon {
  font-size: 1.2em;
  vertical-align: -0.22em;
}
.good {
  color: var(--ncd-good);
}
.bad {
  color: var(--ncd-bad);
}

/* ---------- ค้นหา ---------- */
.ncd-search {
  display: flex;
  gap: 10px;
  margin: 16px 0 8px;
}
.ncd-search-box {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
  background: #fff;
  border: 1px solid #cfd8e3;
  border-radius: 14px;
  min-height: 52px;
}
.ncd-search-box:focus-within {
  border-color: var(--ncd-blue);
  box-shadow: 0 0 0 3px rgba(31, 111, 197, 0.15);
}
.ncd-search-box .ncd-icon {
  color: var(--ncd-muted);
}
.ncd-search-box input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  font: inherit;
  font-size: 1.05rem;
}
.ncd-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid #cfd8e3;
  border-radius: 12px;
  background: #fff;
  color: var(--ncd-ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.ncd-button:hover {
  background: #f0f5fb;
}
.ncd-button.primary {
  min-height: 52px;
  padding: 0 24px;
  border-color: var(--ncd-blue);
  background: var(--ncd-blue);
  color: #fff;
}
.ncd-button:disabled,
.ncd-search input:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.ncd-message {
  margin: 6px 2px;
  color: var(--ncd-muted);
}
.ncd-message.error {
  color: var(--ncd-bad);
  font-weight: 600;
}
.ncd-candidates {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 8px;
  margin: 8px 0 16px;
}
.ncd-candidate {
  display: grid;
  gap: 2px;
  text-align: left;
  padding: 12px 14px;
  background: #fff;
  border: 1px solid var(--ncd-line);
  border-radius: 12px;
  font: inherit;
  cursor: pointer;
}
.ncd-candidate:hover,
.ncd-candidate:focus-visible {
  border-color: var(--ncd-blue);
  background: #f5f9ff;
}
.ncd-candidate span {
  color: var(--ncd-muted);
  font-size: 0.9rem;
}

/* ---------- ข้อมูลผู้ป่วย ---------- */
.ncd-patient {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 18px;
  margin-top: 16px;
  padding: 16px 20px;
  background: var(--ncd-card);
  border: 1px solid var(--ncd-line);
  border-radius: 18px;
}
.ncd-avatar {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: #e8f0fa;
  color: var(--ncd-blue);
}
.ncd-avatar .material-symbols-outlined {
  font-size: 30px;
}
.ncd-patient-main {
  flex: 1 1 260px;
  min-width: 0;
}
.ncd-patient small,
.ncd-stat small {
  display: block;
  color: var(--ncd-muted);
  font-size: 0.8rem;
}
.ncd-patient h2 {
  margin: 0;
  font-size: 1.35rem;
}
.ncd-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}
.ncd-meta span {
  padding: 1px 10px;
  border-radius: 99px;
  background: #f0f3f8;
  font-size: 0.85rem;
}
.ncd-stat {
  min-width: 110px;
  padding-left: 18px;
  border-left: 1px solid var(--ncd-line);
}
.ncd-stat strong {
  font-size: 1.3rem;
}
.ncd-stat .ncd-muted {
  display: block;
  margin: 0;
  font-size: 0.78rem;
}

/* ---------- แถบเครื่องมือ ---------- */
.ncd-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin: 14px 0 6px;
}
.ncd-toggle {
  display: inline-flex;
  padding: 3px;
  border-radius: 12px;
  background: #e9edf3;
}
.ncd-toggle button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  font: inherit;
  font-weight: 600;
  color: var(--ncd-muted);
  cursor: pointer;
}
.ncd-toggle button[aria-pressed='true'] {
  background: #fff;
  color: var(--ncd-ink);
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.12);
}
.ncd-legend {
  flex: 1;
  margin: 0;
  color: var(--ncd-muted);
  font-size: 0.88rem;
}
.ncd-legend .dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin: 0 4px 0 10px;
  border-radius: 50%;
}
.ncd-legend .dot.good {
  background: var(--ncd-good);
}
.ncd-legend .dot.bad {
  background: var(--ncd-bad);
}
.ncd-legend .line {
  display: inline-block;
  width: 18px;
  margin: 0 4px 3px 10px;
  border-top: 2px dashed var(--ncd-bad);
}
.ncd-actions {
  display: flex;
  gap: 8px;
}
.ncd-section-title {
  margin: 14px 0 8px;
  font-size: 1.1rem;
}
.ncd-section-title small {
  color: var(--ncd-muted);
  font-weight: 400;
  font-size: 0.85rem;
}

/* ---------- การ์ดกราฟ ---------- */
.ncd-charts {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 12px;
}
.ncd-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 14px 10px;
  text-align: left;
  background: var(--ncd-card);
  border: 1px solid var(--ncd-line);
  border-top: 4px solid var(--ncd-blue);
  border-radius: 16px;
  font: inherit;
  color: inherit;
  cursor: pointer;
}
.ncd-card.diabetes {
  border-top-color: #f59e0b;
}
.ncd-card.lipid {
  border-top-color: #8b5cf6;
}
.ncd-card.kidney {
  border-top-color: #0ea5e9;
}
.ncd-card:hover,
.ncd-card:focus-visible {
  box-shadow: 0 6px 18px rgba(16, 24, 40, 0.1);
}
.ncd-card-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.ncd-card-head small {
  color: var(--ncd-muted);
}
.ncd-card-last {
  font-size: 1.35rem;
  font-weight: 800;
  white-space: nowrap;
}
.ncd-card-last small {
  font-size: 0.72rem;
  font-weight: 400;
}
.ncd-card-chart svg {
  display: block;
  width: 100%;
  height: auto;
}
.ncd-card-range {
  color: var(--ncd-muted);
  font-size: 0.82rem;
}
.ncd-empty {
  margin: 30px 0;
  text-align: center;
  color: var(--ncd-muted);
}
.ncd-target {
  stroke: var(--ncd-bad);
  stroke-width: 1.4;
  stroke-dasharray: 6 5;
  opacity: 0.8;
}
.ncd-axis {
  fill: var(--ncd-muted);
  font-weight: 600;
}
/* ผลล่าสุดที่ตรวจวันนี้: จุดมีวงกระเพื่อม / แท่งเรืองแสง */
.ncd-pulse {
  transform-box: fill-box;
  transform-origin: center;
  animation: ncdPulse 1.8s ease-out infinite;
}
@keyframes ncdPulse {
  from {
    transform: scale(1);
    opacity: 0.45;
  }
  to {
    transform: scale(3.2);
    opacity: 0;
  }
}
.ncd-today-bar {
  animation: ncdGlow 1.8s ease-in-out infinite;
}
@keyframes ncdGlow {
  50% {
    opacity: 0.6;
  }
}
@media (prefers-reduced-motion: reduce) {
  .ncd-pulse,
  .ncd-today-bar {
    animation: none;
  }
}

/* ---------- ผลที่ไม่ใช่ตัวเลข + คำแนะนำ ---------- */
.ncd-others {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 12px;
  margin-top: 12px;
}
.ncd-other,
.ncd-advice-card {
  padding: 14px 16px;
  background: var(--ncd-card);
  border: 1px solid var(--ncd-line);
  border-radius: 16px;
}
.ncd-other h4 {
  margin: 0 0 8px;
}
.ncd-other small {
  color: var(--ncd-muted);
  font-weight: 400;
}
.ncd-other table,
.ncd-table {
  width: 100%;
  border-collapse: collapse;
}
.ncd-other th,
.ncd-other td,
.ncd-table th,
.ncd-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--ncd-line);
  text-align: left;
}
.ncd-other th,
.ncd-table th {
  color: var(--ncd-muted);
  font-weight: 600;
  font-size: 0.85rem;
}
.ncd-table td.good,
.ncd-table td.bad {
  font-weight: 700;
}
.ncd-advice-card {
  margin-top: 12px;
}
.ncd-advice-card h3 {
  margin: 0 0 6px;
  font-size: 1.05rem;
}
.ncd-advice-card ol {
  margin: 0;
  padding-left: 22px;
  columns: 2 280px;
}

/* ---------- หน้าต่างขยาย ---------- */
.ncd-dialog {
  width: min(820px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 18px 20px;
  border: 0;
  border-radius: 20px;
  box-shadow: 0 24px 60px rgba(16, 24, 40, 0.25);
}
.ncd-dialog::backdrop {
  background: rgba(16, 24, 40, 0.45);
}
.ncd-dialog-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.ncd-dialog-head h2 {
  margin: 0;
  font-size: 1.3rem;
}
.ncd-dialog-head small {
  color: var(--ncd-muted);
  font-weight: 400;
}
.ncd-icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: #f0f3f8;
  cursor: pointer;
}
.ncd-dialog-summary {
  display: flex;
  gap: 28px;
  margin: 10px 0;
}
.ncd-dialog-summary small {
  display: block;
  color: var(--ncd-muted);
}
.ncd-dialog-chart svg {
  display: block;
  width: 100%;
  height: auto;
}

@media (max-width: 640px) {
  .ncd-search {
    flex-direction: column;
  }
  .ncd-stat {
    padding-left: 0;
    border-left: 0;
  }
  .ncd-legend {
    flex-basis: 100%;
    order: 3;
  }
  .ncd-legend .dot:first-child {
    margin-left: 0;
  }
  .ncd-actions {
    width: 100%;
  }
  .ncd-actions .ncd-button {
    flex: 1;
    justify-content: center;
  }
}

/* ---------- ใบพิมพ์ A5 แนวนอน ---------- */
.ncd-print {
  display: none;
}
@media print {
  @page {
    size: A5 landscape;
    margin: 0;
  }
  body.ncd-route > *:not(.ncd-print) {
    display: none !important;
  }
  body.ncd-route {
    padding: 0 !important;
    background: #fff !important;
  }
  .ncd-print {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    width: 210mm;
    height: 148mm;
    padding: 7mm 8mm 5mm;
    overflow: hidden;
    color: #111;
    font-size: 10.5pt;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .ncd-print-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding-bottom: 2mm;
    border-bottom: 0.6mm solid #1f6fc5;
  }
  .ncd-print-head strong {
    display: block;
    font-size: 13pt;
  }
  .ncd-print-head span {
    font-size: 9.5pt;
    color: #444;
  }
  .ncd-print-patient {
    text-align: right;
  }
  .ncd-print-body {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr 1.25fr;
    gap: 5mm;
    min-height: 0;
    padding-top: 2mm;
  }
  .ncd-print h4 {
    margin: 1.5mm 0 1.5mm;
    font-size: 10.5pt;
  }
  .ncd-print h4 small {
    font-weight: 400;
    color: #555;
  }
  .ncd-print-latest {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1.6mm;
  }
  .pr-cell {
    padding: 1.4mm 1.6mm;
    border: 0.3mm solid #d0d5dd;
    border-radius: 2mm;
  }
  .pr-cell b {
    display: block;
    font-size: 8.5pt;
  }
  .pr-cell strong {
    display: block;
    font-size: 17pt;
    line-height: 1.15;
  }
  .pr-cell small {
    display: block;
    overflow: hidden;
    font-size: 7pt;
    color: #555;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .pr-cell.good strong {
    color: var(--ncd-good);
  }
  .pr-cell.bad {
    border-color: var(--ncd-bad);
    background: var(--ncd-bad-bg);
  }
  .pr-cell.bad strong {
    color: var(--ncd-bad);
  }
  .ncd-print-advice {
    margin: 0;
    padding-left: 5mm;
    font-size: 10.5pt;
    line-height: 1.45;
  }
  .ncd-print-trends {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5mm 3mm;
  }
  .pr-trend b {
    font-size: 8.5pt;
  }
  .pr-trend b small {
    font-weight: 400;
    color: #555;
  }
  .pr-trend svg {
    display: block;
    width: 100%;
    height: auto;
  }
  .ncd-print .ncd-empty {
    margin: 4mm 0;
    font-size: 8pt;
  }
  .ncd-print-foot {
    padding-top: 1mm;
    border-top: 0.2mm solid #d0d5dd;
    font-size: 7pt;
    color: #666;
    text-align: right;
  }
  .ncd-pulse,
  .ncd-today-bar {
    animation: none;
    display: none;
  }
  /* ขาวดำ: ในเกณฑ์ = เทา, นอกเกณฑ์ = ดำล้วน */
  body.ncd-print-mono .ncd-print {
    --ncd-good: #8a8a8a;
    --ncd-bad: #000;
    --ncd-bad-bg: #fff;
  }
  body.ncd-print-mono .ncd-print-head {
    border-bottom-color: #000;
  }
  body.ncd-print-mono .ncd-print [fill='#248a3d'] {
    fill: #8a8a8a;
  }
  body.ncd-print-mono .ncd-print [stroke='#248a3d'] {
    stroke: #8a8a8a;
  }
  body.ncd-print-mono .ncd-print [fill='#e5352b'] {
    fill: #000;
  }
  body.ncd-print-mono .ncd-print [stroke='#e5352b'] {
    stroke: #000;
  }
  body.ncd-print-mono .ncd-print .ncd-target {
    stroke: #000;
  }
  body.ncd-print-mono .pr-cell.bad {
    border-width: 0.5mm;
    border-color: #000;
  }
}
