:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface-1: #fcfcfb;
  --border: #e4e3df;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #8a8984;
  --grid: #ecebe7;
  --series-1: #2a78d6;
  --series-2: #e0882f;
  --good: #0ca30c;
  --critical: #d03b3b;
  --accent: #2a78d6;
  --forecast: #7a5cc4;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110; --surface-1: #1a1a19; --border: #2c2c2a; --text-primary: #f0efec;
    --text-secondary: #c3c2b7; --text-muted: #8f8e86; --grid: #262624; --series-1: #3987e5; --series-2: #e8964a;
    --accent: #3987e5; --forecast: #a08ae0;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface-1: #1a1a19; --border: #2c2c2a; --text-primary: #f0efec;
  --text-secondary: #c3c2b7; --text-muted: #8f8e86; --grid: #262624; --series-1: #3987e5; --series-2: #e8964a;
  --accent: #3987e5; --forecast: #a08ae0;
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text-primary);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 15px; margin: 0; font-weight: 600; }
a { color: inherit; }
.neg { color: var(--critical); } .pos { color: var(--good); }
.warn { color: var(--critical); }

.page { max-width: 1400px; margin: 0 auto; padding: 20px 20px 60px; }
.page > * + * { margin-top: 16px; }
.bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.spacer { flex: 1; }
.back { text-decoration: none; color: var(--text-secondary); }
.back:hover { color: var(--text-primary); }

/* Элементы управления */
input, select {
  font: inherit; color: var(--text-primary); background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-height: 36px; min-width: 0;
}
.btn {
  font: inherit; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
  border-radius: 8px; padding: 7px 14px; min-height: 36px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--text-muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: none; border-color: transparent; color: var(--text-secondary); }
