:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #1c2420;
  --muted: #6b7570;
  --line: #d9dedb;
  --grid: #e3e6e4;
  --head: #eef1ef;
  --accent: #1e7a4c;
  --accent-soft: #e2f2e9;
  --danger: #c0392b;
  --good: #1e7a4c;
  --stripe: #fafbfa;
  --shadow: 0 2px 10px rgba(0, 0, 0, .08);
  color-scheme: light; /* listas desplegables, calendario y sugerencias del navegador en el mismo modo que la app */
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121614;
    --surface: #1b211e;
    --text: #e6ebe8;
    --muted: #93a09a;
    --line: #2d3531;
    --grid: #29312d;
    --head: #222a26;
    --accent: #3cb37a;
    --accent-soft: #1d3329;
    --danger: #e3685a;
    --good: #3cb37a;
    --stripe: #1e2521;
    --shadow: 0 2px 10px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ---------- Encabezado ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--accent);
  color: #fff;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 0;
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  max-width: 1000px;
  margin: 0 auto;
}

.topbar h1 { font-size: 18px; margin: 0; font-weight: 650; }
.topbar .who { font-size: 13px; opacity: .85; }

.top-actions { display: flex; gap: 6px; }
.bio-btn { margin-bottom: 12px; background: var(--accent-soft); color: var(--accent); }

/* Gastos fijos */
.fixed-list td { padding: 0 8px; }
.fixed-list tbody tr[data-fixed] { cursor: pointer; }
.fixed-list tbody tr[data-fixed]:hover td { background: var(--xl-hl); }
.fixed-list tr.inactive td { color: var(--muted); }
.grid tr.fixed input, .grid tr.fixed select { color: var(--accent); cursor: pointer; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 400; color: var(--text); padding: 12px 0; }
.check input { width: 20px; height: 20px; }
select.input { appearance: auto; }
.fx-change { background: var(--accent-soft); border-radius: 10px; padding: 10px; }
.steps { margin: 12px 0 8px; padding-left: 20px; line-height: 1.6; }
a.btn { display: block; text-align: center; text-decoration: none; }

.icon-btn {
  background: rgba(255, 255, 255, .15);
  border: 0;
  color: #fff;
  border-radius: 10px;
  padding: 6px 10px;
}

.months {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  max-width: 1000px;
  margin: 10px auto 0;
  padding-bottom: 0;
}
.months::-webkit-scrollbar { display: none; }

.month-tab {
  flex: 0 0 auto;
  background: transparent;
  border: 0;
  color: rgba(255, 255, 255, .75);
  padding: 8px 12px 10px;
  border-radius: 10px 10px 0 0;
  white-space: nowrap;
  font-size: 14px;
}
.month-tab.future { font-style: italic; }
.month-tab.active { background: var(--bg); color: var(--text); font-weight: 650; }

/* ---------- Contenido ---------- */

main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 14px 16px 150px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 12px;
}
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 10px; font-weight: 650; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.kpi { background: var(--head); border-radius: 10px; padding: 10px 12px; }
.kpi .label { font-size: 12px; color: var(--muted); }
.kpi .value { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--muted); }

.rows .row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--grid);
}
.rows .row:last-child { border-bottom: 0; }
.rows .row.total { font-weight: 700; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--danger); }
.pos { color: var(--good); }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.link-btn { background: none; border: 0; color: var(--accent); padding: 0; font-weight: 600; }

.balance {
  background: var(--accent-soft);
  border-radius: 10px;
  padding: 12px;
  font-weight: 600;
  margin-top: 8px;
}

