:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1a1f2e;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --accent: #059669;
  --danger: #dc2626;
  --warn: #f59e0b;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.08), 0 4px 10px rgba(0,0,0,0.04);
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--primary); }
.brand h1 { font-size: 18px; margin: 0; color: var(--text); font-weight: 600; }

.tabs { display: flex; gap: 4px; }
.tab {
  background: transparent;
  border: none;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.15s;
}
.tab:hover { background: var(--bg); color: var(--text); }
.tab.active { background: var(--primary); color: white; }

main { max-width: 1100px; margin: 0 auto; padding: 24px 16px; }

.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; flex-wrap: wrap; gap: 8px; }
.card h2 { margin: 0 0 16px; font-size: 18px; font-weight: 600; }
.card h3 { font-size: 15px; font-weight: 600; margin: 20px 0 10px; display: flex; align-items: center; gap: 10px; }

.muted { color: var(--muted); font-size: 13px; }

/* Upload card */
.upload-card { text-align: center; border: 2px dashed var(--border); transition: border-color 0.2s, background 0.2s; }
.upload-card.dragover { border-color: var(--primary); background: #eff6ff; }
.upload-inner svg { color: var(--primary); margin-bottom: 8px; }
.upload-inner h2 { margin: 8px 0; font-size: 17px; }
.upload-buttons { margin-top: 16px; display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; }

.file-preview { display: flex; flex-direction: column; gap: 14px; align-items: stretch; }
.file-meta {
  display: flex; align-items: center; gap: 14px;
  background: var(--bg); border-radius: 8px; padding: 12px 14px;
}
.file-icon { font-size: 28px; }
.file-name { font-weight: 600; word-break: break-all; }
.file-size { font-size: 12px; }
.file-meta > div { flex: 1; text-align: left; }

/* Buttons */
.btn {
  border: none;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn.primary:disabled { background: #93c5fd; cursor: not-allowed; }
.btn.ghost { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: #ebedf0; }
.btn.danger-text { color: var(--danger); background: transparent; padding: 4px 8px; }
.btn.danger-text:hover { background: #fee2e2; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.big { padding: 14px 22px; font-size: 16px; align-self: stretch; justify-content: center; }

/* Form */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin-bottom: 16px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; color: var(--muted); }
.form-grid label.full { grid-column: 1 / -1; }
.form-grid input, .form-grid textarea, select {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: white;
}
.form-grid input:focus, .form-grid textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

/* Table */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--bg); font-weight: 600; font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: 0.3px; }
tr:last-child td { border-bottom: none; }
table input { width: 100%; padding: 6px 8px; border: 1px solid transparent; border-radius: 4px; font-size: 14px; background: transparent; }
table input:focus { background: white; border-color: var(--primary); outline: none; }
table input.qty, table input.unit, table input.total { width: 80px; text-align: right; }
.ralign { text-align: right; }
tfoot td { padding: 12px 10px; background: var(--bg); }

/* Save row */
.save-row { display: flex; gap: 12px; margin-top: 20px; align-items: center; flex-wrap: wrap; }
.sheet-picker { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; }
.sheet-picker select { padding: 9px 11px; }

.badge { padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #d1fae5; color: var(--accent); }
.badge.warn { background: #fef3c7; color: #92400e; }
.badge.danger { background: #fee2e2; color: var(--danger); }

.status { margin-top: 14px; padding: 12px 14px; border-radius: 8px; display: none; font-size: 14px; }
.status.show { display: block; }
.status.success { background: #d1fae5; color: #065f46; }
.status.error { background: #fee2e2; color: #991b1b; }
.status.info { background: #dbeafe; color: #1e40af; }

/* History */
.history-list { display: flex; flex-direction: column; gap: 10px; }
.history-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; background: var(--bg); border-radius: 8px;
  flex-wrap: wrap; gap: 10px;
}
.history-item .h-meta { display: flex; flex-direction: column; gap: 2px; }
.history-item .h-supplier { font-weight: 600; }
.history-item .h-sub { font-size: 12px; color: var(--muted); }
.history-item .h-amount { font-weight: 600; color: var(--accent); }

.config-status, #mappingViewer {
  background: var(--bg); padding: 12px; border-radius: 6px; font-size: 13px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  white-space: pre-wrap;
  margin-bottom: 16px;
}

/* Loading spinner */
.spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid rgba(255,255,255,0.3); border-top-color: white;
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Mobile */
@media (max-width: 600px) {
  .topbar { padding: 12px 14px; }
  .brand h1 { font-size: 16px; }
  main { padding: 14px 10px; }
  .card { padding: 16px; }
  .save-row { flex-direction: column; align-items: stretch; }
  .save-row > * { width: 100%; }
  .upload-buttons { flex-direction: column; }
  .upload-buttons .btn { width: 100%; justify-content: center; }
}
