* { box-sizing: border-box; }

/* Тема: тёмная по умолчанию, светлая через html[data-theme="light"] */
:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel2: #1d212a;
  --border: #262b36;
  --text: #e5e9f0;
  --text-dim: #8b93a3;
  --text-faint: #5b6270;
  --accent: #4f8ef7;
  --accent-contrast: #fff;
  --pos: #34d399;
  --neg: #f87171;
  --warn: #fbbf24;
  --chart-line: #4f8ef7;
  --chart-fill: rgba(79, 142, 247, 0.10);
  --dd-fill: rgba(248, 113, 113, 0.22);
  --shadow: rgba(0,0,0,.4);
}
html[data-theme="light"] {
  --bg: #f7f8fa;
  --panel: #ffffff;
  --panel2: #f3f4f6;
  --border: #e5e7eb;
  --text: #1a1a1a;
  --text-dim: #6b7280;
  --text-faint: #9ca3af;
  --accent: #2563eb;
  --accent-contrast: #fff;
  --pos: #15803d;
  --neg: #dc2626;
  --warn: #b45309;
  --chart-line: #2563eb;
  --chart-fill: rgba(37,99,235,0.08);
  --dd-fill: rgba(220, 38, 38, 0.15);
  --shadow: rgba(0,0,0,.15);
}

body {
  font-family: -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  padding: 24px;
}
.wrap { max-width: 1280px; margin: 0 auto; }
h1 { font-size: 20px; margin: 0 0 4px; }
.top-sub { color: var(--text-dim); font-size: 13px; margin-bottom: 20px; }

.summary-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}
.card .label { color: var(--text-dim); font-size: 12px; }
.card .value { font-size: 22px; font-weight: 700; margin-top: 4px; }
.card .sub { color: var(--text-dim); font-size: 12px; margin-top: 2px; }

.pos { color: var(--pos); font-weight: 600; }
.neg { color: var(--neg); font-weight: 600; }
.muted { color: var(--text-faint); }
.cell-usd { display: block; font-size: 11px; color: var(--text-faint); font-weight: 400; }

/* Панель управления списком фондов */
.funds-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }

/* Панель сверки План↔Факт */
.recon-card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; margin-bottom: 16px; }
.recon-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 10px; gap: 12px; }

.fund-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 12px;
  overflow: hidden;
  transition: box-shadow .15s, border-color .15s;
}
/* Свёрнутый фонд — компактнее, без нижней границы у шапки */
.fund-card.collapsed .fund-header { border-bottom: none; }
.fund-card.collapsed:hover { border-color: var(--text-faint); }
/* Раскрытый фонд — визуально выделен: акцентная левая полоса, тень, приподнят */
.fund-card.expanded {
  border-color: var(--accent);
  box-shadow: 0 4px 20px var(--shadow);
  margin-bottom: 20px;
}
.fund-card.expanded .fund-header {
  background: var(--panel2);
  border-bottom: 2px solid var(--accent);
}

.fund-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.fund-header .title { font-weight: 700; font-size: 15px; }
.fund-header .chevron { color: var(--accent); margin-right: 6px; font-size: 13px; }
.fund-header:hover { background: var(--panel2); }
/* Тело раскрытого фонда — отступ от шапки, чтобы таблица не сливалась в простыню */
.fund-body { padding: 4px 6px 8px; }
.fund-body table { background: var(--panel); }

/* Помесячная доходность раскрытого фонда. Полоса живёт над таблицей счетов:
   сначала «как шло по месяцам», потом «из чего это сложилось по счетам». */
.months-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 8px;
  padding: 8px 8px 10px;
}
.months-strip:empty { display: none; }
.months-label {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-right: 2px;
}
.month-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel2);
  font-size: 12.5px;
  white-space: nowrap;
}
/* Текущий месяц ещё не закончился — отмечаем пунктиром, чтобы его не читали
   наравне с закрытыми. */
