/* ===== App Layout ===== */
#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---- Sidebar ---- */
#sidebar {
  width: 220px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
  overflow-x: visible;
  z-index: 100;
  position: relative;
}

/* Sidebar colapsado — oculto completamente en desktop */
#sidebar.collapsed {
  width: 0;
  overflow: hidden;
  border-right: none;
}

.sidebar-logo {
  padding: 1.25rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.625rem;
  border-bottom: 1px solid var(--border);
  min-height: 60px;
  flex-shrink: 0;
}
.sidebar-logo-icon {
  width: 32px; height: 32px;
  background: var(--accent);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}
.sidebar-logo-text {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1rem;
  color: var(--fg);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
}


.sidebar-nav { flex: 1; padding: 0.75rem 0.5rem; }

.nav-section-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--fg-muted);
  padding: 0.5rem 0.625rem 0.25rem;
  margin-top: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5625rem 0.75rem;
  border-radius: var(--radius);
  color: var(--sidebar-fg);
  font-size: 0.9375rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
  margin-bottom: 2px;
  user-select: none;
  white-space: nowrap;
}
.nav-item:hover { background: var(--surface-2); color: var(--fg); }
.nav-item.active { background: var(--sidebar-active-bg); color: var(--sidebar-active-fg); }
.nav-icon { width: 20px; display: inline-flex; justify-content: center; flex-shrink: 0; }
.ico {
  width: 18px; height: 18px; flex-shrink: 0;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.stat-label .ico, h4 .ico { width: 15px; height: 15px; vertical-align: -2px; margin-right: 4px; color: var(--fg-muted); }
.btn .ico { width: 15px; height: 15px; }
.empty-icon .ico { width: 40px; height: 40px; color: var(--fg-muted); }
.toast .ico { color: var(--fg-muted); }
.sidebar-logo-icon .ico { color: var(--on-accent); }
.login-brand-img { width: 56px; height: 56px; border-radius: 14px; flex-shrink: 0; display: block; }
.login-form { display: flex; flex-direction: column; gap: 1.5rem; }
.ma-form { display: flex; flex-direction: column; gap: 0.875rem; margin-top: 0.75rem; }
.ma-form h4, .ma-section h4 { margin-bottom: 0.25rem; }
.ma-section { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--border); }
.login-mobile-brand { display: flex; flex-direction: column; align-items: center; gap: 0.75rem; margin-bottom: 2rem; }
.login-mobile-brand img { width: 72px; height: 72px; border-radius: 18px; display: block; }
.login-mobile-brand span { font-family: var(--font-display); font-size: 1.5rem; font-weight: 700; }
.login-divider { display: flex; align-items: center; gap: 0.75rem; color: var(--fg-subtle); font-size: 0.8125rem; margin: 0.25rem 0; }
.login-divider::before, .login-divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }
@media (max-width: 768px) {
  .login-form-box input { font-size: 1rem; }
  body #login-page {
    background:
      linear-gradient(rgba(11, 31, 27, 0.7), rgba(11, 31, 27, 0.9)),
      url('/img/login-bg-mobile.webp') center / cover no-repeat fixed;
  }
  .login-right { background: transparent; align-items: flex-start; padding-top: 2.5rem; }
}

.sidebar-user {
  padding: 0.875rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

/* Overlay para mobile */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--scrim);
  z-index: 199;
}
.user-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--on-accent);
  flex-shrink: 0;
}
.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 0.875rem; font-weight: 600; color: var(--fg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 0.75rem; color: var(--fg-muted); }
.user-role-badge {
  font-size: 0.6875rem;
  padding: 0.125rem 0.4rem;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--accent-hover);
  font-weight: 600;
  text-transform: capitalize;
}

