:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #fbfcfe;
  --line: #e4e8f0;
  --line-strong: #d3d9e6;
  --text: #1b2130;
  --muted: #737d93;
  --accent: #2f6bff;
  --accent-soft: #eaf0ff;
  --accent-ink: #1c49c9;
  --ok: #189a5a;
  --warn: #d9822b;
  --danger: #d64545;
  --shadow: 0 1px 2px rgba(23, 33, 56, .06), 0 6px 20px rgba(23, 33, 56, .05);
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent-ink); text-decoration: none; }

/* --------------------------------------------------------------- app bar */
.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 24px;
  height: 58px;
  background: rgba(255, 255, 255, .85);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand {
  font-weight: 700;
  letter-spacing: .2px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand::before {
  content: "";
  width: 22px; height: 22px;
  border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), #7aa2ff);
  box-shadow: 0 2px 8px rgba(47, 107, 255, .35);
}

.appbar nav { display: flex; gap: 6px; }
.appbar nav a {
  padding: 7px 14px;
  border-radius: 9px;
  color: var(--muted);
  font-weight: 500;
}
.appbar nav a:hover { background: var(--surface-2); color: var(--text); }
.appbar nav a.active { background: var(--accent-soft); color: var(--accent-ink); }

/* --------------------------------------------------------------- layout */
.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 20px;
  padding: 20px 24px 60px;
  align-items: start;
  max-width: 1560px;
  margin: 0 auto;
}
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
}

.sidebar { display: flex; flex-direction: column; gap: 16px; }
.content { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  min-width: 0;
}

.card h2 {
  margin: 0 0 12px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}

.card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.card-head h2 { margin: 0; }
.card-head .spacer { flex: 1; }

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

/* --------------------------------------------------------------- controls */
button, input, select {
  font: inherit;
  color: var(--text);
}
input, select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

button {
  padding: 8px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
  cursor: pointer;
  font-weight: 500;
  transition: background .15s, border-color .15s, color .15s;
}
button:hover { background: var(--surface-2); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button.primary:hover { background: #1f57e6; }
button.ghost { border-color: transparent; background: transparent; color: var(--muted); }
button.ghost:hover { background: var(--surface-2); color: var(--text); }
button:disabled { opacity: .5; cursor: default; }

/* chips (склады) */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  cursor: pointer;
  font-weight: 500;
  user-select: none;
}
.chip::before {
  content: "";
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--line-strong);
}
.chip:hover { border-color: var(--accent); }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); }
.chip.on::before { background: var(--accent); }

