:root {
  --bg: #f5f6f8;
  --surface: #fff;
  --text: #1c1f24;
  --muted: #69707c;
  --line: #d5d9e0;
  --accent: #1f5fbf;
  --accent-text: #fff;
  --warn-bg: #fff4d6;
  --err: #b3261e;
  --row-repaired: #eef6ee;
  --row-verified: #e3f0e3;
  --row-overdue: #fdecea;
  --row-selected: #dbe7fb;
  --danger: #b3261e;
  --filter-on: #e6effc;
  --warn-line: #e3b341;
  --ok: #1d7a33;
  --warn-text: #8a5a00;
  color-scheme: light dark; /* встроенные элементы браузера (поля, полосы прокрутки) следуют теме */
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171b;
    --surface: #1e2127;
    --text: #e6e8eb;
    --muted: #9aa1ad;
    --line: #353a43;
    --accent: #5b9bff;
    --accent-text: #0b1220;
    --warn-bg: #3d3314;
    --err: #ff8a80;
    --row-repaired: #1d2a1f;
    --row-verified: #1a311d;
    --row-overdue: #3a1f1d;
    --row-selected: #22324d;
    --danger: #ff8a80;
    --filter-on: #22324d;
    --warn-line: #8a6d1f;
    --ok: #6fcf85;
    --warn-text: #f0c35a;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.4 system-ui, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
}
a.button { display: inline-block; border: 1px solid var(--line); background: var(--surface); padding: 6px 12px; border-radius: 6px; color: inherit; text-decoration: none; }
a.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.link { border: 0; background: none; color: var(--accent); }
input, select, textarea {
  width: 100%;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}
select:disabled { opacity: .5; }
label { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); }
label > * { font-size: 14px; color: var(--text); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--err); }
.warn { background: var(--warn-bg); padding: 8px 12px; border-radius: 6px; }

/* Вход */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 24px; width: min(360px, 100%); display: grid; gap: 14px;
}
.card h1 { margin: 0; font-size: 20px; }

/* Каркас */
#app { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  display: flex; align-items: center; gap: 4px 12px; flex-wrap: wrap;
  padding: 4px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 16px; letter-spacing: .01em; padding-right: 12px; border-right: 1px solid var(--line); }
.login-sub { margin: -8px 0 0; font-size: 13px; }
.spacer { flex: 1; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab, .subtab { white-space: nowrap; }
.tab { border: 0; border-bottom: 2px solid transparent; border-radius: 6px 6px 0 0; background: none; padding: 8px 14px; }
.tab:hover { background: var(--bg); }
.tab.active { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }

.filters { padding: 8px 16px; display: grid; gap: 8px; }
.filters .row { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; align-items: end; }

.table-wrap { flex: 1; overflow: auto; margin: 0 16px; background: var(--surface); border: 1px solid var(--line); }
.grid { border-collapse: collapse; width: 100%; min-width: 1100px; }
.grid th, .grid td {
  border: 1px solid var(--line); padding: 4px 6px; vertical-align: top;
  white-space: pre-line; text-align: center;
}
.grid th { position: sticky; top: 0; background: var(--surface); z-index: 1; font-weight: 600; }
.grid th.wide { width: 18%; }
.grid th[data-sort] { cursor: pointer; user-select: none; white-space: nowrap; }
.grid th[data-sort]:hover { text-decoration: underline; }
/* Рядом со стрелкой — номер уровня, если сортировка по нескольким столбцам */
.grid th[aria-sort=ascending]::after { content: " ▲" attr(data-level); font-size: 0.75em; }
.grid th[aria-sort=descending]::after { content: " ▼" attr(data-level); font-size: 0.75em; }
.grid td.text { text-align: left; }
.grid tr.repaired td { background: var(--row-repaired); }
.grid tr.verified td { background: var(--row-verified); }
.grid tr.overdue td { background: var(--row-overdue); }
.grid tr.empty td { padding: 24px; color: var(--muted); }

.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#admin > section { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.bottombar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px 16px; flex-wrap: wrap;
  padding: 8px 16px;
}
.pager, .toolbar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.toolbar .sep { width: 1px; align-self: stretch; background: var(--line); margin: 0 4px; }

