:root {
  --bg: #f6f5f1; --panel: #ffffff; --panel2: #f0eee8; --text: #22262b; --muted: #6b7178; --line: #e3e0d8;
  --accent: #4f8a8b; --accent-ink: #fff; --accent-soft: #e2efee; --neg: #c2413b; --neg-soft: #fbe9e7; --pos: #2f7d4f;
  --hi: #2f7d4f; --lo: #b7791f; --today: #4f8a8b; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --r: 12px; --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #16191d; --panel: #1f2328; --panel2: #262b31; --text: #e7e9ec; --muted: #9aa1a9; --line: #30363d;
  --accent: #6fb3b4; --accent-ink: #0f1a1a; --accent-soft: #24393a; --neg: #ff7b72; --neg-soft: #3a2224; --pos: #6cc58f;
  --hi: #6cc58f; --lo: #e3b341; --today: #6fb3b4; --shadow: 0 1px 2px rgba(0,0,0,.3);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.4 var(--font); -webkit-text-size-adjust: 100%; }
body { padding-bottom: env(safe-area-inset-bottom); }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { font-size: 16px; } /* stop iOS zoom */
button { cursor: pointer; }
[hidden] { display: none !important; }
.num { font-variant-numeric: tabular-nums; }
.neg { color: var(--neg); } .pos { color: var(--pos); } .muted { color: var(--muted); }

/* top bar */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px; padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: .2px; }
.brand img { border-radius: 7px; }
.mainnav { display: flex; gap: 4px; margin-left: 16px; flex: 1; }
.mainnav button, .tabs button { border: 0; background: transparent; padding: 7px 12px; border-radius: 999px; color: var(--muted); }
.mainnav button.active { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.mainnav button:hover { color: var(--text); }
.iconbtn { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; width: 36px; height: 36px; margin-left: auto; }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--neg); color: #fff; font-size: 11px; line-height: 18px; text-align: center; }

