:root {
    --z-nav: 50;
    --z-modal: 200;
}

body {
    margin: 0;
    font-family: var(--font-stack);
    background-color: var(--bg-color);
    color: var(--text-primary);
    overflow: hidden;
    line-height: 1.5;
    overscroll-behavior-x: none;
}

body.app-initializing {
    overflow: hidden;
}

#app {
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    min-height: 100vh;
    min-height: 100svh;
    min-height: 100dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: max(6.5rem, calc(5.5rem + var(--safe-area-bottom)));
}

.app-toast-shell {
    display: none;
}

#app.fullscreen-view {
    padding-bottom: 0;
}

#main-view-container,
#modal-content {
    position: relative;
    min-height: 100%;
}

.view {
    display: none;
    animation: fadeIn 0.2s ease;
}

.view.active {
    display: block;
}

.modal-wrapper {
    width: 100%;
    height: 100%;
    position: relative;
}

header {
    padding-top: max(var(--space-lg), var(--safe-area-top));
    padding-bottom: var(--space-lg);
    background: var(--bg-color);
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: flex-start;
    /* Aligned to top padding line */
    justify-content: center;
    padding-left: 56px;
    /* space for back button */
    padding-right: 56px;
    /* height: 64px; Removed to support env(safe-area-inset-top) variability */
}

header h1,
header h2 {
    margin: 0;
    text-align: center;
    font-size: var(--font-size-l);
    letter-spacing: -0.2px;
    height: 36px;
    line-height: 36px;
    /* Perfect vertical centering in the 36px box */
    font-weight: 700;
}

/* Default alignment for titles in views (not popups) */
.view header h1,
.view header h2 {
    text-align: left;
    justify-content: flex-start;
}

/* Centered alignment specifically for modal popups */
.modal-wrapper header h1,
.modal-wrapper header h2 {
    text-align: center;
    justify-content: center;
}

/* Back button — used in modal/popup headers */
.back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--card-bg-2, var(--card-bg));
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    flex-shrink: 0;
    margin-right: var(--space-sm);
    transition: background 0.18s, transform 0.15s;
    box-shadow: var(--shadow-sm);
}

.back-btn:active {
    transform: scale(0.93);
    background: var(--border-color);
}

h1 {
    margin: 0;
    font-size: 2.5rem;
    /* Increased from XL */
    font-weight: 800;
    letter-spacing: -0.8px;
}

h2 {
    font-size: 1.75rem;
    /* Increased */
    font-weight: 700;
    margin: 0 0 var(--space-md) 0;
    letter-spacing: -0.4px;
}

h3 {
    font-size: var(--font-size-md);
    font-weight: 600;
    margin: 0;
}

#loading {
    height: 100vh;
    height: 100dvh;
    display: none;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    text-align: center;
    background: var(--bg-color);
    position: fixed;
    inset: 0;
    z-index: 120;
    overflow: hidden;
    opacity: 1;
    transition: opacity 0.26s ease;
}

#loading.active {
    display: flex;
}

#loading.is-exiting {
    opacity: 0;
    pointer-events: none;
}

#loading h1 {
    margin: 0;
    font-size: clamp(1.7rem, 5.5vw, 2.2rem);
    font-weight: 700;
    letter-spacing: -0.3px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    position: relative;
    z-index: 1;
}

html[data-skip-welcome="1"] #loading h1 {
    display: none;
}

html[data-skip-welcome="1"] #loading .welcome-lottie {
    display: none;
}

.welcome-lottie {
    width: min(10rem, 64vw);
    height: min(10rem, 64vw);
    margin: 0 auto var(--space-lg);
    pointer-events: none;
}

#loading .welcome-lottie {
    position: absolute;
    left: 50%;
    bottom: max(var(--space-lg), var(--safe-area-bottom));
    transform: translateX(-50%);
    margin: 0;
}

.welcome-lottie[hidden] {
    display: none !important;
}

