/* ============== Variables tema ============== */
:root[data-theme="dark"] {
  --bg: #0f1320;
  --bg2: #0a0e1a;
  --panel: #161b2d;
  --panel2: #1d2440;
  --panel3: #232a4a;
  --line: #2a3258;
  --text: #e6e9f5;
  --text-muted: #8a93b3;
  --accent: #6aa6ff;
  --accent-soft: rgba(106,166,255,0.15);
  --good: #2ecc71;
  --bad: #ff6b6b;
  --warn: #f39c12;
  --shadow: 0 2px 10px rgba(0,0,0,0.25);
}
:root[data-theme="light"] {
  --bg: #f7f8fb;
  --bg2: #eef0f5;
  --panel: #ffffff;
  --panel2: #f4f5f9;
  --panel3: #e8ebf2;
  --line: #dfe3ec;
  --text: #1f2540;
  --text-muted: #6b7390;
  --accent: #2563eb;
  --accent-soft: rgba(37,99,235,0.10);
  --good: #15803d;
  --bad: #dc2626;
  --warn: #d97706;
  --shadow: 0 1px 4px rgba(20,30,60,0.07);
}
:root { color-scheme: dark light; }

/* ============== Base ============== */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text);
  font-family: 'Inter', -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  font-feature-settings: 'cv11', 'ss01', 'tnum'; }
html { -webkit-tap-highlight-color: transparent; }
a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; background: var(--accent); color: #fff; border: 0;
  padding: 8px 14px; border-radius: 10px; cursor: pointer; font-weight: 600; font-size: 13px;
  transition: filter .15s, transform .05s; }
button:hover { filter: brightness(1.08); }
button:active { transform: translateY(1px); }
button.secondary { background: var(--panel2); color: var(--text); border: 1px solid var(--line); }
button.ghost { background: transparent; color: var(--text); }
button.danger { background: var(--bad); }
button.small { padding: 5px 10px; font-size: 12px; }
button:disabled { opacity: .5; cursor: not-allowed; }
input, select, textarea { font-family: inherit; background: var(--panel2); border: 1px solid var(--line);
  color: var(--text); padding: 8px 11px; border-radius: 10px; outline: none; font-size: 13px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; }
input.inline-edit { padding: 4px 6px; min-width: 140px; }
.acc-name-cell { display: flex; align-items: center; gap: 6px; }
.acc-name-cell .acc-rename-btn { opacity: 0.4; transition: opacity .15s; flex-shrink: 0; }
.acc-name-cell:hover .acc-rename-btn { opacity: 1; }
.muted { color: var(--text-muted); font-size: 13px; }
.error { color: var(--bad); margin-top: 8px; min-height: 18px; }
.hidden { display: none !important; }
.row { display: flex; gap: 8px; align-items: center; }
.r { text-align: right; font-variant-numeric: tabular-nums; }

/* ============== Auth screens ============== */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.auth-card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; width: 380px; max-width: 100%; box-shadow: var(--shadow); }
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-card label { display: block; margin-top: 14px; font-size: 12px; color: var(--text-muted); }
.auth-card input { width: 100%; margin-top: 4px; }
.auth-card button { width: 100%; margin-top: 18px; padding: 11px; }

/* ============== Layout: sidebar + topbar + main ============== */
.app { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; transition: width .2s; }
.sidebar .logo { display: flex; align-items: center; gap: 10px; padding: 16px 18px; font-weight: 700; font-size: 16px; border-bottom: 1px solid var(--line); }
.sidebar .logo .e { display: inline-block; width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-weight: 800; }
.sidebar nav { display: flex; flex-direction: column; padding: 8px; gap: 2px; flex: 1; overflow-y: auto; }
.sidebar nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px; color: var(--text-muted); font-size: 13px; font-weight: 500; cursor: pointer; }
.sidebar nav a:hover { background: var(--panel2); color: var(--text); }
.sidebar nav a.active { background: var(--accent-soft); color: var(--accent); }
.sidebar nav a .ic { width: 18px; text-align: center; font-size: 15px; }
.sidebar .footer { border-top: 1px solid var(--line); padding: 10px 14px; display: flex; align-items: center; justify-content: space-between; font-size: 12px; color: var(--text-muted); }
.sidebar .footer button.ghost { padding: 6px 8px; font-size: 12px; }

.app.collapsed { grid-template-columns: 64px 1fr; }
.app.collapsed .sidebar nav a { justify-content: center; padding: 10px; }
.app.collapsed .sidebar nav a .label { display: none; }
.app.collapsed .sidebar .logo .title,
.app.collapsed .sidebar .footer .user { display: none; }

