:root {
  /* iOS dark system palette */
  --bg: #000000;
  --bg-elev: #1c1c1e;
  --bg-elev2: #2c2c2e;
  --bg-elev3: #3a3a3c;
  --line: rgba(84, 84, 88, 0.6);
  --text: #ffffff;
  --muted: rgba(235, 235, 245, 0.6);
  --muted2: rgba(235, 235, 245, 0.35);
  --accent: #0a84ff;
  --pro: #5e5ce6;
  --perso: #30b0c7;
  --income: #30d158;
  --expense: #ff453a;
  --warn: #ff9f0a;
  --radius: 14px;
  --radius-lg: 22px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Segoe UI", Roboto, sans-serif;
  font-size: 17px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
body { min-height: 100vh; }

#app { max-width: 640px; margin: 0 auto; min-height: 100vh; display: flex; flex-direction: column; }

/* ---------- Header ---------- */
#topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(var(--safe-top) + 10px) 20px 12px;
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--line);
  display: flex; align-items: flex-end; justify-content: space-between; gap: 12px;
}
#app-title { font-size: 13px; font-weight: 600; margin: 0 0 4px; color: var(--muted); letter-spacing: .2px; }
#month-nav { display: flex; align-items: center; gap: 6px; }
#month-label { font-size: 22px; font-weight: 700; text-transform: capitalize; letter-spacing: -.4px; min-width: 128px; text-align: center; }
.icon-btn {
  background: none; border: none; color: var(--accent);
  width: 30px; height: 30px; border-radius: 50%; font-size: 24px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.icon-btn:active { background: var(--bg-elev2); }
#topbar-total { text-align: right; }
#topbar-total .label { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
#month-total { font-size: 20px; font-weight: 700; letter-spacing: -.3px; }

/* ---------- Content ---------- */
#content { flex: 1; padding: 18px 20px calc(104px + var(--safe-bottom)); }

.section-title {
  font-size: 13px; text-transform: uppercase; letter-spacing: .4px;
  color: var(--muted); margin: 26px 4px 8px; font-weight: 500;
}
.card {
  background: var(--bg-elev);
  border-radius: var(--radius);
  padding: 16px;
}
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.stat { background: var(--bg-elev); border-radius: var(--radius); padding: 16px; position: relative; overflow: hidden; }
.stat .stat-label { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.stat .stat-value { font-size: 26px; font-weight: 700; margin-top: 6px; letter-spacing: -.6px; }
.stat .dot { width: 8px; height: 8px; border-radius: 50%; }

/* ---------- Budgets ---------- */
.budget-row { margin-bottom: 16px; }
.budget-row:last-child { margin-bottom: 0; }
.budget-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 7px; gap: 8px; }
.budget-name { font-size: 15px; font-weight: 500; display: flex; align-items: center; gap: 9px; }
.budget-amounts { font-size: 13px; color: var(--muted); white-space: nowrap; }
.budget-amounts .over { color: var(--expense); font-weight: 600; }
.budget-amounts .ok { color: var(--muted); }
.bar { height: 6px; background: var(--bg-elev2); border-radius: 4px; overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: 4px; transition: width .4s cubic-bezier(.4,0,.2,1); }

/* ---------- Transactions / lists ---------- */
.list { background: var(--bg-elev); border-radius: var(--radius); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 13px;
  padding: 12px 16px; cursor: pointer; position: relative;
}
.row + .row::before {
  content: ""; position: absolute; top: 0; left: 62px; right: 0; height: 0.5px; background: var(--line);
}
.row:active { background: var(--bg-elev2); }
.row-emoji {
  width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center;
  font-size: 20px; background: var(--bg-elev2); flex-shrink: 0;
}
.row-main { flex: 1; min-width: 0; }
.row-label { font-size: 16px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-meta { font-size: 13px; color: var(--muted); margin-top: 1px; display: flex; gap: 6px; align-items: center; }
.scope-dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.scope-dot.pro { background: var(--pro); }
.scope-dot.perso { background: var(--perso); }
.row-amount { font-size: 16px; font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row-amount.neg { color: var(--text); }
.row-amount.pos { color: var(--income); }

.empty { text-align: center; color: var(--muted); padding: 48px 20px; font-size: 15px; }

/* ---------- Search & filters ---------- */
.search {
  width: 100%; padding: 10px 14px; border-radius: 10px; border: none;
  background: var(--bg-elev2); color: var(--text); font-size: 17px; margin-bottom: 12px;
}
.search::placeholder { color: var(--muted); }
.search:focus { outline: none; }

.seg-ctl { display: flex; background: var(--bg-elev2); border-radius: 9px; padding: 2px; margin-bottom: 14px; }
.seg-ctl button {
  flex: 1; border: none; background: none; color: var(--text); font-size: 13px; font-weight: 500;
  padding: 7px; border-radius: 7px; cursor: pointer; font-family: inherit;
}
.seg-ctl button.active { background: var(--bg-elev3); font-weight: 600; }

/* ---------- Buttons ---------- */
.btn {
  border: none; border-radius: 12px; padding: 14px 18px; font-size: 17px; font-weight: 600;
  cursor: pointer; background: var(--accent); color: #fff; width: 100%; font-family: inherit;
}
.btn:active { opacity: .8; }
.btn.secondary { background: var(--bg-elev2); color: var(--accent); }
.btn.danger { background: rgba(255,69,58,.16); color: var(--expense); }
.btn:disabled { opacity: .5; }
.btn-row { display: flex; gap: 10px; }

/* ---------- Tabbar ---------- */
#tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  max-width: 640px; margin: 0 auto;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: rgba(20, 20, 22, 0.82);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--line);
  padding: 6px 4px calc(6px + var(--safe-bottom));
}
.tab {
  background: none; border: none; color: var(--muted); font-size: 10px; font-weight: 500;
  display: flex; flex-direction: column; align-items: center; gap: 3px; cursor: pointer; padding: 4px 0; font-family: inherit;
}
.tab svg { width: 26px; height: 26px; }
.tab.active { color: var(--accent); }
.tab-add svg { width: 30px; height: 30px; }
#tabbar .tab-add { color: var(--accent); }

/* ---------- Modal sheet ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
  animation: fadeIn .18s ease;
}
@keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
.sheet {
  background: var(--bg-elev); width: 100%; max-width: 640px;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 12px 18px calc(22px + var(--safe-bottom));
  max-height: 92vh; overflow-y: auto;
  animation: slideUp .26s cubic-bezier(.4,0,.2,1);
}
@keyframes slideUp { from { transform: translateY(40px); opacity: .5 } to { transform: translateY(0); opacity: 1 } }
.sheet h2 { margin: 4px 0 18px; font-size: 22px; font-weight: 700; letter-spacing: -.4px; }
.grabber { width: 36px; height: 5px; border-radius: 3px; background: var(--bg-elev3); margin: 0 auto 14px; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 7px; font-weight: 500; }
.field input, .field select, .field textarea {
  width: 100%; padding: 13px 15px; border-radius: 11px; border: none;
  background: var(--bg-elev2); color: var(--text); font-size: 17px; font-family: inherit;
}
.field textarea { min-height: 84px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); }

.seg { display: flex; gap: 8px; }
.seg button {
  flex: 1; padding: 12px; border-radius: 11px; border: none;
  background: var(--bg-elev2); color: var(--text); font-weight: 600; font-size: 15px; cursor: pointer; font-family: inherit;
}
.seg button.on-1 { background: var(--expense); color: #fff; }
.seg button.on-2 { background: var(--income); color: #fff; }
.seg.pro-perso button.on-1 { background: var(--pro); color: #fff; }
.seg.pro-perso button.on-2 { background: var(--perso); color: #fff; }

.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.cat-pick {
  border: none; background: var(--bg-elev2); border-radius: 12px; padding: 11px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 5px; cursor: pointer;
  font-size: 11px; color: var(--muted); font-family: inherit;
}
.cat-pick .e { font-size: 22px; }
.cat-pick.active { outline: 2px solid var(--accent); color: var(--text); background: var(--bg-elev3); }

.sheet-actions { display: flex; gap: 10px; margin-top: 10px; }
.sheet-actions .btn { flex: 1; }

/* ---------- Import ---------- */
.drop {
  border: 1.5px dashed var(--line); border-radius: var(--radius-lg); padding: 30px 16px; text-align: center;
  color: var(--muted); cursor: pointer; background: var(--bg-elev);
}
.drop:active { border-color: var(--accent); }
.preview { width: 100%; border-radius: var(--radius); margin-top: 14px; }

.cat-manage { display: flex; align-items: center; gap: 13px; padding: 12px 4px; border-bottom: 0.5px solid var(--line); }
.cat-manage:last-child { border-bottom: none; }
.cat-manage .e { font-size: 22px; width: 30px; text-align: center; }
.cat-manage .info { flex: 1; }
.cat-manage .nm { font-weight: 500; font-size: 16px; }
.cat-manage .tg { font-size: 13px; color: var(--muted); margin-top: 1px; }
.cat-manage .del { background: none; border: none; color: var(--expense); font-size: 18px; cursor: pointer; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(104px + var(--safe-bottom)); transform: translateX(-50%) translateY(20px);
  background: rgba(44,44,46,.96); backdrop-filter: blur(10px); color: var(--text);
  padding: 12px 18px; border-radius: 14px; font-size: 14px; font-weight: 500; z-index: 300;
  opacity: 0; pointer-events: none; transition: all .25s ease; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { color: #ffb3ae; }

.hint { font-size: 13px; color: var(--muted); line-height: 1.5; }
.divider { height: 0.5px; background: var(--line); margin: 16px 0; }

.spinner {
  width: 18px; height: 18px; border-radius: 50%; border: 2px solid rgba(255,255,255,.3);
  border-top-color: #fff; animation: spin .7s linear infinite; display: inline-block; vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================
   SWIPE DECK (Tinder-like)
   ============================================================ */
.deck {
  position: fixed; inset: 0; z-index: 200;
  background: var(--bg);
  display: flex; flex-direction: column;
  max-width: 640px; margin: 0 auto;
}
.deck-top {
  padding: calc(var(--safe-top) + 14px) 20px 10px;
  display: flex; align-items: center; justify-content: space-between;
}
.deck-top .title { font-size: 17px; font-weight: 600; }
.deck-top .counter { font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.deck-close { background: var(--bg-elev2); border: none; color: var(--text); width: 30px; height: 30px; border-radius: 50%; font-size: 17px; cursor: pointer; }
.deck-hintbar {
  display: flex; justify-content: space-between; padding: 0 26px; font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .5px;
}
.deck-hintbar .l { color: var(--pro); }
.deck-hintbar .r { color: var(--perso); }

.deck-stage { flex: 1; position: relative; margin: 14px 20px; }
.deck-card {
  position: absolute; inset: 0;
  background: var(--bg-elev);
  border-radius: var(--radius-lg);
  padding: 26px 22px;
  display: flex; flex-direction: column;
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
  will-change: transform;
  touch-action: none;
  transition: transform .28s cubic-bezier(.4,0,.2,1), opacity .28s ease;
}
.deck-card.behind { transform: scale(.94) translateY(14px); opacity: .6; }
.deck-card.gone-right { transform: translateX(140%) rotate(18deg); opacity: 0; }
.deck-card.gone-left { transform: translateX(-140%) rotate(-18deg); opacity: 0; }
.deck-card.dragging { transition: none; }

.deck-badge {
  align-self: flex-start; font-size: 12px; font-weight: 700; letter-spacing: .5px; text-transform: uppercase;
  padding: 5px 11px; border-radius: 999px;
}
.deck-badge.debit { background: rgba(255,69,58,.18); color: var(--expense); }
.deck-badge.credit { background: rgba(48,209,88,.18); color: var(--income); }
.deck-company { font-size: 27px; font-weight: 700; letter-spacing: -.5px; margin-top: 18px; line-height: 1.15; }
.deck-date { font-size: 14px; color: var(--muted); margin-top: 6px; }
.deck-amount { font-size: 42px; font-weight: 700; letter-spacing: -1.2px; margin-top: auto; font-variant-numeric: tabular-nums; }
.deck-amount.credit { color: var(--income); }
.deck-cat-label { font-size: 12px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); margin: 18px 0 8px; }
.deck-cats { display: flex; flex-wrap: wrap; gap: 7px; }
.deck-cats .chip {
  border: none; background: var(--bg-elev2); color: var(--muted); padding: 8px 13px; border-radius: 999px;
  font-size: 13px; font-weight: 500; cursor: pointer; font-family: inherit;
}
.deck-cats .chip.active { background: var(--accent); color: #fff; }

.deck-stamp {
  position: absolute; top: 30px; font-size: 30px; font-weight: 800; letter-spacing: 1px;
  padding: 4px 14px; border-radius: 12px; opacity: 0; pointer-events: none;
}
.deck-stamp.right { right: 22px; color: var(--perso); border: 3px solid var(--perso); transform: rotate(12deg); }
.deck-stamp.left { left: 22px; color: var(--pro); border: 3px solid var(--pro); transform: rotate(-12deg); }

.deck-actions {
  display: flex; align-items: center; justify-content: center; gap: 16px;
  padding: 6px 20px calc(20px + var(--safe-bottom));
}
.deck-btn {
  border: none; cursor: pointer; font-family: inherit; font-weight: 600; font-size: 15px;
  padding: 15px 26px; border-radius: 999px; min-width: 108px;
}
.deck-btn.pro { background: var(--pro); color: #fff; }
.deck-btn.perso { background: var(--perso); color: #fff; }
.deck-btn.undo { background: var(--bg-elev2); color: var(--text); min-width: 0; padding: 15px 18px; font-size: 18px; }
.deck-btn:active { opacity: .8; }

.deck-done {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 30px;
  text-align: center;
}
.deck-done .big { font-size: 52px; }
.deck-done h2 { margin: 0; font-size: 24px; }
