/* =====================================================================
   Компоненты — стандарт 03. Подключается после tokens.css.
   Правила:
   • только var(--…) из tokens.css; без hex, без !important (кроме [hidden] и .sr-only);
   • mobile-first: базовые стили — для телефона, @media (min-width) — для больших экранов;
   • брейкпоинты только 640 / 768 / 1024 / 1280 (DS-30);
   • компонент не задаёт внешних отступов — раскладку делает контейнер через gap (DS-21);
   • каждый компонент описан в одном месте этого файла.
   ===================================================================== */

/* ---------- 0. Сброс и база ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100dvh;
  font: var(--fw-regular) var(--fs-base) / var(--lh) var(--font);
  color: var(--ink); background: var(--bg);
  -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
  overflow-x: clip;                       /* страница никогда не скроллится вбок (DS-31) */
}
h1, h2, h3, h4, p { margin: 0; }
h1 { font-size: var(--fs-xl); font-weight: var(--fw-bold); line-height: var(--lh-tight); letter-spacing: -0.01em; }
h2 { font-size: var(--fs-md); font-weight: var(--fw-semibold); line-height: var(--lh-tight); }
h3 { font-size: var(--fs-base); font-weight: var(--fw-semibold); }
a { color: var(--primary-ink); text-underline-offset: 2px; }
img, svg, video { max-width: 100%; display: block; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
::placeholder { color: var(--faint); }
* { scrollbar-width: thin; scrollbar-color: var(--line-strong) transparent; }
/* Иконки: Lucide (контурные, stroke 2). Размер задаёт контекст: 16 в кнопках, 18 в меню и алертах */
svg.ic { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* Фокус: видимый, контрастный, только с клавиатуры (DS-40) */
:focus { outline: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: var(--r-xs); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; scroll-behavior: auto !important; }
}

/* ---------- 1. Утилиты (единственный допустимый способ «подкрутить» место) ---------- */
.sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.muted { color: var(--muted); }
.ink-2 { color: var(--ink-2); }
.text-ok { color: var(--ok-ink); } .text-warn { color: var(--warn-ink); } .text-bad { color: var(--bad-ink); }
.xs { font-size: var(--fs-xs); } .sm { font-size: var(--fs-sm); }
.mono { font-family: var(--font-mono); font-size: 0.95em; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.clamp-2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.grow { flex: 1 1 auto; min-width: 0; }

/* Раскладка: строка и стопка с gap — вместо margin у детей (DS-21) */
.row { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; min-width: 0; }
.row.nowrap { flex-wrap: nowrap; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: var(--sp-4); min-width: 0; }
.stack.tight { gap: var(--sp-2); }
/* Дети стопки не сжимаются: иначе в прокручиваемом окне таблица получает свою вторую прокрутку */
.stack > *, .modal-b > * { flex-shrink: 0; }
.cluster { display: flex; flex-wrap: wrap; gap: var(--sp-2); }
.grid { display: grid; gap: var(--sp-4); grid-template-columns: minmax(0, 1fr); }
@media (min-width: 768px) {
  .grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid.aside { grid-template-columns: minmax(0, 1fr) 340px; }
}
.grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-3); }   /* плитки: 2 в ряд даже на телефоне */
@media (min-width: 1024px) { .grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--sp-4); } }


/* ---------- 2. Каркас приложения ---------- */
.app { min-height: 100dvh; }
.sidebar {
  position: fixed; inset: 0 auto 0 0; z-index: var(--z-drawer);
  width: min(var(--sidebar-w), 86vw);
  display: flex; flex-direction: column; gap: var(--sp-1);
  padding: var(--sp-5) var(--sp-3) calc(var(--sp-3) + env(safe-area-inset-bottom));
  background: var(--surface); border-right: 1px solid var(--line);
  transform: translateX(-100%); transition: transform var(--t) var(--ease);
  overflow-y: auto;
}
.app.menu-open .sidebar { transform: none; box-shadow: var(--sh-3); }
.scrim { position: fixed; inset: 0; z-index: calc(var(--z-drawer) - 1); background: var(--overlay); }
.app:not(.menu-open) .scrim { display: none; }

