:root {
  --bg: #f3f6f5;
  --surface: #ffffff;
  --surface-2: #edf2f0;
  --text: #0f172a;
  --text-muted: #52606d;
  --border: #d9e2ec;
  --primary: #0f766e;
  --primary-hover: #0d625b;
  --primary-soft: #ccfbf1;
  --primary-text: #0f766e;
  --good: #059669;
  --good-soft: #d1fae5;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
  --info: #2563eb;
  --info-soft: #dbeafe;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
  --nav-h: 66px;
}

:root[data-theme="dark"] {
  --bg: #0b1215;
  --surface: #131f24;
  --surface-2: #1c2c33;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #263840;
  --primary: #14b8a6;
  --primary-hover: #2dd4bf;
  --primary-soft: rgba(20, 184, 166, 0.16);
  --primary-text: #5eead4;
  --good: #34d399;
  --good-soft: rgba(16, 185, 129, 0.16);
  --warn: #fbbf24;
  --warn-soft: rgba(245, 158, 11, 0.16);
  --bad: #f87171;
  --bad-soft: rgba(239, 68, 68, 0.16);
  --info: #60a5fa;
  --info-soft: rgba(59, 130, 246, 0.16);
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #0b1215;
    --surface: #131f24;
    --surface-2: #1c2c33;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --border: #263840;
    --primary: #14b8a6;
    --primary-hover: #2dd4bf;
    --primary-soft: rgba(20, 184, 166, 0.16);
    --primary-text: #5eead4;
    --good: #34d399;
    --good-soft: rgba(16, 185, 129, 0.16);
    --warn: #fbbf24;
    --warn-soft: rgba(245, 158, 11, 0.16);
    --bad: #f87171;
    --bad-soft: rgba(239, 68, 68, 0.16);
    --info: #60a5fa;
    --info-soft: rgba(59, 130, 246, 0.16);
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  }
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
}

.num, .money {
  font-variant-numeric: tabular-nums;
}

.app-shell {
  max-width: 720px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 12px);
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
  box-shadow: var(--shadow);
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  border-radius: 8px;
}

.brand-text {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.brand-name {
  font-weight: 800;
  font-size: 1.1rem;
  letter-spacing: -0.02em;
}

.sync-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 0.74rem;
  font-weight: 600;
  cursor: pointer;
}

.sync-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
}

.sync-pill.sync-synced {
  color: var(--good);
  background: var(--good-soft);
  border-color: transparent;
}
.sync-pill.sync-synced .sync-dot { background: var(--good); }

.sync-pill.sync-connecting {
  color: var(--info);
  background: var(--info-soft);
  border-color: transparent;
}
.sync-pill.sync-connecting .sync-dot { background: var(--info); }

.sync-pill.sync-offline {
  color: var(--warn);
  background: var(--warn-soft);
  border-color: transparent;
}
.sync-pill.sync-offline .sync-dot { background: var(--warn); }

.sync-pill.sync-error {
  color: var(--bad);
  background: var(--bad-soft);
  border-color: transparent;
}
.sync-pill.sync-error .sync-dot { background: var(--bad); }

