/* ---------------------------------------------------------------- tokens */
:root {
  --paper: #F3F5F7;
  --surface: #FFFFFF;
  --surface-2: #F8F9FB;
  --ink: #1C2733;
  --ink-2: #4A5664;
  --muted: #6B7684;
  --line: #DCE1E7;
  --line-strong: #C3CBD4;
  --accent: #2C4C7A;
  --accent-ink: #FFFFFF;
  --accent-soft: #E3EAF4;
  /* Пара прибуток/збиток перевірена на дальтонізм; другий канал — напрям стовпчика і знак суми */
  --profit: #1F7A57;
  --loss: #B4372E;
  --warn-bg: #FBF1DC;
  --warn-ink: #6E4B0B;
  --err-bg: #F9E3E0;
  --err-ink: #8A2A22;
  --ok-bg: #E2F1EA;
  --ok-ink: #17573F;

  --font: "Onest", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --r-sm: 6px;
  --r-md: 10px;
  --shadow-pop: 0 10px 30px rgba(20, 30, 45, .16);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #12171D;
    --surface: #1A2129;
    --surface-2: #1F2731;
    --ink: #E6EBF0;
    --ink-2: #B5BFCA;
    --muted: #8B96A3;
    --line: #2B343F;
    --line-strong: #3A4552;
    --accent: #8FB0E0;
    --accent-ink: #0F1A2A;
    --accent-soft: #233348;
    --profit: #2A8F72;
    --loss: #D45F45;
    --warn-bg: #3A2F17;
    --warn-ink: #F0D49A;
    --err-bg: #3D2020;
    --err-ink: #F2B8AE;
    --ok-bg: #18342A;
    --ok-ink: #A9DEC5;
    --shadow-pop: 0 10px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}

/* ---------------------------------------------------------------- base */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.5 var(--font);
  font-variant-numeric: tabular-nums;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ---------------------------------------------------------------- controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 16px;
  border: 1px solid var(--line-strong); border-radius: var(--r-sm);
  background: var(--surface); cursor: pointer; font-weight: 500;
}
.btn:hover { border-color: var(--ink-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.08); border-color: var(--accent); }
.btn-danger { color: var(--loss); }
.btn-sm { min-height: 32px; padding: 0 10px; font-size: 14px; }
.btn-block { width: 100%; }
.link-btn { background: none; border: 0; padding: 0; color: var(--ink-2); cursor: pointer; text-align: left; }
.link-btn:hover { color: var(--ink); text-decoration: underline; }

.field { display: grid; gap: 6px; margin-bottom: 16px; }
.field > span { font-size: 14px; color: var(--ink-2); font-weight: 500; }
.field small { color: var(--muted); font-size: 13px; }
input:not([type=checkbox]), select {
  min-height: 40px; padding: 0 12px; width: 100%;
  border: 1px solid var(--line-strong); border-radius: var(--r-sm);
  background: var(--surface);
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input-unit { position: relative; }
.input-unit input { padding-right: 52px; }
.input-unit em { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); font-style: normal; color: var(--muted); font-size: 14px; }
.form-error { margin: -4px 0 14px; color: var(--err-ink); background: var(--err-bg); padding: 8px 12px; border-radius: var(--r-sm); font-size: 14px; }

/* ---------------------------------------------------------------- login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; padding: 32px 28px 28px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md);
}
.login-title { font-size: 34px; font-weight: 700; letter-spacing: -.02em; }
.login-sub { margin: 4px 0 28px; color: var(--muted); }

/* ---------------------------------------------------------------- shell */
.shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 24px;
  padding: 24px 16px; border-right: 1px solid var(--line); background: var(--surface);
}
.brand { font-size: 24px; font-weight: 700; letter-spacing: -.02em; padding: 0 10px; }
.nav { display: grid; gap: 2px; }
.nav a {
  display: block; padding: 8px 10px; border-radius: var(--r-sm);
  color: var(--ink-2); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--ink); }