.brand { display: flex; align-items: center; gap: var(--sp-2); padding: 0 var(--sp-2) var(--sp-4); font-weight: var(--fw-bold); font-size: var(--fs-md); }
.brand-mark { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; color: var(--on-primary); background: linear-gradient(135deg, var(--focus), var(--primary-active)); flex: none; }
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-group + .nav-group { border-top: 1px solid var(--line-2); padding-top: var(--sp-2); margin-top: var(--sp-2); }
.nav-title { padding: var(--sp-2) var(--sp-3) var(--sp-1); font-size: var(--fs-xs); color: var(--muted); font-weight: var(--fw-semibold); }
.nav-link {
  display: flex; align-items: center; gap: var(--sp-3); min-height: var(--h); padding: 0 var(--sp-3);
  border-radius: var(--r-sm); color: var(--ink-2); text-decoration: none;
}
.nav-link:hover { background: var(--surface-3); color: var(--ink); }
.nav-link[aria-current="page"] { background: var(--primary-soft); color: var(--primary-ink); font-weight: var(--fw-semibold); }
.nav-link svg { width: 18px; height: 18px; flex: none; }
.nav-link .count { margin-left: auto; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: var(--sp-2); padding-top: var(--sp-3); border-top: 1px solid var(--line-2); }

.topbar {
  position: sticky; top: 0; z-index: var(--z-header);
  display: flex; align-items: center; gap: var(--sp-3);
  height: calc(var(--topbar-h) + env(safe-area-inset-top)); padding: env(safe-area-inset-top) var(--sp-4) 0;
  background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.main {
  min-width: 0;                           /* не даёт таблицам распирать каркас */
  max-width: var(--content-max); margin: 0 auto;
  padding: var(--page-pad) calc(var(--page-pad) + env(safe-area-inset-right)) calc(var(--sp-8) + env(safe-area-inset-bottom)) calc(var(--page-pad) + env(safe-area-inset-left));
}
@media (min-width: 1024px) {
  :root { --page-pad: var(--sp-6); }
  .app { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); }
  .sidebar { position: sticky; top: 0; height: 100dvh; transform: none; width: auto; transition: none; }
  .topbar, .scrim { display: none; }
  .main { width: 100%; }
}

/* Шапка страницы: заголовок слева, действия справа, переносится целиком */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--sp-3) var(--sp-4); margin-bottom: var(--sp-5); }
.page-head > :first-child { flex: 1 1 280px; min-width: 0; }
.page-head .sub { margin-top: var(--sp-1); color: var(--muted); font-size: var(--fs-sm); }
.page-head .actions { display: flex; flex-wrap: wrap; gap: var(--sp-2); }

/* ---------- 3. Кнопки ---------- */
.btn {
  --btn-bg: var(--surface); --btn-fg: var(--ink); --btn-bd: var(--line);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  height: var(--h); padding: 0 var(--sp-4); min-width: 0;
  border: 1px solid var(--btn-bd); border-radius: var(--r-sm);
  background: var(--btn-bg); color: var(--btn-fg);
  font-weight: var(--fw-medium); font-size: var(--fs-base); line-height: 1; white-space: nowrap;
  text-decoration: none; cursor: pointer; user-select: none;
  transition: background-color var(--t-fast), border-color var(--t-fast), color var(--t-fast), box-shadow var(--t-fast);
}
.btn:hover { background: var(--surface-2); border-color: var(--line-strong); }
.btn:active { transform: translateY(1px); }
.btn svg { width: 16px; height: 16px; flex: none; }
.btn-primary { --btn-bg: var(--primary); --btn-fg: var(--on-primary); --btn-bd: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-primary:active { background: var(--primary-active); }
.btn-soft { --btn-bg: var(--primary-soft); --btn-fg: var(--primary-ink); --btn-bd: transparent; }
.btn-soft:hover { background: color-mix(in srgb, var(--primary-soft) 70%, var(--primary) 12%); border-color: transparent; }
.btn-ghost { --btn-bg: transparent; --btn-bd: transparent; --btn-fg: var(--ink-2); }
.btn-ghost:hover { background: var(--surface-3); border-color: transparent; color: var(--ink); }
.btn-danger { --btn-bg: var(--bad); --btn-fg: var(--on-primary); --btn-bd: var(--bad); }
.btn-danger:hover { background: var(--bad-ink); border-color: var(--bad-ink); }
.btn-danger-soft { --btn-bg: var(--surface); --btn-fg: var(--bad-ink); --btn-bd: var(--line); }
.btn-danger-soft:hover { background: var(--bad-soft); border-color: var(--bad-soft); }
.btn-sm { height: var(--h-sm); padding: 0 var(--sp-3); font-size: var(--fs-sm); border-radius: 8px; }
.btn-lg { height: var(--h-lg); padding: 0 var(--sp-5); }
.btn-icon { width: var(--h); padding: 0; }
.btn-icon.btn-sm { width: var(--h-sm); }
.btn-block { width: 100%; }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; transform: none; }
/* Загрузка: текст остаётся на месте (ширина не прыгает), поверх — спиннер */
.btn[aria-busy="true"] { position: relative; color: transparent; pointer-events: none; }
.btn[aria-busy="true"]::after {
  content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--btn-fg); border-right-color: transparent; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
