:root {
  --primary: #6d28d9;
  --primary-dark: #5b21b6;
  --primary-light: #ede9fe;
  --border: #e4e7ef;
  --text: #1e2233;
  --muted: #6b7280;
  --bg: #f4f5fb;
  --danger: #dc2626;
  --success: #16a34a;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Bai Jamjuree", "Noto Sans Thai", sans-serif;
  color: var(--text);
  background:
    radial-gradient(1000px 400px at 85% -10%, rgba(139, 92, 246, .16), transparent 60%),
    radial-gradient(800px 400px at 0% -10%, rgba(56, 189, 248, .12), transparent 60%),
    var(--bg);
  min-height: 100vh;
}

button, input, select { font: inherit; }

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  width: min(420px, 100%);
  padding: 36px 32px;
  background: rgba(255, 255, 255, .94);
  border: 1px solid rgba(255, 255, 255, .8);
  border-radius: 22px;
  box-shadow: 0 18px 50px rgba(76, 29, 149, .14);
  text-align: center;
}

.login-logo {
  width: 64px;
  height: 64px;
  display: grid;
  place-items: center;
  margin: 0 auto 16px;
  border-radius: 18px;
  color: white;
  background: linear-gradient(135deg, #7c3aed, var(--primary-dark));
  font-size: 30px;
  box-shadow: 0 10px 24px rgba(109, 40, 217, .26);
}

.login-card h1 {
  margin: 0;
  color: var(--primary-dark);
  font-size: 24px;
}

.login-card > p {
  margin: 8px 0 24px;
  color: var(--muted);
}

.login-field {
  display: flex;
  flex-direction: column;
  gap: 7px;
  text-align: left;
  font-weight: 600;
  color: #4b5563;
}

.login-field input {
  width: 100%;
  min-height: 50px;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 12px;
  outline: none;
  font-size: 18px;
}

.login-field input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(109, 40, 217, .12);
}

.login-btn {
  width: 100%;
  min-height: 50px;
  margin-top: 16px;
}

.login-error {
  min-height: 22px;
  margin-top: 12px;
  color: var(--danger);
  font-size: 14px;
}

.admin-login-back {
  display: block;
  width: fit-content;
  margin-bottom: 18px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
}

.admin-shell { min-height: 100vh; padding: 24px 16px; }

.admin-card {
  width: min(1200px, 100%);
  margin: auto;
  padding: 24px;
  background: rgba(255, 255, 255, .94);
  border-radius: 22px;
  box-shadow: 0 18px 50px rgba(76, 29, 149, .1);
}

.admin-header,
.admin-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.admin-header > div:first-child { display: flex; align-items: center; gap: 14px; }
.admin-header h1 { margin: 0; color: var(--primary-dark); font-size: 22px; }

.admin-account { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; }
.admin-account button {
  border: 0;
  border-radius: 9px;
  padding: 8px 12px;
  background: #fee2e2;
  color: var(--danger);
  font-weight: 600;
  cursor: pointer;
}

.admin-tabs { display: flex; gap: 8px; margin: 24px 0 18px; border-bottom: 1px solid var(--border); }
.admin-tab {
  border: 0;
  border-bottom: 3px solid transparent;
  padding: 10px 20px;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  cursor: pointer;
}
.admin-tab.active { color: var(--primary); border-bottom-color: var(--primary); }

.admin-toolbar { margin-bottom: 14px; }
.admin-toolbar h2 { margin: 0; font-size: 18px; }
.admin-toolbar span { color: var(--muted); font-size: 13px; }

.admin-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 14px; }
.admin-table { min-width: 700px; table-layout: auto; }
.admin-table th { height: 48px; }
.admin-table td { padding: 8px 12px; font-weight: 500; }
.admin-edit {
  border: 0;
  border-radius: 8px;
  padding: 7px 14px;
  color: var(--primary-dark);
  background: var(--primary-light);
  font-weight: 600;
  cursor: pointer;
}

.admin-edit-modal { width: min(500px, 100%); }

.page-shell {
  min-height: 100vh;
  padding: 32px 16px;
}

