:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --income-text: #006300;
  --expense-text: #b42f2f;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 2px 8px rgba(0, 0, 0, 0.04);
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --good-text: #0ca30c;
    --income-text: #2fbf4a;
    --expense-text: #ef7a7a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}

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

/* ---------- Header & navegación ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 12px 20px 0;
}
.topbar-row {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 18px; margin-right: auto; }
.brand span { color: var(--accent); }
.month-picker { display: flex; align-items: center; gap: 6px; }
.month-picker label { color: var(--text-2); font-size: 13px; }

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  margin-top: 10px;
  scrollbar-width: none;
}
.tab {
  border: 0;
  background: none;
  padding: 10px 14px;
  border-bottom: 2px solid transparent;
  color: var(--text-2);
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

main {
  max-width: 1120px;
  margin: 0 auto;
  padding: 20px;
}
.panel { display: none; }
.panel.active { display: block; }

h2 { font-size: 20px; margin: 0 0 4px; }
h3 { font-size: 16px; margin: 0 0 12px; }
.sub { color: var(--text-2); margin: 0 0 16px; }

/* ---------- Tarjetas ---------- */
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.card + .card, .grid + .card, .card + .grid, .grid + .grid { margin-top: 16px; }
.grid > .card { margin-top: 0; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h3 { margin: 0; }

.stat .label { color: var(--text-2); font-size: 13px; }
.stat .value { font-size: 28px; font-weight: 700; margin-top: 4px; }
.stat .delta { font-size: 13px; color: var(--text-2); margin-top: 4px; }

/* ---------- Formularios ---------- */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  align-items: end;
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 13px; color: var(--text-2); }
input[type="text"], input[type="number"], input[type="date"], input[type="month"], input[type="search"], select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--axis);
  border-radius: 8px;
  background: var(--surface);
  min-height: 38px;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text-2); min-height: 38px; }

.segmented { display: inline-flex; border: 1px solid var(--axis); border-radius: 8px; overflow: hidden; min-height: 38px; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label { padding: 8px 14px; cursor: pointer; color: var(--text-2); }
.segmented input:checked + label { background: var(--accent); color: var(--accent-ink); }
.segmented input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: -2px; }

.btn {
  border: 1px solid var(--axis);
  background: var(--surface);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  min-height: 38px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--critical); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn.link { border: 0; background: none; color: var(--accent); padding: 4px 6px; min-height: auto; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Tablas ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
.amount-in { color: var(--income-text); font-weight: 600; }
.amount-out { color: var(--expense-text); font-weight: 600; }
.desc-sub { color: var(--muted); font-size: 12px; }

.tag {
  display: inline-block;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-2);
  white-space: nowrap;
}
.tag .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: 0; }

.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.filters > * { flex: 1 1 160px; }

.empty { color: var(--muted); text-align: center; padding: 28px 12px; }

/* ---------- Gráficas ---------- */
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

.hbars { display: flex; flex-direction: column; gap: 10px; }
.hbar { display: grid; grid-template-columns: minmax(110px, 160px) 1fr auto; gap: 10px; align-items: center; font-size: 14px; }
.hbar .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .track { height: 12px; position: relative; }
.hbar .fill { height: 100%; border-radius: 0 4px 4px 0; min-width: 2px; }
.hbar .val { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.hbar .val small { color: var(--muted); margin-left: 6px; }

.chart-svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart-svg text { fill: var(--muted); font-size: 11px; }
.chart-svg .gridline { stroke: var(--grid); stroke-width: 1; }
.chart-svg .baseline { stroke: var(--axis); stroke-width: 1; }
.chart-svg .hit { fill: transparent; cursor: default; }
.chart-svg .hit:hover, .chart-svg .hit:focus { fill: var(--text); fill-opacity: 0.04; outline: none; }

.tooltip {
  position: fixed;
  pointer-events: none;
  z-index: 50;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  min-width: 160px;
  display: none;
}
.tooltip .t-title { font-weight: 600; margin-bottom: 4px; }
.tooltip .t-row { display: flex; justify-content: space-between; gap: 12px; }
.tooltip .t-row i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; }

details.table-view { margin-top: 12px; }
details.table-view summary { cursor: pointer; color: var(--text-2); font-size: 13px; }

/* ---------- Progreso y estados ---------- */
.progress { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; border-radius: 999px; background: var(--series-1); }
.progress.good > div { background: var(--good); }
.progress.warning > div { background: var(--warning); }
.progress.critical > div { background: var(--critical); }

