/* Главный экран. Без сборки; ВСЕ цвета — переменные. Две темы: data-theme = light | dark (по умолчанию light).
   Палитра снята пикселями со скриншотов сайта cntrl.pro: светлая — фон #F3F5F9, карточки #FFFFFF, основная кнопка #02071B,
   акцент #2A56F5; тёмная — фон #02071B, карточки #0A1230, основная кнопка #6F8CFF с тёмным текстом. */
:root {
  color-scheme: light;
  --bg: #f3f5f9; --bg-filters: #ffffff; --text: #02071b; --text-2: #3a4460; --muted: #586176;
  --border: #c7cedc; --border-soft: #dce1eb; --grey: #eef1f7; --grey-2: #e2e7f1;
  --surface: #ffffff; --head-bg: #f3f5f9; --head-text: #1b2440;
  --accent-fg: #1537b8; --accent: #2a56f5; --accent-hover: #1f45d6; --accent-text: #ffffff; --accent-soft: #e7ecff; --accent-border: #c7cedc;
  --btn-bg: #02071b; --btn-fg: #ffffff; --btn-hover: #1a2142;
  --grand: #e7ecff; --grand-line: #2a56f5; --total: #eef1f8; --open: #f7f9fc; --tag: #e7ecff;
  --green: #0f7b4a; --green-soft: #e6f6ee;
  --danger: #b42318; --danger-soft: #fef3f2; --danger-border: #f3b4ae; --danger-hover: #fee4e2;
  --warn-bg: #fffaeb; --warn-border: #fedf89; --warn-text: #7a2e0e;
  --row-hover: rgba(42, 86, 245, .05); --shadow: rgba(2, 7, 27, .16);
  --tip-bg: #02071b; --tip-text: #edf0f8;
  --r-btn: 12px; --r-box: 20px;
  --head1-h: 30px; --head-h: 72px;
  --indent: 18px; --pad: 8px; --tree-min: 220px; --tree-max: 340px;
  --cell-bg: var(--surface); --cell-bg-group: var(--open); --cell-bg-total: var(--total); --cell-bg-grand: var(--grand);
  --pin-shadow: rgba(2, 7, 27, .16);
}
:root[data-theme="dark"] { color-scheme: dark;
  --bg: #02071b; --bg-filters: #0a1230; --text: #edf0f8; --text-2: #c4cbde; --muted: #a0a9c0;
  --border: #2a3767; --border-soft: #1c2750; --grey: #111a3f; --grey-2: #1c2750;
  --surface: #0a1230; --head-bg: #0e1738; --head-text: #c9d0e6;
  --accent-fg: #9db0ff; --accent: #6f8cff; --accent-hover: #8299ff; --accent-text: #02071b; --accent-soft: #141e4a; --accent-border: #2a3767;
  --btn-bg: #6f8cff; --btn-fg: #02071b; --btn-hover: #8299ff;
  --grand: #16225a; --grand-line: #6f8cff; --total: #0e1738; --open: #0c1436; --tag: #1c2750;
  --green: #4ade80; --green-soft: #12301f;
  --danger: #ffb3ba; --danger-soft: #36111d; --danger-border: #6b2433; --danger-hover: #4a1826;
  --warn-bg: #3a2f12; --warn-border: #6b5518; --warn-text: #fde68a;
  --row-hover: rgba(111, 140, 255, .09); --shadow: rgba(0, 0, 0, .6);
  --tip-bg: #edf0f8; --tip-text: #02071b; --pin-shadow: rgba(0, 0, 0, .6); }
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 Manrope, Inter, "Segoe UI Variable", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; -webkit-font-smoothing: antialiased; color: var(--text); background: var(--bg); }

/* Кнопки: единая высота 32px */
button { font: inherit; cursor: pointer; border: 1px solid var(--border); background: var(--surface);
         border-radius: var(--r-btn); padding: 0 16px; height: 36px; font-weight: 500; color: var(--text); line-height: 1.2;
         transition: background .12s, border-color .12s, color .12s; }
