* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #f5f6f7;
  color: #111;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px;
  background: #fff;
  border-bottom: 1px solid #dedede;
}

nav {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

nav a {
  padding: 7px 11px;
  border-radius: 999px;
  color: #168a53;
  text-decoration: none;
  font-weight: 600;
  font-size: 14px;
  line-height: 1;
  background: #f1f7f4;
  transition: background .15s, color .15s;
}

nav a:hover {
  background: #d8ede2;
}

nav a.on,
nav a.active {
  background: #168a53;
  color: #fff;
}

h1 {
  margin: 0;
  font-size: 24px;
}

button {
  min-height: 40px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px;
  background: #168a53;
  color: #fff;
  font: inherit;
  font-weight: 700;
}

main {
  padding: 20px 24px;
  overflow-x: auto;
}

.add-product {
  margin-bottom: 20px;
  padding: 18px 20px;
  background: #fff;
  border: 1px solid #dedede;
  border-radius: 10px;
}

.add-product h2 {
  margin: 0 0 14px;
  font-size: 18px;
}

#add-form {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: flex-end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 180px;
}

.field label {
  font-size: 13px;
  color: #555;
  font-weight: 600;
}

.field input,
.field select {
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid #cfcfcf;
  border-radius: 8px;
  font: inherit;
  background: #fff;
}

.field input:focus,
.field select:focus {
  outline: 2px solid #168a53;
  outline-offset: 0;
  border-color: #168a53;
}

.field-code {
  flex: 1 1 280px;
}

.code-row {
  display: flex;
  gap: 8px;
}

.code-row input {
  flex: 1 1 auto;
  min-width: 0;
}

.scan-btn {
  flex: 0 0 auto;
  white-space: nowrap;
  background: #0d6efd;
}

.scan-btn.ghost {
  background: #555;
}

.gen-code-btn {
  display: block;
  width: 100%;
  margin-top: 8px;
  background: #7048e8;
}

.scanner-box {
  flex: 1 1 100%;
}

.scanner-wrap {
  position: relative;
  width: 100%;
  max-width: 480px;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
}

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

.scan-frame {
  position: absolute;
  top: var(--frame-top, 50%);
  left: 50%;
  width: var(--frame-w, 80%);
  height: var(--frame-h, 22%);
  transform: translate(-50%, -50%);
  border: 3px solid rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45);
}

.frame-controls {
  display: grid;
  grid-template-columns: repeat(3, auto);
  gap: 6px;
  margin-top: 10px;
}

.frame-controls button {
  min-height: 36px;
  padding: 0 10px;
  background: #444;
  font-size: 14px;
}

.photo-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.photo-preview {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid #cfcfcf;
  background: #eee;
}

.row-actions {
  display: flex;
  gap: 6px;
}

.icon-btn {
  min-height: 36px;
  min-width: 40px;
  padding: 0 8px;
  background: #eef0f2;
  border: 1px solid #d4d7da;
  border-radius: 8px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.icon-btn:hover {
  background: #e2e6ea;
}

.search-results {
  margin-top: 6px;
  border: 1px solid #d4d7da;
  border-radius: 8px;
  background: #fff;
  max-height: 240px;
  overflow-y: auto;
}

.search-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: #fff;
  color: #111;
  border: 0;
  border-bottom: 1px solid #eee;
  border-radius: 0;
  padding: 10px 12px;
  text-align: left;
  font-weight: 500;
  min-height: 44px;
}

.search-item:hover {
  background: #eef7f2;
}

.search-name {
  font-weight: 700;
}

.search-empty {
  padding: 12px;
  color: #666;
  font-size: 14px;
}

.selected-product {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 8px 12px;
  border: 1px dashed #cfcfcf;
  border-radius: 8px;
  color: #777;
}

.selected-product.has-product {
  border-style: solid;
  border-color: #168a53;
  color: #111;
}

.selected-info {
  display: flex;
  flex-direction: column;
}

.mini-photo {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 6px;
  background: #eee;
}

.mini-photo.empty {
  display: grid;
  place-items: center;
  font-size: 11px;
  color: #888;
}

.grand-total {
  margin-top: 14px;
  font-size: 18px;
  font-weight: 700;
  text-align: right;
}

#cart-section {
  margin-top: 18px;
  border-top: 1px solid #e3e3e3;
  padding-top: 14px;
}

#cart-section h3 {
  margin: 0 0 10px;
  font-size: 16px;
}

.cart-table {
  width: 100%;
  min-width: 0;
  border-collapse: collapse;
  background: #fafafa;
  border: 1px solid #e3e3e3;
}

.cart-table th,
.cart-table td {
  padding: 8px 10px;
  border-bottom: 1px solid #ececec;
  text-align: left;
}