.status { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; white-space: nowrap; }
.status::before { font-weight: 700; }
.status.good::before { content: "✓"; color: var(--good); }
.status.warning::before { content: "!"; color: #b77d00; }
.status.serious::before { content: "▲"; color: var(--serious); font-size: 10px; }
.status.critical::before { content: "✕"; color: var(--critical); }
.status.info::before { content: "i"; color: var(--accent); font-style: italic; font-family: Georgia, serif; }

.tips { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 10px; }
.tip {
  display: grid;
  grid-template-columns: 28px 1fr;
  gap: 10px;
  padding: 12px;
  border-radius: 10px;
  background: var(--surface-2);
}
.tip .ico {
  width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 700; font-size: 14px; color: #fff;
}
.tip.good .ico { background: var(--good); }
.tip.warning .ico { background: #c98500; }
.tip.serious .ico { background: var(--serious); }
.tip.critical .ico { background: var(--critical); }
.tip.info .ico { background: var(--accent); }
.tip strong { display: block; margin-bottom: 2px; }
.tip p { margin: 0; color: var(--text-2); font-size: 14px; }

.rule-row { display: grid; grid-template-columns: 130px 1fr 90px; gap: 12px; align-items: center; margin-bottom: 14px; }
.rule-row .meter { position: relative; height: 12px; background: var(--surface-2); border-radius: 999px; }
.rule-row .meter .bar { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 999px; background: var(--series-1); max-width: 100%; }
.rule-row .meter .target { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--text); }
.rule-row .pct { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.rule-row .who small { display: block; color: var(--muted); font-size: 12px; }

.slider-row { display: grid; grid-template-columns: minmax(120px, 180px) 1fr 70px minmax(110px, auto); gap: 12px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--grid); }
.slider-row:last-child { border-bottom: 0; }
.slider-row input[type="range"] { width: 100%; accent-color: var(--accent); }
.slider-row .pct { text-align: right; font-variant-numeric: tabular-nums; }
.slider-row .save { text-align: right; color: var(--good-text); font-weight: 600; font-variant-numeric: tabular-nums; }

.big-number { font-size: 32px; font-weight: 700; }
.big-number.good { color: var(--good-text); }

.goal { border: 1px solid var(--border); border-radius: 10px; padding: 14px; background: var(--surface); }
.goal-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.goal-head strong { font-size: 16px; }
.goal-meta { display: flex; gap: 16px; flex-wrap: wrap; color: var(--text-2); font-size: 13px; margin: 8px 0; }
.goal .progress { margin: 8px 0; }
.goal-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; align-items: center; }
.goal-actions input { max-width: 160px; }
.goals { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }

.callout {
  border-left: 3px solid var(--accent);
  background: var(--surface-2);
  padding: 12px 14px;
  border-radius: 0 8px 8px 0;
  color: var(--text-2);
  margin-bottom: 16px;
}
.callout strong { color: var(--text); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--page);
  padding: 10px 16px;
  border-radius: 10px;
  display: none;
  gap: 14px;
  align-items: center;
  z-index: 60;
  box-shadow: 0 6px 24px rgba(0,0,0,.2);
}
.toast.show { display: flex; }
.toast button { background: none; border: 0; color: var(--page); font-weight: 700; text-decoration: underline; cursor: pointer; }

/* ---------- Importar ---------- */
.import-table td { padding: 6px 6px; }
.import-table input[type="text"], .import-table select { min-height: 32px; padding: 4px 8px; font-size: 14px; }
.import-table tr.row-off td { opacity: 0.55; }
.import-table tr.row-off td:first-child { opacity: 1; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

/* ---------- Por confirmar ---------- */
.pending-card { border-left: 3px solid var(--warning); margin-bottom: 16px; }
.pending-list { display: flex; flex-direction: column; gap: 8px; }
.pending-row {
  display: grid; grid-template-columns: 1fr 150px auto; gap: 12px; align-items: center;
  padding: 10px 0; border-bottom: 1px solid var(--grid);
}
.pending-row:last-child { border-bottom: 0; }
.pending-row input { text-align: right; }
.pending-tag { background: rgba(250, 178, 25, 0.18); color: var(--text); }
@media (max-width: 640px) {
  .pending-row { grid-template-columns: 1fr 1fr; }
  .pending-row > div:first-child { grid-column: 1 / -1; }
}

/* ---------- Cuenta y sincronización ---------- */
.user-chip { display: flex; align-items: center; gap: 8px; }
.user-chip[hidden] { display: none; }
.user-chip .avatar {
  width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 14px;
}
.user-chip .user-name { font-size: 14px; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sync { font-size: 12px; color: var(--muted); white-space: nowrap; }
.sync.ok { color: var(--good-text); }
.sync.error { color: var(--critical); }

.auth-gate {
  position: fixed; inset: 0; z-index: 40;
  background: var(--page);
  display: grid; place-items: center;
  padding: 20px;
}
.auth-gate[hidden] { display: none; }
.auth-card { max-width: 420px; width: 100%; text-align: center; padding: 32px 28px; }
.auth-card .brand { font-size: 26px; }
.google-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 12px 16px; font-weight: 600; font-size: 15px;
}
.auth-error { color: var(--critical); font-size: 14px; margin: 14px 0 0; min-height: 1em; }

@media (max-width: 640px) {
  .sync { display: none; }
  main { padding: 14px; }
  .stat .value { font-size: 24px; }
  .hbar { grid-template-columns: 100px 1fr auto; }
  .rule-row { grid-template-columns: 100px 1fr 60px; }
  .slider-row { grid-template-columns: 1fr 1fr; }
  .slider-row .pct { text-align: left; }
  .hide-sm { display: none; }
}