.month-chip.is-current { border-style: dashed; }
/* Края месяца не наблюдались — цифры за него нет и быть не может. */
.month-chip.is-unmeasured { opacity: 0.6; border-style: dotted; }
.month-chip.is-unmeasured b { font-weight: 500; font-size: 12px; }
.month-chip .month-name { color: var(--text-dim); }
.month-chip b { font-size: 13.5px; font-variant-numeric: tabular-nums; }
.month-chip .month-usd { font-size: 11.5px; opacity: 0.75; font-variant-numeric: tabular-nums; }

@media (max-width: 640px) {
  .months-strip { gap: 5px 6px; padding: 6px 4px 8px; }
  .month-chip { font-size: 11.5px; padding: 3px 8px; }
  .month-chip .month-usd { display: none; }
}
.fund-body thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; }
.fund-header .fund-nav { font-size: 13px; color: var(--text-dim); margin-left: 10px; }
.fund-stats { display: block; margin-top: 6px; font-size: 13.5px; color: var(--text-dim); white-space: nowrap; overflow-x: auto; }
.fund-stats .fs-item { margin-right: 16px; white-space: nowrap; }
.fund-stats b { font-size: 15px; }

table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-faint);
  font-weight: 600;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 10px 12px;
  font-size: 13.5px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--panel2);
}
.badge.ok { color: var(--pos); }
.badge.rate_limited { color: var(--warn); }
.badge.auth_error, .badge.network_error { color: var(--neg); }
/* Данные пришли, но им нельзя верить — отличаем от обычной ошибки связи. */
.badge.partial_data, .badge.nav_anomaly {
  color: var(--neg);
  background: color-mix(in srgb, var(--neg) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--neg) 40%, transparent);
  font-weight: 600;
}
.badge.pending { color: var(--text-dim); }
.badge.manual { color: var(--accent); }
/* «Нет синка» перекрывает обычный статус и должен быть заметен: счёт, не
   обновлявшийся неделю, раньше светился зелёным «OK». */
.badge.stale {
  color: var(--warn);
  background: color-mix(in srgb, var(--warn) 16%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 45%, transparent);
  font-weight: 600;
}
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }

.tag {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 5px;
  background: var(--panel2);
  color: var(--text-dim);
  font-weight: 600;
}

button {
  font-family: inherit;
  font-size: 13px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { background: var(--panel2); }
button.primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
button.primary:hover { opacity: .9; }
button.danger { color: var(--neg); border-color: var(--neg); }
button.small { padding: 3px 8px; font-size: 12px; }
.btn-link { text-decoration: none; }

.row-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; max-width: 260px; }
.toolbar .row-actions { max-width: none; flex-wrap: nowrap; }
.earn-sub { font-size: 11px; color: var(--text-dim); margin-top: 3px; line-height: 1.5; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
.modal {
  background: var(--panel); border-radius: 10px; padding: 20px;
  width: 440px; max-width: 92vw; max-height: 88vh; overflow-y: auto;
  border: 1px solid var(--border);
  box-shadow: 0 10px 40px var(--shadow);
}
.modal h3 { margin-top: 0; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13.5px; background: var(--panel2); color: var(--text);
}
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.error-msg { color: var(--neg); font-size: 12.5px; margin-top: 8px; }
.hint { font-size: 11.5px; color: var(--text-faint); margin-top: 12px; line-height: 1.4; }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.empty { padding: 40px; text-align: center; color: var(--text-faint); }

.tabs { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 1px solid var(--border); }
.tab-btn {
  border: none; background: none; border-radius: 0; padding: 8px 4px; margin-right: 16px;
  font-size: 14px; color: var(--text-dim); border-bottom: 2px solid transparent; cursor: pointer;
}
.tab-btn:hover { background: none; color: var(--text); }
.tab-btn.active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }

