@import url('sarabun.css');

/* =========================================================================
   ระบบสืบค้นข้อมูลยา - โรงพยาบาลเกษมราษฎร์บางแค
   สไตล์ทั้งหมดของเว็บแอป เขียนเองทั้งหมดไม่พึ่ง framework ภายนอก
   ใช้ร่วมกันระหว่าง index.html และ reference.html
   ========================================================================= */

/* --- ตัวแปรสี: โหมดสว่าง --------------------------------------------- */
:root {
  --bg:          #f0f4f8;
  --surface:     #ffffff;
  --surface-2:   #f8fafc;
  --surface-3:   #f1f5f9;
  --ink:         #0f172a;
  --ink-2:       #334155;
  --muted:       #64748b;
  --faint:       #94a3b8;

  --line:        #0f172a;   /* เส้นขอบการ์ดแบบหนา */
  --line-soft:   #e2e8f0;

  --brand:       #1d4ed8;
  --brand-dark:  #1e40af;
  --brand-soft:  #eff6ff;

  --oral:        #2563eb;
  --inj:         #059669;
  --ext:         #9333ea;
  --accent:      #d97706;
  --accent-soft: #fffbeb;

  --danger:      #b91c1c;
  --danger-ink:  #991b1b;
  --danger-bg:   #fef2f2;
  --danger-line: #fecaca;
  --warn:        #ea580c;

  --mark-bg:     #fde68a;
  --mark-ink:    #78350f;

  --shadow-sm:   0 1px 2px rgba(15, 23, 42, .06);
  --shadow-md:   0 4px 12px rgba(15, 23, 42, .08);
  --shadow-lg:   0 12px 28px rgba(15, 23, 42, .12);

  --radius:      1.25rem;
  --header-h:    132px;
}

/* --- ตัวแปรสี: โหมดมืด ------------------------------------------------ */
[data-theme="dark"] {
  --bg:          #0f172a;
  --surface:     #1e293b;
  --surface-2:   #223049;
  --surface-3:   #273449;
  --ink:         #f8fafc;
  --ink-2:       #cbd5e1;
  --muted:       #94a3b8;
  --faint:       #64748b;

  --line:        #475569;
  --line-soft:   #334155;

  --brand:       #60a5fa;
  --brand-dark:  #93c5fd;
  --brand-soft:  #1e3a5f;

  --oral:        #3b82f6;
  --inj:         #10b981;
  --ext:         #a855f7;
  --accent:      #fbbf24;
  --accent-soft: #292216;

  --danger:      #f87171;
  --danger-ink:  #fca5a5;
  --danger-bg:   #2a1618;
  --danger-line: #7f1d1d;
  --warn:        #fb923c;

  --mark-bg:     #78350f;
  --mark-ink:    #fde68a;

  --shadow-sm:   0 1px 2px rgba(0, 0, 0, .3);
  --shadow-md:   0 4px 12px rgba(0, 0, 0, .35);
  --shadow-lg:   0 12px 28px rgba(0, 0, 0, .45);
}

/* --- พื้นฐาน ----------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Sarabun', 'Leelawadee UI', 'Tahoma', sans-serif;
  font-size: 18px;
  line-height: 1.6;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

img { max-width: 100%; }

.wrap {
  width: 100%;
  max-width: 1280px;
  margin-inline: auto;
  padding-inline: 16px;
}

/* ไอคอนทั้งหมดเป็น SVG แบบเส้น วาดด้วยสีของข้อความที่ครอบอยู่ */
.icon {
  width: 1em;
  height: 1em;
  flex-shrink: 0;
  vertical-align: -0.125em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  overflow: visible;
}

/* ซ่อนจากสายตาแต่ยังอ่านได้ด้วยโปรแกรมอ่านหน้าจอ */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ลิงก์ข้ามไปเนื้อหาหลัก สำหรับผู้ใช้คีย์บอร์ด */
.skip-link {
  position: absolute;
  left: 16px; top: -60px;
  z-index: 200;
  background: var(--brand);
  color: #fff;
  padding: 10px 18px;
  border-radius: 0 0 12px 12px;
  font-weight: 700;
  text-decoration: none;
  transition: top .15s;
}
.skip-link:focus { top: 0; }