.cart-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
  flex-wrap: wrap;
}

#supplier-select {
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid #cfcfcf;
  border-radius: 8px;
  font: inherit;
  background: #fff;
}

/* ---- label printing ---- */

.label-panel {
  margin-bottom: 20px;
  padding: 18px 20px;
  background: #fff;
  border: 1px solid #dedede;
  border-radius: 10px;
}

.label-panel h2 {
  margin: 0 0 14px;
  font-size: 18px;
}

.label-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: flex-end;
}

.label-controls .field {
  flex: 1 1 180px;
}

.label-controls input[type="number"],
.label-controls input:not([type]) {
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid #cfcfcf;
  border-radius: 8px;
  font: inherit;
}

.chk {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  white-space: nowrap;
}

.label-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.label-preview-wrap {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.label-preview {
  display: inline-block;
}

/* Лента 30×20 мм (горизонтальная: 30 ширина × 20 высота). Контент горизонтальный, без поворота. */
.label {
  box-sizing: border-box;
  position: relative;
  width: 30mm;
  height: 20mm;
  border: 1px solid #bbb;
  background: #fff;
  overflow: hidden;
}

.label-inner {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.3mm;
  padding: 1mm;
  text-align: center;
}

.label-company {
  font-size: 6pt;
  font-weight: 700;
  line-height: 1;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.label-name {
  font-size: 6pt;
  font-weight: 600;
  line-height: 1.05;
  max-height: 2.1em;
  overflow: hidden;
}

/* Контейнер штрих-кода: занимает середину и центрирует код */
.label-barcode-box {
  flex: 1 1 auto;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* Штрих-код горизонтальный внутри блока 30×20 (после поворота блока идёт вдоль 30мм). */
.label-barcode {
  width: 100%;
  height: 10mm;
  display: block;
}

.label-price {
  font-size: 8pt;
  font-weight: 800;
  line-height: 1;
}

#print-area {
  display: none;
}

@page {
  size: 30mm 20mm;
  margin: 0;
}

@media print {
  /* Убираем ВСЁ из раскладки, кроме области печати — иначе длинная страница
     товаров растягивается на тысячи страниц. #print-area — прямой потомок body. */
  html,
  body {
    width: 30mm;
    margin: 0 !important;
    padding: 0 !important;
    background: #fff;
  }

  body > *:not(#print-area) {
    display: none !important;
  }

  #print-area {
    display: block;
  }

  .label {
    width: 30mm;
    height: 20mm;
    border: none;
    margin: 0;
    page-break-after: always;
    break-after: page;
  }

  .label:last-child {
    page-break-after: auto;
    break-after: auto;
  }
}

.scanner-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 10px;
  flex-wrap: wrap;
}

#scan-status {
  font-size: 14px;
  color: #444;
}

.actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1 1 100%;
  flex-wrap: wrap;
}

.form-message {
  font-size: 14px;
  font-weight: 600;
}

.form-message[data-kind="success"] {
  color: #168a53;
}

.form-message[data-kind="error"] {
  color: #c0392b;
}

button:disabled {
  opacity: 0.6;
}

.product-search-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 12px 0;
  flex-wrap: wrap;
}

#product-search {
  flex: 1 1 280px;
  padding: 10px 14px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 8px;
  box-sizing: border-box;
}

