/* ============================================================================
   MONEY MAP — LAYOUT SYSTEM
   File: css/layout.css
   Description: Structural layout for the entire application. Defines the
   page grid, sidebar, header, main content area, bottom navigation, and
   responsive behaviour. Mobile-first: base styles are for mobile, with
   min-width media queries for tablet and desktop.
   Load order: tokens.css → reset.css → layout.css → components.css → views.css
   ============================================================================ */


/* ============================================================================
   1. APP SHELL
   The outermost container that holds the entire application.
   On mobile: full-width, with bottom navigation.
   On desktop: sidebar on the left, content on the right.
   ============================================================================ */

.app-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    width: 100%;
    background-color: var(--bg-main);
}


/* ============================================================================
   2. HEADER
   Fixed top bar with the Money Map logo, page title, and user actions.
   Visible on all screen sizes.
   ============================================================================ */

.app-header {
    position: sticky;
    top: 0;
    z-index: var(--z-sticky);
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height);
    padding: 0 var(--space-md);
    background-color: var(--bg-surface);
    border-bottom: 1px solid var(--border-default);
    box-shadow: var(--shadow-xs);
}

.app-header__brand {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.app-header__logo {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--brand-primary);
    color: var(--text-inverse);
    border-radius: var(--radius-md);
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-bold);
}

.app-header__title {
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-bold);
    color: var(--text-primary);
    letter-spacing: var(--letter-spacing-tight);
}

.app-header__actions {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.app-header__page-title {
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
}

/* Hide the page title in header on mobile — it appears in the view itself */
@media (max-width: 639px) {
    .app-header__page-title {
        display: none;
    }
}


/* ============================================================================
   3. MAIN LAYOUT CONTAINER
   Wraps the sidebar and main content area.
   On mobile: single column, full width.
   On desktop: sidebar + content side by side.
   ============================================================================ */

.app-layout {
    display: flex;
    flex: 1;
    flex-direction: column;
    width: 100%;
}


/* ============================================================================
   4. SIDEBAR NAVIGATION (Desktop Only)
   Hidden on mobile. Appears on the left side on screens 1024px and wider.
   Contains the main navigation links, user profile summary, and sign-out.
   ============================================================================ */

.app-sidebar {
    display: none; /* Hidden on mobile by default */
}

@media (min-width: 1024px) {
    .app-sidebar {
        display: flex;
        flex-direction: column;
        position: fixed;
        top: var(--header-height);
        left: 0;
        bottom: 0;
        width: var(--sidebar-width);
        background-color: var(--bg-surface);
        border-right: 1px solid var(--border-default);
        padding: var(--space-md) 0;
        overflow-y: auto;
        z-index: var(--z-sticky);
    }
}

.app-sidebar__nav {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: 0 var(--space-sm);
    flex: 1;
}

.app-sidebar__section-label {
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: var(--letter-spacing-wider);
    padding: var(--space-md) var(--space-md) var(--space-sm);
}

.app-sidebar__link {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    color: var(--text-secondary);
    transition: all var(--transition-fast);
    min-height: var(--touch-target);
}

.app-sidebar__link:hover {
    background-color: var(--bg-hover);
    color: var(--text-primary);
}

.app-sidebar__link--active {
    background-color: var(--brand-primary-lighter);
    color: var(--brand-primary);
    font-weight: var(--font-weight-semibold);
}

.app-sidebar__link--active:hover {
    background-color: var(--brand-primary-light);
}

.app-sidebar__link i {
    width: 20px;
    text-align: center;
    font-size: var(--font-size-base);
}

.app-sidebar__divider {
    height: 1px;
    background-color: var(--border-default);
    margin: var(--space-sm) var(--space-md);
}

.app-sidebar__footer {
    padding: var(--space-md);
    border-top: 1px solid var(--border-default);
    margin-top: auto;
}

.app-sidebar__user {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-sm);
    border-radius: var(--radius-md);
}

.app-sidebar__avatar {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    background-color: var(--brand-primary-light);
    color: var(--brand-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-bold);
}

.app-sidebar__user-info {
    flex: 1;
    min-width: 0;
}

.app-sidebar__user-name {
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-sidebar__user-email {
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}


/* ============================================================================
   5. MAIN CONTENT AREA
   The primary content region where views are rendered.
   On mobile: full width, with padding for bottom navigation.
   On desktop: offset to the right of the sidebar.
   ============================================================================ */

.app-main {
    flex: 1;
    width: 100%;
    padding: var(--space-md);
    padding-bottom: calc(var(--bottom-nav-height) + var(--space-md)); /* Space for bottom nav on mobile */
    max-width: 100%;
    overflow-x: hidden;
}

@media (min-width: 640px) {
    .app-main {
        padding: var(--space-lg);
        padding-bottom: calc(var(--bottom-nav-height) + var(--space-lg));
    }
}

@media (min-width: 1024px) {
    .app-main {
        margin-left: var(--sidebar-width);
        padding: var(--space-lg) var(--space-xl);
        padding-bottom: var(--space-xl); /* No bottom nav on desktop */
        max-width: calc(100% - var(--sidebar-width));
    }
}

@media (min-width: 1280px) {
    .app-main {
        padding: var(--space-xl) var(--space-2xl);
    }
}


/* ============================================================================
   6. BOTTOM NAVIGATION (Mobile Only)
   Fixed to the bottom of the screen on mobile devices.
   Hidden on desktop where the sidebar is used instead.
   Maximum 5 items for usability.
   ============================================================================ */

.app-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: var(--bottom-nav-height);
    background-color: var(--bg-surface);
    border-top: 1px solid var(--border-default);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.04);
    padding-bottom: env(safe-area-inset-bottom, 0); /* iPhone notch support */
}

