/* ==========================================================
   СТО — рабочий интерфейс для ремонта и склада
   Светлая тема, крупные зоны нажатия, без внешних зависимостей.
   ========================================================== */

:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #172033;
  --muted: #6b7280;
  --line: #d9dee7;
  --line-strong: #c7ced9;

  --brand: #1859b8;
  --brand-dark: #10458f;
  --brand-soft: #eaf2ff;

  --ok: #137a4a;
  --ok-soft: #e8f7ef;
  --warn: #9a5a00;
  --warn-soft: #fff4d8;
  --danger: #b42318;
  --danger-dark: #8f1b13;
  --danger-soft: #fff0ee;

  --our: #1859b8;
  --our-soft: #e9f1ff;
  --client: #5d6675;
  --client-soft: #f2f4f7;
  --external: #6542a6;
  --external-soft: #f3effb;

  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 2px 8px rgba(24, 32, 51, .07);
  --shadow-raised: 0 10px 28px rgba(24, 32, 51, .11);
  --page-max: 1440px;
  --tap: 44px;
  --ease: 140ms ease;
}

* { box-sizing: border-box; }
html { color-scheme: light; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input, select, textarea { font: inherit; }
a { color: var(--brand); }
button, a, input, select, textarea { -webkit-tap-highlight-color: transparent; }

/* ---------- Верхняя панель ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  min-height: 64px;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 max(18px, calc((100vw - var(--page-max)) / 2));
  background: rgba(255, 255, 255, .96);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 0 rgba(24,32,51,.03);
  backdrop-filter: blur(10px);
}
.topbar__logo {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  padding: 0 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 22px;
  font-weight: 850;
  letter-spacing: .02em;
}
.topbar__burger {
  display: none;
  width: var(--tap);
  height: var(--tap);
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
}
.nav {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
.nav::-webkit-scrollbar { display: none; }
.nav__link {
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  padding: 0 13px;
  border-radius: 9px;
  color: #485267;
  text-decoration: none;
  font-weight: 650;
  white-space: nowrap;
  transition: background var(--ease), color var(--ease);
}
.nav__link:hover { background: var(--surface-2); color: var(--text); }
.nav__link--active {
  background: var(--brand-soft);
  color: var(--brand-dark);
  box-shadow: inset 0 0 0 1px #cddfff;
}

/* ---------- Страница ---------- */
.page {
  width: min(calc(100% - 32px), var(--page-max));
  margin: 24px auto 48px;
}
.page__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}
.page__title {
  margin: 0;
  font-size: clamp(26px, 2.4vw, 36px);
  line-height: 1.12;
  letter-spacing: -.025em;
}
.page__actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}
.page__actions form { margin: 0; }

/* ---------- Карточки ---------- */
.card {
  margin: 0 0 16px;
  /* Именно clip, а не hidden: hidden делает карточку областью прокрутки,
     и липкая шапка таблицы отсчитывает свои top: 64px внутрь карточки --
     заголовки наезжают на первую строку. clip обрезает так же, но
     области прокрутки не создаёт, и шапка липнет к окну, как задумано. */
  overflow: clip;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card__head {
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 18px;
  border-bottom: 1px solid #e7eaf0;
}
.card__title {
  margin: 0;
  font-size: 18px;
  line-height: 1.3;
}
.card__body { padding: 18px; }
.card__foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px 18px;
}