.welcome-lottie svg {
    width: 100%;
    height: 100%;
}

.welcome-lottie-login {
    width: min(9.5rem, 56vw);
    height: min(9.5rem, 56vw);
    margin-bottom: var(--space-md);
}

.app-logo {
    background-image: var(--logo-image);
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    margin: 0 auto;
}

.main-logo {
    width: 7.5rem;
    /* 120px */
    height: 7.5rem;
    margin-bottom: var(--space-lg);
}

.footer-logo {
    width: 1rem;
    /* 16px */
    height: 1rem;
    margin: 0;
    display: inline-block;
}

/* Navigation */
.nav-gradient-mask {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 120px;
    /* Reduced height */
    background: linear-gradient(to top, var(--bg-color) 0%, transparent 100%);
    /* Start solid color higher */
    pointer-events: none;
    z-index: 90;
    transition: transform 0.3s ease;
}

#bottom-nav {
    position: fixed;
    bottom: calc(24px + var(--safe-area-bottom));
    left: 50%;
    transform: translateX(-50%);
    width: 96%;
    /* Wider pill */
    max-width: 500px;
    height: 64px;
    /* Slightly taller for better spacing */
    background: var(--nav-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 40px;
    border: 1px solid var(--nav-border);
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    padding: 0 2px;
    z-index: 100;
    box-shadow: var(--shadow-lg);
    transition: transform 0.5s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.4s ease;
}

/* Hide on scroll class */
#bottom-nav.nav-hidden {
    transform: translate(-50%, 120%);
    opacity: 0;
}

.nav-gradient-mask.nav-hidden {
    transform: translateY(100%);
}

.nav-item {
    background: none;
    border: none;
    color: var(--text-secondary);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: var(--font-size-xs);
    /* Use responsive variable instead of fixed 10px */
    font-weight: 400;
    /* Regular weight */
    flex: 1;
    height: 100%;
    cursor: pointer;
    transition: color 0.2s;
    position: relative;
    border-radius: 36px;
    z-index: 1;
}

.nav-item svg {
    margin-bottom: 4px;
    /* Increased space between icon and text */
    width: 22px;
    height: 22px;
}

#quick-add-nav-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 4px;
}

#quick-add-nav-icon svg {
    width: 22px;
    height: 22px;
}

#quick-add-nav-btn {
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    overflow: visible;
}

.quick-add-nav-caret {
    position: absolute;
    top: 0.12rem;
    left: 50%;
    transform: translateX(-50%);
    width: 0.72rem;
    height: 0.5rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: rgba(92, 102, 125, 0.72);
    pointer-events: none;
}

.quick-add-nav-caret svg {
    width: 100%;
    height: 100%;
}

.nav-item span {
    letter-spacing: 0.3px;
    opacity: 0.9;
}

