:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --text: var(--tg-theme-text-color, #15171a);
  --muted: var(--tg-theme-hint-color, #8a8f98);
  --accent: var(--tg-theme-button-color, #3b82f6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --card: var(--tg-theme-section-bg-color, var(--bg));
  --line: rgba(127,127,127,.18);
  --red: #e5484d; --amber: #f5a524; --green: #30a46c; --blue: #3b82f6;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #111317; --bg2: #0b0c0f; --text: #eceef2; --muted: #8b919c; --card: #181b20;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg2); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.red { color: var(--red); } .amber { color: var(--amber); } .green { color: var(--green); }
code { background: var(--bg2); padding: 1px 5px; border-radius: 5px; }
a { color: var(--accent); }

/* ---------- шапка и вкладки */
.top { position: sticky; top: 0; z-index: 5; background: var(--bg); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 16px; padding: 10px 16px; }
.brand { font-weight: 700; font-size: 17px; white-space: nowrap; }
#tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; flex: 1; }
#tabs::-webkit-scrollbar { display: none; }
#tabs button { border: 0; background: transparent; color: var(--muted); padding: 7px 12px; border-radius: 9px;
  font: inherit; cursor: pointer; white-space: nowrap; }
#tabs button.active { background: var(--bg2); color: var(--text); font-weight: 600; }
main { max-width: 1100px; margin: 0 auto; padding: 14px 14px 80px; }
@media (max-width: 640px) {
  .brand, #logout { display: none; }
  .top { padding: 8px; }
}

/* ---------- карточки */
.grid { display: grid; gap: 12px; }
.kpis { grid-template-columns: repeat(4, 1fr); margin-bottom: 14px; }
@media (max-width: 760px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
.kpi { background: var(--card); border-radius: var(--radius); padding: 12px 14px; }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.cols { display: grid; grid-template-columns: 1.4fr 1fr; gap: 14px; align-items: start; }
@media (max-width: 860px) { .cols { grid-template-columns: 1fr; } }
.card { background: var(--card); border-radius: var(--radius); padding: 4px 0; margin-bottom: 14px; overflow: hidden; }
.card > h3 { margin: 0; padding: 12px 14px 6px; font-size: 13px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em; display: flex; justify-content: space-between; align-items: center; }
.card > h3 .link { text-transform: none; letter-spacing: 0; font-size: 13px; }
.empty { padding: 14px; color: var(--muted); }

/* ---------- строки списков */
.row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-top: 1px solid var(--line); cursor: pointer; }
.row:first-of-type { border-top: 0; }
.row:hover { background: color-mix(in srgb, var(--bg2) 50%, transparent); }
.row .main { flex: 1; min-width: 0; }
.row .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .meta { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .right { text-align: right; }
.row .amount { font-weight: 600; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.pill { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--bg2); color: var(--muted); white-space: nowrap; }
.pill.overdue { background: color-mix(in srgb, var(--red) 15%, transparent); color: var(--red); }
.pill.soon { background: color-mix(in srgb, var(--amber) 18%, transparent); color: #b7791f; }
.pill.auto { background: color-mix(in srgb, var(--blue) 14%, transparent); color: var(--blue); }
.pill.ok { background: color-mix(in srgb, var(--green) 15%, transparent); color: var(--green); }
.group-title { padding: 14px 14px 4px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.group-title .muted { font-weight: 400; font-size: 13px; margin-left: auto; }
.inactive { opacity: .5; }

/* ---------- кнопки */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 0; border-radius: 10px;
  padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer; background: var(--bg2); color: var(--text); text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { background: color-mix(in srgb, var(--red) 14%, transparent); color: var(--red); }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn.big { padding: 12px 20px; font-size: 16px; width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; font: inherit; padding: 0; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }
.toolbar .spacer { flex: 1; }
.seg { display: inline-flex; background: var(--card); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: none; padding: 5px 10px; border-radius: 8px; font: inherit; font-size: 13px; color: var(--muted); cursor: pointer; }
.seg button.on { background: var(--bg2); color: var(--text); font-weight: 600; }

/* ---------- форма в шторке */
.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
.sheet-body { position: relative; background: var(--bg); width: 100%; max-width: 520px; max-height: 92vh; overflow: auto;
  border-radius: 18px 18px 0 0; padding: 6px 16px 24px; }
@media (min-width: 700px) { .sheet { align-items: center; } .sheet-body { border-radius: 18px; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--bg); padding: 10px 0; }
.sheet-head h2 { margin: 0; font-size: 18px; }
.x { border: 0; background: var(--bg2); color: var(--muted); width: 30px; height: 30px; border-radius: 50%; cursor: pointer; font-size: 14px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select, .field textarea { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg2); color: var(--text); font: inherit; }
.field textarea { min-height: 64px; resize: vertical; }
.field .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.three { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 10px; }
.check { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 12px; cursor: pointer; }
.check input { margin-top: 3px; width: 18px; height: 18px; }
.actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.actions .btn { flex: 1; }
.info-list { margin: 0 0 12px; padding: 0; list-style: none; }
.info-list li { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--line); gap: 12px; }
.info-list li span:first-child { color: var(--muted); }

/* ---------- графики */
.bars { padding: 6px 14px 14px; }
.bar-row { display: grid; grid-template-columns: 140px 1fr 90px; gap: 10px; align-items: center; padding: 5px 0; font-size: 14px; }
.bar-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { background: var(--bg2); border-radius: 6px; height: 12px; overflow: hidden; display: flex; }
.bar-fill { height: 100%; }
.months { display: flex; gap: 6px; align-items: flex-end; height: 170px; padding: 10px 14px 0; }
.month { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; min-width: 0; }
.month .stack { width: 100%; max-width: 38px; display: flex; flex-direction: column-reverse; border-radius: 6px 6px 0 0; overflow: hidden; }
.month .lbl { font-size: 11px; color: var(--muted); }
.legend { display: flex; flex-wrap: wrap; gap: 12px; padding: 8px 14px 14px; font-size: 13px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- вход и тост */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { background: var(--card); border-radius: 20px; padding: 32px 26px; max-width: 360px; width: 100%; text-align: center; }
.login-card .logo { font-size: 44px; }
.login-card h1 { margin: 6px 0 4px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #222; color: #fff;
  padding: 10px 16px; border-radius: 12px; z-index: 30; display: flex; gap: 14px; align-items: center; max-width: 92vw; box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.toast .link { color: #7cb4ff; font-weight: 600; }
@media (max-width: 520px) {
  .bar-row { grid-template-columns: 100px 1fr 72px; font-size: 13px; }
  .kpi .value { font-size: 19px; }
  .row { gap: 10px; padding: 10px 12px; }
  .row .btn.sm { padding: 5px 8px; }
}
.cols > *, .grid > * { min-width: 0; }
@media (max-width: 860px) { .cols { grid-template-columns: minmax(0, 1fr); } }
#tabs { min-width: 0; } .top { max-width: 100vw; }
@media (max-width: 520px) { .row .meta { white-space: normal; } }
