:root {
  --paper: #EEF1F4;
  --sheet: #FFFFFF;
  --ink: #1B2430;
  --muted: #5B6878;
  --form: #27477A;
  --form-soft: #E3EAF4;
  --rule: #C3CDD9;
  --ok: #2E7356;
  --diff: #A8322B;
  --diff-soft: #F8E7E5;
  --radius: 6px;
  font-family: "Golos Text", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--paper);
  line-height: 1.45;
}
* { box-sizing: border-box; }
body { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--form); outline-offset: 2px; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* Шапка */
.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 28px; background: var(--sheet); border-bottom: 1px solid var(--rule);
  flex-wrap: wrap;
}
.bar h1 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -0.01em; color: var(--form); }
.bar-right { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; }
.balance { display: flex; align-items: center; gap: 8px; font-variant-numeric: tabular-nums; }
.balance strong { font-size: 16px; }
.model { display: flex; align-items: center; gap: 8px; }
.model select { width: 320px; }

input, select, textarea {
  border: 1px solid var(--rule); border-radius: var(--radius); background: var(--sheet);
  padding: 7px 10px;
}
input:focus, select:focus, textarea:focus { border-color: var(--form); outline: none; box-shadow: 0 0 0 3px var(--form-soft); }

button { cursor: pointer; border-radius: var(--radius); border: 1px solid transparent; padding: 8px 14px; background: none; }
button:disabled { cursor: not-allowed; opacity: .5; }
.primary { background: var(--form); color: #fff; font-weight: 600; }
.primary:not(:disabled):hover { background: #1E3A66; }
.ghost { border-color: var(--rule); background: var(--sheet); }
.ghost:not(:disabled):hover { border-color: var(--form); color: var(--form); }
.icon { padding: 2px 8px; border-color: transparent; color: var(--muted); font-size: 15px; }
.icon:hover { color: var(--form); background: var(--form-soft); }
.link { padding: 0; color: var(--form); text-decoration: underline; }

main { max-width: 1180px; margin: 0 auto; padding: 28px; }
.notice { background: var(--form-soft); border-left: 3px solid var(--form); padding: 10px 14px; margin: 0 0 20px; }

/* Два листа акта лицом друг к другу */
.desk { display: grid; grid-template-columns: 1fr 56px 1fr; align-items: stretch; }
.sheet {
  background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius);
  display: flex; flex-direction: column; min-height: 220px; cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
}
.sheet-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 16px; border-bottom: 2px solid var(--form);
}
.side { font-weight: 600; color: var(--form); }
.sheet-body {
  flex: 1; display: grid; place-items: center; padding: 24px; text-align: center;
  /* разлиновка, как у бланка акта */
  background-image: repeating-linear-gradient(to bottom, transparent 0 31px, #E6EBF1 31px 32px);
  background-position: 0 8px;
}
.sheet-body p { margin: 4px 0; }
.big { font-size: 20px; font-weight: 600; }
.sheet:hover, .sheet.over { border-color: var(--form); }
.sheet.over { box-shadow: 0 0 0 4px var(--form-soft); }
.sheet.loaded .sheet-body { background-image: none; background: #F5F9F6; }
.sheet.loaded .sheet-head { border-bottom-color: var(--ok); }
.sheet.loaded .side { color: var(--ok); }
.fname { font-size: 17px; font-weight: 600; word-break: break-word; }

.seam { position: relative; display: grid; place-items: center; }
.seam::before {
  content: ""; position: absolute; top: 12px; bottom: 12px; left: 50%;
  border-left: 1px dashed var(--rule);
}
.swap {
  position: relative; width: 36px; height: 36px; padding: 0; border-radius: 50%;
  display: grid; place-items: center; background: var(--paper); border: 1px solid var(--rule);
  color: var(--form); font-size: 18px; transition: border-color .15s, background .15s, transform .25s;
}
.swap:disabled { opacity: 1; color: var(--rule); }
.swap:not(:disabled):hover { border-color: var(--form); background: var(--form-soft); }
.swap:not(:disabled):active { transform: rotate(180deg); }

.action { display: flex; flex-direction: column; align-items: center; gap: 8px; margin: 22px 0 30px; }
.action-buttons { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: center; }
.action .primary { font-size: 17px; padding: 12px 56px; }
.action #reset { font-size: 17px; padding: 12px 22px; }
.action p { margin: 0; min-height: 1.4em; font-variant-numeric: tabular-nums; }
.busy { position: relative; }
.busy::after {
  content: ""; display: inline-block; width: 12px; height: 12px; margin-left: 10px; vertical-align: -1px;
  border: 2px solid rgba(255,255,255,.5); border-top-color: #fff; border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Результат */
.result { background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); }
.result-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 20px; border-bottom: 1px solid var(--rule); }
.result-head h2 { margin: 0; font-size: 18px; }
.result-tools { display: flex; gap: 8px; }
.result-body { padding: 8px 28px 24px; min-height: 220px; overflow-x: auto; }
.placeholder { color: var(--muted); max-width: 60ch; margin: 28px 0; }
.result-body h1, .result-body h2, .result-body h3 { color: var(--form); margin: 24px 0 10px; line-height: 1.25; }
.result-body h1 { font-size: 21px; } .result-body h2 { font-size: 18px; } .result-body h3 { font-size: 16px; }
.result-body p, .result-body li { max-width: 80ch; }
.result-body table { border-collapse: collapse; margin: 8px 0 16px; font-variant-numeric: tabular-nums; font-size: 14px; min-width: 60%; }
.result-body th { text-align: left; font-weight: 600; background: var(--form-soft); border-bottom: 2px solid var(--form); }
.result-body th, .result-body td { padding: 6px 12px; border: 1px solid var(--rule); vertical-align: top; }
.result-body td.num { text-align: right; white-space: nowrap; }
.result-body td.diff { color: var(--diff); background: var(--diff-soft); font-weight: 600; }
.result-body td.ok { color: var(--ok); }
.result-body td.flag { color: var(--diff); font-weight: 500; }
.result-body code { background: var(--paper); padding: 1px 5px; border-radius: 4px; }
.result-body .error { color: var(--diff); }
.result-foot {
  display: flex; gap: 24px; flex-wrap: wrap; padding: 12px 20px; border-top: 1px solid var(--rule);
  background: #F7F9FB; border-radius: 0 0 var(--radius) var(--radius); font-variant-numeric: tabular-nums;
}
.result-foot strong { color: var(--form); }

/* Настройки */
dialog {
  border: none; padding: 0; margin: 0 0 0 auto; height: 100vh; max-height: 100vh;
  width: min(640px, 100vw); background: var(--sheet); color: var(--ink);
  box-shadow: -8px 0 30px rgba(27,36,48,.18);
}
dialog::backdrop { background: rgba(27,36,48,.35); }
.settings { display: flex; flex-direction: column; gap: 14px; height: 100%; padding: 20px 24px; }
.settings-head { display: flex; justify-content: space-between; align-items: center; }
.settings-head h2 { margin: 0; font-size: 20px; color: var(--form); }
.field { display: flex; flex-direction: column; gap: 5px; flex: 1; }
.field > span { font-weight: 500; font-size: 14px; }
.field.narrow { flex: 0 0 120px; }
.field.grow { flex: 1 1 auto; min-height: 200px; }
.field textarea { flex: 1; resize: none; font-size: 13px; line-height: 1.5; }
.row { display: flex; gap: 14px; }
.settings-foot { display: flex; gap: 8px; align-items: center; padding-top: 6px; border-top: 1px solid var(--rule); padding-top: 14px; }
.spacer { flex: 1; }

/* Роли: элементы чужой роли скрыты (а для пользователя ещё и удаляются из страницы) */
body:not([data-role="admin"]) .admin-only { display: none !important; }
body:not([data-role="user"]) .user-only { display: none !important; }
body[data-role=""] main { visibility: hidden; }

.bar-left { display: flex; align-items: center; gap: 28px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; }
.tab { padding: 6px 12px; color: var(--muted); border-radius: var(--radius); }
.tab:hover { color: var(--form); background: var(--form-soft); }
.tab.active { color: var(--form); background: var(--form-soft); font-weight: 600; }
.quota { display: flex; align-items: baseline; gap: 8px; font-variant-numeric: tabular-nums; }
.quota strong { font-size: 18px; color: var(--form); }
.quota.low strong { color: var(--diff); }
.account { display: flex; align-items: center; gap: 12px; padding-left: 16px; border-left: 1px solid var(--rule); }
.who { font-weight: 500; }
.notice.warn { background: var(--diff-soft); border-left-color: var(--diff); color: var(--diff); }

.view-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 14px; flex-wrap: wrap; }
.view-head h2 { margin: 0; font-size: 20px; color: var(--form); }
.inline { display: flex; align-items: center; gap: 8px; }
.table-wrap { overflow-x: auto; background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); }
table.grid { border-collapse: collapse; width: 100%; font-size: 14px; font-variant-numeric: tabular-nums; }
table.grid th { text-align: left; font-weight: 600; background: var(--form-soft); border-bottom: 2px solid var(--form); white-space: nowrap; }
table.grid th, table.grid td { padding: 8px 12px; border-bottom: 1px solid var(--rule); vertical-align: middle; }
table.grid tbody tr:hover { background: #F7F9FB; }
table.grid .r { text-align: right; }
.quota-cell { display: flex; align-items: center; gap: 12px; }
.quota-cell .big { font-size: 17px; font-weight: 600; color: var(--form); min-width: 2.5em; text-align: right; }
.quota-cell .big.zero { color: var(--diff); }
.small { font-size: 12.5px; }
table.grid tr.inactive td { color: var(--muted); }
.quick { display: flex; gap: 4px; }
.quick button { padding: 3px 8px; font-size: 13px; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12.5px; background: var(--paper); border: 1px solid var(--rule); }
.badge.ok { color: var(--ok); border-color: #B7D8C6; background: #EEF7F1; }
.badge.err { color: var(--diff); border-color: #E7BDB9; background: var(--diff-soft); }
.badge.admin { color: var(--form); border-color: #BFCDE3; background: var(--form-soft); }
.files { max-width: 360px; }
.files div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.err-text { color: var(--diff); font-size: 12.5px; }
.danger { color: var(--diff); }
.danger:not(:disabled):hover { border-color: var(--diff); color: var(--diff); }
.form-error { color: var(--diff); margin: 0; }
dialog.small { width: min(560px, 96vw); }
dialog.small .settings { height: auto; }
.quota-edit { display: flex; gap: 6px; }
.quota-edit input { width: 120px; }

/* История */
.filters { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.filters input[type=search] { width: 300px; max-width: 100%; }
#historyTable tbody tr { cursor: pointer; }
#historyTable td.party { font-weight: 600; }
#historyTable td.nowrap, #historyTable td.period { white-space: nowrap; }
.result-meta { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.result-meta strong { color: var(--ink); }
.notice.repeat { display: flex; gap: 10px; flex-wrap: wrap; align-items: baseline; justify-content: center;
  margin: 0; background: #FFF7E0; border-left-color: #C08A00; color: #6B4E00; }
dialog.wide { width: min(1100px, 96vw); }
.run-view { display: flex; flex-direction: column; max-height: 90vh; padding: 18px 22px; gap: 6px; }
.run-view .settings-head { align-items: flex-start; gap: 16px; border-bottom: 1px solid var(--rule); padding-bottom: 10px; }
.run-view .settings-head p { margin: 4px 0 0; }
.run-view .result-body { overflow: auto; padding: 4px 6px 12px; min-height: 0; }

/* Страница входа */
.login-page { min-height: 100vh; display: grid; place-items: center; }
.login-wrap { width: 100%; max-width: 400px; padding: 24px; }
.login-card {
  background: var(--sheet); border: 1px solid var(--rule); border-top: 3px solid var(--form);
  border-radius: var(--radius); padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
.login-card h1 { margin: 0; font-size: 24px; color: var(--form); }
.login-card .muted { margin: -6px 0 6px; }
.login-card .primary { padding: 11px; font-size: 16px; margin-top: 4px; }
.login-switch { margin: 0; text-align: center; font-size: 14px; color: var(--muted); }
.login-done { margin: 0; }
.trap { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }
.captcha { display: flex; gap: 8px; align-items: center; }
.captcha-img { display: block; width: 165px; height: 53px; flex: none; border-radius: var(--radius); overflow: hidden; }
.captcha-img svg { width: 100%; height: 100%; display: block; }
.captcha input { flex: 1; min-width: 0; font-size: 18px; text-align: center; }

/* Маскировка */
.privacy { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; justify-content: center; color: var(--ok); }
.privacy.off { color: var(--muted); }
.privacy::before { content: "🔒"; }
.privacy.off::before { content: "🔓"; }
fieldset.mask { border: 1px solid var(--rule); border-radius: var(--radius); padding: 12px 14px; margin: 0; gap: 8px; }
.check { display: flex; gap: 10px; align-items: flex-start; font-weight: 500; cursor: pointer; }
.check input { margin-top: 3px; width: 16px; height: 16px; accent-color: var(--form); }
.mask textarea { resize: vertical; font-size: 13px; }
dialog.center {
  margin: auto; height: fit-content; max-height: 88vh; width: min(900px, 96vw); border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(27,36,48,.25);
}
.preview { display: flex; flex-direction: column; max-height: 88vh; padding: 20px 24px; gap: 10px; }
.preview-body { overflow: auto; }
.preview h3 { font-size: 15px; margin: 14px 0 8px; color: var(--form); }
.preview table { border-collapse: collapse; font-size: 14px; }
.preview th, .preview td { border: 1px solid var(--rule); padding: 5px 10px; text-align: left; }
.preview th { background: var(--form-soft); }
.preview td.tok { font-weight: 600; color: var(--ok); white-space: nowrap; }
.preview pre {
  background: var(--paper); border: 1px solid var(--rule); border-radius: var(--radius);
  padding: 12px; font-size: 12.5px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; margin: 0 0 12px;
}
.preview mark { background: #DCEFE4; color: var(--ok); font-weight: 600; padding: 0 2px; border-radius: 3px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: var(--radius); max-width: 90vw;
}
.toast.err { background: var(--diff); }

/* Версия и «Что нового» */
.version {
  display: flex; justify-content: flex-end; gap: 8px; align-items: baseline;
  max-width: 1180px; margin: -12px auto 0; padding: 0 28px 16px; font-size: 13px;
}
dialog.news { width: min(600px, 96vw); }
dialog.news .settings { height: auto; }
.whats-new { overflow: auto; max-height: 60vh; }
.whats-new h3 { font-size: 15px; margin: 14px 0 6px; color: var(--form); }
.whats-new h3:first-child { margin-top: 0; }
.whats-new h3 .muted { font-weight: 400; font-size: 13px; margin-left: 6px; }
.whats-new ul { margin: 0; padding-left: 20px; }
.whats-new li { margin: 4px 0; }

@media (max-width: 760px) {
  .account { border-left: none; padding-left: 0; }
  .bar { padding: 12px 16px; }
  .bar-right { width: 100%; gap: 12px; }
  .model { flex: 1 1 100%; order: 3; }
  .model select { width: 100%; min-width: 0; flex: 1; }
  .result-head { flex-wrap: wrap; gap: 8px; padding: 12px 16px; }
  main { padding: 16px; }
  .version { padding: 0 16px 16px; margin-top: 0; }
  .desk { grid-template-columns: 1fr; }
  .seam { height: 40px; }
  .seam::before { top: 0; bottom: 0; }
  .swap { transform: rotate(90deg); }
  .swap:not(:disabled):active { transform: rotate(270deg); }
  .result-body { padding: 8px 16px 20px; }
  .row { flex-direction: column; }
  .field.narrow { flex: 1; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
@media print {
  .bar, .desk, .action, .result-tools, .version, dialog { display: none !important; }
  body, main, .result { background: #fff; border: none; }
}