.btn.ghost:hover { border-color: var(--border); }
.btn.danger { color: var(--critical); }
.btn.sm { padding: 2px 10px; min-height: 28px; font-size: 12px; }
.btn:disabled { opacity: .6; cursor: progress; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface-1); max-width: 100%; }
.seg button { border: 0; background: none; padding: 7px 12px; font: inherit; color: var(--text-secondary); cursor: pointer; white-space: nowrap; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--bg); color: var(--text-primary); font-weight: 600; }
.check { display: inline-flex; align-items: center; gap: 8px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.narrow { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; }
.center { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.err { color: var(--critical); margin: 0; min-height: 1em; white-space: pre-line; }

/* Период */
.period { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.period .seg { overflow-x: auto; scrollbar-width: none; }
.period .custom { display: inline-flex; align-items: center; gap: 6px; color: var(--text-muted); }

/* Прогноз: отдельный цвет и метка — не путать с фактом */
.fc-tag {
  display: inline-block; font-size: 11px; font-weight: 600; letter-spacing: .02em; color: var(--forecast);
  border: 1px solid currentColor; border-radius: 999px; padding: 0 7px; line-height: 17px; vertical-align: 1px;
}
.fc-val { opacity: .7; }
.fc-item { white-space: nowrap; }
.info {
  display: inline-grid; place-items: center; width: 16px; height: 16px; border-radius: 50%; vertical-align: 1px;
  border: 1px solid var(--text-muted); color: var(--text-muted); font-size: 10.5px; font-weight: 600; line-height: 1;
  cursor: help; outline: none; font-style: normal; text-decoration: none;
}
.info:hover, .info:focus { color: var(--forecast); border-color: var(--forecast); }
#tip { max-width: 320px; }
.fc-val .pos, .fc-val .neg { font-style: italic; }
th.fc { color: var(--forecast) !important; }

/* Плитки проектов */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 16px; }
.tile {
  display: flex; flex-direction: column; gap: 8px; text-decoration: none; color: inherit;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; min-height: 160px;
  transition: border-color .15s, transform .15s;
}
.tile:hover { border-color: var(--text-muted); transform: translateY(-1px); }
.tile .name { font-weight: 600; font-size: 16px; }
.tile .big { font-size: 26px; font-weight: 650; letter-spacing: -.01em; }
.tile .row { display: flex; justify-content: space-between; gap: 8px; color: var(--text-secondary); font-size: 13px; }
.tile .fc-row { font-size: 12px; }
.tile-tr { border-top: 1px solid var(--border); padding-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.tile-tr .row { font-size: 12px; }
.tile .foot { margin-top: auto; color: var(--text-muted); font-size: 12px; }
.tile.add { align-items: center; justify-content: center; border-style: dashed; color: var(--text-secondary); font-size: 15px; cursor: pointer; font: inherit; }

/* Сводка */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.kpi { background: var(--bg); border-radius: 10px; padding: 12px 16px; }
.kpi .label { color: var(--text-secondary); font-size: 12px; }
.kpi .value { font-size: 26px; font-weight: 650; margin-top: 2px; letter-spacing: -.01em; }
.kpi .sub { font-size: 12px; color: var(--text-muted); }
.kpi .delta { font-size: 12px; margin-top: 2px; }
.fc-line { margin-top: 12px; display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px 8px; margin-top: 12px; }
.chip { border: 1px solid var(--border); border-radius: 999px; padding: 4px 12px; font-size: 13px; }
.chip .label { color: var(--text-secondary); }
.chip[title] { cursor: help; }
.chart-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 18px 0 8px; }
.chart-bar select { min-height: 34px; }
.chart { position: relative; height: 260px; }
#p-alltime { margin: 10px 0 0; }

/* Таблицы */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
th { font-weight: 500; color: var(--text-secondary); font-size: 12px; vertical-align: bottom; }
th span { font-weight: 400; color: var(--text-muted); font-size: 11px; }
th:first-child, td:first-child, .l { text-align: left; }
tbody tr:last-child td { border-bottom: 0; }
tfoot td { font-weight: 600; border-top: 1px solid var(--border); border-bottom: 0; }
tfoot .muted-row td { font-weight: 400; color: var(--text-secondary); }
td .sub { font-size: 11px; color: var(--text-muted); font-weight: 400; }
td.income { font-weight: 600; }

/* Трафферы → кабинеты */
.chev { display: inline-block; width: 14px; color: var(--text-muted); transition: transform .15s; }
.open > td .chev, .open > td > .chev { transform: rotate(90deg); }
.tr-row { cursor: pointer; background: var(--bg); }
.tr-row:hover td { background: var(--grid); }
.tr-row .tr-actions { margin-left: 6px; opacity: 0; transition: opacity .15s; }
.tr-row:hover .tr-actions, .tr-row.open .tr-actions { opacity: 1; }
.tr-row .tr-actions .btn { padding: 0 6px; min-height: 22px; }
.cab-row { cursor: pointer; }
.cab-row.static { cursor: default; }
.cab-row:hover td { background: var(--bg); }
.cab-row td:first-child { padding-left: 28px; }
.cab-row.open td { border-bottom-color: transparent; font-weight: 600; }
.cab-row .gear { padding: 0 6px; min-height: 22px; margin-left: 2px; opacity: .55; }
.cab-row:hover .gear, .cab-row .gear:focus { opacity: 1; }
.cab-detail > td { max-width: 0; text-align: left; white-space: normal; padding: 6px 10px 18px 28px; background: var(--bg); }
.detail { width: 100%; }

/* Раскрытый кабинет: три группы */
.groups { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 10px; margin-top: 6px; }
.grp { background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; }
.grp h4 { margin: 0 0 6px; font-size: 12px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .04em; }
.grp .ln { display: grid; grid-template-columns: 1fr auto; column-gap: 10px; padding: 3px 0; }
.grp .ln .label { color: var(--text-secondary); font-size: 13px; }
.grp .ln .value { font-weight: 600; text-align: right; }
.grp .ln .hint { grid-column: 1 / -1; font-size: 11px; color: var(--text-muted); margin-top: -2px; }
.grp .ln[title] .label { cursor: help; text-decoration: underline dotted var(--text-muted); text-underline-offset: 3px; }
.fc-grp { border-style: dashed; border-color: color-mix(in srgb, var(--forecast) 45%, var(--border)); }
.camp-h { display: flex; align-items: center; justify-content: space-between; margin: 14px 0 6px; }
.camp-h h3 { font-size: 13px; font-weight: 600; margin: 0; color: var(--text-secondary); }
#t-cabinets:not(.show-more) .more { display: none; }

/* Кампании и дни */
table.inner { background: var(--surface-1); border-radius: 8px; }
table.inner th, table.inner td { font-size: 12px; padding: 6px 7px; }
table.inner th span { font-size: 10.5px; }
table.inner td.camp { white-space: normal; min-width: 140px; max-width: 220px; }
.camp-row { cursor: pointer; }
.camp-row:hover td { background: var(--bg); }
.camp-row td.camp { position: relative; padding-left: 22px; }
.camp-row .chev { position: absolute; left: 6px; top: 7px; }
.camp-row.open .chev { transform: rotate(90deg); }
.camp-row.open td { border-bottom-color: transparent; font-weight: 600; }
.day-row td { color: var(--text-secondary); background: var(--bg); border-bottom-color: var(--surface-1); }
.day-row td.day { padding-left: 22px; }

/* Статус загрузки */
.dot { margin-left: 4px; font-size: 12px; }
.dot.ok { color: var(--good); } .dot.err { color: var(--critical); } .dot.off { color: var(--text-muted); }
/* Подсказка у значка статуса — отдельное плавающее окошко (#tip), чтобы его не обрезал край таблицы */
.dot[data-tip] { cursor: help; outline: none; }
#tip {
  position: fixed; z-index: 50; max-width: 260px; font-size: 12px; line-height: 1.35; color: var(--text-primary);
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .25); pointer-events: none;
}

