:root {
  --bg: #f3f5f8;
  --surface: #fff;
  --surface-2: #f7f8fb;
  --border: #dde2ea;
  --border-strong: #b6bfcc;
  --text: #182130;
  --muted: #586374;      /* около 6:1 на белом: мелкий текст остаётся читаемым */
  --accent: #1d5fd1;
  --accent-hover: #174da9;
  --accent-soft: #e8f0fe;
  --ok: #17663a;
  --ok-soft: #e4f5ea;
  --warn: #8f4a00;
  --warn-soft: #fdf1dc;
  --err: #b3261e;
  --err-soft: #fcebea;
  --top: #1c2534;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--accent); }
h1 { font-size: 20px; line-height: 1.25; margin: 0; }
h2 { font-size: 16px; line-height: 1.3; margin: 0; }
small, .small { font-size: 12px; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.err-text { color: var(--err); }
.ok-text { color: var(--ok); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ===== шапка ===== */
.topbar {
  display: flex; align-items: center; gap: 8px 20px; flex-wrap: wrap;
  padding: 8px 20px; background: var(--top); color: #fff;
}
.brand { font-weight: 650; font-size: 15px; color: #fff; text-decoration: none; }
.nav { display: flex; gap: 2px; flex-wrap: wrap; flex: 1; }
.nav a { color: #c8d1de; text-decoration: none; padding: 6px 11px; border-radius: 6px; }
.nav a:hover { background: #2b3648; color: #fff; }
.nav a[aria-current="page"] { background: #36445a; color: #fff; font-weight: 600; }
.who { display: flex; align-items: center; gap: 10px; color: #c8d1de; font-size: 13px; }
.who form { margin: 0; }
.topbar :focus-visible { outline-color: #fff; }
.btn-top {
  background: transparent; border: 1px solid #55627a; color: #dbe2ec; border-radius: 6px;
  padding: 4px 11px; font: inherit; font-size: 13px; cursor: pointer;
}
.btn-top:hover { background: #2b3648; color: #fff; }

main { max-width: 1320px; margin: 18px auto 32px; padding: 0 20px; }
main.narrow-page { max-width: 420px; margin-top: 10vh; }
.foot { max-width: 1320px; margin: 0 auto 20px; padding: 0 20px; font-size: 12px; color: var(--muted); }

/* ===== базовые блоки ===== */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; margin-bottom: 16px;
}
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 10px 16px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head p { margin: 2px 0 0; }
.head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.grid-side { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.grid-side > * { min-width: 0; }

/* ===== кнопки: одна главная на экран, остальные спокойные ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 6px 13px; border-radius: 6px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500;
  text-decoration: none; white-space: nowrap; cursor: pointer;
}
.btn:hover { background: var(--surface-2); border-color: #8d98a9; }
.btn:disabled { opacity: .6; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { border-color: var(--err); color: var(--err); }
.btn-danger:hover { background: var(--err-soft); border-color: var(--err); }
.btn-lg { min-height: 40px; padding: 8px 20px; font-size: 15px; }
.btn-sm { min-height: 28px; padding: 2px 9px; font-size: 12px; }
.btn.is-copied { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }

/* ===== формы ===== */
label.field { display: block; font-size: 13px; font-weight: 500; color: var(--muted); }
input, select, textarea {
  display: block; width: 100%; min-height: 36px; margin-top: 4px; padding: 6px 10px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 6px; font: inherit;
}
input[type="checkbox"], input[type="radio"] { display: inline-block; width: auto; min-height: 0; margin: 0; }
input[type="file"] { padding: 4px; font-size: 12px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.field-hint { display: block; font-size: 12px; font-weight: 400; color: var(--muted); margin-top: 3px; }
.stack > * + * { margin-top: 12px; }
.input-wrap { position: relative; display: block; }
.input-wrap input { padding-right: 86px; }
.input-wrap .btn-sm { position: absolute; right: 4px; top: 4px; min-height: 28px; }

.segmented { display: inline-flex; padding: 2px; background: #e6eaf0; border-radius: 7px; flex-wrap: wrap; }
.segmented label, .segmented a {
  display: block; padding: 5px 13px; border-radius: 5px; color: var(--muted);
  cursor: pointer; font-weight: 500; text-decoration: none;
}
.segmented input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented label:has(input:checked), .segmented a[aria-current="true"] {
  background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(20, 30, 50, .18);
}
.segmented label:has(input:focus-visible) { outline: 2px solid var(--accent); }

.req-head { display: flex; align-items: center; justify-content: space-between; gap: 10px 16px; flex-wrap: wrap; margin-bottom: 14px; }
.req-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; align-items: start; }
.amount-input { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; min-height: 40px; }
.amount-input::placeholder { font-weight: 400; }
.req-grid select, .req-grid input:not(.amount-input) { min-height: 40px; }
.quick { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.quick-title { font-size: 12px; color: var(--muted); margin-right: 2px; }
.pick {
  padding: 2px 10px; min-height: 26px; border: 1px solid var(--border); border-radius: 13px;
  background: var(--surface-2); font: inherit; font-size: 12.5px; color: var(--text); cursor: pointer;
}
.pick:hover { border-color: var(--accent); color: var(--accent); }
.req-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 14px; }
.form-msg { font-size: 13px; color: var(--muted); }
.form-msg.err { color: var(--err); font-weight: 600; }
.best-box { margin-top: 12px; padding: 10px 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; }
.best-title { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.best-providers { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.best-opt { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.best-off { color: var(--muted); cursor: not-allowed; }
.extra-fields { margin-top: 12px; }

/* ===== уведомления ===== */
.alert {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 10px 14px; margin-bottom: 14px; border-radius: var(--radius);
  border: 1px solid #b9cff5; background: var(--accent-soft); color: #174080;
}
.alert-ok { border-color: #a9dcbb; background: var(--ok-soft); color: #11512d; }
.alert-err { border-color: #f1b8b4; background: var(--err-soft); color: #8c1d18; }
.alert-warn { border-color: #eccb91; background: var(--warn-soft); color: #6d3900; }
.alert-close { background: none; border: 0; color: inherit; font-size: 18px; line-height: 1; cursor: pointer; padding: 0 2px; }
.toast-box { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 80; display: grid; gap: 6px; justify-items: center; width: max-content; max-width: calc(100vw - 32px); }
.toast { padding: 8px 14px; border-radius: 6px; background: #1c2534; color: #fff; font-size: 13px; box-shadow: 0 4px 14px rgba(0, 0, 0, .25); }
.toast-err { background: var(--err); }

/* ===== статусы: цвет дублируется текстом ===== */
.chip {
  display: inline-block; padding: 1px 9px; border-radius: 11px; font-size: 12.5px; font-weight: 500; white-space: nowrap;
  background: var(--accent-soft); color: #174a9e;
}
.chip-done { background: var(--ok-soft); color: var(--ok); }
.chip-failed { background: var(--err-soft); color: var(--err); }
.chip-search, .chip-stuck { background: var(--warn-soft); color: var(--warn); }
.ttl { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; margin-top: 2px; }
.ttl:empty { display: none; }
.ttl-warn { color: var(--warn); font-weight: 600; }
.ttl-dead { color: var(--muted); }

/* ===== показатели ===== */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 16px; }
.kpi { display: block; padding: 11px 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: inherit; text-decoration: none; }
a.kpi:hover { border-color: var(--accent); }
.kpi-label { display: block; font-size: 12.5px; color: var(--muted); }
.kpi-value { display: block; font-size: 22px; font-weight: 650; line-height: 1.3; font-variant-numeric: tabular-nums; }
.kpi-sub { display: block; font-size: 12px; color: var(--muted); }
.kpi-err .kpi-value { color: var(--err); }
.kpi-ok .kpi-value { color: var(--ok); }
.kpi-warn .kpi-value { color: var(--warn); }
body.drawer-open .err-line { max-width: 170px; }
.summary-line { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 0 0 12px; color: var(--muted); }
.summary-line b { color: var(--text); font-variant-numeric: tabular-nums; }
.today-line { font-size: 13px; color: var(--muted); }
.today-line b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ===== таблицы ===== */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: 6px; }
.table-wrap.tall { max-height: 72vh; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid #edf0f4; vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
thead th {
  position: sticky; top: 0; z-index: 1; background: var(--surface-2); color: var(--muted);
  font-size: 12px; font-weight: 600; border-bottom: 1px solid var(--border); white-space: nowrap;
}
th.num { text-align: right; }
tfoot th { background: var(--surface-2); font-weight: 600; border-top: 1px solid var(--border); border-bottom: 0; }
td small { display: block; color: var(--muted); }
.row-link { cursor: pointer; }
.row-link:hover td { background: #f6f8fb; }
.row-link:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.row-link.is-active td { background: var(--accent-soft); }
.row-link.is-active td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
.rec-id { font-weight: 600; white-space: nowrap; }
.req-cell { font-family: var(--mono); font-size: 13px; overflow-wrap: anywhere; }
.err-line { display: block; max-width: 260px; margin-top: 2px; font-size: 12px; color: var(--err); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { padding: 24px 12px; text-align: center; color: var(--muted); }
.meter { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
.meter i { display: block; width: 44px; height: 5px; border-radius: 3px; background: #e4e8ee; overflow: hidden; }
.meter i::before { content: ""; display: block; height: 100%; width: var(--v); background: var(--accent); }

/* ===== история: панель фильтров ===== */
.hist-head { display: flex; align-items: center; justify-content: space-between; gap: 8px 16px; flex-wrap: wrap; margin-bottom: 12px; }
.hist-title { display: flex; align-items: baseline; gap: 6px 14px; flex-wrap: wrap; }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin-bottom: 12px; }
.tab {
  background: none; border: 0; border-bottom: 2px solid transparent; margin-bottom: -1px;
  padding: 7px 12px; font: inherit; font-weight: 500; color: var(--muted); cursor: pointer;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.badge { display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 3px; border-radius: 10px; background: #e6eaf0; color: var(--text); font-size: 12px; font-weight: 500; text-align: center; }
.tab.active .badge { background: var(--accent-soft); color: var(--accent); }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input, .toolbar select { margin: 0; width: auto; min-height: 34px; }
.toolbar input[type="search"] { flex: 1 1 220px; min-width: 180px; }
.filter-info { font-size: 12.5px; color: var(--accent); font-weight: 600; }
.more-line { margin: 10px 0 0; font-size: 13px; color: var(--muted); }

/* ===== фильтры аудита ===== */
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px 12px; align-items: end; }
.filters .wide { grid-column: span 2; }
.filters-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 12px; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.err-list { list-style: none; margin: 0; padding: 0; }
.err-list li { display: flex; justify-content: space-between; gap: 14px; padding: 7px 0; border-bottom: 1px solid #edf0f4; }
.err-list li:last-child { border-bottom: 0; }
.err-list a { color: var(--text); overflow-wrap: anywhere; }

/* ===== боковая панель заявки ===== */
.drawer {
  position: fixed; inset: 0 0 0 auto; width: 440px; max-width: 100%; z-index: 50;
  display: flex; flex-direction: column; background: var(--surface);
  border-left: 1px solid var(--border); box-shadow: -8px 0 24px rgba(20, 30, 50, .1);
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 18px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.drawer-close { background: none; border: 0; font-size: 20px; line-height: 1; padding: 4px 8px; border-radius: 4px; color: var(--muted); cursor: pointer; }
.drawer-close:hover { background: #e6eaf0; color: var(--text); }
.drawer-body { padding: 16px 18px; overflow-y: auto; flex: 1; }
.drawer-backdrop { position: fixed; inset: 0; background: rgba(15, 22, 35, .3); z-index: 40; }
@media (min-width: 1200px) {
  body.drawer-open main, body.drawer-open .foot { margin-right: 450px; margin-left: 10px; max-width: none; }
  .drawer-backdrop { display: none; }
}
.d-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.d-head .ttl { display: inline; margin: 0; }
.d-amount { font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; margin-bottom: 10px; }
.d-note { margin: 0 0 12px; padding: 8px 11px; border-radius: 6px; background: var(--warn-soft); color: #6d3900; font-size: 13px; }
.d-error { margin: 0 0 12px; padding: 8px 11px; border-radius: 6px; background: var(--err-soft); color: #8c1d18; font-size: 13px; overflow-wrap: anywhere; }
.d-primary { padding: 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); margin-bottom: 12px; }
.d-value { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.d-value + .d-value { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.d-value-main { min-width: 0; overflow-wrap: anywhere; }
.d-value-label { display: block; font-size: 12px; color: var(--muted); }
.d-big { font-family: var(--mono); font-size: 18px; font-weight: 600; }
.d-link { font-size: 13px; }
.d-buttons { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.d-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 16px; margin: 0 0 14px; }
.d-meta dt { font-size: 12px; color: var(--muted); }
.d-meta dd { margin: 0; overflow-wrap: anywhere; }
.d-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.d-actions form { margin: 0; }
.d-section { border-top: 1px solid var(--border); padding: 10px 0; }
.d-section > summary { cursor: pointer; font-weight: 600; padding: 2px 0; }
.d-section-body { padding-top: 10px; }
.d-section-body > * + * { margin-top: 8px; }
.ids { display: grid; gap: 8px; }
.ids dt { font-size: 12px; color: var(--muted); }
.ids dd { margin: 0; display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; font-family: var(--mono); font-size: 12px; overflow-wrap: anywhere; }
.ids dd span { min-width: 0; }
.raw-pre {
  max-height: 320px; overflow: auto; margin: 8px 0 0; padding: 10px 12px; border-radius: 6px;
  background: #182130; color: #e4e8ef; font: 11.5px/1.45 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere;
}

/* ===== телефон: строка истории становится компактной карточкой ===== */
@media (max-width: 760px) {
  body { font-size: 15px; }
  .topbar { padding: 8px 16px; }
  .nav { order: 3; flex-basis: 100%; }
  .who { margin-left: auto; }
  main, .foot { padding: 0 12px; }
  main { margin-top: 12px; }
  .card { padding: 14px; }
  .btn { min-height: 40px; }
  .btn-sm { min-height: 36px; padding: 4px 12px; }
  .pick { min-height: 34px; padding: 4px 12px; }
  .req-actions .btn-primary { width: 100%; }
  input, select { font-size: 16px; min-height: 42px; } /* меньше 16px iOS приближает страницу при фокусе */
  .toolbar select { flex: 1 1 40%; }
  .grid-side { grid-template-columns: minmax(0, 1fr); }
  .grid-side > aside { order: -1; }
  .filters .wide { grid-column: auto; }
  .drawer { width: 100%; }
  .d-meta { grid-template-columns: 1fr 1fr; }

  .table-wrap.cards { border: 0; overflow: visible; }
  .table-wrap.tall { max-height: none; }
  .cards table, .cards tbody { display: block; }
  .cards thead { display: none; }
  .cards tr {
    display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px;
    padding: 10px 12px; margin-bottom: 8px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  }
  .cards td { display: block; padding: 0; border: 0; }
  .cards td small { display: inline; }
  .cards .c-amount { order: 1; flex: 1 1 auto; font-size: 17px; font-weight: 650; text-align: left; }
  .cards .c-status { order: 2; text-align: right; }
  .cards .c-req { order: 3; flex: 1 1 60%; }
  .cards .c-act { order: 4; }
  .cards .c-id, .cards .c-date, .cards .c-partner, .cards .c-user { order: 5; font-size: 12.5px; color: var(--muted); font-weight: 400; }
  .cards .c-partner small::before { content: " · "; }
  .cards tr.empty-row { display: block; }
  .cards tr[hidden] { display: none; }
  .scroll-x table { min-width: 680px; }
}
