:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #15191e;
  --muted: #6b7480;
  --border: #e2e6ea;
  --accent: #0f9d76;
  --accent-soft: #dff4ed;
  --on-accent: #ffffff;
  --expense: #d9473c;
  --income: #0f9d76;
  --transfer: #5b6bd6;
  --warn: #c98a00;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .05);
  --radius: 16px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0f12;
    --surface: #171a1f;
    --surface-2: #20242a;
    --text: #eef0f3;
    --muted: #8d96a2;
    --border: #2a2f36;
    --accent: #2fc497;
    --accent-soft: #143a2f;
    --on-accent: #06221a;
    --expense: #ff6b5e;
    --income: #2fc497;
    --transfer: #8b98ff;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
  min-height: 100dvh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
svg.i { width: 20px; height: 20px; flex: none; }
.row.small svg.i { width: 16px; height: 16px; }

/* ---------- Каркас ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  min-height: calc(52px + var(--safe-t));
  padding: var(--safe-t) 12px 0;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
}
.topbar h1 { flex: 1; margin: 0; font-size: 20px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#top-action { display: flex; gap: 4px; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border: 0; border-radius: 12px;
  background: transparent; color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg, .ico svg { width: 22px; height: 22px; }

.view {
  max-width: 640px; margin: 0 auto;
  padding: 8px 16px calc(var(--tabbar-h) + var(--safe-b) + 24px);
}
.view.no-tabs { padding-bottom: calc(var(--safe-b) + 24px); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
}
.tab {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  border: 0; background: none; color: var(--muted); font-size: 11px; padding: 6px 0;
}
.tab svg { width: 24px; height: 24px; }
.tab.active { color: var(--accent); }
.tab-add {
  justify-self: center;
  width: 54px; height: 54px; border-radius: 18px; border: 0;
  background: var(--accent); color: var(--on-accent);
  display: grid; place-items: center;
  box-shadow: 0 6px 16px color-mix(in srgb, var(--accent) 40%, transparent);
  transform: translateY(-8px);
}
.tab-add svg { width: 28px; height: 28px; }

/* ---------- Карточки, списки ---------- */
.card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px;
}
.card.hero {
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 60%, #1b4bd8));
  color: #fff;
}
.card.hero .muted { color: rgba(255, 255, 255, .8); }
.section-title {
  display: flex; justify-content: space-between; align-items: baseline;
  margin: 20px 4px 8px; font-size: 13px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted);
}
.section-title a, .link { color: var(--accent); text-decoration: none; text-transform: none; letter-spacing: 0; font-weight: 600; background: none; border: 0; padding: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.big-num { font-size: 30px; font-weight: 750; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.row { display: flex; align-items: center; gap: 12px; }
.grow { flex: 1; min-width: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.expense { color: var(--expense); }
.income { color: var(--income); }
.transfer { color: var(--transfer); }

.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 12px; }
.item {
  display: flex; align-items: center; gap: 12px;
  width: 100%; padding: 12px 16px; border: 0; background: none; text-align: left;
  border-bottom: 1px solid var(--border); color: inherit; text-decoration: none;
}
.item:last-child { border-bottom: 0; }
.item:active { background: var(--surface-2); }
.item .title { font-weight: 600; }
.item .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .chev { color: var(--muted); }
.item .chev svg { width: 18px; height: 18px; }
.avatar {
  flex: none; display: grid; place-items: center;
  width: 40px; height: 40px; border-radius: 12px;
  background: var(--surface-2); color: var(--muted); font-weight: 700;
}
.avatar svg { width: 20px; height: 20px; }
.avatar.expense { background: color-mix(in srgb, var(--expense) 14%, transparent); color: var(--expense); }
.avatar.income { background: color-mix(in srgb, var(--income) 14%, transparent); color: var(--income); }
.avatar.transfer { background: color-mix(in srgb, var(--transfer) 14%, transparent); color: var(--transfer); }
.day-head { display: flex; justify-content: space-between; margin: 16px 4px 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; background: var(--surface-2); color: var(--muted); }
.badge.ok { background: var(--accent-soft); color: var(--accent); }
.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.empty svg { width: 40px; height: 40px; opacity: .5; }

.bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; border-radius: 99px; background: var(--accent); }
.bar.expense > i { background: var(--expense); }

.chart { display: flex; align-items: flex-end; gap: 6px; height: 140px; padding-top: 8px; }
.chart .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; }
.chart .bars { display: flex; gap: 2px; align-items: flex-end; height: 100%; width: 100%; justify-content: center; }
.chart .bars i { width: 40%; max-width: 12px; border-radius: 4px 4px 0 0; min-height: 2px; }
.chart .bars i.inc { background: var(--income); }
.chart .bars i.exp { background: var(--expense); }
.chart .lbl { font-size: 10px; color: var(--muted); }

