:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c2126;
  --muted: #6b7480;
  --border: #e2e5ea;
  --primary: #0a5cd8;
  --primary-text: #ffffff;
  --ok-bg: #e6f6ec;
  --ok-text: #146c2e;
  --error-bg: #fdecec;
  --error-text: #a3271b;
  --warn: #d98a13;
  --warn-bg: #fdf3e2;
  --warn-text: #8a5a05;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1e2126;
    --text: #e7e9ec;
    --muted: #9aa3ad;
    --border: #2c3036;
    --primary: #4c8dff;
    --primary-text: #0c1420;
    --ok-bg: #123322;
    --ok-text: #7bd79e;
    --error-bg: #3a1a18;
    --error-text: #f2a49c;
    --warn: #e0a53f;
    --warn-bg: #332608;
    --warn-text: #f2c878;
  }
}

[x-cloak] { display: none !important; }

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 20px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar__brand { font-weight: 600; }

.topbar__nav { display: flex; gap: 4px; flex: 1; }

.topbar__nav a {
  color: var(--muted);
  padding: 8px 12px;
  border-radius: 6px;
}

.topbar__nav a:hover { background: var(--bg); text-decoration: none; }
.topbar__nav a.active { color: var(--text); background: var(--bg); font-weight: 600; }

.topbar__user { display: flex; align-items: center; gap: 12px; color: var(--muted); position: relative; }

.settings-link { color: var(--muted); padding: 6px 10px; border-radius: 6px; }
.settings-link:hover { background: var(--bg); text-decoration: none; }
.settings-link.active { color: var(--text); background: var(--bg); font-weight: 600; }

.user-menu { position: relative; }
.user-menu__trigger {
  background: none; border: none; font: inherit; color: var(--text);
  cursor: pointer; padding: 6px 4px; display: flex; align-items: center; gap: 4px;
}
.user-menu__caret { font-size: 10px; color: var(--muted); }
.user-menu__dropdown {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 160px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12); padding: 6px; z-index: 20;
  display: flex; flex-direction: column;
}
.user-menu__dropdown a, .user-menu__dropdown .link-button {
  padding: 8px 10px; border-radius: 6px; color: var(--text); text-align: left;
}
.user-menu__dropdown a:hover, .user-menu__dropdown .link-button:hover {
  background: var(--bg); text-decoration: none;
}

.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

h1 { font-size: 20px; margin: 0 0 16px; }
h2 { font-size: 15px; margin: 0 0 12px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
}

.muted { color: var(--muted); }

.flash {
  padding: 10px 14px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 13px;
}

.flash--ok { background: var(--ok-bg); color: var(--ok-text); }
.flash--error { background: var(--error-bg); color: var(--error-text); }

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
}

input, select {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}

.form-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-end;
}

.form-row label { margin-bottom: 0; min-width: 160px; }

.verify-row { margin-top: 12px; }

button, .btn-secondary {
  font: inherit;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 6px;
  border: 1px solid transparent;
}

