:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #d8dee4;
  --text: #1f2933;
  --muted: #7b8794;
  --accent: #2f6feb;
  --accent-soft: #e8f0fe;
  --head: #ffe599;
  --zebra: #f3f3f3;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}
.topbar {
  display: flex; align-items: center; gap: 6px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 6px 12px;
  position: sticky; top: 0; z-index: 20;
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 15px; margin-right: 10px; }
.spacer { flex: 1; }

/* user chip + logout in topbar */
.user-chip { display: flex; align-items: center; gap: 8px; margin-left: 10px; padding-left: 10px; border-left: 1px solid var(--border); }

/* import modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(16, 24, 40, 0.45); display: flex; align-items: flex-start; justify-content: center; z-index: 100; padding: 60px 16px; }
.modal-card { background: var(--panel); border-radius: 12px; box-shadow: 0 12px 40px rgba(16, 24, 40, 0.25); width: 720px; max-width: 100%; max-height: 80vh; overflow: auto; padding: 18px 20px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; font-size: 15px; margin-bottom: 12px; }
.modal-x { border: 0; background: transparent; font-size: 16px; cursor: pointer; color: var(--muted); }
.modal-x:hover { color: var(--text); }
.modal-label { font-weight: 600; font-size: 13px; display: block; margin-bottom: 4px; }
.modal-input { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font: inherit; box-sizing: border-box; }
.modal-input:focus { border-color: var(--accent); outline: none; }
.modal-actions { display: flex; align-items: center; gap: 10px; margin: 14px 0 6px; }
.imp-err { color: #c0392b; background: #fdecea; border: 1px solid #f5c6cb; border-radius: 8px; padding: 10px 12px; margin-top: 12px; }
.imp-map { margin: 12px 0; font-size: 12px; }
.imp-chip { display: inline-block; background: var(--accent-soft); color: #1c4ed8; border-radius: 10px; padding: 2px 8px; margin: 2px 3px 2px 0; font-size: 11px; }
.imp-prev-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; margin-top: 8px; }
table.imp-prev { border-collapse: collapse; width: 100%; font-size: 11px; }
table.imp-prev th, table.imp-prev td { border: 1px solid var(--border); padding: 4px 6px; text-align: left; white-space: pre-wrap; vertical-align: top; max-width: 220px; }
table.imp-prev th { background: var(--head); position: sticky; top: 0; }

/* login screen */
.login-wrap { min-height: 75vh; display: flex; align-items: center; justify-content: center; }
.login-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 6px 24px rgba(16, 24, 40, 0.08);
  padding: 28px 26px; width: 320px; display: flex; flex-direction: column; gap: 10px;
}
.login-brand { font-weight: 700; font-size: 17px; }
.login-card input {
  padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; font: inherit;
}
.login-card input:focus { border-color: var(--accent); outline: none; }
.login-err { color: #c0392b; font-size: 13px; min-height: 16px; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 7px;
  padding: 5px 11px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}
.tab:hover { background: var(--accent-soft); }
.tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.tabs.sys .tab { font-weight: 500; }

main { padding: 14px; }

.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.btn {
  border: 1px solid var(--border); background: var(--panel);
  border-radius: 7px; padding: 6px 12px; cursor: pointer; font-size: 13px;
}
.btn:hover { background: var(--accent-soft); }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.danger { color: #c0392b; }
.btn.small { padding: 3px 8px; font-size: 12px; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px; margin-bottom: 14px; }
.muted { color: var(--muted); }
h2 { margin: 0 0 10px; font-size: 17px; }
h3 { margin: 16px 0 8px; font-size: 14px; }

/* ===== Settlement table — dense ERP/CRM style ===== */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.05);
}

/* ===== Sticky grid layout (supplier view only) =====
   Топбар + панель з метриками + 3 рядки шапки (назви колонок, фільтри, підсумки)
   лишаються на місці; прокручуються лише рядки даних усередині .table-wrap. */
body.sup-view { height: 100vh; overflow: hidden; display: flex; flex-direction: column; }
/* Прокрутка таблиці вниз плавно ховає верхній бренд-бар (панель з метриками
   піднімається нагору й лишається фіксованою); повернення вгору — показує знову.
   Колапс керується інлайн через JS (max-height/padding/opacity) — це лишає лише
   плавний transition нижче; height на flex-елементі ігнорується, тому max-height. */
