:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #17201d;
  --muted: #64716c;
  --line: #e2e7e5;
  --accent: #0f766e;
  --accent-soft: #e0f2ef;
  --accent-text: #ffffff;
  --ok: #0f9d74;
  --warn: #d48806;
  --over: #d1342f;
  --track: #e8edeb;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(0 0 0 / 0.04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1412;
    --card: #17201d;
    --text: #e8efec;
    --muted: #93a29c;
    --line: #26322e;
    --accent: #2bb3a3;
    --accent-soft: #143430;
    --accent-text: #06201c;
    --ok: #34c79a;
    --warn: #f0b03c;
    --over: #f0645f;
    --track: #24302c;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

/* Sin retraso ni zoom por doble toque en botones y campos. */
button, input, select, label, summary { touch-action: manipulation; }

h1, h2, h3 { margin: 0 0 8px; line-height: 1.2; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.95rem; margin-top: 16px; }
p { margin: 0 0 8px; }
small { font-size: 0.8rem; }
code { font-size: 0.9em; background: var(--accent-soft); padding: 1px 5px; border-radius: 6px; }

.muted { color: var(--muted); }
.neg { color: var(--over); font-weight: 600; }
.warn-text { color: var(--warn); font-weight: 600; }
.loading { text-align: center; color: var(--muted); padding: 40vh 16px 0; }

/* Estructura */
.top {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: calc(env(safe-area-inset-top) + 14px) 16px 10px;
}
.view {
  max-width: 560px; margin: 0 auto;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 96px);
}
.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  border: 0; background: none; border-radius: 0;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 10px; font-size: 0.75rem; color: var(--muted);
}
.tabs button span { font-size: 1.3rem; }
.tabs button.active { color: var(--accent); font-weight: 600; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  padding: 16px;
  margin-bottom: 12px;
}
.card.archived { opacity: 0.65; }
.card.highlight { background: var(--accent-soft); border-color: transparent; }
.card.highlight p { margin: 2px 0 0; font-size: 1.05rem; }
.empty { text-align: center; padding: 32px 16px; }

.login {
  min-height: 100vh; display: grid; place-items: center; padding: 16px;
}
.login .card { width: 100%; max-width: 380px; }

.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.two { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.two input { min-width: 0; }
/* Safari en iPhone da a los campos de fecha un ancho propio que desborda la tarjeta. */
input[type="date"] { -webkit-appearance: none; appearance: none; display: block; }
.actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.actions button { flex: 1; }

/* Formularios */
.field { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 12px; }
.field input, .field select { margin-top: 4px; }
.limits-box { margin: 4px 0 14px; }
.limits { display: grid; grid-template-columns: 1fr 84px 84px; gap: 6px 8px; align-items: center; }
.limits .head { font-size: 0.75rem; color: var(--muted); text-align: right; }
.limits .name { font-size: 0.92rem; overflow-wrap: anywhere; }
.limits .name span { margin-right: 4px; }
.limits input { text-align: right; padding: 8px; min-height: 40px; }
.daily { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.daily h3 { margin-top: 0; }
.daily .list li { padding: 8px 0; }
.field.inline {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  color: var(--text); font-size: 0.95rem; margin-bottom: 8px;
}
.field.inline input { width: 110px; margin: 0; text-align: right; }

input, select {
  width: 100%;
  min-width: 0;
  font: inherit; font-size: 16px; color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

button {
  font: inherit; font-weight: 500;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card); color: var(--text);
  padding: 10px 14px; min-height: 44px;
  cursor: pointer;
}
button:disabled { opacity: 0.55; cursor: progress; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; width: 100%; }
button.big { min-height: 52px; font-size: 1.05rem; margin-top: 4px; }
button.danger { color: var(--over); }
.icon-btn { border: 0; background: none; color: var(--muted); min-height: 36px; padding: 6px 8px; }

.amount {
  font-size: 2.2rem; font-weight: 700; text-align: center;
  min-height: 72px; margin-bottom: 14px;
  background: var(--bg);
}

.cats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
@media (max-width: 360px) { .cats { grid-template-columns: repeat(3, 1fr); } }
.cat { position: relative; }
.cat input { position: absolute; opacity: 0; width: 1px; height: 1px; min-height: 0; pointer-events: none; }
.cat span {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 72px; padding: 6px 4px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--bg);
  font-size: 0.72rem; text-align: center; line-height: 1.15;
  hyphens: auto; overflow-wrap: break-word;
  cursor: pointer;
}
.cat span b { font-size: 1.5rem; font-weight: normal; }
.cat input:checked + span { background: var(--accent-soft); border-color: var(--accent); outline: 2px solid var(--accent); color: var(--text); font-weight: 600; }
.cat input:focus-visible + span { outline: 2px solid var(--accent); }

details { margin-bottom: 12px; }
summary { color: var(--muted); cursor: pointer; padding: 4px 0 10px; font-size: 0.9rem; }

/* Listas */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list li.stack { flex-direction: column; align-items: stretch; gap: 6px; }
.list .ic { font-size: 1.3rem; width: 28px; text-align: center; }
.list .grow { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.list .grow small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.amt { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

.cat-edit { display: flex; gap: 6px; width: 100%; align-items: center; }
.cat-edit .emoji { width: 52px; flex: none; text-align: center; padding: 8px 4px; }
.cat-edit button.primary { width: auto; }
.card > .cat-edit { margin-top: 10px; }

.pill { display: inline-block; font-size: 0.7rem; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); vertical-align: middle; }
.pill.muted { color: var(--muted); background: var(--track); }

/* Barras y KPIs */
.budget { margin-top: 4px; }
.budget .row { font-size: 0.9rem; margin-bottom: 6px; }
.bar { height: 8px; background: var(--track); border-radius: 99px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.bar.ok span { background: var(--ok); }
.bar.warn span { background: var(--warn); }
.bar.over span { background: var(--over); }

.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 12px; }
.kpi { background: var(--bg); border-radius: 12px; padding: 10px 12px; }
.kpi small { color: var(--muted); display: block; }
.kpi b { font-size: 1.15rem; font-variant-numeric: tabular-nums; }

/* Aviso flotante */
#toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(env(safe-area-inset-bottom) + 76px);
  max-width: 480px; margin: 0 auto; z-index: 20;
  padding: 12px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg);
  font-weight: 500; text-align: center;
  opacity: 0; transform: translateY(8px); pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
#toast.show { opacity: 1; transform: none; }
#toast.error { background: var(--over); color: #fff; }
#toast.warn { background: var(--warn); color: #1d1300; }
