@media (max-width: 600px) {
    .hero-buttons { flex-direction: column; width: 100%; padding: 0 2rem; }
    .landing-btn { width: 100%; }
    .footer-inner { flex-direction: column; gap: 0.5rem; text-align: center; }
    .hero-headline { font-size: 2.8rem; }
}

@media (max-width: 900px) {
    /* Old landing media query removed */
}

@media (max-width: 800px) {
    .mobile-fab { display: flex; }
    /* Hide FAB when modal is active to prevent clashing UI */
    .modal-overlay.active ~ .mobile-fab { display: none !important; }
    
    .minimal-btn { display: none; } /* Hide the old button on mobile */

    /* Anti-zoom for iOS/Android (16px minimum) */
    input, textarea, select, .ql-editor, [contenteditable] {
        font-size: 16px !important;
    }

    .list-header { flex-direction: column; align-items: stretch; gap: 0.75rem; padding-bottom: 0.5rem; }
    .header-left, .search-container { max-width: 100%; width: 100%; height: 42px; }
    .header-right { margin-left: 0; justify-content: space-between; width: 100%; gap: 0.75rem; align-items: center; }
    
    .sort-dropdown-container { flex: 1; }
    .sort-trigger { width: 100%; height: 42px; justify-content: space-between; background: rgba(128,128,128,0.05); border-radius: 8px; }
    
    .mobile-sidebar-toggle { display: flex; align-items: center; }
    .sidebar { 
        position: fixed; left: 0; top: 0; bottom: 0; z-index: 2000;
        height: 100dvh; max-height: 100dvh; width: 40vw !important; min-width: 260px;
        background: var(--bg-main); border-right: 1px solid var(--border-color);
        transform: translateX(-100%); transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        display: flex; /* Override display:none cases if any */
    }
    .sidebar.active { transform: translateX(0); box-shadow: 20px 0 50px rgba(0,0,0,0.1); }
    
    .app-layout { grid-template-columns: 1fr; padding: 0; }
    .top-nav { padding: 0 1rem; }
    .app-main { padding: 1rem; }
    
    /* Bottom Sheet Modals */
    .modal-card { 
        position: fixed; bottom: 0; left: 0; right: 0; margin: 0; 
        max-width: 100%; border-radius: 20px 20px 0 0; 
        max-height: 85vh; transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    }
    .modal-overlay.active .modal-card { transform: translateY(0); }
    
    .pricing-grid { grid-template-columns: 1fr; gap: 1rem; }
    .todo-item { padding: 0.7rem 0.5rem; }
    
    .list-header { 
        flex-direction: row !important; 
        align-items: center !important; 
        gap: 0.5rem !important; 
        padding: 0.5rem 0 !important;
    }
    /* Hide sort/completed when search is active on mobile */
    .list-header:has(.search-container.active) .header-right { display: none !important; }
    .list-header:has(.search-container.active) .header-left { flex: 1; width: 100%; }
    .header-left { flex: 1; min-width: 0; }
    .search-container { width: 100% !important; height: 38px !important; }

    .header-right { width: auto !important; margin: 0 !important; }
    .sort-trigger { padding: 0 0.85rem !important; height: 38px !important; font-size: 0.75rem !important; min-width: 80px; }
    #current-sort-label { display: none; } /* Hide label on mobile to save space, icon only or compact */
    .sort-trigger::after { content: "Sort"; margin-left: 4px; }

    .notebook-covers-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.75rem !important;
        padding: 1rem 0 !important;
    }

    .modal-tabs { padding: 0 1.5rem; }
    .modal-body { min-height: auto; padding: 0 1.5rem 1.5rem; }
    .modal-footer { padding: 1.25rem 1.5rem; flex-direction: row; gap: 0.75rem; }
    .modal-footer button { flex: 1; height: 42px; font-size: 0.85rem; }

    /* Profile Refinement */
    .profile-info { display: none; }
    .navbar-profile { gap: 0; padding-left: 0.5rem; }

    /* Sidebar Spacing Refinement */
    .sidebar { padding: 1.5rem 1rem; gap: 2rem; }
    .sidebar-section h3 { margin-bottom: 0.75rem; font-size: 0.75rem; }
    .sidebar-item { padding: 0.75rem 0.75rem; gap: 0.8rem; font-size: 0.95rem; }
}