/* ---------- Кнопки ---------- */
.btn {
  min-width: var(--tap);
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 10px 16px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-weight: 750;
  line-height: 1.1;
  cursor: pointer;
  transition: transform var(--ease), background var(--ease), border-color var(--ease), box-shadow var(--ease);
}
.btn:hover { background: var(--surface-2); border-color: #aeb7c5; }
.btn:active { transform: translateY(1px); }
.btn--primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  box-shadow: 0 2px 5px rgba(24, 89, 184, .2);
}
.btn--primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn--ghost { background: transparent; color: #3e4a5f; }
.btn--danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn--danger:hover { background: var(--danger-dark); border-color: var(--danger-dark); }
.btn--small { min-height: var(--tap); padding: 8px 12px; font-size: 14px; }
/* Контурная красная: для удаления, которое стоит внизу карточки и не должно
   быть самым заметным пятном на экране. */
.btn--danger-ghost { background: transparent; border-color: #e0aaa4; color: var(--danger); }
.btn--danger-ghost:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn--wide { min-width: 180px; }
.btn--icon { width: var(--tap); padding: 0; font-size: 24px; line-height: 1; }
.btn[disabled] { opacity: .45; cursor: not-allowed; box-shadow: none; }

/* ---------- Формы ---------- */
.form { margin: 0; }
.form__grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}
.field { min-width: 0; display: flex; flex-direction: column; gap: 7px; }
.field__label { font-size: 14px; font-weight: 750; color: #3e485a; }
.field__hint, .hint { color: var(--muted); font-size: 14px; line-height: 1.4; }
.hint { margin: 0; }
.input, .select, .textarea, .search__input {
  width: 100%;
  min-height: var(--tap);
  border: 1px solid #bcc5d2;
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  padding: 10px 12px;
  font-size: 16px; /* не уменьшать: Safari на iPhone */
  line-height: 1.35;
  outline: none;
  transition: border-color var(--ease), box-shadow var(--ease), background var(--ease);
}
.textarea { min-height: 84px; resize: vertical; }
.select { padding-right: 34px; }
.input::placeholder, .textarea::placeholder, .search__input::placeholder { color: #818a99; opacity: 1; }
.input:focus, .select:focus, .textarea:focus, .search__input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(24, 89, 184, .14);
}
.field--error .input, .field--error .select, .field--error .textarea {
  border-color: var(--danger);
  background: #fffafa;
}
.error-text { color: var(--danger); font-size: 14px; font-weight: 650; }
.field--check { justify-content: flex-start; }
.checkbox {
  width: 24px;
  height: 24px;
  accent-color: var(--brand);
  margin: 7px 0;
}

/* ---------- Поиск и фильтры ---------- */
.filters {
  margin-bottom: 16px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.filters__row {
  display: flex;
  align-items: center;
  gap: 9px;
  flex-wrap: wrap;
}
.filters__row + .filters__row { margin-top: 10px; }
.search { flex: 1 1 320px; min-width: 240px; }
.search__input {
  padding-left: 42px;
  background-color: #fff;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='8'/%3E%3Cpath d='m21 21-4.35-4.35'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 14px center;
}
.chip {
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: #455064;
  text-decoration: none;
  font-size: 14px;
  font-weight: 750;
  white-space: nowrap;
}
.chip:hover { border-color: #aeb8c7; background: var(--surface-2); }
.chip--active { background: var(--brand-soft); border-color: #bcd2f6; color: var(--brand-dark); }
.chip--order { border-color: #e8bd64; background: var(--warn-soft); color: #754300; }
.chip--order.chip--active { background: #ffd978; border-color: #d29b28; color: #5a3400; }

/* ---------- Вкладки внутри раздела ---------- */
.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  margin: 0 0 16px;
  padding-bottom: 2px;
  border-bottom: 2px solid var(--line);
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs__link {
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  padding: 0 14px;
  margin-bottom: -2px;
  border-bottom: 3px solid transparent;
  color: #485267;
  text-decoration: none;
  font-weight: 700;
  white-space: nowrap;
}
.tabs__link:hover { color: var(--text); background: var(--surface-2); }
.tabs__link--active { color: var(--brand-dark); border-bottom-color: var(--brand); }

/* ---------- Плитки показателей ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin: 0 0 16px;
}
a.stat { color: inherit; text-decoration: none; }
.stat {
  min-width: 0;
  min-height: 94px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
  padding: 15px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: 4px solid #aab3c0;
  border-radius: 12px;
  box-shadow: var(--shadow);
}
a.stat:hover { box-shadow: var(--shadow-raised); }
.stat__label { color: var(--muted); font-size: 13px; font-weight: 750; text-transform: uppercase; letter-spacing: .035em; }
.stat__value { overflow-wrap: anywhere; font-size: clamp(23px, 2.2vw, 31px); font-weight: 850; line-height: 1.08; letter-spacing: -.02em; }
.stat--ok { border-top-color: var(--ok); background: linear-gradient(180deg, var(--ok-soft), #fff 44%); }
.stat--warn { border-top-color: #d79213; background: linear-gradient(180deg, var(--warn-soft), #fff 44%); }
.stat--danger { border-top-color: var(--danger); background: linear-gradient(180deg, var(--danger-soft), #fff 44%); }
.stat--muted { border-top-color: #7f8794; background: linear-gradient(180deg, #f0f2f5, #fff 44%); }
.money, .table__num, .stat__value, .total-row__value, .sticky-bar__value {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}
.money--neg { color: var(--danger); font-weight: 800; }

/* ---------- Метки ---------- */
.tag, .badge-qty {
  min-height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 4px 9px;
  border: 1px solid #d1d6df;
  border-radius: 999px;
  background: #f5f6f8;
  color: #4d5666;
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
}
.tag--paid { color: #075f36; background: var(--ok-soft); border-color: #9ad9b9; }
.tag--partial { color: #744500; background: var(--warn-soft); border-color: #e7bf66; }
.tag--debt { color: #8e160f; background: var(--danger-soft); border-color: #edaaa3; }
.tag--unknown { color: #5e6673; background: #f1f3f5; border-color: #d3d8df; }
.tag--our { color: var(--brand-dark); background: var(--our-soft); border-color: #aac6f1; border-radius: 8px; }
.tag--our::before { content: "■"; font-size: 8px; }
.tag--client { color: #495363; background: var(--client-soft); border-color: #aeb6c1; border-style: dashed; }
.tag--client::before { content: "◇"; font-size: 14px; line-height: 1; }
.tag--external { color: var(--external); background: var(--external-soft); border-color: #cbbce8; }
.badge-qty { min-width: 58px; color: #344054; background: #eef2f6; border-color: #c9d0da; }
.badge-qty--low { color: #704100; background: var(--warn-soft); border-color: #e4b85d; }
.badge-qty--zero { color: #8f1d15; background: var(--danger-soft); border-color: #e7a49e; }

/* ---------- Переключатели ---------- */
.switch {
  display: inline-flex;
  align-items: stretch;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f3f5f8;
}
.switch__option {
  min-height: var(--tap);
  min-width: 74px;
  padding: 8px 13px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: #536072;
  font-weight: 800;
  cursor: pointer;
  transition: background var(--ease), color var(--ease), border-color var(--ease), box-shadow var(--ease);
}
.switch__option:hover { background: #fff; }
.switch--owner .switch__option { min-width: 92px; text-transform: uppercase; letter-spacing: .025em; }
.switch--owner .switch__option--our::before { content: "■ "; font-size: 8px; vertical-align: middle; }
.switch--owner .switch__option--client::before { content: "◇ "; font-size: 15px; vertical-align: -1px; }
.switch--owner .switch__option--our.switch__option--active {
  color: #fff;
  background: var(--our);
  border-color: var(--our);
  box-shadow: 0 2px 6px rgba(24,89,184,.23);
}
.switch--owner .switch__option--client.switch__option--active {
  color: #303846;
  background: #fff;
  border: 2px dashed #7e8794;
  box-shadow: 0 1px 4px rgba(24,32,51,.08);
}
.switch--pay { flex-wrap: wrap; }
.switch--pay .switch__option { min-width: 100px; }
.switch--pay .switch__option--paid.switch__option--active { color: #fff; background: var(--ok); }
.switch--pay .switch__option--partial.switch__option--active { color: #4e2d00; background: #ffc84c; border-color: #d59e21; }
.switch--pay .switch__option--debt.switch__option--active { color: #fff; background: var(--danger); }
.switch--pay .switch__option--unknown.switch__option--active { color: #fff; background: #66707e; }

/* ---------- Таблицы ---------- */
.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
}
.table th, .table td {
  padding: 13px 14px;
  border-bottom: 1px solid #e5e8ed;
  vertical-align: middle;
  text-align: left;
}
.table th {
  position: sticky;
  top: 64px;
  z-index: 5;
  background: #f3f5f8;
  color: #596273;
  font-size: 12px;
  font-weight: 850;
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
}
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr[onclick] { cursor: pointer; }
.table tbody tr:hover { background: #f7faff; }
.table--compact th, .table--compact td { padding-top: 10px; padding-bottom: 10px; }
.table__num { text-align: right !important; white-space: nowrap; }
.table__row--muted { color: #727b88; background: #fafafa; }
.table__cell--muted { color: var(--muted); background: #f6f7f9; }
.table__cell--action { min-width: 118px; }
.table__cell--action .btn { width: 100%; }

/* ---------- Редактор запчастей ---------- */
.parts-editor { display: flex; flex-direction: column; gap: 10px; }
.parts-editor__row {
  display: grid;
  grid-template-columns: minmax(250px, 2.4fr) minmax(90px, .7fr) minmax(190px, 1.15fr) minmax(105px, .8fr) minmax(105px, .8fr) 44px;
  gap: 9px;
  align-items: end;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fbfcfe;
}
.parts-editor__cell { min-width: 0; }
.parts-editor__cell::before {
  content: attr(data-label);
  display: block;
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .035em;
}
.parts-editor__cell--name { display: grid; gap: 7px; }
.parts-editor__remove { align-self: end; color: var(--danger); border-color: #e3bbb7; background: #fff; }
.parts-editor__add { width: 100%; margin-top: 10px; border-style: dashed; color: var(--brand); }

/* ---------- Итоги ---------- */
.total-row {
  display: grid;
  grid-template-columns: auto minmax(110px, 1fr) auto minmax(110px, 1fr) auto minmax(140px, 1.2fr);
  align-items: center;
  gap: 8px 14px;
  margin: 16px 0;
  padding: 16px 18px;
  border: 1px solid #c9d8ee;
  border-radius: var(--radius);
  background: #f3f7fd;
}
.total-row__label { color: var(--muted); font-size: 13px; font-weight: 800; text-transform: uppercase; }
.total-row__value { text-align: right; font-size: 20px; font-weight: 850; }
/* nth-last-of-type считает среди всех span подряд, поэтому (1) попадал
   на значение, а не на подпись «Итого», и правило не срабатывало. */
.total-row__label:nth-last-of-type(2), .total-row__value:last-child { color: var(--brand-dark); }
.total-row__value:last-child { font-size: 25px; }
.sticky-bar { display: none; }
/* Шапка для печати: на экране не нужна, появляется только на бумаге */
.print-head { display: none; }
.print-head__title { font-weight: 800; }

/* Удаление ремонта: отдельно от остальных действий, у правого края */
.order-delete { display: flex; justify-content: flex-end; margin: 22px 0 4px; }
.order-delete form { margin: 0; }

/* ---------- Сообщения ---------- */
.flash {
  margin: 0 0 16px;
  padding: 13px 15px;
  border: 1px solid #d6dde7;
  border-left: 5px solid #778395;
  border-radius: 10px;
  background: #fff;
  color: #465163;
}
.flash--ok { border-color: #a8d8be; border-left-color: var(--ok); background: var(--ok-soft); color: #195d3d; }
.flash--error { border-color: #edb5b0; border-left-color: var(--danger); background: var(--danger-soft); color: #7e251f; }
.empty { padding: 32px 18px; text-align: center; color: var(--muted); font-weight: 650; }

/* ---------- Пагинация ---------- */
.pager { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 18px 0; flex-wrap: wrap; }
.pager__link {
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: #465163;
  text-decoration: none;
}

/* ---------- Фокус с клавиатуры ---------- */
:focus-visible {
  outline: 3px solid rgba(24, 89, 184, .32);
  outline-offset: 2px;
}

/* ==========================================================
   Планшет / узкий компьютер
   ========================================================== */
@media (max-width: 1050px) {
  .form__grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .parts-editor__row {
    grid-template-columns: minmax(220px, 2fr) 90px minmax(190px, 1fr) 110px 110px 44px;
    overflow-x: auto;
  }
  .table th, .table td { padding-left: 10px; padding-right: 10px; }
}

/* ==========================================================
   Телефон — полноценный рабочий режим Игоря
   ========================================================== */
@media (max-width: 760px) {
  body { padding-bottom: 66px; }

  .topbar {
    position: sticky;
    min-height: 56px;
    padding: 0 12px;
    gap: 8px;
    /* Без blur: backdrop-filter делает панель точкой отсчёта для вложенных
       position: fixed, и нижнее меню прилипало к низу панели, а не экрана.
       Фон здесь и так почти непрозрачный, размытие ничего не давало. */
    backdrop-filter: none;
  }
  .topbar__burger { display: none; }
  .topbar__logo { min-width: 54px; padding: 0; font-size: 20px; }
  .nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 60;
    height: 62px;
    gap: 0;
    padding: 5px max(6px, env(safe-area-inset-right)) calc(5px + env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    background: rgba(255,255,255,.98);
    border-top: 1px solid var(--line);
    box-shadow: 0 -4px 16px rgba(24,32,51,.08);
    overflow-x: auto;
  }
  .nav__link {
    min-width: 78px;
    min-height: 50px;
    padding: 0 10px;
    justify-content: center;
    border-radius: 9px;
    font-size: 13px;
  }
  /* Порядок в нижнем меню: чем чаще нужно, тем левее. Меню прокручивается,
     но первые три пункта должны попадать под палец без прокрутки. */
  .nav__link[href="/orders"] { order: 1; }
  .nav__link[href="/stock"] { order: 2; }
  .nav__link[href="/"] { order: 3; }
  .nav__link[href="/cash"] { order: 4; }
  .nav__link[href="/clients"] { order: 5; }
  .nav__link[href="/works"] { order: 6; }
  .nav__link[href="/suppliers"] { order: 7; }
  .nav__link[href="/reports"] { order: 8; }

  .page {
    width: min(calc(100% - 20px), var(--page-max));
    margin: 14px auto 28px;
  }
  .page__head { align-items: flex-start; flex-direction: column; gap: 10px; margin-bottom: 12px; }
  .page__title { font-size: 27px; }
  .page__actions { width: 100%; justify-content: flex-start; }
  .page__actions > .btn, .page__actions > form { flex: 1 1 auto; }
  .page__actions > form .btn { width: 100%; }

  .card { margin-bottom: 12px; border-radius: 12px; }
  .card__head { min-height: 0; align-items: flex-start; flex-direction: column; padding: 13px 14px; }
  .card__title { font-size: 18px; }
  .card__body { padding: 14px; }
  .card__foot { padding: 12px 14px 14px; flex-direction: column; align-items: stretch; }
  .card__foot .btn { width: 100%; }

  .form__grid { grid-template-columns: 1fr; gap: 14px; }
  .input, .select, .textarea, .search__input { min-height: 48px; font-size: 16px; }
  .btn { min-height: 48px; }
  .btn--icon { width: 48px; }

  .filters { padding: 10px; margin-bottom: 12px; }
  .filters__row { align-items: stretch; gap: 8px; }
  .search { order: -10; flex: 1 0 100%; min-width: 100%; }
  .search__input { min-height: 52px; font-weight: 650; }
  .filters__row > .select,
  .filters__row > .input { flex: 1 1 145px; }
  .tabs { gap: 0; margin-bottom: 12px; }
  .tabs__link { padding: 0 12px; font-size: 15px; }

  .chip { min-height: 46px; padding-inline: 14px; }
  .chip--order { font-size: 15px; }

  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 12px;
  }
  .stat { min-height: 84px; padding: 12px; border-radius: 11px; }
  .stat__label { font-size: 11px; }
  .stat__value { font-size: 22px; }

  /* Все таблицы превращаем в вертикальные карточки без горизонтального скролла. */
  .card:has(> .table) { overflow: visible; background: transparent; border: 0; box-shadow: none; }
  .card > .table { background: transparent; }
  .table, .table tbody { display: block; width: 100%; }
  .table thead { display: none; }
  .table tr {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0;
    margin-bottom: 10px;
    overflow: hidden;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow);
  }
  .table td {
    min-width: 0;
    display: grid;
    grid-template-columns: minmax(105px, 38%) minmax(0, 1fr);
    align-items: center;
    gap: 10px;
    min-height: 43px;
    padding: 9px 12px;
    border-bottom: 1px solid #eceef2;
    text-align: right !important;
    overflow-wrap: anywhere;
  }
  .table td:last-child { border-bottom: 0; }
  .table td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
    text-align: left;
    text-transform: uppercase;
    letter-spacing: .025em;
  }
  .table td:first-child {
    grid-template-columns: 1fr;
    padding-top: 12px;
    padding-bottom: 12px;
    background: #f8fafc;
    text-align: left !important;
    font-size: 17px;
    font-weight: 800;
  }
  .table td:first-child::before { margin-bottom: -3px; }
  .table td .tag, .table td .badge-qty { justify-self: end; }
  .table__cell--action { min-width: 0; }
  .table__cell--action .btn { width: 100%; min-height: 48px; font-size: 15px; }
  .table__row--muted { opacity: 1; }
  .table__row--muted td:first-child { background: #f5f5f5; }

  /* На складе остаток и заказ визуально сильнее остальных полей. */
  .table td[data-label="Остаток"] { background: #fbfcfe; font-size: 16px; font-weight: 800; }
  .table td[data-label="Заказ"] { padding-top: 10px; padding-bottom: 10px; background: #fffaf0; }
  .table td[data-label="Заказ"]::before { color: #875100; }
  .table td[data-label="Позиция"] { font-size: 17px; }
  .table td[data-label="Артикул"],
  .table td[data-label="Производитель"] { color: #5f6877; font-size: 14px; }

  .switch { width: 100%; }
  .switch__option { flex: 1 1 0; min-height: 48px; padding: 8px 9px; }
  .switch--owner .switch__option { min-width: 0; font-size: 16px; }
  .switch--pay { display: grid; grid-template-columns: 1fr 1fr; }
  .switch--pay .switch__option { min-width: 0; }

  .parts-editor__row {
    position: relative;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    overflow: visible;
    padding: 14px;
    border-radius: 13px;
    background: #fff;
    box-shadow: var(--shadow);
  }
  .parts-editor__cell { display: block; }
  .parts-editor__cell::before { font-size: 11px; margin-bottom: 5px; }
  .parts-editor__cell--name { grid-column: 1 / -1; display: grid; gap: 7px; }
  .parts-editor__cell--owner { grid-column: 1 / -1; }
  .parts-editor__cell--manufacturer,
  .parts-editor__cell--article { grid-column: 1 / -1; }
  .parts-editor__cell--unit { grid-column: 1 / 2; }
  .parts-editor__cell--qty { grid-column: 2 / 3; }
  /* В наряде ячейки с единицей измерения нет (она только на приходе),
     поэтому количество занимало правую колонку, а левая пустовала. */
  .parts-editor__row:not(:has(.parts-editor__cell--unit)) .parts-editor__cell--qty {
    grid-column: 1 / -1;
  }
  .parts-editor__cell--cost { grid-column: 1 / 2; }
  .parts-editor__cell--price { grid-column: 2 / 3; }
  .parts-editor__remove {
    grid-column: 1 / -1;
    width: 100%;
    height: 46px;
    font-size: 20px;
    border-color: #ecc1bd;
    background: #fff8f7;
  }
  .parts-editor__add { min-height: 52px; font-size: 16px; }

  .total-row {
    grid-template-columns: 1fr auto;
    gap: 7px 12px;
    padding: 14px;
  }
  .total-row__value { font-size: 19px; }
  .total-row__value:last-child { font-size: 24px; }

  .form:has(.sticky-bar) { padding-bottom: 88px; }
  .sticky-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 62px;
    z-index: 55;
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 72px;
    padding: 10px max(10px, env(safe-area-inset-right)) 10px max(10px, env(safe-area-inset-left));
    background: rgba(255,255,255,.98);
    border-top: 1px solid var(--line-strong);
    box-shadow: 0 -6px 22px rgba(24,32,51,.11);
  }
  .sticky-bar__total { min-width: 0; flex: 1; display: flex; flex-direction: column; }
  .sticky-bar__label { color: var(--muted); font-size: 11px; font-weight: 800; text-transform: uppercase; }
  .sticky-bar__value { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 23px; font-weight: 900; color: var(--brand-dark); }
  .sticky-bar__save { flex: 0 0 auto; min-width: 128px; min-height: 52px; }

  /* Ниже кнопки идут закреплённые панель итога и меню -- оставляем запас,
     иначе они её закрывают. Ширину не растягиваем: это удаление. */
  .order-delete { justify-content: center; margin: 18px 0 96px; }
  .order-delete .btn { width: auto; min-height: 44px; }

  .flash { margin-bottom: 12px; padding: 12px; }
  .pager { justify-content: stretch; }
  .pager__link { flex: 1; }
}

@media (max-width: 420px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .stat__value { font-size: 20px; }
  .switch--pay { grid-template-columns: 1fr 1fr; }
  .sticky-bar__save { min-width: 112px; }
  .table td { grid-template-columns: minmax(92px, 36%) minmax(0, 1fr); }
}

/* ==========================================================
   Печать: на бумаге нужны только данные
   ========================================================== */
@media print {
  .topbar, .tabs, .filters, .page__actions, .sticky-bar, .order-delete,
  .pager, .flash, .btn, .card__foot, .table__cell--action,
  .parts-editor__add, .parts-editor__remove, .no-print { display: none !important; }

  body { background: #fff; padding: 0; font-size: 11pt; }
  .page { width: auto; margin: 0; padding: 0; }
  .page__head { display: block; margin-bottom: 6mm; }
  .page__title { font-size: 16pt; }

  /* Шапка листа: что за документ и на какое число */
  .print-head { display: block !important; margin-bottom: 4mm;
    padding-bottom: 2mm; border-bottom: 1pt solid #000; font-size: 10pt; }

  .card { break-inside: auto; margin-bottom: 4mm; border: 1pt solid #999;
    border-radius: 0; box-shadow: none; }
  .card__head { min-height: 0; padding: 2mm 3mm; }
  .card__body { padding: 3mm; }

  .table { font-size: 9pt; }
  .table th { position: static; background: #eee !important; padding: 1.5mm 2mm;
    -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .table td { padding: 1.5mm 2mm; }
  .table tr { break-inside: avoid; }
  thead { display: table-header-group; }  /* шапка повторяется на каждом листе */

  .stats { display: flex; gap: 4mm; }
  .stat { min-height: 0; padding: 2mm 3mm; border: 1pt solid #999;
    border-radius: 0; box-shadow: none; background: #fff !important; }
  .stat__value { font-size: 12pt; }

  .tag, .badge-qty { border: 1pt solid #999; background: #fff !important;
    color: #000 !important; }
  a { color: #000; text-decoration: none; }

  @page { margin: 12mm 10mm; }
}

/* Движение сведено к минимуму: интерфейс должен быть мгновенным. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}

/* ---------- Работы в наряде и комплекты ----------
   Разметка повторяет строку запчасти: те же классы-модификаторы,
   те же подписи ячеек через data-label. */
.works-editor, .kit-editor { display: flex; flex-direction: column; gap: 10px; }
.works-editor__row {
  display: grid;
  grid-template-columns: minmax(250px, 2.6fr) minmax(80px, .6fr) minmax(110px, .8fr) minmax(150px, 1fr) 44px;
  gap: 9px;
  align-items: end;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--card);
}
.kit-editor__row {
  display: grid;
  grid-template-columns: minmax(250px, 3fr) minmax(90px, .7fr) 44px;
  gap: 9px;
  align-items: end;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--card);
}
.works-editor__cell, .kit-editor__cell { min-width: 0; }
.works-editor__cell::before, .kit-editor__cell::before {
  content: attr(data-label);
  display: block;
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}
.works-editor__cell--name { display: grid; gap: 7px; }
.works-editor__remove, .kit-editor__remove {
  align-self: end; color: var(--danger); border-color: #e3bbb7; background: #fff;
}
.works-editor__add, .kit-editor__add {
  width: 100%; margin-top: 10px; border-style: dashed; color: var(--brand);
}

/* Что списалось комплектом -- список, а не редактор: руками его не правят */
.kit-list { margin: 0; padding: 0; list-style: none; display: grid; gap: 8px; }
.kit-list__item {
  display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline;
  padding: 9px 12px; border: 1px solid var(--line);
  border-radius: var(--radius-sm); background: var(--card);
}
.kit-list__name { font-weight: 700; }
.kit-list__qty { font-variant-numeric: tabular-nums; }
.kit-list__from { color: var(--muted); font-size: 13px; }
.kit-part {
  display: inline-block; margin: 2px 6px 0 0;
  color: var(--muted); font-size: 12px;
}

/* Долги заказчика под формой возврата */
.debt-list { margin: 14px 0 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.debt-list__item {
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px;
  align-items: baseline; padding: 7px 0; border-top: 1px solid var(--line);
}
.debt-list__desc { color: var(--muted); }
.debt-list__sum { font-variant-numeric: tabular-nums; }

.works-table .input--num { max-width: 110px; text-align: right; }

@media (max-width: 720px) {
  .works-editor__row {
    grid-template-columns: 1fr 1fr;
    padding: 14px;
  }
  .works-editor__cell--name { grid-column: 1 / -1; }
  .works-editor__cell--kit { grid-column: 1 / -1; }
  .works-editor__remove {
    grid-column: 1 / -1; width: 100%; height: 46px; font-size: 20px;
    border-color: #ecc1bd; background: #fff8f7;
  }
  .kit-editor__row { grid-template-columns: 1fr; padding: 14px; }
  .kit-editor__remove { width: 100%; height: 46px; font-size: 20px; }
  .debt-list__item { grid-template-columns: auto auto; }
  .debt-list__desc { grid-column: 1 / -1; }
}

@media print {
  .works-editor__add, .works-editor__remove,
  .kit-editor__add, .kit-editor__remove { display: none !important; }
}

/* Модификаторы полей, которых не было: широкое поле на всю строку сетки
   и ячейка под кнопку, чтобы она вставала по нижнему краю ряда. */
.field--wide { grid-column: 1 / -1; }
.field--action { justify-content: flex-end; }
.field--action .btn { min-height: 44px; }
.field--inline { flex-direction: row; align-items: center; gap: 8px; }
.field--inline .field__label { white-space: nowrap; }

.card--accent { border-color: #c9d8f4; box-shadow: inset 3px 0 0 var(--brand); }

.tag--kit { color: var(--brand-dark); background: var(--our-soft); border-color: #aac6f1; }
.tag--cash-in { color: #075f36; background: var(--ok-soft); border-color: #9ad9b9; }
.tag--cash-out { color: #8e160f; background: var(--danger-soft); border-color: #edaaa3; }

.switch--kit .switch__option { min-width: 74px; }
.switch--kit .switch__option--on.switch__option--active { color: #fff; background: var(--brand); }
.switch--kit .switch__option--off.switch__option--active { color: #fff; background: #66707e; }
.switch--cash .switch__option { min-width: 92px; }
.switch--cash .switch__option--in.switch__option--active { color: #fff; background: var(--ok); }
.switch--cash .switch__option--out.switch__option--active { color: #fff; background: var(--danger); }

@media (max-width: 720px) {
  .field--inline { flex-direction: column; align-items: stretch; }
}

@media (max-width: 720px) {
  /* Справочник работ длинный: сотня с лишним позиций. В обычной "карточной"
     раскладке страница вырастала до тридцати экранов, и до нужной работы
     приходилось долго крутить. Здесь строка -- одна: название и цена рядом. */
  .works-table tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 116px;
    align-items: center;
    gap: 4px 10px;
    padding: 10px 12px;
  }
  .works-table td {
    display: block;
    min-height: 0;
    padding: 0;
    border-bottom: 0;
    background: transparent !important;
    text-align: left !important;
  }
  .works-table td::before { display: none; }
  .works-table td[data-label="Работа"] { grid-column: 1; font-size: 16px; font-weight: 700; }
  .works-table td[data-label="Цена"] { grid-column: 2; grid-row: 1 / span 2; }
  .works-table td[data-label="Цена"] .input--num {
    max-width: none; width: 100%; min-height: 46px; text-align: right;
  }
  .works-table td[data-label="Было раз"] {
    grid-column: 1;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
  }
  .works-table td[data-label="Было раз"]::before {
    display: inline;
    content: "в ремонтах: ";
    text-transform: none;
    letter-spacing: 0;
  }
  .works-table td[data-label="Комплект"] { grid-column: 1 / -1; }
  /* Пустая клетка комплекта на телефоне рисовала одинокий прочерк */
  .works-table td.cell--empty { display: none; }
  .works-table .table__cell--action { display: none; }
}

/* ---------- Вход ---------- */
.gate {
  display: grid; place-items: center; min-height: 100vh; padding: 24px 16px;
  background: var(--surface-2, #eef1f5);
}
.gate__card {
  width: min(100%, 380px); padding: 28px 24px 26px;
  background: var(--card, #fff); border: 1px solid var(--line);
  border-radius: var(--radius, 14px); box-shadow: var(--shadow);
}
.gate__card--wide { width: min(100%, 460px); }
.gate__title { margin: 0; font-size: 30px; letter-spacing: -.01em; }
.gate__lede { margin: 6px 0 20px; color: var(--muted); }
.gate .form { display: grid; gap: 16px; }
.gate .input { min-height: 48px; }

/* Кто вошёл -- в шапке справа */
.who { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.who__name { font-weight: 750; }
.who__out { color: var(--muted); font-size: 14px; }
.who__out:hover { color: var(--danger); }

.inline-form { display: flex; gap: 6px; align-items: center; }
.inline-form .input { min-width: 0; }

@media (max-width: 720px) {
  .who { margin-left: auto; gap: 8px; font-size: 14px; }
  .who__name { max-width: 96px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .inline-form { flex-direction: column; align-items: stretch; }
  .inline-form .btn { width: 100%; }
}
@media print { .who { display: none !important; } }
