:root {
  --bg: #0f1115;
  --card: #181b21;
  --card-2: #21252d;
  --line: rgba(255, 255, 255, 0.07);
  --text: #eef0f3;
  --hint: #8a919c;
  --accent: #2ec4a6;
  --accent-text: #06231d;
  --green: #3ddc97;
  --red: #ff6b6b;
  --amber: #ffb84d;
  --income: #4dabf7;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]):not(.tg) {
    --bg: #f3f4f6;
    --card: #ffffff;
    --card-2: #eef0f3;
    --line: rgba(0, 0, 0, 0.07);
    --text: #14171c;
    --hint: #6b7280;
    --accent: #0f9e85;
    --accent-text: #ffffff;
    --green: #12a46b;
    --red: #e03131;
    --amber: #d9480f;
    --income: #1c7ed6;
    color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #f3f4f6; --card: #fff; --card-2: #eef0f3; --line: rgba(0,0,0,.07);
  --text: #14171c; --hint: #6b7280; --accent: #0f9e85; --accent-text: #fff;
  --green: #12a46b; --red: #e03131; --amber: #d9480f; --income: #1c7ed6; color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
.num { font-variant-numeric: tabular-nums; }
.hint { color: var(--hint); }
.small { font-size: 13px; }

#app { max-width: 560px; margin: 0 auto; padding: 12px 16px calc(84px + var(--safe-b)); }

/* ---------- header ---------- */
.topbar { display: flex; align-items: center; gap: 8px; margin: 4px 0 12px; }
.topbar h1 { font-size: 22px; font-weight: 700; margin: 0; flex: 1; letter-spacing: -0.01em; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 12px; background: var(--card);
  display: inline-grid; place-items: center; font-size: 17px; flex: none;
}
.icon-btn:active { transform: scale(0.94); }

.seg { display: flex; background: var(--card); border-radius: 12px; padding: 3px; gap: 2px; }
.seg button { flex: 1; padding: 7px 10px; border-radius: 9px; color: var(--hint); font-weight: 600; font-size: 14px; white-space: nowrap; }
.seg button.on { background: var(--card-2); color: var(--text); }

/* ---------- summary chips ---------- */
.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px 14px; min-width: 0; }
.stat .label { color: var(--hint); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.stat .value { font-size: 18px; font-weight: 700; margin-top: 2px; overflow-wrap: anywhere; }
.stat .sub { color: var(--hint); font-size: 12px; margin-top: 2px; }
.stat.out .value { color: var(--text); }
.stat.in .value { color: var(--income); }

.convert { display: flex; align-items: center; gap: 8px; margin: 0 0 12px; }
.convert .seg { flex: none; }
.convert .seg button { flex: none; padding: 5px 10px; font-size: 13px; }
.convert .rate { font-size: 12px; color: var(--hint); flex: 1; text-align: right; }

/* ---------- month grid ---------- */
.cal { background: var(--card); border-radius: 20px; padding: 10px 8px 8px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 4px 8px; }
.cal-head .title { font-weight: 700; font-size: 17px; }
.dow, .grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.dow div { text-align: center; color: var(--hint); font-size: 12px; font-weight: 600; padding-bottom: 4px; }
.day {
  position: relative; aspect-ratio: 0.78; border-radius: 12px; background: var(--card-2);
  display: flex; flex-direction: column; align-items: center; padding-top: 6px; gap: 4px;
  min-width: 0; overflow: hidden;
}
.day .d { font-size: 14px; font-weight: 600; line-height: 1; }
.day.other { opacity: .35; }
.day.past:not(.other) .d { color: var(--hint); }
.day.today { box-shadow: inset 0 0 0 2px var(--accent); }
.day.today .d { color: var(--accent); }
.day.sel { background: color-mix(in srgb, var(--accent) 18%, var(--card-2)); }
.day.has-overdue::after { content: ""; position: absolute; top: 5px; right: 5px; width: 6px; height: 6px; border-radius: 50%; background: var(--red); }
.day .icons { display: flex; justify-content: center; align-items: center; }
.day .icons .av + .av { margin-left: -9px; }
.day .more { font-size: 10px; color: var(--hint); font-weight: 700; line-height: 1; }

/* ---------- avatars ---------- */
.av {
  --s: 28px; width: var(--s); height: var(--s); border-radius: 50%; flex: none;
  display: inline-grid; place-items: center; overflow: hidden; position: relative;
  background: var(--c, #5b6b7a); color: #fff; font-weight: 700; font-size: calc(var(--s) * 0.42);
  box-shadow: 0 0 0 2px var(--card-2);
}
.av img { width: 100%; height: 100%; object-fit: cover; background: #fff; }
.av .emoji { font-size: calc(var(--s) * 0.55); line-height: 1; }
.av.done { opacity: .45; }
.av.done::after { content: "✓"; position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.45); color: #fff; font-size: calc(var(--s) * .5); }
.av.lg { --s: 42px; box-shadow: none; }
.av.md { --s: 36px; box-shadow: none; }

/* ---------- lists ---------- */
.section-title { margin: 18px 4px 8px; font-size: 13px; color: var(--hint); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; display: flex; justify-content: space-between; }
.list { background: var(--card); border-radius: var(--radius); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 11px 14px; width: 100%; text-align: left; }
.row + .row { border-top: 1px solid var(--line); }
.row .main { flex: 1; min-width: 0; }
.row .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .s { color: var(--hint); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .r { text-align: right; flex: none; }
.row .amt { font-weight: 700; }
.row .amt.income { color: var(--income); }
.row .conv { color: var(--hint); font-size: 12px; }
.row.paid .t, .row.paid .amt { text-decoration: line-through; opacity: .55; }
.row.skipped { opacity: .45; }
.row.overdue .s { color: var(--red); }
.row.inactive { opacity: .5; }
.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 999px; background: var(--card-2); color: var(--hint); vertical-align: 1px; margin-left: 6px; }
.badge.today { background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--accent); }
.badge.red { background: color-mix(in srgb, var(--red) 20%, transparent); color: var(--red); }

.check {
  width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--hint); flex: none;
  display: grid; place-items: center; color: transparent; font-size: 16px; font-weight: 800;
}
.check.on { background: var(--green); border-color: var(--green); color: #06231d; }

.day-group { margin-top: 14px; }
.day-group .dg-head { display: flex; justify-content: space-between; padding: 0 4px 6px; font-weight: 700; }
.day-group .dg-head .hint { font-weight: 500; }
.day-group.is-today .dg-head span:first-child { color: var(--accent); }

.empty { text-align: center; padding: 28px 16px; color: var(--hint); }
.empty .big { font-size: 40px; margin-bottom: 6px; }

/* ---------- tab bar & fab ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: center; gap: 4px;
  padding: 8px 16px calc(8px + var(--safe-b));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar button { flex: 1; max-width: 120px; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px; color: var(--hint); font-size: 11px; font-weight: 600; }
.tabbar button .i { font-size: 20px; line-height: 1.1; }
.tabbar button.on { color: var(--accent); }
.fab {
  position: fixed; right: max(16px, calc(50% - 264px)); bottom: calc(76px + var(--safe-b)); z-index: 21;
  width: 54px; height: 54px; border-radius: 18px; background: var(--accent); color: var(--accent-text);
  font-size: 30px; font-weight: 400; display: grid; place-items: center;
  box-shadow: 0 8px 24px rgba(0,0,0,.35);
}
.fab:active { transform: scale(0.94); }

/* ---------- sheet ---------- */
#sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 40; animation: fade .18s; }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; max-height: 92vh; overflow-y: auto;
  background: var(--bg); border-radius: 22px 22px 0 0; padding: 8px 16px calc(20px + var(--safe-b));
  max-width: 560px; margin: 0 auto; animation: up .22s cubic-bezier(.2,.8,.2,1);
  overscroll-behavior: contain;
}
#sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--card-2); margin: 0 auto 10px; }
#sheet h2 { margin: 4px 0 14px; font-size: 20px; display: flex; align-items: center; gap: 12px; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

/* ---------- forms ---------- */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--hint); margin: 0 4px 5px; font-weight: 600; }
.input, select.input, textarea.input {
  width: 100%; background: var(--card); border: 1px solid transparent; border-radius: 12px; padding: 11px 13px; outline: none;
  appearance: none; -webkit-appearance: none; min-height: 44px;
}
.input:focus { border-color: var(--accent); }
textarea.input { resize: vertical; min-height: 70px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.amount-row { display: grid; grid-template-columns: 1fr 96px; gap: 8px; }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%;
  background: var(--accent); color: var(--accent-text); border-radius: 14px; padding: 13px; font-weight: 700; font-size: 16px; min-height: 48px;
}
.btn.ghost { background: var(--card); color: var(--text); }
.btn.danger { background: transparent; color: var(--red); }
.btn:disabled { opacity: .5; }
.btns { display: grid; gap: 8px; margin-top: 16px; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }
.chip-btn { background: var(--card); border-radius: 10px; padding: 8px 12px; font-weight: 600; font-size: 14px; }
.chip-btn.on { background: var(--accent); color: var(--accent-text); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }

