@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

:root {
  --bg: #0a0e16;
  --card: #131925;
  --card-hi: #171f2e;
  --line: rgba(255,255,255,.08);
  --line-strong: rgba(255,255,255,.14);
  --text: #e8eef7;
  --muted: #8a96a8;
  --faint: #5d6878;
  --green: #34d399;
  --red: #fb7185;
  --amber: #fbbf24;
  --accent: #818cf8;
  --accent-2: #22d3ee;
  --shadow: 0 1px 2px rgba(0,0,0,.35), 0 14px 32px -18px rgba(0,0,0,.75);
  --radius: 16px;
}
* { box-sizing: border-box; }
body {
  margin: 0; min-height: 100vh;
  font-family: "Inter", -apple-system, "Segoe UI", Roboto, sans-serif;
  background:
    radial-gradient(1100px 620px at 88% -12%, rgba(129,140,248,.12), transparent 60%),
    radial-gradient(900px 520px at -12% 8%, rgba(34,211,238,.07), transparent 55%),
    var(--bg);
  color: var(--text); padding: 22px;
  -webkit-font-smoothing: antialiased;
}
.value, .amt, td, th, .api-when, .daysub, .cf-table, .srcrow, .cf-chip { font-variant-numeric: tabular-nums; }

.topbar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px; margin-bottom: 24px; }
.topbar h1 { font-size: 19px; font-weight: 700; margin: 0; letter-spacing: -.01em; display: inline-flex; align-items: center; gap: 11px; }
.topbar h1::before {
  content: "⌃"; display: inline-grid; place-items: center; width: 30px; height: 30px;
  border-radius: 9px; font-size: 18px; font-weight: 800; color: #0a0e16;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 6px 16px -5px rgba(129,140,248,.6);
}
.cur { color: var(--muted); font-weight: 500; font-size: 14px; }
.controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.controls label.daterange { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.navlinks { display: flex; gap: 10px; flex-wrap: wrap; }
.iconbtn { flex: 0 0 auto; width: 40px; height: 40px; padding: 0; display: inline-grid; place-items: center; border-radius: 11px; background: rgba(255,255,255,.05); border: 1px solid var(--line); color: var(--text); cursor: pointer; transition: border-color .15s, background .15s; }
.iconbtn:hover { border-color: var(--line-strong); background: rgba(255,255,255,.08); }
.iconbtn:disabled { opacity: .55; cursor: default; }
.iconbtn svg { display: block; }
.iconbtn.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.iconbtn ~ .controls { flex-basis: 100%; }  /* dashboard: filters/nav drop below the title+refresh row */
input[type="date"] { color-scheme: dark; }
.updated { color: var(--faint); font-size: 12px; }
select, input {
  background: rgba(255,255,255,.04); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 11px; font-size: 14px; font-family: inherit;
  transition: border-color .15s, background .15s;
}
select:focus, input:focus { outline: none; border-color: var(--accent); background: rgba(129,140,248,.08); }
.btn {
  background: rgba(255,255,255,.05); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 15px; cursor: pointer; font-size: 14px; font-family: inherit; font-weight: 500;
  transition: border-color .15s, background .15s, transform .05s;
}
.btn:hover { border-color: var(--line-strong); background: rgba(255,255,255,.08); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: linear-gradient(135deg, var(--accent), #6f7bf5); border-color: transparent; color: #0a0e16; font-weight: 600; }
.btn:disabled { opacity: .5; cursor: default; }

.card {
  background: linear-gradient(180deg, var(--card-hi), var(--card));
  border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 16px; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .08em; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 12px; }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: .05em; }
.kpi .value { font-size: 28px; font-weight: 700; margin-top: 8px; letter-spacing: -.02em; line-height: 1.05; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 5px; }
.kpi.income .value { color: var(--green); }
.kpi.expense .value { color: var(--red); }
.kpi.token .value { color: var(--amber); }
.kpi.margin .value { color: var(--accent); }
.kpi.net.negative .value { color: var(--red); }
.kpi.net .value { color: var(--green); }
.kpi.income, .kpi.expense, .kpi.net, .kpi.token { position: relative; overflow: hidden; }
.kpi.income::before, .kpi.expense::before, .kpi.net::before, .kpi.token::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 2px; }
.kpi.income::before { background: linear-gradient(90deg, var(--green), transparent); }
.kpi.expense::before { background: linear-gradient(90deg, var(--red), transparent); }
.kpi.net::before { background: linear-gradient(90deg, var(--accent), transparent); }
.kpi.token::before { background: linear-gradient(90deg, var(--amber), transparent); }
.balances-top { margin-bottom: 20px; }
.balances-top h3 { margin: 0 0 14px; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .08em; }
.balances-top .kpis { margin-bottom: 0; }
.kpi.balance { background: linear-gradient(180deg, rgba(255,255,255,.035), rgba(255,255,255,.012)); }
.kpi.balance .label { text-transform: uppercase; }
.hint { font-weight: 400; color: var(--faint); font-size: 12px; letter-spacing: 0; text-transform: none; }

.charts { display: grid; grid-template-columns: 2fr 1fr; gap: 12px; }
.chart-box canvas { max-height: 280px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; color: var(--muted); font-weight: 600; padding: 9px 8px; border-bottom: 1px solid var(--line); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
td { padding: 10px 8px; border-bottom: 1px solid var(--line); }
.pos { color: var(--green); }
.neg { color: var(--red); }
.small { color: var(--muted); font-size: 12px; }
.mini td { padding: 7px 8px; }

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 8px; }
.dot.ok { background: var(--green); }
.dot.err { background: var(--red); }
.dot.idle { background: var(--amber); }
.dot.off { background: var(--muted); }

/* API connection status panel */
.api-summary { font-size: 13px; margin-bottom: 10px; }
.api-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 9px 0; border-bottom: 1px solid rgba(51,65,85,.4); }
.api-row:last-child { border-bottom: none; }
.api-badge { font-size: 12px; font-weight: 700; padding: 3px 11px; border-radius: 20px; white-space: nowrap; }
.api-badge.ok { color: #052e16; background: var(--green); }
.api-badge.err { color: #fff; background: var(--red); }
.api-badge.warn { color: #422006; background: #f59e0b; }
.api-badge.manual { color: var(--text); background: #334155; }
.api-badge.off { color: var(--muted); background: #1f2937; }
.api-name { font-weight: 600; }
.api-when { color: var(--muted); font-size: 13px; margin-left: auto; }
.api-err { flex-basis: 100%; color: var(--red); font-size: 12px; }

/* flexible reports (/reports) */
.rnum { text-align: right; white-space: nowrap; }
.report-1d th.rnum, .report-pivot th.rnum { text-align: right; }
.rshare { white-space: nowrap; color: var(--muted); font-size: 13px; }
.rbar-wrap { display: inline-block; width: 64px; height: 6px; background: rgba(255,255,255,.06); border-radius: 3px; overflow: hidden; vertical-align: middle; margin-right: 7px; }
.rbar { display: block; height: 100%; border-radius: 3px; }
.report-scroll { overflow-x: auto; }
.report-pivot th, .report-pivot td { white-space: nowrap; }
.report-pivot .dim { color: var(--faint); }
.report-pivot tfoot td { border-top: 1px solid var(--line-strong); border-bottom: none; }

/* balances + compact connection status, side by side */
.topgrid { display: grid; grid-template-columns: 1fr minmax(180px, 240px); gap: 12px; margin-bottom: 12px; align-items: start; }
.topgrid .balances-top { margin-bottom: 0; }
.api-card { margin-bottom: 0; }
.api-head { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 15px; margin-bottom: 14px; }
.api-head .hdot { width: 11px; height: 11px; border-radius: 50%; flex: 0 0 auto; }
.api-head.ok { color: var(--green); }
.api-head.ok .hdot { background: var(--green); box-shadow: 0 0 11px -1px var(--green); }
.api-head.bad { color: var(--amber); }
.api-head.bad .hdot { background: var(--amber); box-shadow: 0 0 11px -1px var(--amber); }
.api-head.err { color: var(--red); }
.api-head.err .hdot { background: var(--red); box-shadow: 0 0 11px -1px var(--red); }
.api-list { display: flex; flex-direction: column; gap: 10px; }
.api-item { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.api-item .dot { margin-right: 0; }
.api-item .when { margin-left: auto; color: var(--faint); font-size: 11px; white-space: nowrap; }
/* compact connection lamps */
.api-lamps { display: flex; flex-wrap: wrap; gap: 9px 16px; }
.lamp { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.lamp .dot { margin-right: 0; width: 9px; height: 9px; }
.lamp .dot.ok { box-shadow: 0 0 7px -1px var(--green); }
.lamp .dot.err { box-shadow: 0 0 7px -1px var(--red); }
.lamp .dot.idle { box-shadow: 0 0 7px -1px var(--amber); }
.contents { display: contents; }  /* balance cards flow into the parent .kpis grid alongside the lamps card */
.api-card .label { margin-bottom: 12px; }

/* financial statements (/statements) */
.stmt { width: 100%; border-collapse: collapse; font-size: 14px; }
.stmt td { padding: 7px 4px; }
.stmt .lbl { color: var(--text); }
.stmt .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.stmt .pctcol { text-align: right; color: var(--muted); font-size: 12px; width: 54px; white-space: nowrap; }
.stmt tr.detail .lbl { padding-left: 16px; color: var(--muted); font-size: 13px; }
.stmt tr.detail .num { color: var(--muted); }
.stmt tr.sub td { border-top: 1px solid var(--line); font-weight: 700; padding-top: 9px; }
.stmt tr.total td { border-top: 2px solid var(--line-strong); font-weight: 800; font-size: 16px; padding-top: 11px; }
.stmt tr.pos-line .num { color: var(--green); }
.stmt tr.neg-line .num { color: var(--red); }
.stmt tr.sechead td { color: var(--muted); text-transform: uppercase; font-size: 11px; letter-spacing: .06em; padding-top: 14px; padding-bottom: 2px; font-weight: 600; }
.stmt-note { color: var(--faint); font-size: 12px; margin-top: 12px; line-height: 1.55; }

.entry .row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.entry label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.entry label input, .entry label select { margin-top: 2px; }
.entry > label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.msg { margin-left: 12px; font-size: 13px; }
.msg.ok { color: var(--green); }
.msg.err { color: var(--red); }
.del { background: transparent; border: none; color: var(--muted); cursor: pointer; font-size: 18px; }
.del:hover { color: var(--red); }
.preview { margin-top: 14px; max-height: 260px; overflow-y: auto; }
/* operations registry (/ledger) */
.day { margin-bottom: 18px; }
.dayhead { display: flex; align-items: baseline; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.dayhead strong { font-size: 15px; }
.daysub { margin-left: auto; font-size: 13px; white-space: nowrap; }
a.btn { text-decoration: none; display: inline-block; }
.ledger-summary { display: flex; gap: 24px; flex-wrap: wrap; margin-bottom: 18px; }
.srccol { flex: 1; min-width: 200px; }
.srctitle { font-size: 13px; color: var(--muted); margin-bottom: 8px; border-bottom: 1px solid var(--line); padding-bottom: 6px; display: flex; justify-content: space-between; }
.srcrow { display: flex; justify-content: space-between; padding: 3px 0; font-size: 14px; }
.catbadge { display: inline-block; font-size: 11px; color: var(--muted); background: rgba(148,163,184,.14); border: 1px solid var(--line); border-radius: 6px; padding: 1px 7px; margin-left: 6px; vertical-align: middle; white-space: nowrap; }
/* daily cashflow (/cashflow) — tabular */
.cf-period { display: flex; gap: 24px; flex-wrap: wrap; padding: 16px 18px; background: rgba(255,255,255,.03); border: 1px solid var(--line); border-radius: 12px; margin-bottom: 20px; }
.cf-pcol { display: flex; flex-direction: column; gap: 4px; }
.cf-pcol .lbl { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
.cf-pcol .amt { font-size: 22px; font-weight: 700; }
.cf-month { margin-bottom: 22px; }
.cf-month-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding: 8px 0; border-bottom: 2px solid var(--line); margin-bottom: 4px; flex-wrap: wrap; }
.cf-month-head strong { font-size: 16px; text-transform: capitalize; }
.cf-month-tot { font-size: 13px; color: var(--muted); }
.cf-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.cf-table th { text-align: right; color: var(--muted); font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: .3px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.cf-table th:first-child { text-align: left; }
.cf-table td { padding: 7px 8px; text-align: right; white-space: nowrap; }
.cf-r td { border-top: 1px solid rgba(51,65,85,.4); }
.cf-r td.cf-d { text-align: left; text-transform: capitalize; color: var(--text); }
.cf-dash { color: var(--muted); }
.cf-sub td { padding: 0 8px 8px; text-align: left; border: none; white-space: normal; }
.cf-grp { margin-right: 16px; }
.cf-mk { color: var(--muted); font-weight: 700; margin-right: 6px; }
.cf-chip { color: var(--muted); font-size: 12px; margin-right: 10px; white-space: nowrap; }
.cf-chip b { color: var(--text); font-weight: 600; }
/* edit modal + row actions (registry) */
.acts { white-space: nowrap; text-align: right; }
.edit { background: transparent; border: none; color: var(--muted); cursor: pointer; font-size: 15px; margin-right: 6px; }
.edit:hover { color: var(--accent); }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
.modal-box { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: 100%; max-width: 420px; }
.modal-box h3 { margin: 0 0 14px; font-size: 16px; }
.modal-box > label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); margin-bottom: 10px; }
.modal-box .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 10px; }
.modal-box .row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.preview .warn { color: #f59e0b; font-size: 13px; margin-bottom: 6px; }
input[type="file"] { padding: 6px; font-size: 13px; }

@media (max-width: 900px) {
  .charts, .grid-2 { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: repeat(3, 1fr); gap: 12px; }
}

/* Telegram Mini App / phones (~375–420px) */
@media (max-width: 480px) {
  body { padding: 10px; }
  /* wide tables/charts self-scroll, so clip residual page-level horizontal scroll */
  html, body { overflow-x: hidden; }
  .topbar h1 { font-size: 18px; }
  .controls { width: 100%; gap: 8px; }
  .controls label.daterange { flex: 1; }
  .controls label.daterange input { width: 100%; }
  .navlinks { width: 100%; gap: 8px; }
  .navlinks .btn { flex: 1 1 calc(50% - 4px); text-align: center; padding: 9px 4px; font-size: 13px; }
  .topgrid { grid-template-columns: 1fr; }
  /* phones: balances 2-up, lamps card spans full width below them */
  .balances-top .kpis { grid-template-columns: 1fr 1fr; }
  .balances-top .api-card { grid-column: 1 / -1; }
  .card { padding: 14px; }
  /* KPI + balance cards: 3 per row, compact */
  .kpis { gap: 8px; }
  .kpi { padding: 12px 10px; }
  .kpi .label { font-size: 11px; line-height: 1.25; }
  .kpi .value { font-size: 18px; margin-top: 4px; }
  .kpi .sub { font-size: 10px; }
  .kpi .hint { display: none; }
  .sub-when { display: none; }
  .entry .row { grid-template-columns: 1fr; }
  .chart-box canvas { max-height: 220px; max-width: 100%; }
  /* operations feed has 6 cols — drop Источник + Статус so Время/Что/Сумма/USD fit without scroll */
  #txnTable th:nth-child(2), #txnTable td:nth-child(2),
  #txnTable th:nth-child(6), #txnTable td:nth-child(6) { display: none; }
  /* wide data tables (by-model, P&L…) scroll inside their card, not the page */
  .mini { display: block; overflow-x: auto; max-width: 100%; }
  /* health table: hide sync-time + status columns on phones (diagnostics — see desktop) */
  #sourcesTable th:nth-child(4), #sourcesTable td:nth-child(4),
  #sourcesTable th:nth-child(5), #sourcesTable td:nth-child(5) { display: none; }
  /* cashflow: compact table + 2-up period summary */
  .cf-period { gap: 10px 16px; padding: 12px 14px; }
  .cf-pcol { flex: 1 0 40%; }
  .cf-pcol .amt { font-size: 18px; }
  /* fixed layout pins the table to the viewport so the breakdown cell can't push it
     wider than the screen (was 532px on a 375px phone → clipped, unswipeable) */
  .cf-table { font-size: 13px; table-layout: fixed; width: 100%; }
  .cf-table th, .cf-table td { padding: 6px 5px; overflow: hidden; text-overflow: ellipsis; }
  /* breakdown row: let ALL expense/income chips wrap onto multiple lines (every
     category visible, no horizontal scroll needed) */
  .cf-sub td { white-space: normal; overflow: visible; }
  .cf-sub .cf-grp { display: block; margin: 0 0 3px; }
  .cf-chip { display: inline-block; font-size: 11px; margin: 0 10px 3px 0; }
  .cf-month-tot { font-size: 12px; width: 100%; }
  /* registry: hide original-currency amount (col 4); keep Время/Источник/Что/USD/действия */
  table.txns td:nth-child(4) { display: none; }
  /* report: hide the «Операций» count column on phones (sum + share are the focus) */
  .report-1d th:nth-child(4), .report-1d td:nth-child(4) { display: none; }
  .rbar-wrap { width: 44px; }
  /* financial statements: tighter on phones */
  .stmt { font-size: 13px; }
  .stmt td { padding: 6px 2px; }
  .stmt .pctcol { width: 44px; font-size: 11px; }
  .stmt tr.total td { font-size: 15px; }
  .stmt-note { font-size: 11px; }
}