/* account tabs */
.tabs { display: flex; gap: 6px; overflow-x: auto; padding: 10px 16px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button { flex: 0 0 auto; display: flex; flex-direction: column; align-items: flex-start; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px 12px; min-width: 110px; }
.tabs button .n { font-size: 12px; color: var(--muted); white-space: nowrap; }
.tabs button .b { font-weight: 600; font-size: 14px; }
.tabs button.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.tabs button .sw { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }

main { padding: 0 16px 24px; max-width: 1400px; margin: 0 auto; }
.view { display: none; } .view.active { display: block; }

/* calendar */
.calwrap { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.calnav { display: flex; align-items: center; gap: 6px; margin: 4px 0 10px; flex-wrap: wrap; }
.calnav h2 { margin: 0 6px; font-size: 20px; font-weight: 650; cursor: pointer; min-width: 170px; }
.calnav h2:hover { color: var(--accent); }
.btn { white-space: nowrap; border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: 7px 12px; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--neg); }
.btn.sm { padding: 4px 9px; font-size: 13px; }
.spacer { flex: 1; }
.monthpick { position: relative; }
.monthpick input { position: absolute; inset: 0; opacity: 0; width: 100%; }
.grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); background: var(--line); gap: 1px; border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; box-shadow: var(--shadow); }
.grid .dow { background: var(--panel2); padding: 6px 8px; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.day { background: var(--panel); min-height: 118px; padding: 6px 7px 24px; position: relative; cursor: pointer; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.day:hover { background: color-mix(in srgb, var(--panel) 85%, var(--accent-soft)); }
.day.other { background: var(--panel2); opacity: .55; }
.day .dn { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 4px; }
.day.today .dn span { background: var(--today); color: var(--accent-ink); border-radius: 999px; padding: 0 7px; }
.day .bm { color: #d4a017; font-size: 12px; }
.day .it { font-size: 11.5px; display: flex; justify-content: space-between; gap: 4px; white-space: nowrap; overflow: hidden; }
.day .it .t { overflow: hidden; text-overflow: ellipsis; }
.day .it.done .t { text-decoration: line-through; opacity: .6; }
.day .more { font-size: 11px; color: var(--muted); }
.day .bal { position: absolute; left: 7px; right: 7px; bottom: 5px; font-size: 12.5px; font-weight: 650; text-align: right; }
.day.negday { background: var(--neg-soft); }
.day.negday .bal { color: var(--neg); }
.day .tag { position: absolute; right: 6px; top: 6px; font-size: 10px; font-weight: 700; padding: 0 5px; border-radius: 6px; }
.day .tag.hi { background: color-mix(in srgb, var(--hi) 18%, transparent); color: var(--hi); }
.day .tag.lo { background: color-mix(in srgb, var(--lo) 22%, transparent); color: var(--lo); }
.dots { display: none; gap: 2px; flex-wrap: wrap; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--muted); }
.dots i.p { background: var(--pos); } .dots i.n { background: var(--neg); }

/* side panel */
.side { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 72px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; box-shadow: var(--shadow); }
.card h3 { margin: 0 0 8px; font-size: 14px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); font-weight: 650; }
.kv { display: flex; justify-content: space-between; padding: 3px 0; gap: 10px; }
.kv.total { border-top: 1px solid var(--line); margin-top: 6px; padding-top: 8px; font-weight: 650; }
.catrow { padding: 7px 0; border-bottom: 1px solid var(--line); }
.catrow:last-child { border-bottom: 0; }
.catrow .top { display: flex; justify-content: space-between; gap: 8px; font-size: 13.5px; }
.catrow .top .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.catrow .sub { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }
.bar { height: 6px; background: var(--panel2); border-radius: 3px; overflow: hidden; margin: 4px 0 2px; }
.bar i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
.bar i.over { background: var(--neg); }
.bar i.inc { background: var(--pos); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 6px 12px; }
.seg button.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

/* lists */
.list { display: flex; flex-direction: column; }
.row { display: flex; align-items: center; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .ttl { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .meta { font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row.click { cursor: pointer; } .row.click:hover { background: var(--panel2); }
.row input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
.pill { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--panel2); color: var(--muted); }

/* alerts */
.alert { border-left: 4px solid var(--muted); }
.alert.high { border-left-color: var(--neg); } .alert.medium { border-left-color: var(--lo); } .alert.low { border-left-color: var(--accent); }
.alert h4 { margin: 0 0 4px; font-size: 15px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.chip { border: 1px solid var(--line); background: var(--panel2); border-radius: 999px; padding: 3px 9px; font-size: 12.5px; }

/* reports */
.reports { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.chartbox { position: relative; height: 320px; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 6px 0 14px; }

/* forms */
.form { display: grid; gap: 12px; }
.field label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.field input, .field select, .field textarea, .inp { width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: 9px 10px; }
.field textarea { min-height: 64px; resize: vertical; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 4px; }
.settings { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl td, .tbl th { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: left; }
.tbl input, .tbl select { width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 7px; padding: 5px 7px; font-size: 14px; }
.tbl td.r, .tbl th.r { text-align: right; }
.note { font-size: 13px; color: var(--muted); }
.empty { text-align: center; padding: 48px 16px; }
.empty h2 { margin: 0 0 6px; }

/* modal sheet */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(10, 12, 14, .45); }
.sheet { position: relative; background: var(--bg); border-radius: 16px; width: min(560px, 94vw); max-height: 90vh; overflow: auto; padding: 18px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.sheet h2 { margin: 0 0 4px; font-size: 19px; }
.sheet .hd { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 12px; }
.sheet .hd .grow { flex: 1; }
.x { border: 0; background: var(--panel2); width: 32px; height: 32px; border-radius: 50%; font-size: 18px; line-height: 1; }
.toast { position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; font-size: 14px; max-width: 90vw; }

.bottomnav { display: none; }

@media (max-width: 1000px) {
  .calwrap { grid-template-columns: 1fr; }
  .side { position: static; }
  .reports { grid-template-columns: 1fr; }
}
@media (max-width: 700px) {
  html, body { font-size: 14px; }
  .mainnav, .topbar .brand span { display: none; }
  .topbar { padding-left: 12px; padding-right: 12px; }
  .topbar .brand::after { content: 'Perisso'; font-weight: 650; }
  main { padding: 0 8px calc(84px + env(safe-area-inset-bottom)); }
  .tabs { padding: 8px; }
  .tabs button { min-width: 96px; padding: 5px 10px; }
  .calnav h2 { font-size: 17px; min-width: 0; flex: 1; text-align: center; }
  .calnav .wide { display: none; }
  .grid { border-radius: 10px; }
  .grid .dow { padding: 4px 2px; text-align: center; font-size: 10.5px; }
  .day { min-height: 64px; padding: 3px 2px 18px; align-items: center; }
  .day .it, .day .more { display: none; }
  .dots { display: flex; justify-content: center; }
  .day .bal { font-size: 9.5px; left: 1px; right: 1px; text-align: center; bottom: 3px; letter-spacing: -.2px; }
  .day .tag { display: none; }
  .day.hiday { box-shadow: inset 0 -3px 0 var(--hi); } .day.loday { box-shadow: inset 0 -3px 0 var(--lo); }
  .day.negday.loday { box-shadow: inset 0 -3px 0 var(--neg); }
  .sheet { width: 100vw; max-height: 92vh; border-radius: 16px 16px 0 0; padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
  .modal { align-items: flex-end; }
  .bottomnav { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; background: var(--panel); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
  .bottomnav button { flex: 1; border: 0; background: transparent; padding: 7px 0 6px; font-size: 11px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 1px; position: relative; }
  .bottomnav button span { font-size: 18px; line-height: 1.1; }
  .bottomnav button.active { color: var(--accent); font-weight: 600; }
  .bottomnav .dot { position: absolute; top: 6px; right: calc(50% - 16px); width: 8px; height: 8px; border-radius: 50%; background: var(--neg); }
  .settings { grid-template-columns: 1fr; }
  .chartbox { height: 280px; }
  .two { grid-template-columns: 1fr; }
}

/* cloud sync + selection bar */
#syncBtn { margin-left: auto; } #syncBtn:not([hidden]) + #themeBtn { margin-left: 6px; }
.sync-ok { color: var(--pos); } .sync-error { color: var(--neg); } .sync-syncing, .sync-pending { color: var(--muted); }
.selbar { position: sticky; top: 56px; z-index: 5; display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-top: 10px; padding: 8px 10px; background: var(--accent-soft); border-radius: 10px; }

/* envelopes */
.envlist { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; margin: 12px 0; }
.envlist .card { margin: 0; }
.envhd { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.envhd .sw { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.envhd .spacer { flex: 1; }
.envgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 8px; }
.envgrid .big { font-size: 20px; font-weight: 650; }
.pill { margin-left: 4px; }
