/* ===== ระบบสต๊อคชิ้นงานตัวอย่าง EPS — Stylesheet ===== */
:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #eff6ff;
  --ok: #16a34a;
  --ok-soft: #f0fdf4;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .07), 0 6px 18px rgba(15, 23, 42, .05);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", "Noto Sans Thai", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { margin: 0; }

/* ---------- แถบหัวเรื่อง ---------- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  background: rgba(255, 255, 255, .96);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.logo {
  width: 42px; height: 42px; flex: 0 0 42px;
  border-radius: 13px;
  background: linear-gradient(135deg, #2563eb, #06b6d4);
  color: #fff; font-weight: 800; font-size: 13px;
  display: grid; place-items: center;
  letter-spacing: .5px;
}

.logo-img {
  width: 42px; height: 42px; flex: 0 0 42px;
  border-radius: 13px;
  object-fit: contain;
  background: #fff;
  border: 1px solid var(--border);
}

.brand-text h1 { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text p { margin: 0; font-size: 11.5px; color: var(--muted); }

.appbar-actions { margin-left: auto; }

/* ---------- เมนู tabs ---------- */
.tabbar { display: flex; gap: 6px; background: var(--bg); padding: 5px; border-radius: 999px; }

.tab {
  display: flex; align-items: center; gap: 7px;
  border: 0; background: transparent; cursor: pointer;
  font: inherit; font-size: 14px; color: var(--muted);
  padding: 8px 16px; border-radius: 999px;
  transition: background .15s, color .15s;
}
.tab-ico { font-size: 16px; line-height: 1; }
.tab:hover { color: var(--text); }
.tab.active { background: var(--card); color: var(--primary); font-weight: 700; box-shadow: 0 1px 4px rgba(15,23,42,.1); }

/* ---------- เนื้อหา ---------- */
main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 18px 16px 40px;
}

.panel { display: none; }
.panel.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

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

.card h2 { font-size: 16.5px; margin-bottom: 14px; }

.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 12px;
}
.card-head h2 { margin-bottom: 0; }
.card-head.wrap { flex-wrap: wrap; }
.head-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------- ฟอร์ม ---------- */
label { display: block; font-size: 13.5px; font-weight: 600; color: #334155; margin-bottom: 12px; }

input, select, button { font-family: inherit; font-size: 15px; }

input[type="text"], input:not([type]), input[type="number"], select {
  width: 100%;
  margin-top: 5px;
  padding: 11px 12px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, .13); }
input::placeholder { color: #94a3b8; }

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='3' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 34px;
}

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }

.form-actions { display: flex; gap: 10px; margin-top: 4px; }

.input-search {
  width: auto; min-width: 190px; margin: 0 !important;
  padding: 8px 12px !important; font-size: 14px !important;
  border: 1.5px solid var(--border) !important; border-radius: 999px !important;
}