@media (max-width: 1100px) { .notes-grid.masonry { column-count: 3; } }

@media (max-width: 900px) { .notes-grid.masonry { column-count: 2; } }

@media (max-width: 650px) { .notes-grid.masonry { column-count: 1; } }

@media (max-width: 1100px) {
    .notes-grid, .notebook-hub-grid { 
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); 
        gap: 1.25rem; 
    }
}

@media (max-width: 900px) {
    .notes-grid, .notebook-hub-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 1rem; }
}

@media (max-width: 500px) {
    .notes-grid, .notebook-hub-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 0.75rem; }
    .notebook-card .notebook-title { font-size: 0.85rem; }
}

@media (max-width: 700px) {
    .note-excerpt {
        display: none;
    }
    .list-row-info {
        gap: 0.5rem;
    }
}

@media (max-width: 500px) {
    .notebook-list-item {
        padding: 1rem 1.25rem;
        gap: 1rem;
    }
    .notebook-list-item > svg {
        display: none;
    }
}

@media (max-width: 768px) {
    .nav-center {
        margin: 0 0.5rem;
    }
    .nav-brand {
        font-size: 0;
        gap: 0;
    }
}

@media (max-width: 800px) {
    .ce-block__content,
    .ce-toolbar__content {
        padding-left: 1.5rem !important;
        padding-right: 1.5rem !important;
    }
}

@media (max-width: 800px) {
    /* Library Hub Header */
    #notebook-list-header {
        flex-direction: column !important;
        align-items: center !important;
        gap: 1.25rem !important;
        padding-bottom: 2rem !important;
        text-align: center;
    }

    /* Internal Notebook View Header (The Cluttered one) */
    #notebook-view-header {
        flex-direction: row !important;
        flex-wrap: wrap !important;
        justify-content: space-between !important;
        align-items: center !important;
        gap: 0.75rem !important; /* Reduced from 1.25rem */
        padding: 0.5rem 1rem 1rem 1rem !important; /* Tightened top spacing */
    }

    #notebook-view-header #notebook-view-title {
        width: 100% !important;
        text-align: center !important;
        font-size: 1.6rem !important; /* Slightly smaller to feel tighter */
        margin-top: 0.5rem !important;
        margin-bottom: 0.25rem !important; /* Reduced from 0.5rem */
    }

    #notebook-view-header .header-left {
        width: auto !important;
        flex: 1 !important;
        justify-content: flex-start !important;
    }

    #notebook-view-header .header-right {
        width: auto !important;
        flex: 1 !important;
        justify-content: flex-end !important;
        margin-top: 0 !important;
    }

    /* Hide text labels on mobile to save space */
    #current-note-sort-label {
        display: none !important;
    }

    .note-sort-dropdown button {
        padding: 0 0.5rem !important;
        height: 38px;
    }

    /* Adjust Grid Cards for Mobile Height */
    .notes-grid .note-card.white-page {
        aspect-ratio: auto !important; /* Remove tall forced ratio */
        min-height: 180px;
        padding: 1rem !important;
    }
}

@media (max-width: 1100px) {
    .outside-counter {
        bottom: 1rem;
        left: 1rem;
    }
}