/* เส้นโฟกัสที่มองเห็นชัดทุกองค์ประกอบที่กดได้ */
a:focus-visible,
button:focus-visible,
input:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 2px;
  border-radius: 6px;
}

/* =========================================================================
   ส่วนหัว
   ========================================================================= */
.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--surface);
  border-bottom: 4px solid var(--brand);
  box-shadow: var(--shadow-md);
}

.header-inner {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-block: 12px;
}

.brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand-id {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  color: inherit;
  text-decoration: none;
}

.brand-logo {
  width: 48px; height: 48px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  background: #fff;
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.brand-logo img { width: 100%; height: 100%; object-fit: contain; }

.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--brand-dark);
  line-height: 1.25;
}
.brand-org {
  font-size: .85rem;
  font-weight: 700;
  font-style: italic;
  color: var(--brand);
}

/* --- แถวเครื่องมือ: ช่องค้นหา + ปุ่ม ----------------------------------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.search-box {
  position: relative;
  flex: 1 1 260px;
  min-width: 0;
}
.search-box .icon-search {
  position: absolute;
  left: 14px; top: 50%;
  transform: translateY(-50%);
  color: var(--faint);
  font-size: 1rem;
  pointer-events: none;
}
.search-box input {
  width: 100%;
  padding: 10px 40px 10px 42px;
  font: inherit;
  font-size: 1rem;
  font-weight: 500;
  color: var(--ink);
  background: var(--surface-3);
  border: 2px solid var(--line-soft);
  border-radius: 12px;
  outline: none;
  transition: border-color .15s, background-color .15s;
}
.search-box input::placeholder { color: var(--faint); font-size: .95rem; }
.search-box input:focus {
  border-color: var(--brand);
  background: var(--surface);
}
.search-clear {
  position: absolute;
  right: 8px; top: 50%;
  transform: translateY(-50%);
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  padding: 0;
  color: var(--faint);
  background: none;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.05rem;
  transition: color .15s, background-color .15s;
}
.search-clear:hover { color: var(--ink-2); background: var(--line-soft); }
.search-clear[hidden] { display: none; }

/* ปุ่มทั่วไปในแถบเครื่องมือ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  font: inherit;
  font-size: .9rem;
  font-weight: 700;
  color: var(--brand-dark);
  background: var(--brand-soft);
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color .15s, border-color .15s, transform .1s;
}
.btn:hover { border-color: var(--brand); }
.btn:active { transform: translateY(1px); }

.btn-icon { padding: 10px; width: 42px; }

.btn-accent {
  color: #fff;
  background: var(--accent);
  border-color: transparent;
  box-shadow: var(--shadow-sm);
}
[data-theme="dark"] .btn-accent { color: #1c1917; }
.btn-accent:hover { filter: brightness(1.06); border-color: transparent; }

/* =========================================================================
   แถบกรองประเภทยา + ดัชนีตัวอักษร
   ========================================================================= */
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-block: 20px 12px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 18px;
  font: inherit;
  font-size: .9rem;
  font-weight: 700;
  color: var(--muted);
  background: var(--surface);
  border: 2px solid var(--line-soft);
  border-radius: 999px;
  cursor: pointer;
  text-decoration: none;
  transition: all .15s;
}
.chip:hover { border-color: var(--faint); }
.chip .count {
  font-size: .78rem;
  font-weight: 600;
  opacity: .75;
}
.chip[aria-pressed="true"] {
  color: #fff;
  border-color: transparent;
  box-shadow: var(--shadow-sm);
}
.chip[aria-pressed="true"][data-type="all"]        { background: #334155; }
.chip[aria-pressed="true"][data-type="oral"]       { background: var(--oral); }
.chip[aria-pressed="true"][data-type="injectable"] { background: var(--inj); }
.chip[aria-pressed="true"][data-type="external"]   { background: var(--ext); }

/* แถบตัวอักษร A-Z */
.alpha-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
  margin-bottom: 18px;
}
.alpha {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  padding: 4px 7px;
  text-decoration: none;
  font: inherit;
  font-size: .82rem;
  font-weight: 700;
  color: var(--brand);
  background: var(--surface);
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  cursor: pointer;
  transition: all .12s;
}
.alpha:hover:not(.is-off) { background: var(--brand-soft); border-color: var(--brand); }
.alpha.is-off { color: var(--faint); opacity: .4; cursor: default; }
.alpha[aria-pressed="true"] {
  color: #fff;
  background: var(--brand);
  border-color: var(--brand);
}

