* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: var(--fs);
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}
h1, h2, h3 { font-family: var(--font-head); line-height: 1.3; margin: 0 0 .5em; }
h1 { font-size: 24px; }
h2 { font-size: 19px; }
small, .muted { color: var(--text-2); font-size: 13px; }

button, .btn {
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0 18px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
button.primary { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
button.danger { color: var(--danger); border-color: var(--danger); }
button:focus-visible, a:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }

/* ---------- โครงหลัก ---------- */
.dev-banner {
  background: var(--warn-soft);
  color: var(--warn);
  text-align: center;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
}
/* แถบแจ้งเตือนสารใกล้หมดอายุ/หมดอายุ (ติดด้านบนทุกหน้า) */
#alert-bar { position: sticky; top: 0; z-index: 20; }
.alert-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: var(--tap);
  padding: 8px 16px;
  text-decoration: none;
  font-weight: 600;
  font-size: 15px;
}
.alert-bar.is-soon { background: var(--warn-soft); color: var(--warn); border-bottom: 3px solid var(--warn); }
.alert-bar.is-expired { background: var(--danger); color: var(--on-danger); }
.alert-ico { font-size: 18px; }
.alert-text { flex: 1; min-width: 0; }
.alert-go { white-space: nowrap; text-decoration: underline; }

.layout { display: flex; min-height: 100dvh; }
.sidebar { display: none; }
.content {
  flex: 1;
  min-width: 0;
  padding: var(--gap);
  padding-bottom: calc(var(--tabbar) + var(--gap) + env(safe-area-inset-bottom));
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--tabbar) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card);
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  z-index: 10;
}
.nav-link {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--tap);
  color: var(--text-2);
  text-decoration: none;
  font-size: var(--fs-min);
}
.nav-link .ico { font-size: 20px; line-height: 1; }
.nav-link[aria-current="page"] { color: var(--primary); font-weight: 600; }

@media (min-width: 900px) {
  .tabbar { display: none; }
  .sidebar {
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: var(--sidebar);
    flex-shrink: 0;
    padding: 20px 12px;
    background: var(--card);
    border-right: 1px solid var(--line);
    position: sticky;
    top: 0;
    height: 100dvh;
  }
  .sidebar .brand { font-family: var(--font-head); font-size: 20px; padding: 4px 12px 16px; }
  .sidebar .nav-link {
    flex-direction: row;
    justify-content: flex-start;
    gap: 12px;
    padding: 0 12px;
    border-radius: var(--radius-sm);
    font-size: 15px;
  }
  .sidebar .nav-link[aria-current="page"] { background: var(--primary-soft); }
  .content { padding: 28px 32px; }
}