.sidebar-foot { margin-top: auto; display: grid; gap: 8px; padding: 0 10px; font-size: 14px; }
.who { display: grid; margin-bottom: 4px; }
.who span:first-child { font-weight: 600; }
.who-role { color: var(--muted); font-size: 13px; }

.content { padding: 28px 32px 48px; min-width: 0; }
.topbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px 24px; }
.page-title { font-size: 26px; font-weight: 700; letter-spacing: -.015em; }
.period { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.presets { display: flex; flex-wrap: wrap; border: 1px solid var(--line-strong); border-radius: var(--r-sm); overflow: hidden; background: var(--surface); }
.presets button { border: 0; background: none; padding: 7px 12px; cursor: pointer; font-size: 14px; color: var(--ink-2); }
.presets button + button { border-left: 1px solid var(--line); }
.presets button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.range { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.range input { width: 150px; min-height: 36px; font-size: 14px; }

.data-status { margin: 10px 0 0; font-size: 13px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 16px; align-items: center; }
.data-status .live::before {
  content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--profit); margin-right: 6px; vertical-align: 1px;
}
.data-status .stale::before { background: var(--loss); }
.banner { margin-top: 16px; padding: 10px 14px; border-radius: var(--r-sm); background: var(--warn-bg); color: var(--warn-ink); font-size: 14px; }
.banner.err { background: var(--err-bg); color: var(--err-ink); }
.page { margin-top: 24px; }

/* ---------------------------------------------------------------- figures row */
.figures {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md);
  margin-bottom: 24px;
}
.figure { padding: 16px 20px; }
.figure + .figure { border-left: 1px solid var(--line); }
.figure dt { font-size: 13px; color: var(--muted); }
.figure dd { margin: 4px 0 0; font-size: 24px; font-weight: 600; letter-spacing: -.01em; white-space: nowrap; }
.figure dd small { font-size: 14px; font-weight: 500; color: var(--muted); margin-left: 4px; }
.figure.lead dd { font-size: 30px; }

/* ---------------------------------------------------------------- panels & tables */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md); }
.panel-head { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: baseline; justify-content: space-between; padding: 16px 20px 12px; }
.panel-head h2 { font-size: 17px; font-weight: 600; }
.panel-head p { margin: 0; color: var(--muted); font-size: 13px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 10px 10px; text-align: right; white-space: nowrap; border-top: 1px solid var(--line); }
th { white-space: normal; vertical-align: bottom; font-weight: 500; color: var(--muted); font-size: 13px; background: var(--surface-2); border-top: 0; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child { text-align: left; padding-left: 20px; }
th:last-child, td:last-child { padding-right: 20px; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--ink); }
th[aria-sort] { color: var(--ink); }
th[aria-sort="descending"]::after { content: " ↓"; }
th[aria-sort="ascending"]::after { content: " ↑"; }
td.text, th.text { text-align: left; }
td.wrap { white-space: normal; min-width: 190px; }
tr.row-click { cursor: pointer; }
tr.row-click:hover td { background: var(--surface-2); }
tr.row-open td { background: var(--surface-2); }
tfoot td { font-weight: 600; border-top: 2px solid var(--line-strong); }
.num-neg { }
.dim { color: var(--muted); }
.group-name { font-weight: 500; }
.group-sub { display: block; font-size: 12px; color: var(--muted); }

/* Головний елемент: стовпчик прибутку від нуля */
.bar-cell { width: 120px; min-width: 110px; padding-left: 8px; padding-right: 8px; }
.bar {
  position: relative; height: 18px;
  background: linear-gradient(var(--line-strong), var(--line-strong)) no-repeat center / 1px 100%;
}
.bar.all-pos { background-position: left center; }
.bar.all-neg { background-position: right center; }
.bar i { position: absolute; top: 3px; height: 12px; min-width: 2px; }
.bar i.pos { background: var(--profit); border-radius: 0 4px 4px 0; }
.bar i.neg { background: var(--loss); border-radius: 4px 0 0 4px; }
.bar-val { display: none; font-size: 13px; margin-top: 2px; }

