:root {
  --bg: #faf9f5;
  --surface: #ffffff;
  --surface-alt: #f1efe8;
  --text: #1a1a17;
  --text-2: #56554f;      /* muted, 7.4:1 sobre bg (AA) */
  --text-3: #706f68;      /* labels/subtítulos, 4.9:1 sobre bg (AA) */
  --border: #e3e1d9;
  --green: #1d9e75;
  --amber: #ef9f27;
  --red: #e24b4a;
  --gold: #a9852f;        /* oro de marca (contraste AA sobre bg claro) */
  --accent-bg: #e6f1fb;
  --accent-text: #185fa5;
  --warn-bg: #faeeda;
  --warn-text: #7a4708;
  --fill: #1a1a17;
  --on-fill: #ffffff;
  --focus: #c9a227;       /* anillo de foco dorado */
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171714;
    --surface: #22221e;
    --surface-alt: #2c2c28;
    --text: #f1efe8;
    --text-2: #c2c0b6;     /* AA sobre superficies oscuras */
    --text-3: #9d9c93;     /* AA sobre bg oscuro */
    --border: #3a3a34;
    --gold: #d4b25a;
    --accent-bg: #0c447c;
    --accent-text: #b5d4f4;
    --warn-bg: #633806;
    --warn-text: #fac775;
    --fill: #f1efe8;
    --on-fill: #171714;
    --focus: #d4b25a;
  }
}

* { box-sizing: border-box; margin: 0; }
body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}
.hidden { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 12.5px; }
h1 { font-size: 24px; font-weight: 500; }
h2 { font-size: 18px; font-weight: 500; }
.section-label { margin: 18px 0 8px; color: var(--text-3); }

/* Foco visible (WCAG 2.4.7): anillo dorado en cualquier control */
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: var(--radius, 8px);
}
*:focus:not(:focus-visible) { outline: none; }

.screen, .view {
  max-width: 480px;
  margin: 0 auto;
  padding: 24px 18px calc(84px + env(safe-area-inset-bottom));
}
.screen { padding-top: 30vh; display: flex; flex-direction: column; gap: 12px; }

input, button {
  font: inherit;
  color: inherit;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 12px 14px;
  width: 100%;
}
button { cursor: pointer; }
button.primary { background: var(--fill); color: var(--on-fill); border: none; font-weight: 500; }
button:active { transform: scale(0.98); }

.amount-row { display: flex; align-items: baseline; gap: 8px; }
.big { font-size: 32px; font-weight: 500; }
.bar { height: 8px; background: var(--surface-alt); border-radius: 4px; margin: 10px 0 4px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--green); border-radius: 4px; transition: width .3s; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }
.tile-label { font-size: 12.5px; color: var(--text-3); }
.tile-value { font-size: 17px; font-weight: 500; }

.entry { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 8px 0; border-top: 1px solid var(--border); }
.cat-dot { flex: 0 0 auto; width: 9px; height: 9px; border-radius: 50%; }
/* Pantalla de detalle de activo/deuda */
.detail-screen { position: fixed; inset: 0; z-index: 60; background: var(--bg); overflow-y: auto;
  padding: 0 16px calc(28px + env(safe-area-inset-bottom)); animation: detailIn .18s ease; }
@keyframes detailIn { from { transform: translateX(14px); opacity: .4; } to { transform: none; opacity: 1; } }
.detail-head { display: flex; align-items: center; gap: 6px; padding: 12px 0 6px;
  position: sticky; top: 0; background: var(--bg); z-index: 1; }
.detail-head > p { flex: 1; margin: 0; font-weight: 500; font-size: 16px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.detail-big { font-size: 32px; font-weight: 600; margin: 6px 0 0; }
.detail-chart { margin: 12px -4px; }
.metric-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0; }
.metric { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }
.metric .l { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: .05em; }
.metric .v { font-size: 16px; font-weight: 500; margin-top: 2px; }
.detail-donuts { display: flex; flex-wrap: wrap; gap: 18px; justify-content: center; margin: 16px 0 4px; }
.donut-box { text-align: center; max-width: 190px; }
.donut-box p { margin: 6px 0 0; }
.detail-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }

