:root {
  --bg: #eef3f8;
  --surface: #ffffff;
  --surface-soft: #f6f8fb;
  --ink: #101113;
  --muted: #667085;
  --line: #d6dde6;
  --black: #050505;
  --primary: #f97316;
  --primary-strong: #c2410c;
  --primary-soft: #fff3e8;
  --accent: #0ea5e9;
  --accent-strong: #075985;
  --accent-soft: #e7f6ff;
  --success: #166534;
  --warning: #c2410c;
  --danger: #b91c1c;
  --info: #0f6fb5;
  --radius: 8px;
  --shadow: 0 12px 28px rgba(5, 5, 5, .08);
}

* { box-sizing: border-box; }
html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, #f8fafc 0, var(--bg) 100%);
  min-height: 100vh;
  overflow-x: hidden;
}

button, input, select, textarea {
  font: inherit;
}

a { color: var(--accent-strong); }

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.4rem clamp(1rem, 4vw, 2.5rem);
  background: var(--black);
  color: white;
  border-bottom: 4px solid var(--primary);
  box-shadow: 0 12px 28px rgba(5, 5, 5, .16);
  position: sticky;
  top: 0;
  z-index: 20;
}

h1, h2, h3 { margin: 0; line-height: 1.1; }
h1 { font-size: clamp(1.8rem, 4vw, 3rem); }
h2 { font-size: clamp(1.35rem, 3vw, 2rem); }
h3 { font-size: 1.05rem; }

.app-header h1 { color: white; }

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-width: 0;
}

.brand-logo {
  width: clamp(150px, 18vw, 250px);
  height: 70px;
  object-fit: contain;
  background: transparent;
  border: 0;
  border-radius: 0;
  padding: 0;
}

.eyebrow {
  color: var(--muted);
  text-transform: uppercase;
  font-size: .72rem;
  font-weight: 800;
  letter-spacing: 0;
  margin: 0 0 .35rem;
}

.app-header .eyebrow { color: var(--primary); }

.header-actions {
  display: flex;
  gap: .7rem;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.session-box {
  display: flex;
  align-items: center;
  gap: .45rem;
  color: #cbd5e1;
  font-size: .82rem;
  font-weight: 800;
}

.session-box[hidden] { display: none; }

.sync-badge {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: .35rem .6rem;
  border: 1px solid rgba(255,255,255,.16);
  border-radius: var(--radius);
  color: #dbeafe;
  background: rgba(14, 165, 233, .14);
  font-size: .78rem;
  font-weight: 850;
  white-space: nowrap;
}
.sync-badge[data-mode="server"] {
  color: #bbf7d0;
  background: rgba(22, 101, 52, .22);
}
.sync-badge[data-mode="error"] {
  color: #fecaca;
  background: rgba(185, 28, 28, .24);
}

.pwa-install-btn[hidden] { display: none; }

.role-switcher {
  display: grid;
  gap: .25rem;
  font-weight: 700;
  color: #cbd5e1;
  font-size: .78rem;
}

.app-shell {
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr);
  gap: 1rem;
  padding: clamp(1rem, 3vw, 2rem);
  max-width: 1600px;
  margin: 0 auto;
}

.tabs {
  display: grid;
  gap: .45rem;
  align-self: start;
  position: sticky;
  top: 7rem;
}

.tab {
  border: 1px solid transparent;
  background: rgba(255, 255, 255, .78);
  color: var(--muted);
  padding: .85rem 1rem;
  text-align: left;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 800;
  transition: .16s ease;
}
.tab:hover, .tab.active {
  background: var(--black);
  border-color: var(--primary);
  color: white;
  box-shadow: 0 8px 20px rgba(5,5,5,.12);
}

.view { min-width: 0; }

.card {
  background: rgba(255, 255, 255, .92);
  border: 1px solid rgba(219, 227, 239, .88);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(1rem, 2vw, 1.35rem);
}

.grid { display: grid; gap: 1rem; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }

