:root {
  /* PACE palette */
  --brand-navy: #001F60;   /* PACE Navy */
  --brand-blue: #0582BE;   /* PACE Blue */
  --brand-orange: #FF9E18; /* Accent */
  --brand-gray-1: #97A3AE;
  --brand-gray-2: #758592;
  --brand-gray-3: #415364;

  /* Theme tokens */
  --bg: #f7f9fc;
  --card: #ffffff;
  --text: #0f172a;
  --muted: var(--brand-gray-2);
  --accent: var(--brand-blue);
  --accent-press: #046a99;
  --danger: #dc2626;
  --radius: 14px;
  --gap: 14px;
  --shadow: 0 6px 20px rgba(0,0,0,0.08);
}

/* Ensure elements marked as hidden are not displayed, regardless of other styles */
[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 16px/1.4 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  color: var(--text);
  /* Solid fallback; decorative gradients rendered via a fixed pseudo-element
     to avoid stretching/clipping on long pages */
  background: var(--bg);
  touch-action: manipulation; /* reduce double-tap zoom */
}

/* Fixed, full-viewport background layer so gradients don't stretch when the page is long */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background: radial-gradient(1200px 600px at 20% -10%, rgba(0,31,96,0.20), transparent),
              radial-gradient(1000px 500px at 100% 0%, rgba(5,130,190,0.18), transparent),
              var(--bg);
}

.app-bar {
  padding: 16px;
  position: sticky;
  top: 0;
  background: linear-gradient(180deg, rgba(0,31,96,0.90), rgba(5,130,190,0.70));
  backdrop-filter: blur(8px);
  border-bottom: 1px solid rgba(5,130,190,0.35);
  color: #ffffff;
  z-index: 2000; /* stay above scanner overlays and content */
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.app-bar h1 { margin: 0; font-size: 18px; letter-spacing: 0.3px; color: #ffffff; }

.badge-link {
  display: inline-block;
  padding: 4px;
  border-radius: 8px;
  background: rgba(255,255,255,0.1);
  transition: background-color 0.2s ease;
  text-decoration: none;
}

.badge-link:hover {
  background: rgba(255,255,255,0.2);
}

.badge-icon {
  width: 24px;
  height: 32px;
  display: block;
}

.container {
  padding: 16px;
  max-width: 680px;
  margin: 0 auto;
  display: grid;
  gap: var(--gap);
}

.card {
  background: var(--card);
  border: 1px solid rgba(0,0,0,0.08);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }

.row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
}

/* Compact buttons inside the input row */
.row .btn {
  padding: 8px 12px;
  white-space: nowrap;
}
.row #scanToggle {
  background: var(--brand-orange);
  border-color: var(--brand-orange);
  color: #0b1220;
}
.row #scanToggle:active { background: #e68900; border-color: #e68900; }

.input {
  width: 100%;
  appearance: none;
  background: #ffffff;
  border: 1px solid rgba(0,0,0,0.14);
  border-radius: 12px;
  color: var(--text);
  padding: 14px 14px;
  outline: none;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(5,130,190,0.28); }
.input.adjust-mode {
  border-color: var(--brand-orange);
  background: #fff8f0;
  box-shadow: 0 0 0 3px rgba(255, 158, 24, 0.1);
}

.btn {
  appearance: none;
  border: 1px solid rgba(0,0,0,0.12);
  border-radius: 12px;
  padding: 12px 16px;
  background: #ffffff;
  color: var(--text);
  font-weight: 600;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #ffffff; }
.btn.primary:active { background: var(--accent-press); border-color: var(--accent-press); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #ffffff; }
.btn.danger:disabled { display: none; }
.btn.primary.disabled { background: #e2e8f0; border-color: #e2e8f0; color: #94a3b8; cursor: not-allowed; }

.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
#lookupError { color: var(--danger); }

.scanner { display: grid; gap: 8px; }
.scanner { position: relative; z-index: 0; }
.scanner-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 2;
  border-radius: 12px;
  overflow: hidden;
  background: #f0f6fb;
  border: 1px solid rgba(0,31,96,0.10);
}
#scanner-container {
  position: relative;
  width: 100%;
  height: 100%;
}
#scanner-container video,
#scanner-container canvas {
  width: 100% !important;
  height: 100% !important;
  /* iOS needs inline playback to avoid fullscreen */
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  object-fit: cover; /* fill the frame without letterboxing */
}

/* Ensure Quagga overlay canvas sits above video and fills the frame */
.drawingBuffer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
  z-index: 2;
}

/* Mobile-first: use a tall, full preview in portrait; wide in landscape */
@media (orientation: portrait) {
  .scanner-frame {
    aspect-ratio: 4 / 3;
    max-height: 45vh;
  }
}
@media (orientation: landscape) {
  .scanner-frame {
    aspect-ratio: 16 / 9;
  }
}