.grid th.sel, .grid td.sel { width: 36px; cursor: pointer; }
.grid td.sel input, .grid th.sel input { width: auto; cursor: pointer; }
.grid tbody tr[data-id] { cursor: pointer; }
#grid tbody tr[data-id]:hover td { filter: brightness(.97); }
.grid tr.selected td { background: var(--row-selected); }
.grid.compact { min-width: 700px; }
.grid.compact input:not([type=checkbox]) { min-width: 140px; }
.grid.compact input[type=checkbox] { width: auto; }

.subtabs { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; padding: 8px 16px 0; }
.subtab { border-color: transparent; background: none; }
.subtab.active { border-color: var(--line); background: var(--surface); font-weight: 600; }
button.danger { color: var(--danger); }

/* Диалог */
dialog {
  border: 1px solid var(--line); border-radius: 10px; padding: 0;
  width: min(820px, calc(100% - 32px)); background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .35); }
#add-form, #action-form, #czho-form, #czho-attrs-form, .card-body { padding: 20px; display: grid; gap: 14px; }
dialog h2 { margin: 0; font-size: 18px; }
dialog.wide-dialog { width: min(1000px, calc(100% - 32px)); }
label.check { flex-direction: row; align-items: center; gap: 8px; align-self: end; padding-bottom: 6px; }
label.check input { width: auto; }
#card-fields { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
#card-fields dt { color: var(--muted); }
#card-fields dd { margin: 0; white-space: pre-line; }
.form-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }
.actions { display: flex; justify-content: flex-end; gap: 8px; }
.small-table { margin: 8px 0 0; max-height: 220px; }
.small-table .grid { min-width: 0; }

@media (max-width: 640px) {
  .form-grid { grid-template-columns: 1fr; }
  .span-2, .span-3 { grid-column: auto; }
  #card-fields { grid-template-columns: 1fr; }
  #card-fields dd { margin-bottom: 6px; }
}

/* Фото и видео */
.badge { display: inline-block; margin-top: 4px; font-size: 11px; color: var(--muted); white-space: nowrap; }
#card-media h3 { margin: 4px 0 8px; font-size: 15px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.thumb { position: relative; margin: 0; display: grid; gap: 4px; }
.thumb-open {
  position: relative; padding: 0; border: 1px solid var(--line); border-radius: 6px; overflow: hidden;
  aspect-ratio: 4 / 3; background: var(--bg);
}
.thumb-open img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .play {
  position: absolute; left: 6px; bottom: 6px; padding: 2px 6px; border-radius: 4px;
  background: rgb(0 0 0 / .65); color: #fff; font-size: 12px;
}
.thumb figcaption { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thumb-del {
  position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; padding: 0; border-radius: 50%;
  background: rgb(0 0 0 / .6); color: #fff; border: 0; font-size: 16px; line-height: 1;
}
.upload-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 10px; }
.file-btn {
  flex-direction: row; display: inline-flex; cursor: pointer; padding: 6px 12px;
  border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--text); font-size: 14px;
}
.upload-status { margin: 6px 0 0; color: var(--accent); }
#add-files-label input[type=file] { padding: 4px; }
.media-hint { font-size: 12px !important; color: var(--muted) !important; }

#viewer { width: auto; max-width: calc(100% - 32px); padding: 0; background: #000; border: 0; }
#viewer .viewer-body { display: grid; place-items: center; }
#viewer .viewer-body img, #viewer .viewer-body video { max-width: calc(100vw - 32px); max-height: calc(100vh - 110px); display: block; }
#viewer .viewer-bar { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 12px; background: var(--surface); }
#sso-info p { margin: 0 0 8px; }

