:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #1c2430;
  --muted: #64707f;
  --line: #dde2e9;
  --accent: #1f6f43;
  --accent-soft: #e8f3ec;
  --warn: #9a6400;
  --warn-soft: #fdf3e0;
  --error: #a32020;
  --error-soft: #fdeceb;
  --info-soft: #eef3fb;
  --radius: 8px;
}

* { box-sizing: border-box; }

/* Атрибут hidden должен побеждать любое собственное display у элемента. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.wrap { max-width: 1240px; margin: 0 auto; padding: 0 20px; }

.page-header {
  position: relative;
  background: linear-gradient(135deg, #23503a, #1f6f43);
  color: #fff;
  padding: 26px 0 22px;
}
.page-header h1 { margin: 0 0 6px; font-size: 24px; font-weight: 600; }
.page-header-content { min-height: 112px; padding-right: 140px; }
.page-header-bin {
  position: absolute;
  top: 26px;
  right: 20px;
  display: block;
  width: 96px;
  height: auto;
}
@media (max-width: 600px) {
  .page-header-content { min-height: 66px; padding-right: 88px; }
  .page-header-bin { width: 56px; }
}
.lead { margin: 0; max-width: 70ch; color: #d8e8de; font-size: 14px; }

.steps {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding-top: 14px;
  padding-bottom: 14px;
}
.step {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 15px 7px 7px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.step span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--bg);
  font-size: 12px;
  font-weight: 600;
}
.step:disabled { opacity: .5; cursor: not-allowed; }
.step.active { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.step.active span { background: var(--accent); color: #fff; }
.step.done span { background: var(--accent-soft); color: var(--accent); }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px 22px 24px;
  margin-bottom: 22px;
}
.panel h2 { margin: 0 0 8px; font-size: 19px; }
.panel h3 { margin: 22px 0 8px; font-size: 15px; }
.hint { color: var(--muted); font-size: 13.5px; margin: 0 0 14px; max-width: 92ch; }
code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }

.dropzone {
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  padding: 26px;
  text-align: center;
  transition: background .15s, border-color .15s;
}
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.dropzone p { margin: 6px 0; }

.button {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 9px 16px;
  font: inherit;
  cursor: pointer;
  text-align: center;
}
.button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.button:disabled { opacity: .5; cursor: not-allowed; }
.button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.button.primary:hover:not(:disabled) { background: #17552f; color: #fff; }
.button small { font-size: 12px; opacity: .8; font-weight: 400; }
.button.small { padding: 4px 10px; font-size: 13px; }

.file-list { margin-top: 18px; display: grid; gap: 10px; }
.file-card {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  background: #fcfdfd;
}
.file-card.error { border-color: var(--error); background: var(--error-soft); }
.file-card .grow { flex: 1; min-width: 0; }
.file-card .name { font-weight: 600; word-break: break-all; }
.file-card .meta { color: var(--muted); font-size: 13px; margin-top: 3px; }
.file-card select { max-width: 220px; }

.toolbar {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 12px;
  font-size: 13.5px;
}
.toolbar .spacer { flex: 1; }
.restart {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 16px 0 0;
}
.stats { color: var(--muted); font-size: 13px; }

select, input[type="search"], input[type="text"], input[type="number"], input[type="password"] {
  font: inherit;
  font-size: 13.5px;
  padding: 5px 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: inherit;
  max-width: 100%;
}
input[type="text"].formula { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; }
select.wide { min-width: 240px; max-width: 380px; }

/*
 * Прокрутка идёт внутри самой таблицы, а не всей страницей: только так
 * прилипающая шапка остаётся видна — прилипать ей больше не к чему.
 * Невысокая таблица до предела не дорастает и выглядит как прежде.
 */
