:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --border: #e3e5e8;
  --accent: #005bff;
  --accent-text: #ffffff;
  --income-bg: #eaf6ee;
  --income-line: #2e9d57;
  --expense-bg: #fbeeee;
  --expense-line: #d64545;
  --neutral-bg: #f0f2f5;
  --neutral-line: #9aa3ae;
  --good: #1f8a4c;
  --bad: #c83737;
  --warn-bg: #fff6e0;
  --warn-text: #7a5600;
  --radius: 12px;
  --chart-1: #005bff;
  --chart-2: #2e9d57;
  --chart-3: #d64545;
  --chart-4: #e39a19;
  --chart-grid: #e3e5e8;
  --chart-hit: rgba(0, 0, 0, 0.05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1a1d23;
    --text: #e8eaed;
    --muted: #9aa0a8;
    --border: #2c3038;
    --accent: #4d8dff;
    --income-bg: #16261d;
    --income-line: #4cc27a;
    --expense-bg: #2a1a1b;
    --expense-line: #ef6b6b;
    --neutral-bg: #1f232a;
    --neutral-line: #6b7480;
    --good: #5ad08a;
    --bad: #ff7b7b;
    --warn-bg: #2d2513;
    --warn-text: #f1c965;
    --chart-1: #4d8dff;
    --chart-2: #4cc27a;
    --chart-3: #ef6b6b;
    --chart-4: #f0b649;
    --chart-grid: #2c3038;
    --chart-hit: rgba(255, 255, 255, 0.07);
  }
}

/* ---------- графики */
.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--chart-grid); stroke-width: 1; }
.chart .zero { stroke: var(--muted); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 12px; }
.chart .hit { fill: transparent; }
.chart .hit:hover { fill: var(--chart-hit); }
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 13px; color: var(--muted); margin: 6px 0 2px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin-right: 5px; }
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(440px, 1fr)); gap: 16px; }
@media (max-width: 500px) { .charts { grid-template-columns: 1fr; } }

/* ---------- таблица товаров */
.products th, .products td { white-space: nowrap; font-size: 13.5px; padding: 7px 8px; }
.products th[data-sort] { cursor: pointer; user-select: none; }
.products th[data-sort]:hover { color: var(--text); }
.products th[data-dir="asc"]::after { content: " ↑"; }
.products th[data-dir="desc"]::after { content: " ↓"; }
.products th:first-child, .products td:first-child {
  position: sticky; left: 0; background: var(--surface); white-space: normal; min-width: 220px; max-width: 320px;
}
.products tfoot td { font-weight: 600; border-top: 2px solid var(--border); }
.products tr:hover td { background: var(--neutral-bg); }
.products td.neg { color: var(--bad); }
.products td.pos { color: var(--good); }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input[type="search"] { flex: 1 1 220px; }
details summary { cursor: pointer; color: var(--accent); margin-bottom: 8px; }

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- шапка */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-inner {
  max-width: 1500px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 17px; color: var(--text); }
.brand span { color: var(--accent); }
.nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.nav a {
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 8px;
  font-weight: 500;
}
.nav a:hover { background: var(--neutral-bg); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--neutral-bg); color: var(--text); }

.main { max-width: 1500px; margin: 0 auto; padding: 20px 16px 48px; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 0 0 12px; }
.subtitle { color: var(--muted); margin: 0 0 16px; font-size: 14px; }

/* ---------- кнопки и формы */
button, .button {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-block;
}
button:hover, .button:hover { border-color: var(--muted); text-decoration: none; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.link { border: none; background: none; color: var(--muted); padding: 6px 8px; }
button.danger { color: var(--bad); }
input, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-width: 0;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}
.notice { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.notice.ok { background: var(--income-bg); color: var(--good); }
.notice.error { background: var(--expense-bg); color: var(--bad); }
.notice.warn { background: var(--warn-bg); color: var(--warn-text); }

/* ---------- выбор периода */
.period {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 16px;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}
.chip:hover { text-decoration: none; border-color: var(--muted); }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.dates { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

/* ---------- плитки */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 10px;
}
.tile {
  background: var(--neutral-bg);
  border-left: 4px solid var(--neutral-line);
  border-radius: 10px;
  padding: 12px 14px;
  min-height: 108px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.tile.income { background: var(--income-bg); border-left-color: var(--income-line); }
.tile.expense { background: var(--expense-bg); border-left-color: var(--expense-line); }
.tile-title { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.hint {
  display: inline-flex;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid var(--muted);
  font-size: 11px;
  align-items: center;
  justify-content: center;
  cursor: help;
  flex: none;
}
.tile-value { font-size: 22px; font-weight: 650; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
.tile-extra { font-size: 15px; font-weight: 500; color: var(--muted); }
.tile-compare { font-size: 12.5px; color: var(--muted); margin-top: auto; font-variant-numeric: tabular-nums; }
.good { color: var(--good); font-weight: 600; }
.bad { color: var(--bad); font-weight: 600; }

/* ---------- таблицы */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 13px; color: var(--muted); font-weight: 500; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table-wrap { overflow-x: auto; }
.inline-form { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.inline-form input[name="name"] { flex: 1 1 200px; }
.inline-form input[name="amount"] { width: 130px; }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- вход */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }
.login-card form > div { margin-bottom: 12px; }
.login-card input { width: 100%; }
.login-card button { width: 100%; padding: 10px; }
code { background: var(--neutral-bg); padding: 1px 5px; border-radius: 4px; font-size: 13px; }

@media (max-width: 600px) {
  .topbar-inner { gap: 8px; }
  .tiles { grid-template-columns: 1fr 1fr; }
  .tile-value { font-size: 18px; }
  .tile { min-height: 96px; padding: 10px; }
}
@media (max-width: 380px) {
  .tiles { grid-template-columns: 1fr; }
}