/* ---------- ชิ้นส่วน ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--gap);
  box-shadow: var(--shadow);
}
.grid { display: grid; gap: var(--gap); }
.stats { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 900px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.stat .num { font-size: 28px; font-weight: 700; font-family: var(--font-head); }
.stack > * + * { margin-top: var(--gap); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; }
.tight { margin: 0; }
.is-danger { color: var(--danger); }
.is-warn { color: var(--warn); }
.row.end { justify-content: flex-end; }
.content a:not(.btn) { color: var(--primary); text-underline-offset: 3px; }
.content a.muted { color: var(--text-2); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- ฟอร์ม ---------- */
.dialog {
  border: none;
  border-radius: var(--radius);
  padding: 20px;
  width: min(520px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  color: var(--text);
  background: var(--card);
  box-shadow: 0 10px 40px rgba(0, 0, 0, .2);
}
.dialog.wide { width: min(960px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(42, 31, 31, .45); }
.field { display: flex; flex-direction: column; gap: 4px; }
.field-label { font-weight: 600; font-size: 14px; }
.field-hint { color: var(--text-2); font-size: 13px; }
.check { display: flex; align-items: center; gap: 10px; min-height: var(--tap); }
.check input { width: 22px; height: 22px; }
input:not([type="checkbox"]), select, textarea {
  min-height: var(--tap);
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* ต่ำกว่า 16px iOS จะซูมหน้าเอง */
  width: 100%;
}
textarea { min-height: 88px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
input:disabled, select:disabled { background: var(--bg); color: var(--text-2); }
.grid-2 { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 560px) { .grid-2 { grid-template-columns: 1fr 1fr; } }
.form-error:not(:empty) {
  background: var(--danger);
  color: var(--on-danger);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-weight: 600;
}
.form-warn:not(:empty) {
  background: var(--warn-soft);
  color: var(--warn);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
}
.summary { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 10px 14px; }
tr.is-void td { color: var(--text-2); text-decoration: line-through; }
tr.is-void td.no-strike { text-decoration: none; }
button.sm { padding: 0 12px; font-size: 14px; } /* ยังสูง 44px ตามขั้นต่ำ */

/* ห้ามใช้ style="" ในโค้ด เพราะ CSP ไม่อนุญาต inline style ให้ใช้คลาสแทน */

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: var(--fs-min);
  font-weight: 600;
  background: var(--primary-soft);
  color: var(--primary);
}
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--on-danger); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--text-2); font-weight: 600; font-size: 13px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.code { white-space: nowrap; }
/* แถบสีที่แถวล็อตใกล้หมดอายุ/หมดอายุ */
tr.lot-soon td { background: var(--warn-soft); }
tr.lot-soon td:first-child { box-shadow: inset 4px 0 0 var(--warn); }
tr.lot-expired td { background: var(--primary-soft); color: var(--danger); }
tr.lot-expired td:first-child { box-shadow: inset 4px 0 0 var(--danger); }
.nav-link .short { display: inline; }
.nav-link .long { display: none; }
@media (min-width: 900px) {
  .nav-link .short { display: none; }
  .nav-link .long { display: inline; }
}

.notice {
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  background: var(--warn-soft);
  color: var(--warn);
}
.empty { color: var(--text-2); text-align: center; padding: 32px 8px; }

/* ---------- สติกเกอร์ QR ---------- */
.label {
  width: var(--label-w);
  height: var(--label-h);
  display: flex;
  gap: 2mm;
  align-items: center;
  padding: 1.5mm;
  background: #fff;
  color: #000;
  border: 1px dashed #bbb;
  overflow: hidden;
  font-family: var(--font-body);
  font-size: 7pt;
  line-height: 1.25;
}
.label-qr { width: var(--label-qr); height: var(--label-qr); flex-shrink: 0; }
.label-qr svg { width: 100%; height: 100%; display: block; }
.label-text { min-width: 0; }
.label-lot { font-size: 10pt; font-weight: 700; }
.label-name { font-weight: 600; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.label-exp { font-weight: 700; }
.label-preview { display: flex; justify-content: center; padding: 12px; background: var(--bg); border-radius: var(--radius-sm); }
.label-preview .label { transform: scale(1.6); transform-origin: center; margin: 9mm 0; }
#print-root { display: none; }

@media print {
  @page { margin: 5mm; }
  body.printing-labels > *:not(#print-root) { display: none !important; }
  body.printing-labels #print-root { display: flex; flex-wrap: wrap; gap: 2mm; }
  body.printing-labels { background: #fff; }
  #print-root .label { border: none; break-inside: avoid; }
}

/* ---------- หน้าสแกน ---------- */
.scanner {
  position: relative;
  width: 100%;
  max-width: 520px;
  aspect-ratio: 3 / 4;
  background: #111;
  border-radius: var(--radius);
  overflow: hidden;
  margin: 0 auto;
}
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .frame {
  position: absolute;
  inset: 18%;
  border: 3px solid rgba(255, 255, 255, .85);
  border-radius: 16px;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, .35);
  pointer-events: none;
}
.scanner .idle {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: #fff; padding: 24px; text-align: center;
}

.app-version { margin-top: auto; padding: 12px; color: var(--text-2); font-size: 12px; }
.center { text-align: center; }