/* segmented control */
.segmented { display: flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 3px; }
.segmented button {
  flex: 1;
  border: 0;
  background: transparent;
  border-radius: 7px;
  padding: 7px 6px;
  color: var(--muted);
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.segmented.small { margin-left: auto; }
.segmented.small button { padding: 5px 10px; font-size: 12px; }

.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.presets button { padding: 5px 10px; font-size: 12px; border-radius: 8px; }

/* --------------------------------------------------------------- calendar */
.calendar { margin-top: 12px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.cal-title { font-weight: 600; }
.cal-nav { display: flex; gap: 4px; }
.cal-nav button { width: 30px; height: 30px; padding: 0; border-radius: 8px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.cal-dow { text-align: center; font-size: 11px; color: var(--muted); padding: 4px 0; }
.cal-day {
  position: relative;
  width: 100%;
  min-width: 0;
  height: 36px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  cursor: pointer;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  transition: background .12s, color .12s;
}
.cal-day:hover { background: var(--surface-2); }
.cal-day.other { color: #c3c9d6; }
.cal-day.today { border-color: var(--line-strong); }
.cal-day.in-range { background: var(--accent-soft); border-color: transparent; border-radius: 0; }
.cal-day.edge { background: var(--accent); color: #fff; border-radius: 8px; }
.cal-day.has-data::after {
  content: "";
  position: absolute;
  left: 50%; bottom: 4px;
  width: 4px; height: 4px;
  margin-left: -2px;
  border-radius: 50%;
  background: var(--accent);
}
.cal-day.edge.has-data::after { background: #fff; }

.range-label { margin-top: 10px; font-weight: 600; font-size: 13px; }
.hint { margin: 8px 0 0; font-size: 11px; color: var(--muted); line-height: 1.4; }

/* --------------------------------------------------------------- counterparties */
.cp-toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.cp-toolbar input { width: 220px; }
.cp-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 2px 18px;
  max-height: 340px;
  overflow: auto;
  padding-right: 4px;
  margin-top: 2px;
}
.cp-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 9px;
  border-radius: 9px;
  cursor: pointer;
  transition: background .12s;
}
.cp-item:hover { background: var(--surface-2); }
.cp-item:has(input:checked) { background: rgba(47, 107, 255, .07); }
.cp-item input { width: auto; }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.cp-item .cp-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cp-item:has(input:checked) .cp-name { font-weight: 600; }
.cp-item .cp-total { min-width: 52px; text-align: right; font-size: 12px; font-weight: 600; color: var(--accent-ink); font-variant-numeric: tabular-nums; }

.cp-selected { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.cp-selected:empty { display: none; }
.sel-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px 4px 11px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  max-width: 320px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sel-chip button {
  border: 0;
  background: transparent;
  color: var(--accent-ink);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  padding: 0 2px;
  border-radius: 50%;
}
.sel-chip button:hover { background: rgba(28, 73, 201, .12); }
.sel-more {
  border: 1px dashed var(--line-strong);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
}
.sel-more:hover { color: var(--accent-ink); border-color: var(--accent); background: transparent; }

/* --------------------------------------------------------------- summary */
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
}
.stat .v { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .k { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat.accent { background: var(--accent-soft); border-color: #cfe0ff; }
.stat.accent .v { color: var(--accent-ink); }

/* --------------------------------------------------------------- chart/table */
.chart { width: 100%; height: 300px; margin-bottom: 18px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-bottom: 10px; }
.chart-legend:empty { display: none; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.legend-item i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.legend-item b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

.table-wrap {
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: auto;
  max-height: 620px;
}
table { border-collapse: separate; border-spacing: 0; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
th {
  position: sticky; top: 0;
  background: var(--surface-2);
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  z-index: 1;
}
tbody tr:hover td { background: var(--surface-2); }
td.num, th.num { text-align: right; }
td.company, th.company { position: sticky; left: 0; background: var(--surface); z-index: 2; max-width: 280px; overflow: hidden; text-overflow: ellipsis; }
tbody tr:hover td.company { background: var(--surface-2); }
tfoot td { font-weight: 700; background: var(--surface-2); position: sticky; bottom: 0; }
td.zero { color: #c3c9d6; }
td.total, th.total { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }

.empty { padding: 32px; text-align: center; color: var(--muted); }

/* --------------------------------------------------------------- builder */
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 18px;
}
.fieldset { display: flex; flex-wrap: wrap; gap: 16px; padding: 12px 0; border-bottom: 1px dashed var(--line); }
.fieldset:last-child { border-bottom: 0; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 160px; }
.field > label { font-weight: 600; font-size: 12px; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 14px; max-width: 560px; }
.check { display: flex; align-items: center; gap: 6px; font-weight: 400; font-size: 13px; }
.check input { width: auto; }
.actions { justify-content: flex-end; }
.badge { display: inline-block; background: var(--accent-soft); color: var(--accent-ink); border-radius: 20px; padding: 3px 11px; font-size: 12px; font-weight: 600; }
.results-head { display: flex; align-items: center; gap: 10px; margin: 6px 0 12px; }
.sql { margin-top: 14px; }
.sql pre { background: #0f172a; color: #e2e8f0; padding: 14px; border-radius: 10px; overflow: auto; font-size: 12px; }

/* --------------------------------------------------------------- builder page */.builder { padding: 20px 24px 60px; max-width: 1180px; margin: 0 auto; }
.builder .results { min-width: 0; }
.builder .table-wrap { max-height: 620px; background: var(--surface); }
.builder td.metric, .builder th.metric, .builder td.period, .builder th.period { text-align: right; }
.builder td, .builder th { white-space: nowrap; }

/* --------------------------------------------------------------- login page */
.login-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, #e9f0ff 0%, var(--bg) 60%);
}
.login-card {
  width: 360px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-brand { font-weight: 700; font-size: 18px; text-align: center; margin-bottom: 4px; }
.login-brand::before {
  content: "";
  display: inline-block;
  width: 22px; height: 22px;
  margin-right: 8px;
  vertical-align: -4px;
  border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), #7aa2ff);
}
.login-field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.login-field input { font-weight: 400; color: var(--text); }
.login-submit { width: 100%; margin-top: 4px; }
.login-error {
  background: #fdecec;
  color: #b42318;
  border: 1px solid #f7c9c9;
  border-radius: 9px;
  padding: 9px 12px;
  font-size: 13px;
}

/* appbar: пользователь и выход */
.appbar .who { margin-left: auto; display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; }
.appbar .who form { margin: 0; }
.appbar .who button { padding: 5px 12px; font-size: 12px; }
