body {
  min-height: 100vh;
}

.qty-input {
  max-width: 120px;
  margin-left: auto;
}

.needed-input.is-manual {
  background-color: #fff8e1;
  border-color: #ffc107;
}

.coef-row {
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--bs-border-color);
  border-radius: 0.375rem;
  background: #fff;
}

.coef-label {
  margin: 0;
  line-height: 1.2;
}

.coef-input {
  width: 90px;
  flex-shrink: 0;
}

.coef-input.is-changed {
  background-color: #fff8e1;
  border-color: #ffc107;
}

/* Админка: редактор списка */
.editor-table th {
  font-size: 0.8rem;
  white-space: nowrap;
}

.editor-table td {
  padding: 0.3rem 0.35rem;
}

.editor-table .cell-name {
  min-width: 200px;
}

.editor-table .cell-unit {
  width: 70px;
}

.editor-table .cell-per {
  min-width: 150px;
}

.editor-table .cell-num {
  width: 90px;
}

.admin-brand {
  font-size: 1rem;
  white-space: normal;
}

.editor-heading {
  font-size: 1.15rem;
  overflow-wrap: anywhere;
}

@media (min-width: 768px) {
  .admin-brand {
    font-size: 1.25rem;
  }

  .editor-heading {
    font-size: 1.5rem;
  }
}

.cat-name {
  min-width: 200px;
  width: auto;
}

/* Админка: справка "Как считается «Нужно»" */
.calc-help {
  background: #f4f8ff;
  border: 1px solid #cfe0ff;
  border-left: 4px solid var(--bs-primary);
  border-radius: 0.5rem;
  padding: 0.6rem 0.9rem;
  font-size: 0.92rem;
}

.calc-help > summary {
  cursor: pointer;
  font-weight: 600;
  list-style-position: inside;
  padding: 0.15rem 0;
}

.calc-help-body {
  padding-top: 0.6rem;
}

.calc-formula {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem 0.5rem;
  margin-bottom: 0.4rem;
}

.calc-chip {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  background: #fff;
  border: 1px solid #b6cdf7;
  border-radius: 0.4rem;
  font-weight: 500;
  white-space: nowrap;
}

.calc-chip-main {
  background: var(--bs-primary);
  border-color: var(--bs-primary);
  color: #fff;
  font-weight: 600;
}

.calc-op {
  font-weight: 700;
  color: var(--bs-secondary-color);
}

.calc-note {
  color: var(--bs-secondary-color);
  margin-bottom: 0;
}

.calc-section-title {
  margin-bottom: 0.3rem;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--bs-secondary-color);
}

.calc-table {
  --bs-table-bg: transparent;
  margin-bottom: 0;
}

.calc-table th {
  width: 34%;
  font-weight: 600;
}

.calc-table td {
  vertical-align: middle;
}

.calc-why {
  color: var(--bs-secondary-color);
  font-size: 0.85rem;
}

.calc-example {
  padding: 0.5rem 0.75rem;
  background: #fff;
  border: 1px dashed #b6cdf7;
  border-radius: 0.4rem;
}

.calc-warn {
  padding: 0.5rem 0.75rem;
  background: #fff8e1;
  border: 1px solid #ffe69c;
  border-radius: 0.4rem;
}

@media (max-width: 767.98px) {
  /* таблица множителей -> карточки: название, множитель и пояснение друг под другом */
  .calc-table,
  .calc-table tbody,
  .calc-table tr,
  .calc-table th,
  .calc-table td {
    display: block;
    width: 100%;
  }

  .calc-table tr {
    padding: 0.5rem 0;
    border-bottom: 1px solid #d9e5fb;
  }

  .calc-table tr:last-child {
    border-bottom: 0;
  }

  .calc-table th,
  .calc-table td {
    padding: 0.1rem 0;
    border: 0;
  }
}

.cat-toggle {
  flex: 0 0 auto;
  width: 2.25rem;
  padding: 0.25rem 0;
  line-height: 1.5;
}

.cat-chevron {
  display: inline-block;
  font-size: 1.2em;
  transition: transform 0.15s;
}

.cat-card.is-collapsed .cat-chevron {
  transform: rotate(-90deg);
}

.cat-card.is-collapsed .cat-body {
  display: none;
}

.cat-card.is-collapsed .cat-header {
  border-bottom: 0;
}

.cat-count {
  color: var(--bs-secondary-color);
  font-size: 0.85rem;
  white-space: nowrap;
}

@media (min-width: 768px) {
  /* компьютер: строки позиций чередуются, чтобы взгляд не терялся */
  .editor-table tr.product-row:nth-child(even) > td {
    background-color: rgba(0, 0, 0, 0.035);
  }

  .editor-table tr.product-row:hover > td {
    background-color: rgba(13, 110, 253, 0.06);
  }
}

