:root {
  --bg: #f3f4f7;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #16181d;
  --text-muted: #6b7280;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #eef2ff;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --success: #16a34a;
  --success-bg: #ecfdf5;
  --warn-bg: #fffbeb;
  --warn-text: #b45309;
  --purple: #7c3aed;
  --purple-bg: #f5f3ff;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-md: 0 4px 16px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.12);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: 'Prompt', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.app { max-width: 1140px; margin: 0 auto; padding: 0 20px 60px; }

/* ---------- Topbar ---------- */
.topbar {
  padding: 22px 0 0;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
  flex-wrap: wrap;
  gap: 10px;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-logo {
  font-size: 22px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--primary-light);
  border-radius: 10px;
}
.topbar h1 { font-size: 19px; font-weight: 700; margin: 0; line-height: 1.2; }
.brand-subtitle { font-size: 11.5px; color: var(--text-muted); }

.user-bar { display: flex; align-items: center; gap: 10px; }
.user-chip {
  font-size: 13px;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 100px;
}

.tabs {
  display: flex;
  gap: 6px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.tab-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  padding: 10px 16px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  white-space: nowrap;
  border-radius: 8px 8px 0 0;
  transition: color 0.15s, background 0.15s;
}

.tab-btn:hover { background: rgba(0, 0, 0, 0.03); color: var(--text); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }

.tab-content { display: none; padding-top: 22px; }
.tab-content.active { display: block; animation: fade-in 0.15s ease; }

@keyframes fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Cards ---------- */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.15s, transform 0.15s;
}
.card:hover { box-shadow: var(--shadow-md); transform: translateY(-1px); }

.card-icon {
  width: 46px;
  height: 46px;
  min-width: 46px;
  border-radius: 12px;
  background: var(--primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}
.card-accent-green .card-icon { background: var(--success-bg); }
.card-alert .card-icon { background: var(--warn-bg); }
.card-accent-purple .card-icon { background: var(--purple-bg); }

.card-label { font-size: 12.5px; color: var(--text-muted); margin-bottom: 4px; }
.card-value { font-size: 24px; font-weight: 700; line-height: 1.2; }
.card-alert .card-value { color: var(--warn-text); }

/* ---------- Panels & tables ---------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 18px;
  box-shadow: var(--shadow-sm);
}

.panel h2 {
  font-size: 15px;
  margin: 0 0 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}

.table-wrap { overflow-x: auto; position: relative; }

table { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 480px; }
th, td { text-align: left; padding: 11px 10px; border-bottom: 1px solid var(--border); }
th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
tbody tr { transition: background 0.1s; }
tbody tr:hover { background: #f8f9fb; }
tbody tr:last-child td { border-bottom: none; }
.empty-row td { text-align: center; color: var(--text-muted); padding: 32px; }

/* ---------- Toolbar / search ---------- */
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.toolbar-left,
.toolbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.date-filter {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
}
.date-filter input {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-family: inherit;
  color: var(--text);
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface);
}
.segmented button {
  padding: 9px 14px;
  border: none;
  border-right: 1px solid var(--border);
  background: transparent;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.segmented button:last-child { border-right: none; }
.segmented button:hover { background: rgba(0, 0, 0, 0.04); color: var(--text); }
.segmented button.active { background: var(--primary); color: #fff; }

#category-filter {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
}

.modal .inline-form {
  display: flex;
  flex-direction: row;
  gap: 8px;
  margin-bottom: 16px;
}
.inline-form input {
  flex: 1;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 340px;
  min-width: 200px;
}
.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 13px;
  opacity: 0.6;
}
#search-input {
  width: 100%;
  padding: 10px 12px 10px 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}
#search-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

/* ---------- Buttons ---------- */
.btn {
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: #f3f4f6; }
.btn:active { transform: scale(0.98); }

.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }

.btn-ghost { background: transparent; }
.btn-ghost:hover { background: rgba(0, 0, 0, 0.04); }

.btn-block { width: 100%; }

.btn-sm { padding: 6px 11px; font-size: 12.5px; margin-right: 4px; }
.btn-danger { color: var(--danger); border-color: #fecaca; }
.btn-danger:hover { background: var(--danger-bg); }

/* ---------- Badges ---------- */
.badge { padding: 3px 10px; border-radius: 100px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge-in { background: var(--success-bg); color: var(--success); }
.badge-out { background: var(--danger-bg); color: var(--danger); }
.badge-adjust { background: var(--primary-light); color: var(--primary); }
.badge-waiting { background: var(--warn-bg); color: var(--warn-text); }
.badge-arrived { background: var(--purple-bg); color: var(--purple); }
.badge-received { background: var(--success-bg); color: var(--success); }
.badge-cancelled { background: var(--danger-bg); color: var(--danger); }
.badge-stock { background: var(--success-bg); color: var(--success); }
.badge-preorder { background: var(--purple-bg); color: var(--purple); }

.row-low-stock { background: var(--warn-bg); }
.qty-danger { color: var(--danger); font-weight: 700; }
.row-overdue { background: var(--danger-bg); }

.computed-value {
  display: block;
  padding: 9px 11px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.deposit-quick-buttons {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text-muted);
  margin-top: -6px;
}
.deposit-quick-buttons .btn-sm { margin-right: 0; }

/* ---------- Modals ---------- */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 17, 21, 0.5);
  backdrop-filter: blur(2px);
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal-overlay.active { display: flex; }

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 26px;
  width: 400px;
  max-width: 100%;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  animation: pop-in 0.15s ease;
}

