:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --raised: #ffffff;         /* подсказки, поля ввода */
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --on-ink: #ffffff;         /* текст на тёмной (в светлой теме) заливке */
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --shadow: rgba(11, 11, 11, 0.12);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --pos: #006300;            /* рост — текст */
  --neg: #b42a2a;            /* падение — текст */
  --alert-bg: #fdf1e8;
  --alert-ink: #7a3a10;
  --excluded: #f1f0ec;
  --hover: #f1f0ec;
  --row-hover: #f5f4f0;
  --chip: #efeee9;
  --sub-row: #f6f5f1;
  --sub-border: #ecebe5;
  --overlay: rgba(252, 252, 251, 0.85);
  /* графики */
  --series: #2a78d6;
  --series-wash: rgba(42, 120, 214, 0.10);
  --down: #e34948;
  --down-wash: rgba(227, 73, 72, 0.10);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
/* Системная тема — когда пользователь ничего не выбрал */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --raised: #242423;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --on-ink: #0b0b0b;
    --grid: #2c2c2a;
    --axis: #45443f;
    --border: rgba(255, 255, 255, 0.10);
    --shadow: rgba(0, 0, 0, 0.50);
    --accent: #3987e5;
    --accent-ink: #6da7ec;
    --pos: #0ca30c;
    --neg: #e66767;
    --alert-bg: #3a2616;
    --alert-ink: #f2b58a;
    --excluded: #202020;
    --hover: #262625;
    --row-hover: #222221;
    --chip: #2a2a28;
    --sub-row: #1f1f1e;
    --sub-border: #2a2a28;
    --overlay: rgba(26, 26, 25, 0.85);
    --series: #3987e5;
    --series-wash: rgba(57, 135, 229, 0.16);
    --down: #e66767;
    --down-wash: rgba(230, 103, 103, 0.16);
  }
}
/* Явный выбор темы перебивает системную в обе стороны */
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --raised: #242423;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --on-ink: #0b0b0b;
  --grid: #2c2c2a;
  --axis: #45443f;
  --border: rgba(255, 255, 255, 0.10);
  --shadow: rgba(0, 0, 0, 0.50);
  --accent: #3987e5;
  --accent-ink: #6da7ec;
  --pos: #0ca30c;
  --neg: #e66767;
  --alert-bg: #3a2616;
  --alert-ink: #f2b58a;
  --excluded: #202020;
  --hover: #262625;
  --row-hover: #222221;
  --chip: #2a2a28;
  --sub-row: #1f1f1e;
  --sub-border: #2a2a28;
  --overlay: rgba(26, 26, 25, 0.85);
  --series: #3987e5;
  --series-wash: rgba(57, 135, 229, 0.16);
  --down: #e66767;
  --down-wash: rgba(230, 103, 103, 0.16);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 var(--font);
  -webkit-text-size-adjust: 100%;
}
h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 18px; }
h2 { font-size: 15px; }
.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 12px; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

/* ─── Кнопки ─── */
.btn {
  font: inherit;
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-ink); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ─── Переключатель темы: светлая / как в системе / тёмная ─── */
.theme-switch { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; }
.theme-switch button {
  font: inherit; font-size: 14px; line-height: 1; padding: 6px 9px;
  border: 0; border-radius: 0; background: none; color: var(--muted); cursor: pointer;
}
.theme-switch button:first-child { border-radius: 7px 0 0 7px; }
.theme-switch button:last-child { border-radius: 0 7px 7px 0; }
.theme-switch button + button { border-left: 1px solid var(--border); }
.theme-switch button:hover { color: var(--ink); }
.theme-switch button.on { background: var(--chip); color: var(--ink); }

/* ─── Шапка ─── */
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.topbar-actions { display: flex; gap: 6px; align-items: center; }
.topbar-actions form { margin: 0; }

.page { max-width: 1280px; margin: 0 auto; padding: 20px 24px 40px; }

.alert {
  background: var(--alert-bg); color: var(--alert-ink);
  border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; font-size: 13px;
}
.alert ul { margin: 0; padding-left: 18px; }

/* ─── Режим: с начала года / вся история ─── */
.modes {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--chip); border-radius: 10px;
}
.modes button {
  font: inherit; font-size: 14px; font-weight: 500;
  padding: 7px 16px; border: 0; border-radius: 8px;
  background: transparent; color: var(--ink-2); cursor: pointer;
}
.modes button:hover { color: var(--ink); }
.modes button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px var(--shadow); }

/* ─── Итоги ─── */
.kpis {
  display: grid; gap: 12px; margin-bottom: 16px;
  grid-template-columns: 1.35fr repeat(4, 1fr);
}
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; min-width: 0;
}
.kpi-label { font-size: 12px; color: var(--ink-2); }
.kpi-value { font-size: 22px; font-weight: 600; line-height: 1.25; margin-top: 4px; white-space: nowrap; }
.kpi-main .kpi-value { font-size: 30px; }
.kpi-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
/* Процент — всегда строкой под суммой P&L */
.kpi-headline { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; margin-top: 4px; }
.kpi-headline .kpi-value { margin-top: 0; }
.pct-main { font-size: 20px; font-weight: 600; }
.pct-main .unit { font-size: 13px; font-weight: 500; }
.kpi-alt { font-size: 12px; color: var(--ink-2); margin-top: 2px; }

