:root {
  --bg: #F3F5FA; --surface: #FFFFFF; --surface2: #F0F2F8; --border: #E2E6EF;
  --text: #18202F; --muted: #677086; --accent: #4F5FE8; --accent-soft: #E8EBFF;
  --positive: #13A35A; --negative: #E0414B; --warning: #C97F05;
  --positive-soft: #E3F6EC; --negative-soft: #FDE8EA; --warning-soft: #FFF3DC;
  --shadow: 0 4px 20px rgba(16, 24, 40, .06);
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0E1117; --surface: #161A22; --surface2: #1D222C; --border: #272D39;
    --text: #E7EAF1; --muted: #8C95A8; --accent: #7D8CFF; --accent-soft: #232849;
    --positive: #3DD68C; --negative: #FF6B73; --warning: #F5B544;
    --positive-soft: #15301F; --negative-soft: #3A1C20; --warning-soft: #3A2E14;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0E1117; --surface: #161A22; --surface2: #1D222C; --border: #272D39;
  --text: #E7EAF1; --muted: #8C95A8; --accent: #7D8CFF; --accent-soft: #232849;
  --positive: #3DD68C; --negative: #FF6B73; --warning: #F5B544;
  --positive-soft: #15301F; --negative-soft: #3A1C20; --warning-soft: #3A2E14;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.pos { color: var(--positive); } .neg { color: var(--negative); } .warn { color: var(--warning); }
.muted { color: var(--muted); } .small { font-size: 13px; } .tiny { font-size: 12px; }
.accent { color: var(--accent); } .bold { font-weight: 650; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- layout ---------- */
#app { min-height: 100dvh; padding: calc(var(--safe-top) + 8px) 16px calc(var(--safe-bottom) + 92px); max-width: 640px; margin: 0 auto; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 8px 0 12px; }
.topbar h1 { font-size: 30px; font-weight: 750; margin: 0; flex: 1; letter-spacing: -.02em; }
.iconbtn {
  width: 40px; height: 40px; border-radius: 20px; border: 1px solid var(--border); background: var(--surface);
  display: grid; place-items: center; font-size: 18px; position: relative; flex: none;
}
.iconbtn .dot { position: absolute; top: 6px; right: 6px; width: 9px; height: 9px; border-radius: 5px; background: var(--negative); border: 2px solid var(--surface); }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; justify-content: center;
  background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: saturate(1.6) blur(18px);
  -webkit-backdrop-filter: saturate(1.6) blur(18px); border-top: 1px solid var(--border);
  padding: 6px 4px calc(var(--safe-bottom) + 6px);
}
.tabs button {
  flex: 1; max-width: 120px; background: none; border: 0; padding: 4px 0; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; font-weight: 600;
}
.tabs button .ic { font-size: 21px; line-height: 26px; }
.tabs button.on { color: var(--accent); }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card h2 { font-size: 17px; margin: 0 0 10px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.card h2 .right { margin-left: auto; font-size: 13px; font-weight: 600; }
.hero { padding: 20px 18px; }
.hero .label { color: var(--muted); font-size: 13px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; }
.hero .label-row { display: flex; align-items: center; justify-content: space-between; }
.eye { border: 0; background: none; color: var(--muted); padding: 4px; margin: -4px -6px -4px 0; border-radius: 10px; display: grid; place-items: center; }
.eye:active { background: var(--surface2); }
.hint.warn { color: var(--warning); }
.hero .big { font-size: 42px; font-weight: 750; letter-spacing: -.02em; margin: 2px 0; }
.hero .actions { display: flex; gap: 10px; margin-top: 14px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px; box-shadow: var(--shadow); min-width: 0; }
.stat .t { font-size: 11px; color: var(--muted); font-weight: 650; text-transform: uppercase; letter-spacing: .03em; }
.stat .v { font-size: 18px; font-weight: 700; margin-top: 2px; }
.stat .s { font-size: 12px; color: var(--muted); }
.banner { border-radius: 14px; padding: 12px 14px; font-weight: 600; font-size: 14px; margin-bottom: 14px; }
.banner.negative { background: var(--negative-soft); color: var(--negative); }
.banner.warning { background: var(--warning-soft); color: var(--warning); }
.banner.positive { background: var(--positive-soft); color: var(--positive); }
.banner.info { background: var(--accent-soft); color: var(--accent); }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 12px; padding: 11px 16px;
  font-weight: 650; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--negative); }
.btn.block { width: 100%; }
.btn.grow { flex: 1; }
.btn:disabled { opacity: .5; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 16px; padding: 6px 12px; font-size: 14px; font-weight: 600; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.seg { display: flex; background: var(--surface2); border-radius: 11px; padding: 3px; gap: 2px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px 6px; border-radius: 9px; font-weight: 600; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.fab {
  position: fixed; right: max(16px, calc(50% - 304px)); bottom: calc(var(--safe-bottom) + 80px); z-index: 15;
  width: 56px; height: 56px; border-radius: 28px; border: 0; background: var(--accent); color: #fff; font-size: 28px;
  box-shadow: 0 8px 24px rgba(79, 95, 232, .35);
}

/* ---------- lists ---------- */
.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-bottom: 14px; box-shadow: var(--shadow); }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--border); min-height: 54px; }
.row:last-child { border-bottom: 0; }
.row.tap:active { background: var(--surface2); }
.row .main { flex: 1; min-width: 0; }
.row .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .end { text-align: right; }
.row .badge { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; font-size: 16px; flex: none; background: var(--surface2); }
.row .badge.in { background: var(--positive-soft); color: var(--positive); }
.row .badge.out { background: var(--surface2); }
.row .badge.ci { background: var(--accent-soft); color: var(--accent); }
.dayhead { padding: 14px 4px 6px; font-size: 13px; font-weight: 700; color: var(--muted); display: flex; justify-content: space-between; text-transform: uppercase; letter-spacing: .03em; }
.dayhead.today { color: var(--accent); }
.quiet { padding: 8px 14px; font-size: 14px; color: var(--muted); display: flex; justify-content: space-between; border-bottom: 1px solid var(--border); }
.empty { text-align: center; padding: 30px 20px; color: var(--muted); }
.kv { display: flex; justify-content: space-between; padding: 6px 0; }

