:root {
  --bg: #faf9f7;
  --panel: #ffffff;
  --ink: #1c1b19;
  --muted: #6b6862;
  --line: #e3e0da;
  --accent: #2f4f4f;
  --warn: #a8541f;
  --ok: #2f6b46;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161815;
    --panel: #1f221e;
    --ink: #ece9e3;
    --muted: #9a968e;
    --line: #333730;
    --accent: #8fb3a8;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 ui-sans-serif, system-ui, "Segoe UI", Roboto, sans-serif;
}
header {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line);
}
header h1 { font-size: 19px; margin: 0; letter-spacing: 0.5px; }
header .sub { color: var(--muted); font-size: 13px; }
main { padding: 20px; max-width: 1200px; margin: 0 auto; }
.card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px; margin-bottom: 16px;
}
button, .btn {
  font: inherit; padding: 10px 16px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  cursor: pointer;
}
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: default; }
input, select, textarea {
  font: inherit; padding: 9px 10px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--panel); color: var(--ink); width: 100%;
}
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.capture {
  display: block; width: 100%; padding: 44px 20px; text-align: center;
  font-size: 20px; border: 2px dashed var(--line); border-radius: var(--radius);
  background: var(--panel); cursor: pointer;
}
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 160px; }
.muted { color: var(--muted); font-size: 13px; }
.pill {
  display: inline-block; padding: 3px 10px; border-radius: 999px;
  font-size: 12px; border: 1px solid var(--line);
  white-space: nowrap;
}
.pill.review { color: var(--warn); border-color: var(--warn); }
.pill.ready { color: var(--ok); border-color: var(--ok); }
.pill.failed { color: #b3261e; border-color: #b3261e; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.scroll { overflow-x: auto; }
.thumb { max-width: 100%; border-radius: 8px; border: 1px solid var(--line); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 720px) { .grid2 { grid-template-columns: 1fr; } }

/* Phone layout.
   A receipt table cannot be swiped sideways on a phone and stay usable, so
   below 760px every row collapses into a card with its own labels. */
@media (max-width: 760px) {
  header { padding: 12px 16px; }
  header h1 { font-size: 17px; }
  main { padding: 14px; }
  .card { padding: 14px; }

  table.stacked thead { display: none; }
  table.stacked, table.stacked tbody, table.stacked tr, table.stacked td {
    display: block;
    width: 100%;
  }
  table.stacked tr {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 4px 12px 10px;
  }
  table.stacked td {
    border: none;
    padding: 7px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    text-align: left;
  }
  table.stacked td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    font-size: 11px;
    letter-spacing: 0.7px;
    text-transform: uppercase;
    color: var(--muted);
  }
  table.stacked td.num { text-align: right; }
  table.stacked td:empty { display: none; }
  table.stacked td > select { max-width: 62%; }
  table.stacked td.actions { padding-top: 10px; }
  table.stacked td.actions::before { content: none; }
  table.stacked td.actions button { width: 100%; }

  /* The receipt photo was pushing every field below the fold. */
  .thumb { max-height: 38vh; object-fit: contain; width: 100%; }

  .row > * { flex: 1 1 100%; }
  .row > .keep { flex: 0 0 auto; }
  /* min-width 0 matters: a native date input has a wide intrinsic size and
     will refuse to shrink, which wraps the pair onto two rows. */
  .row > .half { flex: 0 1 calc(50% - 7px); min-width: 0; }
  .row > .half input, .row > .half select { min-width: 0; }
  .actionbar { flex-direction: column; gap: 10px; }
  .actionbar > * { flex: 1 1 100% !important; width: 100%; }
  .actionbar button { width: 100%; padding: 13px 16px; }
  button, .btn, input, select, textarea { font-size: 16px; }
}

.exports { display: flex; gap: 8px; }
.exports .btn { flex: 1 1 0; text-align: center; white-space: nowrap; }
textarea { resize: vertical; min-height: 68px; }
.thumb { cursor: zoom-in; }

button.danger { color: #b3261e; border-color: #b3261e; }
button.danger:hover { background: #b3261e; color: #fff; }

/* A destructive action should be the quietest thing on the row until you
   reach for it. Plain grey text, red only once it is armed. */
button.quiet {
  background: none;
  border: none;
  padding: 6px 4px;
  color: var(--muted);
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 3px;
}
button.quiet:hover { color: var(--ink); }
button.quiet.armed {
  color: #b3261e;
  font-weight: 600;
  text-decoration: none;
}
a.openReceipt { color: inherit; text-decoration: none; }
a.openReceipt:hover { text-decoration: underline; }

/* Long vendor names like "United States Postal Service" stretched a row to
   four lines. Two is the limit, the full name is on the review screen. */
.clamp2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Full screen receipt viewer. Opening the raw R2 URL sent you to
   cloudflarestorage.com with no way back, so the photo stays in the app. */
#lightbox {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: #0c0d0b;
  display: flex;
  flex-direction: column;
}
#lightbox[hidden] { display: none; }
.lbBar {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(0, 0, 0, 0.55);
  flex: 0 0 auto;
}
.lbBar button {
  background: rgba(255, 255, 255, 0.08);
  color: #f2f0ea;
  border-color: rgba(255, 255, 255, 0.25);
  padding: 10px 18px;
}
.lbScroll {
  flex: 1 1 auto;
  overflow: auto;
  overscroll-behavior: contain;
}
/* Block layout, not flex. Centering a zoomed image horizontally puts the
   blank middle of a receipt on screen, when the print runs down the left. */
.lbScroll img {
  display: block;
  width: 100%;
  height: auto;
  max-width: 100%;
  margin: 0 auto;
  cursor: zoom-in;
}
.lbScroll.z2 img { width: 200%; max-width: none; }
.lbScroll.z3 img { width: 320%; max-width: none; cursor: zoom-out; }

a.pill { text-decoration: none; }
a.plain { text-decoration: none; color: inherit; }