/* =========================================================================
   แถบสรุปผลการค้นหา
   ========================================================================= */
.result-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 18px;
  font-size: .9rem;
  color: var(--muted);
}
.result-bar p { margin: 0; }
.result-bar strong { color: var(--ink); font-size: 1.05rem; }
.result-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.muted { color: var(--muted); }

.btn-quiet {
  color: var(--muted);
  background: transparent;
  border-color: var(--line-soft);
}

/* =========================================================================
   แบ่งหน้า
   ========================================================================= */
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin: 32px 0 8px;
}
.pager-info { font-size: .9rem; font-weight: 700; color: var(--muted); }
.btn.is-off { opacity: .4; cursor: default; pointer-events: none; }
.icon-flip { transform: scaleX(-1); }

/* =========================================================================
   เส้นทางกลับ
   ========================================================================= */
.crumbs { margin: 18px 0 8px; font-size: .9rem; }
.crumbs a {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-weight: 700;
  color: var(--brand);
  text-decoration: none;
}
.crumbs a:hover { text-decoration: underline; }

/* =========================================================================
   หน้าข้อมูลยารายตัว
   ========================================================================= */
.detail { margin-bottom: 40px; }

.detail-head {
  padding: 22px 24px;
  margin-bottom: 20px;
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.detail-head h1 {
  margin: 8px 0 4px;
  font-size: 1.6rem;
  line-height: 1.25;
  overflow-wrap: anywhere;
}
.detail-sub { margin: 0; font-size: .95rem; color: var(--muted); }
.detail-updated { margin: 10px 0 0; font-size: .82rem; color: var(--faint); }

.detail-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  align-items: start;
}

.detail-main {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 22px 24px;
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.detail-main h2 {
  margin: 0 0 3px;
  font-size: .72rem;
  font-weight: 800;
  letter-spacing: .1em;
  color: var(--muted);
}
.detail-main .field p { font-size: 1.02rem; }
.detail-storage {
  padding-top: 16px;
  border-top: 2px solid var(--line-soft);
}

/* --- กล่อง QR Code --------------------------------------------------- */
.detail-side { position: sticky; top: calc(var(--header-h) + 16px); }

.qr-card {
  padding: 20px;
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  text-align: center;
}
.qr-card h2 {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 0 0 14px;
  font-size: 1rem;
  color: var(--brand-dark);
}

.qr-box {
  display: flex;
  justify-content: center;
  padding: 14px;
  background: #ffffff;
  border: 1px solid var(--line-soft);
  border-radius: 12px;
}
.qr-target { width: 100%; max-width: 230px; line-height: 0; }
.qr-target svg { width: 100%; height: auto; }
.qr-failed { line-height: 1.5; color: var(--danger); font-size: .9rem; }
.qr-noscript { margin: 0; font-size: .85rem; color: var(--muted); line-height: 1.5; }

.qr-hint {
  margin: 12px 0 16px;
  font-size: .82rem;
  line-height: 1.5;
  color: var(--muted);
}

.qr-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.qr-actions + .qr-actions { margin-top: 8px; }

.qr-mode {
  margin: 0 0 14px;
  text-align: left;
}
.qr-mode label {
  display: block;
  margin-bottom: 4px;
  font-size: .78rem;
  font-weight: 700;
  color: var(--muted);
}
.qr-mode select {
  width: 100%;
  padding: 8px 10px;
  font: inherit;
  font-size: .85rem;
  color: var(--ink);
  background: var(--surface-3);
  border: 2px solid var(--line-soft);
  border-radius: 10px;
}
.qr-mode select:focus { border-color: var(--brand); outline: none; }
.qr-mode-note {
  margin: 6px 0 0;
  font-size: .74rem;
  line-height: 1.45;
  color: var(--faint);
}
.qr-mode-note.is-warn { color: var(--warn); font-weight: 700; }

.share-row {
  display: flex;
  gap: 6px;
  margin: 14px 0;
}
.share-row input {
  flex: 1;
  min-width: 0;
  padding: 9px 12px;
  font: inherit;
  font-size: .8rem;
  color: var(--ink-2);
  background: var(--surface-3);
  border: 1px solid var(--line-soft);
  border-radius: 10px;
}

/* =========================================================================
   รายการที่ใกล้เคียง (หน้าไม่พบข้อมูล)
   ========================================================================= */
.guess {
  padding: 22px 24px;
  margin-bottom: 24px;
  background: var(--surface);
  border: 2px solid var(--line-soft);
  border-radius: var(--radius);
}
.guess h2 { margin: 0 0 12px; font-size: 1.05rem; }
.guess-list { display: grid; gap: 4px; margin: 0; padding: 0; list-style: none; }
.guess-list a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  color: var(--ink);
  text-decoration: none;
  border-radius: 10px;
}
.guess-list a:hover { background: var(--brand-soft); }