/* ---------- Формы ---------- */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin: 0 4px 6px; font-weight: 500; }
.input, select.input, textarea.input {
  width: 100%; min-height: 48px; padding: 12px 14px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); outline: none; appearance: none; -webkit-appearance: none;
}
select.input {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 20px) 50%, calc(100% - 15px) 50%;
  background-size: 5px 5px; background-repeat: no-repeat; padding-right: 36px;
}
textarea.input { min-height: 80px; resize: vertical; }
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.amount-input {
  width: 100%; border: 0; background: none; outline: none; text-align: center;
  font-size: 44px; font-weight: 750; letter-spacing: -.02em; padding: 8px 0; font-variant-numeric: tabular-nums;
}
.amount-wrap { text-align: center; padding: 8px 0 4px; }
.amount-wrap .cur { color: var(--muted); font-size: 15px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 50px; padding: 0 20px; border: 0; border-radius: 14px;
  background: var(--accent); color: var(--on-accent); font-weight: 650; width: 100%;
}
.btn:disabled { opacity: .5; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.danger { background: color-mix(in srgb, var(--expense) 12%, transparent); color: var(--expense); }
.btn + .btn { margin-top: 10px; }
.btn.small-btn { min-height: 38px; width: auto; padding: 0 14px; border-radius: 10px; font-size: 14px; }
.sticky-actions {
  position: sticky; bottom: 0; padding: 12px 0 calc(12px + var(--safe-b));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); padding: 4px; border-radius: 12px; gap: 4px; margin-bottom: 12px; }
.segmented button { border: 0; background: none; padding: 9px 4px; border-radius: 9px; font-weight: 600; color: var(--muted); font-size: 14px; }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented button.active.expense { color: var(--expense); }
.segmented button.active.income { color: var(--income); }
.segmented button.active.transfer { color: var(--transfer); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 6px; scrollbar-width: none; margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.chips::-webkit-scrollbar { display: none; }
.chips.wrap { flex-wrap: wrap; overflow: visible; margin: 0; padding: 2px 0 6px; }
.chip {
  flex: none; border: 1px solid var(--border); background: var(--surface);
  padding: 8px 14px; border-radius: 99px; font-size: 14px; white-space: nowrap;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
.acc-chip { display: flex; flex-direction: column; align-items: flex-start; border-radius: 14px; padding: 10px 14px; min-width: 120px; text-align: left; }
.acc-chip .sub { font-size: 12px; opacity: .75; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }

.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 4px; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch i { position: absolute; inset: 0; background: var(--surface-2); border-radius: 99px; transition: .2s; border: 1px solid var(--border); }
.switch i::after { content: ""; position: absolute; width: 24px; height: 24px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + i { background: var(--accent); border-color: var(--accent); }
.switch input:checked + i::after { transform: translateX(20px); }

.swatches { display: flex; gap: 10px; flex-wrap: wrap; }
.swatch { width: 34px; height: 34px; border-radius: 50%; border: 3px solid transparent; }
.swatch.active { border-color: var(--text); }

/* ---------- Нижняя шторка ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .4);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .2s ease;
}
.sheet {
  width: 100%; max-width: 640px; max-height: 92dvh; overflow-y: auto;
  background: var(--bg); border-radius: 22px 22px 0 0;
  padding: 8px 16px calc(16px + var(--safe-b));
  animation: up .25s cubic-bezier(.2, .8, .2, 1);
}
.sheet-handle { width: 40px; height: 5px; border-radius: 99px; background: var(--border); margin: 4px auto 10px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet-head h2 { margin: 0; font-size: 19px; }
@keyframes up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 20px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px;
  font-size: 14px; font-weight: 500; transition: .25s; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--expense); color: #fff; }

/* ---------- Вход ---------- */
.auth {
  min-height: 100dvh; display: flex; flex-direction: column; justify-content: center;
  padding: calc(24px + var(--safe-t)) 8px 24px; max-width: 420px; margin: 0 auto;
}
.auth .logo { width: 72px; height: 72px; border-radius: 20px; margin: 0 auto 16px; display: block; }
.auth h1 { text-align: center; margin: 0 0 4px; font-size: 26px; }
.auth p.muted { text-align: center; margin: 0 0 24px; }
.error-box { background: color-mix(in srgb, var(--expense) 12%, transparent); color: var(--expense); padding: 10px 14px; border-radius: 12px; margin-bottom: 14px; font-size: 14px; }

.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 48px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 700px) {
  .view { padding-top: 16px; }
}
