:root {
  --bg: #0b0f19;
  --surface: #111827;
  --surface-2: #1b2437;
  --border: #263149;
  --text: #f3f4f6;
  --muted: #97a3b8;
  --accent: #34d399;
  --accent-ink: #04241a;
  --danger: #f87171;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
#app { min-height: 100%; display: flex; flex-direction: column; }
.loading { margin: auto; color: var(--muted); padding: 3rem; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: .6rem;
  padding: calc(env(safe-area-inset-top, 0px) + .7rem) 1rem .7rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.05rem; margin: 0; flex: 1; font-weight: 650; }
.topbar .sub { color: var(--muted); font-size: .8rem; font-weight: 400; }
.iconbtn {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  width: 40px; height: 40px; border-radius: 10px; font-size: 1.1rem;
  display: grid; place-items: center; cursor: pointer;
}
.iconbtn:active { transform: scale(.94); }

.wrap { flex: 1; padding: 1rem; padding-bottom: calc(6rem + var(--safe-b)); max-width: 640px; width: 100%; margin: 0 auto; }

/* Auth */
.auth { max-width: 380px; margin: 8vh auto; padding: 0 1.2rem; }
.brand { text-align: center; margin-bottom: 2rem; }
.brand .logo { font-size: 2.6rem; }
.brand h1 { margin: .3rem 0 0; letter-spacing: -.02em; }
.brand p { color: var(--muted); margin: .3rem 0 0; }

label { display: block; font-size: .82rem; color: var(--muted); margin: .9rem 0 .3rem; }
input[type=text], input[type=email], input[type=password], input[type=number] {
  width: 100%; padding: .8rem .9rem; font-size: 1rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; color: var(--text);
}
input:focus { outline: 2px solid var(--accent); border-color: transparent; }

.btn {
  width: 100%; padding: .85rem 1rem; font-size: 1rem; font-weight: 650;
  border: none; border-radius: 12px; cursor: pointer; margin-top: 1rem;
  background: var(--accent); color: var(--accent-ink);
}
.btn:active { transform: scale(.98); }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.ghost { background: transparent; color: var(--accent); margin-top: .4rem; }
.btn:disabled { opacity: .5; }

.link { color: var(--accent); background: none; border: none; cursor: pointer; font-size: .9rem; padding: .4rem; }
.msg { margin-top: 1rem; padding: .7rem .9rem; border-radius: 10px; font-size: .9rem; }
.msg.err { background: #3a1620; color: var(--danger); }
.msg.ok { background: #0f2c22; color: var(--accent); }

/* Lists screen */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem; margin-bottom: .8rem;
  display: flex; align-items: center; gap: .8rem; cursor: pointer;
}
.card:active { background: var(--surface-2); }
.card .grow { flex: 1; }
.card .grow .name { font-weight: 600; }
.card .grow .meta { color: var(--muted); font-size: .8rem; }
.card .chev { color: var(--muted); }
.section-title { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; margin: 1.4rem .2rem .6rem; }

/* Items */
.item {
  display: flex; align-items: center; gap: .8rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: .75rem .85rem; margin-bottom: .5rem;
}
.item.checked { opacity: .5; }
.item.checked .iname { text-decoration: line-through; }
.check {
  width: 26px; height: 26px; border-radius: 8px; flex: none;
  border: 2px solid var(--border); background: transparent; cursor: pointer;
  display: grid; place-items: center; color: var(--accent-ink); font-size: .9rem;
}
.item.checked .check { background: var(--accent); border-color: var(--accent); }
.item .body { flex: 1; min-width: 0; }
.item .iname { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .imeta { color: var(--muted); font-size: .78rem; }
.qty { display: flex; align-items: center; gap: .1rem; color: var(--muted); font-size: .85rem; }
.qty button { background: var(--surface-2); border: 1px solid var(--border); color: var(--text); width: 28px; height: 28px; border-radius: 8px; cursor: pointer; }
.trash { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1rem; padding: .3rem; }

.empty { text-align: center; color: var(--muted); padding: 3rem 1rem; }
.empty .big { font-size: 2.4rem; }

/* FAB scan button */
.fab {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(1.1rem + var(--safe-b)); z-index: 8;
  display: flex; gap: .6rem;
}
.fab button {
  border: none; border-radius: 999px; cursor: pointer; font-weight: 650;
  padding: .95rem 1.4rem; font-size: 1rem; box-shadow: 0 8px 24px rgba(0,0,0,.4);
  display: flex; align-items: center; gap: .5rem;
}
.fab .scan { background: var(--accent); color: var(--accent-ink); }
.fab .add { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }

/* Modal / sheet */
.sheet-bg {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 20;
  display: flex; align-items: flex-end; justify-content: center;
  /* --kb = on-screen keyboard height (set from visualViewport in app.js);
     lifts the sheet so the focused field stays visible while typing */
  padding-bottom: var(--kb, 0px);
  transition: padding-bottom .15s ease-out;
}
.sheet {
  background: var(--surface); width: 100%; max-width: 640px;
  border-radius: 18px 18px 0 0; padding: 1.2rem 1.2rem calc(1.2rem + var(--safe-b));
  border: 1px solid var(--border); border-bottom: none;
  max-height: calc(100dvh - var(--kb, 0px) - 1.5rem);
  overflow-y: auto;
}
.sheet h2 { margin: 0 0 1rem; font-size: 1.1rem; }
.sheet .row { display: flex; gap: .6rem; }
.sheet .row .btn { margin-top: 0; }

/* Scanner */
.scanner-bg { position: fixed; inset: 0; background: #000; z-index: 30; display: flex; flex-direction: column; }
.scanner-bg video { flex: 1; width: 100%; object-fit: cover; }
.scan-frame {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);
  width: 78%; max-width: 340px; height: 160px; border: 3px solid var(--accent);
  border-radius: 16px; box-shadow: 0 0 0 100vmax rgba(0,0,0,.45);
}
.scan-hint { position: absolute; bottom: calc(6rem + var(--safe-b)); left: 0; right: 0; text-align: center; color: #fff; font-size: .9rem; }
.scan-close {
  position: absolute; top: calc(env(safe-area-inset-top,0) + 1rem); right: 1rem;
  width: 44px; height: 44px; border-radius: 50%; border: none; background: rgba(0,0,0,.5);
  color: #fff; font-size: 1.4rem; cursor: pointer; z-index: 2;
}
.scan-status {
  position: absolute; top: calc(env(safe-area-inset-top,0) + 1rem); left: 1rem; right: 5rem;
  color: #fff; font-size: .85rem; background: rgba(0,0,0,.4); padding: .5rem .7rem; border-radius: 10px;
}
.scan-top { position: absolute; top: calc(env(safe-area-inset-top,0) + .8rem); right: 1rem; display: flex; gap: .5rem; z-index: 2; }
.scan-icon {
  width: 44px; height: 44px; border-radius: 50%; border: none; background: rgba(0,0,0,.5);
  color: #fff; font-size: 1.3rem; cursor: pointer;
}
.scan-icon.on { background: var(--accent); }
.scan-tally {
  position: absolute; bottom: calc(1.4rem + var(--safe-b)); left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.6); color: #fff; padding: .5rem 1rem; border-radius: 999px; font-size: .9rem; font-weight: 600;
}
.scan-name {
  position: absolute; bottom: 0; left: 0; right: 0; z-index: 3;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 1rem 1rem calc(1rem + var(--safe-b)); border-radius: 16px 16px 0 0;
}
.scan-name .nb-title { color: var(--muted); font-size: .85rem; margin-bottom: .5rem; }
.scan-name #nb-code { color: var(--accent); font-weight: 600; }
.scan-name .row { display: flex; gap: .6rem; margin-top: .8rem; }
.scan-name .row .btn { margin-top: 0; }

/* Item thumbnails */
.thumb {
  width: 40px; height: 40px; border-radius: 9px; flex: none; object-fit: cover;
  background: var(--surface-2); border: 1px solid var(--border);
}
.thumb.ph { display: grid; place-items: center; color: var(--muted); font-weight: 700; }

/* Sheet extras */
.sheet-img { display: grid; place-items: center; margin: .2rem 0 .4rem; }
.sheet-img img { max-height: 150px; max-width: 70%; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2); }
.sheet-img:empty { display: none; }
.photo-actions { display: flex; gap: .6rem; margin-bottom: .2rem; }
.photo-actions .btn { margin-top: .4rem; }
.photo-btn { display: flex; align-items: center; justify-content: center; cursor: pointer; }
.grid2 { display: flex; gap: .6rem; }
.grid2 > div { flex: 1; }
select {
  width: 100%; padding: .8rem .9rem; font-size: 1rem; -webkit-appearance: none; appearance: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; color: var(--text);
}
.btn.danger-btn { background: #3a1620; color: var(--danger); }
.link.danger { color: var(--danger); }
.section-title.checked-head { display: flex; justify-content: space-between; align-items: center; }

/* Settings / admin */
.card-static { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-bottom: .8rem; }
.card-static > label:first-child, .card-static > *:first-child { margin-top: 0; }
.card-static .btn { margin-top: 1rem; }
.inline { display: flex; gap: .5rem; align-items: center; }
.inline input { flex: 1; }
.inline-btn { margin-top: 0 !important; max-width: 96px; padding: .6rem; }
input:disabled { opacity: .55; }
.danger-title { color: var(--danger); }
.tabs { display: flex; gap: .4rem; padding: .7rem 1rem 0; max-width: 640px; margin: 0 auto; width: 100%; }
.tab { flex: 1; padding: .55rem; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; color: var(--muted); font-size: .85rem; font-weight: 600; cursor: pointer; }
.tab.active { background: var(--surface-2); color: var(--text); border-color: var(--accent); }
.mactions { display: flex; flex-direction: column; align-items: flex-end; gap: .1rem; }
.tag.admintag { background: #0f2c22; color: var(--accent); }

/* Members */
.mrow { display: flex; align-items: center; gap: .7rem; padding: .6rem 0; border-bottom: 1px solid var(--border); }
.mavatar { width: 38px; height: 38px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--border);
  display: grid; place-items: center; font-weight: 700; color: var(--accent); }
.tag { font-size: .7rem; background: var(--surface-2); padding: .1rem .4rem; border-radius: 6px; color: var(--muted); }

/* QR */
.qr { display: grid; place-items: center; padding: 1rem 0; }
.qr img { border-radius: 12px; }

/* Shopping mode: big tap-anywhere rows, steppers hidden */
.iconbtn.on { border-color: var(--accent); color: var(--accent); }
body.shopping .item { padding: 1.05rem .9rem; cursor: pointer; }
body.shopping .item .check { width: 32px; height: 32px; pointer-events: none; }
body.shopping .item .qty button { display: none; }
body.shopping .item .qty { font-size: 1rem; font-weight: 650; }
body.shopping .item .qty span::before { content: '×'; margin-right: .1rem; color: var(--muted); }

/* Quick add */
.sheet.tall { max-height: calc(min(82vh, 100dvh - var(--kb, 0px) - 1.5rem)); display: flex; flex-direction: column; overflow-y: hidden; }
.qa-body { overflow-y: auto; flex: 1; min-height: 200px; margin: .4rem -.2rem 0; padding: 0 .2rem; }
.qa-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: .45rem; margin-bottom: .4rem; }
.qa-tile {
  display: flex; flex-direction: column; align-items: center; gap: .25rem;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
  padding: .6rem .3rem; cursor: pointer; color: var(--text); font-size: .78rem; line-height: 1.15;
}
.qa-tile:active { transform: scale(.95); }
.qa-tile.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface-2)); }
.qa-tile .qa-e { font-size: 1.4rem; }
.qa-tile .qa-n { text-align: center; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.qa-addrow {
  width: 100%; text-align: left; background: var(--surface-2); border: 1px dashed var(--border);
  border-radius: 12px; padding: .8rem; color: var(--accent); font-weight: 600; cursor: pointer; margin-top: .3rem;
}

/* Offline / sync pill */
.netpill {
  position: fixed; top: calc(env(safe-area-inset-top, 0px) + .55rem);
  left: 50%; transform: translateX(-50%); z-index: 60;
  background: #7c2d12; color: #fed7aa; border: 1px solid #9a3412;
  padding: .4rem .85rem; border-radius: 999px; font-size: .8rem; font-weight: 600;
  box-shadow: 0 4px 14px rgba(0,0,0,.35); pointer-events: none; white-space: nowrap;
}

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(6.5rem + var(--safe-b)); transform: translate(-50%, 12px);
  z-index: 50; max-width: 90%; padding: .7rem 1rem; border-radius: 12px; font-size: .9rem;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  box-shadow: 0 8px 24px rgba(0,0,0,.4); opacity: 0; transition: opacity .2s, transform .2s;
}
.toast.in { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: #3a1620; color: var(--danger); border-color: #5b2230; }
.toast.undo { display: flex; gap: 1rem; align-items: center; }
.toast.undo button {
  background: none; border: none; color: var(--accent); font-weight: 700;
  font-size: .9rem; cursor: pointer; padding: .2rem .4rem;
}

/* Sheet animation */
.sheet-bg { opacity: 0; transition: opacity .2s; }
.sheet-bg.in { opacity: 1; }
.sheet-bg .sheet { transform: translateY(100%); transition: transform .25s cubic-bezier(.2,.8,.2,1); }
.sheet-bg.in .sheet { transform: translateY(0); }
.item { animation: pop .18s ease; }
@keyframes pop { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* Light mode */
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb; --surface: #ffffff; --surface-2: #eef1f7; --border: #dce1ea;
    --text: #16202e; --muted: #64748b; --accent: #059669; --accent-ink: #ffffff;
    --danger: #dc2626;
  }
  .toast.err, .btn.danger-btn { background: #fdecec; }
}
