:root {
  --bg: #eef2f8;
  --surface: #ffffff;
  --text: #121826;
  --hint: #667085;
  --accent: #0a5ec2;
  --accent-dark: #084a9c;
  --accent-text: #ffffff;
  --danger: #e1232c;
  --danger-bg: #fdecee;
  --border: #dde3ec;
  --border-strong: #c7cedb;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(18, 24, 38, 0.04);
}

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.app { display: flex; flex-direction: column; min-height: 100vh; }

/* ---------- Header ---------- */

.topbar {
  padding: 14px 14px 10px;
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid var(--border);
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.topbar h1 {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.2px;
  color: var(--accent-dark);
}

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

.gear-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--hint);
}
.gear-btn:active { background: var(--bg); }

.date-btn {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}
.date-btn svg { color: var(--hint); flex-shrink: 0; }
.date-btn input[type="date"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  border: none;
  cursor: pointer;
}

.segmented {
  display: flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
}
.seg-btn {
  flex: 1;
  padding: 8px 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--hint);
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s, color 0.15s;
}
.seg-btn.active {
  background: var(--accent);
  color: var(--accent-text);
}

/* ---------- Layout ---------- */

main { flex: 1; padding: 12px 14px 100px; max-width: 640px; margin: 0 auto; width: 100%; }

.tab-panel { display: none; animation: fade-in 0.18s ease; }
.tab-panel.active { display: block; }

@keyframes fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.hint { color: var(--hint); font-size: 13px; line-height: 1.5; margin: 2px 0 14px; }

/* ---------- Stats row ---------- */

.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow);
}
.stat-tile .stat-label {
  font-size: 11px;
  color: var(--hint);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.stat-tile .stat-value {
  font-size: 18px;
  font-weight: 700;
  margin-top: 2px;
  letter-spacing: -0.2px;
  color: var(--accent-dark);
}
.stats-row-2 { grid-template-columns: repeat(2, 1fr); }

/* ---------- Settings save bar ---------- */

.save-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 10px 10px 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.save-bar-status {
  font-size: 12px;
  color: var(--hint);
  font-weight: 600;
}
.save-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--border);
  color: var(--hint);
  font-size: 13px;
  font-weight: 700;
  padding: 9px 16px;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}
.save-btn.dirty { background: var(--accent); color: var(--accent-text); }
.save-btn:disabled { opacity: 0.6; }

/* ---------- Toolbar ---------- */

.toolbar { display: flex; gap: 8px; margin-bottom: 10px; }

.search-box {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.search-box svg { color: var(--hint); flex-shrink: 0; }
.search-box input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  padding: 10px 0;
  min-width: 0;
}
.search-box input:focus { outline: none; }
.search-box input::-webkit-search-cancel-button { -webkit-appearance: none; }

.search-clear {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: var(--border);
  color: var(--hint);
}
.search-clear[hidden] { display: none; }

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border: 1px solid var(--accent-dark);
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-text);
}
.icon-btn:active { opacity: 0.85; }

/* ---------- Brand chips ---------- */

.chip-row {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 10px;
  margin-bottom: 4px;
  scrollbar-width: none;
}
.chip-row::-webkit-scrollbar { display: none; }

.chip {
  flex-shrink: 0;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--hint);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.chip.active { background: var(--accent); border-color: var(--accent-dark); color: var(--accent-text); }

/* ---------- Brand groups & product cards ---------- */

.brand-group { margin-bottom: 10px; border-radius: var(--radius); overflow: hidden; }

.brand-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 10px 4px;
  border: none;
  background: transparent;
  color: var(--text);
}
.brand-header .brand-name {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--accent-dark);
}
.brand-header .brand-count {
  font-size: 11px;
  color: var(--hint);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 8px;
  margin-left: 6px;
}
.brand-header .chevron {
  color: var(--hint);
  transition: transform 0.18s;
  flex-shrink: 0;
}
.brand-header.collapsed .chevron { transform: rotate(-90deg); }
.brand-header-left { display: flex; align-items: center; gap: 0; }

.brand-cards { display: flex; flex-direction: column; gap: 8px; }
.brand-cards.collapsed { display: none; }

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

.product-name { font-size: 14px; font-weight: 600; line-height: 1.35; margin-bottom: 2px; }
.product-sku { color: var(--hint); font-size: 11px; margin-bottom: 8px; }

.badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  margin-left: 6px;
  vertical-align: middle;
}
.badge.warn { background: var(--danger-bg); color: var(--danger); }

.stepper-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }

.stepper {
  display: flex;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  flex: 1;
}
.stepper button {
  width: 26px;
  height: 28px;
  flex-shrink: 0;
  border: none;
  background: transparent;
  color: var(--accent-dark);
  font-size: 14px;
  font-weight: 700;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.stepper button:active { background: #e7f0fc; }

.stepper input {
  flex: 1;
  min-width: 0;
  text-align: center;
  border: none;
  border-left: 1px solid var(--border-strong);
  border-right: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  padding: 6px 0;
}
.stepper input:focus { outline: none; background: #f2f7fe; }
.stepper-label { font-size: 12px; color: var(--hint); width: 56px; flex-shrink: 0; }

.result-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.result-chip {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 9px;
  font-size: 12px;
  color: var(--hint);
  flex-shrink: 0;
}
.result-chip b { color: var(--accent-dark); font-weight: 700; }

/* ---------- Settings cards ---------- */

.settings-row { display: flex; gap: 8px; margin-top: 10px; }
.settings-row > div { flex: 1; }
.settings-row label { display: block; font-size: 11px; color: var(--hint); margin-bottom: 4px; font-weight: 600; }
.settings-row input {
  width: 100%;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}
.settings-row input:focus { outline: none; border-color: var(--accent); background: #f2f7fe; }

.settings-preview {
  margin-top: 10px;
  font-size: 12px;
  color: var(--hint);
}
.settings-preview b { color: var(--accent-dark); }

.empty-state {
  text-align: center;
  padding: 50px 20px;
  color: var(--hint);
  font-size: 13px;
}

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

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(18, 24, 38, 0.45);
  z-index: 50;
  display: flex;
  align-items: flex-end;
}
.modal-overlay[hidden] { display: none; }

.modal-panel {
  width: 100%;
  max-height: 85vh;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: modal-up 0.18s ease;
}
@keyframes modal-up {
  from { transform: translateY(16px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h2 { font-size: 16px; font-weight: 700; margin: 0; color: var(--accent-dark); }

.modal-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 999px;
  background: var(--bg);
  color: var(--hint);
}

.modal-body { padding: 12px 14px calc(16px + env(safe-area-inset-bottom)); overflow-y: auto; }