.av { flex: 0 0 auto; width: 32px; height: 32px; border-radius: 9px; display: inline-flex;
  align-items: center; justify-content: center; overflow: hidden; }
.av-logo { background: #fff; box-shadow: inset 0 0 0 1px rgba(0,0,0,.06); }
.av-img { width: 100%; height: 100%; object-fit: contain; padding: 3px; box-sizing: border-box; }
.pend-head { display: flex; align-items: center; gap: 10px; min-width: 0; }
.pend-head .entry-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; }

.networth-card {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; text-align: left; margin-top: 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px;
}
.networth-value { font-size: 19px; font-weight: 500; margin-top: 1px; }
.networth-arrow { color: var(--text-3); display: flex; }
.entry-main { flex: 1; min-width: 0; }
.entry-title { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry-sub { font-size: 12.5px; color: var(--text-3); }
.bank-ico { display: inline-flex; align-items: center; margin-right: 5px; vertical-align: text-bottom; }
.bank-logo { height: 16px; width: auto; max-width: 46px; border-radius: 4px; object-fit: contain;
  background: #fff; padding: 1px; box-sizing: content-box; }
.bank-badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 16px; height: 16px; padding: 0 4px;
  border-radius: 5px; font-size: 9.5px; font-weight: 700; color: #fff;
  letter-spacing: .02em;
}
.entry-amount { font-weight: 500; font-size: 14px; white-space: nowrap; }
.tag-auto { font-size: 11px; background: var(--warn-bg); color: var(--warn-text); border-radius: 8px; padding: 2px 7px; margin-left: 6px; }

.month-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.month-nav p { font-weight: 500; }
.icon-btn { width: 44px; height: 44px; padding: 8px; text-align: center; }

.cat-row { margin-bottom: 11px; }
.cat-head { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 3px; }

.chart { display: flex; align-items: flex-end; gap: 8px; height: 100px; }
.chart .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 3px; height: 100%; }
.chart .col div { width: 100%; background: var(--surface-alt); border-radius: 4px 4px 0 0; }
.chart .col.current div { background: var(--green); }
.chart .col span { font-size: 12px; color: var(--text-3); }

.add-amount { font-size: 42px; font-weight: 500; text-align: center; margin: 10px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  width: auto; font-size: 13px; padding: 9px 15px; border-radius: 16px;
  border: 1px solid var(--border); background: transparent; color: var(--text-2);
}
.chip.selected { background: var(--accent-bg); color: var(--accent-text); border-color: var(--accent-text); font-weight: 500; }
#add-merchant { margin-top: 12px; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; margin: 14px 0; }
.keypad button { font-size: 21px; padding: 13px 0; background: var(--surface); }

.row-between { display: flex; align-items: center; justify-content: space-between; }
.badge { font-size: 12px; padding: 3px 10px; border-radius: 10px; font-weight: 500; }
.badge.warn { background: var(--warn-bg); color: var(--warn-text); }

.pending-card { border: 1px solid var(--border); border-radius: 12px; padding: 11px 13px; margin-top: 9px; background: var(--surface); }
.pending-card.urgent { border-color: var(--amber); }
.pending-deadline { display: flex; gap: 6px; font-size: 12px; color: var(--text-2); border-top: 1px solid var(--border); padding-top: 7px; margin-top: 8px; }
.pending-card.urgent .pending-deadline { color: var(--warn-text); }
.chip.mini { font-size: 12.5px; padding: 9px 13px; min-height: 40px; }
.chip.danger { color: var(--text-3); border-style: dashed; }

#budget-cats .cat-budget { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
#budget-cats label { flex: 1; font-size: 14px; }
#budget-cats input { width: 110px; }
#view-settings button { margin-top: 4px; }
#view-settings .primary { margin-top: 12px; }

