:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #0f766e;
  --brand-dark: #115e59;
  --ok: #16a34a;
  --ok-bg: #dcfce7;
  --warn: #ca8a04;
  --warn-bg: #fef9c3;
  --bad: #dc2626;
  --bad-bg: #fee2e2;
  --neutral-bg: #e2e8f0;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 14px rgba(15, 23, 42, .06);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg); color: var(--ink); font-size: 14px; line-height: 1.45;
}
h1 { font-size: 22px; margin: 20px 0 14px; font-weight: 700; letter-spacing: -0.02em; }
h2 { font-size: 16px; margin: 0 0 12px; font-weight: 650; }
.mt { margin-top: 28px; }
.muted { color: var(--muted); margin: 4px 0 16px; }
.container { max-width: none; width: 100%; margin: 0 auto; padding: 0 16px 40px; }
main.bare { margin: 0; padding: 0; }

/* ---------- topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 28px;
  background: #0f172a; color: #fff; padding: 0 24px; height: 56px;
  position: sticky; top: 0; z-index: 50; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; white-space: nowrap; }
.logo {
  width: 30px; height: 30px; border-radius: 8px; background: var(--brand);
  display: inline-flex; align-items: center; justify-content: center; font-weight: 800;
}
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a {
  color: #94a3b8; text-decoration: none; padding: 6px 14px; border-radius: 8px; font-weight: 550;
  transition: all .15s;
}
.topbar nav a:hover { color: #fff; background: rgba(255,255,255,.08); }
.topbar nav a.active { color: #fff; background: var(--brand); }
.userbox { display: flex; align-items: center; gap: 10px; }
.user-name { font-weight: 600; }
.user-role { font-size: 11px; background: rgba(255,255,255,.14); padding: 3px 8px; border-radius: 99px; color: #cbd5e1; }
.logout { color: #94a3b8; text-decoration: none; font-size: 17px; padding: 4px 8px; border-radius: 8px; }
.logout:hover { color: #fca5a5; background: rgba(255,255,255,.08); }

/* ---------- login ---------- */
.login-wrap {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #0f172a 0%, #134e4a 100%); padding: 20px;
}
.login-card {
  background: var(--card); padding: 40px 36px; border-radius: 18px; width: 400px; max-width: 100%;
  box-shadow: 0 24px 60px rgba(0,0,0,.35); text-align: center;
}
.login-logo {
  width: 60px; height: 60px; margin: 0 auto 16px; border-radius: 16px; background: var(--brand);
  color: #fff; font-size: 32px; font-weight: 800; display: flex; align-items: center; justify-content: center;
}
.login-card h1 { font-size: 19px; margin: 0 0 4px; }
.login-card label { display: block; text-align: left; margin: 14px 0 6px; font-weight: 600; font-size: 12.5px; color: var(--muted); }
.login-card input {
  width: 100%; padding: 11px 14px; border: 1.5px solid var(--line); border-radius: 10px; font-size: 14px;
  transition: border .15s;
}
.login-card input:focus { outline: none; border-color: var(--brand); }
.login-card .btn { margin-top: 22px; }

/* ---------- controls ---------- */
.btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 9px 16px;
  border-radius: 10px; cursor: pointer; font-size: 13.5px; font-weight: 600; transition: all .15s;
  white-space: nowrap;
}
.btn:hover { border-color: #cbd5e1; background: #f8fafc; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--bad); }
.btn.danger:hover { background: var(--bad-bg); border-color: #fecaca; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.full { width: 100%; }
.btn.sm { padding: 5px 10px; font-size: 12.5px; border-radius: 8px; }

input, select {
  padding: 9px 12px; border: 1.5px solid var(--line); border-radius: 10px; font-size: 13.5px;
  background: #fff; color: var(--ink); font-family: inherit;
}
input:focus, select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(15,118,110,.12); }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px; border: 1px solid rgba(226,232,240,.6);
}
.alert { padding: 10px 14px; border-radius: 10px; margin: 10px 0; font-weight: 550; }
.alert.error { background: var(--bad-bg); color: var(--bad); }

