:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --ink: #161616;
  --ink-2: #5d5d5a;
  --ink-3: #9a9994;
  --line: #e4e2dc;
  --field: #efeee9;
  --field-focus: #ffffff;

  --c-esen: #3f63d8;
  --c-ahor: #1f9a6f;
  --c-lujo: #d88a1f;
  --c-bal:  #c9c7c0;

  --pos: #1f9a6f;
  --neg: #d4473a;

  --radius: 10px;
  --font: "Geist", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "Geist Mono", ui-monospace, "Cascadia Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --surface: #1a1a1a;
    --ink: #ecebe7;
    --ink-2: #a5a49f;
    --ink-3: #6b6a66;
    --line: #2a2a29;
    --field: #202020;
    --field-focus: #262626;
    --c-esen: #7192f5;
    --c-ahor: #3fc595;
    --c-lujo: #f0a845;
    --c-bal:  #3a3a38;
    --pos: #3fc595;
    --neg: #f06b5e;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }
[hidden] { display: none !important; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

button { font: inherit; color: inherit; cursor: pointer; }

.num, .mono { font-family: var(--mono); font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }

/* ---------- Header ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 14px 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  width: 28px; height: 28px; border-radius: 8px;
  display: grid; place-items: center;
  background: var(--ink); color: var(--bg);
  font-weight: 700; font-size: 15px;
}
.brand-name { font-weight: 600; letter-spacing: -0.02em; }

.month { display: flex; align-items: center; gap: 4px; }
.month-label {
  min-width: 150px; text-align: center;
  font-weight: 500;
}

.icon-btn {
  width: 34px; height: 34px; border-radius: 8px;
  border: 0; background: transparent;
  font-size: 20px; line-height: 1;
  display: grid; place-items: center;
}
.icon-btn:hover { background: var(--field); }

.menu { justify-self: end; position: relative; }
.menu-pop {
  position: absolute; right: 0; top: 40px;
  min-width: 220px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px;
  box-shadow: 0 12px 32px rgba(0,0,0,.12);
  display: flex; flex-direction: column;
}
.menu-pop button, .menu-file {
  text-align: left; border: 0; background: transparent;
  padding: 9px 10px; border-radius: 7px; font-size: 14px;
  cursor: pointer;
}
.menu-pop button:hover, .menu-file:hover { background: var(--field); }
.menu-file input { display: none; }
.danger { color: var(--neg); }

/* ---------- Tabs ---------- */
.tabs {
  display: flex; gap: 4px;
  padding: 0 24px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs button {
  border: 0; background: transparent;
  padding: 10px 12px 12px;
  color: var(--ink-2);
  font-weight: 500;
  position: relative;
  white-space: nowrap;
}
.tabs button.active { color: var(--ink); }
.tabs button.active::after {
  content: ""; position: absolute; left: 12px; right: 12px; bottom: -1px;
  height: 2px; background: var(--ink); border-radius: 2px;
}

main { max-width: 1120px; margin: 0 auto; padding: 28px 24px 80px; }

/* ---------- Summary ---------- */
.summary {
  display: grid;
  grid-template-columns: 1.3fr repeat(4, 1fr);
  gap: 24px;
  align-items: end;
}
.kpi .label { color: var(--ink-2); font-size: 13px; display: flex; align-items: center; gap: 7px; }
.kpi .value { font-size: 22px; font-weight: 500; margin-top: 4px; }
.kpi.big .value { font-size: 36px; letter-spacing: -0.03em; }
.kpi .sub { color: var(--ink-3); font-size: 12.5px; margin-top: 2px; }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot.esen { background: var(--c-esen); }
.dot.ahor { background: var(--c-ahor); }
.dot.lujo { background: var(--c-lujo); }
.dot.bal  { background: var(--c-bal); }

.pos { color: var(--pos); }
.neg { color: var(--neg); }

.bar {
  display: flex; height: 10px; border-radius: 99px; overflow: hidden;
  background: var(--field); margin: 22px 0 6px; gap: 2px;
}
.bar span { display: block; height: 100%; transition: width .35s ease; }
.bar .esen { background: var(--c-esen); }
.bar .ahor { background: var(--c-ahor); }
.bar .lujo { background: var(--c-lujo); }
.bar .bal  { background: var(--c-bal); }
.bar-note { color: var(--ink-3); font-size: 12.5px; }
.bar-note.neg { color: var(--neg); }

/* ---------- Sections ---------- */
.section { margin-top: 40px; }
.section-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  padding-bottom: 10px; border-bottom: 1px solid var(--line);
}
.section-head h2 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: -0.01em; display: flex; align-items: center; gap: 8px; }
.section-head .total { font-size: 15px; font-weight: 500; }
.section-head .pct { color: var(--ink-3); font-size: 13px; margin-left: 8px; }

.cols { display: grid; grid-template-columns: repeat(3, 1fr); gap: 32px; }
.cols .section { margin-top: 40px; }

/* ---------- Editable rows ---------- */
.rows { display: flex; flex-direction: column; }
.row {
  display: grid;
  grid-template-columns: 1fr 108px 28px;
  gap: 8px; align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}
.row.fixed .name { color: var(--ink-2); padding-left: 10px; font-size: 14px; }
.row.fixed .amount { text-align: right; padding-right: 10px; color: var(--ink-2); }
.row.fixed .hint { grid-column: 1 / -1; color: var(--ink-3); font-size: 12px; padding-left: 10px; margin-top: -4px; }

