:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #f0f2ee;
  --text: #1c2420;
  --muted: #5f6b64;
  --border: #dde2dc;
  --accent: #1f6f4a;
  --accent-ink: #ffffff;
  --accent-soft: #e3f1e9;
  --warn: #9a5b00;
  --warn-soft: #fff1d6;
  --danger: #b3261e;
  --danger-soft: #fde7e5;
  --info: #1d5fa8;
  --info-soft: #e3eefb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121614; --surface: #1b201d; --surface-2: #232a26; --text: #e6ebe7; --muted: #9aa69f;
    --border: #2e3732; --accent: #4fb985; --accent-ink: #0d1a13; --accent-soft: #1d3428;
    --warn: #f0b350; --warn-soft: #3a2c12; --danger: #f2877e; --danger-soft: #3d1c1a;
    --info: #79b0ee; --info-soft: #18293d; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #121614; --surface: #1b201d; --surface-2: #232a26; --text: #e6ebe7; --muted: #9aa69f;
    --border: #2e3732; --accent: #4fb985; --accent-ink: #0d1a13; --accent-soft: #1d3428;
    --warn: #f0b350; --warn-soft: #3a2c12; --danger: #f2877e; --danger-soft: #3d1c1a;
    --info: #79b0ee; --info-soft: #18293d; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0 0 12px; }
h2 { font-size: 16px; margin: 20px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.hidden { display: none !important; }

/* Каркас */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20; display: flex; align-items: center; gap: 10px;
  background: var(--accent); color: var(--accent-ink); padding: 10px 16px;
}
.topbar .brand { font-weight: 700; font-size: 16px; flex: 1; color: inherit; }
.topbar button { background: transparent; border: 0; color: inherit; font-size: 20px; padding: 4px 8px; cursor: pointer; position: relative; }
.badge-dot { position: absolute; top: 0; right: 0; background: #e5484d; color: #fff; border-radius: 9px; font-size: 11px; min-width: 18px; height: 18px; line-height: 18px; text-align: center; padding: 0 4px; }
.layout { display: flex; min-height: calc(100vh - 50px); }
.nav {
  width: 230px; flex: none; background: var(--surface); border-right: 1px solid var(--border); padding: 10px 0;
  overflow-y: auto;
}
.nav .sec { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 12px 16px 4px; }
.nav a { display: block; padding: 8px 16px; color: var(--text); border-left: 3px solid transparent; }
.nav a.active { background: var(--accent-soft); border-left-color: var(--accent); font-weight: 600; text-decoration: none; }
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav .user { padding: 12px 16px; border-top: 1px solid var(--border); margin-top: 10px; font-size: 13px; }
main { flex: 1; min-width: 0; padding: 16px; max-width: 1200px; }
#menuBtn { display: none; }
@media (max-width: 860px) {
  #menuBtn { display: inline-block; }
  .nav { position: fixed; top: 50px; left: 0; bottom: 0; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .nav.open { transform: none; }
  main { padding: 12px; }
}

/* Карточки и сетки */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.tiles { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); margin-bottom: 14px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; text-align: left; cursor: pointer; font: inherit; color: inherit; }
.tile:hover { border-color: var(--accent); }
.tile b { display: block; font-size: 15px; }
.tile span { font-size: 12px; color: var(--muted); }
.list-row { display: flex; justify-content: space-between; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); cursor: pointer; }
.list-row:first-of-type { border-top: 0; }
.stat { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* Таблицы */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { background: var(--surface-2); font-size: 12px; color: var(--muted); font-weight: 600; position: sticky; top: 0; white-space: nowrap; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--surface-2); }
tfoot td { font-weight: 700; background: var(--surface-2); }
tr.diff td { background: var(--warn-soft); }

/* Формы */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; margin-bottom: 12px; }
.toolbar .grow { flex: 1; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); min-width: 140px; }
.form-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); margin-bottom: 12px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 10px; min-height: 40px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
td input, td select { min-width: 80px; }
input.qty { max-width: 120px; text-align: right; }
.btn {
  font: inherit; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px;
  padding: 9px 14px; min-height: 40px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.check { display: flex; gap: 8px; align-items: center; color: var(--text); }
.check input { width: auto; min-height: auto; }

/* Статусы */
.st { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.st-draft { background: var(--surface-2); color: var(--muted); }
.st-posted, .st-accepted, .st-closed { background: var(--accent-soft); color: var(--accent); }
.st-sent { background: var(--info-soft); color: var(--info); }
.st-discrepancy { background: var(--warn-soft); color: var(--warn); }
.st-rejected, .st-cancelled { background: var(--danger-soft); color: var(--danger); }

.tabs { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 12px; padding-bottom: 2px; }
.tabs a { padding: 6px 12px; border-radius: 16px; background: var(--surface); border: 1px solid var(--border); color: var(--text); white-space: nowrap; font-size: 14px; }
.tabs a.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); text-decoration: none; }

/* Модальные окна и уведомления */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: flex; align-items: flex-start; justify-content: center; padding: 30px 12px; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 12px; padding: 18px; width: 100%; max-width: 640px; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 60; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; max-width: 90vw; box-shadow: var(--shadow); }
.toast.err { background: var(--danger); color: #fff; }
.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.info { background: var(--info-soft); color: var(--info); }
.login { max-width: 360px; margin: 10vh auto; }
.login .card { padding: 22px; }
.notif { padding: 10px 0; border-top: 1px solid var(--border); }
.notif.unread { font-weight: 600; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
@media print {
  .topbar, .nav, .actions, .toolbar { display: none !important; }
  main { padding: 0; }
}

/* Строки документа на телефоне: карточками */
@media (max-width: 640px) {
  #linesTable thead, #acceptTable thead { display: none; }
  #linesTable tr, #acceptTable tr { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; padding: 10px; border-bottom: 1px solid var(--border); position: relative; }
  #linesTable td, #acceptTable td { border: 0; padding: 0; text-align: left; }
  #linesTable td:first-child, #acceptTable td:first-child { grid-column: 1 / -1; padding-right: 44px; }
  #linesTable td[data-label]::before, #acceptTable td[data-label]::before { content: attr(data-label); display: block; font-size: 12px; color: var(--muted); }
  #linesTable td:first-child::before, #acceptTable td:first-child::before { display: none; }
  #linesTable td:last-child:not([data-label]) { position: absolute; top: 10px; right: 10px; }
  #linesTable tfoot tr { display: flex; justify-content: space-between; }
  #linesTable input.qty, #acceptTable input.qty { max-width: none; }
  #acceptTable td:last-child { grid-column: 1 / -1; }
}
.nav .user a { display: inline; padding: 0; border: 0; }
.nav .user a:hover { background: none; text-decoration: underline; }

/* v1.1: журналы, задания на приёмку, выбор складов у пользователя */
.head-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.head-row h1 { margin: 0; }
.crumbs { font-size: 13px; margin-bottom: 6px; }
.nav-count { display: inline-block; margin-left: 6px; background: var(--info); color: var(--surface); border-radius: 9px; font-size: 11px; min-width: 18px; height: 18px; line-height: 18px; text-align: center; padding: 0 5px; font-weight: 700; }
.alert.task { background: var(--info-soft); color: var(--text); display: flex; flex-wrap: wrap; align-items: center; gap: 8px; border: 1px solid var(--info); }
fieldset.multi { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; margin: 0 0 10px; display: grid; gap: 6px; }
fieldset.multi legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
.st-deleted { background: var(--danger-soft); color: var(--danger); text-decoration: line-through; }