/* Формы */
.cab-form {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; align-items: end;
  padding: 14px; margin-bottom: 14px; border-radius: 10px; background: var(--bg);
}
.cab-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-secondary); min-width: 0; }
.cab-form label input, .cab-form label select { width: 100%; }
.cab-form .actions, .cab-form .err { grid-column: 1 / -1; }
.manual-form { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; align-items: center; }
.manual-form .grow { flex: 1; min-width: 160px; }
.manual-form .orders-field { width: 80px; }
.pct-input { width: 80px; }
.copy { display: flex; gap: 8px; align-items: center; margin-bottom: 16px; }
code { font: 12px ui-monospace, Menlo, monospace; background: var(--bg); padding: 6px 8px; border-radius: 6px; word-break: break-all; }
details.fold summary { cursor: pointer; color: var(--text-secondary); font-weight: 600; }
details.fold[open] summary { margin-bottom: 12px; }
details.fold > p { margin-top: 0; }

/* Окна */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 16px; z-index: 10; overflow-y: auto; }
.modal-h { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.cab-settings {
  width: 100%; max-width: 720px; max-height: calc(100vh - 32px); overflow-y: auto;
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; align-items: end;
}
.cab-settings .modal-h, .cab-settings .full { grid-column: 1 / -1; margin: 0; }
.cab-settings label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-secondary); min-width: 0; }
.cab-settings select, .cab-settings input:not([type=checkbox]) { width: 100%; }
.camp-excl { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; display: flex; flex-direction: column; gap: 6px; }
.camp-excl legend { font-size: 12px; color: var(--text-secondary); padding: 0 4px; }
.cab-settings label.excl { flex-direction: row; align-items: flex-start; gap: 8px; font-size: 13px; color: var(--text-primary); cursor: pointer; }
.cab-settings label.excl input { margin: 2px 0 0; flex: none; width: auto; min-height: 0; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); max-width: calc(100% - 32px);
  background: var(--text-primary); color: var(--surface-1); padding: 10px 16px; border-radius: 8px; z-index: 20; white-space: pre-line;
}

/* Телефон */
@media (max-width: 800px) {
  .page { padding: 16px 16px 40px; }
  .kpis { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .kpi { padding: 10px 12px; }
  .kpi .value { font-size: 20px; }
  .bar .spacer { display: none; }
  .chart { height: 220px; }
  .cab-settings { grid-template-columns: 1fr; }
}
@media (max-width: 600px) {
  .card { padding: 14px 10px; }
  .cab-table .opt, .cab-table td .sub, .tr-row td:first-child .muted { display: none; }
  .cab-table th, .cab-table td { padding: 8px 4px; font-size: 12.5px; }
  .tr-row .tr-actions .btn { padding: 0 3px; }
  .cab-table td:first-child { white-space: normal; word-break: break-word; }
  .cab-row td:first-child { padding-left: 16px; }
  .cab-detail > td { padding: 6px 6px 14px; }
  .tr-row .tr-actions { opacity: 1; }
  .chip { font-size: 12px; padding: 3px 10px; }
  /* кнопки периода и графика — переносом, без скрытой прокрутки */
  .period .seg, #chart-mode { flex-wrap: wrap; gap: 6px; border: 0; background: none; overflow: visible; }
  .period .seg button, #chart-mode button { border: 1px solid var(--border) !important; border-radius: 8px; background: var(--surface-1); padding: 6px 10px; }
  .period .seg button.active, #chart-mode button.active { background: var(--bg); border-color: var(--text-muted) !important; }
  #t-manual td { white-space: normal; padding: 7px 4px; font-size: 12px; }
}

/* Кабинет = аккаунт Директа: логин и домены под названием */
.cab-meta { white-space: normal; font-weight: 400; margin-left: 18px; }
.cab-row.open .cab-meta { font-weight: 400; }
/* Ручной расход в настройках кабинета */
.ms-row { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.ms-row span { flex: 1; }
.ms-form { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 6px; }
.ms-form input { min-height: 32px; }
.ms-form input[name=ms_amount] { width: 120px; }
.ms-form input[name=ms_comment] { flex: 1; min-width: 120px; }
.cab-form .nt { grid-column: 1 / -1; flex-direction: row; }

/* Сценарий: бюджет в месяц */
.scn-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; align-items: end; margin-bottom: 8px; }
.scn-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-secondary); min-width: 0; }
.scn-form input { width: 100%; }
#scenario .chart { margin: 12px 0; }

#t-scn tr.scn-stop td { border-top: 2px dashed var(--text-muted); }