/* На сенсорных экранах — цель не меньше 44px (DS-33) */
@media (pointer: coarse) { .btn { min-height: var(--h-lg); } .btn-sm { min-height: 40px; } .btn-icon { min-width: var(--h-lg); } }

/* ---------- 4. Поля ---------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.label { font-size: var(--fs-sm); font-weight: var(--fw-medium); color: var(--ink-2); }
.label .req { color: var(--bad); margin-left: 2px; }
.hint { font-size: var(--fs-xs); color: var(--muted); }
.error-text { font-size: var(--fs-xs); color: var(--bad-ink); display: flex; gap: var(--sp-1); align-items: center; }
.input, .select, .textarea {
  width: 100%; min-width: 0; height: var(--h); padding: 0 var(--sp-3);
  border: 1px solid var(--line); border-radius: var(--r-sm); background: var(--surface); color: var(--ink);
  font-size: var(--fs-md);                 /* 16px на телефоне: iOS не зумит страницу (DS-34) */
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
}
@media (min-width: 640px) { .input, .select, .textarea { font-size: var(--fs-base); } }
.textarea { height: auto; min-height: 88px; padding: var(--sp-2) var(--sp-3); resize: vertical; line-height: var(--lh); }
.select { appearance: none; padding-right: 36px; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 18px) 55%, calc(100% - 13px) 55%; background-size: 5px 5px; background-repeat: no-repeat; }
.input:hover, .select:hover, .textarea:hover { border-color: var(--line-strong); }
.input:focus-visible, .select:focus-visible, .textarea:focus-visible,
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--focus); box-shadow: var(--ring); }
.input:disabled, .select:disabled, .textarea:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }
.input[aria-invalid="true"], .select[aria-invalid="true"], .textarea[aria-invalid="true"] { border-color: var(--bad); }
.input[aria-invalid="true"]:focus { box-shadow: 0 0 0 3px var(--bad-soft); }
.input.num { text-align: right; }
input[type="date"].input, input[type="month"].input, input[type="time"].input { appearance: none; -webkit-appearance: none; display: block; text-align: left; }
/* Группа: префикс/суффикс (₽, %, шт) */
.input-group { display: flex; align-items: stretch; min-width: 0; border: 1px solid var(--line); border-radius: var(--r-sm); background: var(--surface); overflow: hidden; }
.input-group:focus-within { border-color: var(--focus); box-shadow: var(--ring); }
.input-group .input { border: 0; border-radius: 0; box-shadow: none; }
.input-group .addon { display: grid; place-items: center; padding: 0 var(--sp-3); background: var(--surface-2); color: var(--muted); font-size: var(--fs-sm); white-space: nowrap; }

