:root {
  --bg: #f5f6f8; --surface: #ffffff; --surface2: #eef0f3; --text: #111827; --muted: #6b7280;
  --border: #e2e5ea; --primary: #2563eb; --primary-text: #fff; --danger: #dc2626;
  --ok-bg: #e7f6ec; --ok-text: #14532d; --err-bg: #fdecec; --err-text: #7f1d1d; --info-bg: #e8f0fe; --info-text: #1e3a8a;
  --radius: 12px; --nav-h: 62px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120; --surface: #141c2f; --surface2: #1c263c; --text: #e5e9f0; --muted: #94a0b4;
    --border: #26314a; --primary: #3b82f6; --ok-bg: #12301f; --ok-text: #b7f0cb; --err-bg: #3a1515;
    --err-text: #fecaca; --info-bg: #162544; --info-text: #c7d7fe; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
}
body.auth { padding-bottom: 0; }
a { color: var(--primary); text-decoration: none; }
h1 { font-size: 1.5rem; margin: .3rem 0 1rem; display: flex; align-items: center; gap: .5rem; }
h2.sec { font-size: 1rem; margin: 1.6rem 0 .6rem; display: flex; justify-content: space-between; align-items: baseline; }
h3.grp { font-size: .85rem; margin: .9rem 0 .2rem; color: var(--muted); display: flex; align-items: center; gap: .4rem; }
main { max-width: 760px; margin: 0 auto; padding: 12px 16px; }
.muted { color: var(--muted); } .small { font-size: .85rem; } .center { text-align: center; } .mt { margin-top: 1rem; }
code.key { word-break: break-all; background: var(--surface2); padding: 2px 6px; border-radius: 6px; user-select: all; }