.saved-link-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.saved-link-row .form-control {
  flex: 1 1 260px;
  min-width: 0;
}

.list-actions {
  flex-wrap: nowrap;
}

.editor-bar {
  position: sticky;
  bottom: 0;
  z-index: 1020;
  margin: 0 -0.75rem;
  padding: 0.75rem 0;
  background: #fff;
  border-top: 1px solid var(--bs-border-color);
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
}

/* Кнопка "наверх" */
.back-to-top {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 1030;
  width: 48px;
  height: 48px;
  padding: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px);
  transition: opacity 0.2s, transform 0.2s, visibility 0.2s;
}

.back-to-top.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

/* Печатный макет: на экране скрыт, при печати показывается вместо интерфейса */
.print-area {
  display: none;
}

@media print {
  @page {
    size: A4 portrait;
    margin: 12mm;
  }

  html,
  body {
    background: #fff !important;
  }

  /* прячем весь интерфейс, окна и подложки модалок */
  body > *:not(.print-area) {
    display: none !important;
  }

  .print-area {
    display: block;
    color: #000;
    font-size: 10.5pt;
    line-height: 1.3;
  }

  .print-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 6mm;
    padding-bottom: 3mm;
    margin-bottom: 3mm;
    border-bottom: 2px solid #000;
  }

  .print-header h1 {
    margin: 0;
    font-size: 18pt;
    font-weight: 700;
  }

  .print-meta {
    display: flex;
    gap: 4mm;
    font-size: 11pt;
    white-space: nowrap;
  }

  .print-summary {
    margin: 0 0 4mm;
    font-size: 11pt;
  }

  .print-columns {
    column-count: 2;
    column-gap: 10mm;
  }

  .print-category {
    break-inside: avoid;
    margin-bottom: 5mm;
  }

  .print-category h2 {
    margin: 0 0 1.5mm;
    padding-bottom: 1mm;
    border-bottom: 1.2px solid #000;
    font-size: 10.5pt;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }

  .print-category ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .print-item {
    display: flex;
    align-items: flex-start;
    gap: 2.5mm;
    padding: 1.4mm 0;
    border-bottom: 0.5px dotted #888;
    break-inside: avoid;
  }

  .print-box {
    flex: 0 0 4.2mm;
    width: 4.2mm;
    height: 4.2mm;
    margin-top: 0.4mm;
    border: 1.2px solid #000;
    border-radius: 0.8mm;
    font-size: 9pt;
    line-height: 3.6mm;
    text-align: center;
  }

  .print-name {
    flex: 1;
    font-weight: 500;
  }

  .print-name small {
    display: block;
    color: #555;
    font-size: 8pt;
    font-weight: 400;
  }

  .print-qty {
    white-space: nowrap;
    font-size: 11pt;
    font-weight: 700;
    text-align: right;
  }

  .print-item.is-done {
    color: #777;
  }

  .print-item.is-done .print-qty {
    font-weight: 400;
  }
}

/* Телефон: строки таблицы превращаются в карточки */
@media (max-width: 767.98px) {
  .products-table thead {
    display: none;
  }

  .products-table,
  .products-table tbody,
  .products-table tr,
  .products-table td {
    display: block;
    width: 100%;
  }

  .products-table tr {
    padding: 0.6rem 0;
    border-bottom: 1px solid var(--bs-border-color);
  }

  .products-table tr:last-child {
    border-bottom: 0;
  }

  .products-table td {
    border: 0;
    padding: 0.2rem 0;
  }

  .products-table td.product-name {
    font-size: 1.05rem;
    padding-bottom: 0.4rem;
  }

  .products-table td[data-label] {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
  }

  .products-table td[data-label]::before {
    content: attr(data-label);
    color: var(--bs-secondary-color);
    font-size: 0.9rem;
  }

  .qty-input {
    max-width: 140px;
    font-size: 1rem;
  }
}