/* Чекбокс, радио, переключатель */
.check { display: inline-flex; align-items: flex-start; gap: var(--sp-2); cursor: pointer; line-height: 1.4; }
.check input { width: 18px; height: 18px; margin: 1px 0 0; accent-color: var(--primary); flex: none; }
.switch { position: relative; display: inline-flex; align-items: center; gap: var(--sp-3); cursor: pointer; }
.switch input { appearance: none; -webkit-appearance: none; width: 38px; height: 22px; margin: 0; border-radius: var(--r-full); background: var(--line-strong); position: relative; cursor: pointer; transition: background var(--t); flex: none; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: var(--knob); box-shadow: var(--sh-1); transition: transform var(--t) var(--ease); }
.switch input:checked { background: var(--primary); }
.switch input:checked::after { transform: translateX(16px); }
.switch input:disabled { opacity: 0.5; cursor: not-allowed; }

/* Форма: одна колонка на телефоне, сетка — с 640px */
.form { display: grid; gap: var(--sp-4); grid-template-columns: minmax(0, 1fr); }
@media (min-width: 640px) {
  .form.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form .span-2 { grid-column: 1 / -1; }
}
.form-actions { display: flex; flex-wrap: wrap-reverse; justify-content: flex-end; gap: var(--sp-2); }
@media (max-width: 639.98px) { .form-actions .btn { flex: 1 1 auto; } }

/* ---------- 5. Карточка, плитки ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--sh-1); min-width: 0; }
.card-h { display: flex; align-items: center; flex-wrap: wrap; gap: var(--sp-2) var(--sp-3); padding: var(--sp-4) var(--sp-4); border-bottom: 1px solid var(--line-2); }
.card-h > h2 { flex: 1 1 auto; min-width: 0; }
.card-b { padding: var(--sp-4); }
.card-f { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--sp-2); padding: var(--sp-3) var(--sp-4); background: var(--surface-2); border-top: 1px solid var(--line-2); border-radius: 0 0 var(--r) var(--r); }
.card > .table-wrap { border-radius: 0 0 var(--r) var(--r); }
@media (min-width: 768px) { .card-h { padding: var(--sp-4) var(--sp-5); } .card-b { padding: var(--sp-5); } .card-f { padding: var(--sp-3) var(--sp-5); } }

.stat { display: flex; flex-direction: column; gap: var(--sp-1); padding: var(--sp-4); }
.stat .label { color: var(--muted); font-weight: var(--fw-medium); font-size: var(--fs-sm); }
.stat .value { font-size: clamp(20px, 5.4vw, var(--fs-2xl)); font-weight: var(--fw-bold); line-height: var(--lh-tight); letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.stat .delta { font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }

/* ---------- 6. Бейджи, теги, точки ---------- */
.tag {
  --tag-bg: var(--surface-3); --tag-fg: var(--ink-2);
  display: inline-flex; align-items: center; gap: 6px; height: 22px; padding: 0 var(--sp-2);
  border-radius: var(--r-full); background: var(--tag-bg); color: var(--tag-fg);
  font-size: var(--fs-xs); font-weight: var(--fw-semibold); line-height: 1; white-space: nowrap;
}
.tag.ok { --tag-bg: var(--ok-soft); --tag-fg: var(--ok-ink); }
.tag.warn { --tag-bg: var(--warn-soft); --tag-fg: var(--warn-ink); }
.tag.bad { --tag-bg: var(--bad-soft); --tag-fg: var(--bad-ink); }
.tag.info { --tag-bg: var(--info-soft); --tag-fg: var(--info-ink); }
.tag.pri { --tag-bg: var(--primary-soft); --tag-fg: var(--primary-ink); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none; }
.mp { display: inline-flex; align-items: center; height: 20px; padding: 0 6px; border-radius: var(--r-xs); font-size: 11px; font-weight: var(--fw-bold); letter-spacing: 0.02em; }
.mp.wb { background: var(--wb-soft); color: var(--wb); }
.mp.oz { background: var(--oz-soft); color: var(--oz); }
.mp.ym { background: var(--ym-soft); color: var(--ym); }
.count { min-width: 20px; height: 20px; padding: 0 6px; border-radius: var(--r-full); background: var(--primary); color: var(--on-primary); font-size: 11px; font-weight: var(--fw-bold); display: inline-grid; place-items: center; }

