:root {
  color-scheme: light;
  --page: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --ink: #191919;
  --ink-2: #5f6368;
  --ink-3: #8a8f96;
  --line: #e7e7e7;
  --axis: #c2c2c2;
  --brand: #0f4a96;
  --brand-ink: #ffffff;
  --accent: #257de0;
  --income: #257de0;
  --expense: #f47230;
  --pos: #006c10;
  --neg: #c0392b;
  --good: #0aac23;
  --warning: #fbb315;
  --critical: #dc3c3d;
  --ring: rgba(11, 11, 11, 0.10);
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 16px rgba(0, 0, 0, .04);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0b0b0b;
    --surface: #191919;
    --surface-2: #232323;
    --ink: #efefef;
    --ink-2: #9c9c9c;
    --ink-3: #7a7a7a;
    --line: #303030;
    --axis: #474747;
    --brand: #338ced;
    --brand-ink: #0b0b0b;
    --accent: #338ced;
    --income: #338ced;
    --expense: #e76624;
    --pos: #53c556;
    --neg: #eb6263;
    --ring: rgba(255, 255, 255, 0.10);
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  background: var(--page);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }
.muted { color: var(--ink-2); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.hidden { display: none !important; }

.boot { display: grid; place-items: center; height: 100vh; color: var(--ink-2); }

/* ---------- Layout ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  padding: 20px 12px;
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 17px; }
.brand img { width: 28px; height: 28px; }
.nav a {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 12px; border-radius: 10px;
  color: var(--ink-2); font-weight: 500;
}
.nav a svg { width: 19px; height: 19px; flex: none; }
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a.active { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
.sidebar .spacer { flex: 1; }
.sidebar .user { padding: 10px 12px; font-size: 13px; color: var(--ink-2); border-top: 1px solid var(--line); }

.scrim { display: none; }
.shell > .content { min-width: 0; }
.main { padding: 28px 32px 96px; max-width: 1180px; width: 100%; }
.topbar { display: none; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

@media (max-width: 899px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 30; left: 0; top: 0; width: 270px;
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, .2);
  }
  .shell.menu-open .sidebar { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 25; }
  .shell.menu-open .scrim { display: block; }
  .topbar {
    display: flex; align-items: center; gap: 10px;
    position: sticky; top: 0; z-index: 20;
    background: var(--surface); border-bottom: 1px solid var(--line);
    padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top));
  }
  .topbar .title { font-weight: 650; }
  .main { padding: 18px 14px 110px; }
  h1 { font-size: 20px; }
}

/* ---------- Componentes ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--ring);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
#view > * + * { margin-top: 16px; }
.debt-sum { margin-bottom: 16px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 1050px) { .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 720px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }
@media (max-width: 420px) { .grid-4 { grid-template-columns: 1fr 1fr; gap: 10px; } }
.stack { display: grid; gap: 16px; }

.stat .label { font-size: 13px; color: var(--ink-2); }
.stat .value { font-size: 24px; font-weight: 650; margin-top: 4px; letter-spacing: -.01em; }
.stat .delta { font-size: 12.5px; margin-top: 4px; color: var(--ink-2); }
@media (max-width: 420px) { .stat { padding: 14px; } .stat .value { font-size: 18px; } }

.btn {
  font: inherit; font-weight: 550; font-size: 14px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 10px; padding: 8px 14px; cursor: pointer; min-height: 38px;
}
.btn:hover { background: var(--surface-2); }
.btn svg { width: 17px; height: 17px; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--neg); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.icon { padding: 6px; min-height: 34px; min-width: 34px; }
.btn.sm { min-height: 32px; padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }

.fab {
  position: fixed; right: 20px; bottom: 22px; bottom: max(22px, calc(env(safe-area-inset-bottom) + 12px));
  width: 56px; height: 56px; border-radius: 18px; border: 0;
  background: var(--brand); color: var(--brand-ink); cursor: pointer;
  box-shadow: 0 6px 20px rgba(15, 74, 150, .35); display: grid; place-items: center; z-index: 15;
}
.fab svg { width: 26px; height: 26px; }

label.field { display: grid; gap: 5px; font-size: 13px; color: var(--ink-2); font-weight: 500; }
input, select, textarea {
  font: inherit; font-size: 15px; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 11px; width: 100%; min-height: 40px;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=color] { padding: 3px; width: 56px; }
input[type=checkbox] { width: 18px; height: 18px; min-height: 0; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink); }
.form { display: grid; gap: 14px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 480px) { .row { grid-template-columns: 1fr; } }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.filters > * { width: auto; flex: 1 1 150px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.filters input[type=search] { flex: 2 1 200px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button {
  font: inherit; font-size: 14px; font-weight: 550; border: 0; background: transparent; color: var(--ink-2);
  padding: 7px 14px; border-radius: 8px; cursor: pointer;
}
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, .1); }
.seg.full { display: flex; } .seg.full button { flex: 1; }

.month-nav { display: inline-flex; align-items: center; gap: 4px; }
.month-nav .lbl { min-width: 128px; text-align: center; font-weight: 600; text-transform: capitalize; }

/* Listas */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 12px; padding: 11px 2px; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }
.list .grow { flex: 1; min-width: 0; }
.list .title { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .sub { font-size: 12.5px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list li.click { cursor: pointer; border-radius: 8px; }
.list li.click:hover { background: var(--surface-2); }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.avatar { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; flex: none; background: var(--surface-2); color: var(--ink-2); }
.avatar svg { width: 18px; height: 18px; }
.day-head { font-size: 12.5px; color: var(--ink-2); font-weight: 600; padding: 14px 2px 4px; display: flex; justify-content: space-between; }
.empty { text-align: center; color: var(--ink-2); padding: 28px 10px; }
.empty .btn { margin-top: 12px; }
.tag { font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }

/* Barras de progreso */
.progress { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin-top: 8px; }
.progress > span { display: block; height: 100%; border-radius: 999px; background: var(--accent); width: 0; transition: width .4s ease; }
.progress.good > span { background: var(--good); }
.progress.warning > span { background: var(--warning); }
.progress.critical > span { background: var(--critical); }
.pitem + .pitem { margin-top: 16px; }
.pitem .top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.pitem .meta { display: flex; justify-content: space-between; gap: 10px; font-size: 12.5px; color: var(--ink-2); margin-top: 5px; }
.status { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; font-weight: 600; }
.status svg { width: 14px; height: 14px; }
.status.good { color: var(--pos); }
.status.warning { color: #a86b00; }
.status.critical { color: var(--critical); }
@media (prefers-color-scheme: dark) { .status.warning { color: var(--warning); } }

/* Gráficas */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--ink-2); font-size: 11px; font-family: var(--font); font-variant-numeric: tabular-nums; }
.chart .bar-in { fill: var(--income); }
.chart .bar-ex { fill: var(--expense); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover { fill: color-mix(in srgb, var(--ink) 5%, transparent); }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.legend i.in { background: var(--income); }
.legend i.ex { background: var(--expense); }
.tooltip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface); color: var(--ink); border: 1px solid var(--ring);
  box-shadow: 0 6px 24px rgba(0, 0, 0, .14); border-radius: 10px;
  padding: 8px 10px; font-size: 12.5px; min-width: 150px; transform: translate(-50%, calc(-100% - 10px));
}
.tooltip .t { font-weight: 600; margin-bottom: 4px; text-transform: capitalize; }
.tooltip .r { display: flex; justify-content: space-between; gap: 14px; }
.tooltip .r span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); }
.hbars { display: grid; gap: 10px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 30%) 1fr auto; gap: 10px; align-items: center; font-size: 13.5px; }
.hbar .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .track { height: 14px; }
.hbar .fill { height: 100%; border-radius: 0 4px 4px 0; background: var(--expense); min-width: 2px; width: 0; transition: width .4s ease; }
.hbar .val { font-variant-numeric: tabular-nums; color: var(--ink); font-weight: 550; min-width: 86px; text-align: right; }
.hbar .pct { color: var(--ink-2); font-weight: 400; font-size: 12px; margin-left: 4px; }