input.field, select.field {
  width: 100%; min-width: 0;
  font: inherit; color: inherit;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 7px 9px;
  outline: none;
  transition: background .15s, border-color .15s;
}
input.field:hover, select.field:hover { background: var(--field); }
input.field:focus, select.field:focus { background: var(--field-focus); border-color: var(--ink-3); }
input.field.num { text-align: right; }
select.field { appearance: none; cursor: pointer; }

.del {
  width: 28px; height: 28px; border: 0; border-radius: 7px;
  background: transparent; color: var(--ink-3);
  font-size: 18px; line-height: 1; opacity: 0;
  transition: opacity .15s;
}
.row:hover .del, .del:focus { opacity: 1; }
.del:hover { background: var(--field); color: var(--neg); }

.add {
  margin-top: 8px;
  border: 0; background: transparent;
  color: var(--ink-2); font-size: 14px;
  padding: 7px 9px; border-radius: 7px;
  align-self: flex-start;
}
.add:hover { background: var(--field); color: var(--ink); }

/* Ingresos */
.ingresos { display: grid; grid-template-columns: repeat(3, 1fr) 1fr; gap: 24px; padding-top: 14px; }
.ingresos label { display: block; color: var(--ink-2); font-size: 13px; margin-bottom: 4px; }
.ingresos input.field { background: var(--field); text-align: left; font-size: 17px; }
.ingresos .result .value { font-size: 17px; padding: 7px 0; font-weight: 500; }

/* ---------- Tables (deudas / ahorros / patrimonio) ---------- */
.table-head, .trow {
  display: grid;
  gap: 8px; align-items: center;
}
.table-head {
  color: var(--ink-3); font-size: 12.5px;
  padding: 14px 0 6px;
  border-bottom: 1px solid var(--line);
}
.table-head .r { text-align: right; padding-right: 10px; }
.table-head span:first-child { padding-left: 10px; }
.trow { padding: 6px 0; border-bottom: 1px solid var(--line); }
.trow .calc { text-align: right; padding-right: 10px; color: var(--ink-2); }
.trow .lbl { display: none; }
.trow.fixed .name { padding-left: 10px; color: var(--ink-2); }
.trow:hover .del { opacity: 1; }

.t-deudas  { grid-template-columns: 1.6fr 1fr 1fr .7fr 1fr 28px; }
.t-ahorros { grid-template-columns: 1.6fr 1fr 1fr 1.2fr 28px; }
.t-patri   { grid-template-columns: 1.4fr 1fr 1fr 1.1fr 1fr 28px; }

.foot {
  display: grid; gap: 8px; padding: 12px 0;
  font-weight: 500;
}
.foot .r { text-align: right; padding-right: 10px; }
.foot span:first-child { padding-left: 10px; color: var(--ink-2); font-weight: 400; }

.lead { color: var(--ink-2); max-width: 60ch; margin: 0 0 8px; font-size: 14px; }

.rate {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  color: var(--ink-2); font-size: 14px;
}
.rate input.field { width: 120px; background: var(--field); text-align: right; }
.rate .muted { color: var(--ink-3); font-size: 12.5px; }
.ghost {
  border: 1px solid var(--line); background: transparent;
  border-radius: 7px; padding: 6px 10px; font-size: 13px;
}
.ghost:hover { background: var(--field); }

.big-total { display: flex; gap: 40px; flex-wrap: wrap; margin: 6px 0 4px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 99px; font-size: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
  z-index: 50;
}

/* ---------- Responsive ---------- */
@media (max-width: 920px) {
  .summary { grid-template-columns: 1fr 1fr; }
  .kpi.big { grid-column: 1 / -1; }
  .cols { grid-template-columns: 1fr; gap: 0; }
  .ingresos { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 640px) {
  body { font-size: 15px; }
  .top { grid-template-columns: auto 1fr auto; padding: 10px 16px; }
  .brand-name { display: none; }
  .month { justify-self: center; }
  .month-label { min-width: 0; }
  .tabs { padding: 0 8px; }
  main { padding: 20px 16px 80px; }
  .kpi.big .value { font-size: 30px; }
  .kpi .value { font-size: 18px; }
  .ingresos { grid-template-columns: 1fr; gap: 12px; }
  .del { opacity: 1; }

  /* tablas → filas apiladas con etiquetas */
  .table-head { display: none; }
  .trow {
    grid-template-columns: 1fr 1fr !important;
    padding: 10px 0;
    position: relative;
  }
  .trow > .name { grid-column: 1 / -1; font-weight: 500; padding-right: 36px; }
  .trow > .del { position: absolute; top: 12px; right: 0; }
  .trow .cell { display: flex; flex-direction: column; }
  .trow .lbl { display: block; font-size: 11.5px; color: var(--ink-3); padding-left: 9px; }
  .trow .cell input.field.num, .trow .cell .calc { text-align: left; padding-left: 9px; }
  .foot { grid-template-columns: 1fr 1fr !important; row-gap: 10px; }
  .foot > span:first-child { grid-column: 1 / -1; }
  .foot .spacer { display: none; }
  .foot .r { text-align: left; padding-left: 9px; }
  .foot .r::before { content: attr(data-l); display: block; font-size: 11.5px; color: var(--ink-3); font-weight: 400; font-family: var(--font); }
}
