:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #15171c;
  --muted: #6b7280;
  --line: #e5e7eb;
  --input: #f0f1f4;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --orange: #d97706;
  --orange-bg: #fef3c7;
  --radius: 14px;
  --tabs-h: 58px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #181b21;
    --text: #eef0f3;
    --muted: #9097a3;
    --line: #2a2e36;
    --input: #22262e;
    --accent: #3b82f6;
    --green: #22c55e;
    --green-bg: #12301f;
    --red: #f87171;
    --red-bg: #3a1717;
    --orange: #fbbf24;
    --orange-bg: #3a2c0c;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 22px; margin: 4px 0 14px; letter-spacing: -0.01em; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 20px 4px 8px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; }
.right { text-align: right; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

#app { max-width: 760px; margin: 0 auto; }
#top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px; background: var(--bg);
}
.brand { font-weight: 700; letter-spacing: -0.02em; }
#top-title { color: var(--muted); font-size: 13px; }
main { padding: 4px 16px calc(var(--tabs-h) + env(safe-area-inset-bottom) + 24px); }
.loading { padding: 40px 0; text-align: center; color: var(--muted); }

#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; justify-content: center;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
#tabs a {
  flex: 1; max-width: 150px; height: var(--tabs-h); display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 2px; color: var(--muted); font-size: 11px; font-weight: 500;
}
#tabs a .ico { font-size: 19px; line-height: 1; }
#tabs a.active { color: var(--accent); }

.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; border: 1px solid var(--line); }
.card.flush { padding: 0; overflow: hidden; }
.list > * { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); color: inherit; }
.list > *:last-child { border-bottom: 0; }
.list a:active { background: var(--input); }

label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12.5px; color: var(--muted); margin: 0 2px 5px; }
input, select, textarea {
  width: 100%; font: inherit; font-size: 16px; color: var(--text); background: var(--input);
  border: 1px solid transparent; border-radius: 10px; padding: 11px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); background: var(--card); }