.topbar { display: flex; align-items: center; gap: 12px; padding: 14px 22px; background: var(--bg); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 9; }
.topbar h2 { margin: 0; font-size: 17px; font-weight: 600; }
.topbar .right { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.topbar .theme-toggle, .topbar .menu-toggle { background: var(--panel); border: 1px solid var(--line); color: var(--text); padding: 7px 10px; }
.topbar .topbar-toggle { display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; font-size: 12px; color: var(--text); cursor: pointer; user-select: none; white-space: nowrap; transition: background .12s, border-color .12s; }
.topbar .topbar-toggle:hover { background: var(--panel2); }
.topbar .topbar-toggle input[type=checkbox] { width: 14px; height: 14px; margin: 0; cursor: pointer; }
.topbar .topbar-toggle:has(input:checked) { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
@media (max-width: 700px) { .topbar .topbar-toggle span { display: none; } }

.main-wrap { display: flex; flex-direction: column; min-width: 0; }
main { padding: 22px 22px 100px; max-width: 1500px; width: 100%; align-self: center; }
.tab { display: block; animation: fadeIn .18s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px);} to { opacity: 1; transform: none;} }

/* ============== KPIs ============== */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 18px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.kpi .lbl { color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
.kpi .val { display: block; font-size: 22px; margin-top: 6px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .val.pos { color: var(--good); }
.kpi .val.neg { color: var(--bad); }
.kpi .delta { display: inline-block; margin-left: 6px; font-size: 11px; padding: 2px 6px; border-radius: 6px; font-weight: 600; }
.kpi .delta.up { background: rgba(220,38,38,0.15); color: var(--bad); }
.kpi .delta.down { background: rgba(46,204,113,0.15); color: var(--good); }
.kpi .sub { display: block; margin-top: 4px; font-size: 11px; color: var(--text-muted); }

/* ============== Cards y grid ============== */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 14px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; margin-bottom: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); }
.card h3 { margin: 0 0 12px; font-size: 13px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 700; display: flex; justify-content: space-between; align-items: center; }
.card h3 .actions { display: flex; gap: 6px; }
canvas { max-width: 100%; }
.chart-box { position: relative; height: 280px; width: 100%; }
.chart-box.lg { height: 340px; }
.chart-box.sm { height: 230px; }
.chart-box.doughnut { height: 300px; }