.modal-wide { width: 640px; }

@keyframes pop-in {
  from { opacity: 0; transform: scale(0.97) translateY(4px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal h2 { margin-top: 0; font-size: 17px; font-weight: 700; }
.modal-subtitle { color: var(--text-muted); font-size: 13.5px; margin-top: -8px; }

.modal form { display: flex; flex-direction: column; gap: 13px; }
.modal label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--text-muted); }

.modal input,
.modal select,
.login-box input {
  width: 100%;
  min-width: 0;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 400;
  color: var(--text);
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.modal label,
.login-box label { min-width: 0; }
.modal input:focus,
.modal select:focus,
.login-box input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.form-row { display: flex; gap: 12px; }
.form-row label { flex: 1; }

.stock-type-group { display: flex; gap: 8px; font-weight: 500; flex-wrap: wrap; }
.radio-pill {
  flex-direction: row !important;
  align-items: center;
  gap: 6px !important;
  border: 1px solid var(--border);
  border-radius: 100px;
  padding: 7px 14px !important;
  cursor: pointer;
  font-size: 13px !important;
  color: var(--text) !important;
  transition: border-color 0.15s, background 0.15s;
}
.radio-pill:has(input:checked) { border-color: var(--primary); background: var(--primary-light); color: var(--primary) !important; }

.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

/* ---------- Product thumbnails ---------- */
.product-thumb {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--bg);
  border: 1px solid var(--border);
  vertical-align: middle;
}
img.product-thumb.clickable {
  cursor: zoom-in;
  transition: transform 0.15s, box-shadow 0.15s;
}
img.product-thumb.clickable:hover {
  transform: scale(1.15);
  box-shadow: var(--shadow-md);
}

/* ---------- Image lightbox ---------- */
.lightbox-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  align-items: center;
  justify-content: center;
  z-index: 300;
  padding: 32px;
  cursor: zoom-out;
}
.lightbox-overlay.active { display: flex; }
.lightbox-overlay img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  cursor: default;
}
.lightbox-close {
  position: absolute;
  top: 20px;
  right: 24px;
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: #fff;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  transition: background 0.15s;
}
.lightbox-close:hover { background: rgba(255, 255, 255, 0.3); }
.product-thumb-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: var(--text-muted);
}
.product-thumb-lg { width: 64px; height: 64px; border-radius: 12px; font-size: 24px; }

.image-upload-row { display: flex; align-items: center; gap: 14px; }
.image-upload-actions { display: flex; flex-direction: column; gap: 6px; }
.image-upload-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
}

/* ---------- Login screen ---------- */
.login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #eef2ff 0%, var(--bg) 45%);
  z-index: 50;
  padding: 16px;
}

.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 36px 32px;
  width: 360px;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 13px;
  box-shadow: var(--shadow-lg);
}

.login-logo {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  background: var(--primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  margin: 0 auto 4px;
}

.login-box h1 { font-size: 19px; font-weight: 700; margin: 0; text-align: center; }
.login-box .modal-subtitle { text-align: center; margin-top: -6px; margin-bottom: 4px; }
.login-box label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--text-muted); }
.login-box button { margin-top: 8px; padding: 11px; font-size: 14px; }

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin: 0;
  min-height: 16px;
  text-align: center;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #16181d;
  color: #fff;
  padding: 11px 22px;
  border-radius: 100px;
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); }

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  .app { padding: 0 14px 40px; }
  .cards { grid-template-columns: 1fr; }
  .form-row { flex-direction: column; }
  .topbar h1 { font-size: 16px; }
  .brand-logo { width: 34px; height: 34px; font-size: 18px; }
  .card { padding: 15px; }

  /* ตารางกว้างเกินจอมือถือ ต้องเลื่อนซ้าย-ขวาดู - เพิ่มคำใบ้ + เงาขอบขวาให้เห็นชัดว่าเลื่อนได้ */
  .table-wrap::before {
    content: '👉 ปัดซ้าย-ขวาเพื่อดูข้อมูลเพิ่มเติม';
    display: block;
    font-size: 11.5px;
    color: var(--text-muted);
    padding-bottom: 8px;
  }
  .table-wrap::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 24px;
    background: linear-gradient(to right, transparent, rgba(15, 17, 21, 0.14));
    pointer-events: none;
  }
}
