:root {
  --bg: #0f1419;
  --surface: #1a222d;
  --surface2: #232d3b;
  --border: #2e3a4d;
  --text: #e8eef5;
  --muted: #8b9cb3;
  --accent: #3d8bfd;
  --good: #2ecc71;
  --warn: #f1c40f;
  --bad: #e74c3c;
  --radius: 10px;
  font-family: "Segoe UI", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100vh;
}

header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1rem;
}

header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
  flex: 1 1 200px;
}

.period-badge {
  font-size: 0.85rem;
  color: var(--muted);
}

.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 1rem;
  background: var(--surface2);
  cursor: pointer;
  font-size: 0.9rem;
  transition: border-color 0.15s, background 0.15s;
}

.upload-zone:hover,
.upload-zone.drag {
  border-color: var(--accent);
  background: #2a3545;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

button,
.file-label {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  padding: 0.45rem 0.85rem;
}

button.primary {
  background: #1a4d8f;
  border-color: var(--accent);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

input[type="file"] {
  display: none;
}

.threshold-wrap {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.threshold-wrap input {
  width: 7rem;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

main {
  padding: 1rem 1.25rem 2rem;
  max-width: 1400px;
  margin: 0 auto;
}

.grid-kpi {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

@media (min-width: 720px) {
  .kpi-total-wide {
    grid-column: span 2;
  }
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
}

.kpi .label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.kpi .value {
  font-size: 1.35rem;
  font-weight: 600;
  margin-top: 0.25rem;
  word-break: break-word;
}

.kpi .value-sub {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  margin-top: 0.4rem;
  line-height: 1.4;
  word-break: break-word;
}

.kpi .hint {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.35rem;
}

.charts-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

@media (min-width: 960px) {
  .charts-row.two {
    grid-template-columns: 1.2fr 1fr;
  }
}

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.panel h2 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  font-weight: 600;
}

.chart-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 320px;
  height: 400px;
  max-height: 520px;
  overflow: visible;
}

.chart-wrap.tall {
  min-height: 340px;
  height: 420px;
  max-height: 560px;
}

.charts-row .panel > .chart-wrap,
.charts-row .panel > .chart-wrap.tall {
  flex: 1 1 auto;
}

.chart-wrap canvas {
  display: block;
  max-width: 100%;
}

.filter-row {
  margin-bottom: 0.75rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.filter-row label {
  font-size: 0.85rem;
  color: var(--muted);
}

.filter-row select {
  min-width: 220px;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.table-scroll {
  overflow: auto;
  max-height: 420px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

/* Топ должников: итоги всегда под видимой областью прокрутки */
.debtor-table-stack {
  display: flex;
  flex-direction: column;
  max-height: 420px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.debtor-table-stack > .table-scroll {
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  border: none;
  border-radius: 0;
}

.debtor-summary-bar {
  flex: 0 0 auto;
  border-top: 2px solid var(--border);
  background: var(--surface2);
  padding: 0.55rem 0.65rem;
  font-size: 0.82rem;
  font-weight: 600;
}

.debtor-summary-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem 1.25rem;
  justify-content: space-between;
}

.debtor-summary-label {
  color: var(--text);
  flex: 1 1 200px;
}

.debtor-summary-metric {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.debtor-summary-metric.muted {
  font-weight: 500;
  color: var(--muted);
}

.worklist-panel .worklist-hint {
  margin: 0 0 0.65rem;
  font-size: 0.8rem;
  color: var(--muted);
  max-width: 960px;
}

.worklist-filters {
  align-items: flex-end;
}

.worklist-stack {
  max-height: 560px;
}

table.data th.sortable {
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

table.data th.sortable:hover {
  color: var(--accent);
}

table.data th.sorted-col {
  color: var(--accent);
}

table.data th.sorted-col::after {
  content: " \2193";
  font-size: 0.7em;
  opacity: 0.85;
}

table.data th.sorted-col.sorted-asc::after {
  content: " \2191";
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  max-width: 220px;
}

#debtorTable {
  table-layout: fixed;
  width: 100%;
}

#debtorTable th:nth-child(1),
#debtorTable td:nth-child(1) {
  width: 17%;
  word-break: break-word;
  overflow-wrap: anywhere;
}

#debtorTable th:nth-child(2),
#debtorTable td:nth-child(2) {
  width: 6%;
}

#debtorTable th:nth-child(3),
#debtorTable td:nth-child(3) {
  width: 13%;
  word-break: break-word;
  overflow-wrap: anywhere;
}

#debtorTable th:nth-child(4),
#debtorTable td:nth-child(4) {
  width: 11%;
  word-break: break-word;
  overflow-wrap: anywhere;
}

#debtorTable th:nth-child(5),
#debtorTable td:nth-child(5) {
  width: 8%;
}

#debtorTable th:nth-child(6),
#debtorTable td:nth-child(6) {
  width: 7%;
}

#debtorTable th:nth-child(7),
#debtorTable td:nth-child(7) {
  width: 9%;
}

#debtorTable th:nth-child(8),
#debtorTable td:nth-child(8) {
  width: 8%;
}

#debtorTable th:nth-child(9),
#debtorTable td:nth-child(9) {
  width: 13%;
}

#debtorTable th:nth-child(10),
#debtorTable td:nth-child(10) {
  width: 8%;
}

#debtorTable td {
  vertical-align: middle;
}

#debtorTable .tag-list {
  max-width: none;
  width: 100%;
}

#debtorTable .worklist-status {
  min-width: 0;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

.tag {
  display: inline-block;
  padding: 0.12rem 0.35rem;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
}

.tag-growth {
  background: rgba(231, 76, 60, 0.25);
  color: #ff8a80;
}

