:root {
    --md-font-family: 'M PLUS Rounded 1c', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --md-display-font: 'Pacifico', cursive;
    --md-primary: #000000;          
    --md-on-primary: #FFFFFF;
    --md-surface: #FFFFFF;
    --md-surface-variant: #F5F5F5;
    --md-background: #FFFFFF;
    --md-on-background: #000000;
    --md-outline: #E0E0E0;
    --md-outline-variant: #F0F0F0;
    --md-on-surface-variant: #666666;
    --app-radius-lg: 28px;  
    --app-radius-md: 16px;
    --app-radius-sm: 12px;
    --app-elevation-1: 0 4px 12px rgba(0,0,0,0.05);
    --app-elevation-2: 0 8px 24px rgba(0,0,0,0.12);
    --app-elevation-modal: 0 -4px 32px rgba(0,0,0,0.15);
}

body.dark {
    --md-primary: #FFFFFF;
    --md-on-primary: #000000;
    --md-surface: #121212;
    --md-surface-variant: #1E1E1E;
    --md-background: #000000;
    --md-on-background: #FFFFFF;
    --md-outline: #333333;
    --md-outline-variant: #222222;
    --md-on-surface-variant: #AAAAAA;
    --app-elevation-1: 0 4px 12px rgba(0,0,0,0.5);
    --app-elevation-2: 0 8px 24px rgba(0,0,0,0.6);
    --app-elevation-modal: 0 -4px 32px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html { scroll-behavior: smooth; }

body {
    font-family: var(--md-font-family);
    background-color: var(--md-background);
    color: var(--md-on-background);
    width: 100%;
    min-height: 100vh;
    overflow-x: hidden;
}

h1, h2, h3, h4, h5, h6 { font-weight: 700; letter-spacing: -0.02em; }
a { text-decoration: none; color: inherit; }
button { font-family: inherit; }

/* HEADER */
.app-bar {
    position: fixed; top: 0; left: 0; right: 0; height: 70px;
    background-color: var(--md-background);
    border-bottom: 1px solid var(--md-outline);
    z-index: 1000; display: flex; align-items: center; padding: 0 16px;
    /* REMOVED ANIMATION FOR PERFORMANCE */
}

.app-bar-container { width: 100%; max-width: 1400px; margin: 0 auto; display: flex; justify-content: space-between; align-items: center; }
.logo-text { font-family: var(--md-display-font); font-size: 1.8rem; color: var(--md-on-background); padding-bottom: 4px; }
.app-bar-actions { display: flex; gap: 8px; align-items: center; }

.icon-button {
    background: transparent; border: none; color: var(--md-on-surface-variant);
    width: 44px; height: 44px; cursor: pointer; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
}
.icon-button .material-icons-round { font-size: 24px; }

/* CATEGORY BAR */
.category-bar {
    position: sticky; top: 70px; z-index: 99;
    background-color: var(--md-background);
    padding: 8px 0; border-bottom: 1px solid var(--md-outline);
}
.category-scroll-wrapper { overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.category-scroll-wrapper::-webkit-scrollbar { display: none; }
.category-scroll { display: flex; gap: 8px; padding: 0 16px; width: max-content; margin: 0 auto; }

.category-button {
    font-family: var(--md-font-family); font-size: 0.85rem; font-weight: 600;
    padding: 6px 16px; border-radius: 100px; border: 1px solid var(--md-outline);
    background-color: var(--md-surface); color: var(--md-on-surface-variant);
    cursor: pointer; white-space: nowrap; user-select: none;
}
.category-button.active {
    background-color: var(--md-primary); color: var(--md-on-primary);
    border-color: var(--md-primary);
}

/* MAIN CONTENT */
.main-content {
    padding-top: 80px; padding-bottom: 100px;
    max-width: 1400px; margin: 0 auto; width: 100%;
    /* CLS FIX: Ensure minimum height so footer stays down */
    min-height: 100vh; 
    /* REMOVED ANIMATION FOR PERFORMANCE */
}
.section { padding: 0 8px; }

/* GRID */
.wallpaper-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 8px; margin-top: 16px;
    /* CLS FIX: Reserve space */
    min-height: 500px; 
}

.wallpaper-card {
    position: relative; border-radius: var(--app-radius-md);
    overflow: hidden; background-color: var(--md-surface-variant);
    aspect-ratio: 3/4; cursor: zoom-in;
    transform: translateZ(0); 
    /* Reduced transition overhead */
    transition: transform 0.2s ease;
}
.wallpaper-card:hover { transform: scale(1.02); z-index: 2; }
.wallpaper-card img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* BOTTOM NAV */
.bottom-nav {
    position: fixed; bottom: 0 !important; left: 0 !important; right: 0 !important;
    width: 100% !important; margin: 0 !important;   
    background-color: var(--md-surface);
    border-top: 1px solid var(--md-outline);
    border-radius: 24px 24px 0 0;
    box-shadow: 0 -4px 16px rgba(0,0,0,0.06);
    display: flex; align-items: center; justify-content: space-evenly; 
    height: auto; padding: 12px 0; padding-bottom: max(12px, env(safe-area-inset-bottom));
    z-index: 1001; transform: none !important; opacity: 1 !important; visibility: visible !important;
}
.bottom-nav-link {
    display: flex; flex-direction: row; align-items: center; justify-content: center;
    text-decoration: none; color: var(--md-on-surface-variant); 
    padding: 8px 16px; border-radius: 50px; background: transparent; min-width: 64px; 
}
.bottom-nav-link .material-icons-round { font-size: 26px; display: block; }
.bottom-nav-link span:not(.material-icons-round) { 
    font-size: 0.85rem; font-weight: 700; max-width: 0; opacity: 0; 
    margin-left: 0; white-space: nowrap; overflow: hidden; 
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); 
}
.bottom-nav-link.active { background-color: var(--md-surface-variant); color: var(--md-primary); }
.bottom-nav-link.active span:not(.material-icons-round) { max-width: 100px; opacity: 1; margin-left: 8px; }

