:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1a1d23;
  --muted: #667085;
  --border: #e2e5ea;
  --primary: #1f4e79;
  --primary-dark: #163a5c;
  --success: #1a7f4b;
  --danger: #c23b3b;
  --warning: #b5720f;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Sarabun", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  /* mobile-first: driver ใช้มือถือเป็นหลัก ปุ่ม/ฟอนต์ใหญ่ */
  font-size: 18px;
  line-height: 1.5;
}

/* Admin ใช้จอใหญ่ → กระชับขึ้น (สเปก §12, breakpoint 601px) */
body.admin { font-size: 15px; }
@media (min-width: 601px) {
  body.admin { font-size: 14px; }
}

h1, h2, h3 { margin: 0 0 12px; }
p { margin: 0 0 8px; }

.container { max-width: 480px; margin: 0 auto; padding: 20px 16px 80px; }
body.admin .container { max-width: 1200px; padding: 20px 24px 80px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

label { display: block; font-weight: 600; margin: 12px 0 4px; font-size: 0.9em; }
label:first-child { margin-top: 0; }

input, select, textarea {
  width: 100%;
  padding: 12px;
  font-size: 1em;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font-family: inherit;
}
body.admin input, body.admin select, body.admin textarea { padding: 8px 10px; }

textarea { resize: vertical; min-height: 60px; }

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px 20px;
  font-size: 1em;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  font-family: inherit;
}
body.admin button, body.admin .btn { padding: 8px 14px; }

