:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ef;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --p1: #2a78d6;      /* Артем */
  --p2: #eb6834;      /* Света */
  --inc: #1baf7a;     /* доходы */
  --exp: #4a3aa7;     /* расходы */
  --heat: 42, 120, 214;
  --good: #006300;
  --bad: #d03b3b;
  --warn-bg: #fdf3dc;
  --pos: #2a78d6;
  --neg: #e34948;
  --other: #b5b3ab;
  --c1: #2a78d6; --c2: #eb6834; --c3: #1baf7a; --c4: #eda100;
  --c5: #e87ba4; --c6: #008300; --c7: #4a3aa7; --c8: #e34948;
  --y-old: #86b6ef; --y-mid: #3987e5; --y-new: #184f95;
  --radius: 12px;
  --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;
    --surface-2: #232321;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --p1: #3987e5;
    --p2: #d95926;
    --inc: #199e70;
    --exp: #9085e9;
    --heat: 57, 135, 229;
    --good: #0ca30c;
    --bad: #e66767;
    --warn-bg: #3a2f12;
    --pos: #3987e5;
    --neg: #e66767;
    --other: #5f5e5a;
    --c1: #3987e5; --c2: #d95926; --c3: #199e70; --c4: #c98500;
    --c5: #d55181; --c6: #008300; --c7: #9085e9; --c8: #e66767;
    --y-old: #184f95; --y-mid: #2a78d6; --y-new: #6da7ec;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --p1: #3987e5;
  --p2: #d95926;
  --inc: #199e70;
  --exp: #9085e9;
  --heat: 57, 135, 229;
  --good: #0ca30c;
  --bad: #e66767;
  --warn-bg: #3a2f12;
  --pos: #3987e5;
  --neg: #e66767;
  --other: #5f5e5a;
  --c1: #3987e5; --c2: #d95926; --c3: #199e70; --c4: #c98500;
  --c5: #d55181; --c6: #008300; --c7: #9085e9; --c8: #e66767;
  --y-old: #184f95; --y-mid: #2a78d6; --y-new: #6da7ec;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; font-weight: 600; }
h2 { font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.error { color: var(--bad); }

button, input, select { font: inherit; color: inherit; }
select, input[type="search"], input:not([type]), input[type="password"] {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-height: 36px;
}
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  min-height: 36px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:disabled { opacity: .45; cursor: default; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  display: grid;
  gap: 14px;
}
.login-card h1 { font-size: 20px; }
.login-card label { display: grid; gap: 6px; color: var(--text-2); font-size: 13px; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 24px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--page);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; white-space: nowrap; }
.tabs { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  border: 0; background: transparent; padding: 7px 12px; border-radius: 8px;
  color: var(--text-2); cursor: pointer; white-space: nowrap; position: relative;
}
.tabs button:hover { background: var(--surface-2); }
.tabs button.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.badge {
  display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 6px;
  border-radius: 9px; background: var(--bad); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}