button[type="submit"], .btn-primary {
  background: var(--primary);
  color: var(--primary-text);
  border-color: var(--primary);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.link-button {
  background: none;
  border: none;
  color: var(--muted);
  padding: 0;
  cursor: pointer;
  font: inherit;
}

.link-button:hover { color: var(--text); text-decoration: underline; }

.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.table th { color: var(--muted); font-weight: 500; }
.table td.actions { display: flex; gap: 8px; }

.inline-form { display: inline; }

.kv { display: grid; grid-template-columns: 160px 1fr; row-gap: 8px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.auth-card {
  max-width: 340px;
  margin: 80px auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px;
}

.auth-card h1 { font-size: 18px; text-align: center; }
.auth-card button { width: 100%; margin-top: 4px; }

.product-cell { display: flex; align-items: center; gap: 10px; }
.product-cell img { width: 36px; height: 36px; object-fit: cover; border-radius: 6px; background: var(--bg); }

.search-row { margin-bottom: 16px; }
.search-row input { min-width: 280px; }

code.token { font-family: ui-monospace, monospace; background: var(--bg); padding: 2px 6px; border-radius: 4px; }

/* Тосты */
.toast-container {
  position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column;
  gap: 8px; z-index: 100;
}
.toast {
  background: var(--error-text); color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 13px; max-width: 320px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  opacity: 0; transform: translateY(8px); transition: opacity 0.2s, transform 0.2s;
}
.toast--visible { opacity: 1; transform: translateY(0); }

/* Плитки статистики */
.mp-tabs { display: flex; gap: 4px; margin-bottom: 20px; border-bottom: 1px solid var(--border); }
.mp-tabs a {
  padding: 8px 16px;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  font-weight: 500;
}
.mp-tabs a:hover { color: var(--text); text-decoration: none; }
.mp-tabs a.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }

.stats-row { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 16px; }
.stat-tile {
  flex: 1; min-width: 160px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 16px; display: flex; flex-direction: column; gap: 4px;
  color: var(--text); cursor: pointer;
}
.stat-tile:hover { text-decoration: none; border-color: var(--primary); }
.stat-tile--active { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.stat-tile--static { cursor: default; }
.stat-tile--warn .stat-tile__value { color: var(--warn-text); }
.stat-tile__value { font-size: 22px; font-weight: 600; }
.stat-tile__label { font-size: 12px; color: var(--muted); }

.sync-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 16px; flex-wrap: wrap;
}

.toolbar { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-end; }
.toolbar label { min-width: 160px; margin-bottom: 0; }
.toolbar .btn-secondary { align-self: center; }

.import-panel { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--border); }
.import-panel form { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }

.import-preview-summary { display: flex; gap: 16px; flex-wrap: wrap; margin: 12px 0; font-size: 13px; }
.import-preview-summary strong { display: block; font-size: 18px; }
.import-preview-errors { color: var(--error-text); font-size: 13px; margin: 8px 0; }
.import-preview-errors li { margin-bottom: 2px; }

/* Себестоимость: строка товара */
.costs-table tbody.cost-group.row--missing > tr:first-child { box-shadow: inset 3px 0 0 var(--warn); }
.cost-cell { min-width: 160px; }
.cost-value { cursor: pointer; display: inline-block; padding: 2px 4px; border-radius: 4px; }
.cost-value:hover { background: var(--bg); }
.btn-add-cost {
  color: var(--primary); font-size: 12px; font-weight: 600; border: 1px dashed var(--primary);
  border-radius: 6px; padding: 3px 8px;
}
.inline-edit { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; position: relative; }
.inline-edit input[type="text"] { width: 90px; padding: 4px 6px; }
.inline-edit input[type="date"] { padding: 4px 6px; }
.inline-edit__note { width: 130px; }
.inline-edit button { padding: 4px 10px; }
.field-error {
  position: absolute; top: 100%; left: 0; margin-top: 4px; background: var(--error-bg);
  color: var(--error-text); padding: 4px 8px; border-radius: 6px; font-size: 12px;
  white-space: nowrap; z-index: 5;
}

.chevron-btn { font-size: 12px; }
.history-row td { background: var(--bg); padding: 12px 10px; }
.history-table { font-size: 12px; }
.history-row--current { font-weight: 600; }
.badge-current { color: var(--ok-text); background: var(--ok-bg); border-radius: 4px; padding: 1px 6px; font-size: 11px; }

/* Склад */
.mp-tabs--sub { margin-top: -4px; margin-bottom: 16px; }

.stock-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.stock-cards--muted { opacity: 0.6; }

