:root {
  --bg: var(--tg-theme-bg-color, #0f1115);
  --bg-secondary: var(--tg-theme-secondary-bg-color, #1a1d24);
  --text: var(--tg-theme-text-color, #f2f4f8);
  --hint: var(--tg-theme-hint-color, #8b93a7);
  --link: var(--tg-theme-link-color, #6c9eff);
  --btn: var(--tg-theme-button-color, #3d7eff);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --card: color-mix(in srgb, var(--bg-secondary) 92%, var(--text) 8%);
  --border: color-mix(in srgb, var(--text) 10%, transparent);
  --income: #34c759;
  --expense: #ff5c5c;
  --radius: 14px;
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 8px;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 18px;
}

.brand-icon { font-size: 20px; }

#views {
  flex: 1;
  overflow-y: auto;
  padding: 0 14px calc(var(--nav-h) + var(--safe-b) + 16px);
  -webkit-overflow-scrolling: touch;
}

.view { display: none; }
.view.active { display: block; animation: fade .18s ease; }

@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.stat-card.primary {
  grid-column: 1 / -1;
  background: linear-gradient(135deg, color-mix(in srgb, var(--btn) 35%, var(--card)), var(--card));
  border-color: color-mix(in srgb, var(--btn) 40%, transparent);
}

.stat-label {
  font-size: 12px;
  color: var(--hint);
  margin-bottom: 4px;
}

.stat-value {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.stat-card.primary .stat-value { font-size: 28px; }
.stat-value.income { color: var(--income); }
.stat-value.expense { color: var(--expense); }

/* Quick actions */
.quick-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.quick-actions.compact {
  grid-template-columns: repeat(3, 1fr);
}

.qa-btn {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 6px;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  transition: transform .1s ease, opacity .1s;
}

.qa-btn span { font-size: 16px; line-height: 1; }
.qa-btn.expense { border-color: color-mix(in srgb, var(--expense) 35%, var(--border)); }
.qa-btn.income { border-color: color-mix(in srgb, var(--income) 35%, var(--border)); }
.qa-btn:active { transform: scale(.96); opacity: .9; }

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  gap: 8px;
}

.card-head h2, .ai-hero h2 {
  font-size: 15px;
  font-weight: 650;
}

.link-btn {
  background: none;
  border: none;
  color: var(--link);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.muted { color: var(--hint); font-size: 13px; line-height: 1.4; }

/* Lists */
.list { display: flex; flex-direction: column; gap: 6px; }
.list.stacked { gap: 10px; }

.list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  border: 1px solid transparent;
}

.list-item:active { border-color: var(--border); }

.li-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--btn) 18%, transparent);
  flex-shrink: 0;
  font-size: 15px;
}

.li-icon.expense { background: color-mix(in srgb, var(--expense) 18%, transparent); }
.li-icon.income { background: color-mix(in srgb, var(--income) 18%, transparent); }

.li-body { flex: 1; min-width: 0; }
.li-title {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.li-sub { font-size: 12px; color: var(--hint); margin-top: 2px; }

.li-right {
  text-align: right;
  flex-shrink: 0;
}

.li-amount {
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.li-amount.expense { color: var(--expense); }
.li-amount.income { color: var(--income); }

.li-actions {
  display: flex;
  gap: 4px;
  margin-top: 4px;
  justify-content: flex-end;
}

.chip {
  border: none;
  background: color-mix(in srgb, var(--btn) 20%, transparent);
  color: var(--link);
  font-size: 11px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 8px;
  cursor: pointer;
}

.chip.danger {
  background: color-mix(in srgb, var(--expense) 18%, transparent);
  color: var(--expense);
}

.chip.ok {
  background: color-mix(in srgb, var(--income) 18%, transparent);
  color: var(--income);
}

.empty {
  text-align: center;
  color: var(--hint);
  font-size: 13px;
  padding: 18px 8px;
}

/* Payment cards */
.pay-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--bg) 50%, transparent);
  border: 1px solid var(--border);
}

.pay-card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.pay-card-main {
  flex: 1;
  min-width: 0;
}

.pay-name {
  font-size: 15px;
  font-weight: 650;
  line-height: 1.35;
  word-break: break-word;
  overflow-wrap: anywhere;
  white-space: normal;
  margin: 0 0 8px;
}

.pay-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 8px;
}