/* ---------- 7. Алерты ---------- */
.alert {
  --al-bg: var(--info-soft); --al-fg: var(--info-ink); --al-ic: var(--info);
  display: flex; flex-wrap: wrap; align-items: flex-start; gap: var(--sp-2) var(--sp-3); padding: var(--sp-3) var(--sp-4);
  border-radius: var(--r); background: var(--al-bg); color: var(--al-fg); font-size: var(--fs-sm); line-height: var(--lh);
}
.alert > svg { width: 18px; height: 18px; flex: none; color: var(--al-ic); margin-top: 1px; }
.alert > .grow { flex: 1 1 220px; }          /* кнопка алерта уходит вниз, если не помещается */
.alert > .grow > b { display: block; font-weight: var(--fw-semibold); }
.alert.ok { --al-bg: var(--ok-soft); --al-fg: var(--ok-ink); --al-ic: var(--ok); }
.alert.warn { --al-bg: var(--warn-soft); --al-fg: var(--warn-ink); --al-ic: var(--warn); }
.alert.bad { --al-bg: var(--bad-soft); --al-fg: var(--bad-ink); --al-ic: var(--bad); }

/* ---------- 8. Вкладки (сегментный контрол) ---------- */
.tabs { display: flex; gap: 4px; padding: 4px; background: var(--surface-3); border-radius: 12px; max-width: 100%; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { flex: none; height: 32px; padding: 0 var(--sp-3); border: 0; border-radius: 9px; background: transparent; color: var(--ink-2); font-weight: var(--fw-medium); cursor: pointer; white-space: nowrap; }
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--sh-1); font-weight: var(--fw-semibold); }

/* Сегментный выбор одного значения (уровень доступа «Нет / Просмотр / Изменение»).
   Разметка: <div class="seg" role="radiogroup"><label><input type="radio" name="x"><span>Нет</span></label>…</div> */
.seg { display: inline-flex; gap: 2px; padding: 3px; background: var(--surface-3); border-radius: 10px; max-width: 100%; }
.seg label { position: relative; flex: 1 1 auto; }
.seg input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.seg span { display: grid; place-items: center; height: 28px; padding: 0 var(--sp-3); border-radius: 8px; font-size: var(--fs-sm); font-weight: var(--fw-medium); color: var(--ink-2); white-space: nowrap; }
.seg input:checked + span { background: var(--surface); color: var(--ink); box-shadow: var(--sh-1); font-weight: var(--fw-semibold); }
.seg input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 1px; }
.seg input:disabled + span { opacity: 0.5; }
.seg.level input[value="none"]:checked + span { color: var(--muted); }
.seg.level input[value="edit"]:checked + span { color: var(--primary-ink); }

