:root {
  --bg: #0f1115;
  --surface: #1a1d23;
  --surface-2: #232730;
  --text: #e6e8eb;
  --muted: #9aa3ad;
  --primary: #1f6feb;
  --primary-dark: #1858c4;
  --danger: #e5534b;
  --border: #2b3038;
  --radius: 12px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.app-header {
  flex: 0 0 auto;
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  text-align: center;
}
.app-header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.app-content {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
}

.tab-panel { animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.toolbar { margin-bottom: 12px; }
#search {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}

/* Сетка товаров */
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}
.product-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.product-card img {
  width: 100%;
  height: 120px;
  object-fit: cover;
  background: var(--surface-2);
}
.product-card .card-body {
  padding: 8px 10px;
  flex: 1;
}
.product-card .card-name {
  font-size: 14px;
  font-weight: 600;
  margin: 0 0 4px;
  word-break: break-word;
}
.product-card .card-barcode {
  font-size: 12px;
  color: var(--muted);
  word-break: break-all;
}
.product-card .card-del {
  border: none;
  background: transparent;
  color: var(--danger);
  font-size: 13px;
  padding: 8px;
  cursor: pointer;
  border-top: 1px solid var(--border);
}

.empty-hint {
  text-align: center;
  color: var(--muted);
  margin-top: 40px;
}

/* Сканер */
.scanner-block { margin-bottom: 16px; }
.scanner-viewport {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
}
.scanner-viewport video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.scan-overlay {
  position: absolute;
  inset: 0;
  border: 2px solid rgba(31,111,235,.7);
  pointer-events: none;
}
.scan-overlay::before {
  content: "";
  position: absolute;
  left: 10%;
  right: 10%;
  top: 50%;
  height: 2px;
  background: var(--primary);
  box-shadow: 0 0 12px var(--primary);
  transform: translateY(-50%);
}
.scan-line {
  position: absolute;
  left: 10%;
  right: 10%;
  height: 2px;
  background: rgba(31,111,235,.9);
  box-shadow: 0 0 10px var(--primary);
  animation: scanmove 2.4s ease-in-out infinite;
}
@keyframes scanmove {
  0%, 100% { top: 20%; }
  50% { top: 80%; }
}

.scanner-controls {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.hint {
  color: var(--muted);
  font-size: 13px;
  margin: 14px 0 8px;
}
.manual-row {
  display: flex;
  gap: 8px;
}
.manual-row input {
  flex: 1;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}

/* Форма товара */
.product-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-top: 16px;
}
.product-form h2 {
  margin: 0 0 14px;
  font-size: 16px;
}
.field {
  display: block;
  margin-bottom: 14px;
}
.field span {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}
.field input[type="text"] {
  width: 100%;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
}
.photo-preview {
  margin-top: 8px;
}
.photo-preview img {
  width: 100%;
  max-height: 200px;
  object-fit: contain;
  border-radius: var(--radius);
  background: var(--surface-2);
}
.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

/* Кнопки */
.btn {
  flex: 1;
  padding: 12px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
}
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:active { background: var(--primary-dark); }
.btn-danger:active { background: #c7423a; }

/* Нижняя навигация */
.bottom-nav {
  flex: 0 0 auto;
  display: flex;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.nav-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 8px 2px;
  font-size: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
}
.nav-btn .nav-icon { font-size: 18px; line-height: 1; }
.nav-btn .nav-icon svg {
  width: 22px;
  height: 22px;
  display: block;
}
.nav-btn.active { color: var(--primary); }

/* Модальное окно товара */
.form-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.product-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  width: 100%;
  max-width: 420px;
  max-height: 90vh;
  overflow-y: auto;
}
.product-form h2 {
  margin: 0 0 14px;
  font-size: 16px;
}
.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.btn-warning {
  background: #d97706;
  border-color: #d97706;
  color: #fff;
}
.btn-warning:active { background: #b45309; }
.field input[readonly] {
  opacity: .8;
  background: var(--surface);
}
.product-form.mode-view {
  border-color: var(--border);
}
.product-form.mode-edit {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(31, 111, 235, .35);
}
.product-form.mode-edit #form-name {
  background: #2a2418;
  border-color: #c9971f;
}
.product-card { cursor: pointer; }

/* Поля срока годности */
.expiry-block {
  border-top: 1px solid var(--border);
  margin-top: 14px;
  padding-top: 14px;
}
.field select,
.field input[type="date"],
.field input[type="number"] {
  width: 100%;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}
.expiry-duration {
  display: flex;
  gap: 10px;
}
.expiry-duration .field { flex: 1; }
.expiry-preview {
  margin: 10px 0 0;
  font-size: 13px;
  color: var(--muted);
}
.card-expiry {
  margin: 4px 0 0;
  font-size: 12px;
  font-weight: 600;
}

/* Статусы срока годности */
.status-ok { color: #3fb950; }
.status-soon { color: #d29922; }
.status-expired { color: var(--danger); }

/* Вкладка «Сроки годности» */
.expiry-list { display: flex; flex-direction: column; gap: 10px; }

/* Вкладка «Выгрузка» */
.backup-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 8px;
}

/* Блок лицензии */
.license {
  margin-top: 24px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}
.license p { margin: 0 0 8px; }
.license p:last-child { margin-bottom: 0; }
.license-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

/* Вкладка «История выгрузки» */
.history-list { display: flex; flex-direction: column; gap: 12px; }
.history-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.history-info { margin-bottom: 10px; min-width: 0; }
.history-name {
  margin: 0 0 4px;
  font-size: 14px;
  font-weight: 600;
  word-break: break-all;
}
.history-sub {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}
.history-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.history-actions .btn { flex: 1 1 auto; padding: 10px 12px; font-size: 14px; }
.expiry-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
}
.expiry-row.status-ok { border-left-color: #3fb950; }
.expiry-row.status-soon { border-left-color: #d29922; }
.expiry-row.status-expired { border-left-color: var(--danger); }
.expiry-info { min-width: 0; }
.expiry-name {
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 600;
}
.expiry-sub {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}
.expiry-badge {
  flex: 0 0 auto;
  font-size: 12px;
  font-weight: 600;
  text-align: right;
}
.expiry-row.status-ok .expiry-badge { color: #3fb950; }
.expiry-row.status-soon .expiry-badge { color: #d29922; }
.expiry-row.status-expired .expiry-badge { color: var(--danger); }

/* Строка названия с кнопкой-камерой */
.name-row { display: flex; gap: 8px; align-items: stretch; }
.name-row input { flex: 1; min-width: 0; }
.icon-btn {
  flex: 0 0 auto;
  width: 46px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; }

/* Учёт: подвкладки */
.accounting-subtabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  margin-bottom: 16px;
  padding-bottom: 4px;
}
.subtab-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 8px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  white-space: nowrap;
}
.subtab-btn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.subtab-panel { animation: fade .2s ease; }
.muted { color: var(--muted); }

/* Приёмка */
.receiving-area { margin-top: 16px; }
.receiving-step { margin-bottom: 16px; }
.receiving-area .scanner-block { margin-bottom: 12px; }

/* Коррекция */
#subtab-correction .btn { margin-right: 8px; margin-bottom: 8px; }

/* Товарный учёт */
.goods-table-wrap { overflow-x: auto; }
.goods-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.goods-table th, .goods-table td { padding: 10px 8px; border-bottom: 1px solid var(--border); text-align: left; }
.goods-table th { color: var(--muted); font-weight: 600; background: var(--surface); }
.goods-table td { color: var(--text); }
.goods-table tr:last-child td { border-bottom: none; }

/* Касса */
.check-items-list { display: flex; flex-direction: column; gap: 8px; }
.check-item-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.check-item-info { display: flex; flex-direction: column; gap: 2px; }
.check-total { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; font-size: 16px; font-weight: 600; }
.check-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }

/* Показатели */
.metrics-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.metric-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 4px; }
.metric-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
.metric-value { font-size: 20px; font-weight: 700; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 80px;
  transform: translateX(-50%);
  background: rgba(0,0,0,.85);
  color: #fff;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 100;
  max-width: 90%;
  text-align: center;
}
