:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e1e6ec;
  --text: #14171c;
  --muted: #646d7a;
  --accent: #e20074;
  --accent-ink: #ffffff;
  --danger: #c8102e;
  --danger-bg: #fdecef;
  --warn: #a45a06;
  --warn-bg: #fdf3e3;
  --ok: #15803d;
  --ok-bg: #eaf7ee;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(18, 24, 33, .06), 0 4px 14px rgba(18, 24, 33, .05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1d222a;
    --border: #2b323c;
    --text: #e8ebef;
    --muted: #9aa4b2;
    --danger: #ff6b7f;
    --danger-bg: #2a161b;
    --warn: #f0b354;
    --warn-bg: #2a2115;
    --ok: #5dd48a;
    --ok-bg: #14261b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }

/* Our own display rules below (button, .btn) would otherwise beat the browser's
   [hidden] { display: none }, leaving hidden buttons visible. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* ------------------------------------------------------------------ header */

header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
}

.header-row {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 1100px;
  margin: 0 auto;
}

.brand {
  font-weight: 700;
  font-size: 17px;
  letter-spacing: -.01em;
  white-space: nowrap;
}

.brand span { color: var(--accent); }

#search {
  flex: 1;
  min-width: 0;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
}

#search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

main { max-width: 1100px; margin: 0 auto; padding: 14px 14px 96px; }

/* ------------------------------------------------------------------- tiles */

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: left;
  font: inherit;
  color: inherit;
  transition: transform .08s ease, border-color .12s ease;
}