@media (max-width: 768px) {
    body.writing-focus-mode .modal-overlay.active {
        padding: 0 !important; /* Start flush after navbar */
    }

    .immersive-editor-card {
        width: 100% !important;
        height: 100% !important;
        height: 100dvh !important; /* Uses dynamic viewport height to fill gaps */
        border-radius: 20px !important; /* Maintain premium curves on mobile/tablet */
        max-width: none !important;
        box-shadow: none !important; /* Remove card physicality for flush look */
        margin: 0 !important;
    }
    
    .editor-header, .editor-status-row, .immersive-body {
        padding-left: 1.25rem !important;
        padding-right: 1.25rem !important;
    }

    .editor-header {
        padding-top: 2.25rem !important; /* Extra space below navbar/top edge */
    }
    
    .ql-editor {
        padding-left: 1.25rem !important;
        padding-right: 1.25rem !important;
    }

    #note-modal-title {
        font-size: 1.5rem;
    }

    #autosave-label {
        display: none !important;
    }

    .autosave-status {
        padding: 0.4rem !important; /* Circular look for mobile */
        width: 28px;
        height: 28px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Fixed Mobile List View */
    .notes-list-view .note-card.list-row {
        padding: 0.75rem 1rem !important;
        gap: 0.75rem !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }

    .list-row-left {
        gap: 0 !important;
    }

    .list-row-left .page-icon, 
    .list-row-left .note-excerpt {
        display: none !important;
    }

    .list-row-info {
        gap: 0 !important;
    }

    .list-row-info h3.note-title {
        min-width: 0 !important;
        font-size: 0.9rem !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    .list-row-right .note-time {
        font-size: 0.7rem !important;
        opacity: 0.6;
        white-space: nowrap;
    }
}
/* --- Additional Mobile Fixes --- */
@media (max-width: 800px) {
    /* Ensure vault list defaults to 2 columns on mobile */
    .vault-list.grid-view {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.75rem !important;
    }
    
    /* Ensure vault file actions are accessible */
    .vault-file-card .file-actions {
        opacity: 1 !important;
    }

    /* Prevent horizontal scroll on tasks */
    .todo-list-container {
        overflow-x: hidden !important;
    }
    .todo-item {
        flex-wrap: wrap;
    }

    /* Fix notebook grids to 2 columns on mobile */
    .notes-grid.masonry, .notebook-hub-grid {
        column-count: 2 !important;
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.75rem !important;
    }
    
    .notebook-covers-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 0.75rem !important;
    }

    /* Modals bottom sheet safe area */
    .modal-card {
        padding-bottom: max(1.5rem, env(safe-area-inset-bottom, 20px)) !important;
    }
    
    .sidebar-overlay {
        position: fixed;
        top: 0; left: 0; right: 0; bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 1999;
        opacity: 0;
        visibility: hidden;
        transition: all 0.3s ease;
    }
    .sidebar-overlay.active {
        opacity: 1;
        visibility: visible;
    }
}

/* --- Targeted Visual Bug Fixes --- */
@media (max-width: 800px) {
    /* 1. Fix Vault List View Overlap */
    .vault-list.list-view .vault-file-card {
        height: auto !important; /* Remove fixed 60px height to prevent text overlap */
        padding: 0.75rem 1rem !important;
    }
    .vault-list.list-view .file-meta {
        flex-wrap: wrap; /* Ensure long file metadata can wrap */
    }

    /* 2. Fix Search Results Overlap */
    .search-item.list-row {
        flex-direction: column !important;
        align-items: flex-start !important;
        padding: 1rem !important;
        height: auto !important;
    }
    .search-item .list-row-left {
        width: 100% !important;
        align-items: flex-start !important;
        gap: 0.75rem !important;
    }
    .search-item .list-row-info {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 0.35rem !important;
    }
    .search-item .todo-submeta-inline {
        white-space: normal !important;
        line-height: 1.4 !important;
        display: block !important; /* override inline */
    }
    .search-item .note-excerpt {
        white-space: normal !important;
        line-height: 1.4 !important;
    }
    .search-item .list-row-right {
        align-self: flex-end;
        margin-top: -30px; /* pull up the badge */
    }

    /* 3. Fix Task Long Titles & Tags */
    .todo-header-row {
        flex-wrap: wrap;
    }
    .todo-text {
        word-break: break-word; /* Ensure long URLs wrap */
        white-space: normal;
    }
    
    /* 4. Vault Grid 2 Columns Enforcement */
    #vault-list.vault-list.grid-view {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* 5. Hide Show Completed Text on Mobile */
    #completed-toggle-text {
        display: none !important;
    }
    .switch-container {
        padding: 0 0.5rem !important; /* reduce padding since there is no text */
    }
    .sidebar-collapse-btn {
        display: none !important;
    }
}