/* ───── KPI mini header debajo de cards de chart ───── */
.chart-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin: -4px 0 10px; }
.chart-head .big { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.chart-head .big.pos { color: var(--good); }
.chart-head .big.neg { color: var(--bad); }
.chart-head .sub { font-size: 11px; color: var(--text-muted); text-align: right; }
.chart-head .sub b { color: var(--text); font-weight: 600; }

/* ───── Leyenda custom para doughnut ───── */
.cat-legend { display: grid; grid-template-columns: 1fr; gap: 6px; font-size: 12px; max-height: 270px; overflow-y: auto; padding-right: 4px; }
.cat-legend .item { display: grid; grid-template-columns: 16px 1fr auto auto; gap: 8px; align-items: center; padding: 4px 6px; border-radius: 6px; transition: background .12s; }
.cat-legend .item:hover { background: var(--panel2); }
.cat-legend .item .sw { width: 12px; height: 12px; border-radius: 4px; }
.cat-legend .item .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cat-legend .item .pc { color: var(--text-muted); font-variant-numeric: tabular-nums; font-size: 11px; }
.cat-legend .item .am { color: var(--bad); font-weight: 600; font-variant-numeric: tabular-nums; }

.doughnut-wrap { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: center; }
@media (max-width: 700px) { .doughnut-wrap { grid-template-columns: 1fr; } }

/* ============== Tabla ============== */
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table th { color: var(--text-muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
.table tr.row-click { cursor: pointer; }
.table tr.row-click:hover { background: var(--panel2); }
.table tr.selected { background: var(--accent-soft); }
.amount-neg { color: var(--bad); font-weight: 600; }
.amount-pos { color: var(--good); font-weight: 600; }
.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; color: #fff; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: middle; margin-right: 6px; }
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 600; background: var(--panel2); border: 1px solid var(--line); }
.pill.cat { color: #fff; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 600; cursor: pointer; border: 1px solid transparent; }
.chip .x { font-size: 14px; cursor: pointer; opacity: .7; }
.chip.tag-pill { background: var(--panel3); color: var(--text); }
.badge { display: inline-flex; align-items: center; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: 11px; font-weight: 600; vertical-align: middle; }

.toolbar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.toolbar input[type=search] { flex: 1; min-width: 220px; }
.row-form { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.row-form input, .row-form select { flex: 1; min-width: 140px; }

/* ============== Drawer ============== */
.drawer-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 90; opacity: 0; pointer-events: none; transition: opacity .2s; }
.drawer-overlay.open { opacity: 1; pointer-events: auto; }
.drawer { position: fixed; right: 0; top: 0; height: 100vh; width: 480px; max-width: 100vw; background: var(--panel); border-left: 1px solid var(--line); z-index: 91; transform: translateX(100%); transition: transform .25s ease; overflow-y: auto; box-shadow: -8px 0 24px rgba(0,0,0,0.2); }
.drawer.open { transform: none; }
.drawer header { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--panel); z-index: 2; }
.drawer header h3 { margin: 0; font-size: 16px; }
.drawer .body { padding: 18px 20px 60px; }
.drawer .body section { margin-bottom: 20px; }
.drawer .body section h4 { margin: 0 0 8px; font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; }
.drawer .kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; font-size: 13px; }
.drawer .kv .k { color: var(--text-muted); }
.drawer .amount-big { font-size: 32px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ============== Toasts ============== */
.toasts { position: fixed; bottom: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 95; max-width: 90vw; }
.toast { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 10px; padding: 12px 14px; box-shadow: var(--shadow); min-width: 240px; display: flex; align-items: flex-start; gap: 10px; font-size: 13px; animation: slideIn .25s ease; }
.toast.success { border-left-color: var(--good); }
.toast.warn { border-left-color: var(--warn); }
.toast.error { border-left-color: var(--bad); }
.toast .x { margin-left: auto; cursor: pointer; opacity: .6; }
.toast .actions { display: flex; gap: 6px; margin-top: 6px; }
@keyframes slideIn { from { opacity: 0; transform: translateY(10px);} to { opacity: 1; transform: none; } }

/* ============== Insight tira ============== */
.insights { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 10px; margin-bottom: 16px; }
.insight { display: flex; gap: 10px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); font-size: 13px; align-items: flex-start; }
.insight .ic { font-size: 22px; line-height: 1; }
.insight.good { border-left: 3px solid var(--good); }
.insight.warn { border-left: 3px solid var(--warn); }
.insight.info { border-left: 3px solid var(--accent); }

/* ============== Calendario heatmap ============== */
.calendar { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.calendar .dow { font-size: 10px; color: var(--text-muted); text-align: center; text-transform: uppercase; padding: 4px; font-weight: 600; }
.calendar .cell { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; min-height: 64px; padding: 6px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; font-size: 11px; transition: transform .1s; }
.calendar .cell:hover { transform: scale(1.03); border-color: var(--accent); }
.calendar .cell .d { font-weight: 600; font-size: 12px; }
.calendar .cell .v { font-variant-numeric: tabular-nums; }
.calendar .cell.out { opacity: .2; pointer-events: none; }
.calendar .cell.today { outline: 2px solid var(--accent); }
.calendar-nav { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.calendar-nav h2 { margin: 0; font-size: 18px; min-width: 200px; text-align: center; text-transform: capitalize; }

/* ============== Subscriptions ============== */
.subs-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.sub-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px; box-shadow: var(--shadow); }
.sub-card.inactive { opacity: .55; }
.sub-card .top { display: flex; align-items: center; gap: 10px; }
.sub-card .icon { width: 38px; height: 38px; border-radius: 10px; background: var(--panel2); display: flex; align-items: center; justify-content: center; font-size: 20px; }
.sub-card .name { font-weight: 600; font-size: 14px; }
.sub-card .freq { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.sub-card .amt { font-size: 22px; font-weight: 700; margin: 8px 0 4px; color: var(--bad); }
.sub-card .meta { font-size: 11px; color: var(--text-muted); }
.sub-card .actions { margin-top: 8px; display: flex; gap: 6px; }

/* ============== Budgets ============== */
.budget-row { display: grid; grid-template-columns: 1fr 80px 110px 110px; gap: 12px; align-items: center; padding: 10px; border-bottom: 1px solid var(--line); }
.budget-row .name { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px; }
.budget-bar { height: 8px; background: var(--panel2); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.budget-bar > div { height: 100%; background: var(--good); transition: width .3s; }
.budget-bar.warn > div { background: var(--warn); }
.budget-bar.danger > div { background: var(--bad); }

/* ============== Import dropzone ============== */
.dropzone { border: 2px dashed var(--line); border-radius: 14px; padding: 36px; text-align: center; cursor: pointer; background: var(--panel2); transition: all .15s; }
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone p { margin: 6px 0; }
.dropzone .icon { font-size: 36px; }

/* ============== Bottom nav móvil ============== */
.bottom-nav { display: none; position: fixed; bottom: 0; left: 0; right: 0; background: var(--panel); border-top: 1px solid var(--line); padding: 6px 0 calc(6px + env(safe-area-inset-bottom)); z-index: 10; }
.bottom-nav a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px; color: var(--text-muted); font-size: 10px; cursor: pointer; }
.bottom-nav a.active { color: var(--accent); }
.bottom-nav a .ic { font-size: 18px; }

/* ============== Empty state ============== */
.empty { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty .ic { font-size: 48px; margin-bottom: 12px; opacity: .5; }
.empty h3 { color: var(--text); font-size: 16px; margin: 4px 0; }

/* ============== Skeleton ============== */
.skeleton { background: linear-gradient(90deg, var(--panel2) 0%, var(--panel3) 50%, var(--panel2) 100%); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 6px; }
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ============== Responsive ============== */
@media (max-width: 1300px) {
  .kpis { grid-template-columns: repeat(4, 1fr); }
}
@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .bottom-nav { display: flex; }
  main { padding: 16px 14px 90px; }
  .topbar { padding: 12px 14px; gap: 8px; }
  .topbar h2 { font-size: 15px; }
  .drawer { width: 100vw; }
}
@media (min-width: 1600px) {
  .kpis { grid-template-columns: repeat(8, 1fr); }
}

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 14px; padding: 22px; width: 460px; max-width: 95vw; box-shadow: 0 20px 60px rgba(0,0,0,0.4); }
dialog::backdrop { background: rgba(0,0,0,0.5); backdrop-filter: blur(2px); }
dialog label { display: block; margin: 10px 0; font-size: 12px; color: var(--text-muted); }
dialog input, dialog select, dialog textarea { width: 100%; margin-top: 4px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin-top: 18px; }

/* ─── Reglas agrupadas por categoría ──────────────────────────────── */
.rule-group { padding: 16px 18px; }
.rule-list { display: flex; flex-direction: column; gap: 4px; }
.rule-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
  background: var(--bg); border: 1px solid transparent;
  transition: border-color 0.15s;
}
.rule-row:hover { border-color: var(--line); }
.rule-sign {
  font-size: 11px; padding: 2px 8px; border-radius: 6px;
  background: var(--panel); color: var(--text-muted);
  white-space: nowrap; min-width: 64px; text-align: center;
}
.rule-pattern {
  flex: 1; font-size: 12px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-family: 'Cascadia Code', 'Consolas', monospace;
}
.rule-prio {
  font-size: 11px; padding: 2px 8px; border-radius: 6px;
  background: var(--accent-soft, #6aa6ff22); color: var(--accent, #6aa6ff);
  font-weight: 600;
}

/* ============== Menú móvil slide-up ============== */
.mobile-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 20; }
.mobile-overlay.open { display: block; }
.mobile-menu { position: fixed; bottom: 0; left: 0; right: 0; background: var(--panel); border-radius: 18px 18px 0 0; border-top: 1px solid var(--line); z-index: 21; transform: translateY(100%); transition: transform .25s cubic-bezier(.32,.72,0,1); padding: 0 16px calc(16px + env(safe-area-inset-bottom)); box-shadow: 0 -8px 30px rgba(0,0,0,0.2); }
.mobile-menu.open { transform: translateY(0); }
.mobile-menu-handle { width: 40px; height: 4px; background: var(--line); border-radius: 4px; margin: 12px auto 16px; }
.mobile-menu-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; padding-bottom: 4px; }
.mobile-menu-grid a { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 14px 8px; border-radius: 14px; background: var(--panel2); font-size: 12px; color: var(--text); cursor: pointer; border: 1px solid var(--line); transition: background .12s; }
.mobile-menu-grid a:active { background: var(--panel3); }
.mobile-menu-grid a .ic { font-size: 24px; }

/* ─── Chips de categoría y vecinos K-NN ───────────────────────────── */
.cat-chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 8px; border-radius: 12px;
  font-size: 12px; font-weight: 500; white-space: nowrap;
}
.neigh-chip {
  display: inline-block; padding: 3px 8px; margin: 2px 4px 2px 0;
  border-radius: 4px; background: var(--bg); border: 1px solid var(--line);
  font-size: 11px; font-family: 'Cascadia Code', monospace;
  max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  vertical-align: middle;
}