/* ЦЖО */
#czho > section { flex: 1; min-height: 0; display: flex; flex-direction: column; }
section.scroll { overflow: auto; display: block !important; }
section.scroll[hidden] { display: none !important; }
.pad { padding: 0 16px; }
.pad-top { padding-top: 8px; }
h3 { margin: 4px 0 8px; font-size: 15px; }
h3 .link { font-weight: 400; font-size: 13px; padding: 0 6px; }
.fields { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
.fields dt { color: var(--muted); }
.fields dd { margin: 0; white-space: pre-line; overflow-wrap: anywhere; }
.fields dd .link { padding: 0; text-align: left; white-space: normal; }
.grid tr.sub td:first-child { padding-left: 18px; color: var(--muted); }
#czho-grid tbody tr[data-id]:hover td, #czho-hist tbody tr[data-id]:hover td { filter: brightness(.97); }

.eq-card { margin: 0 16px 16px; display: grid; gap: 16px; }
.eq-card > section, .eq-cols > section {
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px;
}
.eq-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.eq-head h2 { margin: 0; font-size: 20px; }
.eq-head .state { min-width: 180px; }
.eq-cols { display: grid; grid-template-columns: 3fr 2fr; gap: 16px; }
.eq-cols .toolbar { margin-top: 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips:empty { display: none; }
.chip { border-radius: 999px; padding: 3px 12px; font-size: 13px; }
.chip.tag { border-style: dashed; }
#czho-units .grid { min-width: 0; width: 100%; }
#czho-units tbody tr { cursor: pointer; }
.inline-filters { display: flex; align-items: center; gap: 8px 16px; flex-wrap: wrap; margin-bottom: 8px; }
.inline-filters label.check { align-self: center; padding: 0; font-size: 13px; color: var(--text); }
label.inline { flex-direction: row; align-items: center; gap: 6px; font-size: 13px; }
label.inline select { width: auto; min-width: 150px; }
label.check.small { font-size: 12px; align-self: center; padding: 0; }
.table-wrap.flat { margin: 0; overflow-x: auto; }
.table-wrap.flat .grid { min-width: 800px; }
.table-wrap.flat .grid th { position: static; }

.search-box { position: relative; }
.found {
  position: absolute; top: 100%; left: 0; z-index: 5; min-width: 100%; width: max-content; max-width: min(640px, 90vw);
  max-height: 320px; overflow: auto; display: grid; padding: 4px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 6px 18px rgb(0 0 0 / .18);
}
.found button { border: 0; text-align: left; border-radius: 4px; font-size: 13px; }
.found button:hover { background: var(--bg); }
.found span { padding: 6px 12px; }

.files { display: grid; gap: 10px; }
.docs { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.docs li { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.docs a { color: var(--accent); overflow-wrap: anywhere; }
.path-line { margin: 0; font-weight: 600; }
.linked { display: grid; gap: 2px; margin-bottom: 8px; }
.linked-item { text-align: left; padding: 2px 0; white-space: normal; }
#card-czho h3 { margin-top: 4px; }

.dicts { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px; padding: 0 16px 16px; align-items: start; }
.dicts .grid.compact { min-width: 0; }
.dicts .dict-wide { grid-column: 1 / -1; }
.dicts .dict-wide td:not(:first-child), .dicts .dict-wide th:not(:first-child) { text-align: center; }
.dicts .grid td { vertical-align: middle; }

@media (max-width: 640px) {
  .eq-cols { grid-template-columns: 1fr; }
  .fields { grid-template-columns: 1fr; }
  .fields dd { margin-bottom: 6px; }
}
.dicts .grid.compact input:not([type=checkbox]) { min-width: 0; }
.dicts .grid th { white-space: normal; font-size: 13px; }
#users-grid select { min-width: 150px; }
.eq-card { grid-template-columns: minmax(0, 1fr); }
.eq-cols > section { min-width: 0; }

/* Дерево оборудования */
.tree-bar { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 16px 8px; }
.tree-bar input { width: min(320px, 100%); }
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree { margin: 0 16px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 6px; }
.tree ul { padding-left: 22px; }
.tree-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 1px 4px; border-radius: 4px; }
.tree-row:hover { background: var(--bg); }
.tree-toggle { border: 0; background: none; padding: 2px 4px; display: flex; align-items: center; gap: 6px; text-align: left; }
.tree-toggle:disabled { opacity: 1; cursor: default; }
.tree-toggle .arrow { width: 12px; color: var(--muted); }
.tree-row .muted { margin-right: auto; }
.tree-row .link { padding: 2px 6px; font-size: 13px; }
.tree-empty { padding: 16px; }

/* Фильтры в заголовках таблиц */
.filter-bar { display: flex; align-items: center; gap: 8px 16px; flex-wrap: wrap; padding: 8px 16px; min-height: 46px; }
.filter-bar .period { display: flex; gap: 8px; }
.filter-bar .hint { margin-left: auto; align-self: center; }
.filter-bar .link { padding: 4px 8px; }
.grid.filterable thead { position: sticky; top: 0; z-index: 3; }
.grid.filterable th { position: static; }
.grid.filterable .filter-row th { padding: 3px 4px; font-weight: 400; vertical-align: middle; white-space: normal; }
.grid.filterable th.wide { min-width: 220px; }
.filter-row input, .filter-row select, .pop-btn { width: 100%; min-width: 0; padding: 3px 5px; font-size: 12px; }
.pop-btn { display: block; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; contain: inline-size; }
.pop-btn::after { content: " ▾"; color: var(--muted); }
.filter-row th.active { background: var(--filter-on); }
.filter-row th.active input, .filter-row th.active select, .filter-row th.active .pop-btn {
  border-color: var(--accent); background: var(--surface); font-weight: 600;
}
.grid.filterable .c-id { min-width: 64px; }
.grid.filterable .c-date { min-width: 116px; }
.grid.filterable .c-unit { min-width: 84px; }
.grid.filterable .c-eq { min-width: 130px; }
.grid.filterable .c-author { min-width: 110px; }
.grid.filterable .c-term, .grid.filterable .c-due, .grid.filterable .c-type { min-width: 130px; }
.grid.filterable .c-prio, .grid.filterable .c-status, .grid.filterable .c-dept { min-width: 120px; }
.grid.filterable .c-done, .grid.filterable .c-tag { min-width: 96px; }
.grid.filterable { min-width: 1280px; }
.popover {
  position: fixed; z-index: 20; display: grid; gap: 8px; min-width: 220px; max-width: calc(100vw - 16px); padding: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 18px rgb(0 0 0 / .18);
}

/* Действия в карточке дефекта */
#card-actions { margin: -4px 0; }
#card-actions-note { margin: -6px 0 0; }

/* Строка «применены фильтры» и сортировка над таблицами */
.filter-status {
  display: flex; align-items: center; flex-wrap: wrap; gap: 4px 8px; padding: 4px 6px 4px 12px;
  background: var(--warn-bg); border: 1px solid var(--warn-line); border-radius: 6px; font-size: 13px;
}
.filter-status[hidden], .sort-status[hidden] { display: none; }
.filter-status .fs-reset {
  padding: 3px 10px; font-weight: 600; background: var(--accent); color: var(--accent-text); border-color: var(--accent);
}
.sort-status { display: flex; align-items: center; gap: 4px; color: var(--muted); }
.sort-status b { color: var(--text); font-weight: 600; }
.sort-status .link { padding: 0 4px; }
.popover input.invalid, .filter-row input.invalid { border-color: var(--err); outline-color: var(--err); }
.popover p { margin: 0; max-width: 260px; }
#logs-grid.grid.filterable { min-width: 900px; }

/* Администрирование: база данных */
#backup { max-width: 900px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
#backup-compare td.differs { font-weight: 600; }
.warnings { margin: 8px 0; padding-left: 20px; color: var(--err); }
.warnings:empty { display: none; }
#backup-compare td:first-child { text-align: left; }

/* ===== Этап 1: доступность и читаемость ===== */

/* Видимый фокус для работы с клавиатуры (мышью обводка не появляется) */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline-offset: 0; }
.grid tr:focus-visible, .grid th:focus-visible { outline-offset: -2px; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* Иконки из спрайта */
.icon { width: 1.1em; height: 1.1em; flex: none; vertical-align: -0.18em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.with-icon { display: inline-flex; align-items: center; gap: 4px; }
button .icon { vertical-align: -0.2em; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; padding: 6px 8px; }

/* Состояние строки словом и значком, а не только цветом */
.state-badge { display: flex; margin-top: 4px; font-size: 11px; font-weight: 600; white-space: nowrap; justify-content: center; }
.state-badge.ok { color: var(--ok); }
.state-badge.warn { color: var(--warn-text); }
.state-badge.bad { color: var(--err); }
.badge.with-icon { display: flex; justify-content: center; }

@media (min-width: 641px) {
  /* Залипающие столбцы: № и оборудование остаются на месте при горизонтальной прокрутке */
  .grid.sticky-cols { --w1: 96px; }
  .sticky-cols th:nth-child(1), .sticky-cols td:nth-child(1),
  .sticky-cols th:nth-child(4), .sticky-cols td:nth-child(4) { position: sticky; z-index: 1; }
  .sticky-cols th:nth-child(1), .sticky-cols td:nth-child(1) { left: 0; width: var(--w1); min-width: var(--w1); max-width: var(--w1); }
  .sticky-cols th:nth-child(4), .sticky-cols td:nth-child(4) { left: var(--w1); }
  .sticky-cols td:nth-child(1), .sticky-cols td:nth-child(4) { background: var(--surface); }
  .sticky-cols th:nth-child(1), .sticky-cols th:nth-child(4) { z-index: 2; }
  /* Граница сдвигающейся ячейки (у схлопнутых границ таблицы она остаётся на месте) */
  .sticky-cols th:nth-child(4), .sticky-cols td:nth-child(4) { box-shadow: inset -1px 0 0 var(--line), 2px 0 3px -2px rgb(0 0 0 / .25); }
}

/* Уведомления */
#toasts {
  position: fixed; inset: auto auto 20px 50%; transform: translateX(-50%); z-index: 100; margin: 0; padding: 0;
  border: 0; background: none; overflow: visible; color: inherit;
  display: grid; gap: 8px; width: min(560px, calc(100% - 32px)); height: auto; pointer-events: none;
}
#toasts:not(:popover-open) { display: none; } /* без поддержки popover остаётся обычным блоком */
@supports not selector(:popover-open) { #toasts:not(:popover-open) { display: grid; } }
.toast {
  pointer-events: auto; display: flex; align-items: flex-start; gap: 10px; padding: 10px 8px 10px 14px;
  background: var(--surface); color: var(--text); border: 1px solid var(--line); border-left: 5px solid var(--accent);
  border-radius: 8px; box-shadow: 0 6px 20px rgb(0 0 0 / .25); animation: toast-in .18s ease-out;
}
.toast > .icon { margin-top: 2px; width: 1.25em; height: 1.25em; }
.toast.ok { border-left-color: var(--ok); }
.toast.ok > .icon { color: var(--ok); }
.toast.error { border-left-color: var(--err); background: var(--row-overdue); }
.toast.error > .icon { color: var(--err); }
.toast.info > .icon { color: var(--accent); }
.toast-text { flex: 1; min-width: 0; overflow-wrap: anywhere; white-space: pre-line; }
.toast-close { border: 0; background: none; padding: 4px; line-height: 0; color: var(--muted); border-radius: 4px; }
.toast-close:hover { color: var(--text); background: var(--bg); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* Диалог подтверждения, ввода и конфликта версий */
#modal { width: min(520px, calc(100% - 32px)); }
#modal.wide-dialog { width: min(820px, calc(100% - 32px)); }
.modal-body { padding: 20px; display: grid; gap: 12px; }
#modal-message { margin: 0; white-space: pre-line; }
.modal-list { margin: 0; padding-left: 20px; display: grid; gap: 2px; }
#modal-table { max-height: 50vh; overflow: auto; border: 1px solid var(--line); border-radius: 6px; }
.grid.diff { min-width: 0; }
.grid.diff th, .grid.diff td { position: static; }
.grid.diff td:first-child { font-weight: 600; text-align: left; white-space: nowrap; }
.grid.diff td:nth-child(3) { background: var(--warn-bg); }
button.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { button.danger.solid { color: #1b0b0a; } }
#modal .actions { flex-wrap: wrap; }

/* Загрузка таблиц: полоса сверху и приглушённые прежние строки; задержка — чтобы быстрые ответы не мигали */
.table-wrap.loading > .grid tbody tr:not(.skeleton) { opacity: .5; transition: opacity .15s .2s; }
.table-wrap.loading::before {
  content: ""; display: block; position: sticky; top: 0; left: 0; z-index: 6; height: 3px; margin-bottom: -3px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent) 0 0 / 40% 100% no-repeat;
  animation: load-bar 1s linear infinite, load-show 0s .2s backwards;
}
@keyframes load-bar { from { background-position: -40% 0; } to { background-position: 140% 0; } }
@keyframes load-show { from { visibility: hidden; } to { visibility: visible; } }
.grid tr.skeleton td { height: 38px; }
.sk { display: block; height: 12px; border-radius: 6px; background: linear-gradient(90deg, var(--line), var(--bg), var(--line)) 0 0 / 200% 100%; animation: sk 1.2s linear infinite; }
@keyframes sk { to { background-position: -200% 0; } }
.grid tr.empty td button { margin-left: 8px; }
@media (prefers-reduced-motion: reduce) {
  .toast, .sk, .table-wrap.loading::before { animation: none; }
  .table-wrap.loading::before { background: var(--accent); opacity: .6; }
}

/* ===== Этап 3: навигация ===== */
.pager-page { display: inline-flex; align-items: center; gap: 4px; margin: 0 4px; }
.pager .page-input { width: 68px; text-align: center; padding: 5px 4px; }
.pager .page-size { width: auto; margin-left: 6px; }
.pager-info { margin-left: 6px; }
.pager .icon-btn { min-width: 34px; }

/* ===== Этап 4: формы ===== */
.due-hint { min-height: 1.2em; color: var(--muted) !important; }
.due-hint.ok { color: var(--ok) !important; }
.due-hint.warn { color: var(--warn-text) !important; }

.field { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); }
.field > label { display: block; }
.combo { position: relative; font-size: 14px; color: var(--text); }
.combo-list {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 30; margin: 2px 0 0; padding: 4px; list-style: none;
  max-height: 280px; overflow: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 6px;
  box-shadow: 0 6px 18px rgb(0 0 0 / .18);
}
.combo-item, .combo-empty { padding: 6px 10px; border-radius: 4px; cursor: pointer; overflow-wrap: anywhere; }
.combo-empty { cursor: default; color: var(--muted); }
.combo-item.active { background: var(--row-selected); }
.combo-item mark { background: var(--warn-bg); color: inherit; border-radius: 2px; padding: 0 1px; }
.search-box { position: relative; min-width: 220px; }
.path-preview { margin: -4px 0 0; font-size: 13px; color: var(--text); min-height: 1.4em; }
.path-preview b { font-weight: 600; }

/* Перетаскивание файлов */
.dropzone {
  display: grid; gap: 8px; padding: 12px; border: 2px dashed var(--line); border-radius: 8px; background: var(--bg);
  font-size: 14px; color: var(--text); transition: border-color .15s, background .15s;
}
.dropzone.drop-over, .drop-over { border-color: var(--accent); background: var(--filter-on); }
.drop-over { outline: 2px dashed var(--accent); outline-offset: -2px; border-radius: 8px; }
.drop-hint { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; color: var(--muted); }
.drop-hint .link { padding: 0; }
.drop-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.drop-list:empty { display: none; }
.drop-list li { display: flex; align-items: center; gap: 10px; padding: 4px 6px; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.drop-list li.bad { border-color: var(--err); }
.drop-thumb { width: 40px; height: 40px; flex: none; border-radius: 4px; object-fit: cover; background: var(--bg); display: grid; place-items: center; color: var(--muted); }
.drop-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drop-warn { margin: 0; }
progress { width: 160px; height: 8px; vertical-align: middle; margin-left: 8px; accent-color: var(--accent); }
.toast progress { width: 120px; align-self: center; margin: 0; }


/* ===== Этап 5: узкие экраны — карточки вместо широких таблиц ===== */
.m-toggle, .m-sort-row { display: none; }
@media (max-width: 640px) {
  /* Страница прокручивается целиком: вложенная область прокрутки на телефоне оставляла бы таблице узкую щель */
  #app { height: auto; min-height: 100vh; }
  .table-wrap:has(> .grid.filterable) { overflow: visible; flex: none; }
  .topbar { padding: 4px 8px; gap: 2px 8px; }
  .brand { border-right: 0; padding-right: 0; }
  .tabs { order: 3; width: 100%; flex-wrap: nowrap; overflow-x: auto; }
  .tab { padding: 8px 10px; }
  .user { font-size: 13px; }
  .filter-bar, .subtabs, .bottombar, .filters { padding-left: 8px; padding-right: 8px; }
  .table-wrap { margin: 0 8px; }
  .filter-bar .hint { display: none; }
  .m-toggle { display: inline-flex; align-items: center; gap: 6px; }

  .grid.filterable { display: block; width: 100%; min-width: 0 !important; border: 0; }
  .table-wrap.flat .grid.filterable { min-width: 0 !important; }
  .grid.filterable thead { display: block; position: static; }
  .grid.filterable thead tr:first-child { display: none; }
  /* Панель фильтров и сортировки — по кнопке */
  .grid.filterable .filter-row { display: none; }
  .table-wrap.filters-open .filter-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 8px; background: var(--bg); }
  .table-wrap.filters-open .m-sort-row { display: block; padding: 8px 8px 0; background: var(--bg); }
  .m-sort-row td { display: block; border: 0; text-align: left; padding: 0; }
  .m-sort-row label { flex-direction: row; align-items: center; gap: 8px; }
  .m-sort-row select { width: auto; flex: 1; }
  .grid.filterable .filter-row th {
    display: block; padding: 0; border: 0; text-align: left; background: none; white-space: normal;
    min-width: 0; width: auto; max-width: none;
  }
  .grid.filterable .filter-row th::before { content: attr(data-label); display: block; font-size: 12px; color: var(--muted); margin-bottom: 2px; }
  .grid.filterable .filter-row th[data-label=""]::before { display: none; }
  .grid.filterable .filter-row th.sel { display: none; }
  .grid.filterable .filter-row th.active { background: var(--filter-on); border-radius: 6px; padding: 4px; }
  .filter-row input, .filter-row select, .pop-btn { font-size: 14px; padding: 6px 8px; }

  .grid.filterable tbody { display: block; }
  .grid.filterable tbody tr {
    display: grid; gap: 2px; position: relative; padding: 10px 12px; border-bottom: 1px solid var(--line);
  }
  .grid.filterable tbody tr.repaired { background: var(--row-repaired); }
  .grid.filterable tbody tr.verified { background: var(--row-verified); }
  .grid.filterable tbody tr.overdue { background: var(--row-overdue); }
  .grid.filterable tbody tr.selected { background: var(--row-selected); }
  .grid.filterable tbody tr td {
    display: grid; grid-template-columns: 96px 1fr; gap: 8px; border: 0; padding: 1px 0;
    text-align: left; background: transparent;
  }
  .grid.filterable tbody tr td::before { content: attr(data-label); color: var(--muted); font-size: 12px; padding-top: 1px; }
  .grid.filterable tbody tr td:empty { display: none; }
  .grid.filterable tbody tr td:first-child { font-weight: 600; }
  .grid.filterable tbody tr td .state-badge, .grid.filterable tbody tr td .badge { grid-column: 2; justify-content: flex-start; margin-top: 0; }
  .grid.filterable tbody tr td.sel { position: absolute; top: 8px; right: 10px; display: block; width: auto; padding: 0; }
  .grid.filterable tbody tr td.sel::before { display: none; }
  .grid.filterable tbody tr.empty, .grid.filterable tbody tr.skeleton { display: block; }
  .grid.filterable tbody tr.empty td, .grid.filterable tbody tr.skeleton td { display: block; padding: 8px 0; text-align: center; }
  .grid.filterable tbody tr.empty td::before, .grid.filterable tbody tr.skeleton td::before { display: none; }
  .grid.filterable tbody tr.skeleton td:not(:first-child) { display: none; }
  .grid.filterable tbody tr.sub td:first-child { padding-left: 0; }

  .pager { gap: 4px; }
  .pager-info { flex-basis: 100%; margin: 0; }
}