button:hover { background: var(--grey); border-color: var(--muted); }
button:active { background: var(--grey-2); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Отдельные кнопки с промежутками (периоды, уровни); активная — акцентная заливка */
.btns { display: inline-flex; flex-wrap: wrap; gap: 8px; }
.btns button { color: var(--text); border-color: var(--border); background: var(--surface); }
.btns button:hover { background: var(--grey); border-color: var(--muted); }
.btns button.is-active { background: var(--btn-bg); border-color: var(--btn-bg); color: var(--btn-fg); font-weight: 600; }
.btns button.is-active:hover { background: var(--btn-hover); border-color: var(--btn-hover); }
.btn-danger { color: var(--danger); border-color: var(--danger-border); background: var(--danger-soft); }
.btn-danger:hover { background: var(--danger-hover); border-color: var(--danger); }
.btn-neutral { color: var(--text-2); }
.btn-logout { color: var(--text-2); }
input, select { font: inherit; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
.warn { background: var(--warn-bg); color: var(--warn-text); border: 1px solid var(--warn-border); padding: 6px 10px; border-radius: 10px; }
.notice { background: var(--grey); color: var(--text); padding: 6px 10px; border-radius: 10px; }
.notice.error, #message.error { background: var(--danger-soft); border: 1px solid var(--danger-border); color: var(--danger); }

.topbar { display: flex; justify-content: space-between; align-items: center; padding: 14px 24px;
          background: var(--bg); border-bottom: 1px solid var(--border-soft); flex-wrap: wrap; gap: 8px; }
.topbar h1 { font-size: 20px; font-weight: 700; letter-spacing: -.01em; margin: 0; }
.topbar-right { display: flex; gap: 12px; align-items: center; }

.filters { padding: 18px 20px; display: grid; gap: 16px; background: var(--bg-filters); margin: 16px 24px 0;
           border: 1px solid var(--border-soft); border-radius: var(--r-box); }
.period, .selects, .actions { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; }
.filters label, .field { display: grid; gap: 4px; font-size: 12px; color: var(--text-2); }
.field-label { font-size: 12px; color: var(--text-2); }
.date-field input { height: 36px; width: 148px; padding: 0 10px; border: 1px solid var(--border); border-radius: var(--r-btn);
                     background: var(--surface); color: var(--text); font: inherit; }
.date-field input:focus { border-color: var(--accent); outline: 0; }
#f-contains { height: 36px; padding: 0 10px; border: 1px solid var(--border);
                              border-radius: var(--r-btn); background: var(--surface); color: var(--text); }
#f-contains:focus { border-color: var(--accent); outline: 0; }
.selects label { min-width: 200px; max-width: 280px; }
.selects select[multiple] { min-height: 64px; }

/* Свой мультивыбор (поверх <select multiple>, который скрывается после инициализации) */
.ms { position: relative; }
.ms-control { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; min-height: 36px;
              padding: 3px 24px 3px 5px; border: 1px solid var(--border); border-radius: var(--r-btn);
              background: var(--surface); cursor: text; position: relative; color: var(--text); font-size: 14px; }
.ms-control:focus-within { border-color: var(--accent); }
.ms-tags { display: contents; }
.ms-tag { display: inline-flex; align-items: center; max-width: 100%; background: var(--accent-soft); color: var(--accent-fg);
          border-radius: 8px; padding: 1px 3px 1px 8px; font-size: 13px; gap: 2px; }
.ms-tag-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; }
.ms-tag-x, .ms-clear { border: 0; background: transparent; padding: 0 4px; line-height: 1.3;
                       color: var(--text-2); border-radius: 3px; height: auto; }
.ms-tag-x:hover, .ms-clear:hover { background: var(--grey-2); color: var(--danger); border: 0; }
.ms-tag-x, .ms-clear { border: 0; }
.ms-clear { position: absolute; right: 2px; top: 3px; font-size: 16px; }
.ms-input { flex: 1 1 60px; min-width: 60px; border: 0; outline: 0; padding: 2px; background: transparent; }
.ms-list { position: absolute; z-index: 20; left: 0; right: 0; top: 100%; margin-top: 2px;
           max-height: 240px; overflow-y: auto; background: var(--surface); border: 1px solid var(--border);
           border-radius: 12px; box-shadow: 0 8px 24px var(--shadow); color: var(--text); font-size: 14px; }
.ms-list[hidden] { display: none; }
.ms-opt { padding: 5px 8px; cursor: pointer; overflow-wrap: anywhere; }
.ms-opt.is-active { background: var(--grey); }
.ms-opt.is-selected { background: var(--accent-soft); font-weight: 600; }
.ms-opt.is-selected::before { content: "✓ "; color: var(--accent); }
.ms-empty { padding: 6px 8px; color: var(--muted); font-size: 12px; }
@media (max-width: 600px) {
  .selects label { min-width: 100%; max-width: none; }
  .ms-tag-text { max-width: 60vw; }
}
.expand-tools { display: inline-flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.login { max-width: 360px; margin: 15vh auto; padding: 28px; display: grid; gap: 14px; background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--r-box); }
.login a { color: var(--accent-fg); }
.login button[type="submit"] { background: var(--btn-bg); border-color: var(--btn-bg); color: var(--btn-fg); font-weight: 600; }
.login button[type="submit"]:hover { background: var(--btn-hover); }
.login form { display: grid; gap: 10px; }
.login label { display: grid; gap: 3px; }
.login input { padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--r-btn); background: var(--surface); color: var(--text); }