.tile:hover { transform: translateY(-1px); }
.tile[aria-pressed="true"] { border-color: var(--accent); }
.tile .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.tile .value { font-size: 26px; font-weight: 700; line-height: 1.2; margin-top: 4px; font-variant-numeric: tabular-nums; }
.tile .sub { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tile.danger .value { color: var(--danger); }
.tile.warn .value { color: var(--warn); }
.tile.ok .value { color: var(--ok); }

/* ------------------------------------------------------------------- tabs */

.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 8px;
  margin-bottom: 8px;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar { display: none; }

.tab {
  flex: 0 0 auto;
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.tab[aria-selected="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  font-size: 14px;
  color: var(--muted);
}

.toolbar select {
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.spacer { flex: 1; }

/* ------------------------------------------------------------------- cards */

.list { display: grid; gap: 10px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.card.overdue { border-left-color: var(--danger); }
.card.due_soon { border-left-color: var(--warn); }
.card.paid { border-left-color: var(--ok); }
.card.chargeback { border-left-color: var(--danger); opacity: .75; }

.card-top {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.invoice { font-weight: 700; font-size: 16px; font-variant-numeric: tabular-nums; }
.amount { margin-left: auto; font-weight: 700; font-size: 17px; font-variant-numeric: tabular-nums; }

.due {
  font-size: 13px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}

.due.overdue { background: var(--danger-bg); color: var(--danger); }
.due.due_soon { background: var(--warn-bg); color: var(--warn); }
.due.paid { background: var(--ok-bg); color: var(--ok); }

.meta {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  font-size: 13px;
  color: var(--muted);
}

.meta b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.meta .flag { color: var(--danger); font-weight: 700; }

.card-actions { margin-top: 10px; display: flex; gap: 8px; flex-wrap: wrap; }

/* ----------------------------------------------------------------- buttons */

button, .btn {
  font: inherit;
  font-weight: 600;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 8px 14px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
}

button:hover, .btn:hover { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.btn-ok { background: var(--ok-bg); border-color: transparent; color: var(--ok); }
.btn-danger { background: transparent; border-color: transparent; color: var(--danger); }
.btn-sm { padding: 6px 11px; font-size: 13px; min-height: 34px; }

/* --------------------------------------------------------------- empty/misc */

.empty {
  text-align: center;
  padding: 44px 20px;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.empty strong { display: block; color: var(--text); font-size: 16px; margin-bottom: 4px; }

.locations {
  margin-top: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

.locations h2 { margin: 0 0 10px; font-size: 14px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.loc-row { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-top: 1px solid var(--border); font-size: 14px; }
.loc-row:first-of-type { border-top: 0; }
.loc-row .name { font-weight: 600; }
.loc-row .nums { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.loc-row .nums .bad { color: var(--danger); font-weight: 700; }

/* --------------------------------------------------------------- action bar */

.actionbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  display: flex;
  gap: 10px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
}

.actionbar > * { flex: 1; justify-content: center; }

/* ------------------------------------------------------------------ dialog */

dialog {
  border: none;
  border-radius: 14px;
  padding: 0;
  width: min(560px, 100%);
  max-height: 92vh;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}

dialog::backdrop { background: rgba(8, 11, 15, .55); }

.dlg-head {
  position: sticky; top: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  font-weight: 700;
  font-size: 16px;
}

.dlg-head button { margin-left: auto; min-height: 34px; padding: 5px 10px; }
.dlg-body { padding: 16px; overflow-y: auto; }

.dlg-foot {
  position: sticky; bottom: 0;
  display: flex; gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.dlg-foot .btn-primary { flex: 1; justify-content: center; }

/* ------------------------------------------------------------------- forms */

.field { margin-bottom: 13px; }

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 5px;
}

.field input, .field select, .field textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  /* 16px stops iOS Safari from zooming the page on focus */
  font-size: 16px;
}

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

.field textarea { min-height: 64px; resize: vertical; }
.field.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field.row2 > div { min-width: 0; }

.field .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.field .hint.bad { color: var(--danger); font-weight: 600; }
.field .hint.good { color: var(--ok); font-weight: 600; }

/* A field the scanner filled but wasn't sure about. */
.uncertain input, .uncertain select {
  border-color: var(--warn);
  background: var(--warn-bg);
}

.uncertain > label::after {
  content: " check this";
  color: var(--warn);
  font-weight: 700;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.banner {
  padding: 10px 12px;
  border-radius: 9px;
  font-size: 13.5px;
  margin-bottom: 13px;
  line-height: 1.45;
}

.banner.warn { background: var(--warn-bg); color: var(--warn); }
.banner.bad { background: var(--danger-bg); color: var(--danger); }
.banner.ok { background: var(--ok-bg); color: var(--ok); }
.banner ul { margin: 6px 0 0; padding-left: 18px; }

.thumbs { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 13px; }
.thumbs img {
  width: 76px; height: 76px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.scanning { text-align: center; padding: 34px 16px; color: var(--muted); }

.spinner {
  width: 30px; height: 30px;
  margin: 0 auto 14px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
  .tile:hover { transform: none; }
}

/* ------------------------------------------------------------------- login */

.login-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px 24px;
  width: min(360px, 100%);
  box-shadow: var(--shadow);
}

.login-card h1 { margin: 0 0 4px; font-size: 21px; }
.login-card h1 span { color: var(--accent); }
.login-card p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
.login-card button { width: 100%; justify-content: center; margin-top: 6px; }

/* -------------------------------------------- multi-line receipt (batch form) */

.sec {
  margin: 20px 0 10px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.sec:first-child { margin-top: 0; }
.sec-note { margin-left: auto; text-transform: none; letter-spacing: 0; font-weight: 500; }
.opt { font-weight: 400; color: var(--muted); }

fieldset.line {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px 2px;
  margin: 0 0 10px;
  background: var(--surface-2);
}

fieldset.line legend {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 0 4px;
  font-size: 13px;
}

.line-toggle { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; }
.line-toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
.line-title { font-weight: 700; }

.line-device {
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* An unticked line stays visible but clearly out of play. */
fieldset.line.excluded { opacity: .45; }
fieldset.line.excluded input:not(.line-include) { pointer-events: none; }

.chip {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%);
  z-index: 50;
  background: var(--ok-bg);
  color: var(--ok);
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 10px 18px;
  font-weight: 600;
  font-size: 14px;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 28px);
  text-align: center;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

@media (max-width: 480px) {
  .tiles { grid-template-columns: 1fr 1fr; }
  .field.row2 { grid-template-columns: 1fr; }
  dialog { width: 100%; max-height: 100dvh; height: 100dvh; border-radius: 0; }
}