.split-row {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-top: 8px;
}
.split-row .chip { padding: 4px 12px; }

.modal-overlay {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--bg);
  border-radius: 16px 16px 0 0;
  padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  width: 100%; max-width: 480px;
}
.modal h2 { margin-bottom: 4px; }
.modal .row-between button { width: auto; flex: 1; }

#tabbar {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; justify-content: space-around; align-items: center;
  max-width: 480px; margin: 0 auto;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
}
.tab {
  width: 48px; height: 44px; padding: 0; border: none; background: none;
  color: var(--text-3); position: relative;
  display: flex; align-items: center; justify-content: center;
}
.tab svg { width: 24px; height: 24px; stroke: currentColor; fill: none; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--gold); }
.tab.fab {
  width: 52px; height: 52px; border-radius: 50%;
  background: var(--gold); color: #1a1a17 !important; margin-top: -16px;
  box-shadow: 0 3px 12px rgba(169, 133, 47, 0.35);
}
.tab.fab svg { width: 26px; height: 26px; stroke: #1a1a17; stroke-width: 2.2; }
.tab.fab.active { color: #1a1a17; }
.dot {
  position: absolute; top: 4px; right: 6px; min-width: 16px; height: 16px;
  background: var(--red); color: #fff; border-radius: 8px;
  font-size: 10px; line-height: 16px; padding: 0 4px;
}

/* Buscador y estado de bancos (Ajustes) */
.bank-row {
  display: flex; align-items: center; gap: 11px;
  padding: 11px 13px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.bank-info { flex: 1; min-width: 0; }
.bank-name { font-size: 14.5px; font-weight: 500; }
.bank-sub { font-size: 12.5px; color: var(--text-3); margin-top: 1px; }
.bank-sub.warn { color: var(--warn-text); }
.bank-state {
  flex: 0 0 auto; width: 26px; height: 26px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 600;
}
.bank-state.ok { background: rgba(29,158,117,.15); color: var(--green); }
.bank-state.warn { background: var(--warn-bg); color: var(--warn-text); }
.bank-row .chip.mini { flex: 0 0 auto; min-height: 34px; padding: 6px 12px; }
.bank-hit {
  display: block; width: 100%; text-align: left; margin: 4px 0;
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface);
}
.bank-hit:active { transform: scale(0.99); }
#bank-results { display: flex; flex-direction: column; }

/* --- Fase 2: identidad y componentes --- */
.brandbar { display: flex; align-items: center; gap: 8px; margin-bottom: 18px; color: var(--text); }
.wordmark { font-size: 15px; letter-spacing: .22em; font-weight: 500; }
.overline { text-transform: uppercase; letter-spacing: .1em; font-size: 12px; }
.big { letter-spacing: -0.02em; }

/* Toast (sustituye a alert) */
#toast-host {
  position: fixed; left: 0; right: 0; bottom: calc(84px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  z-index: 50; pointer-events: none; padding: 0 16px;
}
.toast {
  max-width: 460px; width: 100%; pointer-events: auto;
  background: var(--text); color: var(--bg);
  border-radius: 12px; padding: 12px 16px; font-size: 13.5px; line-height: 1.4;
  box-shadow: 0 6px 20px rgba(0,0,0,.22);
  display: flex; align-items: flex-start; gap: 10px;
  animation: toast-in .22s ease;
}
.toast.err { background: var(--red); color: #fff; }
.toast.ok { background: var(--green); color: #fff; }
.toast-ico { flex: 0 0 auto; margin-top: 1px; }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }

/* Hoja de confirmación (sustituye a confirm) */
.confirm-box { text-align: left; }
.confirm-box p { margin-bottom: 16px; font-size: 15px; }
.confirm-actions { display: flex; gap: 8px; }
.confirm-actions button { flex: 1; }
button.danger-fill { background: var(--red); color: #fff; border: none; font-weight: 500; }

/* Reglas de categorización (Ajustes) */
.rule-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px 12px; margin-bottom: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  font-size: 13.5px;
}
.rule-row .chip.mini { min-height: 30px; padding: 4px 10px; }
#rule-action { height: 40px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); color: inherit; padding: 0 10px; }

/* --- Patrimonio (Fase 3/4) --- */
.round-add {
  width: 40px; height: 40px; border-radius: 50%; padding: 0;
  background: var(--green); color: #fff; border: none;
  display: flex; align-items: center; justify-content: center; flex: 0 0 auto;
}
.nw-hero {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 14px 16px 4px; margin-bottom: 14px;
}
.nw-ranges { display: flex; gap: 2px; justify-content: center; margin-bottom: 10px; }
.nw-range {
  width: auto; flex: 0 0 auto; padding: 5px 11px; font-size: 12.5px;
  border: none; background: none; color: var(--text-3); border-radius: 14px;
}
.nw-range.sel { background: var(--surface-alt); color: var(--text); font-weight: 500; }
.nw-big { font-size: 34px; font-weight: 500; text-align: center; letter-spacing: -0.02em; }
.nw-change { text-align: center; font-size: 14px; font-weight: 500; margin-top: 2px; min-height: 18px; }
.nw-spark { margin-top: 8px; height: 90px; }

.nw-card {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 13px 14px; margin-bottom: 8px; cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
}
.nw-card.sel { border-color: var(--accent-text); box-shadow: 0 0 0 1px var(--accent-text); }
#nw-detail { margin-bottom: 8px; }
.nw-ico { flex: 0 0 auto; width: 40px; height: 40px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center; }
.nw-card-main { flex: 1; min-width: 0; }
.nw-card-name { font-size: 15px; font-weight: 500; }
.nw-card-val { font-size: 15px; font-weight: 500; }

/* Inversiones agrupadas (Patrimonio) */
.nw-group-head {
  display: flex; justify-content: space-between; align-items: baseline;
  margin: 16px 0 4px; font-size: 12px; text-transform: uppercase;
  letter-spacing: .08em; color: var(--text-3); font-weight: 500;
}
.nw-hold { gap: 10px; }
.advanced { margin: 10px 0 4px; border-top: 1px solid var(--border); }
.advanced > summary { cursor: pointer; list-style: none; padding: 12px 0 4px; font-size: 13px;
  color: var(--text-3); display: flex; align-items: center; gap: 7px; }
.advanced > summary::-webkit-details-marker { display: none; }
.advanced > summary::before { content: "▸"; font-size: 11px; transition: transform .15s ease; }
.advanced[open] > summary::before { transform: rotate(90deg); }
.bank-group { display: flex; align-items: center; gap: 8px; margin: 16px 0 6px; }
.bank-group-name { font-size: 14px; font-weight: 500; }
.bank-group-sub { margin-left: auto; color: var(--text-3); font-size: 13px; font-weight: 500; }
.debt-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 12px 14px; margin-bottom: 8px; }
.debt-card.tappable { cursor: pointer; }
.debt-name { font-size: 14.5px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.debt-amt { font-size: 14.5px; font-weight: 500; color: var(--red); white-space: nowrap; }
.debt-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.dchip { font-size: 11.5px; color: var(--text-3); background: var(--surface-alt);
  border-radius: 7px; padding: 2px 8px; }
#item-coin, #item-modal select {
  height: 44px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: inherit; padding: 0 12px; width: 100%;
}

/* Etiquetas de proyecto (camino, coche, vacaciones…) */
.chip.etag.selected { background: var(--gold); color: #fff; border-color: var(--gold); }
.entry-tags { margin: 3px 0 0; display: flex; flex-wrap: wrap; gap: 4px; }
.etag-pill { font-size: 11px; line-height: 1; padding: 3px 8px; border-radius: 999px;
  background: color-mix(in srgb, var(--gold) 16%, transparent); color: var(--gold); font-weight: 600; }
#add-tag-input, #edit-tag-input { margin-top: 6px; }
