/* ตัวแปร และ reset พื้นฐาน */
:root {
  --brand: #004085;
  --ink: #212529;
  --muted: #6c757d;
  --link: #0d6efd;
  --card: #ffffff;
  --bg: #f8f9fa;
  --border: #dee2e6;
}

* {
  box-sizing: border-box;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Thai", "Noto Sans", sans-serif;
  margin: 0;
  padding: 1.5rem;
  background: var(--bg);
  color: var(--ink);
}

.page {
  max-width: 960px;
  margin: 0 auto;
}

/* ส่วนหัวหน้าเอกสาร */

.page-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.page-logo {
  flex-shrink: 0;
}

.page-logo img {
  display: block;
  height: 100px;
  width: auto;
}

.page-title-wrap {
  text-align: center;
}

h1 {
  font-size: 1.75rem;
  margin: 0 0 0.25rem;
  color: var(--brand);
  line-height: 1.3;
}

h2 {
  font-size: 1.25rem;
  margin: 0 0 0.25rem;
  color: var(--brand);
  line-height: 1.3;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

/* กล่อง card หลัก และโครงฟอร์ม */

.card {
  background: var(--card);
  border-radius: 0.75rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.04);
  padding: 1.25rem 1.5rem 1.5rem;
  border: 1px solid var(--border);
  position: relative;
}

.section-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 1.25rem 0 0.5rem;
  color: var(--brand);
}

/* อยู่ติดกับหัวกระดาษมากเป็นพิเศษ */

.card .section-title:first-of-type {
  margin-top: 0.5rem;
  margin-bottom: 0.25rem;
}

/* คอลัมน์สำหรับรายละเอียด + ราคาทุน/ราคาขาย */

.grid {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 1.25rem 1.75rem;
  margin-top: 0.75rem !important;
}

/* ฟิลด์ข้อความ/ตัวเลข และ label */

label {
  display: block;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 0.25rem;
}

select, input[type="number"], input[type="text"], input[readonly] {
  width: 100%;
  padding: 0.45rem 0.6rem;
  border-radius: 0.4rem;
  border: 1px solid var(--border);
  font-size: 1.1rem;
  font-family: inherit;
}

select:focus, input:focus, input[type="text"]:focus {
  outline: none;
  border-color: var(--link);
  box-shadow: 0 0 0 2px rgba(13, 110, 253, 0.15);
}

/* ช่องราคาทุนตัวใหญ่ */

.cost-input-big {
  font-size: 1.25rem !important;
  font-weight: 500;
}

.cost-label {
  font-size: 1.1rem !important;
  font-weight: 600;
}

/* แถวรายละเอียดรายการยา */

.field-row {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0;
  font-size: 1rem;
  line-height: 1.25;
  border-bottom: 1px solid #e5e5e5;
}

.field-row:last-child {
  border-bottom: none;
}

.field-label {
  color: var(--muted);
  white-space: nowrap;
}

.field-value {
  flex: 1;
  text-align: right;
  font-weight: 500;
  word-break: break-word;
}

/* ข้อความช่วยอธิบาย */

.helptext {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.25rem;
  margin-bottom: 0.25rem;
}

/* ช่วยจัดระยะบางส่วน */

#drugSelect {
  margin-top: 0.25rem !important;
}

.helptext.helptext-search {
  margin-top: 0.15rem;
}

/* บล็อกราคาทุน/ราคาขาย */

.result-block {
  border-radius: 0.6rem;
  border: 1px dashed var(--border);
  padding: 0.75rem 0.9rem;
  background: #fdfdff;
  margin-top: 0.75rem;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 1rem;
  margin-bottom: 0.25rem;
}

.result-main {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--brand);
}

.result-sub {
  font-size: 0.8rem;
  color: var(--muted);
}

.raw-small {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  border: 1px solid #e0e0e0;
  background: #ffffff;
}

