:root {
  --blue: #1a56a0;
  --blue-light: #e8f0fe;
  --green: #065f46;
  --green-light: #d1fae5;
  --orange: #d97706;
  --orange-light: #fef3c7;
  --red: #b91c1c;
  --red-light: #fee2e2;
  --ink: #111827;
  --ink-mid: #6b7280;
  --ink-light: #9ca3af;
  --bg: #f9fafb;
  --border: #e5e7eb;
  --card: #ffffff;
  --radius: 12px;
  --radius-sm: 6px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ─────────────────────────────────────────────────────────────────── */
#app { max-width: 480px; margin: 0 auto; padding-bottom: 80px; }

.screen { display: none; padding: 0 16px; }
.screen.active { display: block; }

/* ── Nav ─────────────────────────────────────────────────────────────────────── */
nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--card);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  padding: 8px 0 max(8px, env(safe-area-inset-bottom));
  z-index: 100;
}

nav button {
  background: none; border: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--ink-mid); font-size: 10px; padding: 4px 12px;
  border-radius: var(--radius-sm); transition: color .15s;
}
nav button.active { color: var(--blue); }
nav button svg { width: 22px; height: 22px; }

/* ── Header ─────────────────────────────────────────────────────────────────── */
.page-header {
  position: sticky; top: 0; z-index: 50;
  background: var(--bg);
  padding: 16px 0 12px;
  display: flex; align-items: center; gap: 10px;
}
.page-header h1 { font-size: 22px; font-weight: 700; }

/* ── Cards ─────────────────────────────────────────────────────────────────── */
.card {
  background: var(--card); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px;
  border: 1px solid var(--border);
}

/* ── Upload zone ─────────────────────────────────────────────────────────────── */
.upload-zone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 32px 16px; text-align: center; cursor: pointer;
  transition: border-color .2s, background .2s;
  background: var(--card);
}
.upload-zone:hover, .upload-zone.drag-over {
  border-color: var(--blue); background: var(--blue-light);
}
.upload-zone .icon { font-size: 40px; margin-bottom: 8px; }
.upload-zone p { color: var(--ink-mid); font-size: 14px; }
.upload-zone strong { color: var(--blue); }

/* ── Camera button ────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 10px 20px; border-radius: var(--radius-sm); border: none;
  font-size: 14px; font-weight: 500; cursor: pointer;
  transition: opacity .15s;
}
.btn:active { opacity: .7; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-secondary { background: var(--bg); color: var(--ink); border: 1px solid var(--border); }
.btn-danger { background: var(--red); color: #fff; }
.btn-full { width: 100%; justify-content: center; }

/* ── Receipt list ────────────────────────────────────────────────────────────── */
.receipt-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 0; cursor: pointer;
  border-bottom: 1px solid var(--border);
  background: var(--card);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.receipt-row:last-child { border-bottom: none; }
