:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --plane: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --good: #006300;
  --critical: #d03b3b;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;
  --series-0: #898781;

  --radius: 10px;
  --gap: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --plane: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --good: #0ca30c;
    --critical: #e66767;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --plane: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --good: #0ca30c;
  --critical: #e66767;

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--plane);
  color: var(--text-primary);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

a {
  color: inherit;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 20px;
  height: 52px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  flex: none;
}

.brand img {
  height: 26px;
  width: auto;
  display: block;
}

/* логотип тёмный на прозрачном — в тёмной теме выбеливаем */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .brand img {
    filter: brightness(0) invert(1);
  }
}

:root[data-theme="dark"] .brand img {
  filter: brightness(0) invert(1);
}

.balances {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.balance {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  white-space: nowrap;
}

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

.balance .amt {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.balance.err .amt {
  color: var(--critical);
}

@media (max-width: 1100px) {
  .balances {
    display: none;
  }
}

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

.nav a {
  padding: 6px 11px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text-secondary);
}

.nav a:hover {
  background: var(--plane);
}

.nav a.active {
  background: var(--plane);
  color: var(--text-primary);
  font-weight: 600;
}

.icon-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 5px 9px;
  font-size: 14px;
}

.icon-btn:hover {
  color: var(--text-primary);
}

.wrap {
  max-width: 1240px;
  margin: 0 auto;
  padding: 20px;
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.card h2 {
  margin: 0 0 14px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}

.grid {
  display: grid;
  gap: var(--gap);
}

.cols-4 {
  grid-template-columns: repeat(4, 1fr);
}

.cols-2 {
  grid-template-columns: 1fr 1fr;
}

.cols-2-1 {
  grid-template-columns: 2fr 1fr;
}

@media (max-width: 900px) {
  .cols-4,
  .cols-2,
  .cols-2-1 {
    grid-template-columns: 1fr;
  }
}

/* --- stat tiles --- */
.tile .label {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.tile .value {
  font-size: 30px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 6px 0 2px;
}

.tile .value.neg {
  color: var(--critical);
}

.tile .sub {
  color: var(--text-secondary);
  font-size: 13px;
}

.delta.up {
  color: var(--critical);
}

.delta.down {
  color: var(--good);
}

/* --- charts --- */
.bars {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 190px;
  padding-top: 22px;
  border-bottom: 1px solid var(--axis);
}

.bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  height: 100%;
  position: relative;
  min-width: 0;
}

.bar-col .cap {
  position: absolute;
  top: -20px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 11px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.seg {
  width: 100%;
  background: var(--series-0);
  margin-bottom: 2px;
}

.seg:first-child {
  border-radius: 4px 4px 0 0;
}

.seg:last-child {
  margin-bottom: 0;
}

.bar-single {
  width: 100%;
  background: var(--series-1);
  border-radius: 4px 4px 0 0;
}

.xaxis {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}

.xaxis span {
  flex: 1;
  text-align: center;
  font-size: 11px;
  color: var(--muted);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.s1 { background: var(--series-1); }
.s2 { background: var(--series-2); }
.s3 { background: var(--series-3); }
.s4 { background: var(--series-4); }
.s5 { background: var(--series-5); }
.s6 { background: var(--series-6); }
.s7 { background: var(--series-7); }
.s8 { background: var(--series-8); }
.s0 { background: var(--series-0); }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin-top: 14px;
  font-size: 12px;
  color: var(--text-secondary);
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 3px;
  flex: none;
}

/* горизонтальные бары (категории, подрядчики) */
.hbar-row {
  display: grid;
  grid-template-columns: 150px 1fr 96px;
  align-items: center;
  gap: 10px;
  padding: 5px 0;
}

.hbar-row .name {
  display: flex;
  align-items: center;
  gap: 7px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hbar-track {
  background: var(--grid);
  border-radius: 4px;
  height: 12px;
  overflow: hidden;
}

.hbar-fill {
  height: 100%;
  border-radius: 4px;
  background: var(--series-1);
}

.hbar-row .amount {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
}

/* --- tables --- */
table {
  width: 100%;
  border-collapse: collapse;
}

th {
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--grid);
  vertical-align: middle;
}

tr:last-child td {
  border-bottom: none;
}

td.num,
th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.scroll {
  overflow-x: auto;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}

/* --- forms --- */
input,
select,
button {
  font: inherit;
  color: inherit;
}

input[type="text"],
input[type="date"],
input[type="month"],
input[type="password"],
input[type="number"],
select {
  background: var(--plane);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-width: 0;
  width: 100%;
}

input:focus,
select:focus {
  outline: 2px solid var(--series-1);
  outline-offset: -1px;
}

.btn {
  background: var(--series-1);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  font-weight: 550;
}

.btn.ghost {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.btn.danger {
  background: none;
  border: none;
  color: var(--muted);
  padding: 2px 6px;
  cursor: pointer;
}

.btn.danger:hover {
  color: var(--critical);
}

.quick-form {
  display: grid;
  grid-template-columns: 120px 110px 1fr 150px 150px 130px auto;
  gap: 8px;
  align-items: center;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 14px;
}

.filters input,
.filters select {
  width: auto;
}

.hint {
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 1000px) {
  .quick-form {
    grid-template-columns: 1fr 1fr;
  }
}

.login-box {
  max-width: 320px;
  margin: 15vh auto;
}

.login-box input {
  margin: 12px 0;
}

.err {
  color: var(--critical);
  font-size: 13px;
}

.row-edit {
  display: none;
}

tr.editing .row-view {
  display: none;
}

tr.editing .row-edit {
  display: table-row;
}
