:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #dbeafe;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --success: #16a34a;
  --success-light: #dcfce7;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --warning: #ea580c;
  --warning-light: #fff7ed;
  --info: #2563eb;
  --info-light: #dbeafe;
  --purple: #9333ea;
  --purple-light: #f3e8ff;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 25px -5px rgba(0,0,0,0.1), 0 8px 10px -6px rgba(0,0,0,0.05);
}

* { 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);
  font-size: 14px;
  line-height: 1.5;
}

/* ===== Navbar ===== */
.navbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  display: flex;
  align-items: center;
  height: 56px;
  gap: 32px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.nav-brand {
  font-size: 16px;
  font-weight: 700;
  white-space: nowrap;
}

.nav-tabs {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.nav-tab {
  background: none;
  border: none;
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  color: var(--text-muted);
  transition: all 0.15s;
  white-space: nowrap;
}

.nav-tab:hover { background: var(--bg); color: var(--text); }
.nav-tab.active { background: var(--primary-light); color: var(--primary); font-weight: 600; }

/* ===== Container ===== */
.container {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px;
}

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

/* ===== Metrics ===== */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.metric-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.metric-label {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.metric-value {
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
}

.metric-clickable {
  cursor: pointer;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.metric-clickable:hover {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary-light);
}
.metric-clickable .metric-value {
  color: var(--primary);
  text-decoration: underline;
  text-decoration-color: transparent;
  text-underline-offset: 3px;
  transition: text-decoration-color 0.2s;
}
.metric-clickable:hover .metric-value {
  text-decoration-color: var(--primary);
}

/* ===== Dashboard Channel Toggle ===== */
.dashboard-toolbar {
  margin-bottom: 16px;
}
.channel-toggle {
  display: inline-flex;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--card);
}
.channel-btn {
  padding: 8px 18px;
  font-size: 13px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s;
  border-right: 1px solid var(--border);
}
.channel-btn:last-child { border-right: none; }
.channel-btn:hover { background: var(--bg); color: var(--text); }
.channel-btn.active {
  background: var(--primary);
  color: #fff;
  font-weight: 500;
}

/* ===== Cards & Grids ===== */
.charts-grid, .lists-grid, .stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 24px;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.card-title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text);
}

.chart-wrap {
  height: 240px;
  position: relative;
}

.list-body { min-height: 120px; }

/* ===== Channel Order Stats Table ===== */
.channel-stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.channel-stats-table th {
  text-align: left;
  padding: 8px 4px;
  color: var(--text-muted);
  font-weight: 500;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
.channel-stats-table td {
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
}
.channel-stats-table tr:last-child td { border-bottom: none; }
.channel-stats-table .shop-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}
.channel-stats-table .shop-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.channel-stats-table .num-cell {
  font-weight: 600;
  text-align: right;
}
.channel-stats-table tfoot td {
  font-weight: 700;
  border-top: 2px solid var(--border);
  border-bottom: none;
  padding-top: 10px;
}

/* ===== Lists ===== */
.list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.list-item:last-child { border-bottom: none; }

.list-item-name { font-weight: 500; }
.list-item-meta { color: var(--text-muted); font-size: 12px; }

/* ===== Badges ===== */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-danger { background: var(--danger-light); color: var(--danger); }
.badge-warning { background: var(--warning-light); color: var(--warning); }
.badge-success { background: var(--success-light); color: var(--success); }
.badge-info { background: var(--info-light); color: var(--info); }
.badge-purple { background: var(--purple-light); color: var(--purple); }
.badge-secondary { background: #e5e7eb; color: #4b5563; }

/* ===== Page Header ===== */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  gap: 12px;
}

.page-title { font-size: 18px; font-weight: 700; }
.section-title { font-size: 16px; font-weight: 600; margin: 24px 0 12px; }

.search-input {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  background: var(--card);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
  min-width: 180px;
}

.search-input:focus { border-color: var(--primary); }

.filter-group { display: flex; gap: 8px; align-items: center; }

/* ===== Buttons ===== */
.btn {
  padding: 8px 18px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
  font-weight: 500;
}

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

.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }

.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: var(--success-dark, #15803d); }

.btn-sm { padding: 4px 10px; font-size: 12px; }

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  padding: 4px;
  border-radius: 4px;
  transition: background 0.15s;
}

.btn-icon:hover { background: var(--bg); }

/* ===== Tables ===== */
.table-wrap {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th {
  background: var(--bg);
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table tbody tr:hover { background: var(--bg); }
.data-table tbody tr:last-child td { border-bottom: none; }

.text-success { color: var(--success); font-weight: 600; }
.text-danger { color: var(--danger); font-weight: 600; }
.text-muted { color: var(--text-muted); }

.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
}

.actions { display: flex; gap: 4px; align-items: center; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  z-index: 200;
  padding: 40px 16px;
  overflow-y: auto;
}

.modal {
  background: var(--card);
  border-radius: var(--radius);
  width: 100%;
  max-width: 640px;
  box-shadow: var(--shadow-lg);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 { font-size: 17px; font-weight: 700; }

.modal-close {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  padding: 0;
}

.modal-close:hover { color: var(--text); }

.modal-body { padding: 20px; max-height: 60vh; overflow-y: auto; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}

/* ===== Forms ===== */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group.full { grid-column: 1 / -1; }

.form-group label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}

.req { color: var(--danger); }

.form-group input,
.form-group select,
.form-group textarea {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus { border-color: var(--primary); }

.form-group textarea { resize: vertical; }

/* Dual currency price input */
.dual-price-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
}
.dual-price-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.dual-price-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
}
.dual-price-col input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  background: var(--bg);
  color: var(--text);
}
.dual-price-col input:focus { border-color: var(--primary); outline: none; }
.dual-price-arrow {
  display: flex;
  align-items: center;
  font-size: 18px;
  color: var(--text-muted);
  padding-top: 18px;
}