.receipt-row-right {
  display: flex; align-items: center; gap: 6px; margin-left: auto; flex-shrink: 0;
}
.receipt-row-menu-wrap { position: relative; }
.row-dot-btn {
  background: none; border: none; cursor: pointer;
  font-size: 18px; padding: 4px 8px; color: var(--ink-light);
  border-radius: 4px; line-height: 1; -webkit-tap-highlight-color: transparent;
}
.row-dot-btn:active { background: var(--bg); }
.row-dot-menu {
  position: absolute; right: 0; top: 100%;
  background: #fff; border: 1px solid var(--border);
  border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.12);
  min-width: 160px; z-index: 200; overflow: hidden;
}
.row-dot-menu button {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 12px 16px; border: none; background: none;
  color: var(--red); font-size: 14px; cursor: pointer; text-align: left;
}
.row-dot-menu button:active { background: var(--red-light); }
.receipt-icon {
  width: 40px; height: 40px; border-radius: 10px;
  background: var(--blue-light); display: flex; align-items: center; justify-content: center;
  font-size: 18px; flex-shrink: 0;
}
.receipt-meta { flex: 1; min-width: 0; }
.receipt-meta .merchant { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.receipt-meta .date { font-size: 12px; color: var(--ink-mid); margin-top: 2px; }
.receipt-amount { font-weight: 600; font-size: 15px; }

/* ── Line items table ────────────────────────────────────────────────────────── */
.line-items-table { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
.line-items-table th { text-align: left; padding: 6px 4px; color: var(--ink-mid); font-weight: 500; border-bottom: 1px solid var(--border); }
.line-items-table td { padding: 7px 4px; vertical-align: top; }
.li-row td { border-top: 1px solid var(--border); }
.li-desc-cell { overflow: hidden; max-width: 0; }
.li-qty { text-align: center; white-space: nowrap; color: var(--ink-mid); vertical-align: top; }
.li-price { text-align: right; white-space: nowrap; }
.li-total { text-align: right; white-space: nowrap; padding-right: 12px !important; }
.item-desc { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-canonical { font-size: 11px; color: var(--blue); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-category { font-size: 11px; color: var(--ink-mid); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Processing row ──────────────────────────────────────────────────────────── */
.processing-row { cursor: default; opacity: 0.8; }
.processing-row .receipt-meta .merchant { color: var(--orange); }

/* ── Deal rows ───────────────────────────────────────────────────────────────── */
.li-deal-row td, .li-final-row td { padding: 3px 4px; }
.deal-tag {
  background: var(--green-light); color: var(--green);
  border-radius: 4px; padding: 1px 7px; font-size: 11px; font-weight: 600;
}
.coupon-tag { background: var(--blue-light); color: var(--blue); }
.deal-saving { color: var(--green); font-weight: 600; }
.price-original { text-decoration: line-through; color: var(--ink-light); }
.price-final { font-weight: 600; }

/* ── Receipt charges ─────────────────────────────────────────────────────────── */
.charge-row td { padding: 8px 4px; border-top: 1px solid var(--border); }
.charge-type-tag {
  border-radius: 4px; padding: 1px 6px; font-size: 11px; font-weight: 600;
}
.charge-credit { background: var(--green-light); color: var(--green); }
.charge-debit { background: var(--orange-light); color: var(--orange); }
.charge-amount { color: var(--orange); font-weight: 600; }
.receipt-total-row td { border-top: 2px solid var(--border); }

/* ── Flags ───────────────────────────────────────────────────────────────────── */
.flag-orange { color: var(--orange); background: var(--orange-light); border-radius: 4px; padding: 1px 5px; font-size: 11px; }
.flag-red { color: var(--red); background: var(--red-light); border-radius: 4px; padding: 1px 5px; font-size: 11px; }
.flag-green { color: var(--green); background: var(--green-light); border-radius: 4px; padding: 1px 5px; font-size: 11px; }

/* ── Validation summary ─────────────────────────────────────────────────────── */
.validation-bar {
  border-radius: var(--radius-sm); padding: 10px 14px; margin-bottom: 12px;
  font-size: 13px; display: flex; align-items: center; gap: 8px;
}
.validation-bar.ok { background: var(--green-light); color: var(--green); }
.validation-bar.warn { background: var(--orange-light); color: var(--orange); }
.validation-bar.err { background: var(--red-light); color: var(--red); }

/* ── Saving badge ─────────────────────────────────────────────────────────────── */
.saving-badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; font-weight: 600; color: var(--green);
  background: var(--green-light); border-radius: 4px; padding: 2px 6px;
}

/* ── Loading ─────────────────────────────────────────────────────────────────── */
.spinner {
  width: 24px; height: 24px; border: 3px solid var(--border);
  border-top-color: var(--blue); border-radius: 50%;
  animation: spin .7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.35);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  z-index: 999; gap: 12px; color: #fff; font-size: 14px;
}

/* ── Auth screen ─────────────────────────────────────────────────────────────── */
.auth-container {
  max-width: 360px; margin: 60px auto; padding: 0 16px;
}
.auth-logo { text-align: center; margin-bottom: 32px; }
.auth-logo h1 { font-size: 26px; font-weight: 700; color: var(--blue); }
.auth-logo p { color: var(--ink-mid); margin-top: 4px; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 4px; }
.form-group input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: 15px; outline: none;
  transition: border-color .15s;
}
.form-group input:focus { border-color: var(--blue); }

.auth-switch { text-align: center; margin-top: 16px; font-size: 14px; color: var(--ink-mid); }
.auth-switch a { color: var(--blue); cursor: pointer; text-decoration: none; }

/* ── Type tabs ───────────────────────────────────────────────────────────────── */
.type-tabs { display: flex; gap: 8px; margin-bottom: 16px; overflow-x: auto; padding-bottom: 2px; }
.type-tab {
  padding: 6px 14px; border-radius: 20px; border: 1px solid var(--border);
  background: var(--card); font-size: 13px; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.type-tab.active { background: var(--blue); color: #fff; border-color: var(--blue); }

/* ── Receipt detail ──────────────────────────────────────────────────────────── */
.detail-header {
  display: flex; align-items: flex-start; gap: 12px; margin-bottom: 16px;
}
.detail-merchant { font-size: 20px; font-weight: 700; }
.detail-meta { font-size: 13px; color: var(--ink-mid); margin-top: 2px; }
.detail-total { font-size: 24px; font-weight: 700; margin-top: 4px; }

.confirm-btn {
  position: sticky; bottom: 72px;
  margin: 16px 0;
}

/* ── Edit mode ───────────────────────────────────────────────────────────────── */
.edit-banner {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--blue); color: #fff; padding: 10px 16px;
  border-radius: var(--radius); margin-bottom: 12px; font-weight: 500;
}
.edit-input {
  display: block; width: 100%; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 14px; font-family: inherit; outline: none;
  background: var(--bg);
}
.edit-input:focus { border-color: var(--blue); background: #fff; }
select.edit-input { height: auto; cursor: pointer; }
.edit-li {
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.edit-li:last-child { border-bottom: none; }
.edit-li-header { display: flex; align-items: center; gap: 8px; }
.edit-del-btn {
  background: none; border: none; cursor: pointer; font-size: 16px;
  color: var(--red); padding: 4px; flex-shrink: 0;
}
.edit-field-mini { display: flex; flex-direction: column; gap: 2px; }
.edit-field-mini span { font-size: 10px; color: var(--ink-mid); font-weight: 500; }
.edit-field-mini .edit-input { padding: 5px 7px; font-size: 13px; }

/* ── Empty state ─────────────────────────────────────────────────────────────── */
.empty {
  text-align: center; padding: 48px 16px; color: var(--ink-mid);
}
.empty .icon { font-size: 48px; margin-bottom: 12px; }
