:root {
  --bg: #ffffff;
  --bg-alt: #f8f9fa;
  --line: #e0e0e0;
  --line-strong: #bdc1c6;
  --text: #202124;
  --text-dim: #5f6368;
  --accent: #1a73e8;
  --accent-bg: #e8f0fe;
  --pos: #188038;
  --neg: #d93025;
  --today: #fdecc8;
  --today-line: #f9ab00;
  --past: #f1f3f4;
  --past-strong: #e4e6e9;
  --past-text: #80868b;
  --fill-soon: #fff3c4;
  --fill-missed: #fce4e4;
  --payday: #e6f4ea;
  --payday-strong: #d6ecdd;
  --manual: #1a73e8;
  --header-h: 48px;
  --nav-h: 56px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}
html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}
body {
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
input, textarea, [contenteditable] {
  user-select: text;
  -webkit-user-select: text;
}
button { font-family: inherit; }
::-webkit-scrollbar { width: 0; height: 0; display: none; }
* { scrollbar-width: none; }

/* ---------- каркас ---------- */
#app { display: flex; flex-direction: column; height: 100%; }

.appbar {
  min-height: var(--header-h);
  padding-top: env(safe-area-inset-top);
  display: flex;
  align-items: center;
  gap: 8px;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.appbar .title { font-weight: 600; font-size: 17px; letter-spacing: -0.2px; }
.appbar .spacer { flex: 1; }
.appbar-btn {
  border: none;
  background: none;
  color: var(--accent);
  font: inherit;
  font-weight: 500;
  height: 36px;
  padding: 0 6px;
  cursor: pointer;
}
.appbar-btn:active { opacity: .5; }

/* ---------- нижня навігація ---------- */
.bottomnav {
  display: flex;
  min-height: var(--nav-h);
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.bottomnav button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 10px;
  padding: 6px 0 4px;
  cursor: pointer;
}
.bottomnav button .ico { font-size: 20px; line-height: 1; filter: grayscale(1); opacity: .7; }
.bottomnav button.active { color: var(--accent); }
.bottomnav button.active .ico { filter: none; opacity: 1; }
.bottomnav button:active { background: var(--bg-alt); }

/* ---------- зведення ---------- */
.summary {
  display: flex;
  gap: 8px;
  padding: 8px max(12px, env(safe-area-inset-left)) 8px max(12px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  background: var(--bg-alt);
  -webkit-overflow-scrolling: touch;
}
.summary .card {
  flex: 0 0 auto;
  min-width: 120px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
}
.summary .card .label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .3px; }
.summary .card .val { font-size: 17px; font-weight: 500; margin-top: 2px; }
.summary .card.chip .val { color: var(--pos); }
.val.pos { color: var(--pos); }
.val.neg { color: var(--neg); }

main { flex: 1; overflow: hidden; position: relative; }

/* ---------- сітка ---------- */
.grid-wrap {
  position: absolute; inset: 0; overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  will-change: scroll-position;
}
table.grid {
  border-collapse: separate;
  border-spacing: 0;
  font-variant-numeric: tabular-nums;
  table-layout: fixed;      /* не міряти 5000 комірок — швидка розкладка */
  width: max-content;
  contain: content;
}
table.grid col { width: 74px; }
table.grid col.c-date { width: 84px; }
table.grid col.c-wd { width: 32px; }
table.grid col.cc-bal_plan, table.grid col.cc-bal_actual,
table.grid col.cc-saved, table.grid col.cc-expenses_total { width: 88px; }
table.grid col.cc-advance, table.grid col.cc-salary { width: 84px; }
table.grid td.date, table.grid th.date { font-size: 12.5px; padding: 0 6px; }
table.grid td.wd, table.grid th.wd { padding: 0 4px; }
/* не рендерити рядки поза екраном — прокрутка довгої таблиці на телефоні */
table.grid tbody tr {
  content-visibility: auto;
  contain-intrinsic-size: auto 31px;
}
table.grid th, table.grid td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0 8px;
  height: 30px;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
table.grid thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--bg-alt);
  color: var(--text-dim);
  font-weight: 500;
  font-size: 12px;
  border-bottom: 1px solid var(--line-strong);
}
table.grid td.date, table.grid td.wd,
table.grid th.date, table.grid th.wd {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--bg);
  text-align: left;
}
table.grid td.wd, table.grid th.wd { left: 92px; color: var(--text-dim); }
table.grid thead th.date, table.grid thead th.wd { z-index: 4; background: var(--bg-alt); }

