:root {
  --bg: #0a0c11;
  --bg-elevated: #10131a;
  --card: #161a24;
  --card-2: #1d2230;
  --card-hover: #232838;
  --border: #262c3a;
  --text: #f3f4f6;
  --text-dim: #9aa1af;
  --text-faint: #6b7280;
  --accent: #60a5fa;
  --accent-2: #38bdf8;
  --accent-soft: rgba(96, 165, 250, 0.16);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.14);
  --success: #4ade80;
  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}
button, input { font-family: inherit; }
button { cursor: pointer; }
a { color: inherit; }

#app { min-height: 100%; display: flex; flex-direction: column; }

.hidden { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 18px 12px;
  background: var(--bg-elevated);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar .brand { display: flex; align-items: center; gap: 10px; }
.topbar .brand .logo {
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  display: flex; align-items: center; justify-content: center;
  color: #fff;
}
.topbar .brand h1 { font-size: 20px; margin: 0; font-weight: 700; }
.topbar .brand h1 span { color: var(--accent-2); }
.topbar-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 999px; border: none;
  background: var(--card-2); color: var(--text-dim);
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--card-hover); color: var(--text); }
.icon-btn.active { color: var(--accent); }

.back-btn {
  width: 38px; height: 38px; border-radius: 999px; border: none;
  background: var(--card-2); color: var(--text);
  display: flex; align-items: center; justify-content: center;
}

.subheader {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px 16px;
  background: var(--bg-elevated);
  position: sticky; top: 68px; z-index: 19;
}
.subheader .month-label { font-weight: 700; font-size: 16px; }

.page-header {
  display: flex; align-items: center; gap: 14px;
  padding: calc(14px + var(--safe-top)) 18px 16px;
  background: var(--bg-elevated);
  position: sticky; top: 0; z-index: 20;
}
.page-header h1 { font-size: 20px; margin: 0; font-weight: 700; }
.page-header .spacer { flex: 1; }

/* ---------- Main scroll area ---------- */
.view { flex: 1; overflow-y: auto; padding-bottom: calc(84px + var(--safe-bottom)); }
.view-inner { padding: 4px 18px 24px; }

