* { box-sizing: border-box; }

:root {
  --green: #14532d;
  --green-light: #16a34a;
  --red: #b91c1c;
  --bg: #f4f5f7;
  --card: #ffffff;
  --border: #e2e4e9;
  --text: #1a1c20;
  --muted: #6b7280;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px calc(12px + env(safe-area-inset-top)) 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--green);
  color: white;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
}

.sync-status {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  font-weight: 600;
}

.sync-ok { background: rgba(255,255,255,0.15); color: #d1fae5; }
.sync-offline { background: #b91c1c; color: white; }
.sync-pending { background: #d97706; color: white; }

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px;
}

.hidden { display: none !important; }

.scan-bar {
  display: flex;
  gap: 8px;
}

.scan-bar input {
  flex: 1;
  min-width: 0;
  font-size: 18px;
  padding: 14px 16px;
  border: 2px solid var(--green-light);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
}

.scan-bar input:focus {
  outline: none;
  border-color: var(--green);
}

.camera-btn {
  flex-shrink: 0;
  width: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--green-light);
  border-radius: 12px;
  background: var(--card);
  color: var(--green);
}

.camera-btn:active { background: #eaf6ee; }

.camera-modal-card { max-width: 420px; }

.camera-video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  background: black;
  border-radius: 10px;
  overflow: hidden;
  margin: 12px 0;
}

#cameraVideo {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.camera-frame {
  position: absolute;
  inset: 15%;
  border: 3px solid rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  pointer-events: none;
}

.wake-lock-btn {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.wake-lock-btn.active {
  color: var(--green);
  border-color: var(--green);
  background: #eaf6ee;
}

.box-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 16px 0 12px 0;
}

.box-title {
  font-size: 20px;
  font-weight: 700;
}

.box-stats {
  display: flex;
  gap: 14px;
  font-size: 14px;
  color: var(--muted);
  font-weight: 600;
}

.item-list {
  list-style: none;
  margin: 0 0 16px 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}

.item-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-right: 8px;
  min-width: 0;
}

.item-sku {
  font-weight: 600;
  font-size: 15px;
  word-break: break-all;
}

.item-product-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.item-thumb {
  width: 40px;
  height: 40px;
  object-fit: contain;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: white;
  flex-shrink: 0;
}

.item-title-wrap {
  min-width: 0;
}

.item-title {
  font-weight: 600;
  font-size: 14px;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.item-sku-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.item-expiration {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.item-expiration.expiring-soon {
  color: var(--red);
}

.item-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.qty-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
}

.qty-btn:active { background: #e5e7eb; }

.item-qty {
  min-width: 28px;
  text-align: center;
  font-weight: 700;
  font-size: 16px;
}

.empty-state {
  color: var(--muted);
  text-align: center;
  padding: 24px 0;
  font-size: 14px;
}

.box-dims {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 16px;
}

.box-dims label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 10px;
}

.box-dims input {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.dims-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  margin-bottom: 0;
}

.dims-row label { margin-bottom: 0; }

.primary-btn {
  width: 100%;
  padding: 16px;
  font-size: 17px;
  font-weight: 700;
  color: white;
  background: var(--green);
  border: none;
  border-radius: 12px;
}

.primary-btn:active { background: #0f3d21; }

.secondary-btn {
  width: 100%;
  padding: 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--green);
  background: white;
  border: 2px solid var(--green);
  border-radius: 10px;
  margin-bottom: 12px;
}

.danger-btn {
  width: 100%;
  padding: 14px;
  font-size: 15px;
  font-weight: 600;
  color: white;
  background: var(--red);
  border: none;
  border-radius: 10px;
}

.link-btn {
  background: none;
  border: none;
  color: var(--green);
  font-size: 15px;
  font-weight: 600;
  padding: 8px 0;
  margin-bottom: 8px;
}

.totals-card {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 16px;
  font-weight: 700;
  font-size: 15px;
  text-align: center;
}

.totals-card div span { display: block; font-size: 20px; color: var(--green); }

.pallet-weight-row {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding-top: 10px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}

.pallet-weight-row span { color: inherit; font-size: inherit; display: inline; }

.pallet-weight-row.pallet-warning {
  color: var(--red);
}

.pallet-warning-icon {
  font-size: 15px;
}

.boxes-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.box-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
}

.box-row-title {
  font-weight: 700;
  font-size: 16px;
}