.detail td { padding: 0; background: var(--surface-2); }
.detail-grid { white-space: normal; position: sticky; left: 0; width: var(--wrap-w, 100%); box-sizing: border-box; display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 0 32px; padding: 14px 20px 18px; }
.detail-grid h3 { font-size: 13px; font-weight: 600; color: var(--muted); margin: 4px 0 6px; }
.ledger { display: grid; grid-template-columns: 1fr auto; gap: 3px 16px; font-size: 14px; margin: 0; }
.ledger dt { color: var(--ink-2); }
.ledger dd { margin: 0; text-align: right; white-space: nowrap; }
.ledger .total { font-weight: 600; border-top: 1px solid var(--line-strong); padding-top: 4px; margin-top: 2px; }
.detail-actions { position: sticky; left: 0; padding: 0 20px 16px; }

/* категорії статусів: текст + колір, ніколи лише колір */
.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line); }
.chip.sale { background: var(--ok-bg); color: var(--ok-ink); border-color: transparent; }
.chip.return { background: var(--warn-bg); color: var(--warn-ink); border-color: transparent; }
.chip.transit { background: var(--accent-soft); color: var(--ink); border-color: transparent; }
.chip.defect { background: var(--err-bg); color: var(--err-ink); border-color: transparent; }

/* ---------------------------------------------------------------- P&L statement */
.statement { max-width: 640px; }
.statement table td { font-size: 15px; padding-top: 12px; padding-bottom: 12px; }
.statement tr.sub td { color: var(--ink-2); }
.statement tr.subtotal td { font-weight: 600; border-top: 2px solid var(--line-strong); }
.statement tr.result td { font-size: 20px; font-weight: 700; border-top: 2px solid var(--ink); }
.statement .factor { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); margin-left: 8px; }
.statement .factor input { width: 76px; min-height: 30px; padding: 0 8px; font-size: 13px; }
.two-col { display: grid; grid-template-columns: minmax(0, 640px) minmax(0, 1fr); gap: 24px; align-items: start; }

/* ---------------------------------------------------------------- forms */
.form-panel { padding: 20px; max-width: 760px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 20px; }
.form-section + .form-section { border-top: 1px solid var(--line); padding-top: 18px; margin-top: 4px; }
.form-section h2 { font-size: 15px; font-weight: 600; margin-bottom: 12px; }
.form-actions { display: flex; gap: 10px; align-items: center; padding-top: 8px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 16px; }
.toolbar .field { margin: 0; min-width: 220px; }

/* ---------------------------------------------------------------- empty / loading */
.empty { padding: 48px 24px; text-align: center; color: var(--ink-2); }
.empty h2 { font-size: 18px; margin-bottom: 6px; color: var(--ink); }
.skeleton { height: 340px; border-radius: var(--r-md); background: var(--surface); border: 1px solid var(--line); }

/* ---------------------------------------------------------------- dialog, toast, tip */
.dialog { border: 1px solid var(--line); border-radius: var(--r-md); padding: 24px; width: min(420px, calc(100vw - 32px)); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-pop); }
.dialog::backdrop { background: rgba(10, 16, 24, .45); }
.dialog h2 { font-size: 18px; margin-bottom: 16px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: var(--r-sm); font-size: 14px; box-shadow: var(--shadow-pop); z-index: 20; }
.tip { position: fixed; z-index: 30; pointer-events: none; background: var(--surface); color: var(--ink); border: 1px solid var(--line-strong); box-shadow: var(--shadow-pop); border-radius: var(--r-sm); padding: 8px 10px; font-size: 13px; line-height: 1.4; }
.tip b { font-weight: 600; }