/* ---------- Cards / sections ---------- */
.section-label {
  font-size: 12px; letter-spacing: .08em; text-transform: uppercase;
  color: var(--text-faint); margin: 22px 2px 10px; font-weight: 700;
}
.card {
  background: var(--card);
  border-radius: var(--radius-lg);
  padding: 18px;
}
.total-card {
  background: linear-gradient(160deg, #1b2434, #141824);
  border-radius: var(--radius-lg);
  padding: 20px 20px 22px;
  margin-top: 6px;
}
.total-card .label { font-size: 12px; letter-spacing: .08em; color: var(--text-faint); text-transform: uppercase; font-weight: 700; }
.total-card .amount { font-size: 34px; font-weight: 800; margin: 6px 0 2px; display: flex; align-items: baseline; gap: 2px; }
.total-card .amount .cur { color: var(--text-faint); font-size: 22px; font-weight: 700; }
.total-card .sub { color: var(--text-faint); font-size: 13px; }

/* ---------- Category rows (breakdown) ---------- */
.cat-row {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 0;
}
.cat-badge {
  width: 40px; height: 40px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.cat-row-main { flex: 1; min-width: 0; }
.cat-row-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.cat-row-top .name { font-weight: 600; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-row-top .pct { font-size: 13px; color: var(--text-dim); flex-shrink: 0; }
.cat-row-amount { font-weight: 700; font-size: 14.5px; flex-shrink: 0; text-align: right; }
.cat-row-bar { height: 5px; border-radius: 999px; background: var(--card-2); margin-top: 7px; overflow: hidden; }
.cat-row-bar > div { height: 100%; border-radius: 999px; }
.cat-row-count { color: var(--text-faint); font-size: 12.5px; margin-top: 2px; }

.pct-list .cat-row { padding: 12px 0; }
.pct-list .cat-row-top { display: flex; width: 100%; gap: 10px; }
.pct-list .name { flex: 1; }

/* ---------- Search ---------- */
.search-box {
  display: flex; align-items: center; gap: 10px;
  background: var(--card-2); border-radius: var(--radius-md);
  padding: 12px 14px; margin-top: 18px; color: var(--text-faint);
}
.search-box input {
  border: none; background: transparent; outline: none; color: var(--text);
  font-size: 14.5px; width: 100%;
}
.search-box input::placeholder { color: var(--text-faint); }

/* ---------- Expense list ---------- */
.expense-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.expense-item:last-child { border-bottom: none; }
.expense-main { flex: 1; min-width: 0; }
.expense-main .cat-name { font-weight: 600; font-size: 14.5px; }
.expense-main .meta { font-size: 12.5px; color: var(--text-faint); margin-top: 2px; }
.expense-amount { font-weight: 700; font-size: 15px; color: var(--danger); flex-shrink: 0; }

/* ---------- FAB ---------- */
.fab {
  position: fixed; right: 20px; bottom: calc(84px + var(--safe-bottom));
  width: 58px; height: 58px; border-radius: 999px; border: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #05202e; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 10px 24px rgba(56, 189, 248, .35);
  z-index: 30;
}

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: flex; background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  z-index: 25;
}
.bottom-nav button {
  flex: 1; background: none; border: none; color: var(--text-faint);
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 10px 0 8px; font-size: 11.5px; font-weight: 600;
}
.bottom-nav button.active { color: var(--accent); }

/* ---------- Buttons ---------- */
.btn {
  border: none; border-radius: var(--radius-md);
  padding: 15px 18px; font-size: 15.5px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%;
}
.btn-primary { background: linear-gradient(135deg, #4d7f97, #5c92ab); color: #0b1a22; }
.btn-secondary { background: var(--card-2); color: var(--text); }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-danger-soft { background: var(--danger-soft); color: var(--danger); }
.btn-ghost { background: var(--card-2); color: var(--accent-2); }
.btn:disabled { opacity: .5; }
.btn-sm { padding: 10px 14px; font-size: 13.5px; width: auto; }

/* ---------- Forms ---------- */
.field-label {
  font-size: 12px; letter-spacing: .08em; text-transform: uppercase;
  color: var(--text-faint); font-weight: 700; margin: 18px 2px 8px;
}
.field-label:first-child { margin-top: 0; }
.input {
  width: 100%; background: var(--card-2); border: 1px solid transparent;
  border-radius: var(--radius-md); padding: 14px 16px; color: var(--text);
  font-size: 15px; outline: none;
}
.input:focus { border-color: var(--accent); }
.input::placeholder { color: var(--text-faint); }
.amount-input-row { display: flex; align-items: center; gap: 4px; }
.amount-input-row .cur { color: var(--text-faint); font-size: 20px; font-weight: 700; }
.amount-input-row input { flex: 1; }

.field-row { display: flex; gap: 10px; align-items: center; }

/* ---------- Modals / overlays ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(4,6,10,.6);
  display: flex; align-items: flex-end; z-index: 50;
  animation: fadeIn .15s ease;
}
.overlay.center { align-items: center; padding: 20px; }
@keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
.sheet {
  background: var(--bg-elevated);
  width: 100%; max-height: 92vh; overflow-y: auto;
  border-radius: 26px 26px 0 0;
  padding: 20px 18px calc(24px + var(--safe-bottom));
}
.overlay.center .sheet {
  border-radius: var(--radius-lg); max-width: 400px; max-height: 88vh;
}
.sheet-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 18px;
}
.sheet-header h2 { font-size: 18px; margin: 0; font-weight: 700; }

/* ---------- Category grid (picker) ---------- */
.cat-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
  width: 100%;
}
.cat-grid-item {
  background: var(--card-2); border: 2px solid transparent;
  border-radius: 16px; padding: 12px 6px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  text-align: center;
  min-width: 0; /* deja que el ítem se achique dentro de su columna en vez de forzar scroll horizontal */
  width: 100%;
}
.cat-grid-item .cat-badge { width: 42px; height: 42px; flex-shrink: 0; }
.cat-grid-item .label {
  font-size: 11.5px; font-weight: 600; color: var(--text-dim);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 100%; width: 100%; min-width: 0;
}
.cat-grid-item.selected { border-color: var(--sel-color, var(--accent)); background: var(--sel-bg, var(--accent-soft)); }
.cat-grid-item.selected .label { color: var(--text); }
.cat-grid-item.add-new { border-style: dashed; border-color: var(--border); color: var(--text-faint); justify-content: center; }

/* ---------- Keypad ---------- */
.keypad-screen { display: flex; flex-direction: column; height: 100%; }
.amount-display { text-align: center; padding: 40px 20px 12px; }
.amount-display .label { font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--text-faint); font-weight: 700; }
.amount-display .value { font-size: 46px; font-weight: 800; margin-top: 8px; display: inline-flex; align-items: baseline; justify-content: center; gap: 4px; position: relative; }
.amount-display .value .cur { color: var(--text-faint); font-size: 30px; }
.amount-backspace {
  position: absolute; left: 100%; top: 50%; margin-left: 14px; transform: translateY(-50%);
  width: 34px; height: 34px; flex-shrink: 0;
}
.note-chip {
  margin: 14px auto 0; display: flex; align-items: center; gap: 8px;
  background: var(--card-2); color: var(--text-dim);
  border-radius: 999px; padding: 9px 16px; font-size: 13.5px; border: none;
  max-width: 88%;
}
.keypad { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; padding: 20px 18px; }
.keypad-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 16px; }
.keypad-grid button {
  border: none; border-radius: 16px; padding: 20px 0; font-size: 20px; font-weight: 600;
  background: var(--card-2); color: var(--text);
}
.keypad-grid button.op { background: rgba(56,189,248,.16); color: var(--accent-2); }
.keypad-grid button.equals {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #05202e; font-size: 22px; font-weight: 800;
}

/* ---------- Categorias management list ---------- */
.cat-manage-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border-radius: var(--radius-md);
  padding: 12px 14px; margin-bottom: 10px;
}
.reorder-btns { display: flex; flex-direction: column; gap: 4px; }
.reorder-btns button {
  width: 28px; height: 24px; border-radius: 8px; border: none;
  background: var(--card-2); color: var(--text-dim); display: flex; align-items: center; justify-content: center;
}
.cat-manage-row .cat-badge { width: 42px; height: 42px; }
.cat-manage-main { flex: 1; min-width: 0; }
.cat-manage-main .name { font-weight: 700; font-size: 14.5px; }
.cat-manage-main .icon-name { font-size: 12px; color: var(--text-faint); }
.row-actions { display: flex; gap: 6px; }
.row-actions button {
  width: 34px; height: 34px; border-radius: 10px; border: none;
  background: var(--card-2); color: var(--text-dim); display: flex; align-items: center; justify-content: center;
}
.row-actions button.danger { color: var(--danger); }
.helper-text { color: var(--text-faint); font-size: 12.5px; margin: 0 2px 14px; }