/* Алерты */
#alertsBar { margin-bottom: 14px; }
.alert-item {
  display: flex; align-items: baseline; gap: 8px;
  border: 1px solid var(--border); border-left: 3px solid var(--warn);
  background: var(--panel); border-radius: 8px;
  padding: 8px 12px; margin-bottom: 6px; font-size: 13px;
}
.alert-item.error { border-left-color: var(--neg); }
.alert-item .a-title { font-weight: 600; }
.alert-item .a-msg { color: var(--text-dim); font-size: 12.5px; }

/* Спарклайн в таблице */
.spark { display: block; }
.spark polyline { fill: none; stroke-width: 1.5; }

/* Аналитика */
.analytics-layout { display: grid; grid-template-columns: 240px 1fr; gap: 16px; align-items: start; }
.picker-card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px; }
.picker-card h4 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; color: var(--text-faint); }
.picker-fund { margin-bottom: 10px; }
.picker-fund-name { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.picker-row { display: flex; align-items: center; gap: 6px; font-size: 13px; padding: 2px 0; }
.picker-row label { cursor: pointer; }

.chart-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; flex-wrap: wrap; gap: 8px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: none; border-radius: 0; border-right: 1px solid var(--border); padding: 6px 12px; }
.seg button:last-child { border-right: none; }
.seg button.active { background: var(--accent); color: var(--accent-contrast); }

.chart-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 16px; margin-bottom: 12px; height: 320px; }
.chart-wrap.small { height: 130px; }
.chart-title { font-size: 12px; color: var(--text-dim); margin: 0 0 6px; }
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.stat-note { grid-column: 1 / -1; font-size: 12px; color: var(--text-faint); }

/* Помесячная таблица */
.monthly-card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; margin-bottom: 16px; overflow-x: auto; }
.monthly-card h4 { margin: 0 0 10px; font-size: 13px; }
.monthly-table td, .monthly-table th { padding: 6px 8px; font-size: 12.5px; text-align: right; }
.monthly-table th:first-child, .monthly-table td:first-child { text-align: left; }
.mcell { border-radius: 4px; padding: 2px 4px; display: inline-block; min-width: 46px; }

@media (max-width: 900px) {
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .analytics-layout { grid-template-columns: 1fr; }
}

/* раскрытая строка счёта */
.expand-row td { background: var(--bg-soft, rgba(128,128,128,0.06)); border-top: none; }
button.ghost { background: transparent; border: none; color: var(--text-dim); cursor: pointer; }
button.ghost:hover { color: var(--text); }

/* Отчёт: таблица портфеля/отчёта */
.pf-table th, .pf-table td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: normal; }
.pf-table thead th { font-size: 11px; text-transform: uppercase; color: var(--text-dim); font-weight: 600; }
.pf-table tfoot td { border-top: 2px solid var(--border); }

