:root {
  --bg: #f4f5f9;
  --card: #ffffff;
  --text: #21242e;
  --muted: #757a8c;
  --line: #e5e7ef;
  --accent: #1f8a70;
  --accent-soft: #e2f3ee;
  --green: #2e9e5b;
  --red: #d95d67;
  --blue: #4a7fd6;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(30, 35, 60, .08);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: var(--text); }
h1, h2, h3 { margin: 0 0 .5em; }
a { color: var(--accent); }

/* ---------- login ---------- */
.login-wrap {
  min-height: 100vh; min-height: 100dvh;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.login-card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 32px 28px; width: 100%; max-width: 380px; text-align: center;
}
.login-card .logo { font-size: 40px; margin-bottom: 8px; }
.login-card .logo-img { width: 64px; height: 64px; border-radius: 15px; margin-bottom: 10px; box-shadow: var(--shadow); }
.brand-logo { width: 24px; height: 24px; border-radius: 6px; vertical-align: -6px; margin-right: 2px; }
.login-card h1 { font-size: 22px; }
.login-card p.sub { color: var(--muted); margin: 0 0 20px; font-size: 14px; }

/* ---------- shell ---------- */
header.topbar {
  background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 40;
}
.topbar-inner {
  max-width: 1100px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 12px;
}
.topbar .brand { font-weight: 700; font-size: 17px; white-space: nowrap; }
.topbar .brand span { color: var(--accent); }
.topbar .spacer { flex: 1; }
.topbar .whoami { color: var(--muted); font-size: 13px; white-space: nowrap; }

nav.tabs {
  background: var(--card); border-bottom: 1px solid var(--line);
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  position: sticky; top: 49px; z-index: 39; scrollbar-width: none;
}
nav.tabs::-webkit-scrollbar { display: none; }
nav.tabs .tabs-inner { max-width: 1100px; margin: 0 auto; display: flex; padding: 0 8px; }
nav.tabs button {
  background: none; border: none; padding: 11px 13px; color: var(--muted);
  font-size: 14px; font-weight: 600; white-space: nowrap;
  border-bottom: 2.5px solid transparent;
}
nav.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

main { max-width: 1100px; margin: 0 auto; padding: 18px 16px 80px; }

/* ---------- cards & grids ---------- */
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px; margin-bottom: 16px;
}
.card h2 { font-size: 16px; margin-bottom: 12px; }
.grid { display: grid; gap: 14px; }
.grid > * { min-width: 0; } /* let cards shrink below their content's natural width */
.grid.kpi { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.grid.two { grid-template-columns: 1fr 1fr; }
@media (max-width: 760px) { .grid.two { grid-template-columns: 1fr; } }

.kpi-card { padding: 14px 16px; }
.kpi-card .label { color: var(--muted); font-size: 12.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.kpi-card .value { font-size: 22px; font-weight: 700; margin-top: 4px; }
.kpi-card .value.pos { color: var(--green); }
.kpi-card .value.neg { color: var(--red); }
.kpi-card .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* ---------- forms ---------- */
.field { margin-bottom: 12px; text-align: left; }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px;
  background: #fbfbfd; outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); background: #fff; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  padding: 9px 16px; border-radius: 9px; border: 1px solid transparent;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 14px;
}
.btn:hover { filter: brightness(1.06); }
.btn.secondary { background: #fff; color: var(--text); border-color: var(--line); }
.btn.danger { background: #fff; color: var(--red); border-color: var(--line); }
.btn.small { padding: 5px 10px; font-size: 13px; border-radius: 7px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }

.seg { display: inline-flex; background: var(--bg); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button { border: none; background: none; padding: 7px 14px; border-radius: 7px; font-weight: 600; font-size: 13.5px; color: var(--muted); }
.seg button.active { background: #fff; color: var(--text); box-shadow: var(--shadow); }
.seg button.active.income { color: var(--green); }
.seg button.active.expense { color: var(--red); }
.seg button.active.transfer { color: var(--blue); }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.toolbar input, .toolbar select {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px; background: #fff; font-size: 13.5px;
}
.toolbar .spacer { flex: 1; }

/* ---------- transaction list ---------- */
.tx-row {
  display: flex; align-items: center; gap: 12px; padding: 11px 4px;
  border-bottom: 1px solid var(--line); cursor: pointer;
}
.tx-row:last-child { border-bottom: none; }
.tx-row:hover { background: #fafbfd; }
.tx-cat-dot { width: 38px; height: 38px; border-radius: 11px; flex: none; display: flex; align-items: center; justify-content: center; font-size: 17px; color: #fff; font-weight: 700; }
.tx-main { flex: 1; min-width: 0; }
.tx-main .t1 { font-weight: 600; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-main .t2 { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-amt { text-align: right; font-weight: 700; font-size: 14.5px; white-space: nowrap; }
.tx-amt.income { color: var(--green); }
.tx-amt.expense { color: var(--red); }
.tx-amt.transfer { color: var(--blue); }
.tx-amt .sub { display: block; color: var(--muted); font-weight: 400; font-size: 11.5px; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 20px; font-size: 11px; font-weight: 600; background: var(--accent-soft); color: var(--accent); }
.badge.gray { background: #eef0f5; color: var(--muted); }

/* ---------- account cards ---------- */
.acc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.acc-card { position: relative; }
.acc-card .acc-type { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; font-weight: 600; }
.acc-card .acc-name { font-weight: 700; font-size: 16px; margin: 2px 0 8px; }
.acc-card .acc-bal { font-size: 19px; font-weight: 700; }
.acc-card .acc-bal.neg { color: var(--red); }
.acc-card .acc-bal-extra { color: var(--muted); font-size: 13.5px; font-weight: 600; margin-top: 2px; }
.acc-card .acc-actions { position: absolute; top: 12px; right: 12px; display: flex; gap: 6px; }

/* ---------- progress bars ---------- */
.bar-outer { background: #edeff5; border-radius: 20px; height: 9px; overflow: hidden; margin-top: 6px; }
.bar-inner { height: 100%; border-radius: 20px; background: var(--accent); transition: width .3s; }
.bar-inner.over { background: var(--red); }
.budget-row { padding: 10px 0; border-bottom: 1px solid var(--line); }
.budget-row:last-child { border-bottom: none; }
.budget-row .head { display: flex; justify-content: space-between; font-size: 14px; }
.budget-row .head .amt { color: var(--muted); font-size: 13px; }

/* ---------- charts ---------- */
.hbar-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 13.5px; }
.hbar-row .lbl { width: 120px; flex: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-row .bar { flex: 1; background: #edeff5; border-radius: 6px; height: 20px; overflow: hidden; }
.hbar-row .bar > div { height: 100%; border-radius: 6px; }
.hbar-row .val { width: 90px; flex: none; text-align: right; font-weight: 600; font-size: 12.5px; }

.trend { display: flex; align-items: flex-end; gap: 6px; height: 140px; padding-top: 10px; }
.trend .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 2px; height: 100%; }
.trend .col .bars { display: flex; gap: 2px; align-items: flex-end; height: 100%; width: 100%; justify-content: center; }
.trend .col .bi { width: 42%; max-width: 16px; background: var(--green); border-radius: 3px 3px 0 0; }
.trend .col .be { width: 42%; max-width: 16px; background: var(--red); border-radius: 3px 3px 0 0; }
.trend .col .m { font-size: 10px; color: var(--muted); }

/* ---------- tables ---------- */
table.plain { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.plain th { text-align: left; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; padding: 8px 8px; border-bottom: 1px solid var(--line); }
table.plain td { padding: 9px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.plain tr:last-child td { border-bottom: none; }
.table-scroll { overflow-x: auto; }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(25, 28, 40, .45); z-index: 100;
  display: flex; align-items: flex-start; justify-content: center; padding: 24px 12px; overflow-y: auto;
}
.modal {
  background: var(--card); border-radius: var(--radius); padding: 22px; width: 100%; max-width: 480px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .2); margin-bottom: 40px;
}
.modal h2 { font-size: 17px; margin-bottom: 16px; }
.modal .modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
.modal.wide { max-width: 760px; }

/* ---------- attachments ---------- */
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.thumbs .thumb { position: relative; width: 74px; height: 74px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.thumbs .thumb img { width: 100%; height: 100%; object-fit: cover; cursor: pointer; }
.thumbs .thumb .x {
  position: absolute; top: 2px; right: 2px; background: rgba(0,0,0,.55); color: #fff; border: none;
  width: 20px; height: 20px; border-radius: 50%; font-size: 11px; line-height: 1; padding: 0;
}

/* ---------- sms preview ---------- */
.sms-item { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 10px; }
.sms-item .raw { font-size: 12px; color: var(--muted); background: #f7f8fb; border-radius: 8px; padding: 8px 10px; margin-bottom: 10px; white-space: pre-wrap; word-break: break-word; }
.sms-item.skip { opacity: .45; }
.sms-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; }
.sms-grid .field { margin-bottom: 0; }

/* ---------- misc ---------- */
.empty { text-align: center; color: var(--muted); padding: 30px 10px; font-size: 14px; }
.error-msg { color: var(--red); font-size: 13.5px; margin: 8px 0; min-height: 1em; }
.muted { color: var(--muted); }
.right { text-align: right; }
.mt { margin-top: 14px; }
.pos { color: var(--green); } .neg { color: var(--red); }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.section-head h2 { margin: 0; font-size: 17px; }
.pill-list { display: flex; flex-wrap: wrap; gap: 6px; }
.pill-list .pill { background: #eef0f5; border-radius: 20px; padding: 4px 11px; font-size: 12.5px; display: inline-flex; gap: 6px; align-items: center; }
.pill-list .pill button { border: none; background: none; color: var(--muted); padding: 0; font-size: 12px; }
.lightbox { position: fixed; inset: 0; background: rgba(10,12,20,.85); z-index: 200; display: flex; align-items: center; justify-content: center; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 92vh; border-radius: 8px; }

.fab {
  position: fixed; right: 20px; bottom: 20px; z-index: 50;
  width: 56px; height: 56px; border-radius: 50%; border: none;
  background: var(--accent); color: #fff; font-size: 26px;
  box-shadow: 0 4px 14px rgba(31, 138, 112, .45);
}
@media (min-width: 900px) { .fab { right: calc(50vw - 550px + 20px); } }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 90px; z-index: 300;
  background: #23262f; color: #fff; padding: 10px 18px; border-radius: 10px; font-size: 14px;
  box-shadow: 0 4px 20px rgba(0,0,0,.3); animation: fadein .2s;
}
@keyframes fadein { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- bottom navigation (phones) ---------- */
.bottomnav { display: none; }
.sheet-list button {
  display: flex; width: 100%; gap: 12px; align-items: center; text-align: left;
  padding: 13px 12px; border: none; background: none; font-size: 15px; font-weight: 600;
  color: var(--text); border-radius: 10px;
}
.sheet-list button:active, .sheet-list button:hover { background: var(--bg); }
.sheet-list button .ic { font-size: 19px; width: 26px; text-align: center; }
.sheet-list button.active { color: var(--accent); background: var(--accent-soft); }

/* ---------- phone layout (iPhone & similar) ---------- */
@media (max-width: 760px) {
  /* 16px inputs stop iOS Safari from zooming in when a field is focused */
  input, select, textarea { font-size: 16px; }

  header.topbar { padding-top: env(safe-area-inset-top); }
  .topbar-inner { padding: 8px 14px; }
  .topbar .whoami { overflow: hidden; text-overflow: ellipsis; max-width: 120px; }
  .topbar .signout-btn { display: none; } /* sign out lives in the More sheet */

  nav.tabs { display: none; } /* replaced by the bottom nav */
  main { padding: 14px 12px calc(86px + env(safe-area-inset-bottom)); }
  .card { padding: 15px 14px; }

  .bottomnav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
    background: var(--card); border-top: 1px solid var(--line);
    padding: 5px 2px calc(5px + env(safe-area-inset-bottom));
  }
  .bottomnav button {
    flex: 1; background: none; border: none; color: var(--muted);
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    font-size: 10.5px; font-weight: 600; padding: 4px 0 2px; min-width: 0;
  }
  .bottomnav button .ic { font-size: 21px; line-height: 1.15; }
  .bottomnav button.active { color: var(--accent); }

  .fab { right: 14px; bottom: calc(72px + env(safe-area-inset-bottom)); width: 52px; height: 52px; }
  .toast { bottom: calc(130px + env(safe-area-inset-bottom)); }

  /* modals become bottom sheets, like native iOS */
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal {
    max-width: none; margin-bottom: 0; border-radius: 18px 18px 0 0;
    padding: 18px 16px calc(20px + env(safe-area-inset-bottom));
    max-height: 92vh; max-height: 92dvh; overflow-y: auto;
  }
  .modal.wide { max-width: none; }
}