/* MODAL */
.wallpaper-modal {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.85); z-index: 2000;
    display: flex; align-items: flex-end; justify-content: center;
    opacity: 0; visibility: hidden; transition: opacity 0.3s ease;
}
.wallpaper-modal.active { opacity: 1; visibility: visible; }

.wallpaper-modal-container {
    background: var(--md-surface); width: 100%; max-width: 500px;
    height: auto; max-height: 92vh; border-top-left-radius: 28px; border-top-right-radius: 28px;
    transform: translateY(100%); transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
    display: flex; flex-direction: column; overflow: hidden;
    touch-action: none; will-change: transform;
}
.wallpaper-modal.active .wallpaper-modal-container { transform: translateY(0); }

.modal-drag-handle {
    width: 40px; height: 5px; background-color: var(--md-outline); 
    border-radius: 10px; margin: 12px auto 4px auto; flex-shrink: 0; opacity: 0.6;
}
.wallpaper-modal-header {
    padding: 16px 24px; display: flex; justify-content: space-between; align-items: center; 
    border-bottom: 1px solid var(--md-outline); flex-shrink: 0; background: var(--md-surface);
}
.wallpaper-modal-title { font-size: 1.1rem; font-weight: 700; color: var(--md-on-background); max-width: 75%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;display: flex; 
    align-items: center; /* Vertically centers icon and text */
    gap: 8px; }