button:disabled { opacity: 0.5; cursor: not-allowed; }
button.secondary { background: #fff; color: var(--primary); border: 1px solid var(--primary); }
button.danger { background: var(--danger); }
button.success { background: var(--success); }
button.ghost { background: transparent; color: var(--muted); }
button.block { width: 100%; }
button.small { padding: 8px 12px; font-size: 0.85em; }

.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > * { flex: 1; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 0.75em;
  font-weight: 700;
  background: #eef1f5;
  color: var(--muted);
}
.badge.status-รอรับงาน { background: #eef1f5; color: var(--muted); }
.badge.status-กำลังรับสินค้า, .badge.status-กำลังส่ง { background: #fff3cd; color: var(--warning); }
.badge.status-รับสินค้าแล้ว { background: #dbeafe; color: var(--primary); }
.badge.status-ส่งแล้ว { background: #dcfce7; color: var(--success); }
.badge.status-จบงานสมบูรณ์ { background: #dcfce7; color: var(--success); }
.badge.status-ยกเลิก { background: #f3f4f6; color: #9ca3af; }
.badge.status-รับสินค้าไม่ได้ { background: #fee2e2; color: var(--danger); }
.badge.locked { background: #f3f4f6; color: #9ca3af; }
.badge.unlocked { background: #dcfce7; color: var(--success); }
.badge.late { background: #fee2e2; color: var(--danger); }
.badge.time { background: #fff7ed; color: #c2410c; border: 1px solid #fed7aa; }
.badge.overdue { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }

/* ชิปกรองสถานะในหน้าติดตามงาน — สีเดียวกับ badge สถานะ ให้กวาดตาหาง่ายว่าสถานะไหนเป็นสีอะไร */
.chip-status-รอรับงาน { --chip: var(--muted); --chip-bg: #eef1f5; }
.chip-status-กำลังรับสินค้า, .chip-status-กำลังส่ง { --chip: var(--warning); --chip-bg: #fff3cd; }
.chip-status-รับสินค้าแล้ว { --chip: var(--primary); --chip-bg: #dbeafe; }
.chip-status-ส่งแล้ว { --chip: var(--success); --chip-bg: #dcfce7; }

[class*="chip-status-"] {
  background: var(--chip-bg); color: var(--chip); border: 1.5px solid var(--chip);
  font-weight: 700;
}
[class*="chip-status-"].active { background: var(--chip); color: #fff; }

.error { color: var(--danger); font-size: 0.9em; margin-top: 8px; }
.hint { color: var(--muted); font-size: 0.85em; }
.center { text-align: center; }

.stack { display: flex; flex-direction: column; gap: 12px; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 100;
}
@media (min-width: 601px) {
  .modal-backdrop { align-items: center; }
}
.modal {
  background: #fff; border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px; width: 100%; max-width: 520px;
  max-height: 96vh; max-height: 96dvh; /* dvh = ไม่โดน mobile address bar บังคับให้แคบกว่าจริง */
  overflow-y: auto; /* กันไว้เผื่อเนื้อหายาวเกินจอจริงๆ (เช่น รายละเอียดงานที่มีรูปเยอะ) */
  overflow-x: hidden; /* กัน scrollbar แนวนอนโผล่มาโดยไม่ได้ตั้งใจ (ตาราง/รูปข้างในมี scroll ของตัวเองแยกอยู่แล้ว) */
}
@media (min-width: 601px) {
  .modal { border-radius: var(--radius); }
}

.photo-grid { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.photo-grid img { width: 72px; height: 72px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }
.photo-thumb { position: relative; }
.photo-thumb .remove {
  position: absolute; top: -6px; right: -6px; background: var(--danger); color: #fff;
  border-radius: 50%; width: 20px; height: 20px; font-size: 12px; line-height: 20px; text-align: center; padding: 0;
}

table { width: 100%; border-collapse: collapse; font-size: 0.95em; }
th, td { padding: 8px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: #f8f9fb; font-weight: 700; }
.table-scroll { overflow-x: auto; overflow-y: hidden; }

/* overflow-x:auto เพียงอย่างเดียวทำให้ overflow-y กลายเป็น auto ไปด้วยตามสเปก CSS
   (ถ้าแกนหนึ่งไม่ใช่ visible อีกแกนที่เป็น visible จะถูกบังคับเป็น auto) เกิด scrollbar แนวตั้ง
   โผล่มาโดยไม่ได้ตั้งใจ (เห็นเป็นลูกศร ▲▼ ของ Windows) ต้องล็อก overflow-y:hidden กันไว้เสมอ */
nav.tabs { display: flex; gap: 4px; overflow-x: auto; overflow-y: hidden; margin-bottom: 16px; border-bottom: 2px solid var(--border); }
nav.tabs a {
  padding: 10px 14px; text-decoration: none; color: var(--muted); font-weight: 600;
  white-space: nowrap; border-bottom: 2px solid transparent; margin-bottom: -2px;
}
nav.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); }

.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; background: var(--primary); color: #fff;
}
.topbar .who { font-size: 0.85em; opacity: 0.9; }
.topbar button { background: rgba(255,255,255,0.15); padding: 6px 12px; font-size: 0.85em; }

.role-pick { display: flex; gap: 16px; margin: 24px 0; }
.role-pick button { flex: 1; padding: 24px; font-size: 1.1em; flex-direction: column; }

.stop-item { border: 1px solid var(--border); border-radius: 8px; padding: 10px; margin-bottom: 8px; }
.stop-item.done { background: #f0fdf4; border-color: var(--success); }

.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff; border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 10px 18px; border-radius: 8px;
  font-size: 0.9em; z-index: 200; max-width: 90%;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

/* หน้ารายงาน — การ์ดสรุป 4 ช่อง (เที่ยวทั้งหมด/จบสมบูรณ์/รับสินค้าไม่ได้/รอส่งเอกสาร) */
.summary-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.summary-cards .summary-stat { text-align: center; }
.summary-cards .summary-stat h2 { margin: 4px 0 0; }
@media (max-width: 480px) {
  .summary-cards { grid-template-columns: repeat(2, 1fr); }
}

/* ซ่อนบนจอปกติ โผล่เฉพาะตอนพิมพ์ (หัวกระดาษรายงาน) */
.print-only { display: none; }

/* สเปก §10, §13.5 — พิมพ์รายงานต้องแนวนอน + table-layout:fixed เท่านั้น
   ห้าม auto+nowrap เด็ดขาด เพราะจะทำให้คอลัมน์ท้ายๆ ถูกตัดหายไปเลยตอนพิมพ์ */
@media print {
  @page { size: landscape; margin: 12mm; }

  body { font-size: 12px; background: #fff; color: #111; }
  .topbar, nav.tabs, #tabs, #dupBanner, .no-print { display: none !important; }
  .container { max-width: 100%; padding: 0; }

  .print-only { display: block; margin-bottom: 16px; border-bottom: 3px solid #1d4ed8; padding-bottom: 10px; }
  .print-only h1 { font-size: 20px; margin: 0; color: #1d4ed8; }
  .print-only .print-subtitle { font-size: 13px; color: #444; margin-top: 2px; }
  .print-only .print-meta { display: flex; flex-wrap: wrap; gap: 4px 18px; margin-top: 8px; font-size: 11px; }
  .print-only .print-meta-sub { font-size: 10px; color: #666; margin-top: 4px; }

  /* การ์ดตอนพิมพ์ให้ดูเหมือนส่วนของเอกสารรายงาน ไม่ใช่การ์ด UI ลอยๆ */
  .card { border: none; border-radius: 0; padding: 0; margin-bottom: 14px; break-inside: avoid; }
  .summary-cards { border: 1px solid #ccc; padding: 10px 0; }
  .summary-cards .summary-stat { border-right: 1px solid #ddd; }
  .summary-cards .summary-stat:last-child { border-right: none; }
  .summary-cards .hint { font-size: 10px; }
  .summary-cards h2 { font-size: 20px; }

  h3 { font-size: 13px; border-bottom: 1.5px solid #1d4ed8; color: #1d4ed8; padding-bottom: 3px; margin: 0 0 8px; }

  /* กราฟรวมอยู่หน้าแรกกับการ์ดสรุป ไม่ขึ้นหน้าใหม่แยก — ย่อขนาดกันแผนภูมิล้นกรอบการ์ด */
  #chartsArea { display: flex; gap: 10px; }
  #chartsArea .card { border: 1px solid #ccc; padding: 8px; flex: 1; min-width: 0; overflow: hidden; }
  #chartsArea canvas { max-height: 150px !important; width: 100% !important; }

  table { border: 1px solid #ccc; }
  th, td { border: 1px solid #ddd; }
  th { background: #f0f2f5 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  tr { break-inside: avoid; }

  #resultsCard { page-break-before: always; break-before: page; }
  #resultsTable { table-layout: fixed; width: 100%; white-space: normal; word-wrap: break-word; }
  #resultsTable th, #resultsTable td { font-size: 9px; padding: 4px 5px; }
  /* กำหนด % ความกว้างตายตัวรวม 100% พอดี (สเปก §10) — ลำดับต้องตรงกับคอลัมน์จริงในตาราง
     เลขงาน วันที่ ลูกค้า คนขับ ต้นทาง ปลายทาง สถานะ เอกสาร ระยะทาง ค่าใช้จ่าย ผู้เปิดงาน */
  #resultsTable th:nth-child(1), #resultsTable td:nth-child(1) { width: 11%; }
  #resultsTable th:nth-child(2), #resultsTable td:nth-child(2) { width: 8%; }
  #resultsTable th:nth-child(3), #resultsTable td:nth-child(3) { width: 13%; }
  #resultsTable th:nth-child(4), #resultsTable td:nth-child(4) { width: 11%; }
  #resultsTable th:nth-child(5), #resultsTable td:nth-child(5) { width: 13%; }
  #resultsTable th:nth-child(6), #resultsTable td:nth-child(6) { width: 13%; }
  #resultsTable th:nth-child(7), #resultsTable td:nth-child(7) { width: 10%; }
  #resultsTable th:nth-child(8), #resultsTable td:nth-child(8) { width: 6%; }
  #resultsTable th:nth-child(9), #resultsTable td:nth-child(9) { width: 7%; }
  #resultsTable th:nth-child(10), #resultsTable td:nth-child(10) { width: 8%; }
  #resultsTable th:nth-child(11), #resultsTable td:nth-child(11) { width: 10%; }
}