.table-scroll {
  overflow: auto;
  max-height: min(70vh, 680px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
table.grid { border-collapse: collapse; width: 100%; font-size: 13.5px; }
table.grid th, table.grid td {
  border-bottom: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
}
table.grid th {
  background: #f8f9fb;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 1;
  white-space: nowrap;
  /* Собственная граница уезжает вместе со строками — рисуем её тенью. */
  box-shadow: inset 0 -1px 0 var(--line);
}
table.grid tbody tr:hover { background: #fbfcfd; }
table.grid.compact td, table.grid.compact th { padding: 5px 8px; }

/*
 * Таблицы сопоставления вмещаются по ширине: доли столбцов заданы, длинный
 * текст переносится, а поля ввода и списки не распирают ячейку под длину
 * самого длинного наименования.
 */
table.grid.fit { table-layout: fixed; }
/* Заголовкам числовых столбцов перенос нужен, самим числам — нет. */
table.grid.fit th, table.grid.fit th.num { white-space: normal; }
table.grid.fit td { overflow-wrap: anywhere; }
table.grid.fit td.num, table.grid.fit th.num { overflow-wrap: normal; }
table.grid.fit select,
table.grid.fit input { width: 100%; min-width: 0; max-width: 100%; }
table.grid.fit .manual-inputs input { width: calc(50% - 4px); }
/* Значок статуса длинный — в узком столбце ему нужен перенос. */
table.grid.fit .badge { white-space: normal; }

#norms-table th:nth-child(1), #norms-table td:nth-child(1) { width: 22%; }
#norms-table th:nth-child(2), #norms-table td:nth-child(2) { width: 13%; }
#norms-table th:nth-child(3), #norms-table td:nth-child(3) { width: 14%; }
#norms-table th:nth-child(4), #norms-table td:nth-child(4) { width: 21%; }
#norms-table th:nth-child(5), #norms-table td:nth-child(5) { width: 10%; }
#norms-table th:nth-child(6), #norms-table td:nth-child(6) { width: 10%; }
#norms-table th:nth-child(7), #norms-table td:nth-child(7) { width: 10%; }

/* Две категории делят оставшееся место; служебные столбцы не сжимаются. */
#registry-table { min-width: 1100px; }
#registry-table th, #registry-table td { padding: 8px; }
#registry-table th:nth-child(2), #registry-table td:nth-child(2) { width: 80px; }
#registry-table th:nth-child(3), #registry-table td:nth-child(3) { width: 98px; }
#registry-table th:nth-child(4), #registry-table td:nth-child(4) { width: 122px; }
#registry-table th:nth-child(5), #registry-table td:nth-child(5) { width: 92px; }
#registry-table th:nth-child(7), #registry-table td:nth-child(7) { width: 76px; }
#registry-table th:nth-child(8), #registry-table td:nth-child(8) { width: 164px; overflow-wrap: normal; }
#registry-table th:nth-child(9), #registry-table td:nth-child(9) { width: 76px; }
#registry-table th:nth-child(7) { white-space: nowrap; }
#registry-table .badge { max-width: 100%; overflow-wrap: normal; word-break: normal; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.row-attention { background: var(--warn-soft); }
tr.row-none { background: var(--error-soft); }
tr.row-manual td:first-child { box-shadow: inset 3px 0 0 var(--accent); }

.badge {
  display: inline-block;
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 12px;
  white-space: nowrap;
  background: var(--bg);
  color: var(--muted);
}
.badge.exact, .badge.auto { background: var(--accent-soft); color: var(--accent); }
.badge.weak { background: var(--warn-soft); color: var(--warn); }
.badge.none { background: var(--error-soft); color: var(--error); }
.badge.manual, .badge.formula { background: var(--info-soft); color: #2a4d80; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  background: #fcfdfd;
}
.card .value { font-size: 21px; font-weight: 600; font-variant-numeric: tabular-nums; }
.card .label { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.card.error .value { color: var(--error); }
.card.warn .value { color: var(--warn); }
.card.ok .value { color: var(--accent); }

.issue {
  border-left: 3px solid var(--line);
  background: #fcfdfd;
  border-radius: 0 6px 6px 0;
  padding: 9px 12px;
  margin-bottom: 8px;
}
.issue.error { border-color: var(--error); background: var(--error-soft); }
.issue.warning { border-color: var(--warn); background: var(--warn-soft); }
.issue.info { border-color: #7593c2; background: var(--info-soft); }
.issue .title { font-weight: 600; font-size: 13.5px; }
.issue .detail { font-size: 13px; color: #3a4655; margin-top: 2px; }

.downloads { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 12px; }
.downloads .button { align-items: flex-start; text-align: left; padding: 14px 16px; }

.log { margin-top: 16px; font-size: 13px; color: var(--muted); }
.log div { padding: 2px 0; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(28, 36, 48, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.overlay-box {
  background: #fff;
  border-radius: var(--radius);
  padding: 22px 30px;
  text-align: center;
  min-width: 240px;
}
.spinner {
  width: 30px; height: 30px;
  margin: 0 auto 12px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toasts { position: fixed; right: 18px; bottom: 18px; display: grid; gap: 8px; z-index: 60; }
.toast {
  background: #232d3b;
  color: #fff;
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 13.5px;
  max-width: 380px;
  box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
.toast.error { background: var(--error); }
.toast.ok { background: var(--accent); }

.page-footer { padding: 10px 20px 30px; color: var(--muted); font-size: 13px; }
.page-footer a { color: var(--accent); }

.formula-row { display: grid; gap: 6px; }
.formula-row .preview { font-size: 12.5px; color: var(--muted); }
.formula-row .error-text { font-size: 12.5px; color: var(--error); }
.manual-inputs { display: flex; gap: 8px; flex-wrap: wrap; }
.manual-inputs input { width: 120px; }

@media (max-width: 700px) {
  .page-header h1 { font-size: 20px; }
  .panel { padding: 16px 14px 20px; }
  .wrap { padding: 0 12px; }
}

.ai-block {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}
.ai-block h3 { margin-top: 0; }
.ai-block .issue { margin-top: 12px; }
.ai-block details { margin-top: 12px; font-size: 13px; }
.ai-block summary { cursor: pointer; color: var(--muted); }
.ai-block ul.detail { margin: 6px 0 0; padding-left: 20px; }
pre.sample {
  background: var(--bg);
  border-radius: 6px;
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 12px;
  line-height: 1.45;
  max-height: 320px;
}
.ai-block li.loss { color: var(--error); }
.ai-block ul.detail ul { margin: 2px 0 4px; padding-left: 18px; color: var(--muted); }
.endpoint-settings { font-size: 13px; max-width: 100%; }
.endpoint-settings summary { cursor: pointer; color: var(--muted); }
.endpoint-settings .toolbar { margin: 8px 0 0; }
.endpoint-settings .hint { margin: 0; }
.endpoint-settings label { max-width: 100%; }
.endpoint-settings .button { white-space: normal; }
