:root {
  --bg: #f3f5f4;
  --card: #ffffff;
  --text: #17201e;
  --muted: #5d6b67;
  --line: #e2e7e5;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --positive: #157347;
  --negative: #b42318;
  --soft: #e6efed;
  /* Gráficos: colores validados para distinguirse también con daltonismo. */
  --series-income: #2a78d6;
  --series-expense: #eb6834;
  --series-worth: #6250d6;
  --grid: #e7ebe9;
  --axis: #b9c4c0;
  --highlight: rgb(23 32 30 / .06);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101413;
    --card: #1a201f;
    --text: #e8eeec;
    --muted: #9aaaa5;
    --line: #2b3432;
    --accent: #2dd4bf;
    --accent-text: #062a26;
    --positive: #5ed39b;
    --negative: #ff8a80;
    --soft: #263130;
    --series-income: #3987e5;
    --series-expense: #d95926;
    --series-worth: #9085e9;
    --grid: #262e2c;
    --axis: #46524f;
    --highlight: rgb(255 255 255 / .07);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

[hidden] { display: none !important; }
.hidden { display: none; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 8px;
  background: var(--bg);
}

.topbar > div {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

h1 { margin: 0; font-size: 28px; }
.gear { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 10px; color: var(--muted); }
.gear svg { width: 26px; height: 26px; fill: currentColor; }
.gear[aria-current="page"] { color: var(--accent); background: var(--soft); }
h2 { font-size: 15px; margin: 0 0 8px; }

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 150px);
}

main > div { display: flex; flex-direction: column; gap: 16px; }
section > h2 { color: var(--muted); font-weight: 600; }

