:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #f8f9f7;
  --line: #dfe3dc;
  --line-strong: #c9cfc5;
  --text: #1d2420;
  --muted: #66706a;
  --primary: #1f5c4a;
  --primary-ink: #ffffff;
  --primary-soft: #e3efe9;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --danger: #a3261d;
  --danger-soft: #fbe5e2;
  --ok: #1e6b3a;
  --ok-soft: #e1f2e6;
  --info: #22527a;
  --info-soft: #e2edf6;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(20, 30, 25, .06), 0 1px 1px rgba(20, 30, 25, .04);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 var(--font); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 4px; line-height: 1.25; }
h2 { font-size: 17px; margin: 0 0 10px; }
h3 { font-size: 15px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre-line; }

/* ---------- шапка ---------- */
.topbar { background: var(--primary); color: var(--primary-ink); }
.topbar-inner { max-width: 1320px; margin: 0 auto; padding: 0 16px; display: flex; align-items: center; gap: 4px; flex-wrap: wrap; min-height: 52px; }
.brand { font-weight: 700; color: #fff; margin-right: 12px; letter-spacing: .2px; white-space: nowrap; }
.brand span { opacity: .7; font-weight: 500; }
.nav { display: flex; flex-wrap: wrap; gap: 2px; align-items: center; flex: 1; }
.nav > a, .nav summary { color: #fff; padding: 7px 10px; border-radius: 6px; cursor: pointer; list-style: none; white-space: nowrap; font-size: 14px; }
.nav > a:hover, .nav summary:hover, .nav > a.active { background: rgba(255,255,255,.14); text-decoration: none; }
.nav summary::-webkit-details-marker { display: none; }
.nav summary::after { content: " ▾"; opacity: .7; font-size: 11px; }
.nav details { position: relative; }
.nav details[open] > summary { background: rgba(255,255,255,.14); }
.menu { position: absolute; top: calc(100% + 4px); left: 0; z-index: 20; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.12); min-width: 220px; padding: 6px; }
.menu a { display: block; padding: 8px 10px; border-radius: 6px; color: var(--text); }
.menu a:hover { background: var(--surface-2); text-decoration: none; }
.userbox { display: flex; align-items: center; gap: 8px; font-size: 13px; color: rgba(255,255,255,.9); }
.userbox a, .userbox button { color: #fff; opacity: .85; background: none; border: 0; font: inherit; cursor: pointer; padding: 4px 6px; }
.userbox a:hover, .userbox button:hover { opacity: 1; text-decoration: underline; }

/* ---------- раскладка ---------- */
.page { max-width: 1320px; margin: 0 auto; padding: 18px 16px 48px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; }
.card > h2:first-child { margin-top: 0; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 10px; }

/* ---------- сообщения ---------- */
.messages { list-style: none; padding: 0; margin: 0 0 14px; }
.messages li { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 8px; white-space: pre-line; border: 1px solid transparent; }
.msg-success { background: var(--ok-soft); color: var(--ok); border-color: #c5e3cf; }
.msg-error { background: var(--danger-soft); color: var(--danger); border-color: #f1c6c0; }
.msg-warning { background: var(--warn-soft); color: var(--warn); border-color: #f2d9ab; }
.msg-info { background: var(--info-soft); color: var(--info); }

/* ---------- кнопки и формы ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line-strong); background: var(--surface); color: var(--text); padding: 7px 13px; border-radius: 6px; font: inherit; font-size: 14px; cursor: pointer; white-space: nowrap; line-height: 1.3; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: #184a3b; }
.btn-danger { color: var(--danger); border-color: #e6b3ad; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-warn { color: var(--warn); border-color: #e9cf9f; }
.btn-sm { padding: 4px 9px; font-size: 13px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.actions form { margin: 0; }

label { font-size: 13px; color: var(--muted); display: block; margin-bottom: 3px; }
input[type=text], input[type=search], input[type=number], input[type=date], input[type=password], select, textarea {
  width: 100%; padding: 7px 9px; border: 1px solid var(--line-strong); border-radius: 6px; font: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(31, 92, 74, .35); outline-offset: 0; border-color: var(--primary); }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--primary); vertical-align: middle; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px 16px; }
.form-grid .wide { grid-column: 1 / -1; }
.field .help { font-size: 12px; color: var(--muted); margin-top: 3px; }
.field .errors { color: var(--danger); font-size: 13px; margin-top: 3px; }
.field ul { list-style: none; padding: 0; margin: 4px 0 0; columns: 2; }
.field ul label { color: var(--text); font-size: 14px; display: flex; gap: 6px; align-items: center; margin: 3px 0; }
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.filters > div { min-width: 150px; }
.filters .grow { flex: 1; min-width: 200px; }

/* ---------- таблицы ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table.t { width: 100%; border-collapse: collapse; font-size: 14px; }
.t th, .t td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.t thead th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; font-size: 13px; color: var(--muted); z-index: 1; white-space: nowrap; }
.t tbody tr:hover td { background: #fafbf9; }
.t tr.group td, .t tr.group th { background: var(--primary-soft); font-weight: 600; color: var(--primary); border-bottom-color: #cfe1d8; }
.t tr.group.sub td, .t tr.group.sub th { background: #eef5f1; }
.t tfoot td { font-weight: 600; background: var(--surface-2); }
.t td.qty-input { width: 120px; }
.t td.qty-input input { text-align: right; }
.t .deficit td { background: var(--warn-soft) !important; }
.t .neg { color: var(--danger); }
.t .pos { color: var(--ok); }
.empty { padding: 22px; text-align: center; color: var(--muted); }

/* ---------- статусы ---------- */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; border: 1px solid transparent; }
.st-draft { background: #eceeea; color: #4c544f; }
.st-pending { background: var(--warn-soft); color: var(--warn); }
.st-shipped { background: var(--info-soft); color: var(--info); }
.st-posted { background: var(--ok-soft); color: var(--ok); }
.st-rejected { background: var(--danger-soft); color: var(--danger); }
.st-reversed { background: #efe9f5; color: #5b3b7d; text-decoration: line-through; }
.role { background: rgba(255,255,255,.18); padding: 2px 8px; border-radius: 999px; }

/* ---------- карточки задач ---------- */
.task-list { list-style: none; padding: 0; margin: 0; }
.task-list li { padding: 8px 0; border-bottom: 1px solid var(--line); display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.task-list li:last-child { border-bottom: 0; }
.kpi { font-size: 26px; font-weight: 700; }
.create-links { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- документ ---------- */
.doc-meta { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px 20px; }
.doc-meta div b { display: block; font-size: 12px; color: var(--muted); font-weight: 500; }
.timeline { list-style: none; padding: 0; margin: 0; font-size: 13px; }
.timeline li { padding: 6px 0; border-bottom: 1px dashed var(--line); }
.inline-reason { display: flex; gap: 6px; align-items: center; }
.inline-reason input { width: 240px; }

/* ---------- подбор (множественный выбор) ---------- */
.picker-bar { position: sticky; top: 0; z-index: 5; background: var(--surface); padding: 10px 0; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; border-bottom: 1px solid var(--line); margin-bottom: 10px; }
.picker-bar input[type=search] { max-width: 300px; }
.picker-bar select { max-width: 260px; }
.picker-bar .count { font-weight: 700; }
.t tr.pk-group-head th { background: var(--primary-soft); color: var(--primary); cursor: pointer; }
.t tr.pk-group-head.sub th { background: #eef5f1; }
.t tr.pk-row.checked td { background: #f0f7f3; }
.t tr.pk-row td:first-child, .t tr.pk-group-head th:first-child { width: 36px; text-align: center; }
.hidden { display: none !important; }

/* ---------- вход ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }
.login-card h1 { margin-bottom: 14px; }

/* ---------- печать ---------- */
.print-page { background: #fff; max-width: 820px; margin: 0 auto; padding: 24px; font-size: 13px; }
.print-page h1 { font-size: 18px; text-align: center; }
.print-page table { width: 100%; border-collapse: collapse; margin: 12px 0; }
.print-page th, .print-page td { border: 1px solid #333; padding: 4px 6px; }
.print-page .sign { display: grid; grid-template-columns: 1fr 1fr; gap: 28px 40px; margin-top: 28px; }
.print-page .sign div { border-top: 1px solid #333; padding-top: 3px; font-size: 12px; }
@media print {
  .no-print { display: none !important; }
  body { background: #fff; }
  .print-page { padding: 0; }
}

@media (max-width: 720px) {
  body { font-size: 15px; }
  .page { padding: 12px 10px 40px; }
  .card { padding: 12px; }
  .brand { width: 100%; padding-top: 10px; }
  /* меню раскрывается на всю ширину под шапкой, а не от пункта — иначе уезжает за правый край */
  .topbar-inner { position: relative; }
  .nav details { position: static; }
  .menu { left: 10px; right: 10px; top: auto; margin-top: 4px; min-width: 0; }
  .userbox { width: 100%; padding-bottom: 8px; }
  .picker-bar input[type=search], .picker-bar select { max-width: none; }
  .t th, .t td { padding: 7px 6px; }
  .t td.qty-input { width: 92px; }
  .field ul { columns: 1; }
  .inline-reason { flex-wrap: wrap; }
  .inline-reason input { width: 100%; }
}