textarea { min-height: 96px; resize: vertical; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 15px; }
input[type=checkbox] { width: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 600;
  font-size: 15px; padding: 11px 16px; border-radius: 11px; border: 0; background: var(--input); color: var(--text);
  cursor: pointer; white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.green { background: var(--green); color: #fff; }
.btn.danger { background: var(--red-bg); color: var(--red); }
.btn.ghost { background: transparent; color: var(--accent); padding: 8px 10px; }
.btn.block { width: 100%; padding: 14px; font-size: 16px; }
.btn.small { padding: 7px 11px; font-size: 13.5px; border-radius: 9px; }
.btn:disabled { opacity: .45; pointer-events: none; }

.seg { display: flex; background: var(--input); border-radius: 11px; padding: 3px; gap: 3px; margin-bottom: 12px; }
.seg button { flex: 1; font: inherit; font-weight: 600; font-size: 14px; border: 0; background: transparent; color: var(--muted); padding: 8px 6px; border-radius: 8px; cursor: pointer; }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg.pay button.on[data-v="paid"] { background: var(--green); color: #fff; }
.seg.pay button.on[data-v="debt"] { background: var(--orange); color: #fff; }

.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.badge.paid { background: var(--green-bg); color: var(--green); }
.badge.debt { background: var(--orange-bg); color: var(--orange); }
.badge.info { background: var(--input); color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 20px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .v.green { color: var(--green); }
.stat .v.orange { color: var(--orange); }

.alert { border-radius: 12px; padding: 11px 13px; margin-bottom: 12px; font-size: 14px; }
.alert.err { background: var(--red-bg); color: var(--red); }
.alert.warn { background: var(--orange-bg); color: var(--orange); }
.alert ul { margin: 6px 0 0; padding-left: 18px; }

.group-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; cursor: pointer; }
.group-head .model { font-weight: 600; }
.group-items { border-top: 1px solid var(--line); }
.serial-row { display: flex; align-items: center; gap: 8px; padding: 8px 14px; border-bottom: 1px solid var(--line); font-size: 14px; color: inherit; }
.serial-row:last-child { border-bottom: 0; }
.x { border: 0; background: transparent; color: var(--muted); font-size: 18px; padding: 2px 8px; cursor: pointer; }

.cart-group { border: 1px solid var(--line); border-radius: 12px; margin-bottom: 10px; overflow: hidden; background: var(--card); }
.cart-group .head { display: flex; gap: 10px; align-items: center; padding: 10px 12px; }
.cart-group .head input { width: 120px; text-align: right; padding: 8px 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px 10px; }
.chip { display: inline-flex; align-items: center; gap: 2px; background: var(--input); border-radius: 8px; padding: 3px 3px 3px 8px; font-size: 12.5px; font-family: ui-monospace, Menlo, monospace; }
.chip .x { font-size: 14px; padding: 0 5px; }

.total-bar { display: flex; align-items: baseline; gap: 8px; margin: 6px 2px 14px; }
.total-bar .sum { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%); top: calc(env(safe-area-inset-top) + 10px);
  z-index: 200; pointer-events: none;   /* never block taps on buttons underneath */ background: #111827; color: #fff; padding: 11px 16px; border-radius: 12px; font-size: 14px;
  max-width: calc(100% - 32px); box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
#toast.err { background: var(--red); }

/* login */
.login { max-width: 360px; margin: 12vh auto 0; padding: 0 16px; }
.login .logo { font-size: 28px; font-weight: 800; letter-spacing: -0.03em; margin-bottom: 4px; }

/* scanner */
.modal { position: fixed; inset: 0; z-index: 100; background: #000; display: flex; flex-direction: column; }
.scan-wrap { position: relative; flex: 1; overflow: hidden; }
.scan-wrap video { width: 100%; height: 100%; object-fit: cover; }
.scan-frame { position: absolute; left: 8%; right: 8%; top: 38%; height: 22%; border: 2px solid rgba(255,255,255,.85); border-radius: 14px; box-shadow: 0 0 0 9999px rgba(0,0,0,.35); }
.scan-bar { background: #111; color: #fff; padding: 12px 16px calc(env(safe-area-inset-bottom) + 12px); }
.scan-last { min-height: 40px; font-size: 14px; margin-bottom: 10px; white-space: pre-line; }
.scan-last.ok { color: #4ade80; }
.scan-last.bad { color: #f87171; }
.scan-last.muted { color: #9ca3af; }
.scan-top { position: absolute; top: calc(env(safe-area-inset-top) + 12px); left: 16px; right: 16px; color: #fff; display: flex; justify-content: space-between; font-weight: 600; text-shadow: 0 1px 3px #000; }

/* invoice */
body.print-mode #top, body.print-mode #tabs { display: none !important; }
body.print-mode main { padding-bottom: 24px; }
.invoice { background: #fff; color: #111; border-radius: 6px; padding: 28px 26px; font-size: 13.5px; }
.invoice h1 { font-size: 20px; margin: 0 0 4px; color: #111; }
.invoice table { width: 100%; border-collapse: collapse; margin: 14px 0; }
.invoice th, .invoice td { border-bottom: 1px solid #ddd; padding: 6px 6px; text-align: left; vertical-align: top; }
.invoice th { font-size: 12px; color: #555; font-weight: 600; }
.invoice .tot td { border-bottom: 0; font-weight: 700; font-size: 15px; }
.invoice .meta { display: grid; grid-template-columns: auto 1fr; gap: 3px 14px; margin-top: 12px; }
.invoice .meta div:nth-child(odd) { color: #666; }
.invoice .sign { display: flex; gap: 30px; margin-top: 36px; }
.invoice .sign div { flex: 1; border-top: 1px solid #999; padding-top: 4px; font-size: 12px; color: #555; }
.invoice-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
@media print {
  @page { margin: 12mm; }
  html, body { background: #fff !important; }
  .invoice-actions, #toast { display: none !important; }
  main { padding: 0 !important; }
  .invoice { padding: 0; border-radius: 0; }
}

@media (min-width: 700px) {
  main { padding-top: 10px; }
  .two { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
}
