:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #f8f9f6;
  --line: #e2e5dd;
  --text: #1d211b;
  --muted: #69705f;
  --accent: #137333;
  --accent-soft: #e6f4ea;
  --accent-text: #ffffff;
  --warn: #9a5b00;
  --warn-soft: #fff4de;
  --error: #b3261e;
  --error-soft: #fdecea;
  --info: #1a5fb4;
  --info-soft: #e8f0fb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 30, 10, .06), 0 4px 16px rgba(20, 30, 10, .06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121411;
    --surface: #1b1e19;
    --surface-2: #22261f;
    --line: #30362c;
    --text: #e6e9e1;
    --muted: #9ba392;
    --accent: #5cc27a;
    --accent-soft: #1d3324;
    --accent-text: #0b1f10;
    --warn: #f0b659;
    --warn-soft: #362a14;
    --error: #f2877f;
    --error-soft: #3a1c1a;
    --info: #8ab4f8;
    --info-soft: #1b2738;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button, input, select, textarea { font: inherit; color: inherit; }
code { font-size: .9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
[hidden] { display: none !important; }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-danger { color: var(--error); }
.btn-block { width: 100%; }
.btn-icon { min-width: 38px; padding: 0 10px; font-size: 18px; }

/* ---------- шапка ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-left));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 18px; color: var(--text); text-decoration: none; white-space: nowrap; }
.brand-sub { color: var(--accent); font-weight: 600; }
.status { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; min-width: 0; }
.top-actions { display: flex; gap: 4px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px; font-size: 13px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--line);
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none; }
.pill.ok { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.pill.bad { color: var(--error); background: var(--error-soft); border-color: transparent; cursor: pointer; }
.pill.busy { color: var(--info); background: var(--info-soft); border-color: transparent; }

/* ---------- раскладка ---------- */
.layout {
  display: grid; grid-template-columns: 360px 1fr;
  height: calc(100vh - 59px); height: calc(100dvh - 59px);
}
.sidebar {
  display: flex; flex-direction: column; gap: 10px;
  padding: 14px; border-right: 1px solid var(--line);
  overflow-y: auto; background: var(--bg);
}
.detail { overflow-y: auto; padding: 18px 22px 40px; }

/* ---------- загрузка ---------- */
.upload {
  display: flex; flex-direction: column; gap: 4px; padding: 16px; width: 100%;
  border: 2px dashed var(--accent); border-radius: var(--radius);
  background: var(--accent-soft); cursor: pointer; text-align: center; font: inherit; color: inherit;
}

/* Форма «Новые чеки»: выбранные фото */
.picked { display: flex; gap: 8px; flex-wrap: wrap; }
.picked-empty { color: var(--muted); font-size: 14px; padding: 6px 0; }
.picked-item {
  position: relative; margin: 0; width: 92px; display: flex; flex-direction: column; gap: 3px;
}
.picked-item img { width: 92px; height: 122px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); background: var(--surface-2); }
.picked-item figcaption { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.picked-item button {
  position: absolute; top: 4px; width: 26px; height: 26px; border-radius: 50%; border: 0; cursor: pointer;
  background: rgba(0, 0, 0, .6); color: #fff; font-size: 13px; line-height: 26px; padding: 0;
}
.picked-del { right: 4px; }
.picked-up { left: 4px; }
.mode { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin: 0; display: flex; flex-direction: column; gap: 8px; }

/* Поле с подсказками (объект) */
.combo { position: relative; }
.combo input { width: 100%; padding-right: 36px; }
.combo-toggle {
  position: absolute; right: 2px; top: 2px; bottom: 2px; width: 32px; border: 0; background: transparent;
  color: var(--muted); cursor: pointer; font-size: 14px;
}
.combo-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 30; margin: 0; padding: 4px;
  list-style: none; max-height: 240px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
}
.combo-list li { padding: 8px 10px; border-radius: 7px; cursor: pointer; }
.combo-list li:hover, .combo-list li.active { background: var(--accent-soft); color: var(--accent); }

.object-filter { width: 100%; }
.upload-title { font-weight: 700; font-size: 17px; color: var(--accent); }
.upload-hint { font-size: 13px; color: var(--muted); }
.upload-progress { font-size: 13px; color: var(--info); background: var(--info-soft); padding: 8px 12px; border-radius: 10px; }

.drop-overlay {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: rgba(19, 115, 51, .18); backdrop-filter: blur(2px);
  font-size: 22px; font-weight: 700; color: var(--accent);
}
.drop-overlay > div { background: var(--surface); padding: 24px 32px; border-radius: 16px; box-shadow: var(--shadow); border: 2px dashed var(--accent); }

