/* ============================================
   LAYOUT — Grid, Responsive, Navigation
   ============================================ */

/* ---- App Container ---- */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ---- App Body (header + main wrapper, sits right of sidebar on desktop) ---- */
.app-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

/* ---- Sidebar (Desktop only) ---- */
.app-sidebar {
  display: none;
}

/* ---- Top Bar / Header ---- */
.app-header {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-6);
  background: rgba(12, 16, 23, 0.72);
  background-image: var(--glass-shine);
  backdrop-filter: blur(24px) saturate(1.2);
  -webkit-backdrop-filter: blur(24px) saturate(1.2);
  border-bottom: 1px solid var(--border);
}

.app-header__logo {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.app-header__logo-icon {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--bg);
  font-weight: 700;
  font-size: 14px;
}

.app-header__title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.app-header__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-left: auto;
}

/* ---- Top Bar Search ---- */
.topbar-search {
  display: none;
  align-items: center;
  gap: var(--space-3);
  flex: 1;
  max-width: 480px;
  height: 40px;
  padding: 0 var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 180ms cubic-bezier(0.16, 1, 0.3, 1);
}

.topbar-search:hover {
  border-color: var(--border-hover);
}

.topbar-search__icon {
  color: var(--text-faint);
  flex-shrink: 0;
}

.topbar-search__text {
  font-size: 13px;
  color: var(--text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-search__kbd {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-faint);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  flex-shrink: 0;
}

/* ---- Top Bar Icon Button ---- */
.topbar-icon-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  transition: transform 180ms cubic-bezier(0.16, 1, 0.3, 1),
              color 180ms cubic-bezier(0.16, 1, 0.3, 1);
}

.topbar-icon-btn:hover {
  color: var(--accent);
  transform: scale(1.05);
}

.topbar-icon-btn:active {
  transform: scale(0.95);
}

.topbar-icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Mobile: hide logo in header on mobile (it's visible by default) */
.app-header__logo--mobile {
  display: flex;
}

/* Mobile: hide theme toggle that duplicates sidebar one */
.theme-toggle--mobile {
  display: block;
}

/* ---- Month Picker ---- */
.month-picker {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.month-picker__label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  min-width: 120px;
  text-align: center;
}

.month-picker__btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  transition: transform var(--duration-hover) var(--ease-out),
              opacity var(--duration-hover) var(--ease-out);
}

.month-picker__btn:hover {
  transform: scale(1.05);
  opacity: 0.9;
  color: var(--text);
}

.month-picker__btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.month-picker__btn:active {
  transform: scale(0.95);
}

/* ---- Main Content ---- */
.app-main {
  flex: 1;
  padding: var(--space-6);
  padding-bottom: calc(var(--space-24) + var(--space-12));
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
  overflow-x: hidden;
}

/* ---- Dashboard Grid ---- */
.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-6);
  min-width: 0;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-4);
}

.charts-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-6);
  min-width: 0;
}

.charts-row > * {
  min-width: 0;
}

/* ---- Dashboard Widget Rows ---- */
.widgets-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-6);
  min-width: 0;
}

.widgets-row > * {
  min-width: 0;
}

.widget-card {
  min-height: 0;
  min-width: 0;
  overflow: visible;
}

.widget-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-4);
}

.widget-card__title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.widget-card__action {
  font-size: 12px;
  font-weight: 500;
  color: var(--accent);
  cursor: pointer;
  background: none;
  border: none;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  transition: opacity 180ms cubic-bezier(0.16, 1, 0.3, 1);
}

.widget-card__action:hover {
  opacity: 0.8;
}

.widget-card__action:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---- Bottom Navigation ---- */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 64px;
  padding-bottom: env(safe-area-inset-bottom, 0);
  background: rgba(11, 15, 20, 0.78);
  background-image: var(--glass-shine);
  backdrop-filter: blur(24px) saturate(1.3);
  -webkit-backdrop-filter: blur(24px) saturate(1.3);
  border-top: 1px solid var(--border);
}

.bottom-nav__item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: var(--space-2) var(--space-1);
  min-width: 48px;
  min-height: 44px;
  border: none;
  background: none;
  color: var(--text-faint);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: transform var(--duration-hover) var(--ease-out),
              opacity var(--duration-hover) var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}

.bottom-nav__item:hover {
  color: var(--text-muted);
}

.bottom-nav__item:active {
  transform: scale(0.92);
}

.bottom-nav__item.active {
  color: var(--accent);
}

