/* Цвета берутся из темы Telegram; запасные значения — для обычного браузера */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --surface: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --accent: var(--tg-theme-button-color, #2f80ed);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --earned: #2fa36b;
  --spent: #e5484d;
  --saved: #2f80ed;
  --monthly: #b07d2b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17212b);
    --surface: var(--tg-theme-secondary-bg-color, #232e3c);
    --text: var(--tg-theme-text-color, #f5f5f5);
    --hint: var(--tg-theme-hint-color, #7d8b99);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

main { max-width: 560px; margin: 0 auto; padding: 12px 16px 96px; display: grid; gap: 16px; }
.view { display: grid; gap: 16px; }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }

.month-nav { display: flex; align-items: center; justify-content: space-between; }
.month-nav h1 { margin: 0; font-size: 1.1rem; font-weight: 600; }
.month-nav h1::first-letter { text-transform: uppercase; }
.icon { font-size: 1.6rem; line-height: 1; width: 44px; height: 44px; border-radius: 50%; color: var(--accent); }
.icon:active { background: var(--surface); }

.free { display: grid; justify-items: center; gap: 2px; padding: 8px 0; }
.free span { color: var(--hint); font-size: .9rem; }
.free strong { font-size: 2rem; font-variant-numeric: tabular-nums; }
.free strong.negative { color: var(--danger); }

.hint { color: var(--hint); font-size: .8rem; margin: 0; }
.center { text-align: center; }

.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.card {
  display: grid; gap: 6px; text-align: left; padding: 14px; border-radius: 14px;
  background: var(--surface); border-left: 4px solid var(--kind-color);
}
.card:active { filter: brightness(.95); }
.card span { color: var(--hint); font-size: .85rem; }
.card strong { font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.card small { color: var(--hint); font-size: .75rem; }

.earned { --kind-color: var(--earned); }
.spent { --kind-color: var(--spent); }
.saved { --kind-color: var(--saved); }
.monthly { --kind-color: var(--monthly); }

.list-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.list-head h2 { margin: 0; font-size: 1rem; }
select { background: var(--surface); border: 0; border-radius: 8px; padding: 6px 10px; }

.entries { list-style: none; margin: 0; padding: 0; display: grid; }
.entry {
  display: grid; grid-template-columns: 10px 1fr auto; align-items: center; gap: 12px;
  padding: 10px 4px; border-bottom: 1px solid var(--surface); cursor: pointer;
}
.entry:active { background: var(--surface); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--kind-color); }
.entry-text { display: grid; gap: 2px; min-width: 0; }
.entry-text b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-text small { color: var(--hint); }
.entry-amount { font-variant-numeric: tabular-nums; font-weight: 500; }
.entry.earned .entry-amount { color: var(--earned); }

.fab {
  position: fixed; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 50%; padding: 0;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text); box-shadow: 0 4px 12px rgb(0 0 0 / .2);
}
.fab svg { display: block; }

.sheet-backdrop { position: fixed; inset: 0; background: rgb(0 0 0 / .4); }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-width: 560px; margin: 0 auto;
  padding: 20px 16px calc(16px + env(safe-area-inset-bottom));
  background: var(--bg); border-radius: 16px 16px 0 0; display: grid; gap: 14px;
  max-height: 100vh; overflow-y: auto;
}
.sheet h2 { margin: 0; font-size: 1.05rem; }
/* Элементы сетки не должны растягивать шторку шире экрана */
.sheet { overflow-x: hidden; }
.sheet > * { min-width: 0; }

.kinds { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.kinds input { position: absolute; opacity: 0; pointer-events: none; }
.kinds span {
  display: block; text-align: center; padding: 10px 6px; border-radius: 10px; font-size: .9rem;
  background: var(--surface); border: 2px solid transparent;
}
.kinds input:checked + span { border-color: var(--kind-color); font-weight: 600; }
.kinds label:nth-child(1) { --kind-color: var(--earned); }
.kinds label:nth-child(2) { --kind-color: var(--spent); }
.kinds label:nth-child(3) { --kind-color: var(--saved); }
.kinds label:nth-child(4) { --kind-color: var(--monthly); }

.field { display: grid; gap: 6px; color: var(--hint); font-size: .85rem; }
.field input { padding: 12px; border-radius: 10px; border: 0; background: var(--surface); color: var(--text); font-size: 1rem; width: 100%; }
.field input[name="amount"] { font-size: 1.4rem; font-variant-numeric: tabular-nums; }

.sheet-actions { display: grid; gap: 8px; }
.sheet-actions button { padding: 13px; border-radius: 10px; font-weight: 500; }
.primary { background: var(--accent); color: var(--accent-text); }
.secondary { background: var(--surface); }
.danger { background: var(--surface); color: var(--danger); }

.error { color: var(--danger); margin: 0; font-size: .9rem; }

.progress { height: 6px; border-radius: 3px; background: color-mix(in srgb, var(--hint) 25%, transparent); overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--kind-color, var(--monthly)); transition: width .3s; }
.progress-bar.done { background: var(--earned); }

.view-head { display: flex; align-items: center; gap: 12px; }
.view-head h2 { margin: 0; font-size: 1.1rem; }
.link { color: var(--accent); padding: 4px 0; font-size: .95rem; }

.loans { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.loan { display: grid; gap: 8px; padding: 14px; border-radius: 14px; background: var(--surface); border-left: 4px solid var(--monthly); --kind-color: var(--monthly); }
.loan-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.loan-head b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.loan-amount { font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.loan-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.loan-foot small { color: var(--hint); }
button.small { padding: 8px 14px; border-radius: 8px; font-weight: 500; }
.wide-button { padding: 13px; border-radius: 10px; font-weight: 500; }

.field select { padding: 12px; border-radius: 10px; font-size: 1rem; color: var(--text); width: 100%; }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: .95rem; }
.check input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }
.check span { display: grid; gap: 2px; }
.check small { color: var(--hint); font-size: .8rem; }

.free-button { width: 100%; text-align: center; border-radius: 14px; }
.free-button:active { background: var(--surface); }
.entry.positive .entry-amount { color: var(--earned); }
.wallet { border-left-color: var(--earned); }

.breakdown { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.breakdown-row { display: grid; gap: 4px; padding: 6px 8px; border-radius: 10px; cursor: pointer; --kind-color: var(--spent); }
.breakdown-row:active, .breakdown-row.active { background: var(--surface); }
.breakdown-row .bar-label { display: flex; justify-content: space-between; gap: 8px; font-size: .95rem; }
.breakdown-row .bar-label b { font-weight: 500; font-variant-numeric: tabular-nums; white-space: nowrap; }

.filter-pill { margin-bottom: 8px; padding: 6px 12px; border-radius: 16px; background: var(--surface); font-size: .9rem; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 7px 12px; border-radius: 16px; background: var(--surface); color: var(--text); font-size: .9rem; border: 2px solid transparent; }
.chip.selected { border-color: var(--spent); font-weight: 600; }

.category-rows { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; max-height: 45vh; overflow-y: auto; }
.category-row { display: flex; gap: 8px; align-items: center; }
.category-row input { padding: 10px; border-radius: 10px; border: 0; background: var(--surface); color: var(--text); font-size: 1rem; flex: 1; min-width: 0; width: 100%; }
.category-row .emoji-input { flex: none; width: 52px; text-align: center; }
.category-row .delete { color: var(--hint); padding: 6px 8px; }
.category-row.new { padding-top: 8px; border-top: 1px solid var(--surface); }
.card small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