/* =========================================================================
   หน้าสแกน QR Code
   ========================================================================= */
.scan { margin-bottom: 40px; }
.scan-head { margin: 8px 0 20px; text-align: center; }
.scan-head h1 {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 0 0 6px;
  font-size: 1.5rem;
}
.scan-head p { margin: 0; color: var(--muted); }

.scan-stage[hidden], .scan-result[hidden] { display: none; }

.scan-viewport {
  position: relative;
  max-width: 480px;
  margin: 0 auto;
  aspect-ratio: 1;
  overflow: hidden;
  background: #000;
  border: 2px solid var(--line);
  border-radius: var(--radius);
}
.scan-viewport video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.scan-frame {
  position: absolute;
  inset: 16%;
  border: 3px solid rgba(255, 255, 255, .9);
  border-radius: 18px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35);
}
.scan-status {
  margin: 14px 0 0;
  text-align: center;
  font-weight: 700;
  color: var(--muted);
}

.scan-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 20px;
}

.scan-result {
  max-width: 560px;
  margin: 24px auto 0;
  padding: 22px 24px;
  text-align: center;
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
}
.scan-result h2 { margin: 0 0 12px; font-size: 1.15rem; }
.scan-payload { margin: 0; }
.scan-payload code {
  display: inline-block;
  max-width: 100%;
  padding: 8px 12px;
  font-size: .85rem;
  color: var(--ink-2);
  background: var(--surface-3);
  border-radius: 8px;
  overflow-wrap: anywhere;
}
.scan-warn {
  margin: 16px 0 0;
  padding: 12px 14px;
  font-size: .88rem;
  color: var(--danger-ink);
  background: var(--danger-bg);
  border: 1px solid var(--danger-line);
  border-radius: 10px;
}
.scan-warn[hidden] { display: none; }

.scan-note {
  max-width: 620px;
  margin: 36px auto 0;
  padding: 16px 20px;
  font-size: .9rem;
  color: var(--ink-2);
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-left: 5px solid var(--accent);
  border-radius: 12px;
}
.scan-note h2 { margin: 0 0 6px; font-size: .98rem; color: var(--accent); }
.scan-note p { margin: 0; }
.scan-note code {
  padding: 1px 6px;
  background: var(--surface);
  border-radius: 4px;
  font-size: .9em;
}

/* =========================================================================
   การ์ดยา
   ========================================================================= */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 20px;
  align-items: start;
}

.drug-card {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  scroll-margin-top: calc(var(--header-h) + 16px);
  box-shadow: var(--shadow-sm);
  transition: transform .18s, box-shadow .18s;
}
.drug-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); }

/* ไฮไลต์การ์ดที่เพิ่งถูกลิงก์มาถึง */
.drug-card.is-target {
  border-color: var(--brand);
  box-shadow: 0 0 0 4px var(--brand-soft), var(--shadow-lg);
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 18px;
  background: var(--surface-2);
  border-bottom: 2px solid var(--line);
}
.card-head-main { min-width: 0; }
.card-head h3 a { color: inherit; text-decoration: none; }
.card-head h3 a:hover { text-decoration: underline; text-underline-offset: 2px; }
.card-head h3 {
  margin: 4px 0 0;
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--ink);
  overflow-wrap: anywhere;
}