/* ---------- 9. ТАБЛИЦЫ ---------- */
/* Обёртка обязательна: скролл по горизонтали внутри, шапка закреплена (DS-50) */
.table-wrap { position: relative; overflow: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
/* Тени у краёв, пока есть что прокручивать: подсказка, что таблица шире экрана */
.table-wrap {
  background:
    linear-gradient(to right, var(--surface) 30%, transparent) left / 32px 100% no-repeat local,
    linear-gradient(to left, var(--surface) 30%, transparent) right / 32px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, color-mix(in srgb, var(--ink) 14%, transparent), transparent) left / 12px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, color-mix(in srgb, var(--ink) 14%, transparent), transparent) right / 12px 100% no-repeat scroll;
  background-color: var(--surface);
}
.table-wrap.fill { max-height: calc(100dvh - var(--table-offset, 220px)); }  /* таблица на весь экран: скролл одной области */
.table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--fs-base); }
.table th, .table td { padding: 10px var(--sp-3); text-align: left; vertical-align: middle; border-bottom: 1px solid var(--line-2); }
.table th {
  position: sticky; top: 0; z-index: var(--z-sticky);
  background: var(--surface-2); color: var(--muted);
  font-size: var(--fs-xs); font-weight: var(--fw-semibold); white-space: nowrap;
  border-bottom-color: var(--line);
}
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: var(--surface-2); }
.table tr.is-selected td { background: var(--primary-soft); }
.table tr[data-href], .table tr.clickable { cursor: pointer; }
.table td { color: var(--ink-2); }
.table td.primary-cell { color: var(--ink); font-weight: var(--fw-medium); }
/* Числа: правый край + моноширинные цифры. Специфичность выше любых правил выравнивания (DS-52) */
.table th.num, .table td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table td.center, .table th.center { text-align: center; }
/* Текстовые колонки: ограничены и переносятся/обрезаются средствами CSS, не срезом строки на сервере (DS-53) */
.table .col-text { white-space: normal; overflow-wrap: anywhere; }
.table .col-text > .clip { display: block; min-width: 12rem; max-width: 24rem; }     /* <td class="col-text"><span class="clip">…</span></td> */
.table .col-trunc > .clip { display: block; max-width: 18rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.table .col-thumb { width: 56px; }
.table .col-thumb img { width: 40px; height: 40px; object-fit: cover; border-radius: var(--r-xs); background: var(--surface-3); }
/* Ячейка «в две строки»: название + артикул.
   Ширину ограничиваем у ВНУТРЕННЕГО блока: max-width у <td> в авто-раскладке браузеры игнорируют */
.cell-2 { display: flex; flex-direction: column; gap: 2px; min-width: 0; max-width: var(--cell-max, 18rem); }
.cell-2 > :last-child { font-size: var(--fs-xs); color: var(--muted); }
/* Действия в строке — всегда последняя колонка, прижаты вправо */
.table td.actions, .table th.actions { text-align: right; white-space: nowrap; width: 1%; }
.table td.actions .row { justify-content: flex-end; flex-wrap: nowrap; gap: var(--sp-1); }
/* Итоговая строка */
.table tfoot td { position: sticky; bottom: 0; z-index: var(--z-sticky); background: var(--surface-2); color: var(--ink); font-weight: var(--fw-semibold); border-top: 1px solid var(--line); border-bottom: 0; }
/* Закреплённый первый столбец — только с 768px; ширина задаётся переменной, не магией (DS-54) */
@media (min-width: 768px) {
  .table.freeze-1 th:first-child, .table.freeze-1 td:first-child {
    position: sticky; left: 0; z-index: calc(var(--z-sticky) - 1); background: var(--surface);
    box-shadow: inset -1px 0 0 var(--line);
  }
  .table.freeze-1 th:first-child { z-index: calc(var(--z-sticky) + 1); background: var(--surface-2); }
  .table.freeze-1 tbody tr:hover td:first-child { background: var(--surface-2); }
  .table.freeze-1 tbody tr.is-selected td:first-child { background: var(--primary-soft); }
}
.table.dense th, .table.dense td { padding-top: 6px; padding-bottom: 6px; }
/* Строка фильтров под шапкой */
.table tr.filters th { top: var(--thead-h, 37px); padding: 6px var(--sp-2); background: var(--surface); font-weight: var(--fw-regular); }
.table tr.filters .input, .table tr.filters .select { height: var(--h-sm); font-size: var(--fs-sm); }
/* Сортировка */
.th-sort { display: inline-flex; align-items: center; gap: 4px; padding: 0; border: 0; background: none; color: inherit; font: inherit; cursor: pointer; }
.th-sort::after { content: "↕"; opacity: 0.35; font-size: 11px; }
th[aria-sort="ascending"] .th-sort::after { content: "↑"; opacity: 1; color: var(--primary); }
th[aria-sort="descending"] .th-sort::after { content: "↓"; opacity: 1; color: var(--primary); }

/* Мобильный режим: строки превращаются в карточки (DS-55).
   В разметке у каждой <td> — data-label="Название колонки". */
@media (max-width: 767.98px) {
  .table.stack-mobile thead { display: none; }
  .table.stack-mobile, .table.stack-mobile tbody, .table.stack-mobile tr, .table.stack-mobile td { display: block; width: 100%; }
  .table.stack-mobile tr { display: flex; flex-direction: column; padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--line-2); }
  .table.stack-mobile td.primary-cell { order: -1; }   /* заголовок карточки — всегда сверху, где бы ни стояла колонка */
  .table.stack-mobile tbody tr:hover td { background: none; }
  .table.stack-mobile td { display: flex; justify-content: space-between; align-items: baseline; gap: var(--sp-3); padding: 3px 0; border: 0; text-align: right; max-width: none; min-width: 0; }
  .table.stack-mobile td:empty { display: none; }
  .table.stack-mobile td::before { content: attr(data-label); color: var(--muted); font-size: var(--fs-sm); text-align: left; flex: none; max-width: 45%; }
  .table.stack-mobile td.primary-cell { display: block; text-align: left; font-size: var(--fs-md); padding-bottom: var(--sp-1); }
  .table.stack-mobile td.primary-cell::before, .table.stack-mobile td.actions::before { content: none; }
  .table.stack-mobile td.actions { width: 100%; justify-content: flex-end; padding-top: var(--sp-2); }
  .table.stack-mobile tfoot { display: block; }
  .table.stack-mobile .cell-2 { max-width: none; }
  .table.stack-mobile .primary-cell .truncate { white-space: normal; }   /* на телефоне название переносится, а не режется */
  .table.stack-mobile .col-text > .clip { min-width: 0; max-width: none; }
}

