:root {
  --bg: #f2f3f8;
  --bg-elev: #ffffff;
  --card: #ffffff;
  --text: #14162b;
  --text-muted: #5c5f77;
  --border: #e5e6f0;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-contrast: #ffffff;
  --success: #15803d;
  --success-bg: #e7f7ee;
  --danger: #b42318;
  --danger-bg: #fdeceb;
  --info: #1d4ed8;
  --info-bg: #eaf0fd;
  --muted-bg: #eeeff6;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f1f;
    --bg-elev: #12162b;
    --card: #171b33;
    --text: #edeef7;
    --text-muted: #9a9db8;
    --border: #262b47;
    --primary: #818cf8;
    --primary-dark: #6366f1;
    --primary-contrast: #1a1240;
    --success: #4ade80;
    --success-bg: #0f2a1c;
    --danger: #f87171;
    --danger-bg: #2b1414;
    --info: #7fa8ff;
    --info-bg: #131f36;
    --muted-bg: #1c2140;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 2px 8px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}
button, input, select, textarea { font-family: inherit; }

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
  padding-bottom: calc(24px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
}

.appbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(12px + var(--safe-top)) 14px 12px;
  background: linear-gradient(180deg, var(--primary-dark), var(--primary));
  color: var(--primary-contrast);
}
.appbar h1 { font-size: 18px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.appbar .sub { font-size: 12px; opacity: .85; font-weight: 400; display: block; }
.icon-btn {
  background: rgba(255,255,255,.15);
  border: none;
  color: inherit;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:active { background: rgba(255,255,255,.28); }

.month-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  color: var(--primary-contrast);
}
.month-nav .month-label { font-weight: 700; font-size: 15px; }
.month-nav button {
  background: rgba(255,255,255,.15);
  border: none;
  color: inherit;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  font-size: 16px;
  cursor: pointer;
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin: 12px 14px 0;
  background: var(--muted-bg);
  border-radius: var(--radius-sm);
}
.tab {
  flex: 1;
  text-align: center;
  padding: 9px 6px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  background: transparent;
  border: none;
  cursor: pointer;
}
.tab.active { background: var(--card); color: var(--primary); box-shadow: var(--shadow); }

.content { padding: 14px; display: flex; flex-direction: column; gap: 14px; flex: 1; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card h3 { margin: 0 0 10px; font-size: 14px; color: var(--text-muted); font-weight: 700; text-transform: uppercase; letter-spacing: .03em; }

.empty-state { text-align: center; padding: 48px 20px; color: var(--text-muted); }
.empty-state .emoji { font-size: 44px; display: block; margin-bottom: 10px; }
.empty-state p { margin: 6px 0; font-size: 14px; line-height: 1.5; }

.summary-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.stat .label { font-size: 12px; color: var(--text-muted); font-weight: 600; margin-bottom: 6px; }
.stat .value { font-size: 19px; font-weight: 800; }
.stat.wide { grid-column: 1 / -1; }
.stat.success .value { color: var(--success); }
.stat.danger .value { color: var(--danger); }
.stat.primary { background: linear-gradient(135deg, var(--primary-dark), var(--primary)); border: none; }
.stat.primary .label { color: rgba(255,255,255,.85); }
.stat.primary .value { color: #fff; }

.list { display: flex; flex-direction: column; gap: 8px; }
.item-row {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
}
.item-row .info { min-width: 0; }
.item-row .title { font-weight: 700; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-row .meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.item-row .amount { font-weight: 800; font-size: 15px; white-space: nowrap; }
.item-row .amount.ingreso { color: var(--success); }
.item-row .amount.gasto { color: var(--danger); }

.cat-icon {
  width: 36px; height: 36px; border-radius: 10px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 17px;
}

.date-group { margin-top: 6px; }
.date-group .dg-header { font-size: 12.5px; font-weight: 700; color: var(--text-muted); margin: 14px 2px 6px; }
.date-group:first-child .dg-header { margin-top: 0; }

.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin: 4px 2px -2px; }

.cat-row { display: flex; flex-direction: column; gap: 6px; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px 13px; }
.cat-row .top { display: flex; justify-content: space-between; align-items: center; font-size: 13.5px; font-weight: 600; }
.cat-row .top .amount { font-weight: 800; }
.bar-track { height: 8px; border-radius: 4px; background: var(--muted-bg); overflow: hidden; }
.bar-fill { height: 100%; border-radius: 4px; background: var(--primary); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; color: var(--text-muted); }
.field input[type=text], .field input[type=number], .field input[type=date], .field select, .field textarea {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 15px;
  width: 100%;
}
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.segmented { display: flex; gap: 6px; background: var(--muted-bg); border-radius: 12px; padding: 4px; }
.segmented button {
  flex: 1; border: none; background: transparent; padding: 10px 4px; border-radius: 9px;
  font-weight: 700; font-size: 12.5px; color: var(--text-muted); cursor: pointer;
}
.segmented button.active.ingreso { background: var(--success-bg); color: var(--success); }
.segmented button.active.gasto { background: var(--danger-bg); color: var(--danger); }
.segmented button.active.prestamo { background: var(--info-bg); color: var(--info); }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--border); background: var(--bg-elev); color: var(--text);
  padding: 8px 13px; border-radius: 999px; font-size: 13px; font-weight: 600; cursor: pointer;
}
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: none; border-radius: 12px; padding: 13px 16px; font-size: 14.5px; font-weight: 700;
  cursor: pointer; width: 100%;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-secondary { background: var(--muted-bg); color: var(--text); }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-ghost { background: transparent; color: var(--primary); }
.btn-sm { padding: 8px 12px; font-size: 13px; width: auto; border-radius: 9px; }
.btn:active { filter: brightness(.95); }

.fab {
  position: fixed;
  bottom: calc(20px + var(--safe-bottom));
  right: calc(50% - 280px + 20px);
  background: var(--primary);
  color: #fff;
  border: none;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  font-size: 26px;
  box-shadow: 0 6px 18px rgba(0,0,0,.25);
  cursor: pointer;
  z-index: 15;
}
@media (max-width: 599px) { .fab { right: 20px; } }

.sheet-backdrop { position: fixed; inset: 0; background: rgba(15,23,42,.5); opacity: 0; pointer-events: none; transition: opacity .18s ease; z-index: 40; }
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-width: 560px; margin: 0 auto;
  background: var(--bg-elev); border-radius: 20px 20px 0 0; padding: 18px 18px calc(18px + var(--safe-bottom));
  transform: translateY(100%); transition: transform .22s ease; z-index: 41;
  max-height: 88vh; overflow-y: auto; box-shadow: 0 -8px 30px rgba(0,0,0,.25);
}
.sheet.open { transform: translateY(0); }
.sheet .handle { width: 40px; height: 4px; background: var(--border); border-radius: 4px; margin: 0 auto 14px; }
.sheet h2 { margin: 0 0 4px; font-size: 17px; }
.sheet .sheet-sub { font-size: 13px; color: var(--text-muted); margin-bottom: 16px; }
.sheet .actions { display: flex; flex-direction: column; gap: 8px; }

.toast-root { position: fixed; bottom: 24px; left: 0; right: 0; display: flex; justify-content: center; z-index: 60; pointer-events: none; }
.toast {
  background: #14162b; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 13.5px; box-shadow: var(--shadow); opacity: 0; transform: translateY(10px);
  transition: all .2s ease; max-width: 90%; text-align: center;
}
.toast.show { opacity: 1; transform: translateY(0); }

.stack { display: flex; flex-direction: column; gap: 10px; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.divider { height: 1px; background: var(--border); margin: 4px 0; }
.footer-note { text-align: center; color: var(--text-muted); font-size: 11.5px; padding: 10px 20px 4px; line-height: 1.5; }
