/* ============================================================
   ระบบใบสั่งงานผลิตเสื้อ - สไตล์หน้าจอ + การพิมพ์ A4
   ============================================================ */

:root {
  --ink: #1a1a1a;
  --line: #222;
  --muted: #6b7280;
  --accent: #1d4ed8;
  --accent-dark: #1e40af;
  --bg: #eef1f6;
  --card: #ffffff;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  font-family: "Sarabun", "Noto Sans Thai", "TH SarabunPSK", Tahoma, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

/* ---------- Toolbar (หน้าจอเท่านั้น) ---------- */
.toolbar {
  background: linear-gradient(135deg, #0f172a, #1e3a8a);
  color: #fff;
  padding: 14px 20px;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 4px 18px rgba(0,0,0,.25);
}
.toolbar-inner {
  max-width: 980px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-icon { font-size: 30px; }
.brand h1 { font-size: 20px; font-weight: 700; line-height: 1.2; }
.brand p { font-size: 13px; opacity: .85; margin-top: 2px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  color: #fff;
  transition: transform .12s ease, box-shadow .12s ease, opacity .12s ease;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 4px 14px rgba(0,0,0,.3); }
.btn:active { transform: translateY(0); }
.btn-pdf { background: #dc2626; }
.btn-print { background: var(--accent); }
.btn-reset { background: #475569; }

/* ---------- กระดาษ A4 (หน้าจอ) ---------- */
#sheet-wrap {
  padding: 24px 12px;
  overflow-x: auto;
}
.sheet {
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto;
  background: var(--card);
  padding: 12mm 12mm 14mm;
  box-shadow: 0 10px 40px rgba(0,0,0,.18);
  border-radius: 4px;
  font-size: 11.5px;
  line-height: 1.35;
}

/* ---------- ส่วนหัวเอกสาร ---------- */
.header-zone { border-bottom: 2px solid var(--line); padding-bottom: 4mm; }
.doc-title {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 2px;
  margin-bottom: 3mm;
  display: inline-block;
  border: 2px solid var(--line);
  padding: 2mm 5mm;
  text-align: center;
  width: 100%;
}
.hdr-table { width: 100%; border-collapse: collapse; }
.hdr-table td { padding: 1.4mm 1mm; vertical-align: middle; }
.hdr-table .lbl { width: 42mm; font-weight: 700; }
.hdr-table .spacer { width: 20mm; }
.hdr-table input {
  width: 100%;
  padding: 1mm 2mm;
  font-size: 11.5px;
  font-family: inherit;
  border: 1px solid #94a3b8;
  border-radius: 5px;
}

/* ---------- ส่วนคุณลักษณะสินค้า ---------- */
.sec-title {
  font-weight: 800;
  font-size: 13px;
  margin: 5mm 0 2mm;
  padding: 1.4mm 3mm;
  background: #e2e8f0;
  border-left: 5px solid var(--line);
}
.spec-table { width: 100%; border-collapse: collapse; }
.spec-table .k {
  width: 34mm;
  font-weight: 700;
  background: #f1f5f9;
}
.spec-table td {
  border: 0.4mm solid var(--line);
  padding: 1.6mm 2.5mm;
  vertical-align: top;
}

/* ตัวเลือก (radio/checkbox) */
.opts { display: flex; flex-wrap: wrap; gap: 1.2mm 4mm; align-items: center; }
.opts.cols1 { gap: 0; }
.opt {
  display: inline-flex;
  align-items: center;
  gap: 1.2mm;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  padding: 0.6mm 1mm;
  border-radius: 4px;
  transition: background .12s ease;
}
.opt:hover { background: #e8eefc; }
.opt input {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  width: 4mm;
  height: 4mm;
  min-width: 4mm;
  min-height: 4mm;
  border: 0.5mm solid #111;
  border-radius: 1mm;
  background: #fff;
  position: relative;
  cursor: pointer;
  vertical-align: middle;
}
.opt input:checked::after {
  content: "";
  position: absolute;
  inset: 0.3mm;
  background: url('data:image/svg+xml;utf8,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="%23000" stroke-width="4.5" stroke-linecap="round" stroke-linejoin="round"%3E%3Cpolyline points="20 6 9 17 4 12"/%3E%3C/svg%3E') center/contain no-repeat;
}
.opt.fixed { cursor: default; opacity: 1; }
.opt.fixed:hover { background: transparent; }

/* ---------- ตาราง Size ---------- */
.size-table {
  width: 100%;
  border-collapse: collapse;
  text-align: center;
  table-layout: fixed;
}
.size-table th, .size-table td {
  border: 0.4mm solid var(--line);
  padding: 0;
  vertical-align: middle;
}
.size-table .sc {
  background: #fff;
}
.size-table .sc .sname {
  display: block;
  font-size: 10px;
  font-weight: 700;
  background: #e2e8f0;
  border-bottom: 0.4mm solid var(--line);
  padding: 1.2mm 0;
  white-space: nowrap;
}
.size-table .size-input {
  width: 100%;
  height: 8mm;
  border: none;
  outline: none;
  text-align: center;
  font-size: 14px;
  font-family: inherit;
  background: transparent;
  padding: 0;
  -moz-appearance: textfield;
}
.size-input::-webkit-outer-spin-button,
.size-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.size-table .size-input:focus { background: #fff7ed; box-shadow: inset 0 0 0 2px var(--accent); }
.size-table .sc-total { background: #fef3c7; }
.size-table .sc-total .sname { background: #fde68a; }
.size-table .size-total {
  display: block;
  font-size: 16px;
  font-weight: 800;
  padding: 2.2mm 0;
}
.size-total-all {
  display: flex;
  justify-content: flex-end;
  align-items: stretch;
  border: 0.6mm solid #111;
  margin-top: 1mm;
  font-size: 14px;
  font-weight: 800;
  background: #fffbeb;
}
.size-total-all span {
  flex: 1;
  text-align: right;
  padding: 2mm 3mm;
  background: #f1f5f9;
}
.size-total-all strong {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 30mm;
  padding: 2mm;
  background: #fbbf24;
  font-size: 17px;
}

/* ---------- Responsive หน้าจอมือถือ ---------- */
@media (max-width: 640px) {
  .toolbar-inner { flex-direction: column; align-items: stretch; text-align: center; }
  .actions { justify-content: center; }
  .actions .btn { flex: 1; }
  .sheet { min-width: 190mm; border-radius: 0; }
  #sheet-wrap { padding: 8px 0; }
  .opts { gap: 1.2mm 2.5mm; }
  .opt { white-space: normal; }
  .opt input { width: 5mm; height: 5mm; min-width: 5mm; min-height: 5mm; }
}

/* ---------- ปุ่มส่ง ---------- */
.btn-submit {
  background: linear-gradient(135deg, #16a34a, #15803d);
  font-size: 15px;
  padding: 10px 20px;
}
.btn-back { background: #475569; text-decoration: none; display: inline-block; text-align: center; }
.btn-copy { background: #0f766e; }
.btn-delete { background: #dc2626; }

/* ---------- หน้าเข้าสู่ระบบ ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-box {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
  padding: 36px 40px;
  text-align: center;
  width: 100%;
  max-width: 400px;
}
.login-icon { font-size: 44px; margin-bottom: 10px; }
.login-box h1 { font-size: 20px; margin-bottom: 6px; }
.login-hint { font-size: 13px; color: var(--muted); margin-bottom: 14px; }
.login-box input[type="password"] {
  width: 100%;
  padding: 12px 14px;
  font-size: 15px;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  margin-bottom: 12px;
  font-family: inherit;
}
.login-box .btn { width: 100%; }
.login-error {
  background: #fee2e2;
  color: #b91c1c;
  padding: 8px;
  border-radius: 8px;
  margin-bottom: 12px;
  font-size: 14px;
}

/* ---------- หลังบ้าน ---------- */
.toolbar-admin .toolbar-inner { max-width: 1200px; }
.admin-wrap { max-width: 1200px; margin: 0 auto; padding: 24px 16px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 20px; }
.stat {
  background: #fff;
  border-radius: 12px;
  padding: 18px;
  text-align: center;
  box-shadow: 0 4px 14px rgba(0,0,0,.08);
  font-size: 14px;
  color: #334155;
}
.stat b { display: block; font-size: 30px; margin-bottom: 2px; }
.stat-pending b { color: #dc2626; }
.stat-filled b { color: #16a34a; }
.stat-all b { color: #1d4ed8; }

.card {
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 4px 14px rgba(0,0,0,.08);
  padding: 20px;
  margin-bottom: 20px;
}
.card h2 { font-size: 17px; margin-bottom: 14px; color: #0f172a; }
.create-form { display: flex; gap: 10px; flex-wrap: wrap; }
.create-form input[type="text"] {
  flex: 1;
  min-width: 240px;
  padding: 12px 14px;
  font-size: 15px;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  font-family: inherit;
}
.alert { padding: 12px 14px; border-radius: 10px; margin-top: 12px; font-size: 14px; }
.alert-success { background: #dcfce7; color: #166534; }
.alert-error { background: #fee2e2; color: #b91c1c; }
.empty { color: var(--muted); text-align: center; padding: 24px; }

.table-wrap { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin-table th, .admin-table td {
  border-bottom: 1px solid #e2e8f0;
  padding: 10px 10px;
  text-align: left;
  vertical-align: middle;
}
.admin-table thead th { background: #f8fafc; color: #475569; font-size: 13px; white-space: nowrap; }
.admin-table tbody tr:hover { background: #f8fafc; }
.mono { font-family: Consolas, monospace; font-size: 13px; }
.job-name { font-weight: 700; }
.url-cell { min-width: 300px; }
.url-cell input {
  width: 100%;
  padding: 8px 10px;
  font-size: 13px;
  font-family: Consolas, monospace;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #f8fafc;
}
.url-cell .btn-copy { margin-top: 6px; }
.actions-cell { white-space: nowrap; }
.actions-cell .btn, .actions-cell select {
  margin: 2px 2px;
  padding: 8px 10px;
  font-size: 13px;
  border-radius: 8px;
  font-family: inherit;
  border: 1px solid #cbd5e1;
}
.inline-form { display: inline-block; }
.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.st-pending { background: #fee2e2; color: #b91c1c; }
.st-filled { background: #dcfce7; color: #166534; }
.st-done { background: #e0e7ff; color: #3730a3; }

/* ---------- แสดงค่าในใบงาน (หน้าดู/พิมพ์หลังบ้าน) ---------- */
.hdr-table .value-text { font-size: 13px; font-weight: 700; padding: 1mm 2mm; display: block; }
.size-table .size-value {
  display: block;
  font-size: 15px;
  font-weight: 700;
  padding: 2mm 0;
}
/* ไฮไลต์ตัวเลือกที่ลูกค้าเลือก (หน้าดู/พิมพ์) */
.opt.chosen {
  background: #fef08a;
  border: 0.4mm solid var(--line);
  padding: 0.6mm 1mm;
  border-radius: 4px;
  font-weight: 700;
}
.opt.chosen input:checked::after { background-color: #ca8a04; border-radius: 1mm; }

/* ---------- ส่วนส่งงาน/ตรวจสอบ (มุมมอง admin) ---------- */
.workflow-zone { margin-top: 6mm; }
.warning {
  font-weight: 800;
  font-size: 12px;
  text-align: center;
  color: #b91c1c;
  border: 0.6mm solid #b91c1c;
  background: #fef2f2;
  padding: 2mm;
  margin-bottom: 3mm;
  border-radius: 4px;
}
.wf-cols { display: flex; gap: 0; border: 0.4mm solid var(--line); }
.wf-col { flex: 1; width: 50%; }
.wf-col + .wf-col { border-left: 0.4mm solid var(--line); }
.wf-table { width: 100%; border-collapse: collapse; }
.wf-table td { border: 0.4mm solid var(--line); padding: 1.6mm 2.5mm; vertical-align: middle; }
.wf-col:first-child .wf-table tr:first-child td,
.wf-col:last-child .wf-table tr:first-child td { border-top: none; }
.wf-table .k { width: 34mm; font-weight: 700; background: #f1f5f9; }
.wf-table .wf-blank { display: block; height: 5mm; border-bottom: 0.4mm solid #94a3b8; }
.wf-table .v.sign .wf-blank { border-bottom: 0.4mm solid #94a3b8; }
.wf-table .opt { margin-right: 2mm; }
.wf-table .opt span { white-space: nowrap; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #0f172a;
  color: #fff;
  padding: 12px 22px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: all .25s ease;
  z-index: 200;
  box-shadow: 0 10px 30px rgba(0,0,0,.3);
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: #dc2626; }
.toast.success { background: #16a34a; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 150;
  padding: 20px;
  backdrop-filter: blur(3px);
}
.modal-overlay.hidden { display: none; }
.modal-box {
  background: #fff;
  border-radius: 18px;
  padding: 30px 34px;
  text-align: center;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 30px 80px rgba(0,0,0,.35);
  animation: pop .25s ease;
}
@keyframes pop {
  from { transform: scale(.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
.modal-icon { font-size: 52px; margin-bottom: 8px; }
.modal-box h2 { font-size: 22px; margin-bottom: 8px; color: #0f172a; }
.modal-box p { font-size: 15px; color: #475569; line-height: 1.7; }
.modal-actions { display: flex; gap: 8px; margin-top: 20px; justify-content: center; flex-wrap: wrap; }
.modal-actions .btn { flex: 1; min-width: 120px; }

/* ============================================================
   การพิมพ์ : A4 หน้าเดียว จัดตามเอกสารต้นฉบับ
   ============================================================ */
@media print {
  @page {
    size: A4 portrait;
    margin: 7mm 9mm;
  }
  html, body {
    background: #fff;
    margin: 0;
    padding: 0;
  }
  .toolbar { display: none; }
  #sheet-wrap { padding: 0; overflow: visible; }
  .sheet {
    width: 100%;
    min-height: 0;
    margin: 0;
    padding: 0;
    box-shadow: none;
    border-radius: 0;
    font-size: 9.5pt;
  }

  /* ปรับหัวเอกสารให้ชิด A4 */
  .doc-title { font-size: 15pt; padding: 1.2mm 3mm; margin-bottom: 2.5mm; width: 100%; }
  .hdr-table td { padding: 1mm 0.8mm; }
  .hdr-table .lbl { width: 40mm; }
  .hdr-table input { font-size: 9.5pt; border-width: 0.3mm; padding: 0.5mm 1mm; border-radius: 0; }
  .hdr-table input[type="date"] { border: 1px solid #94a3b8; }

  /* คุณลักษณะสินค้า */
  .sec-title { font-size: 10.5pt; margin: 3.5mm 0 1.5mm; padding: 1mm 2mm; }
  .spec-table .k { width: 33mm; }
  .spec-table td { padding: 1.1mm 1.8mm; border-width: 0.3mm; }

  /* จัดตำแหน่งตัวเลือกเป็นแถวตายตัว */
  .opts.cols5 { display: grid; grid-template-columns: repeat(5, 1fr); gap: 0.8mm 1mm; }
  .opts.cols3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.8mm 1mm; }
  .opts.cols2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.8mm 1mm; }
  .opts.cols1 { display: block; }
  .opt { padding: 0; gap: 1mm; font-size: 9.5pt; }
  .opt input { width: 3.4mm; height: 3.4mm; min-width: 3.4mm; min-height: 3.4mm; border-width: 0.35mm; border-radius: 0.5mm; }

  /* ตาราง Size */
  .size-table { margin-top: 0; }
  .size-table th, .size-table td { border-width: 0.3mm; }
  .size-table .sname { font-size: 7.5pt; padding: 0.8mm 0; border-bottom-width: 0.3mm; }
  .size-table .size-input { height: 6mm; font-size: 10pt; }
  .hdr-table .value-text { font-size: 10pt; padding: 0.5mm 1mm; }
  .size-table .size-value { font-size: 12pt; padding: 1.2mm 0; }
  .size-table .size-total { font-size: 11pt; padding: 1.2mm 0; }
  .size-total-all { font-size: 11pt; margin-top: 0; border-width: 0.4mm; }
  .size-total-all span { padding: 1.4mm 2mm; }
  .size-total-all strong { font-size: 13pt; padding: 1.4mm; min-width: 26mm; }

  /* ส่วนส่งงาน/ตรวจสอบ */
  .workflow-zone { margin-top: 4mm; }
  .warning { font-size: 10pt; padding: 1.4mm; margin-bottom: 2mm; border-width: 0.4mm; }
  .wf-cols { border-width: 0.3mm; }
  .wf-col + .wf-col { border-left-width: 0.3mm; }
  .wf-table td { padding: 1.2mm 1.8mm; border-width: 0.3mm; }
  .wf-table .k { width: 31mm; }
  .wf-table .wf-blank { height: 3.5mm; border-bottom-width: 0.3mm; }
  .wf-table .opt { margin-right: 2mm; font-size: 8.5pt; }
  .wf-table input[type="checkbox"] { width: 3.2mm; height: 3.2mm; }
}