.pct-bar { height: 6px; background: var(--grid); border-radius: 3px; margin-top: 4px; overflow: hidden; }
.pct-bar span { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

/* ---------- Planilla estilo Excel ---------- */

:root {
  --xl-line: #c6c6c6;
  --xl-grid: #e1e1e1;
  --xl-head: #f3f3f3;
  --xl-sel: #217346;
  --xl-hl: #d2e7da;
  --xl-cell: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root {
    --xl-line: #3a423e;
    --xl-grid: #2c3430;
    --xl-head: #222a26;
    --xl-sel: #3cb37a;
    --xl-hl: #1f3a2c;
    --xl-cell: #1b211e;
  }
}

.fxbar {
  display: flex;
  border: 1px solid var(--xl-line);
  background: var(--xl-cell);
  font-size: 13px;
  margin-bottom: 6px;
  min-height: 30px;
}
.namebox { width: 64px; flex: 0 0 auto; padding: 6px 8px; border-right: 1px solid var(--xl-line); text-align: center; }
.fx { padding: 6px 8px; color: var(--muted); font-style: italic; font-family: Georgia, serif; border-right: 1px solid var(--xl-line); }
.fxval { flex: 1; padding: 6px 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.grid-wrap {
  overflow: auto;
  border: 1px solid var(--xl-line);
  background: var(--xl-cell);
  max-height: calc(100vh - 290px);
  min-height: 220px;
}

table.grid {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  font-family: Aptos, Calibri, "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  background: var(--xl-cell);
}
.grid th, .grid td {
  border-right: 1px solid var(--xl-grid);
  border-bottom: 1px solid var(--xl-grid);
  padding: 0;
  height: 30px;
  white-space: nowrap;
  overflow: hidden;
}
.grid thead th {
  background: var(--xl-head);
  color: var(--muted);
  font-weight: 400;
  font-size: 12px;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 2;
  border-color: var(--xl-line);
  height: 24px;
}
.grid .rh {
  background: var(--xl-head);
  color: var(--muted);
  font-weight: 400;
  font-size: 12px;
  text-align: center;
  position: sticky;
  left: 0;
  z-index: 1;
  border-color: var(--xl-line);
}
.grid th.rh[data-open] { cursor: pointer; }
.grid th.rh[data-open]:hover { background: var(--xl-hl); }
.grid thead .corner { left: 0; z-index: 3; }
.grid .hl { background: var(--xl-hl) !important; color: var(--xl-sel); font-weight: 700; }

.grid input, .grid select {
  display: block;
  width: 100%;
  height: 30px;
  border: 0;
  border-radius: 0;
  background: transparent;
  padding: 0 6px;
  font: inherit;
  color: var(--text);
  outline: none;
}
.grid select { appearance: none; cursor: pointer; }
/* La lista desplegable usa los colores de la app (si no, en modo oscuro queda letra clara sobre fondo blanco). */
.grid select option, .grid datalist option { background: var(--xl-cell); color: var(--text); }
.grid td:focus-within, .grid .total-cell:focus {
  outline: 2px solid var(--xl-sel);
  outline-offset: -2px;
}
.grid td.num, .grid td.num input { text-align: right; font-variant-numeric: tabular-nums; }
.grid .title { font-weight: 700; padding: 0 6px; }
.grid tr.titles td { background: var(--xl-head); }
.grid tr.newrow input::placeholder { color: var(--muted); opacity: .55; font-style: italic; }
.grid tr.total td { font-weight: 700; padding: 0 6px; border-top: 3px double var(--xl-line); outline: none; }
.grid .total-cell { color: var(--xl-sel); }
.grid td.calc input { color: var(--muted); font-style: italic; } /* pesos calculados desde dólares */
.grid tr.total.more td { border-top: 0; font-weight: 400; }
.grid tr.total.strong td { font-weight: 700; }
.grid tr.total.muted td { color: var(--muted); font-style: italic; }
.grid tr.total.pos .total-cell { color: var(--good); }
.grid tr.total.neg .total-cell { color: var(--danger); }
.grid td.del { text-align: center; }
.del-btn { border: 0; background: none; color: var(--muted); font-size: 14px; width: 100%; height: 30px; }
.del-btn:hover { color: var(--danger); background: var(--xl-hl); }

/* Pestaña Familia: un cuadro por persona */
.fam-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.fam-top > div { flex: 1 1 260px; }
.fam-title { margin: 0 0 4px; font-size: 20px; }
.fam-top .btn { padding: 10px 14px; font-size: 15px; }
.fam-card { padding: 12px; }
.fam-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.fam-head h3 { margin: 0; font-size: 17px; }
.fam-head .fam-text { flex: 1; }
.saldo { font-weight: 700; font-variant-numeric: tabular-nums; padding: 2px 10px; border-radius: 999px; background: var(--accent-soft); }
.saldo.pos, .num.pos { color: var(--good); }
.saldo.neg { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.danger-link { color: var(--danger); }
.fam-card .grid-wrap { max-height: none; min-height: 0; }
.fam-total .row.total .num { font-size: 18px; }

/* Gastos de otras personas: llegan a la tarjeta pero no son propios */
.grid tr.third td:not(.rh) { background: var(--xl-head); }
.grid tr.third input, .grid tr.third select { color: var(--muted); font-style: italic; }
.cuota-cell { display: flex; align-items: center; }
.cuota-cell span { padding-left: 6px; color: var(--muted); font-size: 12px; }
.cuota-cell input { padding-left: 1px; }

.sheet-actions { display: flex; justify-content: space-between; align-items: center; margin: 10px 2px; gap: 8px; flex-wrap: wrap; }

/* En el celu: letra de 16px para que el iPhone no haga zoom al tocar una celda. */
@media (max-width: 640px) {
  table.grid { font-size: 16px; }
  .grid-wrap { max-height: calc(100vh - 300px); }
}

/* Pestañas de hoja abajo, como en Excel */
.sheetbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 15;
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--xl-head);
  border-top: 1px solid var(--xl-line);
  padding: 0 8px env(safe-area-inset-bottom);
}
.sheetbar::-webkit-scrollbar { display: none; }
.sheet-tab {
  flex: 0 0 auto;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  padding: 9px 14px 7px;
  font-size: 14px;
  color: var(--muted);
  line-height: 1.15;
  text-align: center;
}
.sheet-tab small { display: block; font-size: 11px; opacity: .8; }
.sheet-tab.active { background: var(--xl-cell); color: var(--xl-sel); border-bottom-color: var(--xl-sel); font-weight: 700; }

/* ---------- Botón agregar ---------- */

.fab {
  position: fixed;
  right: 20px;
  bottom: calc(env(safe-area-inset-bottom) + 70px);
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 15px 22px;
  font-size: 16px;
  font-weight: 700;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
  z-index: 20;
}

/* ---------- Formularios / modal ---------- */

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
@media (min-width: 640px) { .backdrop { align-items: center; } }

.modal {
  background: var(--surface);
  width: 100%;
  max-width: 480px;
  max-height: 92vh;
  overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(env(safe-area-inset-bottom) + 18px);
}
@media (min-width: 640px) { .modal { border-radius: 18px; } }
.modal h3 { margin: 0 0 14px; font-size: 18px; }

.field { margin-bottom: 12px; }
.field > label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 600; }

