/* Apply the tiling background using the CSS variables set on the body tag by PHP. */
body {
    /*
     * Support for a customizable tiling background image.
     * These CSS variables are set via an inline style on the body tag by PHP.
     */
    background-image: var(--background-image-url);
    background-size: var(--background-image-size, auto);
    background-repeat: repeat;
}

html {
    scroll-behavior: smooth;
}

/* New Page Container for two-column layout */
.page-container {
    /* display: flex; is removed as it conflicts with the fixed sidebar and dynamic margin logic. */
    max-width: 1800px;
    margin: 20px auto;
}

/* Filter Chips */
#filter-chips-container {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 20px;
}

.filter-chip {
    padding: 8px 16px;
    border: 1px solid var(--primary-color);
    background-color: var(--surface-color);
    color: var(--primary-color);
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.3s;
}

.filter-chip:hover {
    background-color: #f5f5f5;
    /* Keep specific hover color */
}

.filter-chip.active {
    background-color: var(--primary-color);
    color: var(--text-color-primary);
}

/* New Sidebar Navigation */
#category-sidebar {
    display: none !important;
    /* Hidden for now, remove if we want to use the sidebar */
    flex-shrink: 0;
    /* Prevent sidebar from shrinking */
    width: fit-content;
    /* Allow the sidebar to size to its content */
    max-width: 250px;
    /* But prevent it from becoming excessively wide */
    position: fixed;
    /* Fixed position relative to the viewport */
    left: 0;
    top: 140px;
    /* Position below the sticky top nav */
    height: calc(100vh - 160px - 80px);
    /* Adjust height to fill viewport below nav */
    overflow-y: auto;
    padding: 0 10px;
    /* Add horizontal padding */
    transition: transform 0.3s ease-in-out;
    /* Smooth slide-out animation */
    background-color: var(--surface-color);
    /* A slightly darker, distinct background */
    z-index: 999;
    /* Keep it above main content but below nav */
    border-top-right-radius: 8px;
    border-bottom-right-radius: 8px;
    /* Round the top right corner */
    /* Hide scrollbar for IE, Edge */
    -ms-overflow-style: none;
    /* Hide scrollbar for Firefox */
    scrollbar-width: none;
}

/* Hide scrollbar for Chrome, Safari and Opera */
#category-sidebar::-webkit-scrollbar {
    display: none;
}

#category-sidebar ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

#category-sidebar a {
    display: block;
    padding: 10px 15px;
    text-decoration: none;
    color: var(--text-color-primary);
    border-radius: 5px;
    transition: background-color 0.2s, color 0.2s;
}

#category-sidebar a:hover {
    background-color: #f0f0f0;
}

#category-sidebar a.active {
    background-color: var(--primary-color);
    color: var(--text-color-primary);
    font-weight: bold;
}

/* Styles for the new sidebar toggle button */
#sidebar-toggle {
    display: none !important;
    /* Hidden for now, remove if we want to use the sidebar */
    position: fixed;
    /* The 'left' property will now be set dynamically by JavaScript */
    top: 190px;
    transform: translateY(-50%);
    width: 25px;
    height: 50px;
    background-color: #555;
    color: var(--text-color-primary);
    border-radius: 0 5px 5px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1001;
    /* Above the sidebar */
    transition: left 0.3s ease-in-out, background-color 0.2s;
    font-size: 18px;
    line-height: 1;
}

/*
 * Increase the touch target of the sidebar toggle to make it easier to press on mobile.
 * This adds an invisible 10px area around the button that is still clickable.
 */
#sidebar-toggle::after {
    content: '';
    position: absolute;
    top: -10px;
    left: -10px;
    right: -10px;
    bottom: -10px;
}

#sidebar-toggle:hover {
    background-color: var(--primary-color);
}

/* Collapsed state for the sidebar */
.sidebar-collapsed #category-sidebar {
    transform: translateX(-100%);
    /* Slide the sidebar completely off-screen */
}

/* Adjust main content margin when sidebar is collapsed */
.sidebar-collapsed #main-content {
    margin-left: 0;
}

