:root {
  --bg: #f4f5f7;
  --bg-elevated: #ffffff;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --border: #e2e4e9;
  --accent: #2563eb;
  --green: #16a34a;
  --red: #dc2626;
  --orange: #d97706;
  --header-bg: rgba(37, 99, 235, 0.10);
  --section-bg: rgba(217, 119, 6, 0.10);
  --expense-bg: rgba(217, 119, 6, 0.05);
  --income-bg: rgba(22, 163, 74, 0.10);
  --subtotal-bg: rgba(37, 99, 235, 0.14);
  --total-bg: rgba(22, 163, 74, 0.18);
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --bg-elevated: #1c1f26;
    --text: #e8e9ec;
    --text-muted: #9299a6;
    --border: #2b2f38;
    --accent: #5b8def;
    --green: #34d399;
    --red: #f87171;
    --orange: #fbbf24;
    --header-bg: rgba(91, 141, 239, 0.14);
    --section-bg: rgba(251, 191, 36, 0.10);
    --expense-bg: rgba(251, 191, 36, 0.06);
    --income-bg: rgba(52, 211, 153, 0.12);
    --subtotal-bg: rgba(91, 141, 239, 0.18);
    --total-bg: rgba(52, 211, 153, 0.20);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  }
}

:root[data-theme="dark"] {
  --bg: #14161a;
  --bg-elevated: #1c1f26;
  --text: #e8e9ec;
  --text-muted: #9299a6;
  --border: #2b2f38;
  --accent: #5b8def;
  --green: #34d399;
  --red: #f87171;
  --orange: #fbbf24;
  --header-bg: rgba(91, 141, 239, 0.14);
  --section-bg: rgba(251, 191, 36, 0.10);
  --expense-bg: rgba(251, 191, 36, 0.06);
  --income-bg: rgba(52, 211, 153, 0.12);
  --subtotal-bg: rgba(91, 141, 239, 0.18);
  --total-bg: rgba(52, 211, 153, 0.20);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}
:root[data-theme="light"] {
  --bg: #f4f5f7;
  --bg-elevated: #ffffff;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --border: #e2e4e9;
  --accent: #2563eb;
  --green: #16a34a;
  --red: #dc2626;
  --orange: #d97706;
  --header-bg: rgba(37, 99, 235, 0.10);
  --section-bg: rgba(217, 119, 6, 0.10);
  --expense-bg: rgba(217, 119, 6, 0.05);
  --income-bg: rgba(22, 163, 74, 0.10);
  --subtotal-bg: rgba(37, 99, 235, 0.14);
  --total-bg: rgba(22, 163, 74, 0.18);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar h1 {
  font-size: 15px;
  font-weight: 600;
  padding: 0 10px 16px;
  color: var(--text-muted);
}

.nav-item {
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  user-select: none;
}
.nav-item:hover { background: var(--header-bg); }
.nav-item.active { background: var(--accent); color: white; font-weight: 600; }

.theme-toggle {
  margin-top: auto;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
}

main {
  flex: 1;
  padding: 28px 36px;
  max-width: 1100px;
}

h2 { margin-top: 0; font-size: 22px; }
.muted { color: var(--text-muted); font-size: 13px; }

.card-row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin: 18px 0;
}
.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 20px;
  min-width: 180px;
  flex: 1;
}
.card .label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.card .value { font-size: 22px; font-weight: 700; }
.card .delta { font-size: 12px; margin-top: 4px; }
.delta.up { color: var(--green); }
.delta.down { color: var(--red); }

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-elevated);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
th, td {
  text-align: left;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
th { color: var(--text-muted); font-weight: 600; background: var(--bg); }
tr.header { background: var(--header-bg); font-weight: 600; }
tr.section { background: var(--section-bg); font-weight: 600; }
tr.expense { background: var(--expense-bg); }
tr.income { background: var(--income-bg); }
tr.subtotal { background: var(--subtotal-bg); font-weight: 700; }
tr.total { background: var(--total-bg); font-weight: 700; }
tr.sub td { color: var(--text-muted); }
tr.coinvest_pct td { color: var(--orange); font-style: italic; }
tr.loss-row { background: rgba(220, 38, 38, 0.12); }
tr.loss-row td:last-child { color: var(--red); font-weight: 600; }
tr.stock-critical { background: rgba(220, 38, 38, 0.12); }
tr.stock-critical td:last-child { color: var(--red); font-weight: 600; }
tr.stock-warning { background: rgba(217, 119, 6, 0.12); }
tr.stock-warning td:last-child { color: var(--orange); font-weight: 600; }
tr.stock-ok td:last-child { color: var(--green); font-weight: 600; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Индикаторы динамики к предыдущему периоду (только недели/месяцы, см. app.js trendBadge) */
.trend {
  display: block;
  font-size: 11px;
  font-weight: 600;
  margin-top: 2px;
  white-space: nowrap;
}
.trend-up { color: var(--green); }
.trend-down { color: var(--red); }
.trend-flat { color: var(--text-muted); }
/* Больше воздуха между колонками недель/месяцев — легче сравнивать соседние периоды */
table.table-wide-cols th, table.table-wide-cols td { padding-left: 18px; padding-right: 18px; }

.sticky-cols-table th.sticky-col-1, .sticky-cols-table td.sticky-col-1 {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--bg-elevated);
  min-width: 150px;
  max-width: 150px;
  white-space: normal;
  box-shadow: 2px 0 0 var(--border);
}
.sticky-cols-table th.sticky-col-2, .sticky-cols-table td.sticky-col-2 {
  position: sticky;
  left: 150px;
  z-index: 2;
  background: var(--bg-elevated);
  min-width: 170px;
  max-width: 170px;
  white-space: normal;
  box-shadow: 2px 0 0 var(--border);
}
.sticky-cols-table thead th.sticky-col-1, .sticky-cols-table thead th.sticky-col-2 {
  z-index: 3;
  background: var(--bg);
}

.btn {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius);
  padding: 9px 16px;
  font-size: 13px;
  cursor: pointer;
}
.btn:disabled { opacity: 0.6; cursor: default; }
.btn.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn.danger { background: var(--red); }