.header-actions {
  display: flex;
  gap: 6px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1.05rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.header-store-bar {
  margin-top: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.store-bar-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

.store-bar-select {
  flex: 1;
  min-width: 0;
  height: 36px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 0 10px;
  font-size: 0.9rem;
  font-weight: 600;
}

/* Main Content */
.app-main {
  flex: 1;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Cards & Common */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

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

.card-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.muted {
  color: var(--text-muted);
  font-size: 0.86rem;
}

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

.row-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.wrap {
  flex-wrap: wrap;
}

/* Buttons & Inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}

.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

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

.btn-good {
  background: var(--good);
  border-color: var(--good);
  color: #ffffff;
}

.btn-danger {
  background: var(--bad-soft);
  border-color: transparent;
  color: var(--bad);
}

.btn-sm {
  min-height: 32px;
  padding: 0 10px;
  font-size: 0.82rem;
  border-radius: 8px;
}

.btn-block {
  width: 100%;
}

.input, .select, .textarea {
  width: 100%;
  min-height: 42px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 8px 12px;
  font-size: 0.96rem;
  font-family: inherit;
}

.input:focus, .select:focus, .textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
  background: var(--surface);
}

.textarea {
  min-height: 90px;
  resize: vertical;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 10px;
}

.field-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
}

/* Badges & Chips */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  white-space: nowrap;
}

.badge-good { background: var(--good-soft); color: var(--good); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-bad { background: var(--bad-soft); color: var(--bad); }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-neutral { background: var(--surface-2); color: var(--text-muted); }

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

/* Budget Card */
.budget-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.budget-stat {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 8px 10px;
}

.budget-stat-label {
  font-size: 0.74rem;
  color: var(--text-muted);
  font-weight: 600;
}

.budget-stat-val {
  font-size: 1.02rem;
  font-weight: 800;
  margin-top: 2px;
}

.progress-track {
  height: 9px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--primary);
  transition: width 0.25s ease;
}

.progress-fill.over { background: var(--bad); }
.progress-fill.warn { background: var(--warn); }

/* Quick Add Bar */
.quick-add-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.quick-add-qty-row {
  display: grid;
  grid-template-columns: 95px 1fr auto;
  gap: 8px;
}

/* Shopping List Items */
.sl-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sl-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
}

.sl-item.checked {
  opacity: 0.82;
  background: var(--surface-2);
}

.sl-check-btn {
  width: 28px;
  height: 28px;
  min-width: 28px;
  border-radius: 8px;
  border: 2px solid var(--primary);
  background: transparent;
  color: #ffffff;
  font-weight: 800;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.sl-item.checked .sl-check-btn {
  background: var(--good);
  border-color: var(--good);
}

.sl-body {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.sl-title {
  font-weight: 700;
  font-size: 0.98rem;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.sl-item.checked .sl-title-name {
  text-decoration: line-through;
  color: var(--text-muted);
}

.sl-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.sl-price-col {
  text-align: right;
  cursor: pointer;
}

.sl-price-main {
  font-weight: 800;
  font-size: 0.96rem;
}

.sl-del-btn {
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.05rem;
  cursor: pointer;
  border-radius: 8px;
}

/* Calculator */
.calc-big-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.calc-big-input {
  width: 100%;
  height: 54px;
  font-size: 1.45rem;
  font-weight: 800;
  padding: 8px 42px 8px 14px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.calc-big-input:focus {
  outline: none;
  border-color: var(--primary);
  background: var(--surface);
}

.input-clear-btn {
  position: absolute;
  right: 8px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: var(--border);
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.calc-qty-grid {
  display: grid;
  grid-template-columns: 1fr 145px;
  gap: 8px;
}

.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
}

.segmented button {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-weight: 700;
  font-size: 0.84rem;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
}

.segmented button.active {
  background: var(--primary);
  color: #ffffff;
}

.ref-banner {
  background: var(--surface-2);
  border-radius: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
}

.hero-result {
  border-radius: 14px;
  padding: 14px;
  background: var(--primary-soft);
  border: 1px solid transparent;
}

.hero-result.good { background: var(--good-soft); }
.hero-result.bad { background: var(--bad-soft); }
.hero-result.warn { background: var(--warn-soft); }

.hero-result-kicker {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.hero-result-value {
  font-size: 1.45rem;
  font-weight: 900;
  margin: 4px 0;
  line-height: 1.2;
}

/* Equivalents Grid */
.equiv-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

@media (min-width: 520px) {
  .equiv-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

.equiv-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 10px;
}

.equiv-card.is-active {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.equiv-card.is-approx {
  border-style: dashed;
}

.equiv-unit {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}

.equiv-amount {
  font-size: 1.05rem;
  font-weight: 800;
  margin-top: 2px;
}

.equiv-unit-price {
  font-size: 0.78rem;
  color: var(--primary-text);
  font-weight: 700;
  margin-top: 2px;
}

/* Store Comparison List */
.store-rank-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.store-rank-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 0.88rem;
}

.store-rank-row.best {
  background: var(--good-soft);
}

/* Charts */
.chart-summary-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.82rem;
  margin-bottom: 6px;
  flex-wrap: wrap;
}

.price-chart-svg {
  width: 100%;
  height: auto;
  display: block;
  overflow: visible;
}

.chart-grid-line {
  stroke: var(--border);
  stroke-width: 1;
}

.chart-axis-text {
  fill: var(--text-muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.chart-avg-line {
  stroke: var(--warn);
  stroke-width: 1.3;
  stroke-dasharray: 4 4;
}

.chart-min-line {
  stroke: var(--good);
  stroke-width: 1.3;
  stroke-dasharray: 2 3;
}

.chart-trend-line {
  stroke: var(--text-muted);
  stroke-width: 1.6;
  stroke-opacity: 0.55;
}

.chart-point { cursor: pointer; }

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.chart-legend-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
}

.chart-legend-dot.filled-demo {
  background: var(--text);
}

.chart-legend-dot.hollow-demo {
  border: 2px solid var(--text);
  background: transparent;
}

.chart-callout {
  margin-top: 8px;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 0.86rem;
}

.chart-callout-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.chart-empty {
  padding: 20px;
  text-align: center;
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: 10px;
  font-size: 0.88rem;
}

/* Bottom Navigation */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 720px;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  z-index: 40;
}

.nav-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  font-family: inherit;
}

.nav-btn.active {
  color: var(--primary-text);
  font-weight: 700;
}

.nav-icon { font-size: 1.25rem; }
.nav-label { font-size: 0.74rem; }

/* Modals / Bottom Sheets */
.modal-root {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(2, 6, 23, 0.58);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}

.modal-root.hidden {
  display: none;
}

@media (min-width: 600px) {
  .modal-root {
    align-items: center;
    padding: 16px;
  }
}

.modal-sheet {
  width: 100%;
  max-width: 640px;
  max-height: 92dvh;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 -6px 30px rgba(0, 0, 0, 0.25);
}

@media (min-width: 600px) {
  .modal-sheet {
    border-radius: 18px;
    max-height: 88dvh;
  }
}

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

.modal-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 800;
}

.modal-body {
  padding: 14px 16px 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: calc(var(--nav-h) + 14px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: min(92vw, 420px);
  pointer-events: none;
}

.toast {
  background: #0f172a;
  color: #f8fafc;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
}

.toast.toast-good { background: #065f46; }
.toast.toast-bad { background: #991b1b; }

.divider {
  height: 1px;
  background: var(--border);
  margin: 6px 0;
}

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

code.code-block {
  display: block;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  font-size: 0.76rem;
  overflow-x: auto;
  white-space: pre;
}