.pill-label {
  color: var(--muted);
  margin-right: 0.25rem;
}

.pill-value {
  font-weight: 500;
}

/* แถบสถานะด้านล่าง */

.status-strip {
  margin-top: 0.4rem;
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
  line-height: 1.2;
}

.status-main {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  flex: 1 1 auto;
}

.status-scope {
  flex: 1 1 auto;
  text-align: right;
  white-space: normal;
  overflow-wrap: break-word;
  word-break: break-word;
}

/* Auto-complete dropdown */

.autocomplete-list {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  background: #ffffff;
  border: 1px solid var(--border);
  border-top: none;
  display: none;
  max-height: 220px;
  overflow-y: auto;
  z-index: 99999;
  border-radius: 0 0 0.4rem 0.4rem;
}

.autocomplete-item {
  padding: 0.55rem 0.75rem;
  cursor: pointer;
  font-size: 1rem;
  border-bottom: 1px solid #f1f1f1;
}

.autocomplete-item:hover {
  background: #eef5ff;
}

/* ปุ่มพิมพ์ */

#printButton {
  position: absolute;
  top: 0.5rem;
  right: 1.5rem;
  padding: 0.25rem 0.75rem;
  font-size: 0.9rem;
  border-radius: 0.4rem;
  border: 1px solid var(--border);
  background: #ffffff;
  cursor: pointer;
}

#printButton:hover {
  background: #f1f3f5;
}

/* Responsive จอเล็ก */

@media (max-width: 640px) {
  body {
    padding: 1rem 0.75rem;
  }
}

@media (max-width: 640px) {
  .page-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
}

@media (max-width: 640px) {
  .page-logo img {
    height: 60px;
  }
}

@media (max-width: 640px) {
  .card {
    padding: 1rem 1rem 1.25rem;
  }
}

/* grid เป็นคอลัมน์เดียว */

@media (max-width: 768px) {
  .grid {
    grid-template-columns: 1fr;
  }
}

/* status strip บนมือถือ */

@media (max-width: 640px) {
  .status-strip {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (max-width: 640px) {
  .status-scope {
    text-align: left;
  }
}

/* print mode */
@media print {

    @page {
        size: A4 portrait !important;
        margin: 8mm;
    }

    body {
        margin: 0;
        padding: 0;
        background: #ffffff;
    }

    .page {
        width: 210mm;
        max-width: 210mm;
        margin: 0 auto;
    }

    .page-header {
        position: static;
        flex-direction: row !important;
        align-items: center;
        justify-content: center;
        padding: 0.5rem 0 0rem;
        margin-bottom: 0rem;
        background: #ffffff;
    }

    .page-logo img {
        height: 90px;
    }

    .helptext-search {
        display: none !important;
    }

    .autocomplete-list,
    .helptext-cost,
    #printButton {
        display: none !important;
    }

    .card {
        box-shadow: none;
        border-radius: 0;
        border: none;
        padding: 0 0.25rem 0.5rem;
        margin-top: 0;
    }

    h1 {
        font-size: 1.4rem;
    }

    h2 {
        font-size: 1.05rem;
    }

    .section-title {
        font-size: 1.1rem;
        margin-top: 0.75rem;
        margin-bottom: 0.25rem;
    }

    .field-row {
        padding: 0.25rem 0;
        font-size: 1.05rem;
    }

    .result-main {
        font-size: 1.5rem;
    }

    .helptext,
    .raw-small,
    .result-sub {
        font-size: 0.7rem;
    }

    .grid {
        grid-template-columns: 1.1fr 1fr !important;
        gap: 1.25rem 1.75rem;
    }

    .status-strip {
        font-size: 0.7rem;
        flex-direction: row !important;
        align-items: flex-start;
        margin-top: 0.75rem;
        padding-top: 0.25rem;
        border-top: 1px solid var(--border);
    }

    .status-scope {
        text-align: right !important;
    }
}