/* Iodine Tracker – สไตล์หลัก
   สีน้ำเงินกับฟ้าจากโลโก้บริษัท ที่เหลือเป็นขาว/เทา เพื่อให้หน้าจอสะอาดตา
   สีแดง/เขียวใช้เฉพาะแสดงสถานะ ปุ่มและช่องกรอกสูง 44px สำหรับแท็บเล็ต */

@font-face { font-family: "Sarabun"; font-weight: 400; font-display: swap; src: url("fonts/Sarabun-Regular.ttf") format("truetype"); }
@font-face { font-family: "Sarabun"; font-weight: 500; font-display: swap; src: url("fonts/Sarabun-Medium.ttf") format("truetype"); }
@font-face { font-family: "Sarabun"; font-weight: 600; font-display: swap; src: url("fonts/Sarabun-SemiBold.ttf") format("truetype"); }

:root {
  --navy: #0d4c8f;
  --navy-dark: #0a3c72;
  --sky: #29b9ed;
  --tint: #eaf6fc;
  --text: #1b2733;
  --muted: #5f6b7a;
  --border: #dde3ea;
  --hover: #f5f8fb;
  --danger: #b3261e;
  --danger-tint: #fdeeed;
  --ok: #1a7340;
  --ok-tint: #e8f5ed;
  --warn: #7a5600;
  --warn-tint: #fdf5df;
  --radius: 8px;
}