/* Пагинация / «Показать ещё» */
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--sp-2); padding: var(--sp-3) var(--sp-4); border-top: 1px solid var(--line-2); font-size: var(--fs-sm); color: var(--muted); }

/* ---------- 10. Пустые состояния, загрузка ---------- */
.empty { display: flex; flex-direction: column; align-items: center; gap: var(--sp-2); padding: var(--sp-10) var(--sp-4); text-align: center; color: var(--muted); }
.empty > svg { width: 40px; height: 40px; color: var(--faint); }
.empty h3 { color: var(--ink); }
.empty p { max-width: 42ch; font-size: var(--fs-sm); }
.skeleton { display: block; height: 12px; border-radius: var(--r-full); background: linear-gradient(90deg, var(--surface-3) 25%, var(--surface-2) 50%, var(--surface-3) 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite linear; }
@keyframes shimmer { to { background-position: -200% 0; } }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--line-strong); border-right-color: var(--primary); animation: spin 0.7s linear infinite; display: inline-block; }
.loading { display: flex; align-items: center; justify-content: center; gap: var(--sp-2); padding: var(--sp-10); color: var(--muted); font-size: var(--fs-sm); }

/* ---------- 11. Модальное окно: нативный <dialog> + showModal() (DS-60) ---------- */
dialog.modal {
  width: min(520px, 100%); max-width: 100%; max-height: 92dvh; padding: 0; margin: auto auto 0;
  border: 0; border-radius: var(--r-lg) var(--r-lg) 0 0; background: var(--surface); color: var(--ink); box-shadow: var(--sh-3);
  display: flex; flex-direction: column; overflow: hidden;
}
dialog.modal:not([open]) { display: none; }
dialog.modal::backdrop { background: var(--overlay); }
dialog.modal.wide { width: min(720px, 100%); }
dialog.modal.xl { width: min(1000px, 100%); }
.modal-h { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-4) var(--sp-5); border-bottom: 1px solid var(--line-2); }
.modal-h h2 { flex: 1 1 auto; font-size: var(--fs-lg); }
.modal-b { padding: var(--sp-5); overflow-y: auto; overscroll-behavior: contain; }
.modal-f { display: flex; flex-wrap: wrap-reverse; justify-content: flex-end; gap: var(--sp-2); padding: var(--sp-3) var(--sp-5) calc(var(--sp-3) + env(safe-area-inset-bottom)); background: var(--surface-2); border-top: 1px solid var(--line-2); }
.modal-f .left { margin-right: auto; }         /* «Удалить» / «Назад» — слева, отдельно от основных */
@media (max-width: 639.98px) { .modal-f .btn { flex: 1 1 auto; } }
@media (min-width: 640px) {
  dialog.modal { margin: auto; border-radius: var(--r-lg); max-height: 88dvh; }
  .modal-f { padding-bottom: var(--sp-3); }
}
body:has(dialog.modal[open]) { overflow: hidden; }

/* Выезжающая панель справа (карточка записи) */
dialog.drawer { margin: 0 0 0 auto; height: 100dvh; max-height: 100dvh; width: min(560px, 100%); border-radius: 0; }
dialog.drawer .modal-b { flex: 1 1 auto; }   /* подвал панели всегда прижат к низу */
@media (min-width: 640px) { dialog.drawer { border-radius: var(--r-lg) 0 0 var(--r-lg); } }

