:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --text: #172033;
  --muted: #667085;
  --line: #d9e1ea;
  --primary: #2f6fed;
  --primary-dark: #2459c7;
  --warning: #fff3cd;
  --warning-border: #f0c36a;
  --error: #fde8e8;
  --error-border: #f3a6a6;
  --success: #e7f6ed;
  --success-border: #96d2aa;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: #14213d;
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { font-weight: 700; letter-spacing: .2px; }
.topbar nav { display: flex; align-items: center; gap: 16px; }
.topbar a { color: #fff; }
.user { color: #cbd5e1; }
.container { max-width: 1500px; margin: 24px auto; padding: 0 18px 48px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 8px 22px rgba(16, 24, 40, .04);
  margin-bottom: 18px;
}
.panel-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 14px;
}
.panel-header.compact { align-items: center; }
h1, h2 { margin: 0 0 6px; }
h1 { font-size: 28px; }
h2 { font-size: 20px; }
.muted { color: var(--muted); margin: 0; }
code { background: #eef2f7; padding: 2px 5px; border-radius: 4px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  min-height: 38px;
  padding: 8px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
}
.btn:hover { text-decoration: none; border-color: #b9c4d0; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-light { background: #f8fafc; }
.btn-small { min-height: 30px; padding: 5px 9px; font-size: 13px; }
button[disabled] { opacity: .6; cursor: not-allowed; }

.filters, .sync-form {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: end;
}
label { font-size: 13px; color: var(--muted); display: flex; flex-direction: column; gap: 5px; }
input[type="date"], input[type="text"], input[type="password"], input[name="login"], select {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 14px;
  background: #fff;
  color: var(--text);
}
.filters input[type="text"] { min-width: 220px; }
.filters select { min-width: 170px; }
.checkline { flex-direction: row; align-items: center; color: var(--text); padding-bottom: 9px; }

.alert {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  margin: 12px 0;
}
.alert-warning { background: var(--warning); border-color: var(--warning-border); }
.alert-error { background: var(--error); border-color: var(--error-border); }
.alert-success { background: var(--success); border-color: var(--success-border); }

.sync-status {
  display: flex;
  align-items: center;
  gap: 14px;
  border: 1px solid var(--line);
  background: #f8fbff;
  border-radius: 12px;
  padding: 14px;
  margin-top: 14px;
}
.loader {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 4px solid #dbeafe;
  border-top-color: var(--primary);
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.progress-wrap {
  height: 8px;
  width: 360px;
  max-width: 100%;
  background: #e2e8f0;
  border-radius: 999px;
  overflow: hidden;
  margin-top: 8px;
}
.progress-bar { height: 100%; background: var(--primary); transition: width .25s ease; }
.sync-counters { margin-top: 6px; color: var(--muted); font-size: 13px; }

.table-scroll { overflow: auto; border: 1px solid var(--line); border-radius: 12px; }
.data-table, .mini-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.data-table th, .data-table td, .mini-table th, .mini-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
.data-table th, .mini-table th {
  position: sticky;
  top: 0;
  background: #eef2f7;
  text-align: left;
  z-index: 1;
}
.data-table tbody tr:hover { background: #f8fafc; }
.num { text-align: right !important; white-space: nowrap; }
.empty { text-align: center; padding: 28px !important; color: var(--muted); }
.row-warning { background: var(--warning); }
.row-error { background: var(--error); }
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  margin: 2px 2px 2px 0;
  background: #e2e8f0;
}
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-error { background: #fee2e2; color: #991b1b; }
.badge-info { background: #dbeafe; color: #1d4ed8; }
.badge-success { background: #dcfce7; color: #166534; }

.inner-header { margin-top: 10px; }
.filter-panel { margin-bottom: 14px; }
.details-block { margin-top: 14px; }
.details-block summary { cursor: pointer; font-weight: 600; }
.actions-row { display: flex; gap: 8px; flex-wrap: wrap; }
.nowrap-actions { flex-wrap: nowrap; }
.inline-form { margin: 0; display: inline-flex; }
pre { white-space: pre-wrap; word-break: break-word; max-width: 480px; margin: 0; font-size: 12px; }

.login-card {
  max-width: 420px;
  margin: 80px auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px;
  box-shadow: 0 12px 34px rgba(16, 24, 40, .08);
}
.form-stack { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }

.cards-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(180px, 1fr));
  gap: 12px;
  margin-top: 12px;
}
.info-card {
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}
.label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 5px; }

@media (max-width: 900px) {
  .panel-header { flex-direction: column; }
  .cards-grid { grid-template-columns: 1fr; }
  .topbar { padding: 0 12px; }
  .topbar nav { gap: 8px; font-size: 13px; }
}

/* Визуальные правки v3: компактная таблица без горизонтального скролла */
body { overflow-x: hidden; }
.container {
  max-width: 1680px;
  width: min(100%, calc(100vw - 36px));
}

.filters-clean {
  display: grid;
  grid-template-columns: 150px 150px minmax(240px, 1.4fr) minmax(180px, .8fr) minmax(240px, 1.2fr) auto;
  align-items: end;
  gap: 12px;
}
.filters-clean input[type="text"],
.filters-clean input[type="date"],
.filters-clean select {
  width: 100%;
  min-width: 0;
}
.filter-actions {
  display: flex;
  gap: 10px;
  align-items: end;
  white-space: nowrap;
}
.select-wrap {
  position: relative;
  display: block;
}
.select-wrap::after {
  content: "";
  position: absolute;
  right: 12px;
  top: 50%;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: translateY(-65%) rotate(45deg);
  pointer-events: none;
}
.select-control {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 34px !important;
  background: #fff;
  cursor: pointer;
}
.select-control:focus,
input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(47, 111, 237, .12);
}

.table-fit {
  width: 100%;
  overflow-x: visible;
  border: 1px solid var(--line);
  border-radius: 12px;
}
.main-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 12px;
  line-height: 1.25;
}
.main-table th,
.main-table td {
  padding: 8px 8px;
  overflow-wrap: anywhere;
  word-break: normal;
}
.main-table th {
  white-space: normal;
  line-height: 1.2;
}
.main-table .num {
  white-space: normal;
  overflow-wrap: normal;
  word-break: normal;
}
.main-table .cell-cargo {
  font-size: 12px;
}
.main-table .badge {
  font-size: 11px;
  padding: 2px 6px;
}
.table-actions {
  flex-direction: column;
  gap: 6px;
  align-items: stretch;
}
.table-actions .btn,
.table-actions .inline-form,
.table-actions .inline-form button {
  width: 100%;
}
.table-actions .btn-small {
  min-height: 28px;
  padding: 4px 6px;
  font-size: 12px;
}

.col-cargo { width: 12%; }
.col-date { width: 6%; }
.col-issued { width: 6%; }
.col-party { width: 9%; }
.col-party-wide { width: 12%; }
.col-money { width: 7%; }
.col-money-wide { width: 8.5%; }
.col-status { width: 7%; }
.col-flags { width: 8%; }
.col-actions { width: 7%; }

@media (max-width: 1200px) {
  .container { width: min(100%, calc(100vw - 20px)); padding-left: 10px; padding-right: 10px; }
  .panel { padding: 14px; }
  .main-table { font-size: 11px; }
  .main-table th, .main-table td { padding: 7px 5px; }
  .table-actions .btn-small { font-size: 11px; }
}

@media (max-width: 1100px) {
  .filters-clean {
    grid-template-columns: repeat(2, minmax(140px, 1fr));
  }
  .filter-counterparty,
  .filter-search {
    grid-column: span 2;
  }
  .filter-actions {
    grid-column: span 2;
  }
}

/* Логика обновления v4 */
.sync-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}
.sync-form-period,
.sync-form-action {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}
.sync-form-action {
  align-self: end;
}