.product {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 12px;
  align-items: start;
}

/* Ensure result and cart cards render above scanner stacking context */
#result { position: relative; z-index: 1; }
#cart { position: relative; z-index: 1; }
.product.small img { width: 48px; height: 48px; }
.product.small .product-info h3 { margin: 0 0 2px; font-size: 16px; }
.product img {
  width: 84px;
  height: 84px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid rgba(0,0,0,0.08);
}
.product-info h2 { margin: 0 0 6px; font-size: 18px; }
.muted { color: var(--muted); margin: 0; }
.code { margin: 6px 0 0; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--brand-navy); }

.qty-line {
  margin-top: 12px;
}
.qty-actions { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.qty-button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px; /* slightly smaller */
}
.delete-btn { white-space: nowrap; padding: 10px 12px; }
.qty-button span { opacity: 0.9; }
.qty-button strong { font-size: 22px; }

.list { list-style: none; padding: 0 0 64px 0; margin: 0; display: grid; gap: 8px; }
.list li:last-child { margin-bottom: 8px; }
.list li { position: relative; overflow: hidden; border-radius: 10px; }
.list li .swipe-item {
  display: flex;
  justify-content: space-between;
  padding: 10px 12px;
  background: #ffffff;
  border: 1px solid rgba(0,31,96,0.10);
  transition: transform 0.18s ease;
  position: relative;
  z-index: 0;
}
.list li .swipe-delete {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 90px;
  border-radius: 0;
  right: -96px; /* hidden off-canvas by default */
  opacity: 0;
  transition: right 0.18s ease, opacity 0.18s ease;
}
.list li.revealed .swipe-delete { right: 0; opacity: 1; }

/* Review page item styles */
.review-item { display: grid; grid-template-columns: 96px 1fr; gap: 10px; align-items: start; padding: 10px; background: #ffffff; border: 1px solid rgba(0,0,0,0.08); border-radius: 12px; }
.review-left { display: contents; }
.review-thumb { width: 96px; height: 96px; object-fit: cover; border-radius: 10px; border: 1px solid rgba(255,255,255,0.1); }
.review-content { display: grid; }
.review-name { font-weight: 700; font-size: 16px; line-height: 1.25; }
.review-code { color: var(--muted); font-size: 13px; }
.review-bottom { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; }
.qty-controls { display: grid; grid-template-columns: auto minmax(56px, auto) auto; align-items: center; gap: 8px; -webkit-user-select: none; user-select: none; }
.qty-controls .qty-btn { padding: 8px 10px; width: 36px; height: 36px; touch-action: manipulation; }
.qty-controls .qty-val { display: inline-block; padding: 4px 8px; min-width: 56px; text-align: center; border-radius: 6px; background: transparent; white-space: nowrap; font-variant-numeric: tabular-nums; font-feature-settings: 'tnum' 1, 'lnum' 1; }
.del-btn { height: 36px; min-width: 40px; padding: 0 10px; }

/* Compact tweaks for small devices */
@media (max-width: 430px) {
  .review-item { grid-template-columns: 84px 1fr; }
  .review-thumb { width: 84px; height: 84px; }
  .qty-controls { grid-template-columns: auto minmax(50px, auto) auto; gap: 6px; }
  .qty-controls .qty-btn { width: 32px; height: 32px; padding: 6px 8px; }
  .qty-controls .qty-val { min-width: 50px; }
}

.cart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.cart-footer { margin-top: 12px; padding: 12px 0 14px 0; display: flex; justify-content: flex-end; border-top: 1px solid rgba(0,31,96,0.08); position: relative; z-index: 1; }

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0,0,0,0.55);
  padding: 16px;
  z-index: 1000;
}
.modal-content {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.modal-actions {
  margin-top: 12px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* Not-found modal: center a single, larger button */
#notFoundModal .modal-actions {
  display: flex;
  justify-content: center;
}
#notFoundContinue {
  min-width: 200px;
  padding: 14px 20px;
  font-size: 16px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  background: var(--brand-navy);
  color: #ffffff;
  border: 1px solid rgba(0,31,96,0.25);
  padding: 10px 14px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 1100;
}
.toast.error { background: var(--danger); border-color: var(--danger); font-size: 32px; padding: 20px 28px; }
.toast.success { background: #16a34a; border-color: #16a34a; font-size: 32px; padding: 20px 28px; }

/* Subtle pop highlight when quantity changes */
@keyframes qtyPop {
  0% { transform: scale(1); }
  40% { transform: scale(1.22); }
  100% { transform: scale(1); }
}
.qty-pop { animation: qtyPop 240ms ease-out; }