.bottom-nav__item.active .bottom-nav__icon {
  transform: translateY(-1px);
}

.bottom-nav__icon {
  width: 24px;
  height: 24px;
  transition: transform var(--duration-hover) var(--ease-out);
}

.bottom-nav__label {
  font-size: 10px;
  line-height: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 64px;
}

/* Very small phones: tighten nav labels */
@media (max-width: 380px) {
  .bottom-nav__label {
    font-size: 9px;
    max-width: 52px;
  }
  .bottom-nav__item {
    min-width: 44px;
  }
}

/* ---- FAB (Floating Action Button) ---- */
.fab {
  position: fixed;
  bottom: calc(64px + var(--space-4) + env(safe-area-inset-bottom, 0));
  right: var(--space-6);
  z-index: var(--z-fab);
  width: 56px;
  height: 56px;
  border-radius: var(--radius-lg);
  background: var(--accent);
  color: var(--bg);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-accent), var(--shadow-elevated);
  transition: transform var(--duration-hover) var(--ease-out),
              opacity var(--duration-hover) var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}

.fab:hover {
  transform: scale(1.08);
}

.fab:active {
  transform: scale(0.95);
}

.fab:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 3px;
}

/* Hide FAB when modal is open */
.app:has(#modalContainer:not(:empty)) .fab {
  opacity: 0;
  pointer-events: none;
}

.fab__icon {
  width: 24px;
  height: 24px;
}

/* ---- View Container ---- */
.view {
  opacity: 0;
  transform: translateY(12px);
  transition: opacity var(--duration-normal) var(--ease-out),
              transform var(--duration-normal) var(--ease-out);
}

.view.active {
  opacity: 1;
  transform: translateY(0);
}

/* ---- Section Titles ---- */
.section-title {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.03em;
  margin-bottom: var(--space-4);
  color: var(--text);
}

.section-title--with-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.section-title__action {
  font-size: 14px;
  font-weight: 500;
  color: var(--accent);
  cursor: pointer;
  background: none;
  border: none;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  transition: opacity var(--duration-hover) var(--ease-out);
}

.section-title__action:hover {
  opacity: 0.8;
}

/* ============================================
   RESPONSIVE BREAKPOINTS
   ============================================ */

/* Tablet (768px+) — Sidebar appears, bottom nav hides */
@media (min-width: 768px) {
  .app {
    flex-direction: row;
  }

  /* ---- Sidebar ---- */
  .app-sidebar {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 220px;
    z-index: calc(var(--z-sticky) + 1);
    padding: var(--space-6) var(--space-4);
    background: rgba(12, 16, 23, 0.88);
    background-image: var(--glass-shine);
    backdrop-filter: blur(24px) saturate(1.2);
    -webkit-backdrop-filter: blur(24px) saturate(1.2);
    border-right: 1px solid var(--border);
    overflow-y: auto;
  }

  .app-sidebar__logo {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 0 var(--space-3);
    margin-bottom: var(--space-8);
  }

  /* Sidebar nav */
  .sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
  }

  .sidebar-nav__item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-3);
    border: none;
    background: none;
    color: var(--text-muted);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    border-radius: var(--radius);
    min-height: 44px;
    text-align: left;
    transition: transform var(--duration-hover) var(--ease-out),
                color var(--duration-hover) var(--ease-out),
                background var(--duration-hover) var(--ease-out);
  }

  .sidebar-nav__item:hover {
    color: var(--text);
    background: var(--surface);
  }

  .sidebar-nav__item:active {
    transform: scale(0.97);
  }

  .sidebar-nav__item.active {
    color: var(--accent);
    background: var(--accent-muted);
  }

  .sidebar-nav__item:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  .sidebar-nav__icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
  }

  .app-sidebar__footer {
    margin-top: auto;
    padding: var(--space-4) var(--space-3) 0;
    border-top: 1px solid var(--border);
  }

  /* ---- App body shifts right of sidebar ---- */
  .app-body {
    margin-left: 220px;
  }

  /* ---- Top Bar adjustments ---- */
  .app-header {
    justify-content: flex-start;
  }

  /* Hide mobile logo in header on desktop (sidebar has logo) */
  .app-header__logo--mobile {
    display: none;
  }

  /* Show search bar on desktop */
  .topbar-search {
    display: flex;
  }

  /* Hide mobile theme toggle on desktop (sidebar has it) */
  .theme-toggle--mobile {
    display: none;
  }

  /* ---- Main Content ---- */
  .app-main {
    padding: var(--space-8);
    padding-bottom: var(--space-8);
  }

  .stats-row {
    grid-template-columns: repeat(4, 1fr);
    gap: var(--space-6);
  }

  .charts-row {
    grid-template-columns: repeat(2, 1fr);
  }

  .charts-row--wide {
    grid-template-columns: 2fr 1fr;
  }

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

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

  .bottom-nav {
    display: none;
  }

  .fab {
    bottom: var(--space-8);
    right: var(--space-8);
  }

  .app-header__nav {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }
}