/* ---------------------------------------------------------------- mobile */
@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px 16px; padding: 12px 16px; border-right: 0; border-bottom: 1px solid var(--line); }
  .brand { font-size: 20px; padding: 0; }
  .nav { order: 3; width: 100%; min-width: 0; display: flex; overflow-x: auto; gap: 4px; }
  .nav a { white-space: nowrap; }
  .sidebar-foot { margin: 0 0 0 auto; display: flex; gap: 12px; padding: 0; }
  .who, #btn-password { display: none; }
  .content { padding: 20px 16px 40px; }
  .figures { grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); overflow: hidden; }
  .figure { background: var(--surface); padding: 14px 16px; }
  .figure + .figure { border-left: 0; }
  .figure.lead { grid-column: 1 / -1; }
  .figure dd { font-size: 20px; }
  .figure.lead dd { font-size: 28px; }
  td.wrap { min-width: 150px; }
  .bar-cell { min-width: 130px; width: 130px; }
  .bar-val { display: block; }
  .two-col { grid-template-columns: minmax(0, 1fr); }
  .statement td.text { white-space: normal; }
  .range input { width: 140px; }
}

/* ---------------------------------------------------------------- прогноз */
/* Прогнозні колонки — окрема смуга з легким тлом, щоб факт і прогноз не плутались */
th.fc { background: var(--accent-soft); color: var(--ink-2); }
td.fc { background: color-mix(in srgb, var(--accent-soft) 45%, transparent); }
th.fc-first, td.fc-first { border-left: 2px solid var(--accent); }
tr.row-click:hover td.fc, tr.row-open td.fc { background: var(--accent-soft); }
.fc-figure { background: color-mix(in srgb, var(--accent-soft) 60%, var(--surface)); }
.detail-note { margin: 0 0 8px; font-size: 13px; color: var(--ink-2); max-width: 46ch; }

/* розкладка змінних витрат у P&L */
.statement tr.detail-line td { font-size: 14px; padding-top: 6px; padding-bottom: 6px; border-top-color: transparent; }
.statement tr.detail-line td:first-child { padding-left: 40px; }

.side-stack { display: grid; gap: 24px; align-content: start; }

/* ID з кнопкою копіювання */
.copy-id {
  font: inherit; color: inherit; background: none; border: 0; padding: 0 2px; margin: 0 -2px;
  cursor: copy; border-radius: 4px; user-select: text; font-variant-numeric: tabular-nums;
}
.copy-id:hover, .copy-id:focus-visible { color: var(--accent); background: var(--accent-soft); outline: none; }
.copy-icon { margin-left: 4px; opacity: .55; }
.copy-id:hover .copy-icon { opacity: 1; }

/* вкладки «Продажі / Тести» */
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--r-sm); overflow: hidden; background: var(--surface); margin-bottom: 16px; }
.seg button { border: 0; background: none; padding: 8px 16px; cursor: pointer; font: inherit; font-size: 14px; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); }
.seg-count { display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 4px; border-radius: 999px; font-size: 12px;
  background: var(--accent-soft); color: var(--ink); }
.seg button[aria-selected="true"] .seg-count, .presets button[aria-pressed="true"] .seg-count { background: rgba(255,255,255,.22); color: inherit; }
.chip-warn { margin-top: 4px; white-space: normal; }
.ledger dt.indent { padding-left: 12px; }

/* сторінка «Товари» */
.product-filter { display: inline-flex; margin-bottom: 16px; }
td.verdict { min-width: 190px; }
td.verdict .chip { white-space: nowrap; }
.trend.up { color: var(--profit); }
.trend.down { color: var(--loss); }
.footnote { margin: 12px 4px 0; font-size: 13px; color: var(--muted); max-width: 90ch; }

/* тестові акаунти в налаштуваннях */
.form-hint { margin: -4px 0 12px; font-size: 13px; color: var(--muted); max-width: 80ch; }
.check-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px; }
.check { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--r-sm); cursor: pointer; }
.check:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.check input { margin-top: 3px; accent-color: var(--accent); }
.check span { display: flex; flex-direction: column; font-size: 14px; }
.check small { color: var(--muted); font-size: 12px; }
td.sales-cell { white-space: normal; min-width: 110px; }
td.sales-cell .group-sub { white-space: normal; }
.form-actions .btn { white-space: nowrap; }

