:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #111827;
  --muted: #667085;
  --line: #e5e7eb;
  --accent: #111827;
  --accent-soft: #eef2f7;
  --good: #0f766e;
  --good-soft: #ecfdf5;
  --danger: #b42318;
  --danger-soft: #fef3f2;
  --warning: #92400e;
  --warning-soft: #fffbeb;
  --shadow: 0 8px 30px rgba(15, 23, 42, 0.06);
  --radius: 18px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 250px 1fr; }
.sidebar {
  background: #0f172a;
  color: #fff;
  padding: 24px 18px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 0 6px; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 13px; display: grid; place-items: center;
  background: #fff; color: #111827; font-weight: 800; font-size: 22px;
}
.brand-title { font-weight: 800; font-size: 18px; }
.brand-subtitle { font-size: 12px; color: #94a3b8; margin-top: 3px; }
.nav { display: grid; gap: 7px; }
.nav-item {
  border: 0; background: transparent; color: #cbd5e1; border-radius: 12px;
  padding: 12px 14px; text-align: right; display: flex; gap: 10px; align-items: center;
}
.nav-item:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav-item.active { background: #fff; color: #0f172a; font-weight: 700; }
.sidebar-note {
  margin-top: auto; border: 1px solid rgba(255,255,255,.12); border-radius: 14px;
  padding: 14px; display: grid; gap: 5px; color: #cbd5e1; font-size: 12px;
}
.sidebar-note strong { color: #fff; }

.main { min-width: 0; padding: 28px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 24px; }
.topbar h1 { margin: 0; font-size: 28px; }
.topbar p { margin: 5px 0 0; color: var(--muted); }
.primary-btn, .secondary-btn, .danger-btn, .file-btn {
  border-radius: 11px; padding: 10px 16px; border: 1px solid transparent; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.primary-btn { background: var(--accent); color: #fff; }
.primary-btn:hover { opacity: .9; }
.secondary-btn, .file-btn { background: #fff; color: var(--text); border-color: var(--line); }
.danger-btn { background: var(--danger-soft); color: var(--danger); border-color: #fecdca; }

.view { display: none; }
.view.active { display: block; }
.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 16px; }
.stat-card, .panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
.stat-card { padding: 20px; display: grid; gap: 7px; min-height: 135px; align-content: center; }
.stat-card.accent { background: #111827; color: #fff; }
.stat-card.accent .stat-label, .stat-card.accent .stat-meta { color: #cbd5e1; }
.stat-label { font-size: 13px; color: var(--muted); }
.stat-value { font-size: 30px; line-height: 1; }
.stat-value.small { font-size: 20px; line-height: 1.25; }
.stat-meta { font-size: 12px; color: var(--muted); }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.panel { padding: 20px; margin-bottom: 16px; }
.panel-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; margin-bottom: 18px; }
.panel-head h2 { margin: 0; font-size: 18px; }
.panel-head p { margin: 5px 0 0; font-size: 13px; color: var(--muted); }
.empty-state { color: var(--muted); font-size: 14px; }
.empty-state.padded { padding: 28px 0; text-align: center; }

.ranking { display: grid; gap: 13px; }
.rank-row { display: grid; grid-template-columns: minmax(120px, 1fr) 2fr auto; gap: 12px; align-items: center; }
.rank-name strong { display: block; font-size: 14px; }
.rank-name span { color: var(--muted); font-size: 12px; }
.rank-track, .bar-track { height: 9px; background: #eef2f7; border-radius: 999px; overflow: hidden; }
.rank-fill, .bar-fill { height: 100%; background: #111827; border-radius: inherit; }
.rank-score { font-size: 12px; color: var(--muted); white-space: nowrap; }

.recommendations { display: grid; gap: 10px; }
.recommendation {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 13px; padding: 13px 14px;
}
.recommendation strong { display: block; margin-bottom: 5px; }
.recommendation span { color: var(--muted); font-size: 13px; }
.recommendation .saving { color: var(--good); font-weight: 700; }

.bar-list { display: grid; gap: 12px; }
.bar-row { display: grid; grid-template-columns: minmax(150px, 1fr) 3fr auto; align-items: center; gap: 14px; }
.bar-name { font-size: 13px; }
.bar-value { font-weight: 700; font-size: 13px; white-space: nowrap; }

.toolbar-row { display: flex; gap: 10px; margin-bottom: 16px; }
.search-wrap { flex: 1; }
input, select, textarea {
  width: 100%; border: 1px solid #d0d5dd; background: #fff; color: var(--text);
  border-radius: 10px; padding: 10px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: #667085; box-shadow: 0 0 0 3px rgba(17,24,39,.06); }
textarea { resize: vertical; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { color: var(--muted); font-weight: 600; text-align: right; border-bottom: 1px solid var(--line); padding: 11px 10px; white-space: nowrap; }
td { border-bottom: 1px solid #f0f2f5; padding: 13px 10px; vertical-align: middle; }
.badge { display: inline-flex; border-radius: 999px; padding: 4px 8px; background: var(--accent-soft); font-size: 11px; }
.row-actions { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.edit-btn { border: 0; background: var(--accent-soft); color: var(--text); padding: 6px 9px; border-radius: 8px; font-size: 12px; font-weight: 700; }
.edit-btn:hover { background: #e2e8f0; }
.icon-btn { border: 0; background: transparent; color: var(--muted); padding: 6px; border-radius: 8px; }
.icon-btn:hover { background: var(--danger-soft); color: var(--danger); }
.hidden { display: none !important; }

.form-panel { max-width: 900px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.field { display: grid; gap: 7px; }
.field > span { font-size: 13px; font-weight: 700; }
.span-2 { grid-column: span 2; }
.form-actions { display: flex; gap: 10px; padding-top: 4px; }


.barcode-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.field-help { color: var(--muted); font-size: 12px; font-weight: 400; }
.barcode-lookup {
  border: 1px solid var(--line); border-radius: 14px; background: var(--surface-2);
  padding: 14px; display: grid; gap: 10px;
}
.barcode-summary { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.barcode-summary strong { display: block; margin-bottom: 3px; }
.barcode-summary .meta { color: var(--muted); font-size: 12px; }
.lookup-prices { display: grid; gap: 7px; max-height: 310px; overflow: auto; }
.lookup-price-row {
  display: grid; grid-template-columns: minmax(0,1fr) auto auto; gap: 10px; align-items: center;
  background: #fff; border: 1px solid var(--line); border-radius: 11px; padding: 9px 10px;
}
.lookup-price-row.cheapest { border-color: #a6f4c5; background: var(--good-soft); }
.lookup-store { min-width: 0; }
.lookup-store strong { display: block; font-size: 13px; }
.lookup-store span { display: block; color: var(--muted); font-size: 11px; margin-top: 2px; }
.lookup-price { font-weight: 800; white-space: nowrap; }
.lookup-use { padding: 7px 9px; font-size: 11px; }
.lookup-message { color: var(--muted); font-size: 13px; }
.lookup-saving { color: var(--good); background: var(--good-soft); border: 1px solid #a6f4c5; border-radius: 10px; padding: 9px 10px; font-size: 13px; font-weight: 700; }

.api-search-row { display: grid; grid-template-columns: 2fr 1fr auto; gap: 10px; }
.status-pill { padding: 6px 10px; border-radius: 999px; background: var(--good-soft); color: var(--good); font-size: 11px; font-weight: 700; }
.api-hint { margin-top: 10px; color: var(--muted); font-size: 12px; }
.api-results { margin-top: 18px; display: grid; gap: 10px; }
.api-product, .api-price-row {
  border: 1px solid var(--line); border-radius: 13px; padding: 13px 14px; background: var(--surface-2);
}
.api-product { display: flex; justify-content: space-between; align-items: center; gap: 14px; }
.api-product h3 { margin: 0 0 5px; font-size: 14px; }
.api-product p { margin: 0; color: var(--muted); font-size: 12px; }
.api-product button { white-space: nowrap; }
.api-price-row { display: grid; grid-template-columns: 1fr auto; gap: 12px; }
.api-price-row .store { font-weight: 700; }
.api-price-row .address { color: var(--muted); font-size: 12px; margin-top: 4px; }
.api-price-row .price { font-weight: 800; font-size: 18px; }
.api-back { margin-bottom: 4px; }
.cheapest { border-color: #a6f4c5; background: var(--good-soft); }

.danger-zone { display: flex; justify-content: space-between; align-items: center; gap: 20px; border-color: #fecdca; }
.danger-zone h2 { margin: 0 0 5px; font-size: 18px; }
.danger-zone p { margin: 0; color: var(--muted); }

.toast {
  position: fixed; left: 24px; bottom: 24px; background: #111827; color: #fff;
  border-radius: 12px; padding: 11px 15px; box-shadow: var(--shadow); opacity: 0;
  transform: translateY(8px); pointer-events: none; transition: .2s ease; z-index: 50;
}
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1050px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .two-col { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; padding: 16px; }
  .nav { grid-template-columns: repeat(5, minmax(0,1fr)); gap: 5px; }
  .nav-item { justify-content: center; padding: 10px 7px; font-size: 18px; }
  .nav-item span { display: none; }
  .sidebar-note { display: none; }
  .main { padding: 16px; }
  .topbar { align-items: flex-start; }
  .topbar h1 { font-size: 24px; }
  .topbar p { font-size: 13px; }
  .topbar .primary-btn { padding: 9px 11px; font-size: 13px; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .stat-card { min-height: 120px; padding: 16px; }
  .stat-value { font-size: 25px; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: span 1; }
  .api-search-row, .barcode-row { grid-template-columns: 1fr; }
  .lookup-price-row { grid-template-columns: 1fr auto; }
  .lookup-use { grid-column: 1 / -1; }
  .toolbar-row { flex-direction: column; }
  .rank-row, .bar-row { grid-template-columns: 1fr; gap: 6px; }
  .danger-zone { flex-direction: column; align-items: stretch; }
}

/* V1.7 area filters */
.area-filter-row {
  margin-top: 10px;
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 10px;
}
.distance-pill {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 3px 8px;
  margin-inline-start: 6px;
  background: #eef2f7;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}
.barcode-name-only {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.barcode-name-only strong { font-size: 15px; }
.barcode-name-only .source { color: var(--muted); font-size: 11px; }

@media (max-width: 760px) {
  .area-filter-row { grid-template-columns: 1fr; }
}

/* V1.8 smart memory */
.smart-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.smart-suggestions:empty { display: none; }
.smart-chip {
  border: 1px solid var(--border, #d7dde6);
  background: #f7f9fc;
  color: #172033;
  border-radius: 999px;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}
.smart-chip:hover { background: #eef2f7; }
.smart-chip small { color: #6b778c; }
.memory-note {
  margin-top: 6px;
  color: #6b778c;
  font-size: 0.82rem;
}

/* V2.0 accounts */
.sidebar-user {
  margin-top: auto; border: 1px solid rgba(255,255,255,.12); border-radius: 14px;
  padding: 12px 14px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  color: #cbd5e1; font-size: 13px;
}
.sidebar-user strong { color: #fff; display: block; }
.sidebar-user + .sidebar-note { margin-top: 0; }
.logout-btn {
  border: 1px solid rgba(255,255,255,.2); background: transparent; color: #fff;
  border-radius: 9px; padding: 6px 10px; font-size: 12px; white-space: nowrap;
}
.logout-btn:hover { background: rgba(255,255,255,.08); }
.invite-box {
  margin-top: 16px; border: 1px solid #a6f4c5; background: var(--good-soft);
  border-radius: 14px; padding: 14px; display: grid; gap: 10px; font-size: 13px;
}
.invite-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.invite-row input { direction: ltr; text-align: left; font-size: 12px; }
.family-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 13px; padding: 12px 14px;
}
.family-row strong { display: block; }
.family-row .muted, .muted { color: var(--muted); font-size: 12px; }
.family-list { display: grid; gap: 10px; }

.auth-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 400px; padding: 28px; margin: 0; display: grid; gap: 18px; }
.auth-brand { padding: 0; }
.auth-brand .brand-mark { background: #111827; color: #fff; }
.auth-brand .brand-subtitle { color: var(--muted); }
.auth-form { display: grid; gap: 14px; }
.auth-form h1 { margin: 0; font-size: 22px; }
.auth-text { margin: 0; font-size: 14px; }
.auth-message {
  border: 1px solid #fecdca; background: var(--danger-soft); color: var(--danger);
  border-radius: 10px; padding: 10px 12px; font-size: 13px;
}
.auth-note { margin: 0; color: var(--muted); font-size: 12px; text-align: center; }

@media (max-width: 760px) {
  .nav { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); }
  .sidebar-user { margin-top: 0; }
  .family-row, .invite-row { grid-template-columns: 1fr; flex-direction: column; align-items: stretch; }
}