/* ---------- 12. Тосты (DS-61) ---------- */
.toasts {
  position: fixed; z-index: var(--z-toast); inset: calc(var(--sp-3) + env(safe-area-inset-top)) var(--sp-3) auto;
  display: flex; flex-direction: column; gap: var(--sp-2); pointer-events: none;
}
@media (min-width: 640px) { .toasts { inset: auto var(--sp-4) calc(var(--sp-4) + env(safe-area-inset-bottom)) auto; width: min(380px, calc(100vw - 2rem)); } }
.toast {
  pointer-events: auto; display: flex; align-items: flex-start; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4); border-radius: 12px; background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); box-shadow: var(--sh-2); font-size: var(--fs-sm); line-height: 1.45;
  animation: toast-in var(--t) var(--ease);
}
.toast > svg { width: 18px; height: 18px; flex: none; margin-top: 1px; }
.toast.ok > svg { color: var(--ok); } .toast.bad > svg { color: var(--bad); } .toast.warn > svg { color: var(--warn); } .toast.info > svg { color: var(--info); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- 13. Плавающая кнопка «+» на телефоне ---------- */
.fab { position: fixed; z-index: var(--z-fab); right: calc(var(--sp-4) + env(safe-area-inset-right)); bottom: calc(var(--sp-4) + env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 50%; box-shadow: var(--sh-3); }
@media (max-width: 767.98px) { body:has(.fab) .main { padding-bottom: calc(96px + env(safe-area-inset-bottom)); } }
@media (min-width: 768px) { .fab { display: none; } }

/* ---------- 14. Баннер состояния (нет связи, новая версия) ---------- */
.banner { position: fixed; inset: 0 0 auto; z-index: var(--z-banner); display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: var(--sp-2); padding: calc(var(--sp-2) + env(safe-area-inset-top)) var(--sp-4) var(--sp-2); background: var(--warn); color: var(--on-warn); font-size: var(--fs-sm); font-weight: var(--fw-medium); text-align: center; }

/* ---------- 15. Прочее ---------- */
.kbd { display: inline-block; padding: 1px 6px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: var(--r-xs); font: var(--fw-medium) 11px var(--font-mono); color: var(--ink-2); background: var(--surface-2); }
.divider { height: 1px; background: var(--line-2); border: 0; margin: 0; }
.avatar { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary-ink); font-weight: var(--fw-semibold); font-size: var(--fs-sm); flex: none; }
.progress { height: 8px; border-radius: var(--r-full); background: var(--surface-3); overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--ok); border-radius: inherit; width: var(--value, 0%); transition: width var(--t) var(--ease); }

/* ---------- 17. Журналы: фильтры, «было → стало», ключ–значение, код ---------- */
.filters { display: grid; gap: var(--sp-3); grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); align-items: end; border-bottom: 1px solid var(--line-2); }
.filters .grow-2 { grid-column: span 2; }
@media (max-width: 639.98px) { .filters { grid-template-columns: repeat(2, minmax(0, 1fr)); } .filters .grow-2 { grid-column: 1 / -1; } }
.change { display: inline-flex; align-items: baseline; gap: 6px; white-space: nowrap; color: var(--muted); }
.change b, .table .num b { color: var(--ink); font-weight: var(--fw-semibold); }
.was { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--line-strong); }
.kv { display: grid; grid-template-columns: minmax(96px, max-content) minmax(0, 1fr); gap: var(--sp-2) var(--sp-4); margin: 0; font-size: var(--fs-sm); }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; color: var(--ink); min-width: 0; overflow-wrap: anywhere; }
.code { margin: 0; padding: var(--sp-3); border-radius: var(--r-sm); background: var(--surface-2); border: 1px solid var(--line); font: var(--fs-sm) / 1.5 var(--font-mono); color: var(--ink-2); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 320px; overflow: auto; }

/* ---------- 16. Показ по ширине — В КОНЦЕ файла, чтобы перебивать display компонентов ---------- */
@media (max-width: 767.98px) { .hide-mobile { display: none; } }
@media (min-width: 768px) { .hide-desktop { display: none; } }