/* Adjust toggle button position and icon when collapsed */
.sidebar-collapsed #sidebar-toggle {
    left: 0;
}

.sidebar-collapsed #sidebar-toggle .arrow {
    transform: scaleX(-1);
    /* Flip the arrow icon */
}

/* Grid container for items within each category section */
.food-grid-container {
    display: grid;
    /* Use auto-fit and 1fr to allow items to grow and fill space, creating a left-aligned, responsive grid. */
    /* By changing 1fr to a fixed max-width, we prevent items from stretching too wide on large screens. */
    grid-template-columns: repeat(auto-fit, minmax(240px, 340px));
    gap: 20px;
    /* --- Collapse animation styles --- */
    overflow: hidden;
    max-height: fit-content;
    opacity: 1;
    transition: max-height 0.4s ease-in-out,
        opacity 0.3s ease-in-out,
        padding 0.4s ease-in-out,
        margin 0.4s ease-in-out;
}

/* New Main Content Area & Category Sections */
#main-content {
    /* flex-grow: 1; is removed as it was part of a previous flex layout and is now causing width constraints. */
    min-width: 0;
    /* Prevents flexbox overflow issues */
    /* The 'margin-left' property will now be set dynamically by JavaScript */
    /* Add significant padding to the bottom to allow the last category to be scrolled to the top of the viewport. */
    padding-bottom: 80vh;
    transition: margin-left 0.3s ease-in-out;
    /* Smooth transition for content shift */
    /* The promo banner is now a static element, so it will push the content down naturally.
       The previous padding-top is no longer needed. */
}

.category-section {
    margin-bottom: 40px;
    transition: margin-bottom 0.4s ease-in-out;
    /* The scroll-margin-top will now be set dynamically by JavaScript to account
       for the responsive height of the sticky navigation bar. */
}

.category-section h2 {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 0 15px;
    box-sizing: border-box;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    /* Remove tap highlight on mobile */
}

/* Floating category header - shown via JS when scrolling through a category */
#floating-category-header {
    display: none;
    /* Hidden by default, shown by JS */
    position: fixed;
    top: 80px;
    /* Below the fixed navbar */
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 40px);
    max-width: 1760px;
    background-color: var(--surface-color);
    padding: 12px 20px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    z-index: 999;
    /* Below navbar (1000) but above content */
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    box-sizing: border-box;
}

#floating-category-header .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#floating-category-header .category-name {
    font-size: 1.2em;
    font-weight: bold;
    color: var(--text-color-primary);
}

#floating-category-header .collapse-icon {
    border: solid var(--text-color-primary);
    border-width: 0 2px 2px 0;
    display: inline-block;
    padding: 4px;
    transform: rotate(45deg);
    transition: transform 0.3s ease;
}

.category-toggle-icon {
    border: solid var(--text-color-primary);
    border-width: 0 2px 2px 0;
    display: inline-block;
    padding: 4px;
    transform: rotate(45deg);
    /* Down arrow */
    transition: transform 0.3s ease;
}

.category-section.category-collapsed .category-toggle-icon {
    transform: rotate(-135deg);
    /* Up arrow */
}

.food-item {
    background-color: var(--surface-color);
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    text-align: center;
    position: relative;
    /* Needed for tooltip positioning */
    /* Remove padding from the card itself to allow the image to be flush with the edges. */
    /* The content will be wrapped in a div that has padding. */
    padding: 0;
}

.category-section.category-collapsed {
    /* Reduce the bottom margin when collapsed to tighten up the list of categories. */
    margin-bottom: 0;
}

.category-section.category-collapsed .food-grid-container {
    max-height: 0;
    /* Reduce the bottom margin when collapsed to tighten up the list of categories. */
    margin-bottom: 0;
    /* Also transition the padding for a smoother collapse */
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
    padding-top: 0;
    padding-bottom: 0;
    margin-bottom: 0;
    pointer-events: none;
    /* Prevent clicks when hidden */
}