.wallpaper-modal-close { background: var(--md-surface-variant); border: none; color: var(--md-on-surface); width: 36px; height: 36px; border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; }
.wallpaper-modal-content { background: var(--md-background); flex: 1; display: flex; justify-content: center; align-items: center; position: relative; overflow-y: auto; padding: 10px; }
.wallpaper-modal-image { width: 100%; height: auto; max-height: 70vh; object-fit: contain; border-radius: var(--app-radius-sm); }
.wallpaper-modal-footer { padding: 20px 24px; border-top: 1px solid var(--md-outline); background: var(--md-surface); flex-shrink: 0; padding-bottom: max(20px, env(safe-area-inset-bottom)); }
.wallpaper-modal-actions { display: flex; width: 100%; gap: 16px; }
.wallpaper-modal-button { flex: 1; padding: 14px; border-radius: 100px; border: none; font-weight: 700; cursor: pointer; font-size: 1rem; display: flex; align-items: center; justify-content: center; gap: 8px; }
.wallpaper-modal-button.primary { background: var(--md-primary); color: var(--md-on-primary); }
.wallpaper-modal-button.secondary { background: var(--md-surface-variant); color: var(--md-on-surface); }

/* SEARCH MODAL */
.search-modal {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.5); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
    z-index: 2000; display: none; align-items: flex-start; justify-content: center;
    padding-top: 15vh; opacity: 0; transition: opacity 0.3s ease;
}
.search-modal.active { display: flex; opacity: 1; }
.search-container { width: 90%; max-width: 600px; }
.search-input-container {
    background-color: var(--md-surface-variant); height: 64px; padding: 0 16px 0 24px; 
    border-radius: 32px; display: flex; align-items: center; 
    box-shadow: 0 4px 8px rgba(0,0,0,0.05); border: 1px solid transparent; 
    transform-origin: center top; animation: elasticSlideDown 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
@keyframes elasticSlideDown { 0% { opacity: 0; transform: translateY(-40px) scale(0.9); } 100% { opacity: 1; transform: translateY(0) scale(1); } }
.search-input-container .material-icons-round:first-child { color: var(--md-on-surface-variant); font-size: 28px; margin-right: 16px; }
.search-input-container input { flex: 1; border: none; outline: none; background: transparent; font-size: 1.35rem; font-weight: 500; color: var(--md-on-background); font-family: var(--md-font-family); height: 100%; }
.close-search { background: var(--md-on-surface-variant); border: none; color: var(--md-surface-variant); cursor: pointer; width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; opacity: 0; transform: scale(0.8); transition: all 0.3s ease; }
.search-modal.active .close-search { opacity: 1; transform: scale(1); transition-delay: 0.1s; }

/* MISC */
.loading-spinner { display: flex; justify-content: center; padding: 40px; width: 100%; }
.spinner { width: 48px; height: 48px; border-radius: 50%; border: 5px solid var(--md-surface-variant); border-top-color: var(--md-primary); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.refresh-button { position: fixed; bottom: 100px; right: 24px; width: 56px; height: 56px; border-radius: 16px; background: var(--md-primary); color: var(--md-on-primary); border: none; box-shadow: var(--app-elevation-2); cursor: pointer; z-index: 990; display: flex; align-items: center; justify-content: center; }
.material-icons-round.spinning { animation: spin 0.8s linear infinite; }
.about-container, .privacy-container { max-width: 800px; margin: 0 auto; padding: 0 16px; }
.about-card { background: var(--md-surface); border: 1px solid var(--md-outline); border-radius: var(--app-radius-lg); padding: 32px; margin-bottom: 24px;  }
.coffee-button { background-color: #FF813F; color: white; padding: 14px 28px; border-radius: 50px; text-decoration: none; font-weight: 700; display: inline-flex; align-items: center; gap: 10px; }
.app-footer { margin-top: auto; background-color: var(--md-surface); padding: 40px 20px 120px; border-top: 1px solid var(--md-outline); text-align: center; }
#noFavoritesMessage {
    order: -1;
}

/* MOBILE */
@media (max-width: 600px) {
    .wallpaper-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
    .app-bar { padding: 0 12px; }
    .main-content { padding-bottom: 90px !important; }
}