.quick-add-popover {
    position: fixed;
    transform: translate(-50%, -100%);
    padding: 0.625rem;
    min-width: min(22rem, calc(100vw - 1.5rem));
    background: var(--nav-bg);
    border: 1px solid var(--nav-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    z-index: 120;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.quick-add-preview {
    display: none;
    align-items: flex-start;
    gap: 0.75rem;
    margin-bottom: 0.625rem;
    padding: 0.75rem;
    border-radius: var(--radius-md);
    background: rgba(var(--accent-rgb), 0.08);
    border: 1px solid rgba(var(--accent-rgb), 0.12);
}

.quick-add-preview.is-visible {
    display: flex;
}

.quick-add-preview-media-wrap {
    width: 2.75rem;
    height: 2.75rem;
    flex-shrink: 0;
    border-radius: 0.9rem;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(128, 128, 128, 0.08);
}

.quick-add-preview-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.quick-add-preview-icon {
    width: 1.35rem;
    height: 1.35rem;
    color: var(--text-secondary);
}

.quick-add-preview-icon svg {
    width: 100%;
    height: 100%;
}

.quick-add-preview-copy {
    min-width: 0;
    flex: 1;
    padding-top: 0.04rem;
}

.quick-add-preview-title {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    font-size: 0.84rem;
    line-height: 1.2;
    font-weight: 700;
    color: var(--text-primary);
}

.quick-add-preview-calories {
    flex-shrink: 0;
    text-align: right;
    color: var(--text-primary);
    padding-top: 0.04rem;
}

.quick-add-preview-calories strong {
    display: block;
    font-size: 1rem;
    line-height: 1;
}

.quick-add-preview-calories span {
    font-size: 0.66rem;
    color: var(--text-secondary);
    text-transform: none;
    letter-spacing: 0.08em;
}

.quick-add-popover-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.625rem;
}

.quick-add-popover-column {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
}

.quick-add-popover-title {
    padding: 0 0.75rem 0.2rem;
    color: var(--text-secondary);
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

body.quick-add-gesture-active,
body.quick-add-gesture-active #app {
    touch-action: none;
}

.quick-add-option {
    padding: 0.55rem 0.75rem;
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
    transition: background 0.15s ease, color 0.15s ease;
}

.quick-add-option > span:last-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.quick-add-option-icon {
    width: 1rem;
    height: 1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    opacity: 0.85;
}

.quick-add-option-icon svg {
    width: 100%;
    height: 100%;
}

.quick-add-recent-thumb {
    color: var(--text-secondary);
    flex-shrink: 0;
}

.quick-add-recent-thumb-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.quick-add-popover-empty {
    padding: 0.55rem 0.75rem;
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
}

.quick-add-option.active {
    background: rgba(var(--accent-rgb), 0.16);
    color: var(--accent-color);
    font-weight: 600;
}

@media (max-width: 420px) {
    .quick-add-popover {
        min-width: min(19rem, calc(100vw - 1rem));
    }

    .quick-add-popover-grid {
        gap: 0.45rem;
    }
}

/* Dark mode specific navbar background */
[data-theme="dark"] #bottom-nav {
    background: #0D0D14;
    /* Darker than card-bg for contrast */
}

#nav-active-indicator {
    position: absolute;
    top: 4px;
    bottom: 4px;
    left: 0;
    width: 0;
    background: rgba(255, 255, 255, 0.72);
    box-shadow: inset 0 0 0 1px rgba(13, 13, 26, 0.06), 0 4px 14px rgba(15, 23, 42, 0.08);
    border-radius: 36px;
    z-index: 0;
    pointer-events: none;
    transform: translate3d(0, 0, 0);
    transition: transform 0.34s cubic-bezier(0.22, 0.8, 0.2, 1), width 0.34s cubic-bezier(0.22, 0.8, 0.2, 1), opacity 0.2s ease;
    opacity: 0;
}

#bottom-nav.indicator-ready #nav-active-indicator {
    opacity: 1;
}

.nav-item.active {
    color: var(--text-primary);
    font-weight: 600;
}

.nav-item.highlight {
    color: var(--accent-color);
}

[data-theme="dark"] #nav-active-indicator {
    background: var(--card-bg-2, rgba(128, 128, 128, 0.1));
    box-shadow: none;
}

/* Header Action Row */
.header-actions {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
}

.view-topbar {
    --topbar-button-size: 2.5rem;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: center;
    gap: 0.75rem;
    width: 100%;
    box-sizing: border-box;
    padding:
        max(0.7rem, var(--safe-area-top))
        max(1rem, var(--safe-area-left))
        0.75rem
        max(1rem, var(--safe-area-right));
    z-index: 30;
}

.view-topbar-solid {
    position: sticky;
    top: 0;
    background: rgba(242, 242, 247, 0.94);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid rgba(127, 127, 127, 0.08);
}

[data-theme="dark"] .view-topbar-solid {
    background: rgba(13, 13, 20, 0.92);
}

.view-topbar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    margin-bottom: 0;
    background: transparent;
    pointer-events: none;
    z-index: 35;
}