.stock-card {
  display: flex;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  cursor: pointer;
}
.stock-card:hover { border-color: var(--primary); }
.stock-card img { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; background: var(--bg); flex-shrink: 0; }
.stock-card__body { min-width: 0; flex: 1; }
.stock-card__name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stock-card__offer { font-size: 12px; }
.stock-card__metrics { margin-top: 6px; }
.stock-card__qty { font-size: 15px; font-weight: 600; }
.stock-card__qty--zero { color: var(--error-text); }
.stock-card__missing { font-size: 12px; color: var(--warn-text); margin-top: 4px; }
.stock-card__used-by { font-size: 12px; margin-top: 4px; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto; z-index: 50;
}
.modal {
  position: relative; background: var(--surface); border-radius: 12px;
  padding: 24px; max-width: 640px; width: 100%;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.modal--wide { max-width: 760px; }
.modal-close {
  position: absolute; top: 14px; right: 14px; background: none; border: none;
  color: var(--muted); font-size: 16px; padding: 4px 8px; cursor: pointer;
}
.modal-close:hover { color: var(--text); }
.modal-header { display: flex; gap: 12px; align-items: center; padding-right: 24px; }
.modal-header__img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; background: var(--bg); }
.modal-header h2 { margin: 0 0 2px; font-size: 17px; }
.modal-footer { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }

.row--limiting { box-shadow: inset 3px 0 0 var(--warn); background: var(--warn-bg); }
.row--depleted { opacity: 0.55; }
.row--missing { box-shadow: inset 3px 0 0 var(--warn); }

