:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: #e5e5ea;
  --accent: #0071e3;
  --accent-soft: #e8f1fd;
  --green: #1a8f3c;
  --green-soft: #e7f6ec;
  --red: #d70015;
  --red-soft: #fdecee;
  --orange: #b25000;
  --orange-soft: #fff3e3;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 10px rgba(0,0,0,.04);
  --side: 232px;
  --tap: 44px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 26px; font-weight: 700; letter-spacing: -.02em; margin: 0; }
h2 { font-size: 18px; font-weight: 600; margin: 0 0 12px; letter-spacing: -.01em; }
h3 { font-size: 15px; font-weight: 600; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num, td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13.5px; letter-spacing: .01em; }
.pos { color: var(--green); } .neg { color: var(--red); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }

/* ---------- каркас */
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--side); background: rgba(250,250,252,.92);
  backdrop-filter: saturate(180%) blur(20px); border-right: 1px solid var(--line); padding: 18px 12px; overflow-y: auto;
}
.brand { font-weight: 700; font-size: 17px; padding: 4px 10px 18px; letter-spacing: -.02em; display: flex; gap: 8px; align-items: center; }
.brand-dot { width: 22px; height: 22px; border-radius: 7px; background: linear-gradient(135deg,#1d1d1f,#555); display: inline-block; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px; color: var(--text);
  font-weight: 500; margin-bottom: 2px;
}
.nav a:hover { background: #ececf0; text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav .ic { flex: none; opacity: .9; }
.side-user { margin-top: 24px; padding: 12px 10px; border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.main { margin-left: var(--side); padding: 28px 32px 60px; max-width: 1400px; }
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.top .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.crumbs { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.tabbar, .more-sheet { display: none; }

/* ---------- картки і сітки */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 16px; }
.card.flush { padding: 0; overflow: hidden; }
.card.flush > h2 { padding: 16px 20px 0; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
.kpi { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; }
.kpi .label { font-size: 13px; color: var(--muted); }
.kpi .value { font-size: 24px; font-weight: 650; letter-spacing: -.02em; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
a.kpi { color: inherit; display: block; } a.kpi:hover { text-decoration: none; box-shadow: 0 0 0 2px var(--accent-soft), var(--shadow); }
.dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
.dl dt { color: var(--muted); } .dl dd { margin: 0; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.sep { height: 1px; background: var(--line); margin: 14px 0; }

/* ---------- кнопки */
.btn, button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 36px; padding: 0 16px;
  border-radius: 10px; border: 0; background: var(--accent); color: #fff; font: inherit; font-weight: 550; cursor: pointer;
  white-space: nowrap; text-decoration: none !important;
}
.btn:hover, button:hover { filter: brightness(1.06); }
.btn.secondary, button.secondary { background: #e9e9ee; color: var(--text); }
.btn.ghost, button.ghost { background: transparent; color: var(--accent); padding: 0 8px; }
.btn.danger, button.danger { background: var(--red-soft); color: var(--red); }
.btn.small, button.small { min-height: 30px; padding: 0 12px; font-size: 13.5px; border-radius: 8px; }
.btn[disabled], button[disabled] { opacity: .45; pointer-events: none; }

/* ---------- форми */
input, select, textarea {
  font: inherit; color: var(--text); background: #fff; border: 1px solid #d2d2d7; border-radius: 10px;
  padding: 8px 12px; min-height: 40px; width: 100%; outline: none; -webkit-appearance: none; appearance: none;
}
select { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236e6e73' stroke-width='1.6' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px; }
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type=checkbox] { width: 20px; min-height: 20px; height: 20px; padding: 0; accent-color: var(--accent); -webkit-appearance: checkbox; appearance: auto; }
label { font-size: 13px; color: var(--muted); font-weight: 500; display: block; margin-bottom: 5px; }
.form > div, form.stack > div, .field { margin-bottom: 14px; }
.form .helptext, .helptext { display: block; font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.errorlist { list-style: none; padding: 0; margin: 4px 0 0; color: var(--red); font-size: 13px; }
.form div:has(> input[type=checkbox]) { display: flex; align-items: center; gap: 10px; flex-direction: row-reverse; justify-content: flex-end; }
.form div:has(> input[type=checkbox]) label { margin: 0; color: var(--text); font-size: 15px; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.inline-form > * { flex: 1 1 140px; margin: 0; }
.inline-form > button, .inline-form > .btn { flex: 0 0 auto; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.filters > * { flex: 1 1 150px; max-width: 240px; }
.filters .search { flex: 2 1 240px; max-width: 420px; }
.filters button, .filters .btn { flex: 0 0 auto; }
.filters .ftoggle { display: none; }

/* ---------- таблиці */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12.5px; font-weight: 600; color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: #fbfbfd; }
td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: #fafafc; }
tr.clickable { cursor: pointer; }
tfoot td { font-weight: 650; background: #fbfbfd; }
td input, td select { min-height: 34px; padding: 5px 8px; }

/* ---------- бейджі */
.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12.5px; font-weight: 550; background: #ececf0; color: var(--muted); white-space: nowrap; }
.badge.in_stock, .badge.arrived, .badge.ok { background: var(--green-soft); color: var(--green); }
.badge.in_transit, .badge.ordered { background: var(--accent-soft); color: var(--accent); }
.badge.sold, .badge.closed { background: #ececf0; color: var(--text); }
.badge.returned, .badge.warn { background: var(--orange-soft); color: var(--orange); }
.badge.defect, .badge.cancelled, .badge.supplier_return, .badge.bad { background: var(--red-soft); color: var(--red); }

/* ---------- повідомлення */
.messages { list-style: none; padding: 0; margin: 0 0 16px; }
.messages li { padding: 11px 14px; border-radius: 12px; margin-bottom: 8px; background: var(--accent-soft); color: #0b4f9c; }
.messages li.error { background: var(--red-soft); color: var(--red); }
.messages li.success { background: var(--green-soft); color: var(--green); }

.segmented { display: inline-flex; background: #e9e9ee; border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.segmented a { padding: 5px 12px; border-radius: 8px; color: var(--text); font-size: 13.5px; font-weight: 500; }
.segmented a.on { background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.1); }
.segmented a:hover { text-decoration: none; }
details > summary { cursor: pointer; font-weight: 600; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "›"; display: inline-block; margin-right: 8px; transition: transform .15s; color: var(--muted); }
details[open] > summary::before { transform: rotate(90deg); }
.pagination { display: flex; gap: 8px; align-items: center; justify-content: center; padding: 14px; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.sticky-bar { position: sticky; bottom: 0; background: rgba(245,245,247,.92); backdrop-filter: blur(12px); padding: 12px 0; display: flex; gap: 10px; align-items: center; }

/* пошук-список у формі продажу */
.picker { position: relative; }
.picker-results { position: absolute; z-index: 20; left: 0; right: 0; top: 100%; background: #fff; border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.14); max-height: 320px; overflow-y: auto; margin-top: 4px; }
.picker-results > div { padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.picker-results > div:hover { background: var(--accent-soft); }

/* ---------- телефон */
@media (max-width: 860px) {
  body { font-size: 16px; }
  .sidebar { display: none; }
  .main { margin: 0; padding: 14px 14px calc(84px + env(safe-area-inset-bottom)); }
  h1 { font-size: 24px; }
  .g2, .g3 { grid-template-columns: 1fr; }
  .g4 { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .kpi .value { font-size: 20px; }
  .card { padding: 14px; border-radius: 16px; }
  .top { margin-bottom: 14px; }
  .top .actions { width: 100%; }
  .top .actions .btn { flex: 1; }
  .btn, button { min-height: var(--tap); font-size: 16px; }
  .btn.small, button.small { min-height: 36px; }
  input, select, textarea { min-height: var(--tap); font-size: 16px; }
  .filters > *, .filters .search { max-width: none; flex: 1 1 100%; }
  .filters button { flex: 1 1 100%; }
  .filters.collapsible:not(.show) > select, .filters.collapsible:not(.show) > input[type=date] { display: none; }
  .filters.collapsible > .ftoggle { display: inline-flex; }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 50; background: rgba(250,250,252,.94);
    backdrop-filter: saturate(180%) blur(20px); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 10.5px; color: var(--muted); padding: 4px 0; }
  .tabbar a.active { color: var(--accent); }
  .tabbar a:hover { text-decoration: none; }
  .more-sheet { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 60; display: none; align-items: flex-end; }
  .more-sheet.open { display: flex; }
  .more-sheet .sheet { background: #fff; width: 100%; border-radius: 18px 18px 0 0; padding: 10px 12px calc(16px + env(safe-area-inset-bottom)); }
  .more-sheet .sheet a { display: flex; gap: 12px; align-items: center; padding: 14px 10px; font-size: 17px; color: var(--text); border-bottom: 1px solid var(--line); }
  /* таблиці → картки */
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr { padding: 10px 14px; border-bottom: 1px solid var(--line); }
  table.cards td { border: 0; padding: 3px 0; text-align: right; overflow: hidden; }
  table.cards td::before { content: attr(data-label); float: left; margin-right: 12px; color: var(--muted); font: 13.5px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; letter-spacing: 0; }
  table.cards td[data-label=""]::before, table.cards td:not([data-label])::before { content: none; }
  table.cards td.primary { font-weight: 600; font-size: 16px; text-align: left; padding-bottom: 4px; }
  table.cards td:empty { display: none; }
  table.cards td .inline-form { justify-content: flex-start; text-align: left; }
  table.cards tfoot { display: block; }
  table.cards tfoot tr { background: #fbfbfd; }
  .hide-m { display: none !important; }
  .sticky-bar { bottom: calc(62px + env(safe-area-inset-bottom)); margin: 0 -14px; padding: 10px 14px; }
  .sticky-bar button { flex: 1; }
}
@media (min-width: 861px) { .show-m { display: none !important; } }

/* ---------- друк */
@media print {
  .sidebar, .tabbar, .no-print, .messages { display: none !important; }
  .main { margin: 0; padding: 0; }
  body { background: #fff; font-size: 12px; }
  .card { box-shadow: none; padding: 0; }
  th { background: #fff; }
}

/* заголовки груп на складі (категорія · бренд) */
tr.group-row td { background: var(--accent-soft); padding: 8px 14px; font-size: 14px; }
@media (max-width: 860px) { table.cards tr.group-row { display: block; border-radius: 10px; margin: 14px 0 6px; } table.cards tr.group-row td { display: block; } }

a.edit-pencil { display: inline-block; margin-left: 8px; padding: 2px 8px; border-radius: 8px; background: var(--accent-soft); text-decoration: none; font-family: inherit; }

/* Сканер камерою */
.scan-row { display: flex; gap: 8px; align-items: stretch; }
.scan-row > input { flex: 1; }
.scan-btn { padding: 0 14px; font-size: 18px; }
.scan-overlay { position: fixed; inset: 0; z-index: 1000; background: rgba(0,0,0,.6); display: flex; align-items: flex-end; justify-content: center; }
.scan-panel { background: var(--card); width: 100%; max-width: 520px; max-height: 94vh; overflow-y: auto; border-radius: 16px 16px 0 0; padding: 14px 16px 0; display: flex; flex-direction: column; }
.scan-head { display: flex; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 10px; }
#scan-video { width: 100%; border-radius: 12px; overflow: hidden; background: #000; min-height: 200px; }
#scan-video video { width: 100% !important; }
.scan-msg { margin: 8px 0; }
.scan-list { display: flex; flex-direction: column; gap: 6px; }
.scan-chip { text-align: left; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.scan-chip:disabled { opacity: .6; }
@media (min-width: 700px) { .scan-overlay { align-items: center; } .scan-panel { border-radius: 16px; } }
.scan-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
.scan-act { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 14px; }

/* Компактні списки на телефоні: 2 рядки замість картки на 5–7 рядків */
@media (max-width: 860px) {
  table.cards tr.group-row td { text-align: left; }
  table.cards.compact tr:not(.group-row) { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 10px; row-gap: 3px; padding: 10px 14px; }
  table.cards.compact tr:not(.group-row)::after { content: ""; flex-basis: 100%; order: 1; height: 0; }
  table.cards.compact td { width: auto; padding: 0; text-align: left; }
  table.cards.compact td::before { content: none; }
  table.cards.compact td.primary { order: 0; flex: 1 1 0; min-width: 0; font-size: 15px; padding: 0; }
  table.cards.compact td.amt { order: 1; margin-left: auto; font-weight: 600; white-space: nowrap; }
  table.cards.compact td.meta { order: 2; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
  table.cards.compact td.meta.neg { color: var(--red, #d70015); }
  table.cards.compact td.meta.pos { color: var(--green, #248a3d); }
  table.cards.compact td.meta[data-short]::before { content: attr(data-short) " "; float: none; margin: 0; font: inherit; color: var(--muted); }
  table.cards.compact td.hide-sm, table.cards.compact td.meta:empty { display: none; }
  table.cards.compact .badge { font-size: 11px; padding: 1px 7px; }
  table.cards.compact td.empty { flex-basis: 100%; }
}

/* Пошук на телефоні: [поле + 📷] в один рядок, [Фільтри] [Знайти] поряд */
@media (max-width: 860px) {
  .filters > .search { flex: 1 1 calc(100% - 66px); order: 0; }
  .filters > .scan-btn { flex: 0 0 56px; order: 0; }
  .filters > select, .filters > input[type=date], .filters > div { order: 1; }
  .filters > .ftoggle, .filters > button:not(.scan-btn) { order: 2; flex: 1 1 40%; }
}

.badge.cond { font-size: 10.5px; font-weight: 600; letter-spacing: .3px; padding: 1px 6px; background: #fff4e5; color: #b25e00; vertical-align: middle; }

/* Період: на телефоні — один список замість кнопок; дати лише для «свої дати» */
.period-form .period-select { display: none; }
@media (max-width: 860px) {
  .period-form .segmented { display: none; }
  .period-form .period-select { display: block; order: 0; flex: 1 1 100%; max-width: none; }
  .period-form input[type=date] { order: 1; flex: 1 1 40%; max-width: none !important; }
  .period-form:not(.custom) input[type=date], .period-form:not(.custom) button { display: none; }
}

/* Показ лише на ноутбуці / лише на телефоні */
.mob-only { display: none !important; }
@media (max-width: 860px) {
  .desk-only { display: none !important; }
  .mob-only { display: block !important; }
  details.more-menu.mob-only { display: inline-block !important; position: relative; }
  .grid.g2.keep2 { grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; }
  .acc-list a.row { display: flex; padding: 7px 0; color: var(--text); font-size: 15px; }
  .acc-list a.row:hover { text-decoration: none; }
}
.desk-only.actions-inline, .actions .desk-only { display: contents; }
details.more-menu > summary { list-style: none; cursor: pointer; }
details.more-menu > summary::-webkit-details-marker { display: none; }
details.more-menu .more-list { position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; background: var(--card); border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.16); padding: 6px; display: flex; flex-direction: column; gap: 4px; min-width: 220px; }
details.more-menu .more-list .btn { justify-content: flex-start; width: 100%; }

.ptag { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 6px; font-size: 11px; font-weight: 600; line-height: 18px; text-align: center; vertical-align: 1px; }
.ptag.k { background: #eef3ff; color: #2456c9; }
.ptag.p { background: #fff4e5; color: #b25e00; }
.ptag.z { background: #f2eefc; color: #6b3fb8; }

.scan-foot { position: sticky; bottom: 0; background: var(--card); padding: 10px 0 calc(14px + env(safe-area-inset-bottom)); margin-top: 10px; }
.scan-foot .scan-close { width: 100%; font-size: 17px; padding: 14px; border-radius: 12px; }

.overdue-tag { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 6px; font-size: 11px; font-weight: 600; line-height: 18px; background: #fdecec; color: #c2001a; vertical-align: 1px; }
a.overdue-bar { display: block; color: var(--text); padding: 12px 16px; margin-bottom: 12px; background: #fff6f6; }
a.overdue-bar:hover { text-decoration: none; }