/* Пояснение методологии: при наведении, фокусе с клавиатуры или нажатии на телефоне */
.hint { position: relative; cursor: help; border-bottom: 1px dotted currentColor; outline: none; }
.hint:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }
.hint-box {
  display: none; position: absolute; left: 0; top: calc(100% + 8px); z-index: 20;
  width: 340px; max-width: min(340px, 86vw);
  background: var(--raised); color: var(--ink); border: 1px solid var(--axis); border-radius: 8px;
  padding: 10px 12px; box-shadow: 0 6px 20px var(--shadow);
  font-size: 12.5px; font-weight: 400; line-height: 1.45; white-space: normal; cursor: auto;
}
.hint:hover .hint-box, .hint:focus .hint-box, .hint:focus-within .hint-box { display: block; }
/* В таблице: подсказка открывается влево от числа и не обрезается рамкой таблицы */
.hint-left .hint-box { left: auto; right: 0; text-align: left; }
.hint-box table.src { width: 100%; margin-top: 6px; font-size: 12px; border-collapse: collapse; }
.hint-box table.src td { padding: 2px 6px 2px 0; border: 0; white-space: nowrap; background: none; }

td.strong { font-weight: 600; }
/* Раскрывающиеся строки: места хранения */
button.expand {
  font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
button.expand:hover { color: var(--accent-ink); }
.caret { display: inline-block; width: 12px; color: var(--ink-2); font-size: 12px; }
tr.sub td { background: var(--sub-row); border-bottom-color: var(--sub-border); font-size: 12px; color: var(--ink-2); padding-top: 5px; padding-bottom: 5px; }
tr.sub.unassigned td { color: var(--alert-ink); font-style: italic; }
tr.excluded.expandable td { cursor: default; }
.tag { font-size: 11px; color: var(--muted); background: var(--chip); border-radius: 6px; padding: 1px 6px; margin-left: 4px; white-space: nowrap; }
td.note { color: var(--muted); font-size: 12px; }

/* ─── Переключатель периода ─── */
.ranges { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 12px; }
.ranges button {
  font: inherit; font-size: 13px;
  padding: 5px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink-2);
  cursor: pointer;
}
.ranges button:hover { background: var(--hover); }
.ranges button.active { background: var(--ink); border-color: var(--ink); color: var(--on-ink); }

/* ─── Карточки ─── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px 18px;
  margin-bottom: 16px;
}
.card > h2 { margin-bottom: 12px; }
.card-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 12px; flex-wrap: wrap; margin-bottom: 8px;
}
.figure { text-align: right; }
.figure-value { display: block; font-size: 28px; font-weight: 600; line-height: 1.15; }
.figure-sub { display: block; font-size: 13px; color: var(--ink-2); }

.chart-wrap { position: relative; height: 280px; }
/* Графики: на компьютере — в один ряд, на узком экране — друг под другом */
.charts { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.charts .card { margin-bottom: 0; min-width: 0; }
@media (max-width: 1000px) { .charts { grid-template-columns: 1fr; } }
.chart-empty {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 0 24px; color: var(--muted); font-size: 13px;
  background: var(--overlay);
}
.chart-empty[hidden] { display: none; }

/* ─── Таблица ─── */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 8px 10px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--grid); vertical-align: middle; }
/* Заголовки переносятся — так таблица помещается без прокрутки вбок */
th { white-space: normal; vertical-align: bottom; line-height: 1.25; }
td .sec { font-size: 11px; font-weight: 400; color: var(--muted); margin-top: 1px; }
td.pos .sec, td.neg .sec { color: inherit; opacity: .8; }
td.name { white-space: normal; min-width: 150px; }
.ticker { color: var(--muted); font-size: 12px; margin-left: 2px; }
th { font-weight: 600; color: var(--ink-2); font-size: 12px; border-bottom-color: var(--axis); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: var(--row-hover); }
tr.excluded td { color: var(--muted); background: var(--excluded); }
tfoot td { font-weight: 600; border-top: 1px solid var(--axis); border-bottom: none; }
.stale { color: var(--alert-ink); }

/* ─── Вход ─── */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 28px;
}
.login-card .alert { margin-bottom: 14px; }
.login-card label:first-of-type { margin-top: 0; }
.login-card label { display: block; font-size: 13px; color: var(--ink-2); margin: 12px 0 4px; }
.login-card input {
  width: 100%; font: inherit; font-size: 15px;
  padding: 9px 11px; border: 1px solid var(--axis); border-radius: 8px; background: var(--raised); color: var(--ink);
}
.login-card button { width: 100%; margin-top: 20px; padding: 10px; font-size: 15px; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .kpi-main { grid-column: 1 / -1; }
}
@media (max-width: 640px) {
  .topbar-title { flex: 1; min-width: 0; }
  .topbar-actions { order: 2; }
  .modes { order: 3; width: 100%; }
  .modes button { flex: 1; }
  .kpi-value { font-size: 20px; }
  .kpi-main .kpi-value { font-size: 26px; }
  .topbar { padding: 12px 16px; flex-wrap: wrap; }
  .page { padding: 14px 16px 32px; }
  .card { padding: 14px 14px 16px; }
  .figure { text-align: left; }
  .figure-value { font-size: 24px; }
  .chart-wrap { height: 220px; }
}

/* На широком экране таблицы помещаются целиком — рамку не обрезаем, чтобы подсказки
   (источники цены) не срезались. Должно стоять после основного правила .table-wrap */
@media (min-width: 1001px) { .table-wrap { overflow: visible; } }