.tag {
  display: inline-block;
  padding: 2px 9px;
  font-size: .68rem;
  font-weight: 800;
  letter-spacing: .06em;
  color: #fff;
  border-radius: 5px;
}
.tag[data-type="oral"]       { background: var(--oral); }
.tag[data-type="injectable"] { background: var(--inj); }
.tag[data-type="external"]   { background: var(--ext); }

.copy-link {
  display: grid;
  place-items: center;
  width: 34px; height: 34px;
  flex-shrink: 0;
  padding: 0;
  font-size: 1rem;
  color: var(--faint);
  background: none;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  transition: color .15s, background-color .15s;
}
.copy-link { text-decoration: none; }
.copy-link:hover { color: var(--brand); background: var(--brand-soft); }

.card-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 18px;
}

/* หนึ่งหัวข้อในการ์ด: ไอคอน + เนื้อหา */
.field { display: flex; gap: 11px; }
.field > .icon { margin-top: 5px; font-size: 1rem; }
.field-body { min-width: 0; flex: 1; }
.field h4 {
  margin: 0 0 2px;
  font-size: .7rem;
  font-weight: 800;
  letter-spacing: .1em;
  color: var(--muted);
}
.field p {
  margin: 0;
  font-size: .97rem;
  line-height: 1.55;
  color: var(--ink-2);
  overflow-wrap: anywhere;
}
.field p.strong { font-weight: 700; color: var(--ink); }

.field[data-kind="indication"] > .icon { color: var(--oral); }
.field[data-kind="usage"] > .icon      { color: var(--inj); }
.field[data-kind="adr"] > .icon        { color: var(--accent); }
.field[data-kind="contra"] > .icon     { color: var(--danger); }
.field[data-kind="caution"] > .icon    { color: var(--warn); }

/* กล่องคำเตือน: ข้อห้าม + ข้อควรระวัง */
.warn-box {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  background: var(--danger-bg);
  border: 2px solid var(--danger-line);
  border-radius: 14px;
}
.warn-box .field + .field {
  padding-top: 12px;
  border-top: 1px solid var(--danger-line);
}
.warn-box h4 { color: var(--danger); }
.warn-box p  { font-weight: 600; color: var(--ink-2); }
.warn-box .field[data-kind="caution"] p { color: var(--danger-ink); font-weight: 700; }

.card-foot {
  display: flex;
  gap: 11px;
  padding: 13px 18px;
  margin-top: auto;
  background: var(--surface-3);
  border-top: 2px solid var(--line);
}
.card-foot > .icon { margin-top: 4px; color: var(--muted); }
.card-foot h4 {
  margin: 0;
  font-size: .68rem;
  font-weight: 800;
  letter-spacing: .1em;
  color: var(--muted);
}
.card-foot p {
  margin: 0;
  font-size: .88rem;
  font-weight: 700;
  line-height: 1.45;
  color: var(--ink-2);
  overflow-wrap: anywhere;
}

/* คำที่ตรงกับคำค้น */
mark {
  padding: 0 2px;
  color: var(--mark-ink);
  background: var(--mark-bg);
  border-radius: 3px;
}

/* =========================================================================
   สถานะต่าง ๆ: กำลังโหลด / ไม่พบ / ผิดพลาด
   ========================================================================= */
.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 64px 16px;
  text-align: center;
  color: var(--muted);
}
.state[hidden] { display: none; }
.state .icon { font-size: 2.5rem; color: var(--faint); }
.state h2 { margin: 0; font-size: 1.15rem; color: var(--ink); }
.state p { margin: 0; max-width: 46ch; }