@media (min-width: 1024px) {
    .app-bottom-nav {
        display: none; /* Hidden on desktop */
    }
}

.app-bottom-nav__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    flex: 1;
    min-height: var(--touch-target);
    padding: var(--space-xs) 0;
    color: var(--text-muted);
    font-size: var(--font-size-2xs);
    font-weight: var(--font-weight-medium);
    transition: color var(--transition-fast);
    text-decoration: none;
    -webkit-tap-highlight-color: transparent;
}

.app-bottom-nav__item i {
    font-size: var(--font-size-xl);
    line-height: 1;
}

.app-bottom-nav__item--active {
    color: var(--brand-primary);
}

.app-bottom-nav__item--active i {
    font-weight: 900; /* Solid icon variant for active state */
}

.app-bottom-nav__item:hover {
    color: var(--brand-primary);
}

/* Centre action button (e.g., "Add Transaction") */
.app-bottom-nav__action {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    background-color: var(--brand-primary);
    color: var(--text-inverse);
    border-radius: var(--radius-full);
    font-size: var(--font-size-xl);
    margin-top: -12px; /* Float above the nav bar */
    box-shadow: var(--shadow-md);
    transition: all var(--transition-fast);
}

.app-bottom-nav__action:hover {
    background-color: var(--brand-primary-hover);
    transform: scale(1.05);
}


/* ============================================================================
   7. VIEW CONTAINERS
   Each "page" or "screen" in the application is a view.
   Only one view is visible at a time (controlled by JavaScript).
   ============================================================================ */

.view {
    display: none;
    animation: viewFadeIn var(--transition-base) ease;
}

.view--active {
    display: block;
}

@keyframes viewFadeIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.view__header {
    margin-bottom: var(--space-lg);
}

.view__title {
    font-size: var(--font-size-2xl);
    font-weight: var(--font-weight-bold);
    color: var(--text-primary);
    letter-spacing: var(--letter-spacing-tight);
    line-height: var(--line-height-tight);
}

@media (min-width: 640px) {
    .view__title {
        font-size: var(--font-size-3xl);
    }
}

.view__subtitle {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    margin-top: var(--space-xs);
}

.view__toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-sm);
    margin-top: var(--space-md);
    flex-wrap: wrap;
}


/* ============================================================================
   8. CONTENT CONTAINERS
   Wrappers for grouping related content within a view.
   ============================================================================ */

.content-container {
    max-width: var(--max-width-xl);
    margin: 0 auto;
    width: 100%;
}

.content-container--narrow {
    max-width: var(--max-width-sm);
}

.content-container--medium {
    max-width: var(--max-width-md);
}


/* ============================================================================
   9. RESPONSIVE GRIDS
   Auto-filling grids that adapt to screen size without explicit breakpoints.
   Cards automatically wrap to new rows when there is not enough space.
   ============================================================================ */

/* Standard card grid — used for dashboard widgets, stat cards, etc. */
.grid {
    display: grid;
    gap: var(--space-md);
    grid-template-columns: 1fr;
}

@media (min-width: 640px) {
    .grid {
        grid-template-columns: repeat(2, 1fr);
        gap: var(--space-lg);
    }
}

@media (min-width: 1024px) {
    .grid {
        grid-template-columns: repeat(auto-fill, minmax(var(--card-min-width), 1fr));
    }
}

/* Two-column grid — for forms, split layouts */
.grid--2col {
    grid-template-columns: 1fr;
}