/* ---------- Icon / color pickers ---------- */
.picker-preview {
  display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 6px;
}
.picker-preview .cat-badge { width: 64px; height: 64px; }
.picker-preview .name { font-weight: 700; }
.picker-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.picker-btn {
  display: flex; align-items: center; gap: 10px; justify-content: center;
  background: var(--card-2); border-radius: var(--radius-md); padding: 13px; border: none; color: var(--text);
  font-weight: 600; font-size: 14px;
}
.swatch { width: 20px; height: 20px; border-radius: 999px; flex-shrink: 0; }
.icon-picker-grid, .color-picker-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; margin-top: 14px; }
.icon-picker-grid button {
  aspect-ratio: 1; border-radius: 12px; border: 2px solid transparent; background: var(--card-2);
  color: var(--text-dim); display: flex; align-items: center; justify-content: center;
}
.icon-picker-grid button.selected { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.color-picker-grid button { aspect-ratio: 1; border-radius: 999px; border: 3px solid transparent; }
.color-picker-grid button.selected { border-color: #fff; }

/* ---------- Stats / bar chart ---------- */
.bars-wrap {
  display: flex; align-items: flex-end; gap: 3px; height: 130px; padding-top: 10px;
}
.bars-wrap .bar-col { flex: 1; height: 100%; display: flex; align-items: flex-end; position: relative; }
.bars-wrap .bar { width: 100%; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; }
.bars-axis { display: flex; justify-content: space-between; color: var(--text-faint); font-size: 11.5px; margin-top: 6px; }

.expandable .cat-row { cursor: pointer; }
.expand-detail { padding: 4px 0 10px 52px; display: none; }
.expand-detail.open { display: block; }
.expand-detail .mini-expense { display: flex; justify-content: space-between; font-size: 13px; color: var(--text-dim); padding: 5px 0; }

/* ---------- Toggle switch ---------- */
.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 4px 0;
}
.switch-row .txt .title { font-weight: 700; font-size: 14.5px; }
.switch-row .txt .desc { font-size: 12.5px; color: var(--text-faint); margin-top: 2px; line-height: 1.4; }
.switch {
  width: 46px; height: 27px; border-radius: 999px; background: var(--card-2);
  border: none; position: relative; flex-shrink: 0; transition: background .15s;
}
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 21px; height: 21px;
  border-radius: 999px; background: #fff; transition: transform .15s;
}
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(19px); }