* { box-sizing: border-box; }
html { font-size: 16px; }
body {
  margin: 0; background: #fff; color: var(--text);
  font-family: "Sarabun", "Leelawadee UI", "Thonburi", "Noto Sans Thai", Tahoma, sans-serif;
  line-height: 1.6;
}
a { color: var(--navy); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2 { font-weight: 600; line-height: 1.4; margin: 0; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; margin-bottom: .75rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.center { text-align: center; }

/* ---------- แถบบน ---------- */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0 1.5rem;
  padding: 0 1.5rem; border-bottom: 1px solid var(--border); background: #fff;
}
.topbar .logo { display: block; padding: .5rem 0; line-height: 0; }
.topbar .logo img { height: 36px; }
.nav { display: flex; flex: 1; gap: 1.25rem; overflow-x: auto; }
.nav a, .nav span {
  padding: 1.1rem 0 calc(1.1rem - 3px); border-bottom: 3px solid transparent;
  color: var(--muted); font-weight: 500; white-space: nowrap;
}
.nav a:hover { color: var(--navy); text-decoration: none; }
.nav a.active { color: var(--navy); border-bottom-color: var(--sky); }
.nav span { color: #a9b2bd; }
.user { display: flex; align-items: center; gap: 1rem; margin-left: auto; padding: .5rem 0; }
.user form { margin: 0; }
.link-btn { padding: 0; border: 0; background: none; color: var(--muted); font: inherit; cursor: pointer; }
.link-btn:hover { color: var(--navy); text-decoration: underline; }

/* ---------- เนื้อหา ---------- */
main { max-width: 1120px; margin: 0 auto; padding: 1.75rem 1.5rem 3rem; }
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: .75rem 1.5rem; margin-bottom: 1.5rem; }
.crumb { font-size: .875rem; color: var(--muted); margin-bottom: .2rem; }
.crumb:empty { display: none; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.actions form { margin: 0; }

.messages { list-style: none; padding: 0; margin: 0 0 1.25rem; }
.messages li { padding: .65rem 1rem; border-radius: var(--radius); margin-bottom: .5rem; background: var(--ok-tint); color: var(--ok); }
.messages li.error { background: var(--danger-tint); color: var(--danger); }
.messages li.warning { background: var(--warn-tint); color: var(--warn); }

.card { border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem 1.5rem; margin-bottom: 1.25rem; background: #fff; }
.narrow { max-width: 720px; }
.empty { padding: 2.5rem 1rem; text-align: center; color: var(--muted); }

/* ---------- ปุ่ม ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 44px; padding: .4rem 1.1rem; border: 1px solid var(--border); border-radius: var(--radius);
  background: #fff; color: var(--text); font: inherit; font-weight: 500; cursor: pointer;
}
.btn:hover { background: var(--hover); text-decoration: none; }
.btn-primary { background: var(--navy); border-color: var(--navy); color: #fff; }
.btn-primary:hover { background: var(--navy-dark); border-color: var(--navy-dark); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-tint); border-color: var(--danger-tint); }
.btn-sm { min-height: 36px; padding: .15rem .75rem; font-size: .9rem; }
/* ปุ่มไปบันทึกก่อนหน้า/ถัดไป: เปลี่ยนเป็นสีน้ำเงินเมื่อชี้ เพื่อให้รู้ว่ากดได้ */
.record-nav { display: inline-flex; gap: .25rem; margin-left: .5rem; vertical-align: middle; }
.record-nav .btn { min-width: 44px; padding: 0 .5rem; color: var(--navy); font-size: .9rem; transition: background-color .15s, color .15s, border-color .15s; }
.record-nav a.btn:hover, .record-nav a.btn:focus-visible { background: var(--navy); border-color: var(--navy); color: #fff; }
.record-nav .is-disabled, .record-nav .is-disabled:hover { background: #fff; color: #c3cad3; cursor: not-allowed; }

/* ---------- ฟอร์ม ---------- */
label { display: block; margin-bottom: .25rem; font-weight: 500; font-size: .95rem; }
input[type=text], input[type=password], input[type=email], input[type=number], input[type=time],
input[type=date], input[type=search], select, textarea {
  width: 100%; min-height: 44px; padding: .45rem .75rem; font: inherit; color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius); background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--sky); outline-offset: 0; border-color: var(--sky); }
input:disabled, select:disabled, textarea:disabled { background: var(--hover); color: var(--text); opacity: 1; }
/* Safari บน iPad ไม่ใช้ min-height กับ select และให้ช่องวันที่กว้างเกินกล่อง จึงต้องปิดหน้าตาเริ่มต้นของระบบ */
input[type=date], select:not([multiple]) { -webkit-appearance: none; appearance: none; height: 44px; }
input[type=date] { display: block; min-width: 0; max-width: 100%; }
input[type=date]::-webkit-date-and-time-value { text-align: left; }
select:not([multiple]) {
  padding-right: 2.25rem; background-repeat: no-repeat; background-position: right .75rem center; background-size: 12px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2 4.5l4 4 4-4' fill='none' stroke='%235f6b7a' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}
input[type=checkbox] { width: 20px; height: 20px; margin: 0; accent-color: var(--navy); flex: none; }
input[type=file] { width: 100%; font: inherit; font-size: .9rem; color: var(--muted); }
input[type=file]::file-selector-button {
  min-height: 40px; margin-right: .75rem; padding: 0 .9rem; border: 1px solid var(--border);
  border-radius: var(--radius); background: #fff; color: var(--text); font: inherit; font-weight: 500; cursor: pointer;
}
.field { margin-bottom: 1rem; }
.field .help { color: var(--muted); font-size: .85rem; margin-top: .2rem; }
.field ul { padding-left: 1.1rem; margin: .25rem 0 0; }
.errorlist, .field ul.errorlist { list-style: none; padding: 0; margin: .25rem 0 0; color: var(--danger); font-size: .9rem; }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--danger); }
.field-check label { display: inline-flex; gap: .5rem; align-items: center; margin: 0; }
.field div > div { display: inline-block; }
.field div > div > label { display: inline-flex; gap: .4rem; align-items: center; margin-right: 1.25rem; font-weight: 400; }
.errorlist.nonfield { margin: 0 0 1rem; padding: .65rem 1rem; background: var(--danger-tint); border-radius: var(--radius); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 1.25rem; }
.form-grid.two { grid-template-columns: repeat(2, 1fr); }
.form-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .5rem; }

.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .75rem; margin-bottom: 1.25rem; }
.filters > div { min-width: 9rem; }
.filters .grow { flex: 1; min-width: 14rem; }
.filters .check { min-width: 0; padding-bottom: .6rem; }
.filters .check label { display: inline-flex; gap: .45rem; align-items: center; margin: 0; font-weight: 400; }
.filters .buttons { display: flex; gap: .5rem; min-width: 0; }
.filters-even > div:not(.buttons) { flex: 0 0 10.5rem; min-width: 0; }

