:root {
  --bg: var(--tg-theme-secondary-bg-color, #f2f3f7);
  --card: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #161824);
  --hint: var(--tg-theme-hint-color, #8b90a2);
  --line: rgba(128, 132, 150, 0.16);
  --field: rgba(128, 132, 150, 0.09);
  --brand: #3046c5;
  --debt: #e5484d;
  --debt-soft: rgba(229, 72, 77, 0.1);
  --pay: #0f9f8c;
  --pay-soft: rgba(15, 159, 140, 0.12);
  --radius: 18px;
  color-scheme: light dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
main { max-width: 560px; margin: 0 auto; padding: 14px 16px 40px; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
h1, h2 { margin: 0; }
.hint { color: var(--hint); font-size: 13px; }
.loading, .empty { text-align: center; color: var(--hint); padding: 40px 10px; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- header ---------- */
.top { display: flex; align-items: center; gap: 12px; margin: 4px 0 16px; }
.logo { width: 40px; height: 40px; border-radius: 12px; background: var(--brand); display: grid; place-items: center; flex: none; }
.logo svg { width: 22px; height: 22px; }
.top h1 { font-size: 20px; font-weight: 750; letter-spacing: -0.2px; }
.page-head { display: flex; align-items: center; gap: 12px; margin: 2px 0 16px; }
.page-head h1 { font-size: 19px; font-weight: 700; }
.back { width: 38px; height: 38px; border-radius: 50%; background: var(--card); display: grid; place-items: center; flex: none; box-shadow: 0 0 0 1px var(--line); }
.back svg { width: 18px; height: 18px; }
.dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.dot.debt { background: var(--debt); }
.dot.pay { background: var(--pay); }

/* ---------- big actions ---------- */
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.big {
  border-radius: 22px; min-height: 150px; padding: 18px 14px; color: #fff;
  display: flex; flex-direction: column; align-items: flex-start; justify-content: space-between; text-align: left;
  transition: transform .12s ease;
}
.big:active { transform: scale(0.97); }
.big.debt { background: linear-gradient(150deg, #f0636a, var(--debt)); }
.big.pay { background: linear-gradient(150deg, #22b8a3, var(--pay)); }
.big .ico { width: 46px; height: 46px; border-radius: 14px; background: rgba(255,255,255,.22); display: grid; place-items: center; }
.big .ico svg { width: 24px; height: 24px; }
.big b { font-size: 17px; line-height: 1.2; }

/* ---------- cards ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-top: 12px; box-shadow: 0 0 0 1px var(--line); }
.total { display: flex; justify-content: space-between; align-items: flex-end; gap: 10px; }
.total .sum { font-size: 27px; font-weight: 800; letter-spacing: -0.5px; margin-top: 2px; }
.total .right { text-align: right; flex: none; }
.link { color: var(--brand); font-weight: 600; font-size: 14px; }
.dark { background: #1b1e2e; color: #fff; box-shadow: none; }
.dark .hint { color: rgba(255,255,255,.6); }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 12px; }
.tile { background: var(--card); border-radius: 16px; padding: 14px 6px; text-align: center; box-shadow: 0 0 0 1px var(--line); font-size: 13px; font-weight: 600; }
.tile .ti { font-size: 24px; display: block; margin-bottom: 6px; }

.section-head { display: flex; justify-content: space-between; align-items: center; margin: 22px 2px 8px; }
.section-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--hint); font-weight: 700; }
.section-head a { color: var(--brand); font-weight: 600; font-size: 14px; }

/* ---------- list ---------- */
.list { background: var(--card); border-radius: var(--radius); box-shadow: 0 0 0 1px var(--line); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-top: 1px solid var(--line); }
.row:first-child { border-top: 0; }
.row:active { background: var(--field); }
.av { width: 42px; height: 42px; border-radius: 50%; color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 14px; flex: none; }
.av.lg { width: 54px; height: 54px; font-size: 18px; }
.mid { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.mid b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mid small { color: var(--hint); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.amt { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.amt.debt { color: var(--debt); }
.amt.pay { color: var(--pay); }
.amt.zero { color: var(--hint); font-weight: 500; font-size: 13px; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 20px; background: var(--field); color: var(--hint); }
.badge.ok { background: var(--pay-soft); color: var(--pay); }
.badge.new { background: rgba(48,70,197,.12); color: var(--brand); }

/* ---------- forms ---------- */
.label { display: block; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; color: var(--hint); margin: 16px 2px 7px; }
.input {
  width: 100%; border: 1.5px solid transparent; background: var(--card); color: var(--text);
  border-radius: 14px; padding: 13px 14px; font: inherit; outline: none; box-shadow: 0 0 0 1px var(--line);
}
.card .input { background: var(--field); box-shadow: none; }
.input:focus { border-color: var(--brand); }
.input.amount { font-size: 26px; font-weight: 750; text-align: center; padding: 14px; }
.chips { display: grid; grid-template-columns: repeat(5, 1fr); gap: 7px; margin-top: 8px; }
.chip { background: var(--card); box-shadow: 0 0 0 1px var(--line); border-radius: 11px; padding: 9px 0; font-size: 13px; font-weight: 600; }
.card .chip { background: var(--field); box-shadow: none; }
.chip:active { transform: scale(.96); }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg button { border-radius: 14px; padding: 13px 8px; background: var(--card); box-shadow: 0 0 0 1px var(--line); font-weight: 650; }
.card .seg button { background: var(--field); box-shadow: none; }
.seg button.on { background: var(--brand); color: #fff; box-shadow: none; }
.seg.kind button.on.debt { background: var(--debt); }
.seg.kind button.on.pay { background: var(--pay); }
.btn { width: 100%; border-radius: 15px; padding: 15px; font-weight: 700; font-size: 16px; color: #fff; background: var(--brand); margin-top: 18px; }
.btn.debt { background: var(--debt); }
.btn.pay { background: var(--pay); }
.btn.ghost { background: var(--field); color: var(--text); }
.btn.danger-out { background: transparent; color: var(--debt); box-shadow: inset 0 0 0 1.5px var(--debt-soft); }
.btn:disabled { opacity: .45; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn-row .btn { margin-top: 14px; }
.add-btn { display: flex; align-items: center; justify-content: center; gap: 8px; }

/* picker */
.picker { position: relative; }
.dropdown { position: absolute; left: 0; right: 0; top: calc(100% + 6px); z-index: 5; background: var(--card); border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,.18), 0 0 0 1px var(--line); max-height: 320px; overflow-y: auto; }
.dropdown .row { cursor: pointer; }
.row.create .av { background: transparent; border: 2px dashed var(--pay); color: var(--pay); }
.row.create b { color: var(--pay); }
.picked { display: flex; align-items: center; gap: 12px; background: var(--card); border-radius: 14px; padding: 10px 12px; box-shadow: 0 0 0 1.5px var(--brand); }
.picked .x { width: 32px; height: 32px; border-radius: 50%; background: var(--field); font-size: 16px; }

/* customer card */
.profile { display: flex; align-items: center; gap: 14px; }
.icon-btn { width: 38px; height: 38px; border-radius: 50%; background: var(--field); display: grid; place-items: center; flex: none; }
.icon-btn svg { width: 17px; height: 17px; }
.balance { margin-top: 14px; border-radius: 14px; padding: 12px 14px; display: flex; justify-content: space-between; align-items: center; background: var(--field); }
.balance.debt { background: var(--debt-soft); }
.balance.debt .sum { color: var(--debt); }
.balance.pay { background: var(--pay-soft); }
.balance.pay .sum { color: var(--pay); }
.balance .sum { font-size: 22px; font-weight: 800; }
.remind { display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 12px; }
.note-line { font-size: 12.5px; color: var(--hint); text-align: center; margin-top: 8px; }

.tx { padding: 12px 14px; border-top: 1px solid var(--line); }
.tx:first-child { border-top: 0; }
.tx-main { display: flex; gap: 12px; align-items: center; }
.tx-ico { width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; flex: none; }
.tx-ico svg { width: 18px; height: 18px; }
.tx-ico.debt { background: var(--debt-soft); color: var(--debt); }
.tx-ico.pay { background: var(--pay-soft); color: var(--pay); }
.tx .mid small { white-space: normal; }
.tx-actions { display: flex; justify-content: flex-end; gap: 16px; margin-top: 6px; font-size: 13px; }
.tx-actions button { color: var(--hint); font-weight: 600; padding: 2px 0; }
.tx-actions button.del { color: var(--debt); }

/* report */
.period h3 { margin: 0 0 8px; font-size: 15px; }
.kv { display: flex; justify-content: space-between; padding: 6px 0; border-top: 1px solid var(--line); font-size: 14px; }
.kv:first-of-type { border-top: 0; }
.kv.sub { color: var(--hint); font-size: 13px; padding-left: 12px; }

/* toast & sheet */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #1b1e2e; color: #fff; padding: 11px 18px; border-radius: 14px; font-size: 14px; font-weight: 600; z-index: 50; width: max-content; max-width: calc(100% - 32px); text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.25); }
.toast.err { background: var(--debt); }
.sheet-wrap { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--bg); width: 100%; max-width: 560px; border-radius: 22px 22px 0 0; padding: 18px 16px 26px; }
.sheet h3 { margin: 0; font-size: 17px; }

.welcome { text-align: center; padding: 50px 16px; }
.welcome .logo { margin: 0 auto 16px; width: 60px; height: 60px; border-radius: 18px; }
.welcome .logo svg { width: 32px; height: 32px; }

@media (prefers-color-scheme: dark) {
  :root:not([data-tg]) { --bg: #12131a; --card: #1c1e29; --text: #eef0f6; --hint: #8f94a8; }
}