.spinner {
  width: 44px; height: 44px;
  border: 4px solid var(--line-soft);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.state-error {
  color: var(--danger-ink);
  background: var(--danger-bg);
  border: 2px solid var(--danger-line);
  border-radius: var(--radius);
}
.state-error .icon { color: var(--danger); }
.state-error h2 { color: var(--danger); }
.state-error code {
  padding: 2px 6px;
  font-size: .85rem;
  background: var(--surface);
  border-radius: 5px;
}

/* ปุ่มโหลดเพิ่ม + จุดสังเกตสำหรับเลื่อนอัตโนมัติ */
.load-more {
  display: flex;
  justify-content: center;
  padding: 28px 0;
}
.load-more[hidden] { display: none; }

/* =========================================================================
   ส่วนท้าย
   ========================================================================= */
.safety {
  margin: 48px 0 24px;
  padding: 36px 24px;
  text-align: center;
  color: #fff;
  background: #7f1d1d;
  border: 2px solid var(--line);
  border-radius: 2rem;
  box-shadow: var(--shadow-lg);
}
[data-theme="dark"] .safety { background: #631818; }
.safety > .icon { font-size: 2.6rem; opacity: .9; }
.safety h2 {
  margin: 12px 0 18px;
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -.01em;
}
.safety .lead {
  max-width: 60ch;
  margin: 0 auto 14px;
  padding-bottom: 14px;
  font-size: 1.15rem;
  font-weight: 800;
  border-bottom: 1px solid rgba(255, 255, 255, .25);
}
.safety .detail {
  max-width: 68ch;
  margin: 0 auto;
  font-size: 1rem;
  font-weight: 600;
  opacity: .95;
}
.safety .detail em {
  font-style: italic;
  font-weight: 800;
  letter-spacing: .1em;
  text-decoration: underline;
  text-underline-offset: 4px;
  text-decoration-thickness: 2px;
}
.safety-meta {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  margin-top: 26px;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, .25);
  font-size: .8rem;
}
.safety-meta .org {
  font-weight: 800;
  letter-spacing: .2em;
  opacity: .85;
}
.safety-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 14px;
}
.safety-links a { color: #fff; font-weight: 700; }
.cc-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 14px;
  background: rgba(255, 255, 255, .12);
  border-radius: 999px;
  text-decoration: none;
  transition: background-color .15s;
}
.cc-badge:hover { background: rgba(255, 255, 255, .22); }

/* =========================================================================
   ข้อความแจ้งเตือนชั่วคราว
   ========================================================================= */
.toast {
  position: fixed;
  left: 50%; bottom: 28px;
  z-index: 100;
  transform: translate(-50%, 16px);
  max-width: calc(100vw - 32px);
  padding: 12px 26px;
  font-size: .92rem;
  font-weight: 700;
  color: #fff;
  background: #1e293b;
  border: 2px solid rgba(255, 255, 255, .85);
  border-radius: 999px;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

/* ปุ่มกลับขึ้นบนสุด */
.to-top {
  position: fixed;
  right: 18px; bottom: 18px;
  z-index: 60;
  display: grid;
  place-items: center;
  width: 46px; height: 46px;
  font-size: 1.05rem;
  color: #fff;
  background: var(--brand);
  border: 0;
  border-radius: 50%;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.to-top.is-visible { opacity: .95; pointer-events: auto; }
.to-top:hover { transform: translateY(-2px); opacity: 1; }

/* =========================================================================
   หน้าเอกสารอ้างอิง
   ========================================================================= */
.ref-card {
  padding: 28px;
  margin-bottom: 24px;
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: 1.5rem;
  box-shadow: var(--shadow-sm);
}
.ref-card h2 {
  margin: 0 0 14px;
  font-size: 1.3rem;
  color: var(--brand-dark);
}
.ref-card h3 {
  margin: 20px 0 8px;
  font-size: 1.05rem;
  color: var(--ink);
}
.ref-card p, .ref-card li { color: var(--ink-2); }
.ref-card a { color: var(--brand); }
.ref-note {
  padding: 16px 20px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-left: 5px solid var(--accent);
  border-radius: 12px;
}


/* =========================================================================
   หน้าเอกสารอ้างอิง (ส่วนเพิ่มเติม)
   ========================================================================= */
.page-title { margin: 8px 0 20px; font-size: 1.6rem; }

.ref-facts { margin: 16px 0; display: grid; gap: 8px; }
.ref-facts > div {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line-soft);
}
.ref-facts dt { min-width: 190px; font-weight: 700; color: var(--muted); }
.ref-facts dd { margin: 0; font-weight: 700; }

.ref-list { margin: 0; padding: 0; list-style: none; display: grid; gap: 12px; }
.ref-list li {
  padding-left: 16px;
  border-left: 3px solid var(--brand);
}
.ref-list b { display: block; color: var(--ink); }

.ref-cols { display: grid; gap: 20px; margin: 18px 0; }
.ref-cols ul { margin: 6px 0 0; padding-left: 20px; }
.ref-cols li { margin-bottom: 5px; }

/* =========================================================================
   แผ่นพิมพ์ QR Code ยกชุด
   ========================================================================= */
.sheet-setup { margin: 8px 0 28px; }
.sheet-setup h1 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 6px;
  font-size: 1.5rem;
}
.sheet-setup > p { margin: 0 0 18px; color: var(--muted); }

