:root {
  --bg: #f4f5f7; --surface: #ffffff; --surface2: #f0f2f5; --text: #1c2230; --muted: #667085;
  --border: #dfe3ea; --accent: #2c5bc4; --accent-text: #ffffff; --accent-soft: #e6edfb;
  --pos: #1a7544; --pos-soft: #e3f3ea; --neg: #b03a32; --neg-soft: #f9e8e6;
  --warn: #8a6100; --warn-soft: #fbf1d9; --shadow: 0 1px 2px rgba(20, 30, 50, .06);
  --radius: 8px; --nav-h: 60px; --side-w: 232px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171d; --surface: #1c2028; --surface2: #242934; --text: #e5e8ee; --muted: #9aa3b2;
    --border: #2f3541; --accent: #6b93f0; --accent-text: #0e1522; --accent-soft: #232f4a;
    --pos: #5cc489; --pos-soft: #1c3027; --neg: #f08a80; --neg-soft: #3a2422;
    --warn: #e6b94d; --warn-soft: #352c14; --shadow: none;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 var(--font);
  overflow-x: hidden; min-width: 320px;
}
body.noscroll { overflow: hidden; }
h1, h2, h3 { margin: 0; font-weight: 600; line-height: 1.25; }
h1 { font-size: 20px; } h2 { font-size: 16px; } h3 { font-size: 14px; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.grow { flex: 1 1 auto; min-width: 0; }
.wrap { overflow-wrap: anywhere; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.pos { color: var(--pos); } .neg { color: var(--neg); }
.nowrap { white-space: nowrap; }

/* ---------- каркас ---------- */
.shell { display: grid; grid-template-columns: var(--side-w) minmax(0, 1fr); min-height: 100vh; }
.side {
  position: sticky; top: 0; height: 100vh; overflow-y: auto; background: var(--surface);
  border-right: 1px solid var(--border); padding: 14px 10px; display: flex; flex-direction: column; gap: 4px;
}
.brand { font-weight: 700; font-size: 18px; padding: 4px 10px 10px; letter-spacing: .2px; }
.side .navgroup { margin: 12px 10px 4px; font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: .6px; }
.navlink {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 6px; color: var(--text);
  cursor: pointer; border: 0; background: none; width: 100%; text-align: left;
}
.navlink:hover { background: var(--surface2); text-decoration: none; }
.navlink.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.count {
  margin-left: auto; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--neg);
  color: #fff; font-size: 12px; font-weight: 600; text-align: center; line-height: 20px;
}
.side .who { margin-top: auto; padding: 10px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; }
.main { min-width: 0; padding: 20px 24px 40px; max-width: 1180px; width: 100%; }
.topbar { display: none; }
.bottom { display: none; }

.pagehead { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.pagehead h1 { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
.crumbs { font-size: 12px; color: var(--muted); margin-bottom: 4px; }

/* ---------- элементы ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; margin-bottom: 14px; min-width: 0;
}
.card > h2, .card > .cardhead { margin-bottom: 10px; }
.cardhead { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cardhead h2 { flex: 1 1 auto; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); margin-bottom: 14px; }
.grid > .card { margin: 0; }
.kpi .label { color: var(--muted); font-size: 12px; }
.kpi .value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; overflow-wrap: anywhere; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 36px; padding: 6px 14px;
  border: 1px solid var(--border); border-radius: 6px; background: var(--surface); cursor: pointer;
  text-decoration: none !important; color: var(--text); white-space: nowrap;
}
.btn:hover { background: var(--surface2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--neg); border-color: var(--neg); }
.btn.good { color: var(--pos); border-color: var(--pos); }
.btn.small { min-height: 30px; padding: 3px 10px; font-size: 13px; }
.btn.link { border: 0; background: none; color: var(--accent); padding: 4px 6px; min-height: 0; }
.btn[disabled] { opacity: .55; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; line-height: 18px;
  background: var(--surface2); color: var(--muted); white-space: nowrap;
}
.badge.ok { background: var(--pos-soft); color: var(--pos); }
.badge.bad { background: var(--neg-soft); color: var(--neg); }
.badge.wait { background: var(--warn-soft); color: var(--warn); }
.badge.acc { background: var(--accent-soft); color: var(--accent); }

.notice { padding: 10px 12px; border-radius: 6px; background: var(--accent-soft); margin-bottom: 12px; overflow-wrap: anywhere; }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.bad { background: var(--neg-soft); color: var(--neg); }
.notice.ok { background: var(--pos-soft); color: var(--pos); }
.empty { text-align: center; padding: 28px 12px; color: var(--muted); }
.empty b { display: block; color: var(--text); margin-bottom: 4px; font-weight: 600; }
.loading { padding: 28px; text-align: center; color: var(--muted); }
.loading::before {
  content: ""; display: inline-block; width: 14px; height: 14px; margin-right: 8px; vertical-align: -2px;
  border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .loading::before { animation: none; } }

/* ---------- формы ---------- */
.field { margin-bottom: 12px; min-width: 0; }
.field > label, .field > .lbl { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field .hint { font-size: 12px; color: var(--muted); margin-top: 3px; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=search], input[type=file], select, textarea {
  width: 100%; min-height: 38px; padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); min-width: 0;
}
input[type=file] { padding: 5px; }
textarea { min-height: 70px; resize: vertical; }
input:disabled, select:disabled { opacity: .6; }
.check { display: flex; gap: 8px; align-items: center; margin: 6px 0; cursor: pointer; }
.check input { width: 18px; height: 18px; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.cols > * { min-width: 0; }
.seg { display: flex; flex-wrap: wrap; gap: 4px; }
.seg label {
  flex: 1 1 auto; text-align: center; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px;
  cursor: pointer; background: var(--surface); font-size: 13px; color: var(--text); margin: 0;
}
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.seg label.on.inc { background: var(--pos-soft); border-color: var(--pos); color: var(--pos); }
.seg label.on.exp { background: var(--neg-soft); border-color: var(--neg); color: var(--neg); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 12px; }
.filters .field { margin: 0; flex: 1 1 130px; max-width: 220px; }
.formerr { margin: 8px 0 0; }

/* ---------- таблицы и карточки ---------- */
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th {
  text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 6px 8px;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
.tbl td { padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; overflow-wrap: anywhere; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl th.num, .tbl td.num { text-align: right; }
.tbl tbody tr.click { cursor: pointer; }
.tbl tbody tr.click:hover { background: var(--surface2); }
.tbl tfoot td { font-weight: 600; border-top: 1px solid var(--border); }

/* лента операций */
.erows { margin: 0 -14px; }
.erow {
  display: grid; grid-template-columns: 86px minmax(0, 1fr) minmax(0, 150px) 150px 112px auto; gap: 10px;
  align-items: center; padding: 8px 14px; border-top: 1px solid var(--border); cursor: pointer;
}
.erow:first-child { border-top: 0; }
.erow:hover { background: var(--surface2); }
.erow .e-main { min-width: 0; }
.erow .e-title { font-weight: 500; overflow-wrap: anywhere; }
.erow .e-sub { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.erow .e-acc { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.erow .e-amt { font-weight: 600; }
.erow .e-act { display: flex; gap: 6px; }
.erow.head { cursor: default; font-size: 12px; color: var(--muted); font-weight: 600; padding-top: 4px; padding-bottom: 4px; }
.erow.head:hover { background: none; }
.erow.rej { background: linear-gradient(0deg, transparent, transparent); }
.more { text-align: center; padding-top: 12px; }

.kv { display: grid; grid-template-columns: minmax(110px, 160px) minmax(0, 1fr); gap: 6px 12px; }
.kv dt { color: var(--muted); font-size: 12px; padding-top: 2px; }
.kv dd { margin: 0; overflow-wrap: anywhere; }
dl.kv { margin: 0; }

/* матрица прав */
.matrix-wrap { overflow: auto; max-height: calc(100vh - 230px); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.matrix { border-collapse: separate; border-spacing: 0; }
.matrix th, .matrix td { padding: 4px 6px; border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); background: var(--surface); }
.matrix thead th {
  position: sticky; top: 0; z-index: 3; font-size: 12px; font-weight: 600; min-width: 128px; max-width: 150px;
  vertical-align: bottom; overflow-wrap: anywhere; text-align: left;
}
.matrix thead th:first-child, .matrix tbody th { position: sticky; left: 0; z-index: 2; min-width: 150px; max-width: 170px; text-align: left; font-weight: 500; }
.matrix thead th:first-child { z-index: 4; }
.matrix select { min-height: 30px; padding: 2px 4px; font-size: 13px; }
.matrix select.lv-full { background: var(--accent-soft); font-weight: 600; }
.matrix select.lv-view { background: var(--pos-soft); }
.matrix select.lv-none { color: var(--muted); }
.matrix select.saving { outline: 2px solid var(--warn); }
.matrix select.saved { outline: 2px solid var(--pos); }
.matrix select.failed { outline: 2px solid var(--neg); }
.permlist .permrow { display: grid; grid-template-columns: minmax(0, 1fr) 150px; gap: 8px; align-items: center; padding: 6px 0; border-top: 1px solid var(--border); }

.tabs { display: flex; gap: 4px; margin-bottom: 14px; flex-wrap: wrap; }
.tabs a { padding: 6px 12px; border-radius: 16px; color: var(--text); background: var(--surface); border: 1px solid var(--border); }
.tabs a.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; text-decoration: none; }

.audit-diff { font-size: 12px; font-family: ui-monospace, Consolas, monospace; overflow-wrap: anywhere; }
.audit-diff .old { color: var(--neg); text-decoration: line-through; }
.audit-diff .new { color: var(--pos); }

/* ---------- диалоги ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50; background: rgba(10, 14, 22, .5); display: flex;
  align-items: center; justify-content: center; padding: 16px;
}
.modal {
  background: var(--surface); border-radius: 10px; width: 100%; max-width: 480px; max-height: calc(100vh - 32px);
  display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0, 0, 0, .35); border: 1px solid var(--border);
}
.modal.wide { max-width: 680px; }
.modal-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px 8px; }
.modal-head h2 { flex: 1 1 auto; overflow-wrap: anywhere; }
.modal-x { border: 0; background: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); padding: 2px 6px; }
.modal-body { padding: 4px 16px 12px; overflow-y: auto; overflow-x: hidden; min-height: 0; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; padding: 10px 16px 14px; border-top: 1px solid var(--border); }
.receipt-img { max-width: 100%; max-height: 320px; border-radius: 6px; border: 1px solid var(--border); display: block; margin-top: 6px; }
.codes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; font: 15px ui-monospace, Consolas, monospace; margin: 10px 0; }
.codes span { padding: 6px 8px; background: var(--surface2); border-radius: 6px; text-align: center; user-select: all; }
.qr { background: #fff; padding: 6px; border-radius: 6px; width: 200px; height: 200px; margin: 8px auto; }
.qr svg { width: 100%; height: 100%; display: block; }
.secret { font: 14px ui-monospace, Consolas, monospace; user-select: all; overflow-wrap: anywhere; text-align: center; }
.temp { font: 18px ui-monospace, Consolas, monospace; user-select: all; padding: 10px; background: var(--surface2); border-radius: 6px; text-align: center; overflow-wrap: anywhere; }

.toasts { position: fixed; z-index: 80; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; max-width: calc(100vw - 32px); }
.toast { padding: 10px 14px; border-radius: 6px; background: var(--text); color: var(--bg); box-shadow: 0 4px 16px rgba(0,0,0,.3); overflow-wrap: anywhere; }
.toast.bad { background: var(--neg); color: #fff; }

/* ---------- вход ---------- */
.authwrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.authbox { width: 100%; max-width: 380px; }
.authbox h1 { margin-bottom: 4px; }
.authbox .card { padding: 20px; }
.authbox .btn.primary { width: 100%; }

/* ---------- телефон ---------- */
.fab { display: none; }
@media (max-width: 899px) {
  .shell { display: block; }
  .side { display: none; }
  .topbar {
    display: flex; align-items: center; gap: 8px; position: sticky; top: 0; z-index: 20; background: var(--surface);
    border-bottom: 1px solid var(--border); padding: 10px 16px; min-height: 48px;
  }
  .topbar .brand { padding: 0; font-size: 16px; }
  .topbar .ttl { font-weight: 600; flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .main { padding: 14px 16px calc(var(--nav-h) + 28px + env(safe-area-inset-bottom)); }
  .bottom {
    display: grid; grid-template-columns: 1fr 1fr 1.25fr 1fr 1fr; align-items: end; position: fixed; z-index: 30;
    left: 0; right: 0; bottom: 0; background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom); height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  }
  .bottom .bi {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; height: var(--nav-h);
    color: var(--muted); font-size: 11px; border: 0; background: none; cursor: pointer; position: relative; padding: 0;
    text-decoration: none !important;
  }
  .bottom .bi .ic { font-size: 19px; line-height: 1; }
  .bottom .bi.active { color: var(--accent); font-weight: 600; }
  .bottom .bi .count { position: absolute; top: 4px; left: 54%; margin: 0; min-width: 18px; line-height: 18px; font-size: 11px; }
  .bottom .plus {
    justify-self: center; align-self: center; margin-top: -18px; height: 52px; padding: 0 22px; border-radius: 26px;
    border: 0; background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 15px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, .3); cursor: pointer; white-space: nowrap;
  }
  .desk-only { display: none !important; }
  h1 { font-size: 18px; }
  .erow { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "main amt" "meta stat" "act act"; gap: 2px 10px; padding: 10px 14px; }
  .erow.head { display: none; }
  .erow .e-date { grid-area: meta; font-size: 12px; color: var(--muted); }
  .erow .e-main { grid-area: main; }
  .erow .e-acc { display: none; }
  .erow .e-amt { grid-area: amt; text-align: right; align-self: start; }
  .erow .e-st { grid-area: stat; text-align: right; }
  .erow .e-act { grid-area: act; margin-top: 6px; }
  .erow .e-act:empty { display: none; }
  .erow .e-act .btn { flex: 1 1 0; }

  /* таблицы -> карточки */
  table.tbl.cards, .tbl.cards tbody, .tbl.cards tr, .tbl.cards td { display: block; width: 100%; }
  .tbl.cards thead { display: none; }
  .tbl.cards tr { padding: 10px 0; border-bottom: 1px solid var(--border); }
  .tbl.cards tr:last-child { border-bottom: 0; }
  .tbl.cards td { border: 0; padding: 2px 0; display: flex; justify-content: space-between; gap: 12px; text-align: right; }
  .tbl.cards td::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-align: left; flex: 0 0 auto; padding-top: 2px; }
  .tbl.cards td.first { display: block; text-align: left; font-weight: 600; font-size: 15px; padding-bottom: 4px; }
  .tbl.cards td.first::before, .tbl.cards td.nolabel::before { content: none; }
  .tbl.cards td.nolabel { display: block; text-align: left; }
  .tbl.cards tfoot { display: block; }
  .cols { grid-template-columns: 1fr; }
  .modal { max-width: none; }
  .overlay { align-items: flex-end; padding: 0; }
  .modal { border-radius: 14px 14px 0 0; max-height: 92vh; border-bottom: 0; }
  .modal-foot { padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .modal-foot .btn { flex: 1 1 auto; }
  input[type=text], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea { font-size: 16px; }
  .filters .field { max-width: none; flex: 1 1 calc(50% - 8px); }
  .toasts { left: 16px; right: 16px; bottom: calc(var(--nav-h) + 12px); }
  .kv { grid-template-columns: 1fr; gap: 0; }
  .kv dd { margin-bottom: 8px; }
  .matrix-wrap { max-height: none; }
}
@media (min-width: 900px) {
  .mob-only { display: none !important; }
}

.arow { padding: 8px 14px; border-top: 1px solid var(--border); }
.arow:first-child { border-top: 0; }
.arow .audit-diff { margin-top: 4px; padding: 6px 8px; background: var(--surface2); border-radius: 6px; }
@media (max-width: 899px) {
  .kpi .value { font-size: 20px; }
  .grid > .card.kpi { padding: 10px 14px; }
  .grid { gap: 8px; }
}