.input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 16px; /* evita el zoom en iPhone */
}
.input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.input.big { font-size: 26px; font-weight: 700; padding: 10px 12px; }

.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 14px;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.hint { font-size: 13px; color: var(--muted); margin-top: 5px; }

.btn {
  border: 0;
  border-radius: 12px;
  padding: 13px 16px;
  font-weight: 700;
  font-size: 16px;
  background: var(--head);
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: transparent; color: var(--danger); }
.btn.block { width: 100%; }
.actions { display: flex; gap: 8px; margin-top: 16px; }
.actions .btn.primary { flex: 1; }

.error { color: var(--danger); font-size: 14px; margin: 8px 0 0; }

/* ---------- Login / configuración ---------- */

.center-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}
.login-card { width: 100%; max-width: 380px; }
.login-card h1 { text-align: center; margin: 8px 0 4px; font-size: 24px; }
.login-card .logo { display: block; margin: 0 auto; width: 72px; height: 72px; border-radius: 18px; }
.login-card p.lead { text-align: center; color: var(--muted); margin: 0 0 20px; }
.who-pick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.who-pick .chip { padding: 14px; border-radius: 12px; font-size: 16px; text-align: center; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 140px);
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  z-index: 100;
  font-size: 14px;
  box-shadow: var(--shadow);
}