/* ---------- Datos screen ---------- */
.data-card { background: var(--card); border-radius: var(--radius-lg); padding: 18px; margin-bottom: 18px; }
.data-card .head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.data-card .head .ico { color: var(--accent-2); }
.data-card .head h3 { font-size: 15.5px; margin: 0; font-weight: 700; }
.data-card p { color: var(--text-dim); font-size: 13.5px; line-height: 1.5; margin: 0 0 16px; }
.data-card.import .head .ico { color: #c084fc; }

/* ---------- Login ---------- */
.login-screen {
  min-height: 100%; display: flex; flex-direction: column; justify-content: center;
  padding: 30px 26px calc(30px + var(--safe-bottom));
}
.login-logo { text-align: center; margin-bottom: 26px; }
.login-logo .mark {
  width: 66px; height: 66px; border-radius: 20px; margin: 0 auto 14px;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  display: flex; align-items: center; justify-content: center; color: #fff;
}
.login-logo h1 { font-size: 26px; margin: 0; font-weight: 800; }
.login-logo h1 span { color: var(--accent-2); }
.login-logo p { color: var(--text-faint); font-size: 13.5px; margin-top: 6px; }
.login-tabs { display: flex; background: var(--card-2); border-radius: 999px; padding: 4px; margin-bottom: 22px; }
.login-tabs button {
  flex: 1; border: none; background: none; color: var(--text-faint); padding: 10px; border-radius: 999px;
  font-weight: 700; font-size: 13.5px;
}
.login-tabs button.active { background: var(--accent); color: #071019; }
.login-error {
  background: var(--danger-soft); color: var(--danger); border-radius: var(--radius-sm);
  padding: 11px 14px; font-size: 13.5px; margin-bottom: 14px;
}
.login-form .field-label { margin-top: 16px; }
.login-form .field-label:first-child { margin-top: 0; }
.login-submit { margin-top: 22px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-bottom)); transform: translateX(-50%);
  background: #1f2430; color: var(--text); padding: 12px 18px; border-radius: 999px;
  font-size: 13.5px; z-index: 90; box-shadow: 0 10px 30px rgba(0,0,0,.4);
  display: flex; align-items: center; gap: 8px; max-width: 88vw;
}
.toast.error { color: var(--danger); }
.toast.success .ico { color: var(--success); }

.empty-state { text-align: center; color: var(--text-faint); padding: 40px 10px; font-size: 14px; }

.spinner {
  width: 22px; height: 22px; border-radius: 999px; border: 3px solid var(--card-2);
  border-top-color: var(--accent); animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.center-loading { display: flex; align-items: center; justify-content: center; height: 100%; padding: 60px; }

::-webkit-scrollbar { width: 0; height: 0; }

/* ---------- Fullscreen modal (Nuevo Gasto / Editar Gasto) ---------- */
.fullscreen-modal {
  position: fixed; inset: 0; background: var(--bg); z-index: 60;
  display: flex; flex-direction: column;
}
.fullscreen-modal .fs-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 18px 14px; background: var(--bg-elevated);
}
.fullscreen-modal .fs-header h2 { font-size: 17px; margin: 0; font-weight: 700; }
.fullscreen-modal .fs-body { flex: 1; overflow-y: auto; padding: 4px 18px calc(24px + var(--safe-bottom)); }