.card {
  width: min(1570px, 100%);
  margin: 0 auto;
  background: rgba(255, 255, 255, .92);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, .7);
  border-radius: 22px;
  box-shadow: 0 18px 50px rgba(76, 29, 149, .10);
  overflow: hidden;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  padding: 24px 28px 12px;
}

.toolbar h1 {
  margin: 0 8px 0 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: .2px;
  white-space: nowrap;
  background: linear-gradient(120deg, var(--primary), #9333ea);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.icon { -webkit-text-fill-color: initial; color: var(--primary); }

.control-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 14px;
  color: #4b5563;
  white-space: nowrap;
}

input[type="date"], select, input[type="search"] {
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: white;
  min-height: 44px;
  padding: 8px 14px;
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
}

input[type="date"]:focus, select:focus, input[type="search"]:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(109, 40, 217, .12);
}

select { min-width: 160px; cursor: pointer; }

.btn {
  border: 0;
  border-radius: var(--radius);
  min-height: 44px;
  padding: 8px 20px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease, background .15s ease;
}

.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: .55; cursor: wait; transform: none; }

.btn-primary {
  color: white;
  background: linear-gradient(135deg, #7c3aed, var(--primary));
  box-shadow: 0 6px 16px rgba(109, 40, 217, .28);
}
.btn-primary:hover { background: linear-gradient(135deg, #6d28d9, var(--primary-dark)); }

.btn-success {
  color: white;
  background: linear-gradient(135deg, #22c55e, var(--success));
  box-shadow: 0 6px 16px rgba(22, 163, 74, .28);
}
.btn-success:hover { background: linear-gradient(135deg, #16a34a, #15803d); }

.btn-light {
  color: #374151;
  background: #eef0f6;
  border: 1.5px solid var(--border);
}
.btn-light:hover { background: #e4e7f0; }

.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 28px 14px;
  color: var(--muted);
  font-size: 14px;
}

#searchInput { width: min(320px, 100%); }

.table-wrap {
  margin: 0 28px;
  border: 1px solid var(--border);
  border-radius: 16px;
  overflow: auto;
  max-height: calc(100vh - 230px);
  box-shadow: 0 2px 10px rgba(16, 24, 40, .04);
}

table {
  width: 100%;
  min-width: 640px;
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
}

thead {
  position: sticky;
  top: 0;
  z-index: 2;
}

th {
  background: linear-gradient(160deg, #7c3aed, #5b21b6);
  color: white;
  height: 56px;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: .3px;
  text-align: center;
  border-right: 1px solid rgba(255, 255, 255, .14);
}

th:last-child { border-right: 0; }

td {
  height: 46px;
  border-top: 1px solid var(--border);
  border-right: 1px solid var(--border);
  text-align: center;
  font-weight: 500;
  background: #fff;
}

td:last-child { border-right: 0; }

tbody tr:nth-child(even) td { background: #fbfaff; }
tbody tr:hover td { background: var(--primary-light); }

/* คอลัมน์ FG / QA / ส่วนต่าง กว้างเท่ากัน ที่เหลือเป็นของชื่อสินค้า */
.product-col { width: 40%; }
th:not(.product-col) { width: 20%; }

.product-name {
  text-align: left;
  padding: 8px 14px;
  font-weight: 600;
  overflow-wrap: break-word;
}

.number-input {
  width: 100%;
  height: 44px;
  border: 0;
  padding: 5px 10px;
  text-align: center;
  font-weight: 600;
  background: #fffdf4;
  outline: none;
  transition: background .12s ease, box-shadow .12s ease;
}

.number-input:focus {
  background: var(--primary-light);
  box-shadow: inset 0 0 0 2px var(--primary);
}

.diff-cell { font-weight: 700; }
.diff-negative { color: var(--danger); background: #fef2f2 !important; }
.diff-positive { color: #b45309; background: #fffbeb !important; }
.diff-zero { color: var(--success); }

.fg-cell { font-weight: 600; }

.qa-col { padding: 0; }

.qa-cell {
  width: 100%;
  height: 44px;
  border: 0;
  background: #fffdf4;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: background .12s ease;
}

.qa-cell:hover { background: var(--primary-light); }
.qa-cell.has-value { color: var(--primary-dark); }
.qa-cell small { color: var(--muted); font-weight: 500; }

.qa-add-hint { color: #9ca3af; font-weight: 500; }

/* ---------- modal ---------- */

body.modal-open { overflow: hidden; }

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(17, 20, 34, .5);
  backdrop-filter: blur(2px);
}

.modal-overlay[hidden] { display: none; }

.modal {
  width: min(560px, 100%);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  background: white;
  border-radius: 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .3);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 22px;
  background: linear-gradient(135deg, #7c3aed, var(--primary));
  color: white;
}

.modal-header h2 { margin: 0; font-size: 18px; font-weight: 700; }

.modal-close {
  border: 0;
  background: rgba(255, 255, 255, .18);
  color: white;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
}

.modal-close:hover { background: rgba(255, 255, 255, .3); }

.modal-body {
  padding: 18px 22px;
  overflow-y: auto;
}

.modal-product {
  margin: 0 0 14px;
  font-weight: 700;
  color: var(--primary-dark);
}

.qa-entries {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.qa-entry {
  border: 1.5px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  background: #fbfaff;
}

.qa-entry-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.qa-entry-title { font-weight: 700; color: var(--primary-dark); }

.qa-entry-remove {
  border: 0;
  background: #fee2e2;
  color: var(--danger);
  padding: 4px 12px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}

.qa-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 12px;
}

.qa-field > span { font-size: 13px; font-weight: 600; color: #4b5563; }

.qa-field input[type="text"],
.qa-field input[type="number"],
.qa-field select,
.qa-field textarea {
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  font: inherit;
  outline: none;
  background: white;
  resize: vertical;
}

.qa-field input:focus,
.qa-field select:focus,
.qa-field textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(109, 40, 217, .12);
}

.qa-upload {
  display: inline-flex;
  cursor: pointer;
  width: fit-content;
}

.qa-upload-btn {
  display: inline-block;
  padding: 8px 16px;
  border: 1.5px dashed var(--primary);
  border-radius: 10px;
  color: var(--primary-dark);
  font-weight: 600;
  background: var(--primary-light);
}

.qa-image-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.qa-thumb {
  position: relative;
  width: 72px;
  height: 72px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.qa-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.qa-thumb-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: 50%;
  background: rgba(17, 20, 34, .72);
  color: white;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
}

.qa-add {
  width: 100%;
  margin-top: 14px;
  border: 1.5px dashed var(--primary);
  color: var(--primary-dark);
  background: var(--primary-light);
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 22px;
  border-top: 1px solid var(--border);
  background: #fff;
}

.qa-total { font-size: 15px; color: #4b5563; }
.qa-total strong { color: var(--primary-dark); font-size: 18px; }

.modal-actions { display: flex; gap: 10px; }

.summary {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: flex-end;
  padding: 16px 28px 22px;
  color: #4b5563;
  font-size: 14px;
}

.summary > div {
  background: #f6f5fc;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 16px;
}

.summary strong {
  color: var(--primary-dark);
  font-weight: 700;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  min-width: 240px;
  max-width: 360px;
  padding: 13px 18px;
  color: white;
  background: #16182b;
  border-radius: 14px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .24);
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: .22s ease;
  font-weight: 500;
}

.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: #b42318; }

.back-btn {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  white-space: nowrap;
}

.menu-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.menu-card {
  width: min(520px, 100%);
  background: rgba(255, 255, 255, .92);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, .7);
  border-radius: 22px;
  box-shadow: 0 18px 50px rgba(76, 29, 149, .10);
  padding: 36px 32px;
}

.menu-title {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  text-align: center;
  background: linear-gradient(120deg, var(--primary), #9333ea);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.menu-title .icon { -webkit-text-fill-color: initial; }

.menu-subtitle {
  margin: 8px 0 26px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px 20px;
  border: 1.5px solid var(--border);
  border-radius: 16px;
  background: white;
  text-decoration: none;
  color: var(--text);
  transition: transform .14s ease, box-shadow .14s ease, border-color .14s ease;
}

.menu-item:hover {
  transform: translateY(-2px);
  border-color: var(--primary);
  box-shadow: 0 10px 26px rgba(109, 40, 217, .16);
}

.menu-item-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  flex: none;
  border-radius: 14px;
  font-size: 22px;
  color: var(--primary);
  background: var(--primary-light);
}

.menu-item-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.menu-item-text strong { font-size: 17px; font-weight: 700; }
.menu-item-text small { color: var(--muted); font-size: 13px; }

.menu-item-arrow {
  margin-left: auto;
  font-size: 26px;
  color: #c4b5fd;
}

.menu-item:hover .menu-item-arrow { color: var(--primary); }

.menu-user {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 22px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}

.menu-user strong { color: var(--text); }

.menu-user button {
  border: 0;
  background: #fee2e2;
  color: var(--danger);
  border-radius: 9px;
  padding: 7px 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

@media (max-width: 760px) {
  .page-shell { padding: 10px; }
  .card { border-radius: 16px; }
  .toolbar { padding: 16px 12px 8px; gap: 8px; }
  .toolbar h1 { width: 100%; font-size: 19px; }
  .back-btn { flex: 0 0 auto; }
  .control-label { width: 100%; justify-content: space-between; font-size: 13px; }
  .control-label input, .control-label select { flex: 1; min-width: 0; }
  .btn { flex: 1; min-height: 46px; }
  .status-row { padding: 4px 12px 10px; flex-direction: column; align-items: stretch; }
  #searchInput { width: 100%; }
  .summary { padding: 12px; justify-content: center; gap: 8px; font-size: 13px; }
  .summary > div { padding: 6px 12px; }

  /* ตารางพอดีจอ ไม่ต้องเลื่อนแนวนอน หัวตารางลอยติดขอบบนตอนเลื่อนหน้า */
  .table-wrap {
    margin: 0 10px;
    max-height: none;
    overflow: visible;
    border-radius: 12px;
  }

  table { min-width: 100%; }

  th {
    height: 44px;
    font-size: 13px;
    padding: 4px 2px;
  }

  td {
    height: 44px;
    font-size: 14px;
    padding: 0;
  }

  .product-col { width: 40%; }
  th:not(.product-col) { width: 20%; }

  .product-name {
    padding: 8px;
    font-size: 13px;
    line-height: 1.35;
    word-break: break-word;
  }

  .number-input {
    height: 44px;
    padding: 4px 2px;
    font-size: 16px; /* กัน iOS ซูมอัตโนมัติตอนแตะช่องกรอก */
  }

  .qa-cell { font-size: 14px; }
  .diff-cell { font-size: 14px; }

  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal { width: 100%; max-height: 92vh; border-radius: 18px 18px 0 0; }
  .modal-footer { flex-direction: column-reverse; align-items: stretch; gap: 10px; }
  .modal-actions .btn { flex: 1; }
  .qa-field input,
  .qa-field select,
  .qa-field textarea { font-size: 16px; } /* กัน iOS ซูม */

  .admin-shell { padding: 10px; }
  .admin-card { padding: 14px; border-radius: 16px; }
  .admin-header { align-items: flex-start; }
  .admin-header > div:first-child { align-items: flex-start; flex-direction: column; gap: 8px; }
  .admin-header h1 { font-size: 19px; }
  .admin-account { flex-direction: column; align-items: flex-end; }
  .admin-account span { text-align: right; }
  .admin-tabs { overflow-x: auto; }
  .admin-tab { flex: 1; min-width: 80px; padding: 10px; }
  .admin-toolbar .btn { flex: 0 0 auto; }

  .admin-table { min-width: 0; }
  .admin-table thead { display: none; }
  .admin-table tbody,
  .admin-table tr,
  .admin-table td { display: block; width: 100%; }
  .admin-table tr { padding: 10px; border-bottom: 1px solid var(--border); }
  .admin-table tr:last-child { border-bottom: 0; }
  .admin-table td {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    height: auto;
    min-height: 36px;
    border: 0;
    text-align: right;
    overflow-wrap: anywhere;
  }
  .admin-table td::before {
    content: attr(data-label);
    color: var(--muted);
    font-weight: 600;
    text-align: left;
  }
}