/* ---- Main content area ---- */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ---- Header ---- */
#header {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 0.875rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-shrink: 0;
}
.header-title { font-family: var(--font-display); font-size: 1.125rem; font-weight: 600; flex: 1; }
.month-selector {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.375rem 0.75rem;
  font-size: 0.9375rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--fg);
}
.header-actions { display: flex; align-items: center; gap: 0.5rem; }
.icon-btn {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--fg-muted);
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.icon-btn:hover { background: var(--surface-2); color: var(--fg); }
.theme-toggle { font-size: 1.1rem; }

/* ---- Page content ---- */
#content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
}

/* ---- Dashboard ---- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  box-shadow: var(--card-shadow);
}
.stat-label {
  font-size: 0.8125rem;
  color: var(--fg-muted);
  font-weight: 500;
  margin-bottom: 0.5rem;
}
.stat-value {
  font-variant-numeric: tabular-nums;
  font-family: var(--font-display);
  font-size: 1.75rem;
  font-weight: 700;
  line-height: 1.1;
}
.stat-sub { font-size: 0.8125rem; color: var(--fg-muted); margin-top: 0.3rem; }
.stat-hero { margin-bottom: 1rem; padding: 1.5rem 1.5rem 1.25rem; }
.stat-hero .stat-value { font-size: 2.6rem; font-variant-numeric: tabular-nums; }
.stat-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.dash-stack { display: flex; flex-direction: column; gap: 1rem; }
.stat-row-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; }

/* Bento grid */
.bento-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}
.bento-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

/* Budget section */
.budget-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0;
}
.budget-label {
  font-size: 0.875rem;
  font-weight: 500;
  min-width: 100px;
}
.budget-amounts {
  font-size: 0.8125rem;
  color: var(--fg-muted);
  min-width: 100px;
  text-align: right;
}
.budget-pct {
  font-size: 0.8125rem;
  font-weight: 600;
  min-width: 36px;
  text-align: right;
}
@media (max-width: 768px) {
  .budget-row { gap: 0.5rem; }
  .budget-label { min-width: 0; font-size: 0.8125rem; }
  .budget-amounts { min-width: 0; font-size: 0.75rem; white-space: nowrap; }
  .budget-pct { min-width: 28px; font-size: 0.75rem; }
  .bdg-cat-name { flex: 0 0 auto !important; }
}

/* ---- Section header ---- */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

/* ---- Month tabs ---- */
.tabs {
  display: flex;
  gap: 0.25rem;
  background: var(--surface-2);
  padding: 0.25rem;
  border-radius: var(--radius-lg);
  margin-bottom: 1.25rem;
  overflow-x: auto;
}
.tab {
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--fg-muted);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.tab.active {
  background: var(--surface);
  color: var(--fg);
  box-shadow: var(--shadow-sm);
}
.tab:hover:not(.active) { color: var(--fg); }

/* ---- Empty state ---- */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--fg-muted);
}
.empty-icon { font-size: 2.5rem; margin-bottom: 0.75rem; }
.empty-title { font-size: 1rem; font-weight: 600; margin-bottom: 0.375rem; color: var(--fg); }

/* ---- Login page ---- */
#login-page {
  display: flex;
  min-height: 100vh;
  background: var(--bg);
}
.login-left {
  flex: 1;
  background:
    linear-gradient(rgba(11, 31, 27, 0.62), rgba(11, 31, 27, 0.82)),
    url('/img/login-bg.webp') center / cover no-repeat;
  color: var(--fg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem 2rem;
}
.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}
.login-form-box {
  width: 100%;
  max-width: 400px;
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 2rem;
}
.login-brand-icon {
  width: 48px; height: 48px;
  background: var(--accent);
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem;
}

