/* Складской учёт — мобильный PWA-интерфейс.
   Дизайн: крупные элементы (телефон в руках на складе), высокий контраст,
   поддержка светлой и тёмной темы. */

:root {
  --bg: #f2f4f8;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --border: #dfe3ea;
  --text: #10151f;
  --text-dim: #5b6472;
  --primary: #1558d6;
  --primary-press: #1247b0;
  --primary-fg: #ffffff;
  --danger: #c8232c;
  --danger-bg: #fdeceb;
  --success: #12805c;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow: 0 1px 2px rgba(16, 21, 31, .06), 0 6px 20px rgba(16, 21, 31, .07);
  --tabbar-h: 64px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #171b22;
    --surface-2: #1e232c;
    --border: #2c333e;
    --text: #eef1f6;
    --text-dim: #9aa4b2;
    --primary: #4b8bff;
    --primary-press: #3a76e6;
    --primary-fg: #08101f;
    --danger: #ff7a75;
    --danger-bg: #35191b;
    --success: #4fd0a4;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
  /* Ничто не должно вылезать по горизонтали на узких экранах */
  overflow-x: hidden;
}

[hidden] { display: none !important; }

/* ===== Первый запуск ===== */
.onboarding {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Экранная клавиатура не должна прятать поле и кнопку */
  overflow-y: auto;
  padding: max(20px, env(safe-area-inset-top)) 16px calc(20px + env(safe-area-inset-bottom));
  background: var(--bg);
}

.onboarding__card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 28px 22px 24px;
  text-align: center;
}

.onboarding__logo { border-radius: 18px; margin-bottom: 14px; }
.onboarding__title { margin: 0 0 8px; font-size: 24px; letter-spacing: -.02em; }
.onboarding__text { margin: 0 0 20px; color: var(--text-dim); font-size: 15px; }

/* ===== Каркас приложения ===== */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar__title { font-size: 20px; font-weight: 700; letter-spacing: -.02em; }

.topbar__user {
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  color: var(--text-dim);
}

.content {
  flex: 1;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}

/* ===== Остатки ===== */
.stock { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }

.stock__item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 12px 12px 14px;
  box-shadow: var(--shadow);
}

.stock__name {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 17px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.stock__qty {
  flex: none;
  font-size: 24px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.02em;
}

.stock__item.is-negative { border-color: var(--danger); background: var(--danger-bg); }
.stock__item.is-negative .stock__qty,
.stock__item.is-negative .stock__name { color: var(--danger); }

.stock__edit {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--primary);
  cursor: pointer;
}
.stock__edit svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.stock__edit:active { background: var(--border); }
.stock__item.is-negative .stock__edit { color: var(--danger); border-color: var(--danger); background: transparent; }

.stock__skeleton {
  height: 74px;
  border-radius: var(--radius);
  background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 37%, var(--surface) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.2s ease-in-out infinite;
}

@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }

/* ===== Журнал операций ===== */
.log-filter { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.log-filter__sep { flex: none; color: var(--text-dim); }
.field--date { flex: 1 1 0; min-width: 0; height: 44px; padding: 0 10px; font-size: 15px; }

.log { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }

.log__item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.log__head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.log__badge {
  flex: none;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-dim);
}
.log__item--income .log__badge { color: var(--success); }
.log__item--expense .log__badge { color: var(--primary); }
.log__item--adjustment .log__badge { background: var(--danger-bg); color: var(--danger); }

.log__employee {
  flex: 1 1 auto;
  min-width: 0;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.log__details { margin: 0 0 4px; font-size: 14px; line-height: 1.4; color: var(--text); overflow-wrap: anywhere; }

.log__time { display: block; font-size: 12.5px; color: var(--text-dim); }

/* ===== Карточки схем / материалов ===== */
/* minmax(0, 1fr) обязателен: иначе grid-элемент раздувается до min-content
   длинного текста и страница едет по горизонтали на узких экранах. */
.cards { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }

.card {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

.card + .card { margin-top: 10px; }
.card__label { display: block; font-size: 14px; font-weight: 600; color: var(--text-dim); margin-bottom: 8px; }
.card__title {
  font-size: 16px;
  font-weight: 700;
  margin: 0 0 2px;
  letter-spacing: -.01em;
  overflow-wrap: anywhere;
}
.card__norms { margin: 0 0 10px; font-size: 12.5px; line-height: 1.35; color: var(--text-dim); overflow-wrap: anywhere; }
.card__sub { margin: 0 0 10px; font-size: 13px; color: var(--text-dim); }
.card__sub b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ===== Степпер (−  значение  +) ===== */
.stepper { display: flex; align-items: center; gap: 8px; min-width: 0; }

.stepper__btn {
  flex: none;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 26px;
  line-height: 1;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.stepper__btn:active { background: var(--border); }
.stepper__btn:disabled { opacity: .4; cursor: default; }

.stepper__input {
  flex: 1 1 0;
  width: 100%;
  min-width: 0;
  height: 48px;
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  -moz-appearance: textfield;
}
.stepper__input::-webkit-outer-spin-button,
.stepper__input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper__input.is-set { border-color: var(--primary); color: var(--primary); }

/* ===== Поля ===== */
.field {
  display: block;
  width: 100%;
  height: 52px;
  padding: 0 14px;
  font-size: 17px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  outline: none;
}
.field:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(21, 88, 214, .18); }
.field--pin, .field--num { text-align: center; font-size: 22px; font-weight: 700; letter-spacing: .12em; }
.field--num { letter-spacing: normal; font-variant-numeric: tabular-nums; }

/* ===== Кнопки ===== */
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  padding: 0 20px;
  font-size: 17px;
  font-weight: 600;
  font-family: inherit;
  border-radius: 14px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color .12s ease, opacity .12s ease;
}
.btn--primary { background: var(--primary); color: var(--primary-fg); }
.btn--primary:active { background: var(--primary-press); }
.btn--ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn--ghost:active { background: var(--surface-2); }
.btn--wide { width: 100%; margin-top: 14px; }
.btn--grow { flex: 1; }
.btn--sm { min-height: 40px; padding: 0 14px; font-size: 15px; }
.btn:disabled { opacity: .6; cursor: default; }

.btn.is-loading .btn__label { visibility: hidden; }
.btn.is-loading::after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2.5px solid currentColor;
  border-top-color: transparent;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

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

/* Кнопка сохранения и сообщение об ошибке всегда на виду — прилипают над вкладками */
.footer-bar {
  position: sticky;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  z-index: 10;
  margin: 14px -16px 0;
  padding: 12px 16px;
  background: var(--bg);
  box-shadow: 0 -10px 18px -10px rgba(16, 21, 31, .25);
}

.footer-bar .banner { margin-bottom: 10px; }
.footer-bar .actions { margin-top: 0; }

/* ===== Баннеры, подсказки ===== */
.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius);
  margin-bottom: 14px;
  font-size: 15px;
}
.banner--error { background: var(--danger-bg); border: 1px solid var(--danger); color: var(--danger); }
.banner__text { flex: 1; }
.banner .btn--ghost { background: transparent; border-color: currentColor; color: inherit; }