/* Фильтры v4: добавлены даты выдачи */
.filters-clean {
  grid-template-columns:
    130px
    130px
    150px
    150px
    minmax(210px, 1.25fr)
    minmax(160px, .8fr)
    minmax(220px, 1.15fr)
    auto;
}

@media (max-width: 1450px) {
  .filters-clean {
    grid-template-columns: repeat(4, minmax(130px, 1fr));
  }
  .filter-counterparty,
  .filter-search {
    grid-column: span 2;
  }
  .filter-actions {
    grid-column: span 2;
  }
}

@media (max-width: 760px) {
  .sync-toolbar,
  .sync-form-period,
  .sync-form-action {
    display: flex;
    width: 100%;
  }
  .sync-toolbar .btn,
  .sync-toolbar label,
  .sync-toolbar input {
    width: 100%;
  }
  .filters-clean {
    grid-template-columns: 1fr;
  }
  .filter-counterparty,
  .filter-search,
  .filter-actions {
    grid-column: auto;
  }
  .filter-actions {
    flex-direction: column;
    align-items: stretch;
  }
}

@media (max-width: 1100px) and (min-width: 761px) {
  .filters-clean {
    grid-template-columns: repeat(2, minmax(140px, 1fr));
  }
  .filter-counterparty,
  .filter-search,
  .filter-actions {
    grid-column: span 2;
  }
}