.pay-badge {
  display: inline-flex;
  align-items: center;
  font-size: 11px;
  font-weight: 650;
  padding: 3px 8px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--btn) 18%, transparent);
  color: var(--link);
}

.pay-date {
  font-size: 12px;
  color: var(--hint);
}

.pay-sum {
  flex-shrink: 0;
  font-size: 16px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  text-align: right;
  white-space: nowrap;
  padding-top: 1px;
}

.pay-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding-top: 2px;
  border-top: 1px solid var(--border);
}

.pay-actions .chip {
  flex: 1 1 auto;
  min-width: 0;
  text-align: center;
  padding: 8px 10px;
  font-size: 12px;
  border-radius: 10px;
}

#payments-list,
#upcoming-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Charts */
.chart-wrap {
  position: relative;
  height: 180px;
}

.chart-wrap.tall { height: 200px; }

.empty-hint {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  color: var(--hint);
  font-size: 13px;
  text-align: center;
  padding: 12px;
}

.empty-hint.show { display: grid; }

/* Table */
.table { width: 100%; }
.table-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  align-items: center;
}
.table-row:last-child { border-bottom: none; }
.table-row.head {
  color: var(--hint);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  font-weight: 600;
}
.table-row .num {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  text-align: right;
}

/* Tabs */
.tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
  background: var(--card);
  border-radius: 12px;
  padding: 4px;
  border: 1px solid var(--border);
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--hint);
  font-size: 13px;
  font-weight: 600;
  padding: 8px;
  border-radius: 9px;
  cursor: pointer;
}

.tab.active {
  background: color-mix(in srgb, var(--btn) 25%, transparent);
  color: var(--text);
}

/* Buttons */
.btn {
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 650;
  cursor: pointer;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn.primary {
  background: var(--btn);
  color: var(--btn-text);
  border-color: transparent;
}

.btn.full { width: 100%; }
.btn.btn-sm { padding: 7px 12px; font-size: 12px; border-radius: 10px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

/* Bottom nav */
.bottom-nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--nav-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 50;
}

.nav-item {
  flex: 1;
  border: none;
  background: none;
  color: var(--hint);
  font-size: 10px;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  padding-top: 6px;
}

.nav-item .nav-ico { font-size: 18px; line-height: 1; }
.nav-item.active { color: var(--btn); }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 12px;
  padding-bottom: calc(12px + var(--safe-b));
}

.modal-overlay.hidden { display: none; }

.modal {
  width: min(480px, 100%);
  max-height: 88vh;
  overflow-y: auto;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 18px 18px 16px 16px;
  padding: 14px 16px 18px;
  animation: slide-up .2s ease;
}

@keyframes slide-up {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.modal-head h3 { font-size: 17px; font-weight: 700; }

.form-group { margin-bottom: 12px; }

.form-group label {
  display: block;
  font-size: 12px;
  color: var(--hint);
  margin-bottom: 6px;
  font-weight: 600;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 90px;
  gap: 8px;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 11px;
  padding: 11px 12px;
  font-size: 15px;
  font-family: inherit;
  outline: none;
}

input:focus, select:focus, textarea:focus {
  border-color: color-mix(in srgb, var(--btn) 60%, var(--border));
}

textarea { min-height: 80px; resize: vertical; }

.form-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 6px;
}

.form-actions .full { grid-column: 1 / -1; }

/* AI */
.ai-hero { text-align: center; }
.ai-hero h2 { margin-bottom: 6px; }
.ai-hero p { margin-bottom: 14px; }

.ai-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.ai-card .meta {
  font-size: 11px;
  color: var(--hint);
  margin-bottom: 8px;
}

.ai-content {
  font-size: 13.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.ai-content strong, .ai-content b { font-weight: 700; }

/* Toast & loader */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--safe-b) + 16px);
  transform: translateX(-50%);
  background: color-mix(in srgb, var(--text) 90%, var(--bg));
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
  z-index: 200;
  max-width: 90%;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}

.toast.hidden { display: none; }

.loader {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 150;
  display: grid;
  place-items: center;
}

.loader.hidden { display: none; }

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid color-mix(in srgb, var(--text) 20%, transparent);
  border-top-color: var(--btn);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