/* ---------- ปุ่ม ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 10px; cursor: pointer;
  font-weight: 700; font-size: 15px;
  padding: 11px 20px; min-height: 44px;
  transition: filter .15s, background .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: #fff; color: #334155; border: 1.5px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger-soft); color: var(--danger); border: 1.5px solid #fecaca; }
.btn-danger:hover { background: #fee2e2; }
.btn-sm { min-height: 36px; padding: 7px 13px; font-size: 13.5px; font-weight: 600; }
.btn-block { width: 100%; }
.btn-icon {
  min-height: 34px; padding: 5px 11px; font-size: 13px; font-weight: 600;
  border-radius: 9px;
}

/* ---------- กล่องข้อมูลสรุปในฟอร์ม ---------- */
.info-box {
  background: var(--primary-soft);
  border: 1px solid #dbeafe;
  color: #1e40af;
  border-radius: 10px;
  padding: 11px 13px;
  font-size: 14px;
  margin-bottom: 14px;
}
.info-box b { font-size: 16px; }
.info-box .sep { color: #93c5fd; margin: 0 8px; }

/* ---------- ปุ่มเลือกประเภท (ส่งออก/ทำลาย) ---------- */
.segmented {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  background: var(--bg); padding: 5px; border-radius: 12px; margin-bottom: 14px;
}
.segmented button {
  border: 0; background: transparent; cursor: pointer;
  padding: 10px; border-radius: 9px; font-weight: 700; color: var(--muted);
  transition: background .15s, color .15s;
}
.segmented button.active { background: #fff; color: var(--primary); box-shadow: 0 1px 4px rgba(15,23,42,.12); }
.segmented button[data-type="DESTROY"].active { color: var(--danger); }

/* ---------- รายการสินค้า / ประวัติ ---------- */
.list { display: flex; flex-direction: column; gap: 9px; }

.list-item {
  display: flex; align-items: center; gap: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 13px;
  background: #fff;
  flex-wrap: wrap;
}
.list-item:hover { border-color: #cbd5e1; background: #fafcff; }

.list-item-thumb {
  width: 56px; height: 56px; flex: 0 0 56px;
  border-radius: 10px; object-fit: cover;
  border: 1px solid var(--border); background: #f8fafc;
}
.list-item-thumb.empty {
  display: grid; place-items: center;
  font-size: 22px; color: #cbd5e1;
}
.list-item-thumb.sm {
  width: 48px; height: 48px; flex: 0 0 48px; border-radius: 9px;
}

.li-main { flex: 1; min-width: 170px; }
.li-title { font-weight: 700; font-size: 15px; }
.li-sub { font-size: 13px; color: var(--muted); }
.li-qty { font-weight: 800; font-size: 15px; white-space: nowrap; }
.li-actions { display: flex; gap: 7px; }

/* ---------- รูปตัวอย่าง ---------- */
.img-preview {
  margin-top: 10px; display: flex; align-items: center; gap: 10px;
}
.img-preview img {
  width: 90px; height: 90px; object-fit: cover;
  border-radius: 12px; border: 1px solid var(--border);
}
.img-preview .img-remove {
  border: 1px solid var(--border); background: #fff;
  padding: 7px 10px; border-radius: 9px; cursor: pointer;
  font-size: 13px;
}

.badge {
  display: inline-block; font-size: 12px; font-weight: 700;
  padding: 2.5px 9px; border-radius: 999px; white-space: nowrap;
}
.badge-code { background: var(--primary-soft); color: var(--primary-dark); font-family: ui-monospace, Consolas, monospace; }
.badge-in { background: var(--ok-soft); color: var(--ok); }
.badge-out { background: var(--primary-soft); color: var(--primary-dark); }
.badge-destroy { background: var(--danger-soft); color: var(--danger); }
.badge-zero { background: #f1f5f9; color: var(--muted); }
.badge-low { background: var(--warn-soft); color: var(--warn); }

.empty {
  text-align: center; color: var(--muted); font-size: 14px;
  padding: 24px 10px; border: 1.5px dashed var(--border); border-radius: 12px;
}

/* ---------- การ์ดสถิติ ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.stat {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 15px;
}
.stat .stat-label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.stat .stat-value { font-size: 24px; font-weight: 800; margin-top: 3px; }
.stat .stat-value small { font-size: 13px; font-weight: 600; color: var(--muted); }
.stat.blue .stat-value { color: var(--primary); }
.stat.green .stat-value { color: var(--ok); }
.stat.red .stat-value { color: var(--danger); }

/* ---------- ตารางรายงาน ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px; }

table { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 860px; }

th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }

thead th {
  background: #f8fafc; font-size: 12.5px; color: #475569;
  text-transform: none; position: sticky; top: 0;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #f8fbff; }
th.num, td.num { text-align: right; }
td.num { font-variant-numeric: tabular-nums; font-weight: 600; }
td.sticky-name, th.sticky-name { white-space: normal; min-width: 180px; }
.cell-balance { font-weight: 800; font-size: 15px; }
.cell-balance.zero { color: var(--muted); }
.cell-date { font-size: 13px; color: #475569; }
.cell-none { color: #cbd5e1; }

.hint { color: var(--muted); font-size: 14px; margin: 12px 2px 0; }

/* ---------- ข้อความแจ้งเตือน (Toast) ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: -80px;
  transform: translateX(-50%);
  z-index: 100;
  background: #0f172a; color: #fff;
  padding: 12px 20px; border-radius: 999px;
  font-size: 14.5px; font-weight: 600;
  box-shadow: 0 10px 30px rgba(15, 23, 42, .3);
  transition: bottom .25s ease, background .2s;
  max-width: min(92vw, 560px);
  text-align: center;
}
.toast.show { bottom: 92px; }
.toast.ok { background: #14532d; }
.toast.err { background: #7f1d1d; }

/* ================= ขนาดมือถือ ================= */
@media (max-width: 767px) {
  body { font-size: 14.5px; padding-bottom: 0; }

  .appbar { padding: 9px 12px; gap: 8px; }
  .logo { width: 36px; height: 36px; flex-basis: 36px; border-radius: 11px; font-size: 12px; }
  .brand-text h1 { font-size: 14.5px; }
  .brand-text p { display: none; }
  .appbar-actions .btn { min-height: 36px; padding: 6px 11px; font-size: 12.5px; }

  /* เมนูย้ายไปอยู่ด้านล่างจอ */
  .tabbar {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 40;
    background: rgba(255, 255, 255, .97);
    backdrop-filter: blur(8px);
    border-top: 1px solid var(--border);
    border-radius: 0;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    justify-content: space-around;
    gap: 0;
    box-shadow: 0 -4px 16px rgba(15, 23, 42, .07);
  }
  .tab {
    flex-direction: column; gap: 2px;
    font-size: 11px; font-weight: 600;
    padding: 5px 8px; border-radius: 12px;
    flex: 1; max-width: 92px;
  }
  .tab-ico { font-size: 19px; }
  .tab.active { background: var(--primary-soft); box-shadow: none; color: var(--primary); }

  main { padding: 13px 11px calc(88px + env(safe-area-inset-bottom)); }

  .card { padding: 14px; border-radius: 13px; }
  .card h2 { font-size: 15.5px; }
  .grid2 { grid-template-columns: 1fr; gap: 0; }
  .form-actions .btn { flex: 1; }

  .stats { grid-template-columns: repeat(2, 1fr); gap: 9px; }
  .stat { padding: 11px 12px; }
  .stat .stat-value { font-size: 20px; }

  .card-head { flex-wrap: wrap; }
  .input-search { width: 100%; min-width: 0; }
  .head-actions { width: 100%; }
  .head-actions .btn { flex: 1; }

  .toast.show { bottom: calc(78px + env(safe-area-inset-bottom)); }
  .list-item { gap: 8px; }
  .li-qty { font-size: 14.5px; }
}

@media (max-width: 400px) {
  .tab span:last-child { font-size: 10.5px; }
  .appbar-actions .btn span { display: none; }
}

/* ================= การพิมพ์รายงาน ================= */
@media print {
  .appbar, .tabbar, .head-actions, .toast, #btnBackup { display: none !important; }
  body { background: #fff; }
  main { max-width: none; padding: 0; }
  .panel { display: none !important; }
  #tab-report { display: block !important; }
  .stats { display: grid !important; }
  .card { box-shadow: none; border: 0; padding: 0; margin-bottom: 10px; }
  .table-wrap { border: 1px solid #999; overflow: visible; }
  table { min-width: 0; font-size: 11px; }
  th, td { padding: 5px 6px; border: 1px solid #ccc; white-space: normal; }
  thead th { background: #eee !important; -webkit-print-color-adjust: exact; }
}