/* Tabla */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--line); }
th { font-size: 12.5px; color: var(--ink-2); font-weight: 600; }
td.r, th.r { text-align: right; }

/* Modal */
dialog {
  border: 0; border-radius: 18px; padding: 0; width: min(520px, calc(100vw - 24px));
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
  max-height: calc(100vh - 32px);
}
dialog::backdrop { background: rgba(10, 14, 20, .45); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 6px; }
.modal-body { padding: 10px 18px 18px; overflow-y: auto; max-height: calc(100vh - 140px); }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 18px 18px; border-top: 1px solid var(--line); }
.modal-foot .left { margin-right: auto; }
@media (max-width: 560px) {
  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 18px 18px 0 0; max-height: 92vh; }
  .modal-foot { padding-bottom: max(18px, env(safe-area-inset-bottom)); }
}
.amount-input { font-size: 26px; font-weight: 650; text-align: center; min-height: 56px; letter-spacing: -.01em; }

#toast {
  position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px); opacity: 0;
  background: #191919; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  transition: all .2s ease; z-index: 100; pointer-events: none; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: #b42318; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login .card { width: min(380px, 100%); padding: 28px; }
.login .brand { justify-content: center; padding-bottom: 8px; font-size: 20px; }
.login p { text-align: center; margin: 0 0 20px; }
.error-msg { color: var(--neg); font-size: 13.5px; min-height: 18px; }

@media print {
  .sidebar, .topbar, .fab, .page-head .actions { display: none !important; }
  .shell { display: block; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