.hint { margin: 16px 2px 0; font-size: 13px; line-height: 1.5; color: var(--text-dim); }
.hint--top { margin: 0 2px 14px; }
.hint--tight { margin-top: 12px; }

.form-error { margin: 8px 2px 0; color: var(--danger); font-size: 14px; }

.props { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0; font-size: 15px; }
.props dt { color: var(--text-dim); }
.props dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

/* ===== Нижние вкладки ===== */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  height: var(--tabbar-h);
  border: none;
  background: none;
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  cursor: pointer;
}
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tab svg .cut { fill: var(--surface); stroke: currentColor; }
.tab.is-active { color: var(--primary); }
.tab.is-active svg .cut { fill: var(--surface); }

/* ===== Модалки ===== */
.modal { position: fixed; inset: 0; z-index: 60; display: flex; align-items: flex-end; justify-content: center; }
.modal__backdrop { position: absolute; inset: 0; background: rgba(8, 12, 20, .55); backdrop-filter: blur(2px); }

.modal__box {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: 88dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 22px 16px calc(22px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
  animation: sheet-up .18s ease-out;
}
@keyframes sheet-up { from { transform: translateY(14px); opacity: .6; } to { transform: none; opacity: 1; } }

@media (min-width: 560px) {
  .modal { align-items: center; }
  .modal__box { border-radius: var(--radius-lg); padding-bottom: 22px; }
}

.modal__title { margin: 0 0 4px; font-size: 20px; letter-spacing: -.02em; }
.modal__subtitle { margin: 0 0 18px; color: var(--text-dim); font-size: 15px; }
.modal__note { margin: 0 0 14px; font-size: 15px; color: var(--text-dim); }
.modal__note b { color: var(--text); font-variant-numeric: tabular-nums; }

.preview { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.preview li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 14px;
  background: var(--surface-2);
  border-radius: 12px;
  font-size: 16px;
  min-width: 0;
}
.preview li > span:first-child { min-width: 0; overflow-wrap: anywhere; }
.preview .delta { flex: none; font-weight: 700; font-variant-numeric: tabular-nums; }
.preview .delta--minus { color: var(--danger); }
.preview .delta--plus { color: var(--success); }

/* ===== Снекбар ===== */
.snackbar {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  transform: translate(-50%, 8px);
  z-index: 70;
  max-width: min(92vw, 480px);
  padding: 14px 18px;
  border-radius: 14px;
  background: var(--text);
  color: var(--bg);
  font-size: 15px;
  font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity .18s ease, transform .18s ease;
}
.snackbar.is-visible { opacity: 1; transform: translate(-50%, 0); }
.snackbar.is-error { background: var(--danger); color: #fff; }

/* ===== Узкие экраны (iPhone SE и подобные) ===== */
@media (max-width: 374px) {
  .content { padding-left: 12px; padding-right: 12px; }
  .topbar { padding-left: 12px; padding-right: 12px; }
  .topbar__title { font-size: 18px; }
  .stock__name { font-size: 16px; }
  .stock__qty { font-size: 21px; }
  .stock__edit { width: 40px; height: 40px; }
  .card { padding: 12px; }
  .card__title { font-size: 15px; }
  .stepper__btn { width: 44px; height: 44px; font-size: 24px; }
  .stepper__input { height: 44px; font-size: 18px; }
  .btn { min-height: 48px; padding: 0 14px; font-size: 16px; }
  .footer-bar { margin-left: -12px; margin-right: -12px; padding-left: 12px; padding-right: 12px; }
  .tab { font-size: 10px; }
}

/* ===== Ландшафтная ориентация: экономим вертикаль ===== */
@media (max-height: 480px) and (orientation: landscape) {
  .topbar { padding-top: calc(env(safe-area-inset-top) + 8px); padding-bottom: 8px; }
  .card { padding: 10px 12px; }
  .cards { gap: 8px; }
  .hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