.stock-form-toggle { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.stock-form-toggle summary { cursor: pointer; font-weight: 600; color: var(--primary); }
.stock-form-toggle summary.btn-secondary { display: inline-block; font-weight: normal; color: var(--text); }
.stock-form-toggle form { margin-top: 12px; }
.cost-item-row { align-items: center; }

.supply-header-table { max-width: 640px; margin-bottom: 4px; }
.supply-header-table td:first-child { color: var(--muted); white-space: nowrap; }
.supply-header-table td input { width: 140px; }
.supply-table-wrap { overflow-x: auto; margin-top: 12px; }
.supply-table { min-width: 1400px; }
.supply-table th, .supply-table td { white-space: nowrap; }
.supply-table td input, .supply-table td select {
  width: 100%; min-width: 72px; padding: 4px 6px; font-size: 12px;
}
.supply-table img { width: 32px; height: 32px; object-fit: cover; border-radius: 4px; }
.supply-table td.calc { color: var(--muted); font-size: 12px; }
.supply-totals { margin: 12px 0; font-size: 13px; }
.supply-totals strong { color: var(--text); }
.weight-mismatch { color: var(--warn-text); font-weight: 600; }

.photo-placeholder {
  width: 36px; height: 36px; border-radius: 6px; background: var(--bg);
  border: 1px solid var(--border); display: inline-block;
}

.amount--income { color: var(--ok-text); }
.amount--expense { color: var(--error-text); }

/* Аналитика */
.analytics-toolbar { display: flex; justify-content: flex-end; margin: -8px 0 16px; }
.period-switch { display: flex; gap: 4px; }
.period-switch__item {
  padding: 6px 12px; border-radius: 6px; color: var(--muted); font-size: 13px; border: 1px solid var(--border);
}
.period-switch__item:hover { color: var(--text); text-decoration: none; }
.period-switch__item.active { color: var(--primary-text); background: var(--primary); border-color: var(--primary); }

.metric-switch { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.metric-switch__item {
  padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
  color: var(--muted); font-size: 13px; cursor: pointer;
}
.metric-switch__item:hover { color: var(--text); }
.metric-switch__item--active { color: var(--primary-text); background: var(--primary); border-color: var(--primary); }

.analytics-tile { display: flex; gap: 10px; padding: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; cursor: pointer; }
.analytics-tile:hover { border-color: var(--primary); }
.analytics-tile img { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; background: var(--bg); flex-shrink: 0; }
.analytics-tile__metric { margin-top: 6px; font-size: 15px; }

.text-error { color: var(--error-text); }
.text-warn { color: var(--warn-text); }
.text-ok { color: var(--ok-text); }

.pagination { display: flex; gap: 4px; margin-top: 12px; }
.pagination a {
  padding: 4px 10px; border-radius: 6px; border: 1px solid var(--border); color: var(--text);
}
.pagination a.active { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.pagination a:hover { text-decoration: none; background: var(--bg); }

/* Главная (дашборд) */
.dashboard-header { display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap; gap: 8px; margin-bottom: 4px; }
.dashboard-header__freshness { font-size: 12px; color: var(--muted); }

.metric-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.metric-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.metric-card__label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.metric-card__value { font-size: 22px; font-weight: 600; }
.metric-card__value--negative { color: var(--error-text); }
.metric-card__delta { font-size: 12px; margin-top: 4px; }
.metric-card__delta--up { color: var(--ok-text); }
.metric-card__delta--down { color: var(--error-text); }
.metric-card__delta--neutral { color: var(--muted); }

.today-row {
  display: flex; align-items: center; gap: 10px; background: var(--bg); border: 1px dashed var(--border);
  border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; font-size: 13px; flex-wrap: wrap;
}
.today-row strong { font-size: 15px; }

.dashboard-main { display: grid; grid-template-columns: 1.6fr 1fr; gap: 16px; margin-bottom: 20px; }
@media (max-width: 900px) { .dashboard-main { grid-template-columns: 1fr; } }

.chart-svg { width: 100%; display: block; }
.chart-axis { stroke: var(--border); stroke-width: 1; }
.chart-line { stroke: var(--primary); stroke-width: 2; }
.chart-line--mini { stroke-width: 1.5; }
.chart-dot { fill: var(--primary); }
.chart-svg--mini { display: block; }

.expense-breakdown dl { display: grid; grid-template-columns: 1fr auto auto; gap: 6px 12px; margin: 0; font-size: 13px; }
.expense-breakdown dt { color: var(--muted); }
.expense-breakdown dd { margin: 0; text-align: right; }
.expense-breakdown dd.pct { color: var(--muted); }
.expense-breakdown .divider { grid-column: 1 / -1; border-top: 1px solid var(--border); margin: 4px 0; }

.attention-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.attention-card { display: flex; gap: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.attention-card img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; background: var(--bg); flex-shrink: 0; }
.attention-card__name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attention-card__delta { color: var(--error-text); font-weight: 600; margin-top: 4px; }
.attention-card__chart { margin-top: 6px; }

.sticky-table-wrap { overflow-x: auto; margin-top: 12px; }
.sticky-table { border-collapse: separate; border-spacing: 0; min-width: 100%; }
.sticky-table th, .sticky-table td { white-space: nowrap; border-bottom: 1px solid var(--border); padding: 6px 10px; }
.sticky-table th:first-child, .sticky-table td:first-child {
  position: sticky; left: 0; background: var(--surface); z-index: 2; box-shadow: 1px 0 0 var(--border);
}
.sticky-table tfoot td { font-weight: 600; background: var(--bg); }

.cluster-table td.grade--DEFICIT { color: var(--error-text); }
.cluster-table td.grade--SURPLUS { color: var(--warn-text); }
.cluster-table td.grade--POPULAR { color: var(--ok-text); }

.analytics-row { cursor: pointer; }
.analytics-row:hover { background: var(--bg); }

@media (max-width: 720px) {
  .costs-table thead { display: none; }
  .costs-table, .costs-table tbody, .costs-table tr, .costs-table td { display: block; width: 100%; }
  .costs-table tbody.cost-group {
    border: 1px solid var(--border); border-radius: 10px; margin-bottom: 12px; padding: 10px;
  }
  .costs-table tbody.cost-group.row--missing { box-shadow: inset 3px 0 0 var(--warn); }
  .costs-table td { border-bottom: none; padding: 4px 0; }
  .costs-table td:before { content: attr(data-label); display: block; font-size: 11px; color: var(--muted); }
  .history-row td { padding: 8px 0 0; }
}