.form-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.form-static {
  padding: 8px 12px;
  background: var(--bg);
  border-radius: 6px;
  font-weight: 500;
}

.form-section-title {
  grid-column: 1 / -1;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

/* ===== Profit Preview ===== */
.profit-preview {
  margin-top: 16px;
  padding: 14px;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.pp-section { margin-bottom: 10px; }
.pp-section:last-of-type { margin-bottom: 6px; }

.pp-section-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.pp-subtotal {
  font-weight: 600;
  border-top: 1px dashed var(--border);
  margin-top: 4px;
  padding-top: 4px;
}

.profit-row {
  display: flex;
  justify-content: space-between;
  padding: 3px 0;
  font-size: 13px;
}

.profit-total {
  border-top: 2px solid var(--border);
  margin-top: 6px;
  padding-top: 10px;
  font-weight: 700;
  font-size: 16px;
}

#pp-profit.positive { color: var(--success); }
#pp-profit.negative { color: var(--danger); }

.shop-price-tag {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  margin: 1px 0;
}
.shop-price-tag.temu { background: #fff7ed; color: #ea580c; }
.shop-price-tag.facebook { background: #dbeafe; color: #2563eb; }
.shop-price-tag.trademe { background: #d1fae5; color: #059669; }
.shop-price-tag.other { background: #f3e8ff; color: #9333ea; }

.shop-badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid;
  white-space: nowrap;
}

/* ===== Dimension Inputs ===== */
.dim-group {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.dim-group > label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}
.dim-inputs {
  display: flex;
  align-items: center;
  gap: 6px;
}
.dim-input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  text-align: center;
  background: var(--card);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
.dim-input:focus { border-color: var(--primary); }
.dim-x {
  color: var(--text-muted);
  font-size: 14px;
  flex-shrink: 0;
}

/* ===== Product Detail ===== */
.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
}
.detail-section {
  grid-column: 1 / -1;
  margin-bottom: 4px;
}
.detail-section-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  padding: 10px 0 6px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.detail-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 0;
  font-size: 13px;
}
.detail-label { color: var(--text-muted); }
.detail-value { font-weight: 500; text-align: right; }
.detail-value.positive { color: var(--success); }
.detail-value.negative { color: var(--danger); }
.detail-specs {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  padding: 6px 0;
}
.detail-spec-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.detail-spec-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
}
.detail-spec-value {
  font-size: 15px;
  font-weight: 600;
}
.detail-price-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 4px 0;
}

/* ===== Shop Management ===== */
.shop-list-group {
  margin-bottom: 16px;
}
.shop-list-group-title {
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 6px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.shop-list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.shop-list-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.shop-list-name {
  font-weight: 500;
  font-size: 14px;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--text);
  color: #fff;
  padding: 10px 24px;
  border-radius: var(--radius);
  font-size: 14px;
  z-index: 300;
  opacity: 0;
  transition: all 0.3s;
  pointer-events: none;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .navbar { padding: 0 12px; gap: 12px; flex-wrap: wrap; height: auto; padding-top: 8px; padding-bottom: 8px; }
  .nav-brand { font-size: 14px; }
  .nav-tab { padding: 6px 10px; font-size: 13px; }
  .container { padding: 16px; }
  .metrics-grid { grid-template-columns: 1fr 1fr; }
  .charts-grid, .lists-grid, .stats-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .page-header { flex-direction: column; align-items: stretch; }
  .search-input { width: 100%; min-width: 0; }
  .table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .data-table { min-width: 600px; font-size: 13px; }
  .data-table th, .data-table td { padding: 6px 8px; white-space: nowrap; }
  .data-table td:first-child, .data-table th:first-child { white-space: normal; min-width: 100px; }
  .modal-content { width: 95%; max-height: 90vh; overflow-y: auto; margin: 10px; }
  .form-grid { gap: 8px; }
  .metric-card { padding: 12px; }
  .metric-value { font-size: 20px; }
  .nav-tabs { overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
  .nav-tab { flex-shrink: 0; }
}

@media (max-width: 480px) {
  .metrics-grid { grid-template-columns: 1fr; }
  .nav-brand { font-size: 12px; }
  .nav-tab { padding: 5px 8px; font-size: 12px; }
  .container { padding: 10px; }
  .data-table { font-size: 12px; min-width: 500px; }
  .data-table th, .data-table td { padding: 4px 6px; }
  .btn-icon { font-size: 16px; padding: 4px 6px; }
}