body.sup-view .topbar {
  flex: 0 0 auto;
  box-sizing: border-box;
  overflow: hidden;
  min-height: 0;
  transition: max-height .28s ease, opacity .22s ease,
              padding-top .28s ease, padding-bottom .28s ease, border-bottom-width .28s ease;
}
body.sup-view #app { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
body.sup-view .toolbar,
body.sup-view .bulk-bar,
body.sup-view .pager,
body.sup-view #app > p { flex: 0 0 auto; }
body.sup-view .table-wrap { flex: 1 1 auto; min-height: 0; overflow: auto; }
table.grid {
  border-collapse: collapse;
  table-layout: fixed;            /* column widths come from <colgroup>, set in JS */
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
table.grid th, table.grid td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 0; vertical-align: middle; overflow: hidden;
}
table.grid th:last-child, table.grid td:last-child { border-right: 0; }

/* header labels */
table.grid thead tr.labels th {
  background: var(--head);
  font-weight: 600; font-size: 11px; letter-spacing: 0.1px; color: #5b4a12;
  text-align: center; white-space: nowrap;
  height: 30px; padding: 0 8px 0 4px;
  position: sticky; top: 0; z-index: 6;
  border-bottom: 1px solid #e6cf76;
}
table.grid thead tr.labels th:hover { background: #ffdf80; }
.sort-ind { font-size: 9px; color: #6b5e2a; margin-left: 2px; }

/* header inner: drag grip (left) + sortable label + resize handle (right edge) */
.th-grip {
  display: inline-block; vertical-align: middle; margin-right: 2px;
  color: #b08d2e; font-size: 10px; line-height: 1; cursor: grab; opacity: 0;
  user-select: none; -webkit-user-select: none;
}
table.grid thead tr.labels th:hover .th-grip { opacity: 0.75; }
.th-grip:active { cursor: grabbing; }
.th-label { cursor: pointer; }
.th-label:hover { text-decoration: underline; }
.th-resize {
  position: absolute; top: 0; right: 0; width: 7px; height: 100%;
  cursor: col-resize; z-index: 2;
}
.th-resize:hover { background: rgba(47, 111, 235, 0.35); }
table.grid thead tr.labels th.th-dragging { opacity: 0.45; }
table.grid thead tr.labels th.drop-target { box-shadow: inset 3px 0 0 0 var(--accent); }
table.grid thead tr.labels th.th-actions { cursor: default; }
body.col-resizing, body.col-resizing * { cursor: col-resize !important; user-select: none; }

/* filter row */
table.grid tr.filters td {
  background: #fff; height: 24px; padding: 1px 4px;
  /* top is 2px less than the labels height (30) so this row tucks under the
     labels row, leaving no visible seam/gap between them while scrolling. */
  position: sticky; top: 28px; z-index: 5;
}
table.grid tr.filters input {
  width: 100%; border: 1px solid var(--border); border-radius: 5px;
  padding: 2px 6px; font-size: 11px; color: var(--text); background: #fff;
}
table.grid tr.filters input:focus { border-color: var(--accent); outline: none; }

/* totals row */
table.grid tr.totals td {
  background: #fff7da; font-weight: 700; color: #1f2933;
  text-align: right; height: 26px; padding: 4px 7px;
  /* tucks 2px under the filter row (which ends at 52 when stuck) — no seam/gap. */
  position: sticky; top: 50px; z-index: 4;
  border-bottom: 2px solid #e6cf76;
}
/* Compositing layer per sticky header cell so native <select> controls don't
   paint over the шапку while scrolling. (Transform on the whole <thead> instead
   makes it a containing block and opens sub-pixel gaps between the sticky rows.) */
table.grid thead tr.labels th,
table.grid tr.filters td,
table.grid tr.totals td { transform: translateZ(0); }

.hdr-metrics { color: var(--text); margin-left: 4px; }
.hdr-metrics b { color: var(--accent); }

/* body rows */
table.grid tbody tr:nth-child(even) td { background: var(--zebra); }
table.grid tbody tr:hover td { background: #eef4ff; }

/* cells / inputs */
table.grid td .cell {
  width: 100%; border: 0; background: transparent; padding: 4px 6px;
  font: inherit; color: inherit; outline: none; resize: none;
}
/* allow inputs to shrink below their default intrinsic width so columns can be narrow */
table.grid input { min-width: 0; box-sizing: border-box; }
table.grid td .cell:focus { background: var(--accent-soft); box-shadow: inset 0 0 0 2px var(--accent); border-radius: 2px; }

/* numeric cells: right-aligned, tabular figures, tight padding */
table.grid td.num, table.grid td.computed { text-align: right; }
table.grid td.num .cell { text-align: right; padding-left: 4px; padding-right: 6px; }
/* Нульові Ціна опт / Комісія % — підсвічена комірка #d5736d (ймовірно не заповнено). */
table.grid td.num.zero-warn { background: #d5736d !important; }
table.grid td.num .cell.zero-warn { color: #fff; font-weight: 600; background: transparent; }
/* Порожній Статус ТТН — підсвічена комірка (поверх зебри/hover). */
table.grid td.empty-warn { background: #d5736d !important; }
table.grid td.empty-warn .cell { color: #fff; background: transparent; }
table.grid td.computed, table.grid td.num.computed { padding: 4px 6px 4px 4px; color: #475467; }
table.grid td.computed .cell { color: var(--muted); background: transparent; }

table.grid td select.cell { appearance: auto; cursor: pointer; }

/* order (product) cell — clamp to ~3 lines, expand on focus, no permanent scroll */
table.grid td.order-cell { vertical-align: top; }
table.grid td.order-cell textarea.cell {
  height: 3.4em; line-height: 1.25; font-size: 11.5px; overflow: hidden; white-space: pre-wrap;
}
table.grid td.order-cell textarea.cell:focus { height: 6.5em; overflow: auto; }

.rowmenu { white-space: nowrap; text-align: center; }

/* leading columns: row number + selection checkbox */
table.grid th.lead-num, table.grid td.lead-num { width: 40px; min-width: 40px; text-align: center; color: var(--muted); font-size: 11px; padding: 0 4px; }
table.grid th.lead-sel, table.grid td.lead-sel { width: 30px; min-width: 30px; text-align: center; padding: 0; }
table.grid td.lead-sel input, table.grid th.lead-sel input { cursor: pointer; width: 15px; height: 15px; }
table.grid tbody tr.row-selected td { box-shadow: inset 0 0 0 999px rgba(255, 193, 7, 0.16); }
table.grid tbody tr.row-selected td.lead-num { color: #8a6d00; font-weight: 700; }

/* pagination footer */
.pager { display: flex; align-items: center; gap: 10px; margin-top: 10px; flex-wrap: wrap; font-size: 13px; }
.pager select { padding: 4px 7px; border: 1px solid var(--border); border-radius: 6px; font: inherit; }
.pager-nav { display: flex; align-items: center; gap: 8px; margin-left: auto; }

/* bulk-action bar */
.bulk-bar { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin: 8px 0; padding: 9px 12px; background: var(--accent-soft); border: 1px solid #bcd3fb; border-radius: 9px; }
.bulk-group { display: inline-flex; align-items: center; gap: 6px; }
.bulk-group label { font-weight: 600; font-size: 12px; }
.bulk-group select { padding: 4px 7px; border: 1px solid var(--border); border-radius: 6px; font: inherit; max-width: 200px; }

/* Status colours — !important to override zebra striping specificity */
table.grid td.st { font-weight: 600; }
table.grid td.st-novyi, table.grid td.st-novyi select { background: #6fa8dc !important; color: #fff !important; }
table.grid td.st-transit, table.grid td.st-transit select { background: #f1c232 !important; color: #1f2933 !important; }
table.grid td.st-arrived, table.grid td.st-arrived select { background: #3d85c6 !important; color: #fff !important; }
table.grid td.st-oplacheno, table.grid td.st-oplacheno select { background: #6aa84f !important; color: #fff !important; }
table.grid td.st-prodazh, table.grid td.st-prodazh select { background: #38761d !important; color: #fff !important; }
table.grid td.st-vidpost, table.grid td.st-vidpost select { background: #cc4125 !important; color: #fff !important; }
table.grid td.st-vidbalans, table.grid td.st-vidbalans select { background: #dd6633 !important; color: #fff !important; }
table.grid td.st-vidlegke, table.grid td.st-vidlegke select { background: #e69138 !important; color: #fff !important; }
table.grid td.st-vidzam, table.grid td.st-vidzam select { background: #a61c00 !important; color: #fff !important; }
table.grid td.st-util, table.grid td.st-util select { background: #434343 !important; color: #fff !important; }
table.grid td.st select.reason { color: #fff !important; background: #a61c00 !important; margin-top: 3px; }
/* Secondary status dropdown (Відміна - расход/баланс) under the primary status */
table.grid td.st select.status-second { display: block; margin-top: 3px; color: #1f2933 !important; background: #fff !important; }
table.grid td.st select.status-second.sel { background: #dd6633 !important; color: #fff !important; }

/* Payment cell */
table.grid td.pay-unpaid, table.grid td.pay-unpaid select { background: #f4cccc !important; }
table.grid td.pay-paid, table.grid td.pay-paid select { background: #d9ead3 !important; }
table.grid td.pay-provedeno, table.grid td.pay-provedeno select { background: #57bb8a !important; color: #fff !important; }
table.grid td.pay-nakladnij, table.grid td.pay-nakladnij select { background: #8e44ad !important; color: #fff !important; }
table.grid td.pay-vidmina, table.grid td.pay-vidmina select { background: #cc0000 !important; color: #fff !important; }

/* CRM link cell */
td.linkcell { white-space: nowrap; text-align: center; padding: 2px 4px; }
.crm-icon { text-decoration: none; font-size: 16px; }
.linkinput { width: 52px; }

/* Date cell as a 2×2 grid: row1 [date][📅], row2 [time][🕐]. Icons live in their own
   right column, so they never overlap the text and stay strictly stacked. */
td.datecell { width: 112px; min-width: 112px; max-width: 112px; padding: 4px 6px; vertical-align: middle; }
td.datecell .dt-grid {
  display: grid;
  grid-template-columns: 80px 14px;
  grid-template-rows: auto auto;
  column-gap: 4px; row-gap: 2px;
  align-items: center; justify-content: center; line-height: 1.15;
}
td.datecell .dt-grid input.cell {
  grid-column: 1; width: 100%; border: 0; background: transparent; outline: none;
  font-size: 12px; text-align: center; padding: 0; color: var(--text); min-width: 0;
}
td.datecell .dt-date { grid-row: 1; }
td.datecell .dt-time { grid-row: 2; color: #64748b; }
/* hide the native picker icons — we render our own in the grid's right column */
td.datecell input::-webkit-calendar-picker-indicator { display: none; }
td.datecell input::-webkit-inner-spin-button { display: none; }
td.datecell .dt-ico {
  grid-column: 2; width: 14px; height: 14px; padding: 0; border: 0; background: transparent;
  color: #94a3b8; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
td.datecell .dt-cal { grid-row: 1; }
td.datecell .dt-clock { grid-row: 2; }
td.datecell .dt-ico:hover { color: var(--accent); }
td.datecell .dt-grid input.cell:focus { background: var(--accent-soft); border-radius: 3px; }
.pay-amt { text-align: right; padding: 3px 6px; font-weight: 700; }
.pay-split { text-align: right; font-weight: 600; }

/* Налож cell */
table.grid td.nalozh-nakl, table.grid td.nalozh-nakl select { background: #f4cccc !important; }
table.grid td.nalozh-bez, table.grid td.nalozh-bez select { background: #d9ead3 !important; }

/* Stats */
table.stats { border-collapse: collapse; width: 100%; background: var(--panel); }
table.stats th, table.stats td { border: 1px solid var(--border); padding: 7px 10px; text-align: right; }
table.stats th { background: var(--head); text-align: center; }
table.stats td.b { text-align: left; font-weight: 600; }

/* Settings & suppliers forms */
.kv { display: grid; grid-template-columns: 260px 1fr; gap: 8px 12px; align-items: center; max-width: 760px; }
.kv label { font-weight: 600; }
.kv input { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; font: inherit; }
.suppliers-table { border-collapse: collapse; width: 100%; max-width: 900px; background: var(--panel); }
.suppliers-table th, .suppliers-table td { border: 1px solid var(--border); padding: 7px 9px; text-align: left; }
.suppliers-table th { background: var(--head); }
.pill { padding: 2px 8px; border-radius: 10px; font-size: 12px; }
.pill.on { background: #d9ead3; color: #38761d; }
.pill.off { background: #f4cccc; color: #990000; }
.field { display: flex; flex-direction: column; gap: 8px; max-width: 360px; }
.field input, .field select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; font: inherit; }
.row-inline { display: flex; gap: 8px; align-items: center; }
