:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --text: #1c1c1e;
  --muted: #6b7280;
  --line: #d8dae0;
  --accent: #1d4ed8;
  --danger: #b91c1c;
  --danger-bg: #fef2f2;
  --ok: #15803d;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }

.nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.nav-link { text-decoration: none; font-weight: 600; }
.nav-user { margin-left: auto; color: var(--muted); }
.nav-logout { margin: 0; }

#flash:empty { display: none; }
#flash { padding: 0 16px; margin-top: 12px; }

.main { padding: 16px; max-width: 1100px; }

h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 18px; margin: 24px 0 12px; }

.field { display: block; margin-bottom: 12px; }
.field-label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field-check { display: flex; align-items: center; gap: 8px; }
.field-check .field-label { margin: 0; }

input[type="text"], input[type="date"], input[type="password"], input[type="search"],
select, textarea {
  width: 100%;
  max-width: 420px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
}

.field-error { color: var(--danger); font-size: 13px; margin-top: 4px; }

.btn {
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-link { border: 0; background: none; color: var(--accent); padding: 0; }

.alert {
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--panel);
}

.alert-error { border-color: var(--danger); background: var(--danger-bg); color: var(--danger); }
.alert-message { margin: 0; }
.alert-reasons { margin: 6px 0 0; padding-left: 18px; }

.table-wrap { overflow-x: auto; }

table { border-collapse: collapse; width: 100%; background: var(--panel); }
th, td { padding: 7px 9px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: 13px; color: var(--muted); font-weight: 600; }

.money { font-variant-numeric: tabular-nums; white-space: nowrap; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--line);
  white-space: nowrap;
}