/* ---------- фильтры и список ---------- */
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  padding: 4px 12px; border-radius: 999px; font-size: 13px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip:disabled { opacity: .45; cursor: default; }
.kind { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.kind-label { font-size: 14px; color: var(--muted); }
.chip .count { opacity: .7; margin-left: 4px; }

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.item {
  display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center;
  padding: 8px 10px; border-radius: 10px; cursor: pointer;
  background: var(--surface); border: 1px solid transparent;
}
.item:hover { border-color: var(--line); }
.item.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.item img { width: 44px; height: 58px; object-fit: cover; border-radius: 6px; background: var(--surface-2); }
.item-main { min-width: 0; }
.item-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-side { text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.item-sum { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty-list { color: var(--muted); text-align: center; padding: 24px 8px; font-size: 14px; }

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.QUEUED, .badge.PROCESSING { background: var(--info-soft); color: var(--info); }
.badge.WAITING { background: var(--warn-soft); color: var(--warn); }
.badge.RECOGNIZED { background: var(--warn-soft); color: var(--warn); }
.badge.ERROR { background: var(--error-soft); color: var(--error); }
.badge.EXPORTED { background: var(--accent-soft); color: var(--accent); }
.badge.clean { background: var(--accent-soft); color: var(--accent); }

/* ---------- чек ---------- */
.placeholder { display: grid; place-items: center; height: 100%; color: var(--muted); text-align: center; }
.placeholder h2 { color: var(--text); margin: 0 0 6px; }

.detail-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.detail-head h2 { margin: 0; font-size: 22px; }
.detail-head .spacer { flex: 1; }
.back { display: none; }

.detail-body { display: grid; grid-template-columns: minmax(240px, 32%) 1fr; gap: 20px; align-items: start; }

.photo { position: sticky; top: 0; display: flex; flex-direction: column; gap: 8px; }
.photo-frame {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; cursor: zoom-in; max-height: calc(100vh - 220px); display: flex; justify-content: center;
}
.photo-frame img { max-width: 100%; max-height: calc(100vh - 220px); object-fit: contain; display: block; }
.photo-tools { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.meta { font-size: 12px; color: var(--muted); }
/* Длинный чек: фото частей друг под другом, прокручиваются */
.pages { display: flex; flex-direction: column; gap: 10px; }
.pages.many { max-height: calc(100vh - 200px); overflow-y: auto; padding-right: 4px; }
.pages.many .photo-frame, .pages.many .photo-frame img { max-height: none; }
.page { display: flex; flex-direction: column; gap: 6px; }
.page-no { font-size: 13px; color: var(--muted); margin-right: auto; }
label.btn.disabled { opacity: .5; pointer-events: none; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.form { display: flex; flex-direction: column; gap: 14px; }

.alerts { display: flex; flex-direction: column; gap: 6px; }
.alert { padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.error { background: var(--error-soft); color: var(--error); }
.alert.info { background: var(--info-soft); color: var(--info); }
.alert.ok { background: var(--accent-soft); color: var(--accent); }
.alert ul { margin: 4px 0 0; padding-left: 20px; }

.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px 12px; }
.span-2 { grid-column: span 2; }
.span-4 { grid-column: 1 / -1; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field small { color: var(--muted); font-size: 12px; }
input, select, textarea {
  width: 100%; min-height: 38px; padding: 7px 10px;
  border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--surface); }
input.num { text-align: right; font-variant-numeric: tabular-nums; }
input.bad { border-color: var(--warn); background: var(--warn-soft); }

/* ---------- позиции ---------- */
/* Позиция в две строки: название во всю ширину, под ним артикул и числа */
.items-head, .item-row {
  display: grid; gap: 4px 6px; align-items: center;
  grid-template-columns: 24px minmax(0, 1fr) 62px 52px 84px 74px 92px 28px;
}
.item-row {
  grid-template-areas: "n name name name name name name del" ". article qty unit price disc amount .";
  padding-bottom: 8px; border-bottom: 1px solid var(--line);
}
.items-head { font-size: 12px; color: var(--muted); font-weight: 600; padding: 0 2px; }
.items-head .r { text-align: right; }
.items { display: flex; flex-direction: column; gap: 8px; }
.item-row .n { grid-area: n; color: var(--muted); font-size: 13px; text-align: right; }
.item-row .name { grid-area: name; }
.item-row .article { grid-area: article; font-size: 12px; color: var(--muted); }
.item-row .qty { grid-area: qty; }
.item-row .unit { grid-area: unit; }
.item-row .price { grid-area: price; }
.item-row .disc { grid-area: disc; }
.item-row .amount { grid-area: amount; font-weight: 600; }
.item-row .del { grid-area: del; min-height: 32px; padding: 0; border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 18px; }
.item-row .del:hover { color: var(--error); }

.totals {
  display: flex; gap: 14px; flex-wrap: wrap; align-items: center;
  padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-variant-numeric: tabular-nums;
}
.totals .ok { color: var(--accent); font-weight: 600; }
.totals .bad { color: var(--warn); font-weight: 600; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions .spacer { flex: 1; }

.processing { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 40px 16px; text-align: center; color: var(--muted); }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 4px solid var(--info-soft); border-top-color: var(--info); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- просмотр фото ---------- */
.viewer { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .9); display: flex; flex-direction: column; }
.viewer-tools { display: flex; gap: 8px; padding: 10px; justify-content: center; flex-wrap: wrap; }
.viewer-scroll { flex: 1; overflow: auto; display: grid; place-items: center; touch-action: pan-x pan-y pinch-zoom; }
.viewer-scroll img { display: block; max-width: none; }

/* ---------- настройки ---------- */
.dialog {
  width: min(620px, calc(100vw - 24px)); max-height: calc(100vh - 24px); padding: 0;
  border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--text);
}
.dialog::backdrop { background: rgba(0, 0, 0, .4); }
.settings { display: flex; flex-direction: column; gap: 14px; padding: 20px; }
.settings h2 { margin: 0; }
.settings h3 { margin: 0 0 8px; font-size: 15px; }
.settings section { display: flex; flex-direction: column; gap: 10px; padding-top: 12px; border-top: 1px solid var(--line); }
.steps { margin: 0; padding-left: 20px; color: var(--muted); font-size: 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.account { font-size: 14px; word-break: break-all; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; }
.check input { width: auto; min-height: 0; margin-top: 3px; }
.check-result { font-size: 14px; }
.check-result.ok { color: var(--accent); }
.check-result.bad { color: var(--error); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- вход ---------- */
body.locked .topbar, body.locked .layout { display: none; }
.auth { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: min(400px, 100%); display: flex; flex-direction: column; gap: 14px;
  padding: 28px 24px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; box-shadow: var(--shadow);
}
.auth-card h1 { margin: 0; font-size: 24px; }
.auth-brand { font-size: 16px; }
.auth-text { margin: 0; color: var(--muted); }
#authFields { display: flex; flex-direction: column; gap: 12px; }
.auth-note { margin: 0; font-size: 12px; color: var(--muted); }

.devices { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.devices li {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2);
}
.devices .device-main { flex: 1; min-width: 0; }
.devices .device-name { font-weight: 600; }
.devices .device-meta { font-size: 12px; color: var(--muted); }
.settings section > small { color: var(--muted); font-size: 12px; }
.server-info { margin: 0; padding-left: 18px; font-size: 14px; display: flex; flex-direction: column; gap: 3px; }
.server-info code { word-break: break-all; }
.bad-text { color: var(--error); }
.log-box { display: flex; flex-direction: column; gap: 6px; }
.log {
  margin: 0; max-height: 320px; overflow: auto; padding: 8px 10px;
  font: 11px/1.45 ui-monospace, Consolas, monospace; white-space: pre-wrap; word-break: break-word;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px;
}
.change-password { display: flex; flex-direction: column; gap: 10px; }
.change-password summary { cursor: pointer; color: var(--accent); font-weight: 600; margin-bottom: 8px; }
.change-password[open] > .field, .change-password[open] > .row { margin-top: 8px; }

/* ---------- уведомления ---------- */
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60;
  max-width: min(560px, calc(100vw - 32px)); padding: 12px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); box-shadow: var(--shadow); font-size: 14px;
}
.toast.error { background: var(--error); color: #fff; }

/* ---------- узкие экраны ---------- */
@media (max-width: 1250px) {
  .detail-body { grid-template-columns: 1fr; }
  .photo { position: static; }
  .photo-frame, .photo-frame img { max-height: 55vh; }
  .pages.many { max-height: none; overflow: visible; }
}

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .sidebar { border-right: 0; overflow: visible; }
  .detail { display: none; padding: 12px 16px 32px; overflow: visible; }
  body.has-receipt .sidebar { display: none; }
  body.has-receipt .detail { display: block; }
  .back { display: inline-flex; }
  .brand-sub { display: none; }
  .status { order: 3; flex-basis: 100%; }
  .topbar { flex-wrap: wrap; }
  .grid { grid-template-columns: 1fr 1fr; }
  .span-2 { grid-column: 1 / -1; }

  .items-head { display: none; }
  .item-row {
    grid-template-columns: 22px 1fr 1fr 1fr 32px;
    grid-template-areas: "n name name name del" ". article article article ." ". qty unit price ." ". disc disc amount .";
  }
  .item-row input.num::placeholder { text-align: right; }
}