/* варіанти товару */
.variants-hint { display: block; margin-top: 4px; font-size: 12px; color: var(--accent); font-weight: 500; }
.detail-grid.variants { grid-template-columns: minmax(0, 1fr); }
table.mini { width: auto; min-width: min(640px, 100%); font-size: 14px; }
table.mini th, table.mini td { padding: 6px 12px; background: none; }
table.mini th { font-size: 12px; color: var(--muted); font-weight: 500; }
.share { display: inline-block; width: 90px; height: 8px; border-radius: 4px; background: var(--line); vertical-align: middle; overflow: hidden; }
.share i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.panel-gap { margin-top: 20px; }
.version { display: block; margin-top: 8px; font-size: 11px; color: var(--muted); }

/* пошук товарів */
.product-toolbar { display: flex; flex-wrap: wrap; gap: 12px 16px; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.product-toolbar .product-filter { margin-bottom: 0; }
.search { flex: 1 1 260px; max-width: 420px; }
.search input { width: 100%; box-sizing: border-box; min-height: 38px; padding: 0 12px; font: inherit; font-size: 14px;
  border: 1px solid var(--line-strong); border-radius: var(--r-sm); background: var(--surface); color: var(--ink); }
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Заголовки таблиць лишаються видимими під час прокрутки.
   Таблиця прокручується всередині своєї рамки (висота вікна), а рядок заголовків прилипає до її верху. */
.table-wrap thead th { position: sticky; top: 0; z-index: 3; box-shadow: inset 0 -1px 0 var(--line); }
table.mini thead th { position: static; box-shadow: none; }
/* Широкий екран: таблиця вміщується, тож заголовок прилипає до верху вікна під час звичайної прокрутки сторінки */
@media (min-width: 1200px) { .table-wrap { overflow: visible; } }
/* Вузький екран: таблицю гортають убік, тому вона прокручується у своїй рамці, і заголовок прилипає до неї */
@media (max-width: 1199px) { .table-wrap { max-height: calc(100vh - 80px); overflow: auto; } }

/* закупівлі */
.toolbar-right { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; flex: 1 1 360px; justify-content: flex-end; }
.toolbar-right .search { flex: 1 1 220px; }
.banner.static { margin: 0 0 16px; }
.mini-wrap { overflow-x: auto; }
tr.warn-row td { background: color-mix(in srgb, var(--warn-bg) 60%, transparent); }
.warn-text { color: var(--warn-ink); }
td.actions { white-space: nowrap; }
td.actions .btn + .btn { margin-left: 6px; }
.batch-items { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 20px 14px; }
.draft { display: grid; gap: 12px; }
.back { justify-self: start; }
.draft-panel { max-width: 980px; }
.draft-panel h2 { font-size: 18px; margin-bottom: 14px; }
.draft-h { font-size: 15px; font-weight: 600; margin: 18px 0 8px; }
.dropzone { border: 2px dashed var(--line-strong); border-radius: var(--r-md); padding: 22px; text-align: center; display: grid; gap: 6px; justify-items: center; background: var(--surface-2); }
.dropzone p { margin: 0; }
.dropzone.over, .dropzone:focus-visible { border-color: var(--accent); background: var(--accent-soft); outline: none; }
.thumbs { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 12px; }
.thumbs figure { margin: 0; display: grid; gap: 4px; justify-items: center; }
.thumbs img { height: 140px; width: auto; max-width: 220px; object-fit: cover; object-position: top; border: 1px solid var(--line); border-radius: var(--r-sm); }
.draft-table select { width: 100%; min-width: 260px; font: inherit; font-size: 14px; padding: 6px 8px; border: 1px solid var(--line-strong); border-radius: var(--r-sm); background: var(--surface); color: var(--ink); }
.draft-table select.unmatched { border-color: var(--loss); }
.qty-input { width: 80px; font: inherit; font-size: 14px; padding: 6px 8px; text-align: right; border: 1px solid var(--line-strong); border-radius: var(--r-sm); background: var(--surface); color: var(--ink); }
.draft-sum { margin-top: 10px; font-size: 14px; }
.draft-sum.match { color: var(--profit); }
.draft-sum.mismatch { color: var(--loss); }
.now { color: var(--loss); font-weight: 600; }
.draft-table input.qty-input { width: 90px; min-width: 0; }