.sheet-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  padding: 18px 20px;
  background: var(--surface);
  border: 2px solid var(--line-soft);
  border-radius: var(--radius);
}
.sheet-field { display: flex; flex-direction: column; gap: 5px; flex: 1 1 200px; }
.sheet-field label { font-size: .82rem; font-weight: 700; color: var(--muted); }
.sheet-field input,
.sheet-field select {
  padding: 9px 12px;
  font: inherit;
  font-size: .92rem;
  color: var(--ink);
  background: var(--surface-3);
  border: 2px solid var(--line-soft);
  border-radius: 10px;
}
.sheet-field input:focus,
.sheet-field select:focus { border-color: var(--brand); outline: none; }

.sheet-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 16px;
  font-size: .9rem;
  color: var(--muted);
}
.sheet-bar p { margin: 0; }
.sheet-bar strong { color: var(--ink); font-size: 1.05rem; }

.sheet-note {
  margin-top: 14px;
  padding: 14px 18px;
  font-size: .88rem;
  color: var(--ink-2);
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-left: 5px solid var(--accent);
  border-radius: 10px;
}

/* --- ตัวป้าย ---------------------------------------------------------- */
.qr-sheet {
  display: grid;
  gap: 4mm;
  grid-template-columns: repeat(auto-fill, minmax(42mm, 1fr));
}
.qr-sheet[data-size="sm"] { grid-template-columns: repeat(auto-fill, minmax(32mm, 1fr)); }
.qr-sheet[data-size="lg"] { grid-template-columns: repeat(auto-fill, minmax(58mm, 1fr)); }

.qr-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5mm;
  padding: 2mm;
  text-align: center;
  background: #ffffff;
  border: 1px dashed #94a3b8;
  border-radius: 6px;
  break-inside: avoid;
}
.qr-label.is-failed { border-color: var(--danger); }

.qr-label-code { width: 100%; line-height: 0; }
.qr-label-code svg { width: 100%; height: auto; display: block; }

.qr-label-text {
  display: flex;
  flex-direction: column;
  width: 100%;
  line-height: 1.25;
  color: #000;
}
.qr-label-name {
  font-size: 7pt;
  font-weight: 700;
  overflow-wrap: anywhere;
}
.qr-label-type { font-size: 5.5pt; color: #475569; }

.qr-sheet[data-size="sm"] .qr-label-name { font-size: 6pt; }
.qr-sheet[data-size="lg"] .qr-label-name { font-size: 9pt; }
.qr-sheet[data-size="lg"] .qr-label-type { font-size: 7pt; }

/* =========================================================================
   จอเล็ก / จอใหญ่
   ========================================================================= */
@media (min-width: 640px) {
  .brand-title { font-size: 1.4rem; }
  .brand-org   { font-size: .95rem; }
  .brand-logo { width: 58px; height: 58px; }
}

@media (min-width: 900px) {
  :root { --header-h: 104px; }
  .header-inner {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding-block: 16px;
  }
  .brand { width: auto; }
  .toolbar { flex: 0 1 620px; }

  /* หน้ารายตัว: เนื้อหาซ้าย QR Code ขวา */
  .detail-grid { grid-template-columns: minmax(0, 1fr) 320px; }
  .ref-cols { grid-template-columns: 1fr 1fr; }
  .detail-head h1 { font-size: 1.9rem; }
}

@media (max-width: 480px) {
  body { font-size: 17px; }
  .card-grid { grid-template-columns: 1fr; gap: 16px; }
  .safety { padding: 28px 18px; border-radius: 1.5rem; }
  .safety h2 { font-size: 1.25rem; }
}

/* ผู้ใช้ที่ตั้งค่าลดการเคลื่อนไหว */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}