/* ---------- ตาราง ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { padding: .65rem 1rem; text-align: left; vertical-align: middle; border-bottom: 1px solid var(--border); }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data th { background: var(--hover); color: var(--muted); font-size: .875rem; font-weight: 500; white-space: nowrap; }
table.data td.right, table.data th.right { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.center, table.data th.center { text-align: center; font-variant-numeric: tabular-nums; }
table.data.wide { min-width: 800px; }
@media (max-width: 1000px) {
  table.data.record-list { font-size: .875rem; }
  table.data.record-list th { white-space: normal; }
  table.data.record-list th, table.data.record-list td { padding-left: .4rem; padding-right: .4rem; }
  table.data.record-list th:first-child, table.data.record-list td:first-child { padding-left: .8rem; }
  table.data.record-list th:last-child, table.data.record-list td:last-child { padding-right: .8rem; }
}
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: var(--hover); }
table.data tr.inactive td { color: var(--muted); }
.row-actions { display: inline-flex; gap: .4rem; }
.row-actions form { margin: 0; }
.table-note { display: flex; justify-content: space-between; gap: 1rem; margin-top: .6rem; font-size: .875rem; color: var(--muted); }

.badge { display: inline-block; padding: 0 .55rem; border-radius: 999px; font-size: .8rem; font-weight: 500; background: var(--hover); color: var(--muted); white-space: nowrap; }
.badge-ok { background: var(--ok-tint); color: var(--ok); }
.badge-danger { background: var(--danger-tint); color: var(--danger); }
.badge-info { background: var(--tint); color: var(--navy); }
.pagination { display: flex; gap: .75rem; align-items: center; justify-content: center; margin-top: 1rem; }

.diff { font-size: .85rem; margin: 0; padding: 0; list-style: none; }
.diff .old { color: var(--danger); text-decoration: line-through; }
.diff .new { color: var(--ok); }

/* ---------- หน้าหลัก / ตั้งค่า ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; }
.tile { display: block; padding: 1.1rem 1.25rem; border: 1px solid var(--border); border-radius: var(--radius); color: var(--text); }
a.tile:hover { border-color: var(--navy); background: var(--tint); text-decoration: none; }
.tile .name { display: block; font-size: 1.1rem; font-weight: 600; color: var(--navy); }
.tile .desc { display: block; color: var(--muted); font-size: .925rem; }
.tile.off { background: var(--hover); border-color: transparent; }
.tile.off .name { color: var(--muted); }
.brands { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 2rem; margin-top: 3rem; padding-top: 1.5rem; border-top: 1px solid var(--border); }
.brands img { height: 40px; }

/* ---------- ผู้รับอีเมลแจ้งเตือน ---------- */
.alert-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 1.25rem; margin-bottom: 1.25rem; }
.alert-grid .card { margin-bottom: 0; }
.alert-grid .field div > div { display: block; padding: .15rem 0; }
.alert-grid .field div > div > label { margin-right: 0; overflow-wrap: anywhere; }

/* ---------- สินค้า ---------- */
.product-layout { display: grid; grid-template-columns: 240px 1fr; gap: 2rem; }
.image-box {
  display: flex; align-items: center; justify-content: center; aspect-ratio: 1; overflow: hidden; margin: 0 0 .6rem;
  border: 1px dashed var(--border); border-radius: var(--radius); background: var(--hover); color: var(--muted); font-weight: 400;
}
.image-box img { max-width: 100%; max-height: 100%; object-fit: contain; }
.signature-box { max-width: 320px; aspect-ratio: 3 / 1; margin-bottom: 1rem; }
.thumb { display: block; width: 40px; height: 40px; object-fit: contain; }

