:root {
  --bg: #F5F6F8;
  --surface: #FFFFFF;
  --border: #E4E6EA;
  --text-primary: #12151B;
  --text-secondary: #6B7280;
  --text-muted: #9CA3AF;
  --accent: #2563EB;
  --accent-bg: #EAF1FE;
  --success: #16A34A;
  --success-bg: #E9F8EF;
  --danger: #DC2626;
  --danger-bg: #FDEDED;
  --warning: #D97706;
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F1115;
    --surface: #1A1D23;
    --border: #2A2E37;
    --text-primary: #F3F4F6;
    --text-secondary: #9CA3AF;
    --text-muted: #6B7280;
    --accent: #4F8EF7;
    --accent-bg: #16233D;
    --success: #34D399;
    --success-bg: #0F2A1E;
    --danger: #F87171;
    --danger-bg: #3A1518;
    --warning: #FBBF24;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
}

body {
  font-family: "Noto Sans Thai", -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  margin: 0;
  color: var(--text-primary);
  /* --footer-h ถูกตั้งค่าโดย JS ตามความสูงจริงของ .footer-bar กันเนื้อหาโดนบัง */
  padding-bottom: calc(var(--footer-h, 120px) + 20px);
}

header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding: calc(16px + env(safe-area-inset-top)) 20px 12px;
}

header h1 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 12px;
}

.steps {
  display: flex;
  gap: 6px;
}

.step-dot {
  flex: 1;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  transition: background 0.25s ease;
}

.step-dot.done {
  background: var(--success);
}

.step-dot.active {
  background: var(--accent);
}

main {
  padding: 4px 20px 20px;
  max-width: 480px;
  margin: 0 auto;
}

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

.card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.card-icon {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent-bg);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.card-icon svg {
  width: 18px;
  height: 18px;
}

.card-title {
  font-size: 14px;
  font-weight: 600;
  flex: 1;
}

.check-badge {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--success-bg);
  color: var(--success);
  display: none;
  align-items: center;
  justify-content: center;
}

.check-badge.show {
  display: flex;
}

.check-badge svg {
  width: 13px;
  height: 13px;
}

.input-row {
  display: flex;
  gap: 8px;
}

input[type="text"],
input[type="number"],
select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text-primary);
  min-height: 46px;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-bg);
}

input[readonly] {
  color: var(--text-secondary);
}

.sn-hint {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 6px;
  min-height: 16px;
}

.sn-hint.warn {
  color: var(--danger);
}

.scan-btn {
  flex-shrink: 0;
  width: 46px;
  height: 46px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.scan-btn:active {
  transform: scale(0.94);
}

.scan-btn svg {
  width: 20px;
  height: 20px;
}

.parsed-grid {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.parsed-item {
  flex: 1;
}

.parsed-item .mini-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
  font-weight: 500;
}

.parsed-item input {
  font-size: 13px;
  padding: 8px 10px;
  min-height: 36px;
  background: var(--bg);
  color: var(--text-secondary);
}

.mode-toggle {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 12px;
}

.mode-btn {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px 4px;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  color: var(--text-secondary);
  border-radius: 6px;
  min-height: 36px;
}

.mode-btn.active {
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.untagged-row {
  margin-bottom: 10px;
}

.untagged-row:last-child {
  margin-bottom: 0;
}

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

.qty-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.qty-btn {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-primary);
  font-size: 20px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.qty-btn:active {
  background: var(--bg);
}

#qty {
  text-align: center;
  font-weight: 600;
  font-size: 17px;
}

.footer-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom));
  z-index: 20;
}

#submit-btn {
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: block;
  padding: 15px;
  border: none;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  font-family: inherit;
  min-height: 50px;
}

#submit-btn:active {
  transform: scale(0.98);
}

#submit-btn:disabled {
  background: var(--border);
  color: var(--text-muted);
}

.snackbar {
  position: fixed;
  left: 20px;
  right: 20px;
  bottom: calc(84px + env(safe-area-inset-bottom));
  max-width: 440px;
  margin: 0 auto;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 30;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}

.snackbar.show {
  opacity: 1;
  transform: translateY(0);
}

.snackbar.success {
  background: var(--success-bg);
  color: var(--success);
}

.snackbar.error {
  background: var(--danger-bg);
  color: var(--danger);
}

.snackbar svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

#scanner-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 100;
  display: flex;
  flex-direction: column;
}

#scanner-overlay.hidden {
  display: none;
}

.scanner-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(16px + env(safe-area-inset-top)) 20px 16px;
}

.scanner-top span {
  color: white;
  font-size: 15px;
  font-weight: 500;
}

#cancel-scan-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

#cancel-scan-btn svg {
  width: 18px;
  height: 18px;
}

#reader {
  flex: 1;
}

.scanner-hint {
  text-align: center;
  color: rgba(255, 255, 255, 0.6);
  font-size: 13px;
  padding: 16px 20px calc(24px + env(safe-area-inset-bottom));
}