/* Desktop (1024px+) */
@media (min-width: 1024px) {
  .app-main {
    padding: var(--space-8) var(--space-8);
  }

  .dashboard-grid {
    gap: var(--space-8);
  }

  .fab {
    width: 64px;
    height: 64px;
    border-radius: var(--radius-lg);
    bottom: var(--space-12);
    right: var(--space-12);
  }

  .fab__icon {
    width: 28px;
    height: 28px;
  }
}

/* Wide (1280px+) */
@media (min-width: 1280px) {
  .app-main {
    padding: var(--space-12);
  }
}

/* ---- Onboarding Mode: hide chrome ---- */
.app--onboarding .app-header {
  display: none;
}

.app--onboarding .app-sidebar {
  display: none;
}

.app--onboarding .bottom-nav {
  display: none;
}

.app--onboarding .fab {
  display: none;
}

.app--onboarding .app-body {
  margin-left: 0;
}

.app--onboarding .app-main {
  padding-bottom: var(--space-6);
}

/* ---- Desktop Navigation (legacy — no longer used, kept for compat) ---- */
.desktop-nav {
  display: none;
}

/* ---- Light Theme Overrides ---- */
[data-theme="light"] .app-header {
  background: rgba(247, 248, 250, 0.68);
}

[data-theme="light"] .app-sidebar {
  background: rgba(247, 248, 250, 0.88);
}

[data-theme="light"] .bottom-nav {
  background: rgba(247, 248, 250, 0.72);
}

[data-theme="light"] .fab {
  color: #FFFFFF;
}

/* ---- Mobile Dashboard Fixes ---- */
@media (max-width: 767px) {
  /* Cash flow chart header: stack title and month picker */
  .chart-card__header {
    flex-direction: column;
    gap: var(--space-2);
  }
  .chart-card__header .month-picker {
    position: static !important;
    transform: none !important;
  }

  /* Ensure charts don't overflow */
  .chart-card__canvas-wrap {
    max-width: 100%;
    overflow: hidden;
  }

  /* Health / Bills / Milestones: stack on mobile */
  .charts-row--thirds {
    grid-template-columns: 1fr !important;
  }

  /* Business page: ensure content isn't hidden behind bottom nav */
  .reveal-stagger {
    padding-bottom: var(--space-12);
  }

  /* Business tab navigation: horizontal scroll on mobile */
  .business-tabs {
    gap: var(--space-1) !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap !important;
    scrollbar-width: none;
    padding-bottom: var(--space-1);
  }
  .business-tabs::-webkit-scrollbar { display: none; }
  .business-tabs .business-tab {
    flex: 0 0 auto !important;
    padding: var(--space-2) var(--space-3) !important;
    font-size: 13px !important;
    white-space: nowrap;
  }

  /* Stat cards: scale down values & icons to fit 2-col grid */
  .stats-row {
    gap: var(--space-3);
  }
  .stats-row .stat-card {
    padding: var(--space-3);
  }
  .stats-row .stat-card__value {
    font-size: clamp(16px, 4.5vw, 20px);
  }
  .stats-row .stat-card__icon {
    width: 28px;
    height: 28px;
  }
  .stats-row .stat-card__icon svg {
    width: 14px;
    height: 14px;
  }
  .stats-row .stat-card__header {
    margin-bottom: var(--space-2);
  }

  /* Expense Breakdown: smaller values on mobile */
  .expense-breakdown__values {
    gap: var(--space-3) !important;
  }
  .expense-breakdown__value {
    font-size: 16px !important;
  }

  /* FAB: ensure it doesn't overlap content */
  .fab {
    z-index: 100;
  }

  /* Business stat cards: proper 2-column grid */
  .cc-stat-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Business bottom grid: stack on mobile */
  .cc-bottom-grid {
    grid-template-columns: 1fr !important;
  }
}