.view-topbar-leading,
.view-topbar-center,
.view-topbar-trailing {
    display: flex;
    align-items: center;
    min-width: 0;
}

.view-topbar-leading {
    justify-content: flex-start;
}

.view-topbar-center {
    justify-content: center;
}

.view-topbar-trailing {
    justify-content: flex-end;
}

.view-topbar-leading > *,
.view-topbar-center > *,
.view-topbar-trailing > * {
    pointer-events: auto;
}

.shell-chrome-btn,
#modal-close-btn {
    width: var(--topbar-button-size, 2.5rem);
    height: var(--topbar-button-size, 2.5rem);
    background: var(--nav-bg);
    border: 1px solid var(--nav-border);
    border-radius: 14px;
    color: var(--text-primary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    transition: background 0.18s ease, transform 0.15s ease, border-color 0.18s ease;
    box-shadow: var(--shadow-md);
    padding: 0;
}

#modal-close-btn {
    position: absolute;
    top: max(0.7rem, var(--safe-area-top));
    left: max(1rem, var(--safe-area-left));
    z-index: 35;
}

.shell-chrome-btn:active,
#modal-close-btn:active {
    transform: scale(0.94);
}

.modal-overlay.hide-close-btn #modal-close-btn {
    display: none !important;
}

/* Modal Overlay System */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    min-height: 100vh;
    min-height: 100svh;
    min-height: 100dvh;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    z-index: var(--z-modal);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s;
}

.modal-overlay.active {
    opacity: 1;
    visibility: visible;
}

/* Full screen modal content */
.modal-overlay #modal-content {
    width: 100%;
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    min-height: 100vh;
    min-height: 100svh;
    min-height: 100dvh;
    background: var(--bg-color, #F2F2F7);
    overflow-y: auto;
    position: relative;
    -webkit-overflow-scrolling: touch;
}

/* Hide internal close buttons when in modal to avoid duplication with generic X */
.modal-overlay .header-actions .btn-circle {
    display: none !important;
}

/* Utility Classes */
.d-none {
    display: none;
}

.text-center {
    text-align: center;
}

.text-right {
    text-align: right;
}

.w-100 {
    width: 100%;
}

.mb-xs {
    margin-bottom: var(--space-xs);
}

.mb-sm {
    margin-bottom: var(--space-sm);
}

.mb-md {
    margin-bottom: var(--space-md);
}

.mb-lg {
    margin-bottom: var(--space-lg);
}

.mb-xl {
    margin-bottom: var(--space-xl);
}

.mt-xs {
    margin-top: var(--space-xs);
}

.mt-sm {
    margin-top: var(--space-sm);
}

.mt-md {
    margin-top: var(--space-md);
}

.mt-lg {
    margin-top: var(--space-lg);
}

.mt-xl {
    margin-top: var(--space-xl);
}

.p-lg {
    padding: var(--space-lg);
}

.flex-center {
    display: flex;
    align-items: center;
    justify-content: center;
}

.flex-col {
    display: flex;
    flex-direction: column;
}

.justify-between {
    justify-content: space-between;
}


/* Desktop Responsive Layout */
@media (min-width: 768px) {
    body {
        overflow: auto;
        /* Allow full page scroll */
    }

    #app,
    .modal-wrapper {
        width: 100%;
        max-width: 900px;
        margin: 0 auto;
        position: relative;
        height: auto;
        /* Remove fixed height constraint */
        overflow-y: visible;
        /* Prevent inner scroll area */
    }

    .modal-wrapper {
        height: 100%;
    }

    .modal-overlay #modal-content {
        height: 100%;
        overflow-y: auto;
    }

    #bottom-nav {
        max-width: 900px;
        margin: 0 auto;
    }

    .view-topbar {
        padding-left: max(1.25rem, var(--safe-area-left));
        padding-right: max(1.25rem, var(--safe-area-right));
    }
}