.food-item img {
    width: 100%;
    /* Ensure the image takes up the full width of its container */
    height: auto;
    /* Allow the height to adjust proportionally */
    max-height: 250px;
    /* Set a maximum height to prevent images from becoming too large */
    object-fit: contain;
    /* Scale the image down to fit within the container without cropping */
    border-radius: 10px 10px 0 0;
    justify-content: center;
}

.food-item-content {
    /* Define base padding here instead of inline JS */
    padding: 20px;
}

.food-item.selected {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
}

/* Ensure text inside a selected item is readable */
.food-item.selected .food-item-content h3,
.food-item.selected .food-item-content p {
    color: white;
}

.search-container {
    display: flex;
    justify-content: center;
    margin: 20px 0;
}

#search-bar {
    width: 100%;
    /* Be flexible */
    padding: 10px;
    max-width: 300px;
    /* But not too wide */
    border: 1px solid #ccc;
    border-radius: 5px 0 0 5px;
    box-sizing: border-box;
    /* Ensure padding is included in the width calculation */
    font-size: 16px;
    /* Prevent iOS zoom on focus */
}

#search-button {
    padding: 10px 20px;
    background-color: var(--primary-color);
    color: var(--text-color-primary);
    border: none;
    border-radius: 0 5px 5px 0;
    cursor: pointer;
}

/* The .bottom-nav base styles are now in style.css */
/* Make selector more specific to only target the calculate button */
.bottom-nav #calculate-button {
    background-color: var(--primary-color);
    color: var(--text-color-primary);
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1.1em;
    /* The button now controls its own padding and width constraints */
    padding: 12px 20px;
    width: calc(100% - 20px);
    /* Full width minus horizontal margins/padding */
    max-width: 420px;
    box-sizing: border-box;
    font-family: var(--font-family);
}

.bottom-nav #calculate-button:hover {
    background-color: var(--primary-color-hover);
}

/* New styles for the food estimate card in the bottom nav */
.food-estimate-card {
    background-color: var(--surface-color);
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    padding: 10px 15px;
    text-align: center;
    flex-shrink: 0;
    /* Prevent the card from shrinking */
}

.food-estimate-card h3 {
    margin: 0 0 4px 0;
    font-size: 0.8em;
    color: var(--text-color-secondary);
    font-weight: normal;
}

.food-estimate-card p {
    margin: 0;
    font-size: 0.9em;
    color: var(--text-color-primary);
}

.food-estimate-card #food-estimate-value {
    font-weight: bold;
    font-size: 1.1em;
    color: var(--primary-color);
}

.age-btn {
    padding: 8px 16px;
    border: 2px solid var(--primary-color);
    background: var(--surface-color);
    color: var(--primary-color);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 1em;
    /* Ensure consistent font size */
    font-family: var(--font-family);
    /* Ensure consistent font family */
}

.age-btn--selected {
    background: var(--primary-color);
    color: var(--text-color-primary);
}

/* Helper Text and Info Chip */
.helper-text {
    text-align: center;
    margin: -10px 0 20px;
    font-size: 1.1em;
    color: var(--text-color-primary);
}