/* Filters */
.filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding: 14px 24px 0;
  max-width: 1280px; margin: 0 auto;
}
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; }
.seg button {
  border: 0; background: transparent; padding: 5px 12px; border-radius: 7px; cursor: pointer;
  color: var(--text-2); display: inline-flex; align-items: center; gap: 6px;
}
.seg button.active { background: var(--surface); color: var(--text); box-shadow: 0 0 0 1px var(--border); font-weight: 600; }
.small-seg button { padding: 3px 10px; font-size: 12.5px; }
.kpi .value.small-value { font-size: 20px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.period { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
#custom-range { display: inline-flex; align-items: center; gap: 6px; }
.period-label { font-size: 13px; }

main { max-width: 1280px; margin: 0 auto; padding: 14px 24px 48px; }
.tab { display: grid; gap: 16px; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
.col { display: grid; gap: 16px; min-width: 0; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}
.card.narrow { max-width: 760px; }
.card h2 + p { margin-top: 6px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.chart { width: 100%; min-height: 120px; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi .label { color: var(--text-2); font-size: 13px; }
.kpi .value { font-size: 28px; font-weight: 600; margin-top: 2px; letter-spacing: -0.01em; }
.kpi.hero .value { font-size: 34px; }
.kpi .delta { font-size: 12.5px; margin-top: 2px; color: var(--muted); }
.kpi .delta.good { color: var(--good); }
.kpi .delta.bad { color: var(--bad); }
.kpi .sub { font-size: 12.5px; color: var(--muted); }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: 12.5px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap { white-space: normal; min-width: 160px; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
tr.selected td { background: var(--surface-2); font-weight: 600; }
tr.total td { font-weight: 600; border-bottom: 0; }
tr.excluded td { color: var(--muted); }
tr.attention td { background: var(--warn-bg); }
.heat td.cell { text-align: right; font-variant-numeric: tabular-nums; min-width: 72px; }
.sub-cat { color: var(--muted); font-size: 12px; }
.neg { color: var(--good); }

/* Top expenses list */
.top-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 32px; }
@media (max-width: 900px) { .top-list { grid-template-columns: minmax(0, 1fr); } }
.top-item { display: grid; grid-template-columns: auto 1fr auto; gap: 4px 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--grid); }
.top-item .what { min-width: 0; }
.top-item .what div:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-item .amt { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.top-item .amt .muted { font-weight: 400; display: block; font-size: 12px; }

/* Operations */
.ops-filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.ops-filters input { flex: 1 1 220px; }
#ops-summary { margin-bottom: 6px; }
#ops-more { margin-top: 10px; }

/* Upload */
.dropzone {
  display: grid; place-items: center; text-align: center;
  border: 1.5px dashed var(--axis); border-radius: var(--radius);
  padding: 28px 16px; margin: 14px 0; cursor: pointer; color: var(--text-2);
}
.dropzone.drag { border-color: var(--accent); background: var(--surface-2); }
.dropzone input { display: none; }
#upload-result { margin-top: 14px; }
.result-box { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; display: grid; gap: 6px; }
.result-box.ok { border-color: var(--good); }
.result-box.fail { border-color: var(--bad); }

/* Mapping */
.map-select { min-width: 190px; }
.map-person { font-weight: 600; padding-top: 18px; }

/* Drill-down dialog */
.drill {
  width: min(920px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
}
.drill::backdrop { background: rgba(0, 0, 0, 0.35); }
.drill-head {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 12px;
  padding: 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface);
}
#drill-body { padding: 8px 16px 16px; display: grid; gap: 14px; }
#drill-body h3 { font-size: 13px; color: var(--text-2); margin: 8px 0 0; font-weight: 600; }

/* Advisor */
.advisor { display: grid; grid-template-columns: minmax(0, 3fr) minmax(320px, 2fr); gap: 16px; align-items: start; }
.head-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.head-actions select { max-width: 220px; min-height: 30px; padding: 3px 8px; font-size: 13px; }
.ai-status { min-height: 18px; margin-bottom: 6px; }
.ai-status .pulse::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent); margin-right: 8px; animation: pulse 1.2s ease-in-out infinite; vertical-align: 1px; }
@keyframes pulse { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }
.md { font-size: 14.5px; line-height: 1.55; overflow-wrap: anywhere; }
.md h1, .md h2 { font-size: 17px; margin: 22px 0 8px; }
.md h3 { font-size: 15px; margin: 16px 0 6px; }
.md > :first-child { margin-top: 0; }
.md p { margin: 8px 0; }
.md ul, .md ol { padding-left: 22px; margin: 8px 0; }
.md li { margin: 3px 0; }
.md table { display: block; overflow-x: auto; font-size: 13px; margin: 10px 0; }
.md th, .md td { white-space: normal; min-width: 90px; vertical-align: top; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 12.5px; }
.md blockquote { margin: 8px 0; padding: 4px 12px; border-left: 3px solid var(--grid); color: var(--text-2); }
.chat { position: sticky; top: 72px; display: flex; flex-direction: column; height: calc(100vh - 96px); min-height: 420px; }
.chat-log { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; padding: 4px 2px 8px; }
.msg { max-width: 92%; padding: 9px 12px; border-radius: 12px; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-ink); white-space: pre-wrap; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg .md { font-size: 14px; }
.msg .md h1, .msg .md h2, .msg .md h3 { font-size: 14.5px; margin: 10px 0 4px; }
.msg .tool-note { color: var(--muted); font-size: 12px; margin: 2px 0; }
.msg.error { background: transparent; border: 1px solid var(--bad); color: var(--bad); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.chips button { border: 1px solid var(--border); background: var(--surface); border-radius: 16px; padding: 5px 11px; font-size: 13px; cursor: pointer; color: var(--text-2); text-align: left; }
.chips button:hover { background: var(--surface-2); color: var(--text); }
.chat-form { display: flex; gap: 8px; align-items: flex-end; padding-top: 8px; border-top: 1px solid var(--grid); }
.chat-form textarea { flex: 1; resize: none; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; max-height: 160px; }
.disclaimer { margin: 0; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--page); padding: 9px 16px; border-radius: 9px; font-size: 14px; z-index: 50;
}

.empty { padding: 32px 8px; text-align: center; color: var(--muted); }

@media (max-width: 900px) {
  .grid.two { grid-template-columns: minmax(0, 1fr); }
  .advisor { grid-template-columns: minmax(0, 1fr); }
  .chat { position: static; height: 75vh; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .topbar { padding: 8px 16px; gap: 8px; flex-wrap: wrap; }
  .brand { flex: 1; }
  .tabs { order: 3; flex-basis: 100%; }
  .filters { padding: 12px 16px 0; }
  main { padding: 12px 16px 40px; }
  .kpis { gap: 10px; }
  .kpi { padding: 12px; }
  .kpi .value { font-size: 22px; }
  .kpi.hero .value { font-size: 26px; }
  .card { padding: 12px; }
  .drill { width: 100vw; max-width: 100vw; max-height: 92vh; margin: auto 0 0; border-radius: var(--radius) var(--radius) 0 0; }
}
