*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --green: #16a34a;
  --green-light: #22c55e;
  --green-bg: #f0fdf4;
  --green-border: #86efac;
  --red: #dc2626;
  --orange: #ea580c;
  --blue: #2563eb;
  --purple: #7c3aed;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-800: #1f2937;
  --gray-900: #111827;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--gray-50);
  color: var(--gray-900);
  max-width: 680px;
  margin: 0 auto;
  padding: 0 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ── Header ─────────────────────────────────── */

header {
  background: linear-gradient(135deg, var(--gray-900) 0%, #374151 100%);
  color: #fff;
  margin: 0 -16px;
  padding: 28px 24px 20px;
}
header h1 {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -1.5px;
  line-height: 1;
}
.tagline {
  color: var(--green-light);
  font-size: 15px;
  font-weight: 500;
  margin-top: 4px;
}

/* ── Tabs ───────────────────────────────────── */

.tabs {
  display: flex;
  background: #fff;
  margin: 0 -16px;
  border-bottom: 1px solid var(--gray-200);
  position: sticky;
  top: 0;
  z-index: 10;
}
.tab {
  flex: 1;
  padding: 12px;
  border: none;
  background: none;
  font-size: 14px;
  font-weight: 600;
  color: var(--gray-400);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
}
.tab:hover { color: var(--gray-600); }
.tab.active { color: var(--gray-900); border-bottom-color: var(--green); }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ── Filter row ─────────────────────────────── */

.filter-row {
  padding: 14px 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.store-pills {
  display: flex;
  gap: 8px;
  white-space: nowrap;
}
.pill {
  padding: 7px 16px;
  border: 1px solid var(--gray-200);
  border-radius: 20px;
  background: #fff;
  font-size: 13px;
  font-weight: 500;
  color: var(--gray-600);
  cursor: pointer;
  transition: all 0.15s;
  flex-shrink: 0;
}
.pill:hover { border-color: var(--gray-400); }
.pill.active {
  background: var(--gray-900);
  color: #fff;
  border-color: var(--gray-900);
}

/* ── Category grid ──────────────────────────── */

.category-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  padding-bottom: 16px;
}

.category-card {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-lg);
  padding: 16px;
  cursor: pointer;
  transition: all 0.15s;
  position: relative;
  overflow: hidden;
}
.category-card:hover {
  border-color: var(--gray-300);
  box-shadow: var(--shadow);
  transform: translateY(-1px);
}
.category-card .emoji {
  font-size: 28px;
  margin-bottom: 8px;
  display: block;
}
.category-card .cat-name {
  font-weight: 600;
  font-size: 14px;
  color: var(--gray-900);
}
.category-card .cat-meta {
  font-size: 12px;
  color: var(--gray-500);
  margin-top: 2px;
}
.category-card .savings-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: var(--green-bg);
  color: var(--green);
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 6px;
}

/* ── Section header ─────────────────────────── */

.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 0 8px;
}
.section-header h2 { font-size: 18px; font-weight: 700; }
.back-btn {
  background: none;
  border: none;
  color: var(--gray-500);
  font-size: 14px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}
.back-btn:hover { background: var(--gray-100); color: var(--gray-800); }

.search-inline {
  margin-bottom: 12px;
}
.search-inline input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: 14px;
  background: #fff;
}
.search-inline input:focus { outline: none; border-color: var(--gray-400); }

/* ── Product grid ───────────────────────────── */

.product-grid { display: flex; flex-direction: column; gap: 8px; padding-bottom: 80px; }

.product-card {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
  transition: all 0.15s;
  box-shadow: var(--shadow-sm);
}
.product-card:hover { border-color: var(--gray-300); box-shadow: var(--shadow); }
.product-card.selected {
  border-color: var(--green);
  background: var(--green-bg);
  box-shadow: 0 0 0 1px var(--green);
}

