:root {
  --primary: #4F46E5;
  --primary-light: #EEF2FF;
  --success: #10B981;
  --warning: #F59E0B;
  --danger: #EF4444;
  --bg: #F8FAFC;
  --card: #FFFFFF;
  --text: #1E293B;
  --text-secondary: #64748B;
  --border: #E2E8F0;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg: 0 10px 25px rgba(0,0,0,.1);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

.header {
  background: linear-gradient(135deg, #4338CA, #6366F1);
  color: #fff;
  padding: 16px 20px;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(79,70,229,.3);
}

.header h1 {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: .5px;
}

.header .subtitle {
  font-size: 12px;
  opacity: .8;
  margin-top: 2px;
}

.header-switch-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  padding: 6px 12px;
  background: rgba(255,255,255,.2);
  border: 1px solid rgba(255,255,255,.3);
  border-radius: 12px;
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: all .2s;
}

.header-switch-btn:hover {
  background: rgba(255,255,255,.3);
}

.container {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px;
}

/* ===== Status Card ===== */
.status-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: var(--shadow);
  font-size: 13px;
}

.status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot.loaded { background: var(--success); }
.status-dot.empty { background: #CBD5E1; }

.status-info { flex: 1; min-width: 0; }

.status-text { color: var(--text-secondary); font-size: 12px; }
.status-count { font-weight: 700; color: var(--text); font-size: 12px; display: block; }

/* ===== Table Selector ===== */
.table-selector {
  padding: 6px 10px;
  border: 1.5px solid #CBD5E1;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  background: #fff;
  color: var(--text);
  max-width: 130px;
  outline: none;
  cursor: pointer;
}

.table-selector:focus { border-color: var(--primary); }

/* ===== Tabs ===== */
.tabs {
  display: flex;
  background: var(--card);
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.tab {
  flex: 1;
  padding: 10px;
  text-align: center;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  border-radius: 10px;
  cursor: pointer;
  transition: all .2s;
  border: none;
  background: none;
}

.tab.active {
  background: var(--primary);
  color: #fff;
}

/* ===== Panels ===== */
.panel { display: none; }
.panel.active { display: block; }

/* ===== Upload Area ===== */
.upload-zone {
  background: var(--card);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 36px 20px;
  text-align: center;
  cursor: pointer;
  transition: all .2s;
  box-shadow: var(--shadow);
}

.upload-zone:hover, .upload-zone.dragover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.upload-icon {
  font-size: 40px;
  margin-bottom: 10px;
  display: block;
}

.upload-zone .title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
}

.upload-zone .hint {
  font-size: 12px;
  color: var(--text-secondary);
}

.upload-zone input { display: none; }

/* ===== Server URL Load ===== */
.url-load-section {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-top: 12px;
  box-shadow: var(--shadow);
}

.url-load-title {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 600;
  margin-bottom: 8px;
}

.url-load-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.url-input {
  flex: 1;
  padding: 10px 12px;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  outline: none;
  transition: border-color .2s;
}

.url-input:focus { border-color: var(--primary); }
.url-input::placeholder { color: #94A3B8; font-size: 12px; }

/* ===== Keypad ===== */
.keypad {
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.keypad-row {
  display: flex;
  gap: 6px;
}

.key-btn {
  flex: 1;
  padding: 14px 0;
  border: none;
  border-radius: 10px;
  background: var(--card);
  box-shadow: var(--shadow);
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: background .1s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.key-btn:active { background: #E2E8F0; }

.key-fn {
  font-size: 14px;
  color: var(--text-secondary);
  background: #F1F5F9;
}

.key-fn:active { background: #E2E8F0; }

.keypad.compact .key-btn { padding: 8px 0; font-size: 15px; }
.keypad.compact .key-fn { font-size: 11px; }

.upload-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.btn {
  flex: 1;
  padding: 12px 16px;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all .2s;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:active { background: #4338CA; }

.btn-outline {
  background: #fff;
  color: var(--primary);
  border: 1.5px solid var(--primary);
}

.btn-outline:active { background: var(--primary-light); }

.btn-danger {
  background: #FEF2F2;
  color: var(--danger);
  border: 1.5px solid #FECACA;
}

.btn-danger:active { background: #FEE2E2; }

.btn-small {
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 6px;
  flex: 0;
}

/* ===== Search ===== */
.search-box {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.search-input {
  flex: 1;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  outline: none;
  transition: border-color .2s;
  background: var(--card);
}

.search-input:focus { border-color: var(--primary); }

.search-input::placeholder { color: #94A3B8; }

/* ===== Scanner ===== */
.scanner-box {
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}

#reader {
  width: 100%;
  max-width: 460px;
  margin: 0 auto;
}

#reader video {
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-height: 200px;
  object-fit: cover;
}

.scan-controls {
  display: flex;
  gap: 8px;
  padding: 12px;
}

.scan-result-input {
  background: #F0FDF4;
  border: 1.5px solid #BBF7D0;
  color: #166534;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  margin: 0 12px 12px;
  word-break: break-all;
}

/* ===== Result Card ===== */
.result-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: slideUp .3s ease;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

.result-header {
  background: linear-gradient(135deg, #ECFDF5, #D1FAE5);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.result-header .icon {
  width: 36px; height: 36px;
  background: var(--success);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: #fff;
  flex-shrink: 0;
}

.result-header .info { flex: 1; }
.result-header .name { font-size: 16px; font-weight: 700; }
.result-header .sku { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }

.result-body { padding: 14px 16px; }

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.info-item {
  background: var(--bg);
  border-radius: 8px;
  padding: 10px 12px;
}

.info-item .label {
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: .5px;
}

.info-item .value {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  word-break: break-all;
}

.info-full {
  grid-column: 1 / -1;
}

.no-result {
  background: var(--card);
  border-radius: var(--radius);
  padding: 32px 16px;
  text-align: center;
  box-shadow: var(--shadow);
  color: var(--text-secondary);
}

.no-result .icon { font-size: 40px; margin-bottom: 8px; display: block; }



/* Multi-color badge */
.multi-color-badge {
  display: inline-block;
  margin-top: 4px;
  padding: 3px 12px;
  background: linear-gradient(135deg, #6366F1, #8B5CF6);
  color: #fff;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .5px;
  animation: pulse-badge 2s ease-in-out infinite;
}

@keyframes pulse-badge {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

/* ===== Same Style Colors ===== */
.same-style-section {
  margin-top: 12px;
}

.same-style-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.same-style-title .badge {
  background: var(--primary-light);
  color: var(--primary);
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

.color-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow);
  font-size: 13px;
}

.color-table th {
  background: #F1F5F9;
  padding: 10px 12px;
  text-align: left;
  font-size: 11px;
  color: var(--text-secondary);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
}

.color-table td {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  font-weight: 500;
}

.color-table tr.matched {
  background: #FEF3C7;
}

.color-table tr.matched td {
  font-weight: 700;
  color: #92400E;
}

.color-table tr:not(.matched):active {
  background: #F8FAFC;
}

.color-table .color-col {
  display: flex;
  align-items: center;
  gap: 6px;
}

.color-swatch {
  width: 18px; height: 18px;
  border-radius: 50%;
  border: 1.5px solid #CBD5E1;
  flex-shrink: 0;
  background: #f0f0f0;
}

.color-dot {
  width: 16px; height: 16px;
  border-radius: 50%;
  border: 1px solid #ccc;
  flex-shrink: 0;
}

/* ===== History ===== */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-item {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: all .15s;
}

.history-item:active { background: #F1F5F9; }

.history-item .sku-code { font-weight: 600; font-size: 14px; }
.history-item .time { font-size: 11px; color: var(--text-secondary); }

.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-secondary);
}

.empty-state .icon { font-size: 48px; display: block; margin-bottom: 10px; }
.empty-state p { font-size: 14px; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  top: 72px;
  left: 50%;
  transform: translateX(-50%);
  background: #1E293B;
  color: #fff;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  z-index: 999;
  opacity: 0;
  transition: opacity .3s;
  pointer-events: none;
}

.toast.show { opacity: 1; }

/* ===== Table List ===== */
.table-list-section {
  margin-top: 16px;
}

.table-list-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}

.table-list { display: flex; flex-direction: column; gap: 8px; }

.table-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  gap: 8px;
}

.table-item.active {
  border: 2px solid var(--primary);
  background: var(--primary-light);
}

.table-item .tbl-info { flex: 1; min-width: 0; }
.table-item .tbl-name { font-size: 14px; font-weight: 600; }
.table-item .tbl-meta { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }

.table-item .tbl-actions { display: flex; gap: 6px; flex-shrink: 0; }

.tbl-btn {
  padding: 5px 10px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  border: none;
  cursor: pointer;
  white-space: nowrap;
}

.tbl-btn-select { background: var(--primary); color: #fff; }
.tbl-btn-select:active { background: #4338CA; }
.tbl-btn-delete { background: #FEF2F2; color: var(--danger); border: 1px solid #FECACA; }

/* ===== Scan Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity .25s;
}

.modal-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.45);
}

.modal-content {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: 16px 16px 0 0;
  padding: 20px 16px 32px;
  transform: translateY(100%);
  transition: transform .3s cubic-bezier(.4,0,.2,1);
  box-shadow: 0 -4px 20px rgba(0,0,0,.15);
}

.modal-overlay.show .modal-content {
  transform: translateY(0);
}

.modal-content::-webkit-scrollbar { width: 0; }

.modal-close {
  position: sticky;
  top: 0;
  float: right;
  width: 32px; height: 32px;
  border-radius: 50%;
  background: #E2E8F0;
  border: none;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  z-index: 1;
}

.modal-handle {
  width: 36px; height: 4px;
  background: #CBD5E1;
  border-radius: 2px;
  margin: 0 auto 16px;
}

.modal-content .result-card {
  box-shadow: none;
  border: 1px solid var(--border);
}

/* ===== spinner ===== */
.spinner {
  width: 20px; height: 20px;
  border: 2.5px solid #E2E8F0;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  display: inline-block;
  vertical-align: middle;
  margin-right: 6px;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Task Progress Bar ===== */
.task-progress-bar {
  background: var(--card);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.task-progress-track {
  height: 6px;
  background: #E2E8F0;
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 6px;
}

.task-progress-fill {
  height: 100%;
  border-radius: 3px;
  transition: width .3s ease;
}

.task-progress-fill.active { background: var(--primary); }
.task-progress-fill.completed { background: var(--success); }
.task-progress-fill.cancelled { background: #94A3B8; }

.task-progress-detail {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.task-progress-text {
  font-size: 11px;
  color: var(--text-secondary);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-progress-percent {
  font-size: 11px;
  font-weight: 700;
  color: var(--primary);
  white-space: nowrap;
}

.task-progress-actions {
  display: flex;
  gap: 4px;
  white-space: nowrap;
}

/* ===== Task Panel ===== */
.task-summary-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}

.task-summary-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.task-summary-name {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}

.task-summary-meta {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.task-status-badge {
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}

.task-status-active {
  background: var(--primary-light);
  color: var(--primary);
}

.task-status-done {
  background: #ECFDF5;
  color: var(--success);
}

.task-status-cancelled {
  background: #F1F5F9;
  color: #94A3B8;
}

.task-summary-progress {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.task-summary-bar {
  flex: 1;
  height: 8px;
  background: #E2E8F0;
  border-radius: 4px;
  overflow: hidden;
}

.task-summary-fill {
  height: 100%;
  border-radius: 4px;
  transition: width .3s ease;
}

.task-summary-stats {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.task-summary-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.task-records-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}

.task-records-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.task-record-item {
  background: var(--card);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: var(--shadow);
  font-size: 12px;
}

.task-record-idx {
  width: 22px; height: 22px;
  background: var(--primary-light);
  color: var(--primary);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  flex-shrink: 0;
}

.task-record-info { flex: 1; min-width: 0; }

.task-record-name {
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-record-meta {
  font-size: 10px;
  color: var(--text-secondary);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-record-time {
  font-size: 10px;
  color: var(--text-secondary);
  white-space: nowrap;
  flex-shrink: 0;
}

.share-link-hint {
  width: 100%;
  font-size: 10px;
  color: var(--success);
  word-break: break-all;
  margin-top: 2px;
  padding: 4px 8px;
  background: #F0FDF4;
  border-radius: 4px;
}

/* ===== Product Header Row ===== */
.prod-header-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: linear-gradient(135deg, #FEF3C7, #FDE68A);
  border-radius: var(--radius) var(--radius) 0 0;
}

/* ===== Product Code Hero ===== */
.prod-code-hero {
  flex: 1;
  text-align: left;
}

.prod-code-hero .hero-label {
  font-size: 11px;
  color: #92400E;
  font-weight: 500;
  letter-spacing: 1px;
  text-transform: uppercase;
  margin-bottom: 2px;
}

.prod-code-hero .hero-value {
  font-size: 24px;
  font-weight: 800;
  color: #78350F;
  letter-spacing: 2px;
  font-family: 'SF Mono', 'Consolas', 'Menlo', monospace;
}

.prod-code-hero .hero-copy {
  display: inline-block;
  margin-top: 6px;
  padding: 3px 10px;
  background: #F59E0B;
  color: #fff;
  border-radius: 12px;
  font-size: 10px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background .15s;
}

.prod-code-hero .hero-copy:active { background: #D97706; }

/* ===== Product Image ===== */
.prod-image-container {
  position: relative;
  width: 80px;
  height: 80px;
  background: rgba(255,255,255,.6);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: 2px solid #FCD34D;
}

.prod-image-container:hover {
  background: rgba(255,255,255,.8);
}

.prod-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.prod-image-placeholder {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #D97706;
}

/* ===== Image Preview Modal ===== */
.image-preview-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity .25s;
}

.image-preview-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.image-preview-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.8);
}

.image-preview-content {
  position: relative;
  width: 90%;
  max-width: 90vw;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.image-preview-img {
  max-width: 100%;
  max-height: 85vh;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0,0,0,.3);
}

.image-preview-close {
  position: absolute;
  top: -40px;
  right: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,.2);
  border: none;
  font-size: 20px;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .2s;
}

.image-preview-close:hover {
  background: rgba(255,255,255,.3);
}

.image-preview-hint {
  margin-top: 12px;
  font-size: 12px;
  color: rgba(255,255,255,.7);
}