/* ---- Отчёт Kaspi ---- */
.kaspi-controls { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.report-section { margin-top: 18px; }
.report-section h2 { font-size: 16px; margin: 8px 0; }
.group { border: 1px solid #ddd; border-radius: 8px; margin: 6px 0; overflow: hidden; }
.group-head { width: 100%; text-align: left; background: #f7f7f7; border: none; padding: 10px 12px; cursor: pointer; display: flex; flex-wrap: wrap; align-items: center; gap: 10px; font-size: 15px; }
.group-head .toggle { font-weight: 700; width: 14px; }
.group-head .gname { flex: 1; }
.group-head .gcount { background: #168a53; color: #fff; border-radius: 12px; padding: 1px 10px; font-size: 13px; }
.group-body { padding: 8px 12px; }
table.orders { width: 100%; border-collapse: collapse; font-size: 13px; }
table.orders th, table.orders td { border-bottom: 1px solid #eee; padding: 6px 8px; text-align: left; vertical-align: top; }
table.orders td.num { text-align: right; white-space: nowrap; }
.group-head .gsum, .order-head .osum { color: #168a53; font-weight: 700; white-space: nowrap; }
.order-row { border-top: 1px solid #eee; }
.order-head { width: 100%; text-align: left; background: #fff; border: none; padding: 8px 6px; cursor: pointer; display: flex; align-items: center; gap: 8px; font-size: 13px; flex-wrap: wrap; }
.order-head .ocode { font-weight: 600; }
.order-head .oclient { flex: 1; color: #444; }
.order-body { padding: 4px 10px 10px; }
table.orders td.num.pos { color: #168a53; font-weight: 600; }
table.orders td.num.neg { color: #c0392b; font-weight: 600; }
table.orders td.costcell { cursor: pointer; color: #8e44ad; text-decoration: underline dotted; white-space: nowrap; }
.kaspi-label { background: #0d6efd; color: #fff; border-radius: 6px; padding: 1px 9px; font-weight: 700; font-size: 13px; white-space: nowrap; }
.group-head .gname { flex: 1; color: #888; font-size: 12px; }
/* группа: продажа (синий) · себест. (фиолетовый) · маржа/% (зелёный/красный) */
.group-head .gsum { color: #0d6efd; font-weight: 700; white-space: nowrap; }
.group-head .gcost { color: #8e44ad; font-weight: 700; white-space: nowrap; }
.group-head .gmargin.pos, .group-head .gpct.pos { color: #168a53; font-weight: 700; white-space: nowrap; }
.group-head .gmargin.neg, .group-head .gpct.neg { color: #c0392b; font-weight: 700; white-space: nowrap; }
/* заказ: те же цвета */
.order-head .osum { color: #0d6efd; font-weight: 600; white-space: nowrap; }
.order-head .ocost { color: #8e44ad; font-weight: 600; white-space: nowrap; }
.order-head .omargin.pos, .order-head .opct.pos { color: #168a53; font-weight: 600; white-space: nowrap; }
.order-head .omargin.neg, .order-head .opct.neg { color: #c0392b; font-weight: 600; white-space: nowrap; }
/* доставка (расход) — оранжевый, продавцу (нетто) — бирюзовый */
.group-head .gdeliv, .order-head .odeliv { color: #d35400; font-weight: 700; white-space: nowrap; }
.group-head .gcomm, .order-head .ocomm { color: #b5179e; font-weight: 700; white-space: nowrap; }
.group-head .gtax, .order-head .otax { color: #7048e8; font-weight: 700; white-space: nowrap; }
.group-head .gmkt, .order-head .omkt { color: #e8590c; font-weight: 700; white-space: nowrap; }
.group-head .gres, .order-head .ores { color: #1971c2; font-weight: 700; white-space: nowrap; }
.group-head .gnetpct.pos, .order-head .onetpct.pos { color: #0a7d72; font-weight: 800; white-space: nowrap; }
.group-head .gnetpct.neg, .order-head .onetpct.neg { color: #c0392b; font-weight: 800; white-space: nowrap; }
.group-head .gseller.pos, .order-head .oseller.pos { color: #0a7d72; font-weight: 800; white-space: nowrap; }
.group-head .gseller.neg, .order-head .oseller.neg { color: #c0392b; font-weight: 800; white-space: nowrap; }

table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
  background: #fff;
  border: 1px solid #dedede;
}

th,
td {
  padding: 12px;
  border-bottom: 1px solid #e7e7e7;
  text-align: left;
  vertical-align: top;
}

th {
  background: #fafafa;
  font-size: 13px;
  color: #555;
}

.photo {
  width: 92px;
  height: 92px;
  object-fit: cover;
  border-radius: 8px;
  background: #eee;
}

.photo-empty {
  display: grid;
  place-items: center;
  width: 92px;
  height: 92px;
  border-radius: 8px;
  background: #eee;
  color: #777;
  font-size: 13px;
}

.name {
  font-weight: 700;
}

.muted {
  color: #666;
  font-size: 13px;
}

#empty {
  padding: 24px;
  background: #fff;
  border: 1px solid #dedede;
}

@media (max-width: 640px) {
  header {
    flex-wrap: wrap;
    padding: 12px 14px;
  }

  nav {
    width: 100%;
    gap: 5px;
    margin-top: 2px;
  }

  nav a {
    padding: 6px 10px;
    font-size: 12.5px;
  }

  main {
    padding: 14px 16px;
  }

  h1 {
    font-size: 20px;
  }

  #add-form {
    gap: 12px;
  }

  .field,
  .field-code,
  .actions {
    flex: 1 1 100%;
  }

  #add-submit {
    width: 100%;
  }

  table,
  thead,
  tbody,
  th,
  td,
  tr {
    display: block;
  }

  table {
    min-width: 0;
    border: 0;
    background: transparent;
  }

  thead {
    display: none;
  }

  tbody tr {
    margin-bottom: 12px;
    background: #fff;
    border: 1px solid #dedede;
    border-radius: 10px;
    padding: 8px 12px;
  }

  td {
    border-bottom: 0;
    padding: 6px 0;
  }
}