.rnp-ad-breakdown { margin: 4px 0 8px; padding-left: 20px; font-size: 13px; color: var(--text-muted); }
.rnp-ad-breakdown li { margin: 2px 0; }

.unit-econ-product-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 6px;
  width: 190px;
  padding: 10px 6px;
  box-sizing: border-box;
}
.unit-econ-product-cell img {
  width: 84px;
  height: 84px;
  object-fit: contain;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.unit-econ-product-cell .unit-econ-offer {
  font-size: 11px;
  color: var(--text-muted);
  word-break: break-word;
  max-width: 100%;
}
.unit-econ-product-cell .unit-econ-sku {
  font-size: 10px;
  color: var(--text-muted);
  word-break: break-word;
  max-width: 100%;
}
.unit-econ-product-cell .unit-econ-name {
  font-size: 12px;
  font-weight: 600;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
  overflow: hidden;
  max-width: 100%;
  word-break: break-word;
}
.unit-econ-tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.unit-econ-tabs .btn.active { background: var(--accent); color: #fff; }

.rnp-reconciliation-block { margin-top: 32px; padding-top: 16px; border-top: 2px solid var(--border); }
.rnp-reconciliation-btn {
  font-size: 15px;
  font-weight: 700;
  padding: 14px 24px;
  box-shadow: var(--shadow);
}

select, input[type=text], input[type=number], input[type=date] {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  color: var(--text);
  font-size: 13px;
}

.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }

.warning-box {
  background: var(--expense-bg);
  border: 1px solid var(--orange);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 14px;
}

.chart-wrap {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 20px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 900px) {
  .grid-2 { grid-template-columns: 1fr; }
}

form.inline-form {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-end;
  margin-bottom: 18px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
form.inline-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--text-muted);
}

.key-status { display: flex; flex-direction: column; gap: 8px; margin: 12px 0 20px; }
.key-status .row { display: flex; justify-content: space-between; max-width: 420px; font-size: 13px; }
.key-status code { color: var(--text-muted); }

.auth-gate {
  max-width: 420px;
  margin: 60px auto;
  padding: 32px;
  background: var(--bg-elevated);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.auth-gate h1 { font-size: 20px; margin: 0 0 20px; text-align: center; }
.auth-tabs { display: flex; gap: 8px; margin-bottom: 20px; }
.auth-tabs .btn { flex: 1; }
.auth-tabs .btn.active { background: var(--accent); color: #fff; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-muted); }
.auth-form input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--text); font-size: 14px; }
.auth-form h4 { margin: 8px 0 0; font-size: 13px; color: var(--text-muted); }
.auth-error { color: var(--red); font-size: 13px; margin-top: 12px; min-height: 18px; }

.editable-num {
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  width: 90px;
  padding: 4px 6px;
  border-radius: 6px;
  text-align: right;
}
.editable-num:focus { border-color: var(--accent); background: var(--bg); }

.autosave-badge {
  color: var(--green);
  font-size: 12px;
  margin-left: 4px;
  opacity: 0;
  transition: opacity 0.2s;
}
.autosave-badge.show { opacity: 1; }

.scroll-x { overflow-x: auto; margin-bottom: 4px; }
.scroll-x table { min-width: 100%; }
.scroll-x td, .scroll-x th { white-space: nowrap; }
/* Первая колонка (подпись строки) остаётся видимой при горизонтальной прокрутке через
   много недель/месяцев — без этого подпись уезжает за левый край и приходится скроллить
   обратно, чтобы понять, что за строка. */
.scroll-x table th:first-child, .scroll-x table td:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--bg-elevated);
  font-weight: 600;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.08);
}
.scroll-x table th:first-child { background: var(--bg); z-index: 2; }
.scroll-x tbody tr:hover td { background: rgba(127, 127, 127, 0.08); }
.scroll-x tbody tr:hover td:first-child { background: var(--bg); }

.rnp-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 24px;
}
.rnp-card-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.rnp-photo {
  width: 128px;
  height: 128px;
  object-fit: contain;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  flex-shrink: 0;
}
.rnp-side-stats {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 150px;
}
.rnp-side-stat {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
}
.rnp-side-stat .rnp-side-label { color: var(--text-muted); }
.rnp-side-stat .rnp-side-value { font-weight: 700; }
.rnp-title {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.4;
  margin-bottom: 6px;
  max-width: 60ch;
}
.rnp-meta {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 12px;
}
.rnp-month-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 14px;
  padding: 10px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.rnp-month-summary .stat {
  min-width: 110px;
}
.rnp-month-summary .stat-label {
  font-size: 12px;
  color: var(--text-muted);
}
.rnp-month-summary .stat-value {
  font-size: 15px;
  font-weight: 700;
}
.rnp-summary-group {
  margin-bottom: 16px;
}
.rnp-summary-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.rnp-card details {
  margin-top: 12px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}
.rnp-card summary {
  cursor: pointer;
  font-weight: 700;
  font-size: 13px;
  color: var(--accent);
  padding: 4px 0;
}
.rnp-card summary:hover { text-decoration: underline; }