/* Таблица: шапка и общий итог закреплены при прокрутке */
.table-wrap { overflow: auto; max-height: calc(100vh - 260px); background: var(--surface); margin: 16px 24px 24px;
              border: 1px solid var(--border-soft); border-radius: var(--r-box); }
.table-wrap.loading { opacity: .6; }
table { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
th, td { padding: 6px 12px; border-bottom: 1px solid var(--border-soft); white-space: nowrap; color: var(--text); }
th { background: var(--head-bg); text-align: left; font-weight: 600; vertical-align: bottom; font-size: 12.5px;
     position: sticky; top: 0; z-index: 3; border-bottom: 1px solid var(--border); white-space: normal; min-width: 72px; max-width: 110px;
     line-height: 1.25; color: var(--head-text); }
th[data-tip] { cursor: help; }
th[data-tip]:hover, th[data-tip]:focus-visible { background: var(--grey-2); }
tbody tr:hover td { background-image: linear-gradient(var(--row-hover), var(--row-hover)); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.tree-col { min-width: var(--tree-min); max-width: var(--tree-max); width: var(--tree-max); text-align: left; white-space: normal;
            overflow-wrap: anywhere; position: sticky; left: 0; z-index: 1; background: var(--cell-bg);
            border-right: 1px solid var(--border-soft); }
td.tree-col { padding-left: calc(var(--pad) + var(--lvl, 0) * var(--indent)); }
th.tree-col { z-index: 5; background: var(--head-bg); }
th.tree-col[data-tip]:hover, th.tree-col[data-tip]:focus-visible { background: var(--grey-2); }
.tree-col .label { vertical-align: middle; }
/* Тень справа у закреплённой колонки — только когда таблица прокручена вправо (класс ставит JS) */
.table-wrap.is-scrolled .tree-col { border-right-color: var(--border); box-shadow: 4px 0 6px -2px var(--pin-shadow); }
/* Двухуровневая шапка: 1-й ряд — группы (липнет к top:0), 2-й — подзаголовки (липнет под ним).
   Заголовки без группы растянуты на оба ряда (rowspan=2). Везде выравнивание по низу; у чисел — вправо. */
thead tr.head-2 th { top: var(--head1-h); white-space: nowrap; max-width: none; }
th.group { text-align: center; vertical-align: middle; height: var(--head1-h); color: var(--text); font-size: 12px;
           letter-spacing: .02em; border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
thead tr.head-2 th.conv-first { border-left: 1px solid var(--border); }
thead tr.head-2 th.conv-last { border-right: 1px solid var(--border); }
/* Переключатель темы: одна квадратная кнопка-значок (солнце в светлой теме, луна в тёмной) */
.theme-toggle { width: 36px; flex: 0 0 36px; padding: 0; display: inline-flex; align-items: center; justify-content: center; }
.theme-toggle svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.theme-toggle .i-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .i-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .i-moon { display: block; }

.row-grand td { color: var(--text); position: sticky; top: var(--head-h); z-index: 2; background: var(--grand);
                font-weight: 700; border-bottom: 2px solid var(--grand-line); }
.row-grand td.tree-col { left: 0; z-index: 4; background: var(--cell-bg-grand); }
.row-total td { background: var(--cell-bg-total); font-weight: 600; border-top: 1px solid var(--border); }
.row-group.is-open td { background: var(--cell-bg-group); }
.toggle { width: 22px; height: 22px; padding: 0; border-radius: 8px; margin-right: 6px; line-height: 1; }
.toggle-gap { display: inline-block; width: 28px; }

/* Проекты: название и переключатель в шапке (переключатель виден только при >= 2 проектов) */
.project-name { font-weight: 600; }
.project-switch { display: flex; gap: 6px; align-items: center; }
.project-switch[hidden] { display: none; }
.project-list { list-style: none; padding: 0; }
.project-list li { margin: 8px 0; }

/* Тултип заголовков: узкий столбик с переносом строк */
.tip { position: fixed; z-index: 100; max-width: 240px; width: max-content; padding: 10px 14px;
       background: var(--tip-bg); color: var(--tip-text); border-radius: 12px; font-size: 13px; line-height: 1.4;
       white-space: pre-line; overflow-wrap: break-word; box-shadow: 0 6px 18px var(--shadow); pointer-events: none; }
.tip[hidden] { display: none; }
@media (max-width: 600px) {
  :root { --indent: 10px; --tree-min: 120px; --tree-max: 42vw; }
  :root { --pad: 6px; }
  .topbar { padding: 12px 14px; }
  .filters { margin: 12px 12px 0; padding: 14px; border-radius: 16px; }
  .table-wrap { margin: 12px 12px 16px; border-radius: 16px; }
  .toggle-gap { width: 22px; }
  .btns button { flex: 1 1 auto; }
  .expand-tools { width: 100%; }
}