@media (min-width: 640px) {
    .grid--2col {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Three-column grid — for stat cards on dashboard */
.grid--3col {
    grid-template-columns: 1fr;
}

@media (min-width: 640px) {
    .grid--3col {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1024px) {
    .grid--3col {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* Four-column grid — for small stat cards */
.grid--4col {
    grid-template-columns: repeat(2, 1fr);
}

@media (min-width: 640px) {
    .grid--4col {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1024px) {
    .grid--4col {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* Auto-fill grid — cards fill available space automatically */
.grid--auto {
    grid-template-columns: repeat(auto-fill, minmax(var(--card-min-width), 1fr));
}

/* Stacked layout — single column, no wrapping */
.stack {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
}

.stack--sm {
    gap: var(--space-sm);
}

.stack--lg {
    gap: var(--space-lg);
}


/* ============================================================================
   10. SECTION SPACING
   Consistent vertical spacing between major sections within a view.
   ============================================================================ */

.section {
    margin-bottom: var(--space-lg);
}

@media (min-width: 640px) {
    .section {
        margin-bottom: var(--space-xl);
    }
}

.section__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-md);
}

.section__title {
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
}

.section__action {
    font-size: var(--font-size-sm);
    color: var(--brand-primary);
    font-weight: var(--font-weight-medium);
}


/* ============================================================================
   11. AUTH LAYOUT
   Special centred layout for sign-in, sign-up, and onboarding screens.
   No sidebar or bottom navigation.
   ============================================================================ */

.auth-layout {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: var(--space-md);
    background-color: var(--bg-main);
}

.auth-layout__container {
    width: 100%;
    max-width: 420px;
}

.auth-layout__brand {
    text-align: center;
    margin-bottom: var(--space-xl);
}

.auth-layout__logo {
    width: 56px;
    height: 56px;
    margin: 0 auto var(--space-md);
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--brand-primary);
    color: var(--text-inverse);
    border-radius: var(--radius-lg);
    font-size: var(--font-size-2xl);
    font-weight: var(--font-weight-bold);
}

.auth-layout__heading {
    font-size: var(--font-size-2xl);
    font-weight: var(--font-weight-bold);
    color: var(--text-primary);
}

.auth-layout__subheading {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    margin-top: var(--space-xs);
}


/* ============================================================================
   12. ONBOARDING LAYOUT
   Step-by-step onboarding flow with progress indicator.
   ============================================================================ */

.onboarding-layout {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: var(--space-md);
    background-color: var(--bg-main);
}

.onboarding-layout__container {
    width: 100%;
    max-width: 520px;
}

.onboarding-layout__progress {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-bottom: var(--space-xl);
    justify-content: center;
}

.onboarding-layout__step {
    width: 32px;
    height: 4px;
    border-radius: var(--radius-full);
    background-color: var(--border-default);
    transition: background-color var(--transition-base);
}

.onboarding-layout__step--active {
    background-color: var(--brand-primary);
}

.onboarding-layout__step--complete {
    background-color: var(--brand-primary);
}


/* ============================================================================
   13. LOADING & EMPTY STATE LAYOUTS
   Centred layouts for loading spinners and empty state messages.
   ============================================================================ */

.loading-layout {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 300px;
    gap: var(--space-md);
    color: var(--text-secondary);
}

.loading-layout__spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-default);
    border-top-color: var(--brand-primary);
    border-radius: var(--radius-full);
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.empty-layout {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: var(--space-2xl) var(--space-md);
    min-height: 300px;
    gap: var(--space-md);
}

.empty-layout__icon {
    font-size: 3rem;
    color: var(--text-muted);
    opacity: 0.5;
}

.empty-layout__title {
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
}

.empty-layout__message {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    max-width: 360px;
    line-height: var(--line-height-relaxed);
}


/* ============================================================================
   14. SPLIT LAYOUT
   Two-panel layout for desktop (e.g., list + detail view).
   Stacks vertically on mobile.
   ============================================================================ */

.split-layout {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
}

@media (min-width: 1024px) {
    .split-layout {
        flex-direction: row;
        gap: var(--space-lg);
    }
}

.split-layout__sidebar {
    width: 100%;
}

@media (min-width: 1024px) {
    .split-layout__sidebar {
        width: 320px;
        flex-shrink: 0;
    }
}

.split-layout__main {
    flex: 1;
    min-width: 0; /* Prevent flex children from overflowing */
}


/* ============================================================================
   15. UTILITY LAYOUT HELPERS
   ============================================================================ */

/* Flexbox helpers */
.flex {
    display: flex;
}

.flex-col {
    display: flex;
    flex-direction: column;
}

.flex-center {
    display: flex;
    align-items: center;
    justify-content: center;
}

.flex-between {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.flex-wrap {
    flex-wrap: wrap;
}

.flex-1 {
    flex: 1;
}

.gap-xs { gap: var(--space-xs); }
.gap-sm { gap: var(--space-sm); }
.gap-md { gap: var(--space-md); }
.gap-lg { gap: var(--space-lg); }

/* Alignment helpers */
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.items-end { align-items: flex-end; }
.justify-center { justify-content: center; }
.justify-end { justify-content: flex-end; }

/* Spacing helpers */
.mt-sm { margin-top: var(--space-sm); }
.mt-md { margin-top: var(--space-md); }
.mt-lg { margin-top: var(--space-lg); }
.mb-sm { margin-bottom: var(--space-sm); }
.mb-md { margin-bottom: var(--space-md); }
.mb-lg { margin-bottom: var(--space-lg); }

/* Width helpers */
.w-full { width: 100%; }

/* Display helpers */
.hidden { display: none; }

@media (min-width: 640px) {
    .hidden-tablet { display: none; }
    .show-tablet { display: block; }
}

@media (min-width: 1024px) {
    .hidden-desktop { display: none; }
    .show-desktop { display: block; }
}

@media (max-width: 1023px) {
    .hidden-mobile { display: none; }
}