:root {
  --bg: #F2F2F7;
  --card: #FFFFFF;
  --text: #000000;
  --muted: #8E8E93;
  --sep: rgba(60, 60, 67, .18);
  --fill: rgba(118, 118, 128, .12);
  --accent: #007AFF;
  --green: #34C759;
  --red: #FF3B30;
  --orange: #FF9500;
  --tabbar: rgba(249, 249, 249, .92);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1C1C1E;
    --text: #FFFFFF;
    --muted: #8E8E93;
    --sep: rgba(84, 84, 88, .6);
    --fill: rgba(118, 118, 128, .24);
    --accent: #0A84FF;
    --green: #30D158;
    --red: #FF453A;
    --orange: #FF9F0A;
    --tabbar: rgba(22, 22, 24, .92);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
input, select, textarea { font-size: 17px; } /* ≥16px — iOS не зумит поле */
.hidden { display: none !important; }

.splash {
  min-height: 100dvh; display: grid; place-items: center;
  font-size: 28px; font-weight: 700; color: var(--muted);
}

/* ---------- Каркас ---------- */
.screen { padding: calc(var(--safe-top) + 8px) 16px calc(var(--safe-bottom) + 96px); max-width: 640px; margin: 0 auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 4px 0 12px; }
.budget-btn { font-size: 28px; font-weight: 700; display: flex; align-items: center; gap: 6px; min-width: 0; text-align: left; }
.budget-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.budget-btn::after { content: "▾"; font-size: 18px; color: var(--muted); }
.monthbar { display: flex; align-items: center; justify-content: space-between; background: var(--card); border-radius: var(--radius); padding: 4px; margin-bottom: 14px; }
.monthbar button { width: 44px; height: 36px; font-size: 22px; color: var(--accent); border-radius: 10px; }
.monthbar button:disabled { color: var(--muted); opacity: .4; }
.monthbar .label { font-weight: 600; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.section-title { font-size: 13px; text-transform: uppercase; color: var(--muted); margin: 18px 16px 6px; letter-spacing: .02em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.banner { background: var(--orange); color: #000; border-radius: 10px; padding: 8px 12px; font-size: 14px; margin-bottom: 12px; }

/* ---------- Баланс ---------- */
.balance .label { color: var(--muted); font-size: 15px; }
.balance .value { font-size: 34px; font-weight: 700; margin: 2px 0 12px; font-variant-numeric: tabular-nums; }
.balance .split { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.balance .split div { background: var(--fill); border-radius: 10px; padding: 8px 10px; }
.balance .split b { display: block; font-size: 17px; font-variant-numeric: tabular-nums; }
.pos { color: var(--green); }
.neg { color: var(--red); }

/* ---------- Диаграмма ---------- */
.chart { display: flex; align-items: center; gap: 16px; }
.donut { width: 132px; height: 132px; flex: none; }
.donut text { fill: var(--text); }
.cat-list { list-style: none; margin: 0; padding: 0; }
.cat-item { padding: 10px 0; border-top: 0.5px solid var(--sep); }
.cat-item:first-child { border-top: 0; }
.cat-item .top { display: flex; align-items: center; gap: 10px; }
.cat-item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-item .amt { font-variant-numeric: tabular-nums; font-weight: 600; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.bar { height: 6px; background: var(--fill); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 3px; }
.limit-note { font-size: 13px; margin-top: 3px; }

/* ---------- Список операций ---------- */
.list { background: var(--card); border-radius: var(--radius); overflow: hidden; margin-bottom: 14px; }
.tx { display: flex; align-items: center; gap: 12px; padding: 10px 16px; width: 100%; text-align: left; }
.tx + .tx { border-top: 0.5px solid var(--sep); }
.tx:active { background: var(--fill); }
.tx .ico { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; font-size: 19px; flex: none; }
.tx .main { flex: 1; min-width: 0; }
.tx .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .sum { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tx.pending { opacity: .6; }
.day-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin: 16px 16px 6px; }
.empty { text-align: center; color: var(--muted); padding: 28px 16px; }

.search { width: 100%; border: 0; border-radius: 10px; background: var(--fill); padding: 9px 12px; margin-bottom: 10px; outline: none; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; margin-bottom: 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 6px 12px; border-radius: 16px; background: var(--card); font-size: 15px; }
.chip.on { background: var(--accent); color: #fff; }

/* ---------- Настройки ---------- */
.cell { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; width: 100%; text-align: left; min-height: 44px; }
.cell + .cell { border-top: 0.5px solid var(--sep); }
.cell:active { background: var(--fill); }
.cell .val { color: var(--muted); text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell.link { color: var(--accent); }
.cell.danger { color: var(--red); }
.code { font-family: ui-monospace, Menlo, monospace; letter-spacing: .12em; font-weight: 700; color: var(--text); }

/* ---------- Нижняя панель ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  padding: 6px 0 calc(var(--safe-bottom) + 4px);
  background: var(--tabbar); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--sep);
}
.tabbar button { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); width: 80px; }
.tabbar button svg { width: 26px; height: 26px; }
.tabbar button.on { color: var(--accent); }
.fab {
  position: fixed; right: 18px; bottom: calc(var(--safe-bottom) + 72px); z-index: 11;
  width: 58px; height: 58px; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 32px; line-height: 1;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}
.fab:active { transform: scale(.94); }

/* ---------- Шторка ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 50; opacity: 0; transition: opacity .2s; }
.backdrop.show { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  max-width: 640px; margin: 0 auto; max-height: 92dvh; overflow-y: auto;
  background: var(--bg); border-radius: 14px 14px 0 0;
  padding: 8px 16px calc(var(--safe-bottom) + 16px);
  transform: translateY(100%); transition: transform .25s ease-out, bottom .15s ease-out;
}
.sheet.show { transform: none; }
.sheet .grabber { width: 36px; height: 5px; border-radius: 3px; background: var(--sep); margin: 0 auto 8px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; min-height: 36px; }
.sheet-head h2 { font-size: 17px; margin: 0; }
.sheet-head button { color: var(--accent); min-width: 70px; }
.sheet-head button:last-child { text-align: right; font-weight: 600; }
.sheet-head button:first-child { text-align: left; }

.segment { display: grid; grid-template-columns: 1fr 1fr; background: var(--fill); border-radius: 9px; padding: 2px; margin-bottom: 14px; }
.segment button { padding: 6px; border-radius: 7px; font-size: 15px; font-weight: 500; }
.segment button.on { background: var(--card); box-shadow: 0 2px 6px rgba(0, 0, 0, .12); }

.amount-wrap { display: flex; align-items: baseline; justify-content: center; gap: 8px; margin: 6px 0 16px; }
.amount-input {
  border: 0; outline: none; background: none; text-align: center;
  font-size: 44px; font-weight: 700; width: 100%; max-width: 280px; font-variant-numeric: tabular-nums;
}
.amount-wrap .cur { font-size: 26px; color: var(--muted); font-weight: 600; }

.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.cat-grid button { background: var(--card); border-radius: 12px; padding: 8px 4px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; border: 2px solid transparent; }
.cat-grid button span:first-child { font-size: 22px; }
.cat-grid button span:last-child { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-grid button.on { border-color: var(--accent); }

.field { background: var(--card); border-radius: var(--radius); overflow: hidden; margin-bottom: 14px; }
.field label { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 0 16px; min-height: 46px; }
.field label + label { border-top: 0.5px solid var(--sep); }
.field input, .field select { border: 0; outline: none; background: none; text-align: right; flex: 1; min-width: 0; padding: 11px 0; }
.field input[type="date"] { -webkit-appearance: none; appearance: none; min-height: 22px; }
.field input[type="color"] { flex: none; width: 44px; height: 30px; padding: 0; border-radius: 6px; }

.btn { display: block; width: 100%; padding: 14px; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 600; text-align: center; margin-bottom: 10px; }
.btn:disabled { opacity: .5; }
.btn.secondary { background: var(--card); color: var(--accent); }
.btn.danger { background: var(--card); color: var(--red); }

.emoji-row { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 16px; border-top: 0.5px solid var(--sep); }
.emoji-row button { font-size: 22px; width: 38px; height: 38px; border-radius: 8px; }
.emoji-row button.on { background: var(--fill); outline: 2px solid var(--accent); }

/* ---------- Вход ---------- */
.auth { max-width: 420px; margin: 0 auto; padding: calc(var(--safe-top) + 64px) 20px 40px; }
.auth .logo { width: 84px; height: 84px; border-radius: 20px; display: block; margin: 0 auto 14px; }
.auth h1 { text-align: center; margin: 0 0 4px; font-size: 30px; }
.auth p.sub { text-align: center; color: var(--muted); margin: 0 0 28px; }
.auth .field input { text-align: left; }
.auth .switch { text-align: center; color: var(--accent); margin-top: 8px; width: 100%; padding: 8px; }
.error { color: var(--red); font-size: 15px; margin: -4px 4px 12px; }

#toast {
  position: fixed; left: 50%; top: calc(var(--safe-top) + 12px); z-index: 100;
  transform: translate(-50%, -150%); transition: transform .25s;
  background: rgba(30, 30, 30, .92); color: #fff; padding: 10px 16px; border-radius: 20px;
  font-size: 15px; max-width: 90vw; text-align: center; pointer-events: none;
}
#toast.show { transform: translate(-50%, 0); }
#toast:empty { visibility: hidden; }