.card, .stat, .notice {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.card { padding: 4px 14px; }

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

button {
  min-height: 44px;
  border: 0;
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  background: none;
  touch-action: manipulation;
}

button:disabled { opacity: .4; cursor: default; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.secondary { background: var(--soft); font-weight: 600; }
.danger { background: var(--negative); color: var(--card); font-weight: 600; }
.link { color: var(--accent); font-weight: 600; padding-left: 0; text-align: left; }
.danger-text { color: var(--negative); }
.wide { width: 100%; text-align: center; }

.month { display: flex; align-items: center; justify-content: space-between; }
.arrow { min-width: 44px; font-size: 26px; background: var(--soft); }

.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.stats.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.stat { display: flex; flex-direction: column; gap: 2px; padding: 10px; }
.stat span, small { color: var(--muted); font-size: 13px; }
.stat strong { font-size: clamp(14px, 4.2vw, 19px); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.total { display: flex; flex-direction: column; gap: 4px; text-align: center; }
.total .stat { align-items: center; }
.total .stat strong { font-size: 30px; }

.row {
  display: block;
  width: 100%;
  min-height: 48px;
  padding: 10px 0;
  border-radius: 0;
  text-align: left;
}

.row + .row { border-top: 1px solid var(--line); }
.row-main { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.row-text { display: flex; flex-direction: column; min-width: 0; overflow-wrap: anywhere; }
.row-title { font-weight: 500; }
.row-value { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.card > .link { display: block; width: 100%; border-top: 1px solid var(--line); border-radius: 0; }

.positive { color: var(--positive); }
.negative { color: var(--negative); }
.muted { color: var(--muted); }

.bar { height: 6px; margin-top: 6px; border-radius: 3px; background: var(--soft); overflow: hidden; }
.fill { display: block; height: 100%; background: var(--accent); }
.fill.over { background: var(--negative); }

.empty, .hint { color: var(--muted); font-size: 14px; margin: 10px 0; }
.hint.tight { margin: -8px 0 0; }

.segmented { display: flex; gap: 4px; padding: 3px; border-radius: 12px; background: var(--soft); }
.segmented button { flex: 1; }
.segmented button[aria-pressed="true"] { background: var(--card); font-weight: 700; }

.chart { margin: 0; padding: 10px 0; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--muted); }
.swatch { display: inline-block; width: 10px; height: 10px; margin-right: 6px; border-radius: 3px; }
.tone-income { background: var(--series-income); }
.tone-expense { background: var(--series-expense); }
.tone-worth { background: var(--series-worth); }

.plot { position: relative; height: 160px; margin: 14px 0 0 48px; }
.gridline { position: absolute; left: 0; right: 0; height: 1px; background: var(--grid); }
.gridline.zero { background: var(--axis); }
.gridline span {
  position: absolute;
  right: calc(100% + 6px);
  top: -8px;
  color: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.bands { position: absolute; inset: 0; display: flex; }
/* Toda la franja del mes se puede tocar, no solo la columna. */
.band {
  flex: 1;
  display: flex;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  height: 100%;
  min-height: 0;
  padding: 0 2px;
  border-radius: 6px;
}
.band.selected { background: var(--highlight); }
.slot { position: relative; flex: 1; max-width: 24px; height: 100%; }
.column { position: absolute; left: 0; right: 0; border-radius: 4px 4px 0 0; }
.column.down { border-radius: 0 0 4px 4px; }

.axis-x { display: flex; margin-left: 48px; color: var(--muted); font-size: 11px; }
.axis-x span { flex: 1; min-width: 0; padding-top: 6px; text-align: center; white-space: nowrap; }

.readout { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--line); }
.readout > div { display: flex; flex-wrap: wrap; gap: 4px 18px; }
.readout-item { display: flex; align-items: center; gap: 4px; }
.readout-item .swatch { margin-right: 2px; }

table { width: 100%; margin: 10px 0; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
caption { padding-bottom: 6px; color: var(--muted); font-size: 13px; text-align: left; }
th, td { padding: 8px 0 8px 6px; border-top: 1px solid var(--line); text-align: right; vertical-align: top; }
th:first-child { padding-left: 0; font-weight: 500; text-align: left; }
thead th { border-top: 0; color: var(--muted); font-size: 12px; font-weight: 600; }
td small { display: block; }

.notice { padding: 14px; border-color: var(--accent); }
.notice p { margin: 0 0 8px; }

.fab {
  position: fixed;
  right: max(16px, calc(50% - 304px));
  bottom: calc(env(safe-area-inset-bottom) + 76px);
  z-index: 3;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  padding: 0;
  font-size: 30px;
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: 0 4px 14px rgb(0 0 0 / .25);
}

.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 3;
  display: flex;
  justify-content: center;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card);
  border-top: 1px solid var(--line);
}

.tabbar a {
  flex: 1;
  max-width: 120px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  color: var(--muted);
  font-size: 10px;
  white-space: nowrap;
  text-decoration: none;
}

.tabbar svg { width: 24px; height: 24px; fill: currentColor; }
.tabbar a[aria-current="page"] { color: var(--accent); font-weight: 700; }

/* Las ventanas van arriba para que el teclado no tape los campos. */
.sheet {
  width: min(560px, calc(100% - 20px));
  max-height: calc(100dvh - env(safe-area-inset-top) - 24px);
  margin: calc(env(safe-area-inset-top) + 12px) auto auto;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--card);
  color: var(--text);
  overflow-y: auto;
}

.sheet::backdrop { background: rgb(0 0 0 / .45); }
.sheet h2 { font-size: 20px; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label, legend { font-size: 14px; font-weight: 600; }

input[type="text"], input[type="date"], select {
  width: 100%;
  min-width: 0;
  min-height: 46px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}

input[type="date"] { -webkit-appearance: none; appearance: none; }

.choice { border: 0; padding: 0; margin: 0 0 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.choice legend { margin-bottom: 4px; padding: 0; }
.choice label { flex: 1 1 130px; display: flex; }
.choice input { position: absolute; opacity: 0; }
.choice span {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 46px;
  padding: 8px 12px;
  border: 2px solid var(--line);
  border-radius: 10px;
  text-align: center;
}
.choice input:checked + span { border-color: var(--accent); background: var(--soft); }
.choice input:focus-visible + span { outline: 3px solid var(--accent); }

.actions { display: flex; gap: 8px; margin: 8px 0; }
.actions button { flex: 1; }
.error { color: var(--negative); font-size: 14px; margin: 4px 0; min-height: 1em; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 144px);
  z-index: 10;
  width: max-content;
  max-width: calc(100% - 32px);
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
#toast.show { opacity: 1; }