/* ---------- stats ---------- */
.stats-row { display: grid; grid-template-columns: repeat(6, 1fr); gap: 14px; margin-bottom: 16px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); border: 1px solid rgba(226,232,240,.6); }
.stat-num { font-size: 26px; font-weight: 750; letter-spacing: -0.03em; }
.stat-label { font-size: 12px; color: var(--muted); margin-top: 2px; font-weight: 550; }
.stat.ok .stat-num { color: var(--ok); }
.stat.warn .stat-num { color: var(--warn); }
.stat.bad .stat-num { color: var(--bad); }

/* ---------- filters ---------- */
.filters { display: flex; gap: 12px; align-items: end; flex-wrap: wrap; margin-bottom: 16px; }
.filter label { display: block; font-size: 11.5px; font-weight: 650; color: var(--muted); margin-bottom: 5px; }
.filter.grow { flex: 1; min-width: 180px; }
.filter.grow input { width: 100%; }
.filter.actions { display: flex; gap: 8px; margin-left: auto; }

/* ---------- table ---------- */
.table-card { padding: 0; overflow: hidden; }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.data { width: 100%; border-collapse: collapse; }
table.data thead th { position: sticky; top: 0; z-index: 5; }

/* Липкая колонка действий: кнопка «Документы» всегда видна при гориз. скролле */
#tbl .actions-cell {
  position: sticky;
  right: 0;
  top: 0;
  background: var(--card);
  box-shadow: -8px 0 14px -8px rgba(15, 23, 42, .28);
  z-index: 4;
  width: 1%;
  white-space: nowrap;
}
#tbl thead .actions-cell { z-index: 7; }
#tbl tbody tr:hover .actions-cell { background: #f8fafc; }
table.data th {
  text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); font-weight: 700; padding: 12px 14px; border-bottom: 1.5px solid var(--line);
  background: #f8fafc; white-space: nowrap;
}
table.data td { padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.data tbody tr:hover { background: #f8fafc; }
table.data tbody tr:last-child td { border-bottom: none; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.money { font-weight: 650; white-space: nowrap; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 650; white-space: nowrap; }
.badge.closed { background: var(--ok-bg); color: #166534; }
.badge.partial { background: var(--warn-bg); color: #854d0e; }
.badge.none { background: var(--bad-bg); color: #991b1b; }
.badge.internal { background: var(--neutral-bg); color: #475569; }
.badge.auto { background: #e0f2fe; color: #075985; font-size: 10.5px; }
.badge.manual { background: #ede9fe; color: #5b21b6; font-size: 10.5px; }

.table-footer { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; font-size: 13px; color: var(--muted); }
.pager { display: flex; gap: 6px; }
.pager button { min-width: 34px; }

.desc-cell { max-width: 420px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 12.5px; }
#tbl th:nth-child(4), #tbl td:nth-child(4) { min-width: 180px; }

/* менеджер-select в таблице */
td .mgr-select { width: 170px; padding: 6px 10px; font-size: 12.5px; border-radius: 8px; }

/* ---------- import ---------- */
.import-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.import-card h2 { margin-bottom: 6px; }
.import-icon { font-size: 34px; margin-bottom: 8px; }
.dropzone {
  margin-top: 14px; border: 2px dashed var(--line); border-radius: var(--radius);
  padding: 26px 18px; text-align: center; transition: all .15s; cursor: pointer;
}
.dropzone:hover, .dropzone.drag { border-color: var(--brand); background: #f0fdfa; }
.dz-text { color: var(--muted); font-size: 13px; }
.dz-text .link { color: var(--brand); font-weight: 650; cursor: pointer; }
.dz-file { margin: 10px 0; font-weight: 600; color: var(--brand-dark); min-height: 18px; word-break: break-all; }
.dropzone .btn { margin-top: 6px; }
.import-result { margin-top: 12px; }
.import-result .ok { background: var(--ok-bg); color: #166534; padding: 10px 14px; border-radius: 10px; font-weight: 550; }
.import-result .err { background: var(--bad-bg); color: var(--bad); padding: 10px 14px; border-radius: 10px; font-weight: 550; }

/* ---------- directories ---------- */
.dirs-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
.inline-form { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; align-items: center; }
.inline-form input[type="text"], .inline-form input:not([type]) { flex: 1; min-width: 140px; }
.inline-form input[type="password"], .inline-form input[type="checkbox"] { flex: 0 0 auto; width: auto; min-width: 0; }
.check { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--ink); white-space: nowrap; cursor: pointer; margin: 0; }
.check input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--brand); margin: 0; flex: 0 0 auto; }

/* ---------- modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.5); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 20px;
  backdrop-filter: blur(2px);
}
.modal {
  background: #fff; border-radius: 16px; width: 720px; max-width: 100%; max-height: 86vh;
  display: flex; flex-direction: column; box-shadow: 0 24px 60px rgba(0,0,0,.3);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--muted); padding: 4px 8px; border-radius: 8px; }
.modal-close:hover { background: var(--bg); }
.modal-body { padding: 18px 22px; overflow-y: auto; }
.payment-summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 16px; }
.payment-summary .ps-item { background: var(--bg); border-radius: 10px; padding: 10px 12px; }
.payment-summary .ps-label { font-size: 11px; color: var(--muted); font-weight: 650; }
.payment-summary .ps-value { font-weight: 650; margin-top: 2px; word-break: break-word; }
.link-section { margin-top: 18px; }
.link-form { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.search-hits { margin-top: 10px; max-height: 260px; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; }
.search-hits table td { padding: 8px 12px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%) translateY(80px);
  background: #0f172a; color: #fff; padding: 11px 20px; border-radius: 12px; font-weight: 550;
  box-shadow: var(--shadow); transition: transform .25s; z-index: 200; max-width: 90vw;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--bad); }

.empty { padding: 40px; text-align: center; color: var(--muted); }

@media (max-width: 1100px) {
  .stats-row { grid-template-columns: repeat(3, 1fr); }
  .import-grid, .dirs-grid { grid-template-columns: 1fr; }
  td .mgr-select { width: 140px; }
}
@media (max-width: 720px) {
  .stats-row { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat { padding: 12px 14px; }
  .stat-num { font-size: 21px; }
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 12px; gap: 10px; }
  .topbar nav { order: 3; width: 100%; overflow-x: auto; }
  .topbar nav a { padding: 6px 10px; font-size: 13px; }
  .container { padding: 0 12px 30px; }
  h1 { font-size: 19px; }
  .filters { gap: 8px; }
  .filter.actions { margin-left: 0; width: 100%; }
  .filter.actions .btn { flex: 1; }
  .filter.grow { min-width: 100%; }
  table.data th, table.data td { padding: 8px 10px; font-size: 13px; white-space: nowrap; }
  td .mgr-select { width: 120px; }
  .desc-cell { display: none; }
  /* мобильная таблица сверки: прячем второстепенные колонки */
  #tbl th:nth-child(2), #tbl td:nth-child(2),   /* № п/п */
  #tbl th:nth-child(5), #tbl td:nth-child(5),   /* № счёта */
  #tbl th:nth-child(6), #tbl td:nth-child(6) { display: none; }  /* Закрыто */
  #tbl .actions-cell { box-shadow: -6px 0 12px -6px rgba(15, 23, 42, .28); }
  #tbl .actions-cell .btn { padding: 5px 8px; font-size: 12px; }
  .payment-summary { grid-template-columns: 1fr 1fr; }
  .table-footer { flex-wrap: wrap; gap: 8px; }
  .login-card { padding: 30px 22px; }
  .modal { width: 100%; border-radius: 12px; }
  .link-form .filter.grow { min-width: 100%; }
  input, select { font-size: 16px; }
  input.mgr-select { font-size: 16px; }
  .dropzone { padding: 18px 12px; }
  .inline-form input[type="text"], .inline-form input:not([type]) { min-width: 100%; }
  .import-result .ok, .import-result .err { word-break: break-word; }
}
