/* Лёгкий мобильно-ориентированный стиль. Без внешних зависимостей. */
:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1f2933;
  --muted: #687078;
  --primary: #2563eb;
  --primary-press: #1d4ed8;
  --border: #d8dce0;
  --danger-bg: #fdecea;
  --danger-text: #b3261e;
  --warn-bg: #fff3cd;
  --warn-text: #7a5c00;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 17px;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
}

.brand {
  font-weight: 700;
  text-decoration: none;
  color: var(--text);
}

.logout {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0;
}

.user { color: var(--muted); font-size: 15px; }

.container {
  max-width: 520px;
  margin: 0 auto;
  padding: 16px;
  /* место под фиксированную нижнюю навигацию и плавающую кнопку «+» */
  padding-bottom: calc(96px + env(safe-area-inset-bottom, 0px));
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.login-card { margin-top: 8vh; }

h1 { font-size: 22px; margin: 0 0 16px; }

label {
  display: block;
  margin: 14px 0 6px;
  font-size: 15px;
  color: var(--muted);
}

input {
  width: 100%;
  padding: 14px;
  font-size: 17px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
}

input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.btn-primary {
  width: 100%;
  margin-top: 20px;
  padding: 15px;
  font-size: 17px;
  font-weight: 600;
  color: #fff;
  background: var(--primary);
  border: none;
  border-radius: 10px;
  cursor: pointer;
}

.btn-primary:active { background: var(--primary-press); }

.btn-link {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 15px;
  cursor: pointer;
  padding: 6px 4px;
}

.alert {
  margin: 8px 0 4px;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--danger-bg);
  color: var(--danger-text);
  font-size: 15px;
}

.muted { color: var(--muted); }

/* --- Нижняя навигация --- */
.bottomnav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.bottomnav .nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 56px;
  padding: 6px 2px;
  text-decoration: none;
  color: var(--muted);
  font-size: 12px;
}
.bottomnav .nav-item.active { color: var(--primary); }
.bottomnav .nav-ico { font-size: 20px; line-height: 1; }
.bottomnav .nav-lbl { font-size: 12px; }

/* Центральная крупная кнопка «Движение» (operator+).
   Приподнята ВЫШЕ нижней панели (~56px), чтобы не перекрывать её пункты. */
.fab {
  position: fixed;
  z-index: 21;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(64px + env(safe-area-inset-bottom, 0px));
  width: 60px; height: 60px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 32px;
  line-height: 60px;
  text-align: center;
  text-decoration: none;
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.4);
}
.fab:active { background: var(--primary-press); }

/* --- Список номенклатуры --- */
.list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.btn-sm { width: auto; margin: 0; padding: 9px 14px; font-size: 15px; }

.search { margin: 14px 0 8px; }
.search input { width: 100%; }

.items { list-style: none; margin: 8px 0 0; padding: 0; }
.item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 4px;
  border-top: 1px solid var(--border);
}
.item-name { font-weight: 600; }
.item-sub { color: var(--muted); font-size: 14px; margin-top: 2px; }
.item-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.item-actions form { margin: 0; }
.btn-link.danger { color: var(--danger-text); }
.empty { padding: 20px 0; text-align: center; }
.cancel { display: inline-block; margin-left: 10px; }

[x-cloak] { display: none !important; }

select {
  width: 100%;
  padding: 13px;
  font-size: 17px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
}

/* --- Остатки --- */
.qty { font-size: 18px; font-weight: 700; white-space: nowrap; }
.qty .unit { font-size: 14px; font-weight: 400; color: var(--muted); }
.qty-low, .qty-crit { color: var(--danger-text); }
.qty-warn { color: var(--warn-text); }

/* --- Новое движение / сегментированные переключатели (type-buttons, show-фильтр) --- */
.type-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 6px;
}
.type-buttons.cols-3 { grid-template-columns: repeat(3, 1fr); }
.type-buttons button,
.type-buttons a {
  padding: 16px 8px;
  font-size: 16px;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  display: block;
}
.type-buttons button.active,
.type-buttons a.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.ac-wrap { position: relative; }
.autocomplete {
  position: absolute;
  top: 100%;            /* явная привязка к низу .ac-wrap: не зависит от того,
                            стал ли .ac-wrap flex-контейнером в десктопной раскладке */
  z-index: 10;
  left: 0; right: 0;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-top: 4px;
  max-height: 240px;
  overflow-y: auto;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
}
.ac-item { padding: 12px 14px; cursor: pointer; border-bottom: 1px solid var(--border); }
.ac-item:last-child { border-bottom: none; }
.ac-item small { color: var(--muted); }
.selected { margin: 10px 0; padding: 10px 12px; background: var(--bg); border-radius: 10px; }
.available { margin: 10px 0 0; }
.ok-msg {
  margin: 12px 0 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: #e7f6ec;
  color: #1b7a3d;
  font-size: 15px;
}