/* Печать: только отчёт, без навигации и кнопок */
@media print {
  body * { visibility: hidden; }
  #reportPrintable, #reportPrintable * { visibility: visible; }
  #reportPrintable { position: absolute; left: 0; top: 0; width: 100%; }
  .no-print, .no-print-flex .no-print { display: none !important; }
  .pf-table th, .pf-table td { color: #000 !important; }
}

/* ============ Мобильный компактный режим ============ */
@media (max-width: 640px) {
  body { padding: 10px; -webkit-text-size-adjust: 100%; }
  .wrap { max-width: 100%; }
  h1 { font-size: 17px; }
  .top-sub { font-size: 12px; margin-bottom: 12px; }

  /* Тулбар: заголовок сверху, кнопки в ряд с переносом ниже */
  .toolbar { flex-direction: column; align-items: stretch; gap: 8px; }
  .toolbar .row-actions { max-width: none; flex-wrap: wrap; justify-content: flex-start; }
  .toolbar .row-actions button, .toolbar .row-actions .btn-link { flex: 1 1 auto; }

  /* Сводка: AUM на всю ширину сверху, остальные 4 — симметрично 2×2 */
  .summary-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 12px; align-items: stretch; }
  .summary-grid .card:first-child { grid-column: 1 / -1; }
  .card { padding: 10px 12px; }
  .card .value { font-size: 18px; }

  /* Вкладки — горизонтальный скролл, не ломаются */
  .tabs { overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
  .tab-btn { margin-right: 12px; white-space: nowrap; font-size: 13px; }

  /* Шапка фонда: имя и статистика в столбик, кнопки ниже — переносятся, не режутся */
  .fund-header { flex-direction: column; align-items: stretch; gap: 8px; padding: 12px; }
  .fund-header .row-actions { max-width: none; width: 100%; flex-wrap: wrap; justify-content: flex-start; gap: 6px; }
  .fund-header .row-actions button { flex: 0 1 auto; }
  .fund-stats { white-space: normal; overflow-x: visible; }
  .fund-stats .fs-item { display: inline-block; margin: 0 12px 4px 0; }
  /* строки счетов внутри фонда — тоже кнопки переносим */
  .row-actions { max-width: none; }

  /* Таблицы — компактнее, скролл по горизонтали остаётся у обёрток overflow-x */
  th, td { padding: 7px 8px; font-size: 12px; }
  .pf-table th, .pf-table td { padding: 6px 7px; }
  table { font-size: 12px; }

  /* Кнопки крупнее для пальца */
  button { padding: 8px 12px; }
  button.small { padding: 6px 9px; font-size: 12px; }

  /* Сверка / панели — компактные отступы */
  .recon-card, .monthly-card, .chart-wrap { padding: 10px 12px; }
  .recon-head { flex-direction: column; gap: 6px; }
  .recon-head > div:last-child { text-align: left !important; }

  /* Аналитика — пикер над графиком */
  .analytics-layout { grid-template-columns: 1fr; }
  .chart-wrap { height: 260px; }

  /* Модалки на весь экран снизу */
  .modal { width: 100%; max-width: 100%; border-radius: 12px 12px 0 0; }
  .modal-backdrop { align-items: flex-end; }
}

/* Планшет */
@media (max-width: 900px) and (min-width: 641px) {
  .summary-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ============ Тосты и прогресс синхронизации ============ */
#toastRoot { position: fixed; right: 16px; bottom: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; max-width: min(92vw, 380px); }
.toast { padding: 10px 14px; border-radius: 10px; font-size: 13px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); box-shadow: 0 6px 24px var(--shadow); transition: opacity .25s; }
.toast-success { border-left: 3px solid var(--pos); }
.toast-error { border-left: 3px solid var(--neg); }
.toast-info { border-left: 3px solid var(--accent); }
.toast-progress { border-left: 3px solid var(--accent); }
.pg-msg { font-size: 12.5px; margin-bottom: 6px; }
.pg-bar { height: 5px; background: var(--panel2); border-radius: 999px; overflow: hidden; }
.pg-fill { height: 100%; background: var(--accent); transition: width .2s; }
@media (max-width: 640px) { #toastRoot { left: 10px; right: 10px; bottom: 10px; max-width: none; } }

/* Подсветка изменившегося значения при авто-обновлении */
@keyframes flashHi { 0% { background: var(--warn); color: var(--bg); } 100% { background: transparent; } }
.flash { animation: flashHi 1.4s ease-out; border-radius: 4px; padding: 0 2px; }

/* Компактные карточки-ссылки в сводке (Подключения/Алерты) */
.card-click { cursor: pointer; transition: border-color .12s, background .12s; }
.card-click:hover { border-color: var(--accent); background: var(--panel2); }

/* Портфель/сверка: заголовки переносятся в 2 строки, колонки уже */
.pf-table th { white-space: normal; line-height: 1.25; vertical-align: bottom; }
.pf-table th, .pf-table td { padding: 6px 8px; }

/* Не давать всей странице скроллиться вбок — таблицы скроллятся внутри своих обёрток */
body { overflow-x: hidden; }
.wrap { overflow-x: hidden; }

/* Вкладки: на узких экранах переносятся в 2 ряда, без горизонтального скролла */
@media (max-width: 640px) {
  .tabs { flex-wrap: wrap; overflow-x: visible; gap: 2px 8px; }
  .tab-btn { margin-right: 8px; font-size: 13px; padding: 7px 2px; }
}

/* Сводка: карточки по высоте контента (без пустот), компактнее */
.summary-grid { align-items: start; }
.card { padding: 12px 14px; }
.summary-grid .card .value { font-size: 20px; }

/* pf-таблицы: ширина по контенту (колонки не расползаются с пустыми промежутками) */
.pf-table { width: auto; }

/* Строка счёта: колонка действий не разносит строку по высоте (одна строка, узкая) */
.fund-body td .row-actions { flex-wrap: wrap; max-width: 150px; justify-content: flex-start; gap: 4px; }
.fund-body td { vertical-align: top; }
.acc-name { vertical-align: top; line-height: 1.35; }

/* ============ Мобильный вид: строки счетов → карточки (без таблицы) ============ */
@media (max-width: 640px) {
  /* прячем заголовок таблицы — в карточках он не нужен */
  .fund-body thead { display: none; }
  .fund-body table, .fund-body tbody, .fund-body tr, .fund-body td { display: block; width: 100%; }

  .fund-body tbody tr {
    border: 1px solid var(--border); border-radius: 10px;
    padding: 10px 12px; margin-bottom: 8px; background: var(--panel);
  }
  .fund-body tbody tr.expand-row { border: none; padding: 0; margin: 0 0 8px; }

  .fund-body td { border: none; padding: 2px 0; }

  /* имя счёта — крупно, первой строкой */
  .fund-body td.acc-name { font-size: 14px; padding-bottom: 6px; }

  /* остальные ячейки: подпись из data-label слева, значение справа */
  .fund-body td[data-label] {
    display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
    font-size: 13px; padding: 3px 0;
  }
  .fund-body td[data-label]::before {
    content: attr(data-label);
    color: var(--text-faint); font-size: 11px; text-transform: uppercase; letter-spacing: .03em;
  }
  /* пустые ячейки на мобиле не показываем */
  .fund-body td[data-label]:empty { display: none; }

  /* действия — в ряд, во всю ширину карточки */
  .fund-body td .row-actions { max-width: none; width: 100%; justify-content: flex-start; margin-top: 8px; }
  .fund-body td .row-actions button { flex: 1 1 auto; min-width: 88px; }
}

/* ===== Ровные значения: % сверху, $ под ним, всё по правому краю ===== */
.ret-val { display: inline-flex; flex-direction: column; align-items: flex-end; line-height: 1.25; }
.ret-val .cell-usd { display: block; margin-top: 1px; }

/* ===== Шапка фонда: метрика = подпись сверху, значение снизу ===== */
.fund-stats { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 8px; white-space: normal; overflow: visible; }
.fund-stats .fs-item {
  display: inline-flex; flex-direction: column; margin: 0; white-space: nowrap; line-height: 1.3;
}
.fund-stats .fs-label { font-size: 10.5px; color: var(--text-faint); text-transform: uppercase; letter-spacing: .04em; }
.fund-stats b { font-size: 14px; font-weight: 600; }

/* ===== Опасная кнопка не должна быть большой ===== */
button.danger { flex: 0 0 auto !important; }
.fund-body td .row-actions button.danger,
.fund-header .row-actions button.danger { max-width: 46px; padding: 6px 8px; opacity: .75; }
.fund-body td .row-actions button.danger:hover,
.fund-header .row-actions button.danger:hover { opacity: 1; }

@media (max-width: 640px) {
  /* мобильная карточка: значение справа колонкой, без каскада */
  .fund-body td[data-label] .ret-val { align-items: flex-end; }
  .fund-stats { gap: 8px 16px; }
  .fund-stats b { font-size: 13.5px; }
  /* кнопки в карточке — равные, кроме удаления (маленькая, в углу) */
  .fund-body td .row-actions button { flex: 1 1 30%; min-width: 0; }
  .fund-body td .row-actions button.danger { flex: 0 0 46px !important; margin-left: auto; }
}