/* ---------- เข้าสู่ระบบ ---------- */
.login { max-width: 380px; margin: 8vh auto 0; }
.login .logo { display: block; height: 72px; margin: 0 auto 1.5rem; }
.login h1 { font-size: 1.25rem; text-align: center; }
.login .sub { margin-bottom: 1.5rem; text-align: center; color: var(--muted); }
.login .btn { width: 100%; }

@media (max-width: 900px) {
  .topbar { padding: 0 1rem; }
  .nav { order: 3; flex-basis: 100%; }
  .nav a, .nav span { padding: .6rem 0 calc(.6rem - 3px); }
  main { padding: 1.25rem 1rem 2rem; }
}
@media (max-width: 640px) {
  .product-layout { grid-template-columns: 1fr; }
  .form-grid.two { grid-template-columns: 1fr; }
  .alert-grid { grid-template-columns: 1fr; }
  .image-box { max-width: 240px; }
}

/* ---------- บันทึกผลตรวจ ---------- */
.fail { color: var(--danger); font-weight: 600; }
.badge-warn { background: var(--warn-tint); color: var(--warn); }
.record-meta { color: var(--muted); }
.record-meta strong { color: var(--text); font-weight: 500; }
.form-grid.three { grid-template-columns: repeat(3, 1fr); }
input[type=radio] { width: 20px; height: 20px; margin: 0; accent-color: var(--navy); flex: none; }
.countdown { display: inline-flex; flex-direction: column; align-items: flex-end; line-height: 1.3; }
.countdown strong { font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.countdown.late strong { color: var(--danger); }
table.results { min-width: 620px; }
table.results td.right, table.results th.right { text-align: right; }
table.results td { font-variant-numeric: tabular-nums; }
.entry-panel { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .75rem 1.5rem; padding: .9rem 1rem; background: var(--hover); margin-bottom: 1.25rem; border: 1px solid var(--border); border-radius: var(--radius); }
.entry-field { flex: 0 1 auto; }
.entry-field.grow { flex: 1 1 300px; min-width: 0; }
.entry-field.grow-wide { flex: 1.35 1 390px; }
.entry-field select { min-width: 110px; }
.entry-field label, .entry-label { display: block; margin-bottom: .25rem; font-weight: 500; font-size: .95rem; }
.entry-field.has-error input { border-color: var(--danger); }
.result-field { min-width: 76px; }
.result-field #live-result { min-height: 44px; line-height: 44px; }
.record-summary p { margin-bottom: .35rem; }
.record-facts { display: flex; flex-wrap: wrap; gap: .5rem 2.5rem; margin: 0 0 .75rem; }
.record-facts dt { color: var(--muted); font-size: .875rem; }
.record-facts dd { margin: 0; color: var(--navy); font-size: 1.25rem; font-weight: 600; line-height: 1.4; }
.record-summary details { margin-top: .5rem; }
.record-summary summary { color: var(--navy); cursor: pointer; font-weight: 500; padding: .5rem 0; }
.record-summary form { margin-top: .5rem; }
.stepper { display: grid; grid-template-columns: repeat(2, minmax(46px, 1fr)) minmax(92px, 1.6fr) repeat(2, minmax(46px, 1fr)); gap: 5px; }
.stepper-wide { grid-template-columns: repeat(3, minmax(46px, 1fr)) minmax(92px, 1.6fr) repeat(3, minmax(46px, 1fr)); }
.stepper input { width: auto; min-width: 0; margin: 0 4px; padding: .45rem .5rem; text-align: right; font-variant-numeric: tabular-nums;
  font-size: 1.15rem; font-weight: 600; color: var(--navy); border-color: var(--navy); }
/* กล่องแยกของช่องน้ำหนักและค่าไอโอดีน ให้มองแยกกันได้ชัด */
.measure-box { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .75rem; }
.measure-box > label { color: var(--navy); font-weight: 600; }
.measure-box.has-error { border-color: var(--danger); }
.step { min-height: 44px; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; color: var(--navy); font: inherit; font-size: .8rem; font-weight: 500; cursor: pointer; touch-action: manipulation; }
.step:active { background: var(--tint); }
.chart-section { margin-top: 1.5rem; }
.tabs { display: flex; gap: 1.25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tab { padding: .5rem 0 calc(.5rem - 3px); border: 0; border-bottom: 3px solid transparent; background: none; color: var(--muted); font: inherit; font-weight: 500; cursor: pointer; }
.tab:hover { color: var(--navy); }
.tab.active { color: var(--navy); border-bottom-color: var(--sky); }
/* ตาราง IUS มี 10 คอลัมน์ ต้องแสดงครบบนแท็บเล็ตโดยไม่ต้องเลื่อนซ้ายขวา */
table.data.results-compact th, table.data.results-compact td { padding-left: .4rem; padding-right: .4rem; }
table.data.results-compact th:first-child, table.data.results-compact td:first-child { padding-left: .8rem; }
table.data.results-compact th:last-child, table.data.results-compact td:last-child { padding-right: .8rem; }
.results-compact .product-col { min-width: 170px; }
@media (max-width: 1000px) {
  table.data.results-compact { font-size: .875rem; }
  table.data.results-compact th { white-space: normal; }
  .results-compact .product-col { min-width: 110px; }
  .results-compact .row-actions { flex-direction: column; align-items: stretch; }
}
.entry-break { flex-basis: 100%; height: 0; }
.product-field { flex: 0 1 36rem; min-width: 0; max-width: 100%; }
.product-text-field { flex: 1 1 16rem; min-width: 0; }
.entry-field > .errorlist { margin: .25rem 0 0; }
.shift-table { border-collapse: collapse; margin-top: .25rem; }
.shift-table th { padding: .1rem 1.5rem .1rem 0; color: var(--text); font-weight: 600; text-align: left; white-space: nowrap; vertical-align: top; }
.shift-table td { padding: .1rem 0; }
.chart-title { text-align: center; margin-bottom: .25rem; }
.chart-box { position: relative; height: 340px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 1rem; margin: 1rem 0 0; }
.stats div { padding: 1rem 1.25rem; border: 1px solid var(--border); border-radius: var(--radius); }
.stats dt { color: var(--muted); font-size: .875rem; }
.stats dd { margin: 0; font-size: 1.5rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.notes-text { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--border); color: var(--muted); font-size: .9rem; }
.notes-text strong { color: var(--text); font-weight: 600; }
.limit-table { width: 100%; border-collapse: collapse; margin-bottom: 1rem; }
.limit-table th { color: var(--muted); font-size: .85rem; font-weight: 500; text-align: left; padding: 0 .4rem .2rem 0; }
.limit-table td { padding: 0 .4rem .4rem 0; vertical-align: top; }
.alert-section-title { margin: 1rem 0 .25rem; font-weight: 500; font-size: .95rem; }
@media (max-width: 900px) {
  .form-grid.three { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 640px) {
  .form-grid.three { grid-template-columns: 1fr; }
}
.approval-card { background: var(--ok-tint); border-color: #cfe6d7; }
.approval-card p { margin-bottom: .4rem; }
/* แดชบอร์ด (Phase 6) */
.dash-section { margin-bottom: 2rem; }
.dash-section h2 { margin-bottom: .75rem; }
.dash-section h2 .small { font-weight: 400; margin-left: .35rem; }
.dash-counts { min-width: 460px; }
.dash-counts td, .dash-counts th { font-variant-numeric: tabular-nums; }
.dash-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 1.5rem; }
table.data tr.is-late td { background: var(--danger-tint); }
table.data tr.is-late .dash-countdown { color: var(--danger); }
.dash-countdown { font-weight: 600; font-variant-numeric: tabular-nums; }
.trend-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); gap: 1rem; }
.trend-card { margin: 0; padding: .75rem 1rem 1rem; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
.trend-card figcaption { margin-bottom: .5rem; }
.trend-box { position: relative; height: 240px; }
@media (max-width: 1000px) {
  .dash-grid { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .trend-grid { grid-template-columns: 1fr; }
}