/* ------------------------------------------------------------ Админка: телефон */
@media (max-width: 767.98px) {
  /* таблица наборов -> карточки */
  .lists-table thead {
    display: none;
  }

  .lists-table,
  .lists-table tbody,
  .lists-table tr,
  .lists-table td {
    display: block;
    width: 100%;
  }

  .lists-table tr {
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--bs-border-color);
  }

  .lists-table tr:last-child {
    border-bottom: 0;
  }

  .lists-table td {
    border: 0;
    padding: 0.15rem 0;
  }

  .lists-table .l-actions {
    padding-top: 0.6rem;
    text-align: left !important;
  }

  .list-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    width: 100%;
  }

  .list-actions > .btn {
    margin-left: 0 !important;
    padding: 0.55rem 0.5rem;
    font-size: 0.95rem;
    border-radius: 0.375rem !important;
  }

  /* редактор: строка позиции -> карточка с сеткой полей */
  .editor-table,
  .editor-table tbody,
  .editor-table tr,
  .editor-table td {
    display: block;
    width: 100%;
  }

  .editor-table thead {
    display: none;
  }

  /* позиции - отдельные карточки на сером фоне, с номером и цветной полосой слева */
  .editor-table tbody {
    padding: 0.75rem 0.75rem 0.1rem;
    background: var(--bs-tertiary-bg);
    counter-reset: prod;
  }

  .editor-table tr.product-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.6rem 0.75rem;
    margin-bottom: 0.85rem;
    padding: 0.8rem 1rem 1rem;
    background: #fff;
    border: 1px solid var(--bs-border-color);
    border-left: 4px solid var(--bs-primary);
    border-radius: 0.6rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    counter-increment: prod;
  }

  .editor-table tr.product-row::before {
    content: "Позиция " counter(prod);
    grid-column: 1 / -1;
    order: -1;
    color: var(--bs-secondary-color);
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.03em;
    text-transform: uppercase;
  }

  .editor-table tr.product-row > td {
    box-shadow: none;
    background: transparent;
  }

  .editor-table .c-actions {
    padding-top: 0.25rem !important;
    border-top: 1px dashed var(--bs-border-color) !important;
    margin-top: 0.15rem;
  }

  .editor-table tr.product-row > td {
    padding: 0;
    border: 0;
    min-width: 0;
  }

  .editor-table td[data-label]::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 0.15rem;
    color: var(--bs-secondary-color);
    font-size: 0.8rem;
    line-height: 1.2;
  }

  .editor-table .c-name { grid-column: 1 / -1; order: 0; }
  .editor-table .c-per { grid-column: 1 / -1; order: 1; }
  .editor-table .c-unit { order: 2; }
  .editor-table .c-base { order: 3; }
  .editor-table .c-round { order: 4; }
  .editor-table .c-coef { order: 5; }
  .editor-table .c-needed { order: 6; }
  .editor-table .c-stock { order: 7; }
  .editor-table .c-actions { grid-column: 1 / -1; order: 8; text-align: left !important; }

  .editor-table .cell-name,
  .editor-table .cell-per,
  .editor-table .cell-unit,
  .editor-table .cell-num {
    width: 100%;
    min-width: 0;
  }

  /* 16px, чтобы iOS не увеличивал страницу при фокусе; крупнее зона нажатия */
  .editor-table .form-control,
  .editor-table .form-select,
  .cat-name,
  #listsView .btn,
  #editorView .form-control {
    font-size: 1rem;
  }

  .editor-table .form-control,
  .editor-table .form-select {
    min-height: 2.6rem;
  }

  .editor-table .c-actions .btn-group {
    display: grid;
    grid-template-columns: 1fr 1fr 2fr;
    gap: 0.5rem;
    width: 100%;
  }

  .editor-table .c-actions .btn {
    margin-left: 0 !important;
    padding: 0.5rem;
    border-radius: 0.375rem !important;
  }

  .editor-table tr.empty-row {
    display: block;
  }

  .editor-table tr.empty-row td {
    display: block;
  }

  /* заголовок группы: название на всю ширину, кнопки в ряд */
  .cat-toggle {
    width: 2.6rem;
    height: 2.6rem;
    padding: 0;
    font-size: 1.1rem;
  }

  .cat-name {
    /* стрелка + название занимают первую строку, счётчик и кнопки уходят во вторую */
    flex: 1 1 calc(100% - 3.3rem);
    min-width: 0;
    min-height: 2.6rem;
  }

  .cat-count {
    flex: 1 1 auto;
  }

  .cat-actions {
    display: flex;
    gap: 0.5rem;
  }

  .cat-actions > .btn {
    margin-left: 0 !important;
    padding: 0.45rem 0.9rem;
    border-radius: 0.375rem !important;
  }

  .card-footer .btn,
  #addCategoryBtn {
    width: 100%;
    padding: 0.6rem;
  }

  /* нижняя панель сохранения */
  .editor-bar {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
  }

  .editor-bar .btn {
    flex: 1 1 0;
    padding: 0.6rem;
  }

  .editor-bar #dirtyHint {
    flex: 1 1 100%;
    order: -1;
    text-align: center;
  }

  .saved-link-row .btn {
    flex: 1 1 0;
  }

  main.container {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }
}