.product-card .top-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.product-card .name { font-weight: 600; font-size: 14px; flex: 1; }
.product-card .price { font-weight: 700; font-size: 15px; white-space: nowrap; }
.product-card .price-after {
  font-size: 12px;
  color: var(--green);
  font-weight: 600;
  text-align: right;
}
.product-card .original-price {
  font-size: 12px;
  color: var(--gray-400);
  text-decoration: line-through;
}
.product-card .store-tag {
  display: inline-block;
  font-size: 11px;
  color: var(--gray-600);
  background: var(--gray-100);
  padding: 2px 8px;
  border-radius: 4px;
  margin-top: 6px;
}
.product-card .coupons { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 4px; }
.coupon-tag {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 5px;
  font-weight: 500;
}
.coupon-tag.mfr { background: #dbeafe; color: #1e40af; }
.coupon-tag.store { background: #fef3c7; color: #92400e; }
.coupon-tag.clipless { background: #e0e7ff; color: #3730a3; }
.coupon-tag.cashback { background: #ede9fe; color: #5b21b6; }
.coupon-tag.trigger { background: #fce7f3; color: #9d174d; }
.coupon-tag.bogo { background: #d1fae5; color: #065f46; font-weight: 700; }

.savings-bar {
  margin-top: 8px;
  height: 4px;
  background: var(--gray-100);
  border-radius: 2px;
  overflow: hidden;
}
.savings-bar .fill {
  height: 100%;
  border-radius: 2px;
  transition: width 0.3s;
}
.fill.low { background: var(--gray-300); }
.fill.medium { background: #fbbf24; }
.fill.high { background: var(--green-light); }
.fill.extreme { background: var(--green); }

/* ── Selected bar (sticky bottom) ───────────── */

.selected-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  border-top: 1px solid var(--gray-200);
  box-shadow: 0 -4px 12px rgba(0,0,0,0.08);
  z-index: 20;
  padding: 10px 16px;
  animation: slideUp 0.2s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

.selected-bar-inner {
  max-width: 680px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
}
.selected-chips {
  flex: 1;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.selected-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--green-bg);
  border: 1px solid var(--green-border);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
}
.selected-chip button {
  background: none;
  border: none;
  color: var(--gray-400);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0 2px;
}
.solve-btn {
  padding: 10px 20px;
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s;
}
.solve-btn:hover { background: #15803d; }
.badge {
  background: rgba(255,255,255,0.25);
  padding: 1px 7px;
  border-radius: 10px;
  font-size: 12px;
}

/* ── Script result ──────────────────────────── */

.script-result {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-lg);
  margin: 16px 0;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
}
.script-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  background: var(--gray-900);
  color: #fff;
}
.script-header-bar h2 { font-size: 16px; font-weight: 700; }
.close-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  opacity: 0.6;
  line-height: 1;
}
.close-btn:hover { opacity: 1; }

.script-summary {
  display: flex;
  padding: 16px 18px;
  background: var(--gray-50);
  border-bottom: 1px solid var(--gray-200);
  gap: 24px;
  flex-wrap: wrap;
}
.summary-stat { }
.summary-stat .label {
  font-size: 11px;
  color: var(--gray-500);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 500;
}
.summary-stat .value { font-size: 24px; font-weight: 800; line-height: 1.2; }
.summary-stat .value.savings { color: var(--green); }
.summary-stat .value.money-maker { color: var(--green); }

.script-text {
  padding: 18px;
  font-size: 13px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  color: var(--gray-800);
}

/* ── Money Makers ───────────────────────────── */

.mm-header { padding: 16px 0 12px; }
.mm-header h2 { font-size: 20px; font-weight: 800; }
.mm-subtitle { color: var(--gray-500); font-size: 13px; }
.mm-list { display: flex; flex-direction: column; gap: 12px; padding-bottom: 16px; }

.mm-card {
  background: #fff;
  border: 1px solid var(--green-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.mm-card-header {
  background: linear-gradient(135deg, var(--green-bg) 0%, #dcfce7 100%);
  padding: 12px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.mm-badge {
  background: var(--green);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 5px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.mm-profit { font-size: 22px; font-weight: 800; color: var(--green); }
.mm-card-body { padding: 12px 14px; }
.mm-products { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.mm-store { font-size: 13px; color: var(--gray-500); }
.mm-expand {
  background: none;
  border: none;
  color: var(--green);
  font-size: 13px;
  cursor: pointer;
  padding: 6px 0;
  font-weight: 600;
}
.mm-script { display: none; }
.mm-script.open { display: block; }
.mm-script pre {
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  padding: 12px;
  background: var(--gray-50);
  border-radius: 8px;
  margin-top: 8px;
  font-family: "SF Mono", "Fira Code", monospace;
}

/* ── States ─────────────────────────────────── */

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--gray-400);
  font-size: 14px;
}
.loading {
  text-align: center;
  padding: 40px 20px;
  color: var(--gray-500);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Freshness banner ───────────────────────── */

.freshness-banner {
  margin: 0 -16px;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  text-align: center;
}
.freshness-banner.aging {
  background: #fef3c7;
  color: #92400e;
  border-bottom: 1px solid #fde68a;
}
.freshness-banner.stale {
  background: #fef2f2;
  color: #991b1b;
  border-bottom: 1px solid #fecaca;
}

/* ── Footer ─────────────────────────────────── */

footer {
  text-align: center;
  padding: 20px 0;
  color: var(--gray-400);
  font-size: 12px;
  border-top: 1px solid var(--gray-200);
  margin-top: 24px;
}
footer a { color: var(--gray-600); text-decoration: none; }
.footer-brand { margin-top: 4px; font-weight: 600; }

/* ── Print ──────────────────────────────────── */

@media print {
  header, .tabs, .filter-row, .category-grid, .product-section,
  .selected-bar, footer, .close-btn, .script-header-bar { display: none !important; }
  .script-result { border: none; margin: 0; box-shadow: none; }
}

/* ── Mobile tweaks ──────────────────────────── */

@media (max-width: 400px) {
  .category-grid { grid-template-columns: 1fr; }
  .script-summary { gap: 16px; }
  .summary-stat .value { font-size: 20px; }
}