.tag-nopay {
  background: rgba(241, 196, 15, 0.25);
  color: #f1c40f;
}

.tag-threshold {
  background: rgba(155, 89, 182, 0.28);
  color: #d2a0ff;
}

.worklist-status {
  min-width: 200px;
  max-width: 260px;
  padding: 0.3rem 0.4rem;
  font: inherit;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

table.data th,
table.data td {
  padding: 0.45rem 0.55rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

table.data th {
  position: sticky;
  top: 0;
  background: var(--surface2);
  z-index: 1;
  white-space: nowrap;
}

table.data tbody tr {
  cursor: pointer;
}

table.data tbody tr:hover {
  background: #243041;
}

table.data .num {
  text-align: right;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.age-green {
  color: var(--good);
  font-weight: 600;
}

.age-yellow {
  color: var(--warn);
  font-weight: 600;
}

.age-red {
  color: var(--bad);
  font-weight: 600;
}

.badge-up {
  color: var(--bad);
}

.badge-down {
  color: var(--good);
}

.badge-flat {
  color: var(--muted);
}

.layout-split {
  display: grid;
  gap: 1rem;
}

@media (min-width: 1100px) {
  .layout-split {
    grid-template-columns: 1fr 1fr;
  }
}

.alerts ul {
  margin: 0;
  padding-left: 1.1rem;
  font-size: 0.88rem;
}

.alerts li {
  margin-bottom: 0.35rem;
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--muted);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

.modal-backdrop.open {
  display: flex;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-width: 900px;
  width: 100%;
  max-height: 90vh;
  overflow: auto;
  padding: 1.25rem;
}

.modal header {
  border: none;
  padding: 0 0 0.75rem;
  background: transparent;
}

.modal h2 {
  margin: 0;
  font-size: 1.1rem;
}

.modal .close {
  margin-left: auto;
}

/* Скрыто на экране, видно только при печати/PDF */
.print-only {
  display: none;
}

@media print {
  @page {
    size: A4;
    margin: 12mm;
  }

  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  .print-only {
    display: block !important;
    font-size: 10.5pt;
    color: #111 !important;
    margin: 0 0 0.5rem;
    padding: 0 0 0.35rem;
    border-bottom: 1px solid #333;
  }

  .no-print,
  header .toolbar,
  .upload-zone,
  .filter-row,
  button,
  .modal-backdrop,
  .empty-state {
    display: none !important;
  }

  html,
  body,
  main,
  #dashboard {
    background: #fff !important;
    color: #111 !important;
  }

  body {
    font-size: 9.5pt;
    line-height: 1.35;
  }

  header {
    background: #fff !important;
    border-bottom: 2px solid #000 !important;
    padding: 0.35rem 0 !important;
    margin-bottom: 0.5rem !important;
  }

  header h1 {
    color: #000 !important;
    font-size: 16pt !important;
  }

  .period-badge {
    color: #333 !important;
    font-size: 9pt !important;
  }

  .panel,
  .kpi {
    background: #fff !important;
    border: 1px solid #444 !important;
    color: #111 !important;
    box-shadow: none !important;
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .panel h2 {
    color: #000 !important;
    font-size: 11pt !important;
    border-bottom: 1px solid #999;
    padding-bottom: 0.2rem;
    margin-bottom: 0.4rem !important;
  }

  .kpi .label,
  .kpi .hint {
    color: #444 !important;
  }

  .kpi .value {
    color: #000 !important;
  }

  .kpi .value-sub {
    color: #333 !important;
    font-size: 8.5pt !important;
  }

  .charts-row.two {
    display: block !important;
  }

  .charts-row.two > .panel {
    margin-bottom: 0.75rem;
  }

  .chart-wrap,
  .chart-wrap.tall {
    min-height: 340px !important;
    height: 400px !important;
    max-height: 560px !important;
    overflow: visible !important;
    page-break-inside: avoid;
  }

  .chart-wrap canvas {
    background: #fff !important;
    max-width: 100% !important;
  }

  .debtor-table-stack,
  .worklist-stack,
  .debtor-table-stack > .table-scroll,
  .table-scroll {
    max-height: none !important;
    overflow: visible !important;
    border-color: #444 !important;
  }

  table.data {
    font-size: 8pt;
    width: 100%;
  }

  table.data th,
  table.data td {
    border-color: #555 !important;
    color: #111 !important;
    padding: 0.25rem 0.3rem !important;
  }

  table.data th {
    position: static !important;
    background: #e0e0e0 !important;
    color: #000 !important;
    font-weight: 700;
  }

  table.data tbody tr {
    cursor: default !important;
  }

  table.data tbody tr:hover {
    background: transparent !important;
  }

  .debtor-summary-bar {
    background: #f0f0f0 !important;
    border-top: 2px solid #000 !important;
    color: #000 !important;
    font-size: 9pt !important;
  }

  .debtor-summary-metric,
  .debtor-summary-label {
    color: #000 !important;
  }

  .debtor-summary-metric.muted {
    color: #333 !important;
  }

  select.worklist-status {
    border: 1px solid #333 !important;
    background: #fff !important;
    color: #000 !important;
    font-size: 8pt !important;
    max-width: 100%;
  }

  .tag {
    border: 1px solid #333 !important;
    color: #000 !important;
    background: #f5f5f5 !important;
  }

  .tag-growth,
  .tag-nopay,
  .tag-threshold {
    background: #eaeaea !important;
    color: #000 !important;
  }

  .age-green,
  .age-yellow,
  .age-red {
    color: #000 !important;
    font-weight: 700;
  }

  .badge-up,
  .badge-down,
  .badge-flat {
    color: #000 !important;
  }

  th.sortable.sorted-col::after {
    content: "" !important;
  }
}