/* минулі дні — приглушені сірим */
table.grid tr.past td { background: var(--past); color: var(--past-text); }
table.grid tr.past td.date, table.grid tr.past td.wd { background: var(--past-strong); }
table.grid tr.past td.col-bal { background: #eceef0; }
table.grid tr.past td.neg { color: var(--neg); }
table.grid tr.past td.manual { color: #5f6368; }

/* день зарплати / авансу — зеленим */
table.grid tr.payday td { background: var(--payday); color: var(--text); }
table.grid tr.payday td.date, table.grid tr.payday td.wd { background: var(--payday-strong); }
table.grid tr.payday td.date { box-shadow: inset 3px 0 0 var(--pos); font-weight: 600; }
table.grid tr.payday td.col-bal { background: #dcefe0; }
table.grid td.cell.income-hit { color: var(--pos); font-weight: 600; }

/* сьогодні — жовтим (перекриває зелений, якщо це той самий день) */
table.grid tr.today td { background: var(--today); color: var(--text); }
table.grid tr.today td.date {
  background: var(--today);
  box-shadow: inset 3px 0 0 var(--today-line);
  font-weight: 600;
}
table.grid tr.today td.wd { background: var(--today); }

/* «витратив за сьогодні»: підказка вписати / позначка що забув */
table.grid td.col-spent_actual.fill-soon { background: var(--fill-soon); }
table.grid tr.today td.col-spent_actual.fill-soon { background: #ffe89e; }
table.grid td.col-spent_actual.fill-missed { background: var(--fill-missed); }
table.grid thead th.col-spent_actual { color: #b06000; }
table.grid td.cell.planned { background: var(--fill-soon); color: #b06000; }
table.grid tr.past td.cell.planned { background: #f6e9cf; }

table.grid tr.month-sep td {
  background: var(--bg-alt);
  font-weight: 500;
  color: var(--text-dim);
  text-align: left;
  height: 26px;
  position: sticky;
  left: 0;
}
table.grid tr.weekend td.date, table.grid tr.weekend td.wd { color: var(--neg); }

td.cell { cursor: cell; position: relative; }
td.cell.zero { color: #c5c8cc; }
td.cell.manual { font-weight: 600; color: var(--text); }
td.cell.manual::after {
  content: "";
  position: absolute;
  top: 0; right: 0;
  border: 5px solid transparent;
  border-top-color: var(--manual);
  border-right-color: var(--manual);
}
td.cell.neg { color: var(--neg); }
td.cell.pos { color: var(--pos); }
td.cell.col-saved_day { background: #fafafd; }
td.cell.col-bal { background: #fafafd; }
td.cell.col-usd.has { color: var(--pos); font-weight: 600; }
td.cell.editing { padding: 0; }
td.cell input {
  width: 100%;
  height: 100%;
  border: 2px solid var(--accent);
  outline: none;
  font: inherit;
  text-align: right;
  padding: 0 6px;
  background: #fff;
}

/* ---------- картка дня (мобільний) ---------- */
.daycard-wrap {
  position: absolute; inset: 0; overflow: auto;
  padding: 12px max(12px, env(safe-area-inset-right)) 16px max(12px, env(safe-area-inset-left));
  -webkit-overflow-scrolling: touch;
}
.daynav {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 12px;
}
.daynav button {
  width: 40px; height: 40px;
  border: 1px solid var(--line-strong); background: var(--bg);
  border-radius: 8px; font-size: 18px; cursor: pointer;
}
.daynav input[type=date] {
  flex: 1; height: 40px; border: 1px solid var(--line-strong);
  border-radius: 8px; padding: 0 10px; font: inherit;
}
.daynav .wd { color: var(--text-dim); min-width: 28px; text-align: center; }

.drow {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 6px;
  border-bottom: 1px solid var(--line);
  gap: 12px;
}
.drow .k { color: var(--text-dim); }
.drow .v { font-weight: 500; font-variant-numeric: tabular-nums; text-align: right; }
.drow .v .auto-hint { display: block; font-size: 11px; color: #9aa0a6; font-weight: 400; }
.drow.manual .v { color: var(--accent); }
.drow input {
  width: 130px; height: 38px; font: inherit; text-align: right;
  border: 2px solid var(--accent); border-radius: 6px; padding: 0 8px;
}
.drow.total { border-top: 2px solid var(--line-strong); font-weight: 500; }
.drow.spent { background: var(--today); border-radius: 8px; margin: 8px 0; padding: 14px 10px; }
.drow.spent .k { font-weight: 500; color: var(--text); }
.drow.spent.missed { background: var(--fill-missed); }
.drow.spent .auto-hint { color: #b06000; }
.drow.readonly .v { color: var(--text); }
.drow .v.pos { color: var(--pos); }
.drow .v.neg { color: var(--neg); }
.daycard .note {
  width: 100%; margin-top: 12px; min-height: 54px; font: inherit;
  border: 1px solid var(--line-strong); border-radius: 8px; padding: 8px;
}

/* ---------- калькулятор у комірці ---------- */
.calcbar {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 0 10px;
  border-bottom: 1px solid var(--line);
}
.calcbar .calc-preview {
  min-width: 60px; font-weight: 600; color: var(--pos);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.calcbar .calc-keys { display: flex; gap: 6px; flex: 1; justify-content: flex-end; flex-wrap: wrap; }
.calcbar button {
  min-width: 38px; height: 38px; padding: 0 8px;
  border: 1px solid var(--line-strong); background: var(--bg);
  border-radius: 8px; font-size: 16px; cursor: pointer;
}
.calcbar button:active { background: var(--bg-alt); }
.calcbar button.ok {
  background: var(--accent); color: #fff; border-color: var(--accent);
  font-size: 14px; font-weight: 500; padding: 0 12px;
}

/* ---------- панелі ---------- */
.drawer-bg {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: none; z-index: 20;
}
.drawer-bg.open { display: block; }
.drawer {
  position: absolute; top: 0; right: 0; height: 100%;
  width: min(420px, 92vw);
  background: var(--bg); box-shadow: -4px 0 16px rgba(0,0,0,.2);
  padding: 16px; overflow-y: auto;
}
/* на телефоні — нижній лист (bottom sheet) */
@media (max-width: 600px) {
  .drawer {
    top: auto; bottom: 0; left: 0; right: 0;
    width: 100%; height: auto; max-height: 88vh;
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -4px 20px rgba(0,0,0,.25);
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    animation: sheet-up .22s ease;
  }
  .drawer::before {
    content: ""; display: block; width: 36px; height: 4px; border-radius: 2px;
    background: var(--line-strong); margin: -4px auto 12px;
  }
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
.drawer h2 { margin: 0 0 12px; font-size: 16px; font-weight: 500; }
.drawer .acct, .drawer .setting {
  border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 10px;
}
.drawer label { display: block; font-size: 12px; color: var(--text-dim); margin: 6px 0 2px; }
.drawer input, .drawer select {
  width: 100%; height: 34px; border: 1px solid var(--line-strong);
  border-radius: 6px; padding: 0 8px; font: inherit;
}
.drawer .row2 { display: flex; gap: 8px; }
.drawer .row2 > * { flex: 1; }
.drawer button.primary {
  background: var(--accent); color: #fff; border: none; border-radius: 6px;
  height: 38px; padding: 0 16px; cursor: pointer; font: inherit;
}
.drawer button.ghost {
  background: none; border: none; color: var(--neg); cursor: pointer; font: inherit; padding: 6px 0;
}
.drawer hr { border: none; border-top: 1px solid var(--line); margin: 16px 0; }
.drawer h3.set-h { margin: 0 0 4px; font-size: 13px; font-weight: 500; }
.drawer #pw-save, .drawer #set-save { margin-top: 10px; }
.drawer #pw-msg { margin-top: 8px; }
.drawer .hint { font-size: 12.5px; color: var(--text-dim); margin: -2px 0 14px; line-height: 1.45; }
.drawer .hint b { color: var(--text); }
.drawer a { color: var(--accent); }

/* картки категорій */
.cat-card { border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; overflow: hidden; }
.cat-card.archived { opacity: .55; }
.cat-head { padding: 10px 12px; cursor: pointer; }
.cat-head:hover { background: var(--bg-alt); }
.cat-title { font-weight: 500; display: block; }
.cat-meta { font-size: 12px; color: var(--text-dim); }
.cat-body { padding: 4px 12px 12px; border-top: 1px solid var(--line); }
.cat-body .rule-fields { margin-top: 4px; }
.cat-actions { display: flex; gap: 12px; align-items: center; margin-top: 12px; }
.cat-actions .primary { height: 34px; }
.wd-row { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.wd-box {
  display: flex; align-items: center; gap: 4px; border: 1px solid var(--line-strong);
  border-radius: 6px; padding: 4px 8px; font-size: 13px; cursor: pointer;
}
.wd-box input { width: auto; height: auto; }
.drawer label.chk {
  display: flex; align-items: flex-start; gap: 6px; font-size: 12.5px;
  color: var(--text); margin-top: 4px; line-height: 1.3; cursor: pointer;
}
.drawer label.chk input { width: auto; height: auto; margin-top: 1px; }

/* картка дня — підзаголовок */
.dsub {
  margin: 14px 0 2px; font-size: 12px; text-transform: uppercase;
  letter-spacing: .4px; color: var(--text-dim);
}
.drow.planned .v { color: #b06000; }
.drow.planned { background: var(--fill-soon); border-radius: 8px; }
.drow.income-hit { background: var(--payday); border-radius: 8px; }
.drow.income-hit .k, .drow.income-hit .v { color: var(--pos); font-weight: 600; }

/* ---------- контекстне меню ---------- */
.ctx {
  position: fixed; z-index: 30; background: #fff;
  border: 1px solid var(--line-strong); border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,.18); padding: 4px; min-width: 180px;
}
.ctx button {
  display: block; width: 100%; text-align: left; border: none; background: none;
  padding: 8px 12px; border-radius: 6px; cursor: pointer; font: inherit;
}
.ctx button:hover { background: var(--bg-alt); }

/* ---------- тост / статус ---------- */
.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: #202124; color: #fff; padding: 10px 16px; border-radius: 8px;
  z-index: 40; font-size: 13px; opacity: 0; transition: opacity .2s;
  pointer-events: none; max-width: 90vw;
}
.toast.show { opacity: 1; }
.offline-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--neg);
  display: none;
}
body.offline .offline-dot { display: inline-block; }

/* ---------- login ---------- */
.login-page {
  height: 100%; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  background: var(--bg);
  padding: 24px calc(24px + env(safe-area-inset-left)) calc(24px + env(safe-area-inset-bottom));
}
.login-box {
  width: min(360px, 100%); text-align: center;
  display: flex; flex-direction: column;
}
.login-logo { width: 64px; height: 64px; margin: 0 auto 16px; border-radius: 16px; }
.login-box h1 { font-size: 24px; font-weight: 600; letter-spacing: -0.3px; margin: 0 0 28px; }
.login-box input {
  width: 100%; height: 48px; border: 1px solid var(--line-strong);
  border-radius: 12px; padding: 0 14px; font-size: 16px; margin-bottom: 12px;
  background: var(--bg-alt);
}
.login-box input:focus { outline: none; border-color: var(--accent); background: var(--bg); }
.login-box button {
  width: 100%; height: 48px; background: var(--accent); color: #fff;
  border: none; border-radius: 12px; font-size: 16px; font-weight: 500; cursor: pointer;
}
.login-box button:active { opacity: .8; }
.login-box .err { color: var(--neg); font-size: 13px; min-height: 18px; margin-bottom: 6px; }

@media (max-width: 767px) {
  .summary .card { min-width: 108px; }
}