.kpi {
  display: grid;
  gap: .45rem;
  min-height: 135px;
}
.kpi strong { font-size: clamp(2rem, 6vw, 3.4rem); line-height: 1; }
.kpi span { color: var(--muted); font-weight: 700; }
.kpi.warning { border-color: rgba(249,115,22,.38); background: #fff7ed; }
.kpi.danger { border-color: rgba(185,28,28,.35); background: #fff1f2; }
.kpi.success { border-color: rgba(22,101,52,.3); background: #f0fdf4; }
.kpi.info { border-color: rgba(14,165,233,.32); background: var(--accent-soft); }

.toolbar {
  display: flex;
  gap: .7rem;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.toolbar-left, .toolbar-right {
  display: flex;
  gap: .55rem;
  flex-wrap: wrap;
  align-items: center;
}
.toolbar-actions {
  display: flex;
  gap: .55rem;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
}

.btn {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .72rem .95rem;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  font-weight: 850;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  min-height: 42px;
  line-height: 1.15;
  text-align: center;
  touch-action: manipulation;
  overflow-wrap: anywhere;
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 8px 18px rgba(15,23,42,.08); }
.btn.primary { background: var(--primary); color: var(--black); border-color: var(--primary); }
.btn.secondary { background: var(--accent-soft); color: var(--accent-strong); border-color: rgba(14,165,233,.32); }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.danger { color: var(--danger); border-color: rgba(185,28,28,.25); }
.btn.small { padding: .45rem .62rem; min-height: 36px; border-radius: var(--radius); font-size: .86rem; }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; box-shadow: none; }

.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: white;
  cursor: pointer;
  font-size: 1.4rem;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: white;
  color: var(--ink);
  border-radius: var(--radius);
  padding: .7rem .8rem;
  outline: none;
  min-height: 44px;
  font-size: 16px;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(249,115,22,.16);
}
label {
  display: grid;
  gap: .35rem;
  color: var(--muted);
  font-weight: 800;
  font-size: .84rem;
}
label input, label select, label textarea { color: var(--ink); font-weight: 600; }
.input-with-button { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .35rem; }
.location-field {
  display: grid;
  gap: .55rem;
}
.location-generator {
  display: grid;
  grid-template-columns: .8fr .8fr .8fr auto;
  gap: .45rem;
  align-items: end;
  padding: .65rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
}
.location-generator label { gap: .25rem; }
.location-generator input {
  min-height: 36px;
  text-align: center;
  text-transform: uppercase;
}
.location-generator .btn { min-height: 36px; white-space: nowrap; }

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: white;
  -webkit-overflow-scrolling: touch;
}
table {
  width: 100%;
  border-collapse: collapse;
  min-width: 820px;
}
th, td {
  padding: .75rem .85rem;
  border-bottom: 1px solid #edf2f7;
  text-align: left;
  vertical-align: middle;
}
th {
  background: var(--surface-soft);
  color: var(--muted);
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: 0;
  white-space: nowrap;
}
tr:hover td { background: #fafafa; }
tr.low td { background: #fff7ed; }
tr.zero td { background: #fff1f2; }
td.actions { white-space: nowrap; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: .25rem .55rem;
  font-size: .78rem;
  font-weight: 900;
  background: white;
  color: var(--muted);
  white-space: nowrap;
}
.badge.success { color: var(--success); border-color: rgba(22,101,52,.25); background: #f0fdf4; }
.badge.warning { color: var(--warning); border-color: rgba(249,115,22,.3); background: #fff7ed; }
.badge.danger { color: var(--danger); border-color: rgba(185,28,28,.25); background: #fff1f2; }
.badge.info { color: var(--info); border-color: rgba(29,78,216,.25); background: #eff6ff; }

.muted { color: var(--muted); }
.small-text { font-size: .86rem; }
.section-title {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}
.section-title p { margin: .35rem 0 0; color: var(--muted); }

.modal {
  border: none;
  padding: 0;
  background: transparent;
  width: min(920px, calc(100vw - 1.2rem));
}
.modal::backdrop { background: rgba(15, 23, 42, .62); backdrop-filter: blur(4px); }
.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: clamp(1rem, 2vw, 1.4rem);
  border: 1px solid var(--line);
  box-shadow: 0 30px 70px rgba(0,0,0,.22);
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: .8rem;
}
.full { grid-column: 1 / -1; }
.product-photo-field {
  display: grid;
  grid-template-columns: 150px minmax(0, 1fr);
  gap: 1rem;
  align-items: center;
  padding: .85rem;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
}
.product-photo-preview {
  width: 150px;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: white;
  color: var(--muted);
  font-size: .82rem;
  font-weight: 850;
  text-align: center;
}
.product-photo-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.product-photo-actions {
  display: flex;
  gap: .55rem;
  align-items: center;
  flex-wrap: wrap;
}
.product-photo-actions p { width: 100%; margin: 0; }
.product-thumb {
  width: 58px;
  height: 58px;
  object-fit: cover;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: white;
}
.product-thumb.empty {
  color: var(--muted);
  font-size: .72rem;
  font-weight: 850;
  text-align: center;
}
.modal-actions {
  display: flex;
  gap: .6rem;
  align-items: center;
  margin-top: 1rem;
  flex-wrap: wrap;
}
.spacer { flex: 1; }

#scannerVideo {
  width: 100%;
  max-height: 420px;
  background: #0f172a;
  border-radius: var(--radius);
  display: block;
  object-fit: cover;
}
.scanner-fallback {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: .7rem;
  align-items: end;
  margin-top: 1rem;
}

.status-row {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: .55rem;
}
.status-card {
  padding: .85rem;
  border-radius: var(--radius);
  background: var(--surface-soft);
  border: 1px solid var(--line);
}
.status-card strong { display: block; font-size: 1.45rem; }
.status-card span { color: var(--muted); font-weight: 700; font-size: .82rem; }

.list {
  display: grid;
  gap: .55rem;
}
.list-item {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .85rem;
  background: white;
}
.list-item p { margin: .25rem 0 0; color: var(--muted); }

.report-box {
  border: 1px dashed #cbd5e1;
  border-radius: var(--radius);
  padding: 1rem;
  background: #fff;
}
.report-total {
  text-align: right;
  font-size: 1.25rem;
  font-weight: 950;
}
.report-controls {
  align-items: end;
  grid-template-columns: repeat(4, minmax(0, 1fr));
}
.print-report {
  display: grid;
  gap: 1rem;
}
.print-report-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  border-bottom: 3px solid var(--primary);
  padding-bottom: .9rem;
}
.print-report-title {
  display: grid;
  gap: .25rem;
}
.print-report-logo {
  width: 150px;
  max-height: 64px;
  object-fit: contain;
}
.report-meta {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: .6rem;
}
.report-meta div {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .7rem;
  background: var(--surface-soft);
}
.report-meta span {
  display: block;
  color: var(--muted);
  font-size: .76rem;
  font-weight: 800;
  text-transform: uppercase;
}
.report-meta strong {
  display: block;
  margin-top: .25rem;
  font-size: 1.05rem;
}
.report-subtitle {
  color: var(--muted);
  margin: .25rem 0 0;
}

.label-builder {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 1rem;
}
.label-sheet {
  background: white;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
  min-height: 360px;
}
.labels-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: .65rem;
}
.label-preview {
  border: 1px solid #111827;
  min-height: 126px;
  padding: .55rem;
  background: white;
  display: grid;
  align-content: space-between;
  page-break-inside: avoid;
}
.label-preview h4 { margin: 0; font-size: .98rem; }
.label-preview p { margin: .1rem 0; font-size: .74rem; }
.label-preview svg { width: 100%; height: 42px; }

.toast-host {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  display: grid;
  gap: .6rem;
  z-index: 100;
}
.toast {
  background: var(--black);
  color: white;
  border-radius: var(--radius);
  border-top: 3px solid var(--primary);
  padding: .85rem 1rem;
  box-shadow: var(--shadow);
  max-width: 360px;
  font-weight: 800;
}

.empty-state {
  padding: 2rem;
  text-align: center;
  color: var(--muted);
  border: 1px dashed #cbd5e1;
  border-radius: var(--radius);
  background: #fff;
}

.login-card {
  max-width: 520px;
  margin: clamp(2rem, 7vw, 5rem) auto;
}

.login-card form {
  display: grid;
  gap: .8rem;
  margin-top: 1rem;
}

.inline-check {
  display: flex;
  align-items: center;
  gap: .55rem;
  color: var(--ink);
}

.inline-check input { width: auto; }

.user-role-guide { margin-top: 1rem; }

.user-table td,
.user-table th {
  white-space: nowrap;
}

@media (max-width: 1050px) {
  .app-shell { grid-template-columns: 1fr; }
  .tabs {
    position: static;
    display: flex;
    overflow-x: auto;
    padding-bottom: .35rem;
  }
  .tab { white-space: nowrap; }
  .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .status-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .label-builder { grid-template-columns: 1fr; }
  .report-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .report-meta { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 720px) {
  body { background: var(--bg); }
  .app-header {
    position: static;
    align-items: flex-start;
    flex-direction: column;
    gap: .75rem;
    padding: .8rem .75rem;
    border-bottom-width: 3px;
  }
  .app-header h1 { font-size: 1.55rem; }
  .app-header .eyebrow { font-size: .68rem; }
  .brand-lockup {
    align-items: center;
    gap: .65rem;
    width: 100%;
  }
  .brand-logo {
    width: 118px;
    height: 50px;
    flex: 0 0 auto;
  }
  .header-actions {
    justify-content: flex-start;
    width: 100%;
    gap: .5rem;
  }
  .header-actions > * { flex: 1 1 150px; }
  .header-actions .btn,
  .header-actions select {
    min-height: 40px;
  }
  .session-box {
    width: 100%;
    justify-content: space-between;
  }
  .sync-badge {
    justify-content: center;
    text-align: center;
    white-space: normal;
  }
  .app-shell {
    gap: .75rem;
    padding: .65rem .6rem calc(1rem + env(safe-area-inset-bottom));
  }
  .tabs {
    position: sticky;
    top: 0;
    z-index: 18;
    margin: -.65rem -.6rem 0;
    padding: .55rem .6rem;
    background: rgba(238, 243, 248, .96);
    box-shadow: 0 8px 18px rgba(5, 5, 5, .08);
  }
  .tab {
    flex: 0 0 auto;
    min-height: 40px;
    padding: .58rem .72rem;
    font-size: .9rem;
  }
  .view { min-width: 0; }
  .card {
    padding: .85rem;
    box-shadow: 0 8px 20px rgba(5, 5, 5, .06);
  }
  .grid { gap: .75rem; }
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr; }
  .kpi { min-height: 96px; }
  .kpi strong { font-size: 2rem; }
  .section-title {
    display: grid;
    gap: .75rem;
  }
  .toolbar-actions {
    width: 100%;
    justify-content: stretch;
  }
  .toolbar-actions .btn {
    flex: 1 1 145px;
  }
  .form-grid { grid-template-columns: 1fr; }
  .location-generator { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .location-generator .btn { grid-column: 1 / -1; }
  .product-photo-field { grid-template-columns: 1fr; }
  .product-photo-preview { width: 100%; max-width: 220px; }
  .product-photo-actions .btn { flex: 1 1 150px; }
  .input-with-button {
    grid-template-columns: 1fr;
  }
  .status-row { grid-template-columns: 1fr 1fr; }
  .list-item {
    display: grid;
    gap: .7rem;
  }
  .list-item > div:last-child {
    width: 100%;
    justify-content: flex-start;
  }
  .list-item .btn {
    flex: 1 1 92px;
  }
  .table-wrap {
    margin-inline: -.1rem;
    max-width: calc(100vw - 1.2rem);
  }
  table { min-width: 720px; }
  th, td { padding: .58rem .62rem; }
  td.actions {
    white-space: normal;
    min-width: 150px;
  }
  td.actions .btn {
    margin: .12rem;
  }
  .product-thumb {
    width: 48px;
    height: 48px;
  }
  .modal {
    width: calc(100vw - .75rem);
    max-height: calc(100dvh - .75rem);
  }
  .modal-card {
    max-height: calc(100dvh - .75rem);
    overflow: auto;
    overscroll-behavior: contain;
    padding: .9rem;
  }
  .modal-head {
    align-items: flex-start;
    gap: .7rem;
  }
  .modal-actions {
    align-items: stretch;
  }
  .modal-actions .spacer { display: none; }
  .modal-actions .btn { flex: 1 1 120px; }
  #scannerVideo {
    max-height: 42dvh;
    min-height: 220px;
  }
  .scanner-fallback { grid-template-columns: 1fr; }
  .scanner-fallback .btn { width: 100%; }
  .toolbar { align-items: stretch; }
  .toolbar-left, .toolbar-right { width: 100%; }
  .toolbar input, .toolbar select, .toolbar button, .toolbar .btn { flex: 1 1 150px; }
  .report-controls { grid-template-columns: 1fr; }
  .print-report-header { display: grid; }
  .print-report-logo { width: 132px; }
  .report-meta { grid-template-columns: 1fr; }
  .report-box { padding: .8rem; }
  .report-total { text-align: left; font-size: 1.1rem; }
  .label-sheet {
    min-height: 220px;
    padding: .75rem;
  }
  .labels-grid {
    grid-template-columns: 1fr;
  }
  .label-preview {
    min-height: 112px;
  }
  .toast-host {
    left: .6rem;
    right: .6rem;
    bottom: calc(.6rem + env(safe-area-inset-bottom));
  }
  .toast {
    max-width: none;
    width: 100%;
  }
  .empty-state { padding: 1.1rem; }
}

@media (max-width: 430px) {
  .app-header h1 { font-size: 1.38rem; }
  .brand-logo {
    width: 104px;
    height: 44px;
  }
  .header-actions > * { flex-basis: 100%; }
  .tabs {
    gap: .35rem;
    padding-block: .45rem;
  }
  .tab {
    min-height: 38px;
    padding: .5rem .62rem;
    font-size: .86rem;
  }
  .card { padding: .75rem; }
  .status-row { grid-template-columns: 1fr; }
  .toolbar-actions .btn,
  .product-photo-actions .btn,
  .modal-actions .btn {
    flex-basis: 100%;
  }
  .location-generator {
    grid-template-columns: 1fr;
  }
  .location-generator .btn { grid-column: auto; }
  #scannerVideo { min-height: 190px; }
  table { min-width: 680px; }
}

@media print {
  body * { visibility: hidden !important; }
  #printArea, #printArea * { visibility: visible !important; }
  #printArea {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    border: 0;
    box-shadow: none;
    padding: 0;
  }
  .label-sheet { border: 0; padding: 0; }
  .labels-grid { grid-template-columns: repeat(3, 1fr); gap: 8mm; }
  .label-preview { min-height: 38mm; }
  .no-print { display: none !important; }
  .print-report {
    color: #111;
    font-size: 10pt;
  }
  .print-report .table-wrap {
    border-radius: 0;
    overflow: visible;
  }
  .print-report table {
    min-width: 0;
    font-size: 9pt;
  }
  .print-report th,
  .print-report td {
    padding: 5pt 6pt;
  }
  .print-report-header {
    page-break-after: avoid;
  }
  .report-meta {
    grid-template-columns: repeat(4, 1fr);
  }
}