.badge-draft { color: var(--muted); }
.badge-issued { color: var(--accent); border-color: var(--accent); }
.badge-sent { color: #b45309; border-color: #b45309; }
.badge-signed { color: #6d28d9; border-color: #6d28d9; }
.badge-paid { color: var(--ok); border-color: var(--ok); }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }

.login { max-width: 360px; margin: 48px auto; padding: 20px; background: var(--panel);
         border: 1px solid var(--line); border-radius: 10px; }
.login-title { text-align: center; }
.login-form .btn { width: 100%; }

.error-page { max-width: 560px; }

@media (max-width: 480px) {
  .main { padding: 12px; }
  .nav { gap: 8px; padding: 8px 12px; }
  .nav-user { width: 100%; margin-left: 0; order: 10; }
}

/* --- настройки: исполнители, счета, пользователи --- */

.settings-nav { display: flex; gap: 16px; margin-bottom: 16px; }

.settings-link {
  text-decoration: none;
  padding: 4px 0;
  border-bottom: 2px solid transparent;
}

.settings-link.current { border-bottom-color: var(--accent); font-weight: 600; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}

.card h2 { margin-top: 0; }
.card h3 { font-size: 15px; margin: 16px 0 8px; }

.requisites {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 4px 12px;
  margin: 12px 0;
}

.requisites dt { color: var(--muted); font-size: 13px; }
.requisites dd { margin: 0; }

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

.form { max-width: 460px; }

.account-form,
.user-edit,
.user-password {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  margin: 0 0 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.account-form .field,
.user-edit .field,
.user-password .field { margin-bottom: 0; width: 180px; }

.account-form input,
.user-edit input,
.user-edit select,
.user-password input { max-width: 180px; }

.user-login { font-weight: 600; white-space: nowrap; }

@media (max-width: 480px) {
  .requisites { grid-template-columns: 1fr; }
  .account-form .field,
  .user-edit .field,
  .user-password .field { width: 100%; }
  .account-form input,
  .user-edit input,
  .user-edit select,
  .user-password input { max-width: 100%; }
}

/* --- справочники: разделы, фильтры, инлайн-строки --- */

.catalog-nav { display: flex; flex-wrap: wrap; gap: 16px; margin-bottom: 16px; }

.catalog-link {
  text-decoration: none;
  padding: 4px 0;
  border-bottom: 2px solid transparent;
}

.catalog-link.current { border-bottom-color: var(--accent); font-weight: 600; }

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  margin-bottom: 12px;
}

.filters .field { margin-bottom: 0; }
.filters input[type="search"] { width: 260px; }

/* «Работ не найдено» рядом с формой-строкой не показывается: кнопка создания
   вставляет форму afterbegin, и заглушка перестаёт быть единственной строкой тела */
tbody .empty-row:not(:only-child) { display: none; }

.row-actions { white-space: nowrap; }
.row-actions .btn { padding: 4px 10px; font-size: 13px; }
.row-actions .btn + .btn { margin-left: 6px; }

/* форма-строка: поля лежат прямо в <td> без .field, ширину задаёт ячейка */
td > input[type="text"] { max-width: 100%; }
td > .field-error { margin-top: 2px; }

.override-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.override-form .field { margin-bottom: 0; width: 180px; }
.override-form input { max-width: 180px; }

@media (max-width: 480px) {
  .catalog-nav { gap: 12px; }
  .filters input[type="search"] { width: 100%; }
  .override-form .field { width: 100%; }
  .override-form input { max-width: 100%; }
  .row-actions .btn { padding: 4px 8px; }
}

/* --- заказы: итог списка и автодополнение клиента --- */

.list-total { margin: 10px 0 0; color: var(--muted); }
.list-total .money { color: var(--text); font-weight: 600; }
.pager { display: flex; align-items: center; gap: 8px; margin: 8px 0 0; color: var(--muted); }
.pager .btn[disabled] { opacity: .5; cursor: default; }

.autocomplete { position: relative; margin-bottom: 12px; max-width: 420px; }
.autocomplete .current { display: inline-block; margin-right: 8px; font-weight: 600; }
.autocomplete input[type="search"] { margin-top: 6px; }

.search-results {
  position: absolute;
  z-index: 5;
  left: 0;
  right: 0;
  margin: 2px 0 0;
  padding: 0;
  list-style: none;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  max-height: 260px;
  overflow-y: auto;
}

.search-results:empty { display: none; }
.search-results li { border-bottom: 1px solid var(--line); }
.search-results li:last-child { border-bottom: 0; }
.search-results li.muted { padding: 8px 10px; }

.search-results button {
  display: block;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.search-results button:hover { background: var(--bg); }

.picker-block { margin: 16px 0; }

@media (max-width: 480px) {
  .autocomplete { max-width: 100%; }
  .search-results { max-height: 200px; }
}

/* --- страница заказа: шапка, подсказки полей, итоги --- */

.order-client { margin: -8px 0 16px; color: var(--muted); }

.fields { display: flex; flex-wrap: wrap; gap: 12px; }
.fields .field { width: 220px; }
.fields select { max-width: 220px; }

.hint { margin: -4px 0 12px; color: var(--muted); font-size: 13px; }

.totals { max-width: 420px; }
.totals-row { display: flex; justify-content: space-between; gap: 12px; padding: 4px 0; }
.totals-label { color: var(--muted); }
.totals-total {
  margin-top: 4px;
  padding-top: 8px;
  border-top: 2px solid var(--line);
  font-weight: 600;
}
.totals-total .totals-label { color: inherit; }

/* --- строки работ и запчастей --- */

.lines .pos { width: 36px; color: var(--muted); }
.lines .amount { text-align: right; white-space: nowrap; }

/* .lines-add позиционирует выпадающие списки подсказок: сами .search-results
   абсолютные, и без этого они считали бы координаты от всей страницы */
.lines-add { position: relative; margin-top: 12px; }
.lines-search { display: flex; align-items: center; gap: 8px; }
.lines-search input[type="search"] { flex: 1; max-width: 420px; }

.free-line { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px; margin-top: 12px; }
.free-line h3 { width: 100%; margin: 0; }
.free-line .field { margin-bottom: 0; width: 160px; }

@media (max-width: 480px) {
  .fields .field { width: 100%; }
  .fields select { max-width: 100%; }
  .free-line .field { width: 100%; }
}

/* --- панель действий заказа --- */

/* Без правила кнопки панели соприкасаются краями: формы перехода и одиночные
   кнопки идут подряд без отступов, и на 375px «Удалить» вплотную к «Копировать».
   Раскладка та же, что у .actions; форма с полем занимает свою строку целиком. */
.panel-actions { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px; }
.panel-action { width: 100%; }
.panel-action .field { margin-bottom: 8px; }

/* --- карточка печати --- */

/* Ссылки печати — обычные <a> с видом кнопки: .btn задан для button и не убирает
   подчёркивание ссылки, поэтому оба правила ниже обязательны. Раскладка та же,
   что у .panel-actions, чтобы карточки не расходились на 375px. */
.print-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.print-actions .btn { display: inline-block; text-decoration: none; }
.print-hint { margin: 8px 0 0; color: var(--muted); font-size: 13px; }