.box-row-sub {
  font-size: 13px;
  color: var(--muted);
  margin-top: 2px;
}

.box-row-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

.reopen-btn {
  font-size: 12px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--green);
  background: white;
  color: var(--green);
}

.reopen-btn:active { background: #eaf6ee; }

.duplicate-btn {
  font-size: 12px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: white;
  color: var(--text);
}

.duplicate-btn:active { background: #f0f0f0; }

.detail-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}

.detail-actions .secondary-btn { margin-bottom: 0; }

.box-row-badge {
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
}

.badge-sealed { background: #dcfce7; color: #15803d; }
.badge-current { background: #fef3c7; color: #b45309; }
/* Another device's own unsealed box — not sealed, but not yours either. Distinct
   from both so it's never mistaken for "sealed" (it isn't) or "your current box"
   (it also isn't — reopening/deleting it from here would be rejected server-side,
   since only the device that owns it can do that while it's still active). */
.badge-active-elsewhere { background: #dbeafe; color: #1d4ed8; }

.box-row-packer {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.settings-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}

.ship-title {
  font-size: 20px;
  font-weight: 700;
  margin: 4px 0 16px 0;
}

.ship-result {
  margin-top: 12px;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
}

.ship-result.ship-ok { border-color: var(--green-light); background: #eaf6ee; }
.ship-result.ship-error { border-color: var(--red); background: #fdecec; }

.ship-result h3 {
  font-size: 13px;
  margin: 0 0 6px 0;
}

.ship-result pre {
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  background: white;
  border-radius: 6px;
  padding: 8px;
  margin: 0 0 10px 0;
  border: 1px solid var(--border);
}

.settings-card h2 {
  font-size: 16px;
  margin: 0 0 8px 0;
}

.settings-card label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
  margin-top: 10px;
}

.settings-card input[type="number"] {
  display: block;
  width: 100%;
  max-width: 160px;
  margin-top: 4px;
  padding: 10px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.muted {
  color: var(--muted);
  font-size: 14px;
  margin: 0 0 12px 0;
}

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: white;
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}

.tab-btn {
  flex: 1;
  padding: 14px 0;
  background: none;
  border: none;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.tab-btn.active { color: var(--green); }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal-card {
  background: white;
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 360px;
}

.modal-card h2 {
  font-size: 17px;
  margin: 0 0 6px 0;
}

.modal-card input[type="date"],
.modal-card > input[type="text"] {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 12px 0;
  box-sizing: border-box;
}

.msku-row {
  margin: 12px 0;
}
.msku-row label {
  display: block;
  font-size: 13px;
  color: var(--muted, #666);
  margin-bottom: 4px;
}
.msku-row input[type="text"] {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-sizing: border-box;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

/* Some modals (Submit to Amazon) grow with the number of boxes/SKUs and can
   exceed the viewport height, especially on iPad/mobile — without this, the
   action buttons scroll off-screen with no way to reach them. Cap the card's
   height and let only the middle content scroll, keeping Cancel/Submit pinned
   and always reachable regardless of list length. */
.modal-card-scrollable {
  display: flex;
  flex-direction: column;
  max-height: 85vh;
}
.modal-card-scrollable .modal-scroll {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.modal-card-scrollable .modal-actions {
  flex-shrink: 0;
}

.modal-actions .primary-btn,
.modal-actions .secondary-btn {
  margin-bottom: 0;
}

.modal-actions-col {
  flex-direction: column;
}

.print-btn {
  font-size: 12px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: white;
  color: var(--text);
}

.print-btn:active { background: #f0f0f0; }

.delete-btn {
  font-size: 12px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--red);
  background: white;
  color: var(--red);
}

.delete-btn:active { background: #fdecec; }

/* Print-only box label — invisible on screen, shown (with everything else hidden) via @media print. */
.print-label { display: none; }

@media print {
  body * {
    visibility: hidden;
  }
  .print-label,
  .print-label * {
    visibility: visible;
  }
  .print-label {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 4in;
    padding: 0.3in;
    border: 2px solid #000;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    color: #000;
  }
  .print-label-title {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 12px;
    border-bottom: 2px solid #000;
    padding-bottom: 8px;
  }
  .print-label-row {
    display: flex;
    justify-content: space-between;
    font-size: 15px;
    padding: 4px 0;
  }
  .print-label-row strong {
    font-size: 16px;
  }
}