/* =========================================================================
   สำหรับสั่งพิมพ์ / บันทึกเป็น PDF
   ========================================================================= */
@media print {
  @page { margin: 12mm; }

  body {
    font-size: 11pt;
    background: #fff;
    color: #000;
  }

  .site-header,
  .filters,
  .alpha-bar,
  .toc,
  .load-more,
  .to-top,
  .toast,
  .copy-link,
  .no-print { display: none !important; }

  .wrap { max-width: none; padding: 0; }

  .result-bar {
    margin-bottom: 10mm;
    padding-bottom: 3mm;
    border-bottom: 1pt solid #000;
    color: #000;
  }

  .card-grid {
    display: block;
    column-count: 2;
    column-gap: 8mm;
  }

  .drug-card {
    break-inside: avoid;
    page-break-inside: avoid;
    display: block;
    margin-bottom: 6mm;
    border: 1pt solid #000;
    border-radius: 0;
    box-shadow: none;
  }
  .drug-card:hover { transform: none; }

  .card-head, .card-foot { background: #fff; border-color: #000; }
  .card-head { border-bottom: 1pt solid #000; }
  .card-foot { border-top: 1pt solid #000; }
  .tag { color: #000; background: #fff !important; border: 1pt solid #000; }
  .warn-box { background: #fff; border: 1pt solid #000; }
  .field p, .card-foot p { color: #000; }
  mark { background: none; color: #000; font-weight: 800; text-decoration: underline; }

  /* หน้ารายตัว: พิมพ์เป็นเอกสารให้ผู้ป่วยได้ QR ต้องติดไปด้วย */
  .crumbs, .pager, .result-actions, .scan-actions, .share-row,
  .qr-actions, .scan-note, .scan-stage { display: none !important; }

  .detail-grid {
    display: grid;
    grid-template-columns: 1fr 200px;
    gap: 6mm;
  }
  .detail-head, .detail-main, .qr-card {
    border: 1pt solid #000;
    border-radius: 0;
    box-shadow: none;
    break-inside: avoid;
  }
  .detail-side { position: static; }
  .qr-target { max-width: 170px; }
  .qr-box { border-color: #000; }
  .qr-hint { color: #000; }

  /* แผ่นพิมพ์ QR: กำหนดขนาดป้ายเป็นมิลลิเมตรจริงเพื่อให้พิมพ์ออกมาได้ขนาดตามที่เลือก */
  .sheet-setup { display: none !important; }
  .qr-sheet {
    display: grid;
    gap: 3mm;
    grid-template-columns: repeat(auto-fill, 40mm);
    justify-content: start;
  }
  .qr-sheet[data-size="sm"] { grid-template-columns: repeat(auto-fill, 30mm); }
  .qr-sheet[data-size="lg"] { grid-template-columns: repeat(auto-fill, 55mm); }
  .qr-label { border: 1px dashed #cbd5e1; }
  .qr-sheet + .safety { display: none !important; }

  .safety {
    color: #000;
    background: #fff;
    border: 2pt solid #000;
    border-radius: 0;
    break-inside: avoid;
  }
  .safety-meta { border-top-color: #000; }
  .safety .lead { border-bottom-color: #000; }
  .safety-links, .cc-badge { display: none; }
}

/* ชุดสัญลักษณ์ที่ฝังไว้ต้นหน้า ไม่ใช่ภาพที่ต้องแสดงเอง */
.icon-sprite { display: none; }

/* ------------------------------------------- เครื่องมือสร้าง QR ที่หน้าแรก */

.home-tool {
  margin: 2.5rem auto 0;
  max-width: 44rem;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.home-tool-head { text-align: center; margin-bottom: 1.25rem; }

.home-tool-head h2 {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  font-size: 1.15rem;
  margin: 0 0 .4rem;
}

.home-tool-head p {
  margin: 0;
  font-size: .88rem;
  color: var(--muted);
  line-height: 1.6;
}