/* Карточка остатка как тап-цель (ведёт в историю товара) */
.item-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 4px 2px;
  text-decoration: none;
  color: inherit;
}
.item-link:active { background: var(--bg); }

/* --- Журнал --- */
.chip {
  margin: 10px 0;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: 10px;
  font-size: 15px;
}

.filters { margin: 10px 0 4px; }
.filters > summary {
  cursor: pointer;
  padding: 12px 4px;
  font-weight: 600;
  color: var(--primary);
  list-style: none;
}
.filters > summary::after { content: " ▾"; }
.filters[open] > summary::after { content: " ▴"; }
.filter-form { padding: 6px 2px 12px; }
.filter-dates { display: flex; gap: 12px; }
.filter-dates > div { flex: 1; }

.journal { margin-top: 6px; }
.jitem { padding: 14px 4px; border-top: 1px solid var(--border); }
.jrow { display: flex; align-items: center; gap: 8px; }
.jqty { font-weight: 700; font-size: 17px; }
.jname { margin-top: 4px; font-weight: 600; }
.jmeta { font-size: 13px; margin-top: 3px; }
.jactions { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 14px; }
.jactions .btn-link { padding: 8px 0; }

/* Цветовые метки типов */
.tlabel {
  font-size: 12px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  color: #fff;
  white-space: nowrap;
}
.type-in { background: #1b9e4b; }
.type-out { background: #d33a2c; }
.type-adjust { background: #c98a00; }
.type-init { background: #5b6470; }
.type-transfer { background: #6d4bd6; }

/* Помеченные на удаление: приглушены + зачёркнуты */
.is-marked { opacity: 0.55; }
.is-marked .jname, .is-marked .jqty { text-decoration: line-through; }
.badge {
  margin-left: auto;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--danger-bg);
  color: var(--danger-text);
}

.badge-commercial {
  background: #fff3cd;
  color: #7a5c00;
  margin-left: 0;
}
.badge-production {
  background: #d1fae5;
  color: #065f46;
  margin-left: 0;
}

/* Вторичная кнопка */
.btn-outline {
  display: inline-block;
  padding: 9px 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--primary);
  background: #fff;
  border: 1px solid var(--primary);
  border-radius: 10px;
  text-decoration: none;
  cursor: pointer;
}
.head-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* --- Импорт --- */
.import-buttons { display: flex; gap: 10px; align-items: center; margin-top: 16px; }
.import-buttons .btn-primary { width: auto; margin-top: 0; }
.preview { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 14px; }
.preview h2 { font-size: 18px; margin: 0 0 10px; }
.summary { list-style: none; margin: 0 0 12px; padding: 0; }
.summary li { padding: 4px 0; }
.autocreated {
  margin: 10px 0;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: 10px;
  font-size: 14px;
}
.prows { margin: 12px 0; max-height: 50vh; overflow-y: auto; }
.prow { padding: 10px 4px; border-top: 1px solid var(--border); font-size: 15px; }
.prow-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.prow-action { font-weight: 600; }
.prow-body { margin-top: 2px; }
.prow-qty { font-weight: 700; margin-left: 6px; }
.prow-errors { margin-top: 4px; color: var(--danger-text); font-size: 13px; }
.prow.row-error { background: var(--danger-bg); border-radius: 8px; }
.checkbox { display: flex; align-items: center; gap: 10px; margin: 14px 0; font-size: 15px; }
.checkbox input { width: auto; }

/* --- Контроль минимума (карточка номенклатуры) --- */
.form-section {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.form-section h2 { font-size: 16px; margin: 0 0 8px; }

/* --- Администрирование --- */
.admin-links { list-style: none; padding: 0; margin: 10px 0 0; }
.admin-links li { border-top: 1px solid var(--border); }
.admin-links a { display: block; padding: 16px 4px; text-decoration: none; color: var(--primary); font-weight: 600; }
.user-main { width: 100%; }
.user-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; }
.user-controls select, .user-controls input { width: auto; flex: 1 1 auto; min-width: 120px; }
.user-controls .checkbox { margin: 0; flex: 0 0 auto; }

/* Узкий вьюпорт (~360–380px): не сжимаем тач-цели */
@media (max-width: 380px) {
  body { font-size: 16px; }
  .container { padding-left: 12px; padding-right: 12px; }
  .type-buttons { gap: 8px; }
  .type-buttons button { padding: 14px 6px; font-size: 15px; }
}

/* =====================================================================
   Адаптивная раскладка. По умолчанию (мобильная) действуют стили выше.
   ===================================================================== */

/* Десктопная навигация по умолчанию скрыта (на мобильном — нижняя панель). */
.topnav { display: none; }
/* Десктоп-представления (таблицы) скрыты на мобильном. */
.only-desktop { display: none; }

/* Компактная кнопка действия в строке таблицы (журнал). */
.btn-mini {
  background: #fff;
  border: 1px solid var(--border);
  color: var(--primary);
  font-size: 13px;
  padding: 5px 9px;
  border-radius: 7px;
  cursor: pointer;
  margin: 0 2px;
}
.btn-mini:hover { background: var(--bg); }
.btn-mini.danger { color: var(--danger-text); border-color: #e7b3ae; }

/* ---------------------------------------------------------------------
   Десктоп: ширина ≥ 768px
   --------------------------------------------------------------------- */
@media (min-width: 768px) {
  /* Контент ограничен по ширине и центрирован; нижняя панель не нужна. */
  .container { max-width: 1200px; padding: 24px 24px 32px; }

  /* Навигация: верхняя панель вместо нижней. */
  .bottomnav, .fab { display: none; }
  .topnav {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    flex: 1;
    margin: 0 16px;
  }
  .topnav a {
    padding: 8px 12px;
    border-radius: 8px;
    text-decoration: none;
    color: var(--muted);
    font-size: 15px;
    font-weight: 600;
  }
  .topnav a:hover { background: var(--bg); color: var(--text); }
  .topnav a.active { color: var(--primary); background: rgba(37, 99, 235, 0.10); }

  /* Переключение раскладок «карточки ↔ таблица». */
  .only-mobile { display: none !important; }
  .only-desktop { display: block; }

  /* Кнопки нормального размера (не во всю ширину). */
  .btn-primary { width: auto; }

  /* Узкие центрированные формы (вход, движение, номенклатура). */
  .login-card { max-width: 380px; margin: 8vh auto 0; }
  .login-card .btn-primary { width: 100%; }
  .form-card { max-width: 560px; margin-left: auto; margin-right: auto; }
  .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
  .form-actions { display: flex; align-items: center; gap: 14px; margin-top: 8px; }
  .form-actions .cancel { margin-left: 0; }

  /* Таблицы данных. */
  .data-table { width: 100%; border-collapse: collapse; }
  .data-table th, .data-table td {
    padding: 9px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    font-size: 15px;
    vertical-align: middle;
  }
  .data-table thead th {
    color: var(--muted);
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
  }
  .data-table thead th[data-sort]::after {
    content: "\2195"; opacity: .35; margin-left: 6px; font-size: 11px;
  }
  .data-table thead th.sort-asc::after { content: "\2191"; opacity: 1; }
  .data-table thead th.sort-desc::after { content: "\2193"; opacity: 1; }
  .data-table tbody tr:hover { background: #f1f5ff; }
  .data-table .num { text-align: right; white-space: nowrap; }
  .data-table .qty-low, .data-table .qty-crit { color: var(--danger-text); }
  .data-table .qty-warn { color: var(--warn-text); }
  .row-link { cursor: pointer; }

  /* Журнал: фильтры всегда раскрыты горизонтальной панелью.
     Прячем «Фильтры» только когда панель раскрыта (если JS не отработал —
     summary остаётся, чтобы фильтры были доступны). */
  details.filters[open] > summary { display: none; }
  details.filters > .filter-form {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 12px;
    padding: 0 0 14px;
  }
  .filter-form > div,
  .filter-form .filter-dates { display: flex; }
  .filter-form label { margin: 0 0 4px; }
  .filter-form select,
  .filter-form input { width: auto; }
  .filter-form .btn-primary { margin-top: 0; }
  .filter-form .cancel { align-self: center; }
  .filter-form .autocomplete { min-width: 280px; }
  .actions-cell { white-space: nowrap; }

  /* Импорт: контролы в строку, широкий предпросмотр. */
  .import-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    align-items: flex-end;
  }
  .import-controls > div { display: flex; flex-direction: column; margin: 0; }
  .import-controls label { margin: 0 0 4px; }
  .import-controls select,
  .import-controls input[type="file"] { width: auto; }
  .preview-table td.err { color: var(--danger-text); }
  .preview-table .row-error { background: var(--danger-bg); }

  /* Пакетный ввод */
  .batch-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
  .batch-type-buttons { max-width: 440px; margin-bottom: 4px; }
  .batch-wh-row { display: flex; gap: 20px; flex-wrap: wrap; margin: 12px 0 16px; }
  .batch-wh-row > div { display: flex; flex-direction: column; }
  .batch-wh-row label { margin: 0 0 4px; }
  .batch-wh-row select { width: auto; min-width: 160px; }
  .batch-table { margin: 4px 0 16px; }
  .batch-table td { vertical-align: middle; padding: 8px 12px; }
  .batch-search-input { min-width: 200px; padding: 8px 10px; font-size: 15px; }
  .batch-qty-input { width: 130px; padding: 8px 10px; font-size: 15px; }
  .batch-product-sel { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
  .batch-footer-row { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
  .batch-submit-row { margin-top: 16px; }
}

/* Баннер недоставленных уведомлений */
.alert-banner {
  background: #fff8e1;
  border-bottom: 1px solid #ffe082;
  color: #5f4f00;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 15px;
}
.alert-banner .btn-link { color: var(--primary); text-decoration: underline; }