/* Toast Notification */
.toast {
    visibility: hidden;
    min-width: 250px;
    max-width: 90%;
    background-color: #333;
    color: var(--text-color-primary);
    text-align: center;
    border-radius: 8px;
    padding: 16px;
    position: fixed;
    z-index: 1002;
    /* Above modal overlay */
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    opacity: 0;
    transition: opacity 0.4s, visibility 0.4s;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.toast.show {
    visibility: visible;
    opacity: 1;
}

.info-chip {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #e0f7fa;
    color: #006064;
    padding: 10px 15px;
    border-radius: 20px;
    margin: 0 auto 20px;
    max-width: 300px;
    position: relative;
}

/* Home Screen Styles */
.home-container {
    max-width: 800px;
    margin: 40px auto;
    padding: 20px;
    background: var(--surface-color);
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    text-align: center;
}

.home-container h1 {
    font-size: 2em;
    margin-bottom: 20px;
}

.home-container .disclaimer {
    font-size: 1em;
    color: var(--text-color-secondary);
    margin-bottom: 30px;
}

.home-container .accept-button {
    display: inline-block;
    padding: 10px 20px;
    background-color: var(--primary-color);
    color: var(--text-color-primary);
    text-decoration: none;
    border-radius: 5px;
    font-size: 1.1em;
    transition: background-color 0.2s;
}

.home-container .accept-button:hover {
    background-color: var(--primary-color-hover);
}

header {
    display: flex;
    flex-direction: column;
    align-items: center;
    /* Center items horizontally */
    padding: 20px 0;
    /* Add some padding */
    gap: 15px;
    /* Space between header elements */
}

/* New styles for the top navigation bar */
#categories-main-nav {
    display: flex;
    /* Use flexbox for horizontal arrangement */
    justify-content: space-between;
    /* Space out items */
    align-items: center;
    /* Vertically align items */
    flex-wrap: wrap;
    /* Allow items to wrap */
    width: calc(100% - 40px);
    /* Match page-container's max-width minus padding */
    position: fixed;
    /* Fixed to viewport so navbar stays at top when scrolling */
    top: 10px;
    /* Small offset from very top */
    left: 50%;
    /* Position left edge at center */
    transform: translateX(-50%);
    /* Shift back by half width to center */
    margin: 0;
    /* Remove margin as it's not needed for fixed positioning */
    background-color: var(--surface-color);
    /* var(--text-color-primary) background */
    padding: 10px 20px;
    /* Padding inside the navigation bar */
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    /* Subtle shadow for depth */
    z-index: 1000;
    /* Ensure the nav bar stays on top of other content */
    border-radius: 8px;
    box-sizing: border-box;
    /* Ensures padding is included in the width calculation, preventing overflow. */
}

/* Adjust search container within the new nav bar */
#categories-main-nav .search-container {
    margin: 0;
    /* Remove previous margin */
    flex-grow: 1;
    /* Allow search to fill available space */
    min-width: 180px;
    /* Set a reasonable minimum width to keep it usable */
    max-width: 450px;
    /* And a maximum width for larger screens */
}

/* Adjust environmental toggle within the new nav bar */
#categories-main-nav .environmental-toggle {
    margin: 0;
    /* Remove previous margin */
}

/* Responsive styles for mobile devices */
@media (max-width: 768px) {
    #categories-main-nav {
        width: calc(100% - 60px);
        /* Adjust for smaller padding */
        padding: 10px 15px;
        /* Adjust padding */
        gap: 15px;
    }

    #category-sidebar {
        /* On mobile, constrain the sidebar to be narrower to save space. */
        max-width: 120px;
    }

    #category-sidebar a {
        /* Reduce font size and padding for a more compact look on mobile. */
        font-size: 0.9em;
        padding: 8px 12px;
    }

    /* On mobile, reduce the margin to accommodate the sidebar */
    /*
    #main-content {
        margin-left: 230px; // This is now handled by JS
    } */
    .sidebar-collapsed #main-content {
        margin-left: 10px;
        /* When collapsed, give it a small margin */
    }

    .page-container {
        margin: 10px 0;
        /* Remove horizontal margin to maximize width */
    }

    /* Make grid items smaller to fit 2 columns on mobile */
    .food-grid-container {
        grid-template-columns: repeat(auto-fit, minmax(130px, 150px));
        gap: 15px;
        /* This gap will remain, but padding is adjusted below */
        padding: 0 10px 15px 10px;
        /* Reduce horizontal padding */
    }

    .food-item img {
        height: 100px;
        /* Reduce image height */
    }

    .food-item-content {
        padding: 10px;
        /* Reduce padding inside cards */
    }

    .food-item-content h3 {
        font-size: 1em;
    }

    .food-item-content p {
        font-size: 0.85em;
    }

    .bottom-nav {
        justify-content: space-around;
        /* Give items more space on mobile */
    }
}

