:root {
  --bg: #0f172a;
  --bg-2: #1e293b;
  --card: #1e293b;
  --card-2: #273449;
  --line: #334155;
  --txt: #f1f5f9;
  --muted: #94a3b8;
  --primary: #22c55e;
  --primary-d: #16a34a;
  --accent: #38bdf8;
  --danger: #ef4444;
  --warn: #f59e0b;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--txt);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; flex-direction: column; min-height: 100%; }

/* Top bar */
#topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: rgba(15,23,42,.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
#title { font-weight: 700; font-size: 17px; flex: 1; }
#title.logo {
  font-weight: 900; font-size: 22px; letter-spacing: .01em;
  background: linear-gradient(120deg, var(--primary), var(--accent));
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; color: transparent;
}
.logo-hero {
  font-size: 44px !important; font-weight: 900; margin: 4px 0 2px;
  background: linear-gradient(120deg, var(--primary), var(--accent));
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; color: transparent;
}
.badge {
  background: var(--card-2); color: var(--muted);
  font-size: 12px; padding: 4px 10px; border-radius: 999px;
  border: 1px solid var(--line);
}
.icon-btn {
  background: none; border: none; color: var(--txt);
  font-size: 30px; line-height: 1; padding: 0 6px; cursor: pointer;
}

/* Views */
#views { flex: 1; display: flex; }
.view {
  flex: 1; padding: 16px 16px calc(24px + var(--safe-bot));
  width: 100%; max-width: 640px; margin: 0 auto;
  animation: fade .18s ease;
}
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* Login */
.login-form { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; }
.login-form input {
  background: var(--card); border: 1px solid var(--line); color: var(--txt);
  border-radius: 14px; padding: 16px; font-size: 17px;
}
.login-form input:focus { outline: none; border-color: var(--accent); }
#loginError { color: var(--danger); min-height: 18px; }