/* ---------- chart ---------- */
.chart { position: relative; height: 190px; margin: 4px -4px 0; touch-action: pan-y; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .tip {
  position: absolute; top: 0; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px 10px; font-size: 13px; box-shadow: var(--shadow); white-space: nowrap; transform: translateX(-50%);
}
.bars { display: flex; align-items: flex-end; gap: 8px; height: 130px; padding-top: 18px; }
.bars .b { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; font-size: 11px; color: var(--muted); }
.bars .b i { display: block; width: 70%; border-radius: 6px 6px 3px 3px; min-height: 3px; }
.bars .b span { margin-top: 4px; }

/* ---------- sheets ---------- */
dialog.sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 640px; max-height: 92dvh;
  background: var(--bg); color: var(--text); border-radius: 22px 22px 0 0; overflow: hidden;
}
dialog.sheet::backdrop { background: rgba(5, 8, 15, .45); }
dialog.sheet[open] { animation: up .25s cubic-bezier(.2, .9, .3, 1); display: flex; flex-direction: column; }
@keyframes up { from { transform: translateY(40%); opacity: .4; } to { transform: none; opacity: 1; } }
.sheet header { display: flex; align-items: center; padding: 14px 16px 8px; gap: 8px; }
.sheet header h3 { flex: 1; margin: 0; font-size: 20px; font-weight: 750; }
.sheet header .x { border: 0; background: var(--surface2); width: 32px; height: 32px; border-radius: 16px; font-size: 16px; color: var(--muted); }
.sheet .body { overflow-y: auto; padding: 4px 16px calc(var(--safe-bottom) + 20px); -webkit-overflow-scrolling: touch; }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 8px auto 0; }

/* ---------- forms ---------- */
.form { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-bottom: 14px; }
.field { display: flex; align-items: center; gap: 12px; padding: 0 14px; min-height: 52px; border-bottom: 1px solid var(--border); }
.field:last-child { border-bottom: 0; }
.field > label { flex: none; width: 112px; color: var(--muted); font-size: 15px; }
.field input, .field select, .field textarea {
  flex: 1; min-width: 0; border: 0; background: none; padding: 14px 0; font-size: 17px; text-align: right; outline: none;
  -webkit-appearance: none; appearance: none;
}
.field select { direction: rtl; }
.field select option { direction: ltr; }
.field textarea { text-align: left; resize: none; min-height: 60px; }
.field input[type="date"] { min-height: 24px; }
.field.stack { flex-direction: column; align-items: stretch; gap: 8px; padding: 12px 14px; }
.field.stack > label { width: auto; }
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: var(--border); border-radius: 16px; transition: .2s; }
.switch span::after { content: ""; position: absolute; width: 27px; height: 27px; left: 2px; top: 2px; background: #fff; border-radius: 14px; transition: .2s; box-shadow: 0 2px 4px rgba(0,0,0,.2); }
.switch input:checked + span { background: var(--positive); }
.switch input:checked + span::after { transform: translateX(20px); }
.field > label.switch, .toggle-row > label.switch { flex: none; width: 51px; }
.toggle-row { justify-content: space-between; }
.toggle-row > label { width: auto; flex: 1; color: var(--text); }
.hint { font-size: 13px; color: var(--muted); padding: 0 4px 14px; }
.result { border-radius: var(--radius); padding: 16px; margin-bottom: 14px; background: var(--surface); border: 1px solid var(--border); }
.result .big { font-size: 24px; font-weight: 750; }
.steps { padding-left: 20px; margin: 6px 0 0; } .steps li { margin: 6px 0; }

/* ---------- app lock ---------- */
.lock { max-width: 340px; margin: 0 auto; padding-top: 12vh; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.lock img { border-radius: 18px; box-shadow: 0 10px 30px rgba(79, 95, 232, .3); }
.lock h1 { margin: 8px 0 0; font-size: 26px; }
.lock p { margin: 0 0 14px; }
.lock input { width: 100%; text-align: center; font-size: 22px; letter-spacing: .3em; padding: 12px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); outline: none; }
.lock input:focus { border-color: var(--accent); }
.lock .or { color: var(--muted); font-size: 13px; margin: 6px 0 2px; }
.linkbtn { background: none; border: 0; color: var(--accent); font-weight: 600; padding: 8px; }
body.cover #app, body.cover .tabs, body.cover dialog { filter: blur(24px); }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 84px); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 11px 16px; border-radius: 14px; font-weight: 600; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .25s; z-index: 50; max-width: calc(100% - 32px); display: flex; gap: 12px; align-items: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); pointer-events: auto; }
#toast button { background: none; border: 0; color: var(--accent); font-weight: 700; padding: 0; }

@media (min-width: 700px) { .stats { grid-template-columns: repeat(3, 1fr); } }