/* This block was inside the 768px query, but it's specific to smaller phones. */
/* It's now correctly placed to handle very small screens. */
@media (max-width: 550px) {
    #categories-main-nav {
        flex-direction: column;
        /* Stack items vertically */
        align-items: stretch;
        /* Make items take full width */
        gap: 15px;
        /* Adjust gap for vertical layout */
        padding: 15px;
    }
}

/* On very small screens, make the bottom nav items more flexible */
@media (max-width: 475px) {
    .bottom-nav #calculate-button {
        padding: 10px 15px;
        /* Reduce padding */
        font-size: 1em;
        /* Slightly smaller font */
    }

    .food-estimate-card {
        padding: 8px 12px;
        /* Reduce padding */
    }
}

/* On medium screens, center the wrapped nav items before they stack vertically */
@media (max-width: 991px) {
    #categories-main-nav {
        justify-content: center;
        gap: 20px;
        /* Add a gap for when items wrap and are on the same line */
    }
}

/* This class is added by JS when the tooltip is moved to the body for positioning. */
/* It ensures the tooltip is positioned relative to the viewport, not the trigger icon. */
.item-info-tooltip.tooltip-fixed {
    position: fixed;
    /* Reset properties that conflict with fixed positioning */
    bottom: auto;
    left: auto;
    right: auto;
    transform: none;
}

/* Tooltip for acronyms like PPB */
.acronym-tooltip {
    position: relative;
    cursor: help;
    border-bottom: 1px dotted var(--text-color-primary);
}

.acronym-tooltip .acronym-tooltip-text {
    visibility: hidden;
    width: 180px;
    background-color: var(--tool-tip-background-color);
    color: var(--tool-tip-text-color);
    text-align: center;
    border-radius: 6px;
    padding: 8px;
    position: absolute;
    z-index: 4;
    /* Higher than item-info-tooltip */
    bottom: 130%;
    left: 50%;
    transform: translateX(-50%);
    opacity: 0;
    transition: opacity 0.3s;
    font-size: 0.85rem;
    pointer-events: none;
}

.acronym-tooltip.show-tooltip .acronym-tooltip-text,
.acronym-tooltip:hover .acronym-tooltip-text {
    visibility: visible;
    opacity: 1;
}

#promo-banner {
    display: flex;
    justify-content: center;
    align-items: center;
    background-size: cover;
    background-position: center;
    color: var(--text-color-primary);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
    box-sizing: border-box;
    position: relative;
    width: 98%;
    margin-left: auto;
    margin-right: auto;
    aspect-ratio: 16 / 4;
    /* Maintains consistent proportions while scaling with page width */
    border-radius: 10px;
    box-shadow: none;
    overflow: hidden;
    margin-bottom: 20px;
    background-color: #e0e0e0;
    /* Placeholder color before image loads */
    container-type: inline-size;
}

.promo-banner {
    display: flex;
    justify-content: center;
    align-items: center;
    background-size: cover;
    background-position: center;
    color: var(--text-color-primary);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
    box-sizing: border-box;
    position: relative;
    width: 98%;
    margin-left: auto;
    margin-right: auto;
    aspect-ratio: 16 / 4;
    /* Maintains consistent proportions while scaling with page width */
    border-radius: 10px;
    box-shadow: none;
    overflow: hidden;
    padding: 10px;
    margin-bottom: 20px;
}

.promo-banner span {
    font-size: 1.2em;
    font-weight: bold;
}

/* Switched to ID selectors to match the HTML */
#top-nav-container {
    position: fixed;
    top: 2%;
    z-index: 995;
    min-width: calc(100% - 30px);
    width: calc(100% - 30px);
    /* Use percentage-based width for better cross-device stability */
    /* Add flex properties to center the nav bar */
    display: flex;
    justify-content: center;
    left: 50%;
    /* Position the left edge at the center */
    transform: translateX(-50%);
}

.top-nav-spacer {
    position: absolute;
    z-index: 1001;
    /* This element's primary purpose is to have its height set by JS */
    /* to push down the main content, so it needs minimal styling. */
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    /* background-color: tomato; /* Temporary color for debugging */
    /* height is set dynamically by JS */
    width: 100%;
}