/* Home */
.home-hero { text-align: center; padding: 24px 0 10px; }
.home-hero.compact { padding: 12px 0 4px; }
.home-hero.compact h1 { font-size: 22px; }
.stat-num.accent { color: var(--primary); }
.hero-icon { font-size: 56px; }
.home-hero h1 { margin: 6px 0 2px; font-size: 26px; }
.muted { color: var(--muted); }
.tiny { font-size: 12px; }
.center { text-align: center; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 16px 0; }
.stat { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.stat-num { font-size: 30px; font-weight: 800; }
.stat-lbl { font-size: 12px; color: var(--muted); }

.home-actions { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid transparent; border-radius: 14px;
  font-size: 16px; font-weight: 600; padding: 14px 18px;
  cursor: pointer; text-align: center; user-select: none;
  transition: transform .06s ease, background .15s ease, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn-lg { padding: 17px 18px; font-size: 17px; }
.btn.full { width: 100%; }
.btn-primary { background: var(--primary); color: #052e16; }
.btn-primary:disabled { background: var(--card-2); color: var(--muted); cursor: not-allowed; }
.btn-secondary { background: var(--card-2); color: var(--txt); border-color: var(--line); }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--line); }
.btn-ghost.danger { color: var(--danger); border-color: rgba(239,68,68,.35); }

/* Scan view */
#view-scan { position: relative; padding: 0; max-width: none; }
#reader { width: 100%; min-height: 60vh; background: #000; }
#reader video { width: 100% !important; object-fit: cover; }
.scan-overlay {
  position: absolute; inset: 0; pointer-events: none;
  display: flex; align-items: center; justify-content: center;
}
.scan-frame {
  width: 74%; max-width: 340px; aspect-ratio: 1.5/1;
  border: 3px solid rgba(56,189,248,.9); border-radius: 18px;
  box-shadow: 0 0 0 2000px rgba(0,0,0,.35);
}
.scan-controls {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 14px 16px calc(16px + var(--safe-bot));
  background: linear-gradient(to top, rgba(15,23,42,.96), rgba(15,23,42,.6) 70%, transparent);
}
.manual-row { display: flex; gap: 8px; margin-top: 8px; }
.manual-row input {
  flex: 1; background: var(--card); border: 1px solid var(--line);
  color: var(--txt); border-radius: 12px; padding: 13px 14px; font-size: 16px;
}
.manual-row input:focus { outline: none; border-color: var(--accent); }

/* Product view */
.product-head { display: flex; gap: 14px; margin-bottom: 8px; }
.product-img {
  width: 92px; height: 92px; flex: none; border-radius: 14px;
  background: var(--card-2) center/cover no-repeat;
  border: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center;
  font-size: 34px; color: var(--muted);
  position: relative;
}
.product-img.has-img { cursor: zoom-in; }
.product-img.has-img::after {
  content: "🔍"; position: absolute; bottom: -6px; inset-inline-start: -6px;
  width: 26px; height: 26px; font-size: 13px;
  background: var(--card-2); border: 1px solid var(--line); border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}

.img-col { display: flex; flex-direction: column; gap: 7px; flex: none; width: 92px; }
.take-img-btn {
  display: flex; align-items: center; justify-content: center; gap: 4px;
  background: var(--primary); color: #052e16;
  font-size: 11px; font-weight: 800; padding: 7px 4px;
  border-radius: 10px; cursor: pointer; text-align: center;
  box-shadow: 0 3px 10px rgba(34, 197, 94, .3);
}
.take-img-btn:active { transform: scale(.97); }
.take-img-btn.uploading { opacity: .6; pointer-events: none; }

/* swipe-to-confirm (bank style) — internally LTR: knob starts left, drags right */
.swipe-confirm {
  position: relative; height: 58px; border-radius: 16px;
  background: var(--card); border: 1px solid var(--line);
  overflow: hidden; direction: ltr;
  user-select: none; -webkit-user-select: none; touch-action: none;
  margin-bottom: 10px;
}
.swipe-fill {
  position: absolute; left: 0; top: 0; bottom: 0; width: 0;
  background: linear-gradient(90deg, rgba(34, 197, 94, .38), rgba(34, 197, 94, .12));
}
.swipe-label {
  position: absolute; inset: 0; direction: rtl;
  display: flex; align-items: center; justify-content: center;
  padding-inline: 16px 64px;
  font-size: 13.5px; font-weight: 700; color: var(--muted);
  animation: swipe-hint 2.6s ease-in-out infinite;
}
@keyframes swipe-hint {
  0%, 100% { opacity: .75; } 50% { opacity: 1; }
}
.swipe-knob {
  position: absolute; left: 4px; top: 4px; width: 50px; height: 50px;
  border-radius: 13px; background: var(--primary); color: #052e16;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; font-weight: 900; cursor: grab;
  box-shadow: 0 4px 14px rgba(34, 197, 94, .45);
  will-change: transform;
}
.swipe-knob:active { cursor: grabbing; }
.swipe-confirm.done { border-color: rgba(34, 197, 94, .6); }
.swipe-confirm.done .swipe-label { color: var(--primary); animation: none; }

/* cancel mode — red, knob starts at the RIGHT and drags LEFT */
.swipe-confirm.cancel .swipe-fill {
  background: linear-gradient(270deg, rgba(239, 68, 68, .38), rgba(239, 68, 68, .12));
}
.swipe-confirm.cancel .swipe-knob {
  background: var(--danger); color: #fff;
  box-shadow: 0 4px 14px rgba(239, 68, 68, .55), inset 0 0 0 2px rgba(255, 255, 255, .25);
}
.swipe-confirm.cancel .swipe-label { padding-inline: 64px 16px; }
.swipe-confirm.cancel.done { border-color: rgba(239, 68, 68, .6); }
.swipe-confirm.cancel.done .swipe-label { color: var(--danger); animation: none; }

/* product image lightbox */
.img-modal {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(2, 6, 23, .88); backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center;
  cursor: zoom-out; animation: fade .15s ease;
}
.img-modal img {
  max-width: 94vw; max-height: 86vh; border-radius: 16px;
  background: #fff; box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
}
.product-info { flex: 1; min-width: 0; }
.product-info h2 { margin: 0 0 6px; font-size: 16px; line-height: 1.3; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 4px; }
.chip {
  font-size: 11px; padding: 3px 9px; border-radius: 999px;
  background: rgba(56,189,248,.14); color: var(--accent); border: 1px solid rgba(56,189,248,.25);
  font-variant-numeric: tabular-nums;
}
.chip.subtle { background: var(--card-2); color: var(--muted); border-color: var(--line); }

.progress-wrap { display: flex; align-items: center; gap: 10px; margin: 16px 0 8px; }
.progress-bar { flex: 1; height: 10px; background: var(--card-2); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--primary); border-radius: 999px; transition: width .3s ease; }
.progress-txt { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.section-label { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 16px 4px 8px; }

.pallet-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.pallet-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px; cursor: pointer;
}
.pallet-item.done { border-color: rgba(34,197,94,.5); background: rgba(34,197,94,.07); }
.pallet-qty {
  width: 46px; height: 46px; flex: none; border-radius: 12px;
  background: var(--card-2); display: flex; flex-direction: column;
  align-items: center; justify-content: center; font-weight: 800; font-size: 20px;
}
.pallet-qty small { font-size: 9px; font-weight: 600; color: var(--muted); }
.pallet-main { flex: 1; min-width: 0; }
.pallet-name { font-weight: 600; font-size: 15px; }
.pallet-sub { font-size: 12px; color: var(--muted); }
.pallet-status { font-size: 22px; }
.pallet-item.done .pallet-status::after { content: "✓"; color: var(--primary); }
.pallet-item:not(.done) .pallet-status::after { content: "‹"; color: var(--muted); }

/* Drop view */
.drop-head { text-align: center; margin: 6px 0 18px; }
.drop-store { font-size: 22px; font-weight: 800; }
.drop-qty { font-size: 16px; color: var(--muted); margin-top: 4px; }
.drop-qty b { color: var(--primary); font-size: 26px; }
.drop-prod { font-size: 13px; margin-top: 6px; }

.photo-slots { display: grid; grid-template-columns: 1fr; gap: 12px; max-width: 320px; margin: 0 auto; }
.photo-slot { display: block; }
.photo-preview {
  aspect-ratio: 3/4; border-radius: 16px; border: 2px dashed var(--line);
  background: var(--card) center/cover no-repeat;
  display: flex; align-items: center; justify-content: center; text-align: center;
  color: var(--muted); font-size: 13px; padding: 10px; position: relative; overflow: hidden;
}
.photo-preview.has-photo { border-style: solid; border-color: var(--primary); }
.photo-preview.has-photo .ph-label { display: none; }
.photo-preview.has-photo::after {
  content: "✓"; position: absolute; top: 8px; inset-inline-start: 8px;
  background: var(--primary); color: #052e16; width: 26px; height: 26px;
  border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 800;
}
.ph-label { pointer-events: none; }

.drop-actions { margin-top: 20px; display: flex; flex-direction: column; gap: 10px; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bot));
  transform: translateX(-50%);
  background: var(--card-2); color: var(--txt);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 18px; font-size: 14px; z-index: 60; max-width: 88%;
  box-shadow: 0 10px 30px rgba(0,0,0,.5);
  animation: fade .18s ease;
}
.toast.ok { border-color: var(--primary); }
.toast.err { border-color: var(--danger); }

[hidden] { display: none !important; }