/* ---- Mobile layout ---- */
@media (max-width: 768px) {
  /* Drawer desde la izquierda */
  #sidebar {
    position: fixed;
    top: 0; left: 0;
    height: 100%;
    width: 260px !important;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.4,0,0.2,1);
    z-index: 200;
    border-right: 1px solid var(--border);
    /* Cancelar el collapse desktop en mobile */
  }
  #sidebar.mobile-open {
    transform: translateX(0);
  }
  #sidebar.mobile-open ~ #sidebar-overlay,
  #sidebar-overlay.visible {
    display: block;
  }

  /* En mobile, el collapsed no oculta el drawer (ya se maneja con mobile-open) */
  #sidebar.collapsed { width: 260px !important; overflow: visible !important; border-right: 1px solid var(--border) !important; }

  /* Botón hamburguesa visible en mobile */
  #mobile-menu-btn { display: flex !important; }
  /* Botón toggle de desktop oculto en mobile */
  #sidebar-toggle-btn { display: none !important; }

  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; }
  .stats-grid > .stat-card:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .stat-card { padding: 0.9rem; min-width: 0; }
  .stat-label { font-size: 0.75rem; }
  .stat-value { font-size: 1.3rem; white-space: nowrap; }
  .bento-grid { grid-template-columns: 1fr; }
  .bento-grid-3 { grid-template-columns: 1fr; }
  .stat-row-3 { grid-template-columns: 1fr; }

  .tabs { overflow-x: visible; }
  .tab { flex: 1 1 auto; min-width: 0; padding: 0.5rem 0.1rem; font-size: 0.75rem; overflow: hidden; text-overflow: ellipsis; text-align: center; }
  .tabs { gap: 0.1rem; }

  .month-grid { grid-template-columns: repeat(3, 1fr); }

  #header { padding-left: max(1rem, env(safe-area-inset-left, 0px)); padding-right: max(1rem, env(safe-area-inset-right, 0px)); padding-top: calc(0.75rem + env(safe-area-inset-top, 0px)); }
  .header-title { flex: 1 1 auto; min-width: 0; font-size: 1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .header-actions { gap: 0.4rem; flex-shrink: 0; }
  .month-selector { width: auto; white-space: nowrap; padding-left: 0.6rem; padding-right: 0.6rem; font-size: 0.8125rem; flex-shrink: 0; }
  #header-month-label { white-space: nowrap; }
  #content {
    padding: 1rem max(1rem, env(safe-area-inset-right, 0px)) calc(1rem + env(safe-area-inset-bottom, 0px)) max(1rem, env(safe-area-inset-left, 0px));
  }
  #sidebar { padding-top: env(safe-area-inset-top, 0px); }

  .login-left { display: none; }
  .login-right { padding: 1.5rem; }
}

@media (min-width: 769px) and (max-width: 1024px) {
  .stats-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .stat-value { font-size: 1.3rem; white-space: nowrap; }
  .bento-grid { grid-template-columns: 1fr 1fr; }
  #sidebar { width: 200px; }
}

.mfa-secret {
  display: block; padding: 0.75rem 0.875rem; border-radius: var(--radius);
  background: var(--surface-2); border: 1px solid var(--border);
  font-family: var(--font-display); font-size: 0.9375rem; letter-spacing: 0.06em;
  word-break: break-all; color: var(--fg); line-height: 1.8; text-align: center;
}
.check-row { display: flex; align-items: center; gap: 0.5rem; font-size: 0.875rem; color: var(--fg-muted); cursor: pointer; }
.check-row input { width: auto; }
.cat-name {
  width: 130px; font-size: 0.875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cat-amount { min-width: 80px; text-align: right; }
@media (max-width: 768px) {
  .cat-name { width: 96px; }
  .cat-amount { min-width: 64px; font-size: 0.75rem; }
}

/* ---- Month selector modal ---- */
.month-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin-top: 1rem;
}
.month-chip {
  padding: 0.5rem;
  text-align: center;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}
.month-chip:hover { background: var(--surface-2); border-color: var(--accent); }
.month-chip.exists { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-hover); }

/* ---- Notification badge ---- */
.notif-badge {
  position: absolute;
  top: -4px; right: -4px;
  width: 16px; height: 16px;
  background: var(--red);
  border-radius: 50%;
  font-size: 0.625rem;
  font-weight: 700;
  color: var(--on-accent);
  display: flex; align-items: center; justify-content: center;
}
.relative { position: relative; }

/* ---- Color category dot ---- */
.cat-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