.preset-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 10px 0 16px; }
.preset { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 4px; border-radius: 14px; background: var(--card); font-size: 12px; text-align: center; min-width: 0; }
.preset .lbl { width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preset .av { box-shadow: none; }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.kv { background: var(--card); border-radius: 14px; padding: 10px 12px; min-width: 0; }
.kv .k { font-size: 12px; color: var(--hint); }
.kv .v { font-weight: 700; overflow-wrap: anywhere; }
.link { color: var(--accent); text-decoration: none; overflow-wrap: anywhere; }

#toast {
  position: fixed; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: 14px;
  max-width: calc(100vw - 32px); animation: fade .2s;
}

.loading { text-align: center; padding: 60px 0; color: var(--hint); }
.err { background: color-mix(in srgb, var(--red) 15%, var(--card)); color: var(--text); border-radius: 12px; padding: 10px 12px; margin-bottom: 12px; font-size: 14px; }

/* ---------- render mode (PNG for Telegram) ---------- */
body.render { background: #0b0d10; }
body.render #app, body.render .tabbar, body.render .fab { display: none; }
#render-root { width: 540px; padding: 22px 18px 18px; background: linear-gradient(180deg, #121821 0%, #0b0d10 100%); color: #eef0f3; }
#render-root .r-head { display: flex; align-items: baseline; justify-content: space-between; margin: 0 6px 14px; }
#render-root .r-title { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; }
#render-root .r-sub { color: #8a919c; font-size: 14px; }
#render-root .cal { background: rgba(255,255,255,.03); }
#render-root .day { aspect-ratio: 0.82; }
#render-root .r-foot { display: flex; gap: 8px; margin-top: 12px; }
#render-root .r-foot .stat { flex: 1; background: rgba(255,255,255,.04); }
#render-root .r-brand { text-align: right; color: #5c636e; font-size: 12px; margin: 10px 6px 0; }
#render-root .r-week .row { padding: 10px 12px; }
#render-root .list { background: rgba(255,255,255,.04); }