header.top {
  position: sticky; top: 0; z-index: 10; display: flex; justify-content: space-between; align-items: center;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px; background: #0f172a; color: #fff;
}
header.top .brand { color: #fff; font-weight: 700; font-size: 1.1rem; }
header.top .who { font-size: .8rem; opacity: .75; }

nav.bottom {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10; display: flex; background: var(--surface);
  border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
}
nav.bottom a { flex: 1; height: var(--nav-h); display: flex; flex-direction: column; align-items: center;
  justify-content: center; font-size: .72rem; color: var(--muted); gap: 2px; }
nav.bottom a .ic { font-size: 1.35rem; line-height: 1; }
nav.bottom a.on { color: var(--primary); font-weight: 600; }

.flash { padding: .75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; font-size: .92rem; }
.flash.ok { background: var(--ok-bg); color: var(--ok-text); }
.flash.err { background: var(--err-bg); color: var(--err-text); }
.flash.info { background: var(--info-bg); color: var(--info-text); }

/* Forms */
label { display: block; font-size: .85rem; color: var(--muted); font-weight: 500; }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; font: inherit; font-size: 16px; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: .65rem .75rem;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input[type=checkbox], input[type=radio] { display: inline-block; width: 1.15rem; height: 1.15rem; margin: 0; accent-color: var(--primary); }
label.check { display: flex; align-items: center; gap: .55rem; color: var(--text); font-weight: 400; font-size: .95rem; padding: .35rem 0; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .9rem .8rem; margin: 0; }
legend { font-size: .85rem; color: var(--muted); padding: 0 .3rem; }
.stack { display: flex; flex-direction: column; gap: .85rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.inline { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.inline input:not([type=checkbox]) { flex: 1; min-width: 8rem; margin: 0; }
.inline input.num { width: 5rem; flex: none; display: inline-block; }
.inline label { display: flex; align-items: center; gap: .4rem; color: var(--text); }
.card-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }
details summary { cursor: pointer; color: var(--muted); font-size: .9rem; padding: .3rem 0; }
pre.ocr { white-space: pre-wrap; font-size: .8rem; background: var(--surface2); padding: .75rem; border-radius: 8px; max-height: 300px; overflow: auto; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem; font: inherit; font-weight: 600;
  font-size: .95rem; padding: .65rem 1rem; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; text-align: center; -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-outline { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn.link { background: none; border: none; color: var(--muted); font-weight: 400; }
.btn.big { padding: .85rem 1rem; font-size: 1.05rem; width: 100%; }
.btn.huge { padding: 1.15rem 1rem; font-size: 1.2rem; width: 100%; border-radius: 16px; }
.btn.sm { padding: .4rem .7rem; font-size: .85rem; }
.btn.wide { width: 100%; }
.btn[disabled] { opacity: .6; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; margin-top: .6rem; }
.btns { display: flex; flex-wrap: wrap; gap: .4rem; margin: .5rem 0; }
.sticky-btn { position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 8px); margin-top: 1rem; box-shadow: 0 6px 20px rgba(0,0,0,.18); }
.ic-cam { width: 1.4rem; height: 1.4rem; display: inline-block; background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M9 3 7.2 5H4a2 2 0 0 0-2 2v11a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-3.2L15 3H9zm3 5a5 5 0 1 1 0 10 5 5 0 0 1 0-10zm0 2a3 3 0 1 0 0 6 3 3 0 0 0 0-6z'/%3E%3C/svg%3E") center/contain no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M9 3 7.2 5H4a2 2 0 0 0-2 2v11a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-3.2L15 3H9zm3 5a5 5 0 1 1 0 10 5 5 0 0 1 0-10zm0 2a3 3 0 1 0 0 6 3 3 0 0 0 0-6z'/%3E%3C/svg%3E") center/contain no-repeat; }

/* Cards / chips */
.chips { display: flex; gap: .5rem; overflow-x: auto; padding: 2px 2px 10px; margin: 0 -16px; padding-left: 16px; padding-right: 16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; display: inline-flex; align-items: center; gap: .4rem; padding: .5rem .85rem; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); color: var(--text); font-size: .92rem; white-space: nowrap; }
.chip small { color: var(--muted); font-size: .7rem; }
.chip.on { border-color: var(--c); box-shadow: inset 0 0 0 1px var(--c); background: color-mix(in srgb, var(--c) 12%, var(--surface)); font-weight: 600; }
.chip.add { font-size: 1.2rem; padding: .35rem .9rem; color: var(--muted); }
.dot { display: inline-block; width: .65rem; height: .65rem; border-radius: 50%; background: var(--c, var(--muted)); flex: none; }
.swatch { display: inline-block; width: 1.6rem; height: 1.6rem; border-radius: 8px; background: var(--c); flex: none; }
.colors { display: flex; flex-wrap: wrap; gap: .5rem; }
.cpick input { position: absolute; opacity: 0; }
.cpick span { display: block; width: 2.2rem; height: 2.2rem; border-radius: 50%; background: var(--c); border: 3px solid transparent; }
.cpick input:checked + span { border-color: var(--text); box-shadow: inset 0 0 0 2px var(--surface); }
.scan { margin: .6rem 0 1rem; }
.uploading { display: flex; align-items: center; gap: .5rem; justify-content: center; padding: .8rem; color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; }
.stats div { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .7rem; text-align: center; }
.stats b { display: block; font-size: 1.1rem; }
.stats small { color: var(--muted); font-size: .72rem; }

/* Lists */
.list { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.list > * + * { border-top: 1px solid var(--border); }
.list h3.grp { margin: 0; padding: .6rem .9rem .3rem; background: var(--surface2); }
.rrow { display: flex; align-items: center; gap: .75rem; padding: .7rem .9rem; color: var(--text); min-height: 60px; }
.rrow.hl { background: var(--info-bg); }
.rrow.selectable { cursor: pointer; }
.rrow .main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.rrow .main b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.rrow .main small { color: var(--muted); font-size: .8rem; display: flex; align-items: center; gap: .3rem; flex-wrap: wrap; }
.rrow .amt { text-align: right; display: flex; flex-direction: column; align-items: flex-end; }
.rrow .amt small { color: var(--muted); font-size: .8rem; }
.thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; flex: none; background: var(--surface2); display: flex; align-items: center; justify-content: center; }
.tag { font-size: .68rem; background: var(--surface2); color: var(--muted); border-radius: 6px; padding: 0 .35rem; }
.list.menu a { display: flex; flex-direction: column; padding: .85rem 1rem; color: var(--text); }
.list.menu small { color: var(--muted); font-size: .8rem; }
.catrow { padding: .5rem .7rem; }
.urow { padding: .6rem .9rem; }
.urow summary { display: flex; flex-direction: column; color: var(--text); }
.urow summary small { color: var(--muted); }
.filters { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: .5rem; margin-bottom: .5rem; }
.filters select, .filters input { margin: 0; }
.sel-bar { display: flex; justify-content: space-between; align-items: center; margin: 1.2rem 0 .4rem; }
.empty { text-align: center; padding: 3rem 1rem; }
.danger-zone summary { color: var(--danger); }

/* Receipt page */
.receipt-layout { display: grid; gap: 1rem; }
.rimg img { width: 100%; max-height: 55vh; object-fit: contain; border-radius: var(--radius); background: var(--surface2); display: block; }
.noimg { padding: 1.5rem; text-align: center; color: var(--muted); background: var(--surface2); border-radius: var(--radius); font-size: .9rem; }
.gbp input#gbp { font-size: 1.15rem; font-weight: 600; }
@media (min-width: 760px) {
  main { max-width: 1000px; }
  .receipt-layout { grid-template-columns: 1fr 1fr; align-items: start; }
  .rimg { position: sticky; top: 70px; }
  .rimg img { max-height: 80vh; }
}

/* Auth */
.auth-box { max-width: 360px; margin: 12vh auto 0; text-align: center; }
.auth-box h1 { justify-content: center; }
.auth-box form { text-align: left; margin-top: 1rem; }
.auth-logo { border-radius: 18px; }
.code-input { font-size: 1.6rem; letter-spacing: .4em; text-align: center; }
.qr { width: 200px; height: 200px; background: #fff; border-radius: 8px; display: block; margin: .5rem 0; }

.spinner { width: 1rem; height: 1rem; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin .8s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 380px) { .grid2 { grid-template-columns: 1fr; } }

/* Scan loop */
.scan-loop { display: flex; flex-direction: column; min-height: calc(100vh - 160px); }
.scan-to { display: flex; align-items: center; gap: .4rem; }
.shots { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.shot { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.shot img { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; display: block; background: var(--surface2); }
.shot .st { font-size: .72rem; padding: .35rem .45rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.shot.failed { border-color: var(--danger); cursor: pointer; }
.shot.failed .st { color: var(--danger); white-space: normal; }
.loop-actions { margin-top: auto; display: flex; flex-direction: column; gap: .6rem;
  position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 8px); background: var(--bg); padding-top: .5rem; }
.done-btn.waiting { opacity: .75; }

/* Batch review */
.bitem { margin-bottom: .8rem; }
.bhead { display: flex; gap: .8rem; align-items: center; }
.bthumb { width: 64px; height: 84px; object-fit: cover; border-radius: 8px; display: block; background: var(--surface2); }
.bmeta { display: flex; flex-direction: column; gap: .15rem; }
.bfields { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; margin-top: .8rem; }
.bfields .span2 { grid-column: 1 / -1; margin: 0; }
.err-text { color: var(